#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e6 + 999; n vector<int> v[maxn]; n vector<pair<int, int> > g[maxn]; n int fa[maxn], dep[maxn], siz[maxn], Top[maxn], son[maxn], dfn[maxn], clk; n void dfs1(int x) { n   siz[x] = 1; n   dfn[x] = ++clk; n   for (int y : v[x]) { n     if (y == fa[x]) continue; n     fa[y] = x; n     dep[y] = dep[x] + 1; n     dfs1(y); n     siz[x] += siz[y]; n     if (siz[y] > siz[son[x]]) son[x] = y; n   } n } n void dfs2(int x, int topf) { n   Top[x] = topf; n   if (son[x]) dfs2(son[x], topf); n   for (int y : v[x]) { n     if (y != fa[x] && y != son[x]) dfs2(y, y); n   } n } n int LCA(int x, int y) { n   while (Top[x] ^ Top[y]) n     if (dep[Top[x]] < dep[Top[y]]) n       y = fa[Top[y]]; n     else n       x = fa[Top[x]]; n   return dep[x] > dep[y] ? y : x; n } n int n, st[maxn], V[maxn], imp[maxn], top, a[maxn], s[maxn]; n bool cmp(int x, int y) { return dfn[x] < dfn[y]; } n inline int dis(int x, int y) { n   int lca = LCA(x, y); n   return dep[x] + dep[y] - 2 * dep[lca]; n } n void add(int x, int y) { n   int t = dis(x, y); n   g[x].push_back(make_pair(y, t)); n   g[y].push_back(make_pair(x, t)); n } n void insert(int x) { n   int lca = LCA(x, s[top]); n   while (top > 1 && dfn[s[top - 1]] >= dfn[lca]) add(s[top - 1], s[top]), --top; n   if (lca != s[top]) add(lca, s[top]), s[top] = lca; n   s[++top] = x; n } n struct node { n   int x, t, id, d, fl; n   friend bool operator<(node a, node b) { n     return a.t == b.t ? (a.id == b.id ? a.d > b.d : a.id < b.id) : a.t < b.t; n   } n } Dis[maxn]; n struct nd { n   node first; n   int second; n   friend bool operator<(nd x, nd y) { return y.first < x.first; } n }; n priority_queue<nd> q; n int vis[maxn]; n vector<int> tmp; n void dfs3(int x, int faa) { n   tmp.push_back(x); n   for (auto now : g[x]) { n     int y = now.first; n     if (y == faa) continue; n     dfs3(y, x); n   } n } n int n1, n2; n node To(int x, pair<int, int> y) { n   node ret = Dis[x]; n   if (y.second <= ret.d) { n     ret.d -= y.second; n     return ret; n   } n   int dist = y.second - ret.d; n   ret.t += (dist + V[ret.id] - 1) / V[ret.id]; n   ret.d = (V[ret.id] * ((dist + V[ret.id] - 1) / V[ret.id]) - dist); n   return ret; n } n void dj() { n   while (q.size()) q.pop(); n   for (int i = (1); i <= (n1); ++i) n     Dis[st[i]] = node{st[i], 0, i, 0, 1}, q.push(nd{Dis[st[i]], st[i]}); n   while (q.size()) { n     int x = q.top().second; n     q.pop(); n     if (vis[x]) continue; n     vis[x] = 1; n     for (auto y : g[x]) { n       node t = To(x, y); n       if ((!Dis[y.first].fl) || t < Dis[y.first]) n         Dis[y.first] = t, q.push(nd{Dis[y.first], y.first}); n     } n   } n   for (int i = (1); i <= (n2); ++i) cout << Dis[imp[i]].id <<    ; n   cout << endl; n } n void init() { n   top = 0; n   for (int x : tmp) g[x].clear(), vis[x] = 0, Dis[x] = node{0, 0, 0, 0}; n   tmp.clear(); n } n int main() { n   cin >> n; n   for (int i = (1); i <= (n - 1); ++i) { n     int x, y; n     cin >> x >> y; n     v[x].push_back(y); n     v[y].push_back(x); n   } n   dfs1(1); n   dfs2(1, 1); n   int q; n   cin >> q; n   while (q--) { n     cin >> n1 >> n2; n     for (int i = (1); i <= (n1); ++i) cin >> st[i] >> V[i], a[i] = st[i]; n     for (int i = (1); i <= (n2); ++i) cin >> imp[i], a[i + n1] = imp[i]; n     sort(a + 1, a + 1 + n1 + n2); n     int len = unique(a + 1, a + 1 + n1 + n2) - a - 1; n     sort(a + 1, a + 1 + len, cmp); n     s[top = 1] = 1; n     for (int i = (1); i <= (len); ++i) n       if (a[i] != 1) insert(a[i]); n     while (top > 1) add(s[top - 1], s[top]), --top; n     dj(); n     dfs3(1, 0); n     init(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, c; n   cin >> a >> b >> c; n   map<int, vector<int> > vec; n   for (int i = 0; i < a; i++) { n     int g; n     cin >> g; n     vec[g % c].push_back(g); n   } n   for (int i = 0; i < c; i++) { n     if (vec[i].size() >= b) { n       cout <<  Yes  << endl; n       for (int j = 0; j < b; j++) { n         cout << vec[i][j]; n         if (j != b - 1) { n           cout <<    ; n         } else { n           cout << endl; n         } n       } n       return 0; n     } n   } n   cout <<  No  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const double EPS = 1e-4; n const long double PI = acos((long double)-1.0); n const long long int MOD = 1e9 + 7; n long long int powmod(long long int a, long long int b, long long int mod) { n   long long int res = 1; n   a %= mod; n   for (; b; b >>= 1) { n     if (b & 1) res = res * a % mod; n     a = a * a % mod; n   } n   return res; n } n const int debug = 1; n template <typename T> n void DBG(vector<T> v) { n   if (debug) { n     for (T t : v) cout << t <<     ; n     cout << endl; n   } n } n template <typename T> n void DBG(T t) { n   if (debug) cout << t << endl; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.precision(10); n   cout << fixed; n   string s; n   cin >> s; n   vector<int> counts(26, 0); n   for (char c : s) { n     counts[c -  a ]++; n   } n   vector<char> result(s.size(),  . ); n   for (int prime = 2; prime <= s.size(); prime++) { n     bool is_prime = true; n     for (int divisor = 2; divisor * divisor <= prime; divisor++) { n       if (prime % divisor == 0) { n         is_prime = false; n         break; n       } n     } n     if (is_prime) { n       set<char> se; n       for (int i = prime; i <= s.size(); i += prime) { n         for (int divisor = 2; divisor * divisor <= i; divisor++) { n           if (i % divisor == 0) se.insert(result[divisor - 1]); n         } n       } n       se.erase( . ); n       if (se.size() > 1) { n         cout <<  NO  << endl; n         return 0; n       } n       char c; n       if (se.size() == 1) { n         char c = *se.begin(); n       } else { n         int m = 0; n         for (int k = 0; k < (26); ++k) { n           if (counts[m] < counts[k]) m = k; n         } n         c =  a  + m; n       } n       for (int i = prime; i <= s.size(); i += prime) { n         if (result[i - 1] ==  . ) { n           if (!counts[c -  a ]) { n             cout <<  NO  << endl; n             return 0; n           } n           counts[c -  a ]--; n           result[i - 1] = c; n         } n       } n     } n   } n   for (int i = 0; i < (26); ++i) { n     if (counts[i]) result[0] =  a  + i; n   } n   DBG( YES ); n   DBG(string(result.begin(), result.end())); n } n


#include <bits/stdc++.h> n using ll = long long; n using namespace std; n void debugi(vector<int>& vec) { n   int n = (int)vec.size(); n   for (int i = 0; i < n; i++) { n     cout << vec[i] <<    ; n   } n   cout << endl; n } n void debugl(vector<ll>& vec) { n   int n = (int)vec.size(); n   for (int i = 0; i < n; i++) { n     cout << vec[i] <<    ; n   } n   cout << endl; n } n void debugmap(map<int, int>& mp) { n   for (auto x : mp) { n     cout << x.first <<     << x.second <<   ,  ; n   } n   cout << endl; n } n void debugset(set<int>& st) { n   for (auto x : st) { n     cout << x <<    ; n   } n   cout << endl; n } n void debugparri(vector<pair<int, int>>& arr) { n   for (int i = 0; i < (int)arr.size(); i++) { n     cout <<  [  << arr[i].first <<   :   << arr[i].second <<  ]  n          <<    ; n   } n   cout << endl; n } n void debugpi(pair<int, int>& pi) { n   cout << pi.first <<     << pi.second << endl; n } n void debugq(queue<int> q) { n   while (!q.empty()) { n     cout << q.front() <<    ; n     q.pop(); n   } n   cout << endl; n } n ll mod = 1000000007; n bool check(int n, int i, int j) { n   if ((0 <= i & i < n) && (0 <= j & j < n)) { n     return true; n   } n   return false; n } n int main(int argc, const char* argv[]) { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t; n   cin >> t; n   for (int tt = 0; tt < t; tt++) { n     int n; n     cin >> n; n     vector<int> arr(n); n     for (int i = 0; i < n; i++) { n       cin >> arr[i]; n     } n     vector<int> pre(n), suf(n); n     int s = 0; n     for (int i = 0; i < n; i++) { n       if (arr[i] != i + 1) { n         s += 1; n       } n       pre[i] = s; n     } n     s = 0; n     for (int i = n - 1; i >= 0; i--) { n       if (arr[i] != i + 1) { n         s += 1; n       } n       suf[i] = s; n     } n     int cnt = 0; n     for (int i = 0; i < n; i++) { n       if (pre[i] != 0 && suf[i] != 0 && arr[i] == i + 1) { n         cnt += 1; n       } n     } n     if (cnt == 0) { n       if (s == 0) { n         cout << 0 << endl; n         continue; n       } else { n         cout << 1 << endl; n         continue; n       } n     } n     cout << 2 << endl; n     continue; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n const long long N = 200; n long long c[N][N], dp[N][N * N + N], g[N][N]; n long long modp(long long a, long long b) { n   if (b == 0) return 1; n   long long u = modp(a, b / 2); n   u = (u * u) % mod; n   if (b & 1) return (a * u) % mod; n   return u; n } n int32_t main() { n   long long n, m, o; n   cin >> n >> m >> o; n   for (long long i = 0; i < N; i++) c[i][0] = dp[i][0] = 1; n   for (long long i = 1; i < N; i++) n     for (long long j = 1; j <= i; j++) n       c[i][j] = (c[i - 1][j] + c[i - 1][j - 1]) % mod; n   for (long long i = 1; i <= n; i++) n     for (long long j = 0; j <= n; j++) g[i][j] = modp(c[n][j], (m + n - i) / n); n   for (long long i = 1; i <= n; i++) n     for (long long j = 1; j <= n * i; j++) n       for (long long k = max(j - n, 0LL); k <= j; k++) { n         dp[i][j] = (dp[i][j] + dp[i - 1][k] * g[i][j - k]) % mod; n       } n   cout << dp[n][o] <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1LL * 1001 * 1001 * 1001 * 1001 * 1001 * 1001; n const int N = 10000; n struct edge { n   long long a, b, cap, flow; n   edge(long long a, long long b, long long cap) n       : a(a), b(b), cap(cap), flow(0) {} n   long long cap_to(long long u) { return u == b ? cap - flow : flow; } n   long long another(long long u) { return u == b ? a : b; } n   void add_flow(long long u, long long first) { n     flow += (u == b ? first : -first); n   } n }; n struct dinic_graph { n   vector<edge> e; n   vector<long long> g[N]; n   long long pos[N], d[N], p[N]; n   void add_edge(long long a, long long b, long long c) { n     g[a].push_back((int)e.size()); n     g[b].push_back((int)e.size()); n     e.push_back(edge(a, b, c)); n   } n   bool bfs(long long S, long long T) { n     d[S] = 1; n     queue<long long> q; n     q.push(S); n     while (q.size()) { n       long long u = q.front(); n       q.pop(); n       if (u == T) return 1; n       for (int i = 0; i < g[u].size(); i++) { n         long long to = e[g[u][i]].another(u); n         if (!d[to] && e[g[u][i]].cap_to(to)) { n           d[to] = d[u] + 1; n           q.push(to); n         } n       } n     } n     return 0; n   } n   bool dfs(long long u, long long t) { n     if (u == t) return 1; n     for (; pos[u] < g[u].size(); pos[u]++) { n       long long to = e[g[u][pos[u]]].another(u); n       if (d[to] == d[u] + 1 && e[g[u][pos[u]]].cap_to(to)) { n         p[to] = g[u][pos[u]]; n         if (dfs(to, t)) return 1; n       } n     } n     return 0; n   } n   long long dinic(long long S, long long T) { n     long long max_flow = 0; n     while (1) { n       memset(d, 0, sizeof(d)); n       if (!bfs(S, T)) return max_flow; n       memset(pos, 0, sizeof(pos)); n       while (dfs(S, T)) { n         long long cur_f = INF; n         for (long long u = T; u != S; u = e[p[u]].another(u)) n           cur_f = min(cur_f, e[p[u]].cap_to(u)); n         for (long long u = T; u != S; u = e[p[u]].another(u)) n           e[p[u]].add_flow(u, cur_f); n         max_flow += cur_f; n       } n     } n   } n }; n int n, m; n int x1[55], mezhdy[55], x2[55], nami[55]; n vector<int> x, y; n dinic_graph g; n bool in(double x, double y, int pos) { n   return x >= min(x1[pos], x2[pos]) && x <= max(x1[pos], x2[pos]) && n          y >= min(mezhdy[pos], nami[pos]) && y <= max(mezhdy[pos], nami[pos]); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     cin >> x1[i] >> mezhdy[i] >> x2[i] >> nami[i]; n     x2[i]++; n     nami[i]++; n     x.push_back(x1[i]); n     x.push_back(x2[i]); n     y.push_back(mezhdy[i]); n     y.push_back(nami[i]); n   } n   sort(x.begin(), x.end()); n   sort(y.begin(), y.end()); n   x.erase(unique(x.begin(), x.end()), x.end()); n   y.erase(unique(y.begin(), y.end()), y.end()); n   int S = 0; n   int T = (int)x.size() + (int)y.size() + 1; n   for (int i = 1; i < x.size(); i++) g.add_edge(S, i, x[i] - x[i - 1]); n   for (int i = 1; i < y.size(); i++) n     g.add_edge(i + x.size(), T, y[i] - y[i - 1]); n   for (int i = 1; i < x.size(); i++) n     for (int j = 1; j < y.size(); j++) { n       double xx = 1.0 * (x[i] + x[i - 1]) / 2; n       double yy = 1.0 * (y[j] + y[j - 1]) / 2; n       bool ok = 0; n       for (int p = 0; p < m; p++) n         if (in(xx, yy, p)) ok = 1; n       if (ok) g.add_edge(i, j + x.size(), INF); n     } n   cout << g.dinic(S, T); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, n; n   cin >> a >> b >> n; n   if (abs(a) + abs(b) <= n) { n     if ((n - abs(a) - abs(b)) % 2 == 0) n       cout <<  Yes ; n     else n       cout <<  No ; n   } else { n     cout <<  No ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, now; n int a[3005], b[3005]; n int vis[3005], f[3005], c[3005]; n int ok[3005]; n int findcir(int *a, int n) { n   int ret = 0; n   for (int i = 1; i <= n; i++) b[a[i]] = i; n   memset(vis, 0, sizeof(vis)); n   for (int i = 1; i <= n; i++) { n     if (vis[i]) continue; n     int j; n     ret++; n     for (j = i; !vis[j]; j = b[j]) { n       f[j] = ret; n       vis[j] = 1; n       c[ret]++; n     } n   } n   return ret; n } n void Swap(int x) { n   if (!x) return; n   for (int i = 1; i <= n; i++) n     for (int j = i + 1; j <= n; j++) n       if (f[i] == f[j]) { n         printf( %d %d  , i, j); n         x--; n         if (!x) return; n         swap(a[i], a[j]); n         findcir(a, n); n       } n } n void move(int x) { n   if (!x) return; n   for (int i = 1; i <= n; i++) n     for (int j = i + 1; j <= n; j++) n       if (f[i] != f[j]) { n         printf( %d %d  , i, j); n         x--; n         if (!x) return; n         swap(a[i], a[j]); n         findcir(a, n); n       } n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   now = findcir(a, n); n   now = n - now; n   scanf( %d , &m); n   printf( %d n , abs(now - m)); n   if (now > m) { n     Swap(now - m); n   } else n     move(m - now); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-9; n set<long long> s; n set<int> si; n set<long long>::iterator it; n vector<long long> v; n vector<int> vi; n int n, m, q, k; n int a[200005]; n int main() { n   int i, j, t; n   int w, h; n   ; n   scanf( %d%d , &w, &h); n   ; n   int u1, d1, u2, d2; n   ; n   scanf( %d%d , &u1, &d1); n   ; n   ; n   scanf( %d%d , &u2, &d2); n   ; n   for (i = h; i >= 0; i--) { n     w = w + i; n     if (d1 == i) w -= u1; n     if (d2 == i) w -= u2; n     w = max(0, w); n   } n   printf( %d n , w); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   for (int i = 0; i < t; i++) { n     int x; n     cin >> x; n     cout << x / 2 << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long int n, x; n vector<int> a; n long long int tarkib(long long int r, long long int n) { n   long long int a = 1; n   for (long long int i = n; i > (n - r); i--) a = a * i; n   for (long long int i = 2; i < r + 1; i++) a = a / i; n   return a; n } n int main() { n   long long int r; n   cin >> n; n   for (long long int i = 0; i < n; i++) { n     cin >> x; n     a.push_back(x); n   } n   sort(a.begin(), a.end()); n   long long int i; n   for (i = 3; i < n; i++) { n     if (a[i] > a[i - 1]) break; n   } n   i--; n   i--; n   if (a[0] == a[1]) { n     if (a[1] == a[2]) { n       r = 3; n       i += 2; n     } else { n       r = 1; n     } n   } else { n     if (a[1] == a[2]) { n       i++; n       r = 2; n     } else { n       r = 1; n     } n   } n   cout << tarkib(r, i); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(void) { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n, m; n   cin >> n >> m; n   if (n <= 450 and m == n * (n - 1) / 2) { n     cout <<  NO n ; n     return 0; n   } n   vector<set<int>> g(n + 1); n   for (int i = 0; i < m; ++i) { n     int u, v; n     cin >> u >> v; n     g[u].insert(v); n     g[v].insert(u); n   } n   int x = -1, y = -1; n   for (int i = 1; i <= n and x == -1; ++i) { n     if ((int)g[i].size() == n - 1) continue; n     for (int j = 1; j <= n and x == -1; ++j) { n       if (i == j) continue; n       if (g[i].find(j) == g[i].end()) tie(x, y) = tie(i, j); n     } n   } n   vector<int> v1, v2; n   int cur = 3; n   for (int i = 1; i <= n; ++i) { n     if (i == x) { n       v1.push_back(1); n       v2.push_back(1); n     } else if (i == y) { n       v1.push_back(2); n       v2.push_back(1); n     } else { n       v2.push_back(cur); n       v1.push_back(cur++); n     } n   } n   cout <<  YES n ; n   for (auto x : v1) cout << x <<    ; n   cout <<   n ; n   for (auto x : v2) cout << x <<    ; n } n


#include <bits/stdc++.h> n const long long mod = 1e9 + 7; n const long long maxn = 1e5 + 7; n const double pi = acos(-1); n using namespace std; n long long t, n, m; n long long hang; n long long weizhi; n long long temp; n long long four = 1; n long long weishu; n long long weishu2; n long long lie; n int ans[50]; n map<int, int> MP; n long long cal() { n   long long res = 0; n   long long a = 1; n   weishu2 = weishu; n   for (int i = weishu2; i >= 1; i--) { n     res += a * ans[i]; n     a <<= 2; n   } n   return res; n } n int main() { n   ios::sync_with_stdio(false); n   MP[1] = 2; n   MP[2] = 3; n   MP[3] = 1; n   cin >> t; n   while (t--) { n     memset(ans, 0, sizeof(ans)); n     weishu = 1; n     four = 1; n     cin >> n; n     hang = (n - 1) / 3 + 1; n     while (hang > four) { n       hang -= four; n       four <<= 2; n       weishu++; n     } n     if (n % 3 == 0) { n       lie = 3; n     } else { n       lie = n % 3; n     } n     weishu2 = weishu; n     temp = hang - 1; n     while (temp) { n       ans[weishu2--] = temp % 4; n       temp >>= 2; n     } n     ans[1] = 1; n     if (lie == 2) { n       for (int i = 1; i <= weishu; i++) { n         ans[i] = MP[ans[i]]; n       } n     } else if (lie == 3) { n       for (int i = 1; i <= weishu; i++) { n         ans[i] = MP[ans[i]]; n         ans[i] = MP[ans[i]]; n       } n     } n     cout << cal() << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = LLONG_MAX; n long long gcd(long long x, long long y) { n   if (y == 0) n     return x; n   else n     return gcd(y, x % y); n } n long long power(long long x, unsigned long long y) { n   long long res = 1; n   x = x % MOD; n   if (x == 0) return 0; n   while (y > 0) { n     if (y & 1) res = (res * x) % MOD; n     y = y >> 1; n     x = (x * x) % MOD; n   } n   return res; n } n void solve() { n   long long n, m, b, c, d, ans = 0, one = 0; n   cin >> n; n   long long a[n]; n   for (long long i = (long long)0; i < n; i++) { n     cin >> a[i]; n     if (a[i] >= 2 * one) { n       a[i] -= 2 * one; n       ans += one + a[i] / 3; n       one = a[i] % 3; n     } else { n       one += a[i] % 2; n       one -= a[i] / 2; n       ans += a[i] / 2; n     } n   } n   cout << ans <<   n ; n } n int32_t main() { n   std::ios::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   long long t = 1; n   while (t--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int t, n, hold; n   cin >> t; n   while (t--) { n     bool ans = false; n     cin >> n; n     long long int x; n     for (long long int i = 0; i < n; i++) { n       cin >> hold; n       if (i == 0) x = hold; n       if (x != hold) ans = true; n     } n     if (ans) n       cout << 1 << endl; n     else n       cout << n << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MAXN = 200005; n vector<long long> byk[MAXN]; n long long pangkat(long long x, long long y) { n   long long ret = 1; n   for (long long i = 1; i <= y; i++) ret *= x; n   return ret; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long n, a; n   cin >> n; n   for (long long no = 1; no <= n; no++) { n     cin >> a; n     for (long long i = 2; i * i <= a; i++) { n       long long pa = 0; n       while (a % i == 0) { n         pa++; n         a /= i; n       } n       if (pa) byk[i].push_back(pa); n     } n     if (a > 1) byk[a].push_back(1); n   } n   long long ans = 1; n   for (long long i = 1; i <= 200000; i++) { n     if (byk[i].size() > n - 2) { n       sort(byk[i].begin(), byk[i].end()); n       if (byk[i].size() == n - 1) n         ans *= pangkat(i, byk[i][0]); n       else n         ans *= pangkat(i, byk[i][1]); n     } n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n long long mpow(long long a, long long n, long long mod) { n   long long ret = 1; n   long long b = a; n   while (n) { n     if (n & 1) ret = (ret * b) % mod; n     b = (b * b) % mod; n     n >>= 1; n   } n   return (long long)ret; n } n using namespace std; n int n, m, s; n pair<int, int> bug[(int)(1e5 + 25)]; n int ANS[(int)(1e5 + 25)]; n pair<pair<int, int>, int> ch[(int)(1e5 + 25)]; n bool chk(int x) { n   priority_queue<pair<int, int> > pq; n   long long ans = 0; n   int last = m; n   for (int i = n; i >= 1;) { n     int j = i; n     while (j > 0 && ch[j].first.first >= bug[last].first) { n       pq.push(make_pair(-ch[j].first.second, ch[j].second)); n       j--; n     } n     i = j; n     if (j == 0) { n       break; n     } n     if (pq.empty()) { n       return 0; n     } n     pair<int, int> temp = pq.top(); n     pq.pop(); n     int dum = x; n     ans += (-temp.first); n     while (last > 0 && dum > 0) { n       ANS[bug[last].second] = temp.second; n       dum--; n       last--; n     } n     if (last <= 0) { n       break; n     } n   } n   while (last > 0) { n     if (pq.empty()) { n       return 0; n     } n     pair<int, int> temp = pq.top(); n     pq.pop(); n     int dum = x; n     ans += (-temp.first); n     while (last > 0 && dum > 0) { n       ANS[bug[last].second] = temp.second; n       dum--; n       last--; n     } n   } n   return (ans <= s); n } n void solve() { n   scanf( %d , &n); n   scanf( %d , &m); n   scanf( %d , &s); n   for (int i = 1; i <= m; i++) { n     scanf( %d , &bug[i].first); n     bug[i].second = i; n   } n   for (int i = 1; i <= n; i++) { n     scanf( %d , &ch[i].first.first); n   } n   for (int i = 1; i <= n; i++) { n     scanf( %d , &ch[i].first.second); n     ch[i].second = i; n   } n   sort(bug + 1, bug + m + 1); n   sort(ch + 1, ch + n + 1); n   int lo = 1, hi = m; n   while (lo < hi) { n     int mid = (lo + hi) >> 1; n     if (chk(mid)) { n       hi = mid; n     } else { n       lo = mid + 1; n     } n   } n   if (!chk(lo)) { n     printf( NO ); n     return; n   } n   printf( YES n ); n   for (int i = 1; i <= m; i++) { n     cout << ANS[i] <<    ; n   } n } n int main() { n   int t = 1; n   for (int i = 1; i <= t; i++) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dir4[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; n int dir8[8][2] = {{-1, 0}, {1, 0},  {0, 1}, {0, -1}, n                   {-1, 1}, {1, -1}, {1, 1}, {-1, -1}}; n int dir8h[8][2] = {{2, 1}, {-2, -1}, {-1, 2}, {1, -2}, n                    {1, 2}, {-2, 1},  {2, -1}, {-1, -2}}; n int setbit(int num, int index, int value) { n   return (value) ? (num | (1 << index)) : (num & ~(1 << index)); n } n bool getbit(int num, int index) { return ((num >> index) & 1) == 1; } n int cntbits(int num) { n   int cnt = 0; n   while (num) { n     if (num & 1) cnt++; n     num /= 2; n   } n   return cnt; n } n double eps = 1e-8; n const int N = 1e5 + 100; n vector<int> v[N]; n bool a[N], can[N]; n int root; n bool dfs(int node, int pa) { n   if (node != root && v[node].size() == 1) { n     if (a[node]) { n       can[node] = 1; n       return 1; n     } else { n       can[node] = 0; n       return 0; n     } n   } else { n     if (a[node]) n       can[node] = 1; n     else n       can[node] = 0; n     for (int i = 0; i < v[node].size(); i++) { n       if (v[node][i] != pa) { n         bool x = dfs(v[node][i], node); n         if (!x) can[node] = 0; n       } n     } n     if (a[node]) return 1; n     return 0; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   int n, p, c; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> p >> c; n     a[i] = c; n     if (p != -1) n       v[p].push_back(i), v[i].push_back(p); n     else n       root = i; n   } n   bool cc = dfs(root, -1); n   vector<int> res; n   for (int i = 1; i <= n; i++) { n     if (can[i]) res.push_back(i); n   } n   if (res.size() > 0) { n     for (int i = 0; i < res.size(); i++) cout << res[i] <<    ; n   } else n     cout << -1; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 51; n const long long inf = (long long)1e14; n const long long real_inf = (long long)1e18; n inline long long value(const pair<long long, long long>& task, long long t) { n   return task.first - t * task.second; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   vector<pair<long long, long long>> tasks(n); n   for (int i = 0; i < n; ++i) { n     cin >> tasks[i].first; n     tasks[i].first *= 1000; n   } n   for (int i = 0; i < n; ++i) { n     cin >> tasks[i].second; n   } n   sort(tasks.begin(), tasks.end()); n   reverse(tasks.begin(), tasks.end()); n   auto ok = [&](long long t) -> bool { n     long long dp[N][N][N]; n     for (int i = 0; i < n; ++i) { n       for (int j = 0; j <= n; ++j) { n         for (int k = 0; k <= n; ++k) { n           dp[i][j][k] = real_inf; n         } n       } n     } n     dp[0][0][1] = value(tasks[0], t); n     for (int i = 0; i < n - 1; ++i) { n       for (int j = 0; j <= n; ++j) { n         for (int k = 0; k <= n; ++k) { n           if (dp[i][j][k] == real_inf) { n             continue; n           } n           if (tasks[i].first == tasks[i + 1].first) { n             dp[i + 1][j][k + 1] = n                 min(dp[i + 1][j][k + 1], dp[i][j][k] + value(tasks[i + 1], t)); n             if (j > 0) { n               dp[i + 1][j - 1][k] = min(dp[i + 1][j - 1][k], dp[i][j][k]); n             } n           } else { n             dp[i + 1][j + k][1] = n                 min(dp[i + 1][j + k][1], dp[i][j][k] + value(tasks[i + 1], t)); n             if (j > 0 || k > 0) { n               dp[i + 1][j + k - 1][0] = n                   min(dp[i + 1][j + k - 1][0], dp[i][j][k]); n             } n           } n         } n       } n     } n     for (int i = 0; i <= n; ++i) { n       for (int j = 0; j <= n; ++j) { n         if (dp[n - 1][i][j] <= 0) { n           return true; n         } n       } n     } n     return false; n   }; n   long long left = 0, right = inf, best = inf; n   while (left <= right) { n     long long middle = (left + right) / 2; n     if (ok(middle)) { n       best = middle; n       right = middle - 1; n     } else { n       left = middle + 1; n     } n   } n   cout << best <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int power(int num1, int num2) { n   int ans = 1; n   for (int i = 0; i < num2; i++) { n     ans *= num1; n   } n   return ans; n } n int main() { n   string str; n   int num1, help = 0; n   cin >> num1 >> str; n   for (int i = 0; i < str.size(); i++) { n     help += power(10, i) * (str[i] -  0 ); n   } n   cout << help + num1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e5; n vector<int> G[MAXN + 5]; n int maxFrom[MAXN + 5], down[MAXN + 5], up[MAXN + 5]; n int id[MAXN + 5], sz[MAXN + 5], diam[MAXN + 5]; n bool flag[MAXN + 5]; n int ID; n int n, m, Q; n map<int, int> cnt; n vector<int> accSum[MAXN + 5]; n vector<double> accWeightedSum[MAXN + 5]; n vector<int> vals[MAXN + 5]; n map<pair<int, int>, double> oldAns; n void dfs(int u, int p = -1) { n   id[u] = ID; n   sz[ID]++; n   down[u] = 0; n   flag[u] = 1; n   for (int v : G[u]) { n     if (v == p) continue; n     dfs(v, u); n     down[u] = max(down[u], 1 + down[v]); n   } n } n void dfs2(int u, int p = -1, int parentWithoutme = 0) { n   if (~p) n     up[u] = 1 + parentWithoutme; n   else n     up[u] = 0; n   maxFrom[u] = max(up[u], down[u]); n   diam[ID] = max(diam[ID], maxFrom[u]); n   cnt[maxFrom[u]]++; n   vector<int> paths = {up[u], 0}; n   for (int v : G[u]) { n     if (v == p) continue; n     paths[1] = max(paths[1], 1 + down[v]); n     if (paths[0] < paths[1]) swap(paths[0], paths[1]); n   } n   for (int v : G[u]) { n     if (v == p) continue; n     dfs2(v, u, ((paths[0] != (1 + down[v])) ? paths[0] : paths[1])); n   } n } n void calc(int u) { n   cnt.clear(); n   ID = u; n   dfs(u); n   dfs2(u); n   accSum[ID].resize(cnt.size() + 1); n   accWeightedSum[ID].resize(cnt.size() + 1); n   accSum[ID][0] = 0; n   accWeightedSum[ID][0] = 0; n   int i = 0; n   for (auto p : cnt) { n     vals[ID].push_back(p.first); n     accSum[ID][i + 1] = accSum[ID][i] + p.second; n     accWeightedSum[ID][i + 1] = n         accWeightedSum[ID][i] + p.first * (1. * p.second / sz[ID]); n     i++; n   } n } n int main() { n   int c, c2; n   scanf( %d%d%d , &n, &m, &Q); n   for (c = 0; c < m; c++) { n     int u, v; n     scanf( %d%d , &u, &v); n     u--; n     v--; n     G[u].push_back(v); n     G[v].push_back(u); n   } n   for (c = 0; c < n; c++) { n     if (flag[c]) continue; n     calc(c); n   } n   for (c = 0; c < Q; c++) { n     int u, v; n     scanf( %d%d , &u, &v); n     u--; n     v--; n     if (id[u] == id[v]) n       printf( -1 n ); n     else { n       if (id[u] > id[v]) swap(u, v); n       if (oldAns.count({id[u], id[v]}) > 0) { n         printf( %.9f n , oldAns[{id[u], id[v]}]); n       } else { n         if (sz[id[u]] > sz[id[v]]) swap(u, v); n         int minDiam = max(diam[id[u]], diam[id[v]]); n         double ret = 0; n         int j = 0; n         for (int x : vals[id[u]]) { n           int i = n               lower_bound(vals[id[v]].begin(), vals[id[v]].end(), minDiam - x) - n               vals[id[v]].begin(); n           double f = 1. * (accSum[id[u]][j + 1] - accSum[id[u]][j]) / sz[id[u]]; n           ret += f * (accWeightedSum[id[v]].back() - accWeightedSum[id[v]][i] + n                       (x + 1.0) * (sz[id[v]] - accSum[id[v]][i]) / sz[id[v]]); n           ret += f * (1. * accSum[id[v]][i] / sz[id[v]]) * minDiam; n           j++; n         } n         if (id[u] > id[v]) swap(u, v); n         oldAns[{id[u], id[v]}] = ret; n         printf( %.9f n , ret); n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[105]; n int main() { n   int n, k, x; n   cin >> n >> k; n   for (int i = 0; i < n; i++) cin >> a[i]; n   int ans = 0; n   bool yes = true; n   for (int i = 0; i < n; i++) n     if (a[i] < k) { n       yes = false; n       break; n     } n   while (!yes) { n     for (int i = 0; i < n; i++) n       if ((a[i] < a[i + 1] || i == n - 1) && a[i] < k) a[i]++; n     yes = true; n     for (int i = 0; i < n; i++) n       if (a[i] < k) { n         yes = false; n         break; n       } n     ans++; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[300005] = {0}; n long long two[300005] = {0}; n const long long mod = 1e9 + 7; n int main() { n   two[0] = 1; n   for (int i = 1; i <= 300000; i++) { n     two[i] = (two[i - 1] * 2LL) % mod; n   } n   long long res = 0; n   int n; n   while (cin >> n) { n     a[0] = 0; n     for (int i = 1; i <= n; i++) { n       cin >> a[i]; n     } n     sort(a, a + n + 1); n     res = 0; n     for (int i = 1; i <= n; i++) { n       res += (a[i] * (two[i - 1] - two[n - i])); n       res %= mod; n     } n     cout << res << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(int argc, char* argv[]) { n   cin.sync_with_stdio(false); n   cout.sync_with_stdio(false); n   int N, M; n   cin >> N >> M; n   vector<string> data(N); n   for (int i = 0; i < N; i++) cin >> data[i]; n   vector<string> data_rev(M, string(N,  . )); n   for (int i = 0; i < N; i++) n     for (int j = 0; j < M; j++) data_rev[j][i] = data[i][j]; n   vector<vector<int> > starts(M); n   for (int i = 0; i < M; i++) { n     starts[i].push_back(0); n     for (int j = 1; j < N; j++) { n       if (data_rev[i][j] != data_rev[i][j - 1]) { n         starts[i].push_back(j); n       } n     } n     starts[i].push_back(N); n   } n   vector<vector<short> > already_counted(starts.size()); n   for (size_t i = 0; i < starts.size(); i++) { n     already_counted[i] = vector<short>(starts[i].size(), (short)0); n   } n   long long main_res = 0LL; n   for (int i = 0; i < M; i++) { n     for (size_t j = 3; j < starts[i].size(); j++) { n       if (!already_counted[i][j - 1]) { n         int h = starts[i][j - 1] - starts[i][j - 2]; n         if (h <= starts[i][j] - starts[i][j - 1] && n             h <= starts[i][j - 2] - starts[i][j - 3]) { n           int i_after_last = i + 1; n           while (i_after_last < M) { n             int j_absolute_1 = starts[i][j - 1]; n             int j_absolute_2 = starts[i][j - 2]; n             int j_absolute_3 = starts[i][j - 2] - 1; n             if (data_rev[i][j_absolute_1] != n                     data_rev[i_after_last][j_absolute_1] || n                 data_rev[i][j_absolute_2] != n                     data_rev[i_after_last][j_absolute_2] || n                 data_rev[i][j_absolute_3] != n                     data_rev[i_after_last][j_absolute_3]) { n               break; n             } n             int idx_ = n                 lower_bound(starts[i_after_last].begin(), n                             starts[i_after_last].end(), starts[i][j - 1]) - n                 starts[i_after_last].begin(); n             if (idx_ < starts[i_after_last].size() && n                 starts[i_after_last][idx_] == starts[i][j - 1] && n                 starts[i_after_last][idx_ - 1] == starts[i][j - 2] && n                 h <= starts[i_after_last][idx_ - 1] - n                          starts[i_after_last][idx_ - 2] && n                 h <= starts[i_after_last][idx_ + 1] - n                          starts[i_after_last][idx_]) { n               already_counted[i_after_last][idx_] = (short)1; n               i_after_last++; n             } else n               break; n           } n           int sz = (i_after_last - i); n           main_res += (sz * (sz + 1)) / 2; n         } n       } n     } n   } n   cout << main_res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, c, d; n   while (~scanf( %d%d%d%d , &a, &b, &c, &d)) { n     int t = d - c; n     if (c - b == t && b - a == t) { n       printf( %d n , d + t); n     } else if (a != 0 && b != 0 && c != 0 && d != 0 && b * b == a * c && n                c * c == b * d && d * d % c == 0) { n       printf( %d n , d * d / c); n     } else n       printf( 42 n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int read() { n   int x = 0, f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   return x * f; n } n int n, d; n long long k, now; n int a[1005][1005]; n void Dfs(int u, int i) { n   if (a[u][i] == k) { n     a[u][i] = 0; n     a[u][i + 1]++; n     Dfs(u, i + 1); n   } n } n int main() { n   n = read(); n   k = read(); n   d = read(); n   int t = n; n   for (int i = 1; i <= d; i++) t = t / k + (int)(t % k > 0); n   memset(a, 0, sizeof(a)); n   if (t == 1) { n     for (int u = 2; u <= n; u++) { n       for (int i = 1; i <= d; i++) a[u][i] = a[u - 1][i]; n       a[u][1] += 1; n       Dfs(u, 1); n     } n     for (int i = 1; i <= d; i++) { n       for (int j = 1; j <= n; j++) printf( %d  , a[j][i] + 1); n       puts(  ); n     } n   } else n     puts( -1 ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, k, i, j, mx, ans = 0; n   cin >> n >> k; n   long long int arr[n]; n   for (i = 0; i < n; i++) cin >> arr[i]; n   sort(arr, arr + n); n   for (i = 0; i < n / 2; i++) swap(arr[i], arr[n - 1 - i]); n   for (i = 0; i < n; i++) { n     mx = 0; n     for (j = 0; j < k && i + j < n; j++) { n       mx = max(mx, arr[i + j]); n     } n     ans += (2 * (mx - 1)); n     i += k - 1; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int t, n, m; n int tot; n int op[1005 * 1005 * 3][7]; n string s; n int a[1005][1005]; n int p[1005][1005]; n int sum; n void check(int x1, int x2, int x3, int x4, int x, int y, int add) { n   sum = x1 + x2 + x3 + x4; n   if (!sum) return; n   if (sum == 1) { n     if (x3 != 1) p[x][y + 1] += add; n     if (x2 != 1) p[x + 1][y] += add; n     if (x4 != 1) p[x][y] += add; n     if (x1 != 1) p[x + 1][y + 1] += add; n     return; n   } n   if (sum == 2) { n     if (x3 == 1) p[x][y + 1] += add; n     if (x2 == 1) p[x + 1][y] += add; n     if (x4 == 1) p[x][y] += add; n     if (x1 == 1) p[x + 1][y + 1] += add; n     return; n   } n   if (sum == 3) { n     if (x3 != 1) p[x][y + 1] += add; n     if (x2 != 1) p[x + 1][y] += add; n     if (x4 != 1) p[x][y] += add; n     if (x1 != 1) p[x + 1][y + 1] += add; n     return; n   } n   if (sum == 4) { n     p[x][y + 1] += add; n     p[x + 1][y] += add; n     p[x + 1][y + 1] += add; n     p[x][y] += add; n     return; n   } n } n namespace solve1 { n void main() { n   for (int i = 1; i < n; i += 2) n     for (int j = 1; j < m; j += 2) n       check(a[i][j], a[i][j + 1], a[i + 1][j], a[i + 1][j + 1], i, j, 1); n   return; n } n }  // namespace solve1 n namespace solve2 { n void main() { n   for (int i = 1; i < n; i += 2) n     for (int j = 1; j < m; j += 2) n       check(a[i][j], a[i][j + 1], a[i + 1][j], a[i + 1][j + 1], i, j, 1); n   for (int j = 1; j < m; j += 2) n     check(0, 0, a[n][j], a[n][j + 1], n - 1, j, 10000); n   return; n } n }  // namespace solve2 n namespace solve3 { n void main() { n   for (int i = 1; i < n; i += 2) n     for (int j = 1; j < m; j += 2) n       check(a[i][j], a[i][j + 1], a[i + 1][j], a[i + 1][j + 1], i, j, 1); n   for (int i = 1; i < n; i += 2) n     check(0, a[i][m], 0, a[i + 1][m], i, m - 1, 100); n   return; n } n }  // namespace solve3 n namespace solve4 { n void main() { n   for (int i = 1; i < n; i += 2) n     for (int j = 1; j < m; j += 2) n       check(a[i][j], a[i][j + 1], a[i + 1][j], a[i + 1][j + 1], i, j, 1); n   for (int j = 1; j <= m - 2; j += 2) n     check(0, 0, a[n][j], a[n][j + 1], n - 1, j, 10000); n   for (int i = 1; i <= n - 2; i += 2) n     check(0, a[i][m], 0, a[i + 1][m], i, m - 1, 100); n   check(0, 0, 0, a[n][m], n - 1, m - 1, 1000000); n   return; n } n }  // namespace solve4 n void Print(int x, int y) { n   if (x % 2 == 1 && y % 2 == 1) n     printf( %d %d %d %d %d %d n , x, y, x + 1, y, x, y + 1); n   if (x % 2 == 0 && y % 2 == 1) n     printf( %d %d %d %d %d %d n , x, y, x - 1, y, x, y + 1); n   if (x % 2 == 1 && y % 2 == 0) n     printf( %d %d %d %d %d %d n , x, y, x, y - 1, x + 1, y); n   if (x % 2 == 0 && y % 2 == 0) n     printf( %d %d %d %d %d %d n , x, y, x - 1, y, x, y - 1); n } n void pRint(int x, int y) { n   if (y == m - 1) { n     if (x % 2 == 1) n       printf( %d %d %d %d %d %d n , x, y, x + 1, y, x, y + 1); n     else n       printf( %d %d %d %d %d %d n , x, y, x - 1, y, x, y + 1); n   } else { n     if (x % 2 == 1) n       printf( %d %d %d %d %d %d n , x, y, x + 1, y, x, y - 1); n     else n       printf( %d %d %d %d %d %d n , x, y, x - 1, y, x, y - 1); n   } n } n void prInt(int x, int y) { n   if (x == n - 1) { n     if (y % 2 == 1) n       printf( %d %d %d %d %d %d n , x, y, x + 1, y, x, y + 1); n     else n       printf( %d %d %d %d %d %d n , x, y, x, y - 1, x + 1, y); n   } else { n     if (y % 2 == 1) n       printf( %d %d %d %d %d %d n , x, y, x - 1, y, x, y + 1); n     else n       printf( %d %d %d %d %d %d n , x, y, x - 1, y, x, y - 1); n   } n } n void priNt(int x, int y) { n   if (x == n - 1 && y == m - 1) n     printf( %d %d %d %d %d %d n , x, y, x + 1, y, x, y + 1); n   if (x == n - 1 && y == m) n     printf( %d %d %d %d %d %d n , x, y, x + 1, y, x, y - 1); n   if (x == n && y == m - 1) n     printf( %d %d %d %d %d %d n , x, y, x - 1, y, x, y + 1); n   if (x == n && y == m) printf( %d %d %d %d %d %d n , x, y, x - 1, y, x, y - 1); n } n void print() { n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) { n       if ((p[i][j] % 100) & 1) tot++; n       if ((p[i][j] / 100) & 1) tot++; n       if ((p[i][j] / 10000) & 1) tot++; n       if ((p[i][j] / 1000000) & 1) tot++; n     } n   printf( %d n , tot); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) { n       if ((p[i][j] % 100) & 1) Print(i, j); n       if ((p[i][j] / 100) & 1) pRint(i, j); n       if ((p[i][j] / 10000) & 1) prInt(i, j); n       if ((p[i][j] / 1000000) & 1) priNt(i, j); n     } n } n int main() { n   scanf( %d , &t); n   while (t--) { n     memset(p, 0, sizeof p); n     tot = 0; n     scanf( %d %d , &n, &m); n     for (int i = 1; i <= n; i++) { n       cin >> s; n       for (int j = 0; j < m; j++) a[i][j + 1] = (s[j] ==  1 ); n     } n     if (n % 2 == 0 && m % 2 == 0) solve1::main(); n     if (n % 2 == 1 && m % 2 == 0) solve2::main(); n     if (n % 2 == 0 && m % 2 == 1) solve3::main(); n     if (n % 2 == 1 && m % 2 == 1) solve4::main(); n     print(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m; n long long bin(long long l, long long h) { n   long long ans; n   while (l <= h) { n     long long mid = (l + h) / 2; n     if (n - (mid * (mid + 1)) / 2 <= 0) { n       ans = mid; n       h = mid - 1; n     } else n       l = mid + 1; n   } n   return ans; n } n int main() { n   long long p = 1414213562; n   cin >> n >> m; n   if (m >= n) { n     cout << n << endl; n     return 0; n   } n   long long ans = m; n   n -= m; n   long long ans2 = bin(1, p); n   ans += ans2; n   cout << ans << endl; n } n


#include <bits/stdc++.h> n int main() { n   char c; n   std::set<char> s; n   while (std::cin >> c) { n     if ((c >=  a ) && (c <=  z )) { n       s.insert(c); n     } n   } n   std::cout << s.size() <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n char c; n string s; n set<char> q; n int i, sum; n int main() { n   getline(cin, s); n   for (i = 0; i < s.size(); i++) n     if (isalpha(s[i])) q.insert(s[i]); n   cout << q.size() << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<string> mas; n int n, m; n void input() { n   cin >> n >> m; n   mas.clear(); n   mas.resize(n); n   for (int i = 0; i < n; ++i) cin >> mas[i]; n } n void solve() { n   string ans = mas[0]; n   for (int i = 0; i < m; ++i) { n     char save = ans[i]; n     for (char d =  a ; d <=  z ; d++) { n       ans[i] = d; n       bool f = 1; n       for (int j = 0; j < n; ++j) { n         int err = 0; n         for (int c = 0; c < m; ++c) { n           if (ans[c] != mas[j][c]) err++; n         } n         if (err > 1) { n           f = 0; n           break; n         } n       } n       if (f) { n         cout << ans <<   n ; n         return; n       } n     } n     ans[i] = save; n   } n   cout <<  -1 n ; n } n int main() { n   int Q; n   cin >> Q; n   while (Q--) { n     input(); n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> roda(int f, vector<int> valores) { n   vector<int> temp(valores); n   if (f == 0) { n     temp[17] = valores[5]; n     temp[18] = valores[6]; n     temp[21] = valores[17]; n     temp[22] = valores[18]; n     temp[13] = valores[21]; n     temp[14] = valores[22]; n     temp[5] = valores[13]; n     temp[6] = valores[14]; n   } else if (f == 1) { n     temp[3] = valores[17]; n     temp[4] = valores[19]; n     temp[16] = valores[3]; n     temp[14] = valores[4]; n     temp[10] = valores[16]; n     temp[9] = valores[14]; n     temp[17] = valores[10]; n     temp[19] = valores[9]; n   } else if (f == 2) { n     temp[20] = valores[7]; n     temp[19] = valores[8]; n     temp[24] = valores[20]; n     temp[23] = valores[19]; n     temp[15] = valores[24]; n     temp[16] = valores[23]; n     temp[7] = valores[15]; n     temp[8] = valores[16]; n   } else if (f == 3) { n     temp[1] = valores[24]; n     temp[3] = valores[22]; n     temp[5] = valores[1]; n     temp[7] = valores[3]; n     temp[9] = valores[5]; n     temp[11] = valores[7]; n     temp[24] = valores[9]; n     temp[22] = valores[11]; n   } else if (f == 4) { n     temp[2] = valores[23]; n     temp[4] = valores[21]; n     temp[6] = valores[6]; n     temp[8] = valores[8]; n     temp[10] = valores[10]; n     temp[12] = valores[12]; n     temp[23] = valores[23]; n     temp[21] = valores[21]; n   } else if (f == 5) { n     temp[1] = valores[15]; n     temp[2] = valores[13]; n     temp[18] = valores[1]; n     temp[20] = valores[2]; n     temp[12] = valores[18]; n     temp[11] = valores[20]; n     temp[15] = valores[12]; n     temp[13] = valores[11]; n   } n   return temp; n } n bool checa(vector<int> valores) { n   for (int i = 0; i < 6; i++) { n     int cor = valores[1 + i * 4]; n     for (int j = 1; j < 4; j++) n       if (valores[1 + i * 4 + j] != cor) return false; n   } n   return true; n } n int main() { n   vector<int> valores(25); n   int i; n   for (i = 1; i < 25; i++) cin >> valores[i]; n   int resp = 0; n   for (i = 0; i < 6; i++) { n     resp = resp || checa(roda(i, valores)); n     resp = resp || checa(roda(i, roda(i, roda(i, valores)))); n   } n   if (resp) n     puts( YES ); n   else n     puts( NO ); n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e5 + 10; n const int p = 1e9 + 7; n struct edge { n   int l, r, uk; n }; n edge a[MAXN]; n int dp[4 * MAXN]; n int b[MAXN]; n int n, m; n void update(int k) { n   dp[k] = (dp[k * 2] + dp[k * 2 + 1]) % p; n   if (k > 1) update(k / 2); n } n long long getsum(int v, int ll, int rr, int l, int r) { n   if (ll > r || rr < l) return 0; n   if (ll >= l && rr <= r) return dp[v]; n   return (getsum(v * 2, ll, (ll + rr) / 2, l, r) + n           getsum(v * 2 + 1, (ll + rr) / 2 + 1, rr, l, r)) % n          p; n } n bool operator<(edge a, edge b) { return a.r < b.r; } n int binr(int l, int r, int q) { n   while (l < r - 1) { n     if (b[(l + r) / 2] < q) n       l = (l + r) / 2; n     else n       r = (l + r) / 2; n   } n   return r; n } n int main() { n   int i, size = 1, uk, l; n   cin >> n >> m; n   while (size < m) { n     size *= 2; n   } n   for (i = 0; i < m; i++) { n     cin >> a[i].l >> a[i].r; n   } n   sort(a, a + m); n   b[0] = 0; n   b[1] = a[0].r; n   uk = 2; n   for (i = 1; i < m; i++) { n     if (a[i - 1].r != a[i].r) { n       b[uk] = a[i].r; n       uk++; n     } n     a[i].uk = uk; n   } n   dp[size] = 1; n   update(size / 2); n   int uk1 = 1; n   for (i = 0; i < m; i++) { n     l = binr(-1, uk - 1, a[i].l); n     l++; n     if (i != 0 && a[i - 1].r != a[i].r) uk1++; n     dp[size + uk1] = (dp[size + uk1] + getsum(1, 1, size, l, uk1)) % p; n     update((size + uk1) / 2); n   } n   uk--; n   if (a[m - 1].r != n) n     cout << 0; n   else n     cout << dp[size + uk1]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[200]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   int res = 0, ans = 0; n   a[0] = -10; n   for (int i = 1; i <= n; i++) { n     if (a[i] == a[i - 1] + 1) n       res++; n     else n       res = 0; n     ans = max(res, ans); n   } n   ans = max(ans - 1, 0); n   int f1 = 0; n   for (int i = 1; i <= n + 1; i++) { n     if (a[i] == i) { n       f1++; n     } n   } n   int f2 = 0; n   for (int i = n; i >= 0; i--) { n     if (a[i] == (1000 - n + i)) { n       f2++; n     } n   } n   if (f1 >= n || f2 >= n) { n     f1 = n; n     f2 = n; n   } n   ans = max(ans, max(f1, f2) - 1); n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int n, m; n   cin >> n >> m; n   vector<long int> a[n + 1], b[n + 1]; n   vector<pair<int, int> > v; n   for (int i = 0; i <= n; i++) a[i].resize(m + 1), b[i].resize(m + 1); n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) cin >> a[i][j]; n   } n   for (int i = 1; i <= n - 1; i++) { n     for (int j = 1; j <= m - 1; j++) { n       if (a[i][j + 1] == 1 && a[i + 1][j] == 1 && a[i + 1][j + 1] == 1 && n           a[i][j] == 1) { n         v.push_back(make_pair(i, j)); n         b[i][j + 1] = 1; n         b[i][j] = 1; n         b[i + 1][j] = 1; n         b[i + 1][j + 1] = 1; n       } n     } n   } n   int ct = 1; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       if (a[i][j] != b[i][j]) ct = 0; n     } n   } n   if (ct == 0) n     cout << -1; n   else { n     cout << v.size() <<   n ; n     for (int i = 0; i < v.size(); i++) n       cout << v[i].first <<     << v[i].second <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long i, j, n, a, b, m, k, y, z, cos, pos, color; n   long long r[5005], c[5005], x[100009]; n   cin >> n >> m >> k; n   for (i = 1; i <= k; i++) { n     cin >> cos >> pos >> color; n     x[i] = color; n     if (cos == 1) n       r[pos] = i; n     else n       c[pos] = i; n   } n   for (i = 1; i <= n; i++) { n     for (j = 1; j <= m; j++) { n       y = max(r[i], c[j]); n       cout << x[y] <<    ; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, swaps = 0; n   vector<int> seq; n   cin >> n; n   seq.resize(n * 2); n   for (int i = 0; i < n * 2; i++) cin >> seq[i]; n   for (int i = 0; i < 2 * n - 1; i += 2) { n     int j = i + 1; n     while (seq[j] != seq[i]) j++; n     for (int k = j; k > i + 1; k--) { n       swap(seq[k - 1], seq[k]); n       swaps++; n     } n   } n   cout << swaps << endl; n } n


#include <bits/stdc++.h> n using namespace std; n char s[2000][2000]; n int n, m; n bool h[300][300]; n bool v[300][300]; n const long long mod = 1000000007; n void get(int x, int y, bool& h, bool& v) { n   int cnt = 0; n   for (int i = 0; i < 3; i++) n     for (int j = 0; j < 3; j++) { n       if (s[x + i][y + j] ==  O ) cnt++; n     } n   if (cnt == 5 || cnt == 4 || cnt == 1 || cnt == 0) { n     v = h = true; n     return; n   } n   if (cnt == 2 || cnt == 3) { n     h = s[x][y] ==  O ; n     v = !h; n     return; n   } n   h = (s[x][y + 1] ==  O ); n   v = !h; n } n long long calc1(int y) { n   if (n % 2 != 0) return 0; n   for (int i = 0; i < n; i++) n     if (!v[i][y]) return 0; n   return 1; n } n long long tmp[300]; n long long dp[300]; n long long calc2(int y) { n   if (y + 1 >= m) return 0; n   for (int i = 0; i <= n; i++) tmp[i] = 0; n   tmp[0] = 1; n   for (int i = 0; i < n; i++) { n     if (h[i][y] && h[i][y + 1]) { n       tmp[i + 1] += tmp[i]; n       tmp[i + 1] %= mod; n     } n     if (i < n - 1 && v[i][y] && v[i + 1][y] && v[i + 1][y + 1] && v[i][y + 1]) { n       tmp[i + 2] += tmp[i]; n       tmp[i + 2] %= mod; n     } n   } n   if (calc1(y) && calc1(y + 1)) tmp[n] = (tmp[n] - 1 + mod) % mod; n   return tmp[n]; n } n int main() { n   scanf( %d %d n , &n, &m); n   for (int i = 0; i < 4 * n + 1; i++) { n     gets(s[i]); n   } n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) get(4 * i + 1, 4 * j + 1, h[i][j], v[i][j]); n   dp[0] = 1; n   for (int i = 0; i < m; i++) { n     dp[i + 1] += dp[i] * calc1(i); n     dp[i + 2] += dp[i] * calc2(i); n     dp[i + 1] %= mod; n     dp[i + 2] %= mod; n   } n   cout << dp[m] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3e5 + 5; n long long a[N]; n long long dp[N][20]; n int main() { n   int n, m, k; n   cin >> n >> m >> k; n   a[0] = 0; n   for (int i = 1; i <= m; i++) dp[0][i] = -1e10; n   long long ans = 0; n   for (int i = 1; i <= n; i++) { n     scanf( %lld , &a[i]); n     for (int j = 1; j <= m; j++) { n       if (j == 1) n         dp[i][j] = max(a[i] - k, dp[i - 1][m] + a[i] - k); n       else n         dp[i][j] = dp[i - 1][j - 1] + a[i]; n       ans = max(ans, dp[i][j]); n     } n   } n   printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n long long ar[100100]; n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> ar[i]; n   } n   sort(ar, ar + n); n   long long dif1 = ar[1] - ar[0], dif2 = -1; n   int cnt1 = 0, cnt2 = 0; n   if (n == 1) { n     cout << -1 << endl; n     return 0; n   } else if (n == 2) { n     if (ar[0] == ar[1]) { n       cout << 1 << endl << ar[0]; n       return 0; n     } else { n       if ((ar[0] + ar[1]) % 2 == 0) { n         cout << 3 << endl n              << ar[0] - (ar[1] - ar[0]) <<     << (ar[1] + ar[0]) / 2 <<     n              << ar[1] + (ar[1] - ar[0]) << endl; n         return 0; n       } else { n         cout << 2 << endl n              << ar[0] - (ar[1] - ar[0]) <<     << ar[1] + (ar[1] - ar[0]) n              << endl; n         return 0; n       } n     } n   } else { n     set<long long> nd; n     set<long long> ans; n     for (int i = 1; i < n; i++) { n       nd.insert(ar[i] - ar[i - 1]); n     } n     if (nd.size() > 2) { n       cout << 0 << endl; n     } else if (nd.size() == 1) { n       ans.insert(ar[0] - (ar[1] - ar[0])); n       ans.insert(ar[n - 1] + (ar[1] - ar[0])); n       cout << ans.size() << endl; n       for (auto it : ans) { n         cout << it << endl; n       } n     } else { n       int ls1 = -1, ls2 = -1; n       dif1 = *nd.begin(); n       dif2 = *(++nd.begin()); n       if (dif1 != 2 * dif2 && dif2 != 2 * dif1) { n         cout << 0 << endl; n         return 0; n       } n       for (int i = 1; i < n; i++) { n         if (ar[i] - ar[i - 1] == dif1) { n           cnt1++; n           ls1 = i; n         } else { n           cnt2++; n           ls2 = i; n         } n       } n       if (cnt1 == 1 && dif1 % 2 == 0 && dif1 > dif2) { n         cout << 1 << endl; n         cout << ar[ls1 - 1] + dif2 << endl; n         return 0; n       } n       if (cnt2 == 1 && dif2 % 2 == 0 && dif1 < dif2) { n         cout << 1 << endl; n         cout << ar[ls2 - 1] + dif1 << endl; n         return 0; n       } n       cout << 0 << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[100009], b[100009], s[100009]; n signed main() { n   ios_base::sync_with_stdio(false), cin.tie(NULL); n   long long i, j, n, m, c; n   cin >> n >> m >> c; n   for (i = 1; i <= n; i++) cin >> a[i]; n   for (i = 1; i <= m; i++) cin >> b[i], s[i] = (b[i] + s[i - 1]); n   long long x = n - m; n   for (i = 1; i <= n; i++) { n     long long t = min(i, m); n     long long g = min(i, min(n + 1 - i, x + 1)); n     long long y = s[t] - s[max(t - g, 0ll)]; n     a[i] = (a[i] + y) % c; n   } n   for (i = 1; i <= n; i++) cout << a[i] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n long long mas[200004]; n long long mas2[200004]; n struct ver { n   long long abs; n   int number; n   long long maxabs; n   int masnumber; n   ver *l; n   ver *r; n }; n void build(ver *v, int l, int r, long long *m) { n   if (l == r) { n     v->abs = m[l]; n     v->number = l; n     v->masnumber = l; n     v->maxabs = m[l]; n   } else { n     ver *a = new ver; n     ver *b = new ver; n     v->l = a; n     v->r = b; n     build(a, l, (l + r) / 2, m); n     build(b, (l + r) / 2 + 1, r, m); n     if (a->maxabs < b->maxabs) { n       v->maxabs = b->maxabs; n       v->masnumber = b->masnumber; n     } else { n       v->maxabs = a->maxabs; n       v->masnumber = a->masnumber; n     } n   } n } n pair<long long, long long> maxver(ver *v, int l, int r, int zl, int zr) { n   pair<long long, long long> answer; n   answer.first = -100000; n   answer.second = 0; n   if (l > zr || r < zl) n     return answer; n   else { n     if (zl <= l && r <= zr) { n       answer.first = v->maxabs; n       answer.second = v->masnumber; n       return answer; n     } else { n       pair<long long, long long> left; n       pair<long long, long long> right; n       left = maxver(v->l, l, (l + r) / 2, zl, zr); n       right = maxver(v->r, (l + r) / 2 + 1, r, zl, zr); n       if (left.first < right.first) n         return right; n       else n         return left; n     } n   } n } n int main() { n   ver *root = new ver; n   int n, k; n   pair<long long, long long> answer; n   long long maxans = 0; n   answer.first = 0; n   answer.second = 0; n   cin >> n >> k; n   for (int i = 1; i <= n; i++) cin >> mas[i]; n   for (int i = 0; i < 200004; i++) mas2[i] = 0; n   for (int i = n - k + 1; i <= n; i++) mas2[n - k + 1] += mas[i]; n   for (int i = n - k; i >= 1; i--) mas2[i] = mas2[i + 1] + mas[i] - mas[i + k]; n   build(root, 1, n, mas2); n   for (int i = 1; i <= n - 2 * k + 1; i++) { n     pair<long long, long long> an = maxver(root, 1, n, i + k, n); n     if (an.first + mas2[i] > maxans) { n       maxans = an.first + mas2[i]; n       answer.first = i; n       answer.second = an.second; n     } n   } n   cout << answer.first <<     << answer.second; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double x[100000]; n int main() { n   ios_base::sync_with_stdio(false); n   int n, m; n   cin >> n >> m; n   for (int i = 1; i <= n; i++) cin >> x[i]; n   double ans = 0; n   for (int i = 1; i <= m; i++) { n     int fi, se, we; n     cin >> fi >> se >> we; n     ans = max(ans, (x[fi] + x[se]) / (double)(we)); n   } n   if (n == 1) { n     cout <<  0.0000000000  << endl; n     return 0; n   } n   cout << fixed << setprecision(10) << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   while (n--) { n     string s; n     cin >> s; n     bool b = true, b1 = false, b2 = true, b3 = false; n     int sum = 0; n     for (int i = 0; i < s.size(); i++) { n       sum += (int)s[i] - 48; n       int x = (int)s[i] - 48; n       if (x % 2 == 0) { n         if (x != 0) b3 = true; n         if (x == 0 && b2 == false) b3 = true; n       } n       if (s[i] ==  0  && b2) { n         b1 = true; n         b2 = false; n       } n     } n     if (sum % 3 == 0 && b1 && b3) n       cout <<  red  << endl; n     else n       cout <<  cyan  << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long t, d, ct, q, ans, l, m, r, n, mx, mn, i, j, k, x, y, z, s; n char ch; n string ss =   , str =   ; n long long a[200003], b[200003]; n long long pref[200003]; n int main() { n   ios_base::sync_with_stdio(false), cin.tie(nullptr); n   s = k = d = l = mx = m = mn = r = q = i = j = z = x = y = ct = ans = 0; n   for (i = 0; i < 26; i++) cin >> a[i]; n   cin >> ss; n   n = ss.length(); n   pref[1] = a[ss[0] -  a ]; n   for (i = 1; i < n; i++) pref[i + 1] = pref[i] + a[ss[i] -  a ]; n   vector<long long> v[26]; n   for (i = 0; i < n; i++) v[ss[i] -  a ].push_back(i + 1); n   for (i = 0; i < 26; i++) { n     m = v[i].size(); n     if (m <= 1) continue; n     y = 0; n     vector<long long> diff; n     for (auto h : v[i]) diff.push_back(pref[h - 1]); n     map<long long, long long> mp; n     for (j = 0; j < diff.size(); j++) { n       ans += mp[diff[j] - a[i]]; n       mp[diff[j]]++; n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long maps[105][105]; n int main() { n   long long a, b, c, d, i, j, k, l, t, n, m; n   scanf( %lld , &t); n   while (t--) { n     scanf( %lld , &a); n     for (i = 2; i * i <= a; i++) { n       if (a % i) { n         b = a % i; n         c = a - b; n         break; n       } n     } n     printf( %lld %lld n , i, c); n   } n } n


#include <bits/stdc++.h> n int main() { n   int64_t n; n   std::cin >> n; n   int64_t total(0), smallestOdd(0); n   while (n--) { n     int64_t x; n     std::cin >> x; n     total += x; n     if ((x % 2) && (smallestOdd == 0 || x < smallestOdd)) { n       smallestOdd = x; n     } n   } n   if (total % 2) { n     total -= smallestOdd; n   } n   std::cout << total << std::endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long double pi = acos(-1.0); n long long int inf = 0x3f3f3f3f3f3f3f3f; n const long long int mod = 1e9 + 7; n const long long int mx = 5 * 1000000; n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int t; n   cin >> t; n   while (t--) { n     long long int n, T, a, b; n     cin >> n >> T >> a >> b; n     long long int type[n + 1]; n     for (long long int i = 0; i < n; i++) cin >> type[i]; n     vector<long long int> easy, hard; n     vector<long long int> seq; n     seq.push_back(0); n     set<long long int> ss; n     for (long long int i = 0; i < n; i++) { n       long long int in; n       cin >> in; n       ss.insert(in); n       if (type[i] == 0) n         easy.push_back(in); n       else n         hard.push_back(in); n     } n     for (auto i : ss) seq.push_back(max(i - 1, 0ll)); n     seq.push_back(T); n     sort(easy.begin(), easy.end()); n     sort(hard.begin(), hard.end()); n     long long int ans = 0; n     for (auto i : seq) { n       auto ee = upper_bound(easy.begin(), easy.end(), i); n       long long int ee1; n       if (ee == easy.end()) n         ee1 = easy.size(); n       else n         ee1 = ee - easy.begin(); n       auto hh = upper_bound(hard.begin(), hard.end(), i); n       long long int hh1; n       if (hh == hard.end()) n         hh1 = hard.size(); n       else n         hh1 = hh - hard.begin(); n       long long int mandt = ee1 * a + hh1 * b; n       long long int temp = 0; n       if (mandt <= i) temp = ee1 + hh1; n       long long int rem_time = i - mandt; n       long long int rem_easy = easy.size() - ee1; n       long long int rem_hard = hard.size() - hh1; n       temp += min(rem_easy, rem_time / a); n       rem_time -= min(rem_easy, rem_time / a) * a; n       temp += min(rem_hard, rem_time / b); n       ans = max(ans, temp); n     } n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> D0, D1; n vector<vector<pair<long long, long long> > > G; n bool comp(pair<long long, long long> a, pair<long long, long long> b) { n   return a.second - a.first > b.second - b.first; n } n void dfs(int x, int k, int p = -1) { n   vector<pair<long long, long long> > F; n   for (auto it : G[x]) { n     int y = it.first; n     if (y == p) continue; n     dfs(y, k, x); n     F.push_back(pair<long long, long long>(D0[y], it.second + D1[y])); n   } n   sort(F.begin(), F.end(), comp); n   for (int i = 0; i < (int)F.size(); ++i) { n     if (i < k - 1 and F[i].second > F[i].first) n       D1[x] += F[i].second; n     else n       D1[x] += F[i].first; n     if (i < k and F[i].second > F[i].first) n       D0[x] += F[i].second; n     else n       D0[x] += F[i].first; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int q; n   cin >> q; n   while (q--) { n     int n, k; n     cin >> n >> k; n     G = vector<vector<pair<long long, long long> > >(n); n     D0 = D1 = vector<long long>(n, 0); n     for (int i = 0; i < n - 1; ++i) { n       int x, y, k; n       cin >> x >> y >> k; n       --x; n       --y; n       G[x].push_back(pair<long long, long long>(y, k)); n       G[y].push_back(pair<long long, long long>(x, k)); n     } n     dfs(0, k); n     cout << D0[0] <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n struct line { n   long long int a, b, c; n }; n int main() { n   long long int x1, y1, x2, y2; n   cin >> x1 >> y1 >> x2 >> y2; n   long long int n; n   cin >> n; n   vector<line> data(n); n   for (auto &p : data) { n     cin >> p.a >> p.b >> p.c; n   } n   vector<bool> s1(n), s2(n); n   for (int i = 0; i < n; ++i) { n     s1[i] = data[i].a * x1 + data[i].b * y1 + data[i].c > 0; n     s2[i] = data[i].a * x2 + data[i].b * y2 + data[i].c > 0; n   } n   int cnt = 0; n   for (int i = 0; i < n; ++i) { n     cnt += s1[i] ^ s2[i]; n   } n   cout << cnt << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, k; n long long a[100005]; n bool moze(long long x) { n   long long st[100005]; n   long long curr = 0; n   for (long long i = 0; i < n; i++) { n     long long t = x - a[i]; n     if (t < 0) t = 0; n     if (i >= k) curr -= st[i - k]; n     st[i] = t - curr; n     if (st[i] < 0) st[i] = 0; n     curr += st[i]; n   } n   long long res = 0; n   for (long long i = 0; i < n; i++) res += st[i]; n   return res <= m; n } n void solve() { n   long long l = 1; n   long long r = 1000100000; n   long long x; n   while (l <= r) { n     long long m = (l + r) / 2; n     if (moze(m)) { n       l = m + 1; n       x = m; n     } else n       r = m - 1; n   } n   printf( %lld , x); n } n int main() { n   scanf( %lld%lld%lld , &n, &m, &k); n   for (int i = 0; i < n; i++) scanf( %lld , &a[i]); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n const long long INF = 1e18; n const int MAXN = 2e5 + 5; n const int MAXM = 9e4 + 500; n const int N = 300 + 10; n const double EPS = 1e-5; n int i, j, d; n int n, k, m, p; n int a[N][N], dp[2][N][N], last[N][N]; n vector<pair<int, int> > grid[MAXM]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m >> p; n   for (i = 1; i <= n; i++) { n     for (j = 1; j <= m; j++) { n       cin >> a[i][j]; n       grid[a[i][j]].push_back({i, j}); n     } n   } n   int ans = 1e9; n   for (int now = 1; now <= p; now++) { n     for (auto g : grid[now]) { n       int first = g.first; n       int second = g.second; n       int dist = 1e9; n       for (i = 1; i <= n; i++) { n         if (last[i][second] == now - 1) n           dist = min(dist, dp[1][i][second] + abs(first - i)); n       } n       dp[0][first][second] = n           (now == 1 ? abs(first - 1) + abs(second - 1) : dist); n       if (now == p) ans = min(ans, dist); n     } n     for (auto g : grid[now]) { n       int first = g.first; n       int second = g.second; n       for (i = 1; i <= m; i++) { n         if (last[first][i] < now || n             last[first][i] == now && n                 dp[0][first][second] + abs(second - i) < dp[1][first][i]) { n           last[first][i] = now; n           dp[1][first][i] = dp[0][first][second] + abs(second - i); n         } n       } n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:1024000000,1024000000 ) n using namespace std; n struct edge { n   int v, next; n } e[200010]; n int ecnt = 0, head[100010]; n int n; n int d[100010]; n int leg[100010]; n void addedge(int a, int b) { n   e[++ecnt].v = b; n   e[ecnt].next = head[a]; n   head[a] = ecnt; n } n void del(int x) { n   while (d[x] <= 2) { n     d[x] = 0; n     int xx = x; n     for (int i = head[x]; ~i; i = e[i].next) { n       if (d[e[i].v]) { n         x = e[i].v; n         break; n       } n     } n     if (x == xx) break; n   } n   leg[x]++; n } n bool solve() { n   for (int i = (1); i <= (n); i++) { n     if (d[i]) { n       int cnt = 0; n       for (int j = head[i]; ~j; j = e[j].next) { n         int v = e[j].v; n         if (d[v]) { n           if (leg[v] > 2) n             cnt++; n           else if (d[v] - leg[v] > 1) n             cnt++; n         } n       } n       if (cnt > 2) return false; n     } n   } n   return true; n } n int main() { n   cin >> n; n   memset((head), (-1), sizeof(head)); n   memset((d), (0), sizeof(d)); n   memset((leg), (0), sizeof(leg)); n   for (int i = (1); i <= (n - 1); i++) { n     int u, v; n     scanf( %d%d , &u, &v); n     addedge(u, v); n     addedge(v, u); n     d[u]++; n     d[v]++; n   } n   for (int i = (1); i <= (n); i++) { n     if (d[i] == 1) del(i); n   } n   puts(solve() ?  Yes  :  No ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 9; n int n, a[N]; n struct pp { n   int val, id; n } p[N]; n bool cmp(pp x, pp y) { return x.val < y.val; } n int f[N], siz[N], vis[N]; n int find(int x) { return x == f[x] ? x : (f[x] = find(f[x])); } n bool merge(int u, int v) { n   if (!vis[u] || !vis[v]) return 0; n   int fu = find(u), fv = find(v); n   if (fu == fv) return 0; n   f[fu] = fv; n   siz[fv] += siz[fu]; n   return 1; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   for (int i = 1; i <= n; i++) p[i].val = a[i], p[i].id = i; n   sort(p + 1, p + n + 1, cmp); n   for (int i = 1; i <= n; i++) f[i] = i, siz[i] = 1; n   int mx = 0, cnt = 0, ans = 1, num = 0; n   memset(vis, 0, sizeof(vis)); n   for (int i = 1; i <= n; i++) { n     cnt++; n     int x = p[i].id; n     vis[x] = 1; n     if (x < n && merge(x, x + 1)) cnt--; n     if (x > 1 && merge(x, x - 1)) cnt--; n     int fa = find(x); n     mx = max(mx, siz[fa]); n     if (1ll * cnt * mx == i) { n       if (cnt > num) num = cnt, ans = p[i].val + 1; n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 2e5 + 5; n template <typename T> n void chkmax(T &x, T y) { n   x = max(x, y); n } n template <typename T> n void chkmin(T &x, T y) { n   x = min(x, y); n } n template <typename T> n void read(T &x) { n   x = 0; n   int f = 1; n   char c = getchar(); n   for (; !isdigit(c); c = getchar()) n     if (c ==  - ) f = -f; n   for (; isdigit(c); c = getchar()) x = x * 10 + c -  0 ; n   x *= f; n } n template <typename T> n void write(T x) { n   if (x < 0) x = -x, putchar( - ); n   if (x > 9) write(x / 10); n   putchar(x % 10 +  0 ); n } n template <typename T> n void writeln(T x) { n   write(x); n   puts(  ); n } n vector<int> a[MAXN]; n int n, tot, father[MAXN], p[MAXN], ans[MAXN]; n void work(int pos, int fa) { n   father[pos] = fa; n   for (unsigned i = 0; i < a[pos].size(); i++) n     if (a[pos][i] != fa) work(a[pos][i], pos); n   p[++tot] = pos; n } n int f(int x) { n   if (ans[x] != -1) return ans[x]; n   static int res[MAXN]; n   int tans = 0; n   for (int i = 1; i <= n; i++) { n     int Max = 1, Nax = 1, pos = p[i]; n     for (unsigned i = 0; i < a[pos].size(); i++) n       if (a[pos][i] != father[pos]) { n         int tmp = res[a[pos][i]] + 1; n         if (tmp > Max) { n           Nax = Max; n           Max = tmp; n         } else n           chkmax(Nax, tmp); n       } n     if (Max + Nax - 1 >= x) n       tans++, res[pos] = 0; n     else n       res[pos] = Max; n   } n   return ans[x] = tans; n } n int main() { n   read(n); n   for (int i = 1; i <= n - 1; i++) { n     int x, y; n     read(x), read(y); n     a[x].push_back(y); n     a[y].push_back(x); n   } n   work(1, 0); n   memset(ans, -1, sizeof(ans)); n   for (int i = 1, nxt; i <= n; i = nxt + 1) { n     int now = f(i); n     if (now >= 65 || f(i + 1) != now) { n       ans[nxt = i] = now; n       continue; n     } n     int l = i + 1, r = n; n     while (l < r) { n       int mid = (l + r + 1) / 2; n       if (f(mid) == now) n         l = mid; n       else n         r = mid - 1; n     } n     nxt = l; n     for (int j = i; j <= nxt; j++) ans[j] = now; n   } n   for (int i = 1; i <= n; i++) writeln(ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 250005; n int n, m, k, dep[maxn], fa[maxn]; n bool leaf[maxn], vis[maxn]; n vector<int> e[maxn]; n void dfs(int p) { n   leaf[p] = vis[p] = 1; n   for (int i = 0, v; i < e[p].size(); ++i) { n     if (vis[v = e[p][i]]) continue; n     leaf[p] = 0; n     dep[v] = dep[p] + 1; n     fa[v] = p; n     dfs(v); n   } n } n int main() { n   scanf( %d%d%d , &n, &m, &k); n   for (int i = 1, u, v; i <= m; ++i) { n     scanf( %d%d , &u, &v); n     e[u].push_back(v), e[v].push_back(u); n   } n   dep[1] = 1, dfs(1); n   int tmp = (n + k - 1) / k; n   for (int i = 1; i <= n; ++i) { n     if (dep[i] >= tmp) { n       printf( PATH n ); n       printf( %d n , dep[i]); n       while (i) { n         printf( %d  , i); n         i = fa[i]; n       } n       return 0; n     } n   } n   printf( CYCLES n ); n   vector<int> pos; n   for (int i = 1; i <= n && pos.size() < k; ++i) n     if (leaf[i]) pos.push_back(i); n   for (int i = 1; i <= k; ++i) { n     bool f = 0; n     int p = pos[i - 1]; n     for (int j = 0, v; j < e[p].size(); ++j) { n       v = e[p][j]; n       if (v != fa[p] && (dep[p] - dep[v] + 1) % 3) { n         printf( %d n , dep[p] - dep[v] + 1); n         f = 1; n         while (fa[p] != fa[v]) { n           printf( %d  , p); n           p = fa[p]; n         } n         printf( %d n , p); n         break; n       } n     } n     if (f) continue; n     vector<int> bad; n     for (int j = 0, v; j < e[p].size() && bad.size() < 2; ++j) n       if ((v = e[p][j]) != fa[p]) bad.push_back(v); n     int x = bad[0], y = bad[1]; n     if (dep[x] > dep[y]) swap(x, y); n     printf( %d n , dep[y] - dep[x] + 2); n     printf( %d  , p); n     while (fa[x] != fa[y]) { n       printf( %d  , y); n       y = fa[y]; n     } n     printf( %d n , y); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using i64 = long long; n const int N = 2e5 + 5; n struct Nod { n   i64 sum; n   int max_pos, max_pos2; n }; n Nod pom[N * 4]; n int v[N]; n int n, q, update_pos, update_val, current_pos; n i64 query_sum, peak_sum; n static void pom_build(int nod, int l, int r) { n   if (l == r) { n     pom[nod] = {v[l], l, n + 1}; n     return; n   } n   int med = (l + r) / 2; n   pom_build(2 * nod, l, med); n   pom_build(2 * nod + 1, med + 1, r); n   pom[nod].sum = pom[2 * nod].sum + pom[2 * nod + 1].sum; n   if (v[pom[2 * nod].max_pos] > v[pom[2 * nod + 1].max_pos]) { n     pom[nod].max_pos = pom[2 * nod].max_pos; n     pom[nod].max_pos2 = v[pom[2 * nod].max_pos2] > v[pom[2 * nod + 1].max_pos] n                             ? pom[2 * n].max_pos2 n                             : pom[2 * nod + 1].max_pos; n   } else { n     pom[nod].max_pos = pom[2 * nod + 1].max_pos; n     pom[nod].max_pos2 = pom[2 * nod + 1].max_pos2; n   } n } n static void internal_update(int nod, int l, int r) { n   if (l == r) { n     pom[nod].sum = update_val; n     return; n   } n   int med = (l + r) / 2; n   if (update_pos <= med) n     internal_update(2 * nod, l, med); n   else n     internal_update(2 * nod + 1, med + 1, r); n   pom[nod].sum = pom[2 * nod].sum + pom[2 * nod + 1].sum; n   if (v[pom[2 * nod].max_pos] > v[pom[2 * nod + 1].max_pos]) { n     pom[nod].max_pos = pom[2 * nod].max_pos; n     pom[nod].max_pos2 = v[pom[2 * nod].max_pos2] > v[pom[2 * nod + 1].max_pos] n                             ? pom[2 * n].max_pos2 n                             : pom[2 * nod + 1].max_pos; n   } else { n     pom[nod].max_pos = pom[2 * nod + 1].max_pos; n     pom[nod].max_pos2 = pom[2 * nod + 1].max_pos2; n   } n } n static int find_peak(int nod, int l, int r) { n   if (l == r) return l; n   if (v[pom[1].max_pos] == 0) return n >= 2 ? 2 : -1; n   int med = (l + r) / 2; n   if (current_pos < med && (pom[2 * nod].max_pos != current_pos n                                 ? v[pom[2 * nod].max_pos] n                                 : v[pom[2 * nod].max_pos2]) >= query_sum) n     return find_peak(2 * nod, l, med); n   else if (current_pos < r && n            (pom[2 * nod + 1].max_pos != current_pos n                 ? v[pom[2 * nod + 1].max_pos] n                 : v[pom[2 * nod + 1].max_pos2]) >= query_sum) { n     peak_sum += pom[2 * nod].sum; n     return find_peak(2 * nod + 1, med + 1, r); n   } else n     return -1; n } n static void update(int pos, int val) { n   update_val = val; n   update_pos = pos; n   v[pos] = val; n   internal_update(1, 1, n); n } n static int query() { n   int pos = 0; n   peak_sum = 0; n   query_sum = 0; n   current_pos = 0; n   while ((pos = find_peak(1, 1, n)) != -1) { n     query_sum = peak_sum + v[pos]; n     if (peak_sum == v[pos]) return pos; n     current_pos = pos; n     peak_sum = 0; n   } n   return -1; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0), cout.tie(0); n   int pos, val; n   cin >> n >> q; n   for (int i = 1; i <= n; ++i) cin >> v[i]; n   pom_build(1, 1, n); n   while (q--) { n     cin >> pos >> val; n     update(pos, val); n     cout << query() <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename A, typename B> n string to_string(pair<A, B> p); n template <typename A, typename B, typename C> n string to_string(tuple<A, B, C> p); n template <typename A, typename B, typename C, typename D> n string to_string(tuple<A, B, C, D> p); n string to_string(const string& s) { return     + s +    ; } n string to_string(const char* s) { return to_string((string)s); } n string to_string(bool b) { return (b ?  true  :  false ); } n string to_string(vector<bool> v) { n   bool first = true; n   string res =  { ; n   for (int i = 0; i < static_cast<int>(v.size()); i++) { n     if (!first) { n       res +=  ,  ; n     } n     first = false; n     res += to_string(v[i]); n   } n   res +=  } ; n   return res; n } n template <size_t N> n string to_string(bitset<N> v) { n   string res =   ; n   for (size_t i = 0; i < N; i++) { n     res += static_cast<char>( 0  + v[i]); n   } n   return res; n } n template <typename A> n string to_string(A v) { n   bool first = true; n   string res =  { ; n   for (const auto& x : v) { n     if (!first) { n       res +=  ,  ; n     } n     first = false; n     res += to_string(x); n   } n   res +=  } ; n   return res; n } n template <typename A, typename B> n string to_string(pair<A, B> p) { n   return  (  + to_string(p.first) +  ,   + to_string(p.second) +  ) ; n } n template <typename A, typename B, typename C> n string to_string(tuple<A, B, C> p) { n   return  (  + to_string(get<0>(p)) +  ,   + to_string(get<1>(p)) +  ,   + n          to_string(get<2>(p)) +  ) ; n } n template <typename A, typename B, typename C, typename D> n string to_string(tuple<A, B, C, D> p) { n   return  (  + to_string(get<0>(p)) +  ,   + to_string(get<1>(p)) +  ,   + n          to_string(get<2>(p)) +  ,   + to_string(get<3>(p)) +  ) ; n } n void debug_out() { cerr << endl; } n template <typename Head, typename... Tail> n void debug_out(Head H, Tail... T) { n   cerr <<     << to_string(H); n   debug_out(T...); n } n const long long mod = 998244353; n long long binpow(long long a, long long n) { n   if (n == 0) return 1; n   if (n % 2 != 0) n     return binpow(a, n - 1) * a; n   else { n     long long b = binpow(a, n / 2); n     return b * b; n   } n } n long long gcd(long long a, long long b) { n   while (b) { n     a %= b; n     swap(a, b); n   } n   return a; n } n long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } n bool check(vector<int> a, vector<int> b) { n   for (int i = 0; i < 26; i++) n     if (b[i] > a[i]) return false; n   return true; n } n void solve() { n   int n, m, k; n   cin >> n >> m >> k; n   if (m < n || k < n) n     cout <<  No  << endl; n   else n     cout <<  Yes  << endl; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int t = 1; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = 1e18; n const long long mod = 1e9 + 7; n long long ExtendedEuclid(long long a, long long b, long long &x, long long &y) { n   if (b == 0) { n     x = 1; n     y = 0; n     return a; n   } n   long long g = ExtendedEuclid(b, a % b, x, y); n   long long nx = y, ny = x - a / b * y; n   x = nx; n   y = ny; n   return g; n } n long long modInverse(long long x, long long m) { n   long long a, q; n   ExtendedEuclid(x, m, a, q); n   if (a < 0) { n     a = a + (abs(a) + m - 1) / m * m; n   } n   return a; n } n long long n, m, q; n vector<vector<set<long long>>> adj; n void solve(int testcase) { n   cin >> n >> m; n   adj = vector<vector<set<long long>>>(n, vector<set<long long>>(2)); n   for (long long i = 0; i < m; i++) { n     long long u, v; n     cin >> u >> v; n     u--; n     v--; n     adj[u][(v > u)].insert(v); n     adj[v][(u > v)].insert(u); n   } n   long long ans = 0; n   for (long long i = 0; i < n; i++) { n     if (adj[i][1].size() == 0) ans++; n   } n   cin >> q; n   while (q--) { n     long long op; n     cin >> op; n     if (op == 3) { n       cout << ans <<   n ; n     } else if (op == 1) { n       long long u, v; n       cin >> u >> v; n       u--; n       v--; n       if (u > v) swap(u, v); n       if (adj[u][1].size() == 0) ans--; n       adj[u][(v > u)].insert(v); n       adj[v][(u > v)].insert(u); n     } else if (op == 2) { n       long long u, v; n       cin >> u >> v; n       u--; n       v--; n       if (u > v) swap(u, v); n       if (adj[u][1].size() == 1) ans++; n       adj[u][(v > u)].erase(adj[u][(v > u)].find(v)); n       adj[v][(u > v)].erase(adj[v][(u > v)].find(u)); n     } n   } n   return; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int t = 0; n   solve(t); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma GCC diagnostic ignored  -Wmissing-declarations  n inline int safe_mul(const int x, const int y) n     __attribute__((warn_unused_result)); n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int n, k; n   cin >> n >> k; n   int c = (n - k) / 2; n   string ans; n   for (int i = 0; i < n; ++i) ans.push_back( 0  + (i % (c + 1) < c)); n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 123456; n const int mod = 1e9 + 7; n class table { n  public: n   int id; n   int size; n }; n class group { n  public: n   int id; n   int size; n   int income; n }; n bool byIncome(const group &r1, const group &r2) { n   return r1.income > r2.income; n } n bool bySize(const table &t1, const table &t2) { return t1.size < t2.size; } n bool canFit(table &t, const group &g) { return t.size < g.size; } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n; n   cin >> n; n   vector<group> groups(n); n   for (int i = 0; i < n; i++) { n     groups[i].id = i + 1; n     cin >> groups[i].size >> groups[i].income; n   } n   int k; n   cin >> k; n   vector<table> tables(k); n   for (int i = 0; i < k; i++) { n     tables[i].id = i + 1; n     cin >> tables[i].size; n   } n   sort(groups.begin(), groups.end(), byIncome); n   sort(tables.begin(), tables.end(), bySize); n   int sum = 0; n   vector<pair<int, int> > v; n   for (int i = 0; i < n; i++) { n     group g = groups[i]; n     vector<table>::iterator t = n         lower_bound(tables.begin(), tables.end(), g, canFit); n     if (t == tables.end()) { n       continue; n     } n     sum += g.income; n     v.push_back(make_pair(g.id, t->id)); n     tables.erase(t); n   } n   cout << v.size() <<     << sum << endl; n   for (int i = 0; i < v.size(); i++) { n     cout << v[i].first <<     << v[i].second << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, limak; n   cin >> n; n   int len = n - 1; n   cin >> limak; n   int array[n - 1]; n   for (int i = 0; i < len; i++) cin >> array[i]; n   sort(array, array + len); n   int cnt = 0; n   while (array[len - 1] >= limak) { n     cnt++; n     limak++; n     array[len - 1]--; n     sort(array, array + len); n   } n   cout << cnt << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int tc; n   cin >> tc; n   while (tc--) { n     long long int n, k; n     cin >> n >> k; n     long long int a[n], m = INT_MIN; n     for (long long int i = 0; i < n; i++) { n       cin >> a[i]; n       m = max(m, a[i]); n     } n     long long int ans[n]; n     if (k & 1) { n       for (long long int i = 0; i < n; i++) cout << m - a[i] <<    ; n     } else { n       long long int d = INT_MIN; n       for (long long int i = 0; i < n; i++) { n         a[i] = m - a[i]; n         d = max(d, a[i]); n       } n       for (long long int i = 0; i < n; i++) cout << d - a[i] <<    ; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T1, typename T2> n inline ostream& operator<<(ostream& os, const pair<T1, T2>& p) { n   return os <<  (  << p.first <<  ,   << p.se <<  ) ; n } n template <typename T1, typename T2> n inline ostream& operator<<(ostream& os, const map<T1, T2>& c) { n   bool first = true; n   os <<  [ ; n   for (typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it) { n     if (!first) os <<  ,  ; n     os << *it; n     first = false; n   } n   return os <<  ] ; n } n template <typename T> n inline ostream& operator<<(ostream& os, const vector<T>& v) { n   bool first = true; n   os <<  [ ; n   for (long long i = (0); i <= (((int)(v).size()) - 1); i += (1)) { n     if (!first) os <<  ,  ; n     os << v[i]; n     first = false; n   } n   return os <<  ] ; n } n template <typename T> n inline ostream& operator<<(ostream& os, const set<T>& c) { n   bool first = true; n   os <<  [ ; n   for (typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it) { n     if (!first) os <<  ,  ; n     os << *it; n     first = false; n   } n   return os <<  ] ; n } n const int dx4[] = {-1, 0, 0, +1}; n const int dx8[] = {-1, -1, -1, 0, 0, +1, +1, +1}; n const int dy4[] = {0, -1, +1, 0}; n const int dy8[] = {-1, 0, +1, -1, +1, -1, 0, +1}; n unsigned long long m; n bool first; n vector<int> r, l; n int main() { n   int(n); n   scanf( %d , &(n)); n   cin >> m; n   cerr << ( m ) <<   =   << (m) << endl; n   m--; n   r.push_back(n); n   n--; n   for (long long i = (0); i <= (n - 1); i += (1)) n     if (m & (1LL << i)) n       r.push_back(n - i); n     else n       l.push_back(n - i); n   bool first = true; n   for (long long i = (((int)(l).size()) - 1); i >= (0); i -= (1)) { n     if (!first) cout <<    ; n     first = false; n     cout << l[i]; n   } n   for (long long i = (0); i <= (((int)(r).size()) - 1); i += (1)) { n     if (!first) cout <<    ; n     first = false; n     cout << r[i]; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   scanf( %d , &n); n   scanf( %d , &m); n   int mat[n + 1][m + 1]; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       scanf( %d , &mat[i][j]); n     } n   } n   vector<int> dp(n + 1, 0); n   for (int j = 1; j <= m; j++) { n     vector<int> dp2(n + 1, 0); n     for (int k = n - 1; k >= 1; k--) { n       if (mat[k][j] <= mat[k + 1][j]) dp2[k] = 1 + dp2[k + 1]; n     } n     for (int i = 1; i <= n; i++) dp[i] = max(dp[i], dp2[i]); n   } n   int q, l, r; n   scanf( %d , &q); n   while (q--) { n     scanf( %d , &l); n     scanf( %d , &r); n     if (l + dp[l] >= r) n       printf( Yes ); n     else n       printf( No ); n     printf(  n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n inline int max(int a, int b) { return a > b ? a : b; } n map<int, int> mp; n int a[200005], id; n inline int read(int x) { n   if (mp.count(x) > 0) return mp[x]; n   mp[x] = ++id; n   return id; n } n int main() { n   int n; n   scanf( %d , &n); n   int x; n   for (int i = 1; i <= n; ++i) { n     scanf( %d , &x); n     ++a[read(x)]; n   } n   sort(a + 1, a + 1 + id); n   int ans = 0, last, now, pos; n   for (int i = 1; i <= n; ++i) { n     last = 1; n     now = 0; n     for (int k = i; k <= n; k <<= 1) { n       pos = lower_bound(a + last, a + id + 1, k) - a; n       if (pos > id) break; n       now += k; n       last = pos + 1; n     } n     ans = max(ans, now); n   } n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string a, b; n   cin >> a >> b; n   int p = max(a.size(), b.size()); n   cout << (a == b ? -1 : p) << endl; n } n


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


#include <bits/stdc++.h> n using namespace std; n const int inf = 1e9; n const int MOD = 1e9 + 7; n const int N = 2e2 + 10, M = 1e6 + 10; n const int alp = 10; n vector<pair<int, int> > pos; n pair<int, int> s[M]; n int n, m, q, a[N][N], f[N][N], h[N][N], len[N][N], dx[10], dy[10], deg[N][N], n     g[M]; n string str; n bool check(int x, int y) { return x > 0 && x <= n && y > 0 && y <= m; } n void DFS(int sx, int sy) { n   int top = 1, spe = 0; n   int v = 0; n   s[1] = pair<int, int>(sx, sy); n   while (!spe) { n     int cx = s[top].first, cy = s[top].second; n     h[cx][cy] = top; n     int nx = cx + dx[a[cx][cy]], ny = cy + dy[a[cx][cy]]; n     if (!check(nx, ny)) n       spe = top; n     else { n       if (f[nx][ny]) { n         spe = top + 1; n         v = f[nx][ny]; n         s[top + 1] = pair<int, int>(nx, ny); n         break; n       } n       if (!h[nx][ny]) n         s[++top] = pair<int, int>(nx, ny); n       else n         spe = h[nx][ny]; n     } n   } n   for (auto i = spe; i <= top; i++) v |= (1 << (a[s[i].first][s[i].second])); n   for (auto i = 1; i <= top; i++) f[s[i].first][s[i].second] = v; n   for (auto i = spe - 1; i >= 1; i--) n     len[s[i].first][s[i].second] = len[s[i + 1].first][s[i + 1].second] + 1; n } n void prepare() { n   cin >> n >> m >> q; n   for (auto i = 1; i <= n; i++) { n     cin >> str; n     for (auto j = 1; j <= m; j++) a[i][j] = str[j - 1] -  0 ; n   } n   for (auto i = 0; i < alp; i++) cin >> dx[i] >> dy[i]; n   for (auto i = 1; i <= n; i++) n     for (auto j = 1; j <= m; j++) { n       int nx = i + dx[a[i][j]], ny = j + dy[a[i][j]]; n       if (check(nx, ny)) deg[nx][ny]++; n     } n   for (auto i = 1; i <= n; i++) n     for (auto j = 1; j <= m; j++) n       if (!deg[i][j]) { n         DFS(i, j); n         pos.push_back(pair<int, int>(i, j)); n       } n   for (auto i = 1; i <= n; i++) n     for (auto j = 1; j <= m; j++) n       if (!f[i][j]) { n         DFS(i, j); n         pos.push_back(pair<int, int>(i, j)); n       } n } n string solve() { n   int n1 = str.length(); n   g[n1] = 0; n   for (auto i = n1 - 1; i >= 0; i--) g[i] = g[i + 1] | (1 << (str[i] -  0 )); n   for (auto x : pos) { n     int cur = 0; n     pair<int, int> cx = x; n     while (cur < n1 && len[cx.first][cx.second]) { n       int v = a[cx.first][cx.second]; n       if (str[cur] -  0  == v) cur++; n       int nx = cx.first + dx[v], ny = cx.second + dy[v]; n       cx = pair<int, int>(nx, ny); n     } n     if ((g[cur] & f[cx.first][cx.second]) == g[cur]) return  YES ; n   } n   return  NO ; n } n int main() { n   prepare(); n   while (q--) { n     cin >> str; n     cout << solve() <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int i, j, n, arr[26]; n   string s; n   memset(&arr, -1, sizeof(arr)); n   cin >> s; n   int l = s.length(); n   if (l < 26) { n     cout << -1; n     return 0; n   } n   i = 0; n   j = 0; n   while (j < l) { n     if (j - i == 26) break; n     if (s[j] ==  ? ) n       j++; n     else if (arr[s[j] -  A ] != -1) { n       int temp = i; n       i = arr[s[j] -  A ] + 1; n       for (; temp < i; temp++) n         if (s[temp] !=  ? ) arr[s[temp] -  A ] = -1; n     } else if (s[j] !=  ?  && arr[s[j] -  A ] == -1) { n       arr[s[j] -  A ] = j; n       j++; n     } n   } n   if (j - i == 26) { n     int temp = i, temp2; n     for (temp2 = 0; temp2 < 26; temp2++) { n       if (arr[temp2] == -1) { n         while (s[temp] !=  ?  && temp < j) temp++; n         s[temp] =  A  + temp2; n       } n     } n     while (i--) { n       if (s[i] ==  ? ) s[i] = s[i + 26]; n     } n     while (j < l) { n       if (s[j] ==  ? ) s[j] = s[j - 26]; n       j++; n     } n     cout << s; n   } else n     cout << -1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double a, v, l, d, w, ans, L, t1, t2, v_2; n int main() { n   scanf( %lf%lf%lf%lf%lf , &a, &v, &l, &d, &w); n   L = l - d; n   double A = w * w; n   if (v <= w || A >= 2 * a * d) { n     if (2 * a * l >= v * v) { n       ans = v / a; n       ans += (l - v * v / 2 / a) / v; n     } else n       ans = sqrt(2 * l / a); n   } else { n     double V = sqrt((2 * a * d + w * w) / 2); n     if (V >= v) { n       t1 = v / a + (v - w) / a; n       t1 += (d - (2 * v * v - w * w) / 2 / a) / v; n     } else { n       t1 = V / a + (V - w) / a; n     } n     if (w * w + 2 * a * L <= v * v) { n       v_2 = sqrt(w * w + 2 * a * L); n       t2 = (v_2 - w) / a; n     } else { n       t2 = (v - w) / a; n       t2 += (L - (v * v - w * w) / (2 * a)) / v; n     } n     ans = t1 + t2; n   } n   printf( %.12lf , ans); n   return 0; n } n


// shirley smokes weed n #include <bits/stdc++.h> n #define sz(v) ((int)(v).size()) n #define all(v) (v).begin(), (v).end() n using namespace std; n using lint = long long; n using pi = pair<int, int>; n const int MAXN = 400; n  n int n, m, s[MAXN], e[MAXN], x[MAXN]; n int dp[1 << 18], indep[1 << 18], w[1 << 18], tk[1 << 18]; n  n int main(){ n  scanf( %d %d ,&n,&m); n  for(int i=0; i<m; i++){ n   scanf( %d %d %d ,&s[i],&e[i],&x[i]); n   s[i]--; e[i]--; n  } n  for(int i=1; i<(1<<n); i++){ n   indep[i] = 1; n   for(int j=0; j<m; j++){ n    int b1 = (i >> s[j]) & 1; n    int b2 = (i >> e[j]) & 1; n    if(b1 && b2) indep[i] = 0; n    if(b1 == b2) continue; n    if(b1 == 1 && b2 == 0) w[i] = 1e9; n    else w[i] += x[j]; n   } n  } n  dp[0] = 0; n  for(int i=1; i<(1<<n); i++){ n   dp[i] = 1e9; n   if(w[i] > 1e8) continue; n   for(int j=i; j; j=(j-1)&i){ n    if(indep[j] && dp[i] > dp[i ^ j]){ n     dp[i] = dp[i ^ j]; n     tk[i] = j; n    } n   } n   dp[i] += w[i]; n  } n  vector<int> a(n); n  for(int i = (1 << n) - 1; i; i ^= tk[i]){ n   for(int j = 0; j < n; j++){ n    if((i >> j) & 1) a[j]++; n   } n  } n  for(int i=0; i<n; i++) printf( %d  , n + 1 - a[i]); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   long long int n; n   cin >> n; n   string s; n   cin >> s; n   long long int p = 0, q = 0, t = 0, r = 0, l = 0; n   for (long long int i = 0; s[i] !=   0 ; i++) { n     if (s[i] ==  1  || s[i] ==  2  || s[i] ==  3 ) { n       p = 1; n     } n     if (s[i] ==  1  || s[i] ==  4  || s[i] ==  7 ) { n       r = 1; n     } n     if (s[i] ==  7  || s[i] ==  0  || s[i] ==  9 ) { n       q = 1; n     } n     if (s[i] ==  3  || s[i] ==  6  || s[i] ==  9 ) { n       t = 1; n     } n     if (s[i] ==  0 ) { n       l = 1; n     } n   } n   if (q == 1 && p == 1 && r == 1 && t == 1) { n     cout <<  YES  << endl; n   } else if (l == 1 && p == 1) { n     cout <<  YES  << endl; n   } else { n     cout <<  NO  << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int N, M; n char A[2020][2020]; n int B[2020][2020]; n int D[2020][2020]; n queue<int> qx; n queue<int> qy; n int dx[4] = {1, 0, -1, 0}; n int dy[4] = {0, 1, 0, -1}; n long long ans; n int main() { n   scanf( %d%d , &N, &M); n   for (int i = 1; i <= N; i++) { n     scanf( %s , A[i] + 1); n   } n   ans = N * M; n   for (int i = 2; i < N; i++) { n     for (int j = 2; j < M; j++) { n       B[i][j] = 1; n       for (int d = 0; d < 4; d++) { n         if (A[i][j] != A[i + dx[d]][j + dy[d]]) B[i][j] = 0; n       } n     } n   } n   for (int i = 1; i <= N; i++) { n     for (int j = 1; j <= M; j++) { n       if (B[i][j]) { n         int c = 0; n         for (int d = 0; d < 4; d++) { n           c += B[i + dx[d]][j + dy[d]]; n         } n         if (c < 4) { n           D[i][j] = 1; n           qx.push(i); n           qy.push(j); n         } n       } n     } n   } n   while (qx.size()) { n     int x = qx.front(); n     qx.pop(); n     int y = qy.front(); n     qy.pop(); n     for (int i = 0; i < 4; i++) { n       int nx = x + dx[i], ny = y + dy[i]; n       if (!B[nx][ny] || D[nx][ny]) continue; n       D[nx][ny] = D[x][y] + 1; n       qx.push(nx); n       qy.push(ny); n     } n   } n   for (int i = 1; i <= N; i++) n     for (int j = 1; j <= M; j++) ans += D[i][j]; n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<vector<int> > lang(105); n vector<vector<int> > emp(105); n vector<int> visited(105, false); n int n, m; n void dfs(int i) { n   if (!visited[i]) { n     visited[i] = true; n     int n_lang = emp[i].size(); n     for (int k = 0; k < n_lang; k++) { n       int lg = emp[i][k]; n       int n_emp = lang[lg].size(); n       for (int x = 0; x < n_emp; x++) { n         dfs(lang[lg][x]); n       } n     } n   } n } n int main() { n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     int n_l; n     cin >> n_l; n     for (int j = 0; j < n_l; j++) { n       int lg; n       cin >> lg; n       lg--; n       lang[lg].push_back(i); n       emp[i].push_back(lg); n     } n   } n   bool flag = false; n   for (int i = 0; i < m; i++) { n     if (lang[i].size() != 0) { n       flag = true; n       break; n     } n   } n   if (flag == false) { n     cout << n << endl; n     return 0; n   } n   int cc = 0; n   for (int i = 0; i < n; i++) { n     if (!visited[i]) { n       cc++; n       dfs(i); n     } n   } n   cout << cc - 1 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 505; n int x[N]; n int n, m; n int main() { n   cin >> n >> m; n   for (int i = 0; i < n; i++) cin >> x[i]; n   double ret = 0; n   while (m--) { n     int u, v, c; n     cin >> u >> v >> c; n     u--, v--; n     ret = max(1.0 * (x[u] + x[v]) / c, ret); n   } n   cout << fixed << setprecision(12) << ret <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[205], cnt[105], used[105], ans[205], sz[3]; n int push(int x) { n   if (x) { n     sz[x]++; n     return x; n   } else if (sz[1] < sz[2]) { n     sz[1]++; n     return 1; n   } else { n     sz[2]++; n     return 2; n   } n } n int main() { n   cin >> n; n   for (int i = 1; i <= n * 2; i++) { n     cin >> a[i]; n     cnt[a[i]]++; n   } n   for (int i = 1; i <= n * 2; i++) n     if (cnt[a[i]] > 1 && used[a[i]] < 2) ans[i] = push(++used[a[i]]); n   for (int i = 1; i <= n * 2; i++) n     if (cnt[a[i]] == 1) ans[i] = push(0); n   cout << sz[1] * sz[2] <<   n ; n   for (int i = 1; i <= n * 2; i++) { n     if (!ans[i]) ans[i] = push(0); n     cout << ans[i] <<    ; n   } n } n