#include <bits/stdc++.h> n const int mod = 998244353; n using namespace std; n inline int read() { n   int f = 1, x = 0; n   char s = getchar(); n   while (!isdigit(s)) { n     if (s ==  - ) f = -1; n     s = getchar(); n   } n   while (isdigit(s)) { n     x = (x << 1) + (x << 3) + (s ^ 48), s = getchar(); n   } n   return x * f; n } n int n, a[500100], is[500100], prime[500100], tot, ans1[500100], ans2[500100]; n inline void init(void) { n   for (int i = 2; i <= 4000; i++) { n     if (!is[i]) prime[++tot] = i; n     for (int j = 1; j <= tot && i * prime[j] <= 4000; j++) { n       is[i * prime[j]] = 1; n       if (i % prime[j] == 0) break; n     } n   } n } n inline int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } n signed main(void) { n   n = read(); n   init(); n   for (int i = 1; i <= n; i++) { n     ans1[i] = ans2[i] = -1; n     a[i] = read(); n     int t = a[i]; n     for (int j = 1; j <= tot; j++) { n       int mul = 1; n       while (t % prime[j] == 0) t /= prime[j], mul *= prime[j]; n       if (mul != 1 && a[i] / mul != 1) ans1[i] = mul, ans2[i] = a[i] / mul; n     } n     if (t != 1 && a[i] / t != 1) ans1[i] = t, ans2[i] = a[i] / t; n   } n   for (int i = 1; i <= n; i++) printf( %d  , ans1[i]); n   puts(  ); n   for (int i = 1; i <= n; i++) printf( %d  , ans2[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   string name; n   int version; n }; n int main(void) { n   int n; n   cin >> n; n   map<pair<string, int>, vector<pair<string, int> > > m; n   pair<string, int> init; n   for (int i = 0; i < n; i++) { n     pair<string, int> cur; n     cin >> cur.first >> cur.second; n     cur.second *= -1; n     if (i == 0) init = cur; n     int k; n     cin >> k; n     for (int j = 0; j < k; j++) { n       pair<string, int> node; n       cin >> node.first >> node.second; n       node.second *= -1; n       m[cur].push_back(node); n     } n   } n   set<string> names; n   set<pair<string, int> > ans; n   set<pair<string, int> > layer; n   names.insert(init.first); n   layer.insert(init); n   while (!layer.empty()) { n     set<pair<string, int> > new_layer; n     for (set<pair<string, int> >::iterator it = layer.begin(); n          it != layer.end(); it++) { n       for (int k = 0; k < m[*it].size(); k++) { n         new_layer.insert(m[*it][k]); n       } n     } n     layer.clear(); n     for (set<pair<string, int> >::iterator it = new_layer.begin(); n          it != new_layer.end(); it++) { n       if (names.find(it->first) != names.end()) continue; n       names.insert(it->first); n       ans.insert(*it); n       layer.insert(*it); n     } n   } n   cout << ans.size() << endl; n   for (set<pair<string, int> >::iterator it = ans.begin(); it != ans.end(); n        it++) { n     cout << it->first <<     << -it->second << endl; n   } n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n template <typename T> n istream& operator>>(istream& is, vector<T>& v) { n   for (T& x : v) is >> x; n   return is; n } n template <typename T> n ostream& operator<<(ostream& os, const vector<T>& v) { n   for (int i = 0; i < (int)v.size(); i++) { n     os << v[i] << (i + 1 == (int)v.size() ?    :    ); n   } n   return os; n } n template <typename T, typename U> n ostream& operator<<(ostream& os, const pair<T, U>& p) { n   os <<  (  << p.first <<  ,  << p.second <<  ) ; n   return os; n } n template <typename T, typename U> n ostream& operator<<(ostream& os, const map<T, U>& m) { n   os <<  { ; n   for (auto itr = m.begin(); itr != m.end();) { n     os <<  (  << itr->first <<  ,  << itr->second <<  ) ; n     if (++itr != m.end()) os <<  , ; n   } n   os <<  } ; n   return os; n } n template <typename T, typename U> n ostream& operator<<(ostream& os, const unordered_map<T, U>& m) { n   os <<  { ; n   for (auto itr = m.begin(); itr != m.end();) { n     os <<  (  << itr->first <<  ,  << itr->second <<  ) ; n     if (++itr != m.end()) os <<  , ; n   } n   os <<  } ; n   return os; n } n template <typename T> n ostream& operator<<(ostream& os, const set<T>& s) { n   os <<  { ; n   for (auto itr = s.begin(); itr != s.end();) { n     os << *itr; n     if (++itr != s.end()) os <<  , ; n   } n   os <<  } ; n   return os; n } n template <typename T> n ostream& operator<<(ostream& os, const multiset<T>& s) { n   os <<  { ; n   for (auto itr = s.begin(); itr != s.end();) { n     os << *itr; n     if (++itr != s.end()) os <<  , ; n   } n   os <<  } ; n   return os; n } n template <typename T> n ostream& operator<<(ostream& os, const unordered_set<T>& s) { n   os <<  { ; n   for (auto itr = s.begin(); itr != s.end();) { n     os << *itr; n     if (++itr != s.end()) os <<  , ; n   } n   os <<  } ; n   return os; n } n template <typename T> n ostream& operator<<(ostream& os, const deque<T>& v) { n   for (int i = 0; i < (int)v.size(); i++) { n     os << v[i] << (i + 1 == (int)v.size() ?    :    ); n   } n   return os; n } n template <int i, typename T> n void print_tuple(ostream&, const T&) {} n template <int i, typename T, typename H, class... Args> n void print_tuple(ostream& os, const T& t) { n   if (i) os <<  , ; n   os << get<i>(t); n   print_tuple<i + 1, T, Args...>(os, t); n } n template <typename... Args> n ostream& operator<<(ostream& os, const tuple<Args...>& t) { n   os <<  { ; n   print_tuple<0, tuple<Args...>, Args...>(os, t); n   return os <<  } ; n } n void debug_out() { cerr <<   n ; } n template <class Head, class... Tail> n void debug_out(Head&& head, Tail&&... tail) { n   cerr << head; n   if (sizeof...(Tail) > 0) cerr <<  ,  ; n   debug_out(move(tail)...); n } n template <typename T> n T gcd(T x, T y) { n   return y != 0 ? gcd(y, x % y) : x; n } n template <typename T> n T lcm(T x, T y) { n   return x / gcd(x, y) * y; n } n int topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); } n int topbit(long long t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); } n int botbit(signed a) { return a == 0 ? 32 : __builtin_ctz(a); } n int botbit(long long a) { return a == 0 ? 64 : __builtin_ctzll(a); } n int popcount(signed t) { return __builtin_popcount(t); } n int popcount(long long t) { return __builtin_popcountll(t); } n bool ispow2(int i) { return i && (i & -i) == i; } n template <class T> n T ceil(T x, T y) { n   assert(y >= 1); n   return (x > 0 ? (x + y - 1) / y : x / y); n } n template <class T> n T floor(T x, T y) { n   assert(y >= 1); n   return (x > 0 ? x / y : (x - y + 1) / y); n } n template <class T1, class T2> n inline bool chmin(T1& a, T2 b) { n   if (a > b) { n     a = b; n     return true; n   } n   return false; n } n template <class T1, class T2> n inline bool chmax(T1& a, T2 b) { n   if (a < b) { n     a = b; n     return true; n   } n   return false; n } n const int INF = 1e9; n const long long IINF = 1e18; n const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; n const char dir[4] = { D ,  R ,  U ,  L }; n const long long MOD = 1000000007; n const int MAX_X = 2010; n void solve() { n   int n; n   cin >> n; n   vector<int> dp(MAX_X, INF), ndp(MAX_X, INF); n   dp[0] = 0; n   for (; n--;) { n     int a; n     cin >> a; n     for (int i = 0; i < MAX_X; i++) { n       if (dp[i] == INF) continue; n       if (i + a < MAX_X) chmin(ndp[i + a], max(0, dp[i] - a)); n       chmin(ndp[max(0, i - a)], dp[i] + a); n       dp[i] = INF; n     } n     swap(dp, ndp); n   } n   int ans = INF; n   for (int i = 0; i < MAX_X; i++) chmin(ans, i + dp[i]); n   cout << ans <<   n ; n } n int main() { n   cin.tie(0); n   ios::sync_with_stdio(false); n   int t; n   cin >> t; n   for (; t--;) solve(); n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:102400000,102400000 ) n using namespace std; n int n, m, ans, flag; n bool vis[10]; n string fac[7] = { 1869 ,  1968 ,  1689 ,  6198 ,  8691 ,  8916 ,  1896 }; n string s, ss, anss, xx =  0000 ; n int main() { n   int i, j, t, pos, x, y, u, v, w; n   while (cin >> s) { n     memset(vis, 0, sizeof(vis)); n     ss =   ; n     for (i = 0; i < s.length(); i++) { n       if (s[i] ==  1 ) { n         if (!vis[1]) n           vis[1] = 1; n         else n           ss += s[i]; n       } else if (s[i] ==  6 ) { n         if (!vis[6]) n           vis[6] = 1; n         else n           ss += s[i]; n       } else if (s[i] ==  8 ) { n         if (!vis[8]) n           vis[8] = 1; n         else n           ss += s[i]; n       } else if (s[i] ==  9 ) { n         if (!vis[9]) n           vis[9] = 1; n         else n           ss += s[i]; n       } else n         ss += s[i]; n     } n     flag = 0; n     for (i = 0; i < ss.length(); i++) { n       if (ss[i] !=  0 ) { n         flag = 1; n         char c = ss[i]; n         ss[i] = ss[0]; n         ss[0] = c; n       } n     } n     if (!flag) { n       cout <<  1869  << ss << endl; n     } else { n       t = 0; n       for (i = 0; i < ss.length(); i++) { n         t = (t * 10 + ss[i] -  0 ) % 7; n       } n       for (i = 0; i < 4; i++) { n         t = (t * 10 + xx[i] -  0 ) % 7; n       } n       if (t) t = 7 - t; n       cout << ss << fac[t] << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 998244353; n const int MN = 1 << 8; n int exp(int x, int n) { n   int ret = 1; n   while (n) { n     if (n & 1) ret = 1LL * ret * x % mod; n     x = 1LL * x * x % mod; n     n >>= 1; n   } n   return ret; n } n int inv(int x) { return exp(x, mod - 2); } n int fact[MN], invf[MN]; n int comb(int n, int k) { n   return 1LL * fact[n] * invf[k] % mod * invf[n - k] % mod; n } n int fa[MN]; n void init() { n   for (int i = 0; i < MN; i++) fa[i] = i; n } n int find(int u) { n   if (fa[u] == u) n     return u; n   else n     return fa[u] = find(fa[u]); n } n void mrg(int u, int v) { n   u = find(u); n   v = find(v); n   if (u == v) return; n   fa[v] = u; n } n int cc[MN][MN]; n int dp(int n, int k) { n   int &ret = cc[n][k]; n   if (ret != -1) return ret; n   if (k == 0) return ret = n == 0; n   ret = 0; n   for (int i = 1; i <= n; i++) { n     ret += n         1LL * comb(n - 1, i - 1) * fact[i - 1] % mod * dp(n - i, k - 1) % mod; n     ret %= mod; n   } n   return ret; n } n int N; n int A[MN], B[MN], del[MN], chk[MN], tcnt[3], ans[MN]; n int main() { n   fact[0] = 1; n   for (int i = 1; i < MN; i++) { n     fact[i] = 1LL * fact[i - 1] * i % mod; n   } n   for (int i = 0; i < MN; i++) { n     invf[i] = inv(fact[i]); n   } n   memset(cc, -1, sizeof(cc)); n   scanf( %d , &N); n   for (int i = 0; i < N; i++) { n     scanf( %d , &A[i]); n     A[i]--; n   } n   for (int i = 0; i < N; i++) { n     scanf( %d , &B[i]); n     B[i]--; n   } n   int ccnt = 0; n   init(); n   for (int i = 0; i < N; i++) { n     if (A[i] != -1 && B[i] != -1) { n       if (find(A[i]) == find(B[i])) { n         del[find(A[i])] = 1; n         ccnt++; n       } else n         mrg(A[i], B[i]); n     } n   } n   vector<int> V; n   for (int i = 0; i < N; i++) n     if (!del[i] && fa[i] == i) V.push_back(i); n   if (V.size() == 0) { n     for (int i = 0; i < N; i++) { n       printf( %d  , (N - i == ccnt)); n     } n     return 0; n   } n   for (int i = 0; i < N; i++) { n     if (A[i] != -1 && B[i] == -1) { n       chk[find(A[i])] |= 1; n     } n     if (A[i] == -1 && B[i] != -1) { n       chk[find(B[i])] |= 2; n     } n   } n   for (int i = 0; i < V.size(); i++) { n     tcnt[chk[V[i]]]++; n   } n   for (int i = 1; i <= (int)V.size() - tcnt[3]; i++) { n     int tmp = 0; n     for (int j = 0; j <= min(tcnt[1], tcnt[2]); j++) { n       if (j % 2) n         tmp += mod - 1LL * comb(tcnt[1], j) * comb(tcnt[2], j) % mod * fact[j] % n                          mod * dp((int)V.size() - tcnt[3] - j, i) % mod; n       else n         tmp += 1LL * comb(tcnt[1], j) * comb(tcnt[2], j) % mod * fact[j] % mod * n                dp((int)V.size() - tcnt[3] - j, i) % mod; n       tmp %= mod; n     } n     ans[ccnt + i] = tmp; n   } n   for (int i = 0; i < N; i++) { n     printf( %d  , 1LL * ans[N - i] * fact[tcnt[0]] % mod); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int zero(int n) { n   if (n == 0) return 0; n   if (n % 10 == 0) return zero(n / 10); n   return 10 * zero(n / 10) + (n % 10); n } n int main() { n   int a, b; n   cin >> a >> b; n   if (zero(a) + zero(b) == zero(a + b)) { n     cout <<  YES ; n   } else { n     cout <<  NO ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T1, class T2> n bool chmin(T1 &a, T2 b) { n   return b < a && (a = b, true); n } n template <class T1, class T2> n bool chmax(T1 &a, T2 b) { n   return a < b && (a = b, true); n } n struct MaxFlow { n   struct edge { n     int to, cap, rev; n   }; n   vector<int> dist, it; n   vector<vector<edge>> g; n   MaxFlow(int n) : g(n), dist(n), it(n) {} n   void add(int u, int v, int c) { n     g[u].push_back({v, c, (int)g[v].size()}); n     g[v].push_back({u, 0, (int)g[u].size() - 1}); n   } n   int calc(int s, int t) { n     int res = 0; n     while (bfs(s, t)) { n       fill(it.begin(), it.end(), 0); n       while (int f = dfs(s, t, 1e9)) res += f; n     } n     return res; n   } n   bool bfs(int s, int t) { n     fill(dist.begin(), dist.end(), -1); n     queue<int> q; n     q.push(s); n     dist[s] = 0; n     while (!q.empty()) { n       int v = q.front(); n       q.pop(); n       for (edge e : g[v]) n         if (e.cap > 0 && dist[e.to] < 0) { n           dist[e.to] = dist[v] + 1; n           q.push(e.to); n         } n     } n     return dist[t] >= 0; n   } n   int dfs(int v, int t, int f) { n     if (v == t) return f; n     for (int &i = it[v]; i < g[v].size(); i++) { n       edge &e = g[v][i]; n       if (e.cap > 0 && dist[v] < dist[e.to]) { n         if (int d = dfs(e.to, t, min(f, e.cap))) { n           e.cap -= d; n           g[e.to][e.rev].cap += d; n           return d; n         } n       } n     } n     return 0; n   } n }; n template <class T1, class T2> n void sortPair(vector<T1> &v1, vector<T2> &v2) { n   vector<pair<T1, T2>> u(v1.size()); n   for (int i = 0; i < u.size(); i++) u[i] = make_pair(v1[i], v2[i]); n   sort(u.begin(), u.end()); n   for (int i = 0; i < u.size(); i++) tie(v1[i], v2[i]) = u[i]; n } n bool solve() { n   int n, b, q; n   cin >> n >> b >> q; n   vector<int> u(q), v(q); n   for (int i = 0; i < (q); i++) scanf( %d %d , &u[i], &v[i]); n   u.push_back(b); n   v.push_back(n); n   q++; n   sortPair(u, v); n   for (int i = 0; i < (q - 1); i++) n     if (v[i] > v[i + 1]) return false; n   static int sum[10101][5]; n   for (int i = 0; i < (10101); i++) sum[i][i % 5]++; n   for (int i = (1); i < (10101); i++) n     for (int j = 0; j < (5); j++) sum[i][j] += sum[i - 1][j]; n   int lowSum = 0, highSum = 0; n   MaxFlow mf(q + q * 5 + 5 + 2); n   int src = q + q * 5 + 5; n   int dst = src + 1; n   int prevU = 0; n   int prevV = 0; n   for (int mod = 0; mod < (5); mod++) mf.add(q + q * 5 + mod, dst, n / 5); n   for (int i = 0; i < (q); i++) { n     int mustUse = v[i] - prevV; n     int num = u[i] - prevU; n     mf.add(src, i, mustUse); n     for (int mod = 0; mod < (5); mod++) { n       int mm = sum[u[i]][mod] - sum[prevU][mod]; n       mf.add(i, q + i * 5 + mod, mm); n       mf.add(q + i * 5 + mod, q + q * 5 + mod, mm); n     } n     prevU = u[i]; n     prevV = v[i]; n   } n   return mf.calc(src, dst) == n; n } n int main() { n   cout << (solve() ?  fair  :  unfair ) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   char a[15], b[15]; n   while (cin >> a >> b) { n     int len_a = strlen(a); n     sort(a, a + len_a); n     int k = 0; n     for (int i = 0; i < len_a; i++) { n       if (a[i] !=  0 ) { n         k = i; n         break; n       } n     } n     char tmp; n     tmp = a[0]; n     a[0] = a[k]; n     a[k] = tmp; n     if (strcmp(a, b) == 0) n       cout <<  OK  << endl; n     else n       cout <<  WRONG_ANSWER  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n const int oo = 0x3f3f3f3f, mod = 1e9 + 7; n using namespace std; n int dx[]{1, -1, 0, 0, 1, 1, -1, -1}; n int dy[]{0, 0, 1, -1, 1, -1, 1, -1}; n long long OO = (1LL * oo * oo); n void K_K() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n } n int main() { n   K_K(); n   long long n, mx = 0, mxidx = 0; n   cin >> n; n   map<long long, long long> mp; n   vector<long long> v(n + 2); n   vector<pair<long long, pair<long long, long long>>> ans; n   for (int i = 1; i <= n; i++) { n     cin >> v[i], mp[v[i]]++; n     if (mp[v[i]] > mx) mxidx = i, mx = mp[v[i]]; n   } n   for (int i = mxidx - 1; i > 0; i--) { n     if (v[i] > v[i + 1]) { n       v[i] -= abs(v[i] - v[i + 1]); n       ans.push_back({2, {i, i + 1}}); n     } else if (v[i] < v[i + 1]) { n       ans.push_back({1, {i, i + 1}}); n       v[i] += abs(v[i] - v[i + 1]); n     } n   } n   for (int i = mxidx + 1; i <= n; i++) { n     if (v[i] > v[i - 1]) { n       v[i] -= abs(v[i] - v[i - 1]); n       ans.push_back({2, {i, i - 1}}); n     } else if (v[i] < v[i - 1]) { n       ans.push_back({1, {i, i - 1}}); n       v[i] += abs(v[i] - v[i - 1]); n     } n   } n   cout << (int)(ans.size()) <<   n ; n   for (int i = 0; i < (int)(ans.size()); i++) n     cout << ans[i].first <<     << ans[i].second.first <<     n          << ans[i].second.second <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int a, b, c; n unsigned long ans = 0; n unordered_map<int, int> um; n void eval(int x) { n   int t = 0; n   for (int i = 1; i <= sqrt(x); ++i) { n     if (i * i == x) n       t += 1; n     else if (x % i == 0) n       t += 2; n   } n   ans += t; n   um[x] = t; n } n int main() { n   cin >> a >> b >> c; n   for (int i = 1; i <= a; ++i) { n     for (int j = 1; j <= b; ++j) { n       for (int k = 1; k <= c; ++k) { n         if (um.find(i * j * k) != um.end()) n           ans = (ans + um[i * j * k]) % 1073741824LL; n         else n           eval(i * j * k); n       } n     } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int64_t M = 1000000007; n const int64_t MM = 998244353; n template <typename T, typename U> n static inline void amin(T &x, U y) { n   if (y < x) x = y; n } n template <typename T, typename U> n static inline void amax(T &x, U y) { n   if (x < y) x = y; n } n int64_t _runtimeTerror_() { n   int64_t n; n   cin >> n; n   string s; n   cin >> s; n   vector<long long> d_cnt(n + 1, 0), d_sum(n + 1), u_cnt(n + 1), n       u_sum(n + 1, 0); n   for (int64_t i = 1; i <= n; ++i) { n     u_cnt[i] = u_cnt[i - 1]; n     u_sum[i] = u_sum[i - 1]; n     d_cnt[i] = d_cnt[i - 1]; n     d_sum[i] = d_sum[i - 1]; n     if (s[i - 1] ==  D ) { n       d_cnt[i] += 1; n       d_sum[i] += i; n     } else { n       u_cnt[i] += 1; n       u_sum[i] += i; n     } n   } n   for (int64_t i = 1; i <= n; ++i) { n     int64_t ans = 0; n     if (s[i - 1] ==  U ) { n       int64_t r_d = d_cnt[n] - d_cnt[i]; n       int64_t l_u = u_cnt[i - 1]; n       if (r_d > l_u) { n         auto it = lower_bound(d_cnt.begin(), d_cnt.end(), d_cnt[i] + l_u + 1) - n                   d_cnt.begin(); n         ans += (d_sum[it] - d_sum[i] - (i * (l_u + 1))) * 2; n         ans -= (u_sum[i - 1]) * 2; n         ans += l_u * i * 2; n         ans += i; n       } else { n         ans += (d_sum[n] - d_sum[i] - (d_cnt[n] - d_cnt[i]) * i) * 2; n         ans += (n - i + 1); n         auto it = n             lower_bound(u_cnt.begin(), u_cnt.end(), u_cnt[i - 1] - r_d + 1) - n             u_cnt.begin(); n         ans -= (u_sum[i - 1] - u_sum[it - 1]) * 2; n         ans += (r_d * i) * 2; n       } n       cout << ans <<    ; n     } else { n       int64_t r_d = d_cnt[n] - d_cnt[i]; n       int64_t l_u = u_cnt[i - 1]; n       if (r_d >= l_u) { n         auto it = lower_bound(d_cnt.begin(), d_cnt.end(), d_cnt[i] + l_u) - n                   d_cnt.begin(); n         ans += (d_sum[it] - d_sum[i] - (i * (l_u))) * 2; n         ans -= (u_sum[i - 1]) * 2; n         ans += l_u * i * 2; n         ans += i; n       } else { n         ans += (d_sum[n] - d_sum[i] - (d_cnt[n] - d_cnt[i]) * i) * 2; n         ans += (n - i + 1); n         auto it = lower_bound(u_cnt.begin(), u_cnt.end(), u_cnt[i - 1] - r_d) - n                   u_cnt.begin(); n         ans -= (u_sum[i - 1] - u_sum[it - 1]) * 2; n         ans += ((r_d + 1) * i) * 2; n       } n       cout << ans <<    ; n     } n   } n   return 0; n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int64_t TESTS = 1; n   while (TESTS--) _runtimeTerror_(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, k; n   string s; n   int x; n   cin >> n >> m >> k; n   int a[n][m]; n   int b[n][m]; n   int c[n][m]; n   for (int i = 0; i < n; i++) { n     cin >> s; n     for (int j = 0; j < m; j++) { n       cin >> x; n       a[i][j] = x; n       cin >> x; n       b[i][j] = x; n       cin >> x; n       c[i][j] = x; n     } n   } n   int sum_max = 0; n   int sum; n   vector<pair<int, int> > abc; n   abc.reserve(m); n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       if (i == j) continue; n       sum = 0; n       abc.clear(); n       for (int ii = 0; ii < m; ii++) { n         abc.push_back(make_pair(b[j][ii] - a[i][ii], c[i][ii])); n       } n       sort(abc.begin(), abc.end()); n       int left = k; n       for (int ii = m - 1; ii >= 0 && left > 0; ii--) { n         int d = min(left, abc[ii].second); n         if (abc[ii].first > 0) { n           sum += d * abc[ii].first; n           left -= d; n         } n       } n       if (sum > sum_max) { n         sum_max = sum; n       } n     } n   } n   cout << sum_max << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3e5 + 7; n const int M = N * 3; n const int inf = 1e9 + 7; n const int base = 1e9 + 9; n const double pi = acos(-1); n const double ep = 1e-11; n pair<pair<int, int>, int> p[N]; n int n; n int vp, vs; n int sqr(int x) { return x * x; } n double sqr(double x) { return x * x; } n int dist(pair<pair<int, int>, int> a, pair<pair<int, int>, int> b) { n   return sqr(a.first.first - b.first.first) + n          sqr(a.first.second - b.first.second) + sqr(a.second - b.second); n } n pair<pair<double, double>, double> curr(double t) { n   for (int i = 1; i <= n; i++) { n     double d = sqrt(dist(p[i - 1], p[i])) + ep; n     if (d >= t * vs) { n       pair<pair<double, double>, double> tmp; n       tmp.first.first = t * vs * (p[i].first.first - p[i - 1].first.first) / d + n                         p[i - 1].first.first; n       tmp.first.second = n           t * vs * (p[i].first.second - p[i - 1].first.second) / d + n           p[i - 1].first.second; n       tmp.second = n           t * vs * (p[i].second - p[i - 1].second) / d + p[i - 1].second; n       return tmp; n     } else { n       t -= d / vs; n     } n   } n   return make_pair(make_pair(0, 0), 0); n } n int main() { n   cin >> n; n   double t = 0; n   for (int i = (int)0; i < (int)n + 1; i++) { n     int u, v, c; n     cin >> u >> v >> c; n     p[i] = make_pair(make_pair(u, v), c); n   } n   cin >> vp >> vs; n   { cin >> p[n + 1].first.first >> p[n + 1].first.second >> p[n + 1].second; } n   for (int i = (int)1; i < (int)n + 1; i++) n     t += sqrt(dist(p[i - 1], p[i])) / vs; n   if (dist(p[n], p[n + 1]) > t * t * vp * vp + ep) { n     cout <<  NO ; n     return 0; n   } n   cout <<  YES n ; n   double lo = 0, hi = t + ep, g, res = -1; n   pair<pair<double, double>, double> ans = curr(35); n   do { n     g = (lo + hi) / 2; n     pair<pair<double, double>, double> tmp = curr(g); n     double di = sqr(p[n + 1].first.first - tmp.first.first) + n                 sqr(p[n + 1].first.second - tmp.first.second) + n                 sqr(p[n + 1].second - tmp.second); n     ans = tmp; n     res = g; n     if (di <= sqr(g) * sqr(vp) + ep) { n       res = g; n       hi = g - ep; n     } else n       lo = g + ep; n   } while (lo + ep < hi); n   printf( %.9lf n , res); n   printf( %.9lf %.9lf %.9lf , ans.first.first, ans.first.second, ans.second); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 300010; n int sufp[N], freq[N][2]; n int mi = 0, ma = 0, par = 0; n void add(int x) { n   if (x >= mi and x <= ma) { n     if (x % 2 == par) n       mi = 0; n     else n       mi = 1; n   } else if (x > ma) { n     mi = x - ma; n   } else { n     mi = mi - x; n   } n   if (x % 2) par = 1 - par; n   ma += x; n } n void reset() { mi = ma = par = 0; } n int main() { n   int n; n   scanf( %d , &n); n   vector<int> v, p; n   for (int i = 0; i < n; i++) { n     long long x; n     scanf( %lld , &x); n     v.push_back(__builtin_popcountll(x)); n     p.push_back(v.back() % 2); n   } n   for (int i = n - 1; i >= 0; i--) { n     sufp[i] = p[i]; n     if (i + 1 < n) sufp[i] ^= sufp[i + 1]; n   } n   for (int i = 0; i < n; i++) { n     freq[i][0] += sufp[i] == 0; n     freq[i][1] += sufp[i] == 1; n     if (i) { n       freq[i][0] += freq[i - 1][0]; n       freq[i][1] += freq[i - 1][1]; n     } n   } n   long long ans = 0; n   for (int i = 0; i < n; i++) { n     int pos = i; n     reset(); n     while (pos >= 0 and (mi > 1 or ma < 60)) { n       add(v[pos]); n       if (mi == 0) ans++; n       pos--; n     } n     if (pos < 0) continue; n     int base = sufp[pos + 1]; n     ans += freq[pos][par ^ base]; n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n set<string> ans; n string a; n int dp[1000010][5]; n int main() { n   cin >> a; n   int len = a.size(); n   reverse(a.begin(), a.end()); n   for (int i = 1; i < len - 5; ++i) { n     if (i == 1) { n       string s1 =   ; n       s1 += a[i]; n       s1 += a[i - 1]; n       ans.insert(s1); n       dp[1][0] = 1; n     } n     if (i == 2) { n       string s1 =   ; n       s1 += a[i]; n       s1 += a[i - 1]; n       s1 += a[i - 2]; n       ans.insert(s1); n       dp[2][1] = 1; n     } n     if (i >= 3 && (a[i] != a[i - 2] || a[i - 1] != a[i - 3]) && n         dp[i - 2][0] == 1) { n       string s1 =   ; n       s1 += a[i]; n       s1 += a[i - 1]; n       ans.insert(s1); n       dp[i][0] = 1; n     } n     if (i >= 2 && dp[i - 2][1] == 1) { n       string s1 =   ; n       s1 += a[i]; n       s1 += a[i - 1]; n       ans.insert(s1); n       dp[i][0] = 1; n     } n     if (i - 3 >= 0 && dp[i - 3][0] == 1) { n       string s1 =   ; n       s1 += a[i]; n       s1 += a[i - 1]; n       s1 += a[i - 2]; n       ans.insert(s1); n       dp[i][1] = 1; n     } n     if (i - 5 >= 0 && n         (a[i] != a[i - 3] || a[i - 1] != a[i - 4] || a[i - 2] != a[i - 5]) && n         dp[i - 3][1] == 1) { n       string s1 =   ; n       s1 += a[i]; n       s1 += a[i - 1]; n       s1 += a[i - 2]; n       ans.insert(s1); n       dp[i][1] = 1; n     } n   } n   cout << ans.size() << endl; n   for (set<string>::iterator it = ans.begin(); it != ans.end(); ++it) { n     cout << *it << endl; n   } n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   long long upper_limit = 1e9; n   long long lower_limit = -1e9; n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     int c, d; n     cin >> c >> d; n     if (d == 2) { n       upper_limit = min(upper_limit, 1899LL); n     } else { n       lower_limit = max(lower_limit, 1900LL); n     } n     lower_limit += c; n     upper_limit += c; n   } n   if (upper_limit < lower_limit) { n     cout <<  Impossible ; n   } else if (upper_limit > 1e8) { n     cout <<  Infinity ; n   } else { n     cout << upper_limit; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int pi = 3.14159265; n const int INF = 1 << 30; n const int maxn = 1e5; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n, k; n   string s; n   cin >> n >> k; n   cin >> s; n   int t = s.find( T ); n   int g = s.find( G ); n   if (g > t) swap(t, g); n   for (int i = g; i < n && i <= t; i += k) { n     if (s[i] ==  # ) { n       cout <<  NO  << endl; n       return 0; n     } n     if (i == t) { n       cout <<  YES  << endl; n       return 0; n     } n   } n   cout <<  NO  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n int n, x[5000 + 5], y[5000 + 5], tot; n int color[5000 + 5]; n bool flag; n int ksm(int b, int k) { n   int s = 1; n   while (k > 0) { n     if (k & 1) s = (1ll * s * b) % mod; n     k >>= 1; n     b = (1ll * b * b) % mod; n   } n   return s % mod; n } n int dis(int id1, int id2) { n   return abs(x[id1] - x[id2]) + abs(y[id1] - y[id2]); n } n void clearall() { n   flag = true; n   tot = 0; n   memset(color, 0, sizeof(color)); n } n void dfs(int u, int lim) { n   if (flag) { n     for (int i = 1; i <= n; i++) { n       if (flag) { n         if (u == i || dis(u, i) <= lim) continue; n         if (!color[i]) { n           color[i] = color[u] ^ 1; n           dfs(i, lim); n         } else { n           if (color[i] != color[u]) continue; n           flag = false; n           return; n         } n       } n     } n   } n } n bool check(int lim) { n   int i, j; n   clearall(); n   for (i = 1; i <= n; i++) n     if (!color[i]) { n       tot++; n       color[i] = 2; n       dfs(i, lim); n       if (!flag) break; n     } n   return flag; n } n int main() { n   int i, ans = -1; n   scanf( %d , &n); n   for (i = 1; i <= n; i++) scanf( %d%d , &x[i], &y[i]); n   int l = 0, r = 10000; n   while (l < r - 5) { n     int mid = (l + r) / 2; n     if (check(mid)) n       r = mid; n     else n       l = mid; n   } n   for (i = l; i <= r; i++) n     if (check(i)) { n       ans = i; n       break; n     } n   printf( %d n , ans); n   printf( %d n , ksm(2, tot)); n } n


#include <bits/stdc++.h> n using namespace std; n int dx[] = {1, -1, 0, 0}; n int dy[] = {0, 0, -1, 1}; n void arquivo() { n   freopen(  ,  r , stdin); n   freopen(  ,  w , stdout); n } n const int N = 5000100; n int n; n long long a, b; n int v[N]; n bool memo[N][3]; n long long pd[N][3]; n int atPrime; n long long func(int pos, int op) { n   if (pos == n) return 0LL; n   if (memo[pos][op]) return pd[pos][op]; n   long long ret = 1000000000000000001LL; n   if (!op) { n     ret = func(pos + 1, 1) + a; n     if (v[pos] % atPrime == 0) ret = min(ret, func(pos + 1, 0)); n     if ((v[pos] + 1) % atPrime == 0) ret = min(ret, func(pos + 1, 0) + b); n     if ((v[pos] - 1) % atPrime == 0) ret = min(ret, func(pos + 1, 0) + b); n   } else if (op == 1) { n     ret = func(pos + 1, 1) + a; n     if (v[pos] % atPrime == 0) ret = min(ret, func(pos + 1, 2)); n     if ((v[pos] + 1) % atPrime == 0) ret = min(ret, func(pos + 1, 2) + b); n     if ((v[pos] - 1) % atPrime == 0) ret = min(ret, func(pos + 1, 2) + b); n   } else { n     if (v[pos] % atPrime == 0) ret = min(ret, func(pos + 1, 2)); n     if ((v[pos] + 1) % atPrime == 0) ret = min(ret, func(pos + 1, 2) + b); n     if ((v[pos] - 1) % atPrime == 0) ret = min(ret, func(pos + 1, 2) + b); n   } n   memo[pos][op] = 1; n   return pd[pos][op] = ret; n } n void fatora(long long x, set<int> &all) { n   for (long long i = 2; i * i <= x; ++i) { n     if (x % i == 0) { n       all.insert(i); n       while (x % i == 0) x /= i; n     } n   } n   if (x > 1) all.insert(x); n } n int main() { n   scanf( %d %lld %lld , &n, &a, &b); n   set<int> all; n   long long x; n   for (int i = 0; i < n; ++i) { n     scanf( %d , v + i); n   } n   for (int i = 0; i < 1; ++i) { n     x = v[i]; n     fatora(x, all); n     x = v[i] + 1; n     fatora(x, all); n     x = v[i] - 1; n     fatora(x, all); n   } n   for (int i = n - 1; i < n; ++i) { n     x = v[i]; n     fatora(x, all); n     x = v[i] + 1; n     fatora(x, all); n     x = v[i] - 1; n     fatora(x, all); n   } n   long long ans = (long long)(n - 1LL) * a; n   for (set<int>::iterator it = all.begin(); it != all.end(); it++) { n     atPrime = *it; n     if (atPrime == 1) continue; n     memset(memo, 0, sizeof memo); n     for (int j = n - 1; j >= 0; --j) { n       func(j, 0); n       func(j, 1); n       func(j, 2); n     } n     ans = min(ans, func(0, 0)); n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n T gcd(T a, T b) { n   if (a == 0) return b; n   return gcd(b % a, a); n } n template <typename T> n T pow(T a, T b, long long m) { n   T ans = 1; n   while (b > 0) { n     if (b % 2 == 1) ans = (ans * a) % m; n     b /= 2; n     a = (a * a) % m; n   } n   return ans % m; n } n int main() { n   string s; n   cin >> s; n   int n; n   cin >> n; n   int from[n]; n   char s1, s2; n   string to[n]; n   long long a[10], b[10]; n   for (int i = 0; i < n; i++) { n     cin >> from[i] >> to[i]; n   } n   for (int i = 0; i < n; i++) { n     to[i] = to[i].substr(2); n   } n   for (int i = 0; i < 10; i++) { n     a[i] = 10; n     b[i] = i; n   } n   for (int i = n - 1; i >= 0; i--) { n     long long p = 1, q = 0; n     for (int j = 0; j < to[i].length(); j++) { n       int c = to[i][j] -  0 ; n       long long p2 = (a[c] * p) % 1000000007; n       long long q2 = (a[c] * q + b[c]) % 1000000007; n       p = p2; n       q = q2; n     } n     a[from[i]] = p; n     b[from[i]] = q; n   } n   long long ans = 0; n   for (int i = 0; i < s.length(); i++) { n     int c = s[i] -  0 ; n     ans = (ans * a[c] + b[c]) % 1000000007; n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n const int32_t M = 1e9 + 7; n int32_t mod_inv(int32_t a, int32_t m) { n   int32_t g = m, r = a, x = 0, y = 1; n   while (r != 0) { n     int32_t q = g / r; n     g %= r; n     swap(g, r); n     x -= q * y; n     swap(x, y); n   } n   return x < 0 ? x + m : x; n } n int gcdi(int a, int b) { n   if (b == 0) { n     return a; n   } n   return gcdi(b, a % b); n } n long long gcdl(long long a, long long b) { n   if (b == 0) { n     return a; n   } n   return gcdl(b, a % b); n } n long long lcm(long long a, long long b) { return (a / gcdl(a, b)) * b; } n int factorial(int n) { n   if (n == 0) return 1; n   return n * factorial(n - 1); n } n bool isPrime(long long n) { n   if (n <= 1) return false; n   if (n <= 3) return true; n   if (n % 2 == 0 || n % 3 == 0) return false; n   for (long long i = 5; i * i <= n; i = i + 6) n     if (n % i == 0 || n % (i + 2) == 0) return false; n   return true; n } n void FAST() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n } n void swap_ll(long long *a, long long *b) { n   long long c; n   c = *a; n   *a = *b; n   *b = c; n } n void swap_int(int *a, int *b) { n   int c; n   c = *a; n   *a = *b; n   *b = c; n } n void swap_char(char *a, char *b) { n   char c; n   c = *a; n   *a = *b; n   *b = c; n } n bool mycomp(pair<int, int> &a, pair<int, int> &b) { n   if (a.first == b.first) return a.second < b.second; n   return a.first < b.first; n } n long long fast_pow(long long a, long long p) { n   long long res = 1; n   while (p) { n     if (p % 2 == 0) { n       a = a * 1ll * a % 000000007; n       p /= 2; n     } else { n       res = res * 1ll * a % 000000007; n       p--; n     } n   } n   return res; n } n long long fact(long long n) { n   long long res = 1; n   for (long long i = 1; i <= n; i++) { n     res = res * 1ll * i % 000000007; n   } n   return res; n } n long long nCk(long long n, long long k) { n   return fact(n) * 1ll * fast_pow(fact(k), 000000007 - 2) % 000000007 * 1ll * n          fast_pow(fact(n - k), 000000007 - 2) % 000000007; n } n long long firstDigit(long long n) { n   long long digits = (long long)log10(n); n   n = (long long)(n / pow(10, digits)); n   return n; n } n long long find(long long n, long long d) { n   while (n > 0) { n     if (n % 10 == d) return 1; n     n /= 10; n   } n   return 0; n } n struct node { n   int data; n   node *next; n   node(int d) { n     data = d; n     next = NULL; n   } n }; n void ffin() { FAST(); } n long long add(vector<long long> a, long long b, long long e) { n   long long sum = 0; n   for (long long i = b; i < e; i++) { n     sum += a[i]; n   } n   return sum; n } n void decToBinary(int n) { n   int binaryNum[32]; n   int i = 0; n   while (n > 0) { n     binaryNum[i] = n % 2; n     n = n / 2; n     i++; n   } n   for (int j = i - 1; j >= 0; j--) cout << binaryNum[j]; n } n bool isInteger(double N) { n   int X = N; n   double temp2 = N - X; n   if (temp2 > 0) { n     return false; n   } n   return true; n } n bool check(vector<int> &v) { n   int n = (int)((v).size()); n   for (int i = 0; i < n; i++) { n     for (int j = i + 1; j < n; j++) { n       for (int k = j + 1; k < n; k++) { n         if ((v[i] >= v[j] && v[j] >= v[k]) || (v[i] <= v[j] && v[j] <= v[k])) { n           return true; n         } n       } n     } n   } n   return false; n } n bool digitscheck(long long n) { n   while (n != 0) { n     long long y = n % 10; n     if (y != 0 and y != 1) { n       return false; n     } n     n = n / 10; n   } n   return true; n } n const long long N = 5e+5; n long long a[N][5]; n long long better(long long i, long long j) { n   long long cnt = 0; n   for (int k = 0; k < 5; k++) { n     cnt += (a[i][k] < a[j][k]); n   } n   return (cnt >= 3); n } n void solve() { n   long long n; n   cin >> n; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < 5; j++) cin >> a[i][j]; n   long long best = 0; n   for (int i = 1; i < n; i++) { n     if (better(i, best)) best = i; n   } n   bool ok = true; n   for (int i = 0; i < n; i++) { n     if (i == best) continue; n     ok &= better(best, i); n   } n   if (!ok) best = -2; n   cout << best + 1 <<   n ; n } n int main() { n   ffin(); n   long long tests = 1; n   cin >> tests; n   while (tests--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1E5 + 5; n long long m, n, k, alice[MAXN], bob[MAXN]; n bool cmp(int x, int y) { return x > y; } n int main() { n   cin >> n >> m >> k; n   for (int i = 0; i < n; i++) cin >> alice[i]; n   for (int i = 0; i < m; i++) cin >> bob[i]; n   if (n > m) { n     cout <<  YES  << endl; n     return 0; n   } n   sort(alice, alice + n, cmp); n   sort(bob, bob + m, cmp); n   for (int i = 0; i < n; i++) { n     if (alice[i] > bob[i]) { n       cout <<  YES  << endl; n       return 0; n     } n   } n   cout <<  NO  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, i; n   scanf( %d , &n); n   long long int a[n]; n   set<long long int> x; n   map<long long int, set<int> > m; n   for (i = 0; i < n; i++) { n     scanf( %lld , &a[i]); n     x.insert(a[i]); n     m[a[i]].insert(i); n   } n   while (!x.empty()) { n     long long int j = *x.begin(); n     if (m[j].size() >= 2) { n       m[j].erase(m[j].begin()); n       int k = *(m[j].begin()); n       m[j].erase(m[j].begin()); n       m[2 * j].insert(k); n       x.insert(2 * j); n     } else { n       x.erase(x.begin()); n     } n   } n   vector<pair<int, long long int> > ans; n   map<long long int, set<int> >::iterator it; n   for (it = m.begin(); it != m.end(); it++) { n     if ((it->second).size() == 1) { n       ans.push_back(make_pair(*(it->second).begin(), it->first)); n     } n   } n   sort(ans.begin(), ans.end()); n   cout << ans.size() << endl; n   for (i = 0; i < ans.size(); i++) { n     printf( %lld  , ans[i].second); n   } n   printf(  n ); n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n using min_queue = priority_queue<T, vector<T>, greater<T>>; n const long long MOD = 1e9 + 7; n int N; n int cnt[6]; n int res[100100]; n vector<int> v[6]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   const vector<string> sizes = { S ,  M ,  L ,  XL ,  XXL ,  XXXL }; n   map<string, int> inv; n   { n     int k = 0; n     for (auto s : sizes) inv[s] = k++; n   } n   for (int i = 0; i < 6; i++) { n     cin >> cnt[i]; n   } n   cin >> N; n   for (int i = 0; i < N; i++) { n     string s; n     cin >> s; n     string t; n     for (char c : s) { n       if (c ==  , ) break; n       t += c; n     } n     int k = inv[t]; n     if (s == t) { n       cnt[k]--; n       res[i] = k; n     } else { n       v[k].push_back(i); n     } n   } n   bool good = 1; n   for (int i = 0; i < 5; i++) { n     if (cnt[i] < 0) { n       good = 0; n       break; n     } n     while (!v[i].empty() && cnt[i] > 0) { n       cnt[i]--; n       res[v[i].back()] = i; n       v[i].pop_back(); n     } n     while (!v[i].empty() && cnt[i + 1] > 0) { n       cnt[i + 1]--; n       res[v[i].back()] = i + 1; n       v[i].pop_back(); n     } n     if (!v[i].empty() || cnt[i + 1] < 0) { n       good = 0; n       break; n     } n   } n   if (!good) { n     cout <<  NO n ; n     return 0; n   } n   cout <<  YES n ; n   for (int i = 0; i < N; i++) cout << sizes[res[i]] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   cin >> n >> k; n   char a[n + 1][n + 1]; n   int ans[n + 1][n + 1]; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) { n       cin >> a[i][j]; n       ans[i][j] = 0; n     } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n - k + 1; j++) { n       bool x = true; n       for (int t = j; t < j + k; t++) { n         if (a[i][t] ==  # ) { n           x = false; n           break; n         } n       } n       if (x) { n         for (int t = j; t < j + k; t++) ans[i][t]++; n       } n     } n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n - k + 1; j++) { n       bool x = true; n       for (int t = j; t < j + k; t++) { n         if (a[t][i] ==  # ) { n           x = false; n           break; n         } n       } n       if (x) { n         for (int t = j; t < j + k; t++) ans[t][i]++; n       } n     } n   } n   int mx = ans[1][1]; n   int p = 1, q = 1; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) { n       if (ans[i][j] > mx) { n         mx = ans[i][j]; n         p = i; n         q = j; n       } n     } n   cout << p <<     << q; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long ans, l; n int n, m, N, i, j, k, s[3005], a[3005][3005], b[3005]; n int main() { n   scanf( %d%d , &n, &m); n   for (i = 1; i <= n; i++) { n     scanf( %d%d , &j, &k); n     a[j][++s[j]] = k; n   } n   for (i = 1; i <= m; i++) sort(a[i] + 1, a[i] + s[i] + 1, greater<int>()); n   ans = 1LL << 60; n   for (i = 1; i <= n; i++) { n     l = 0; n     N = 0; n     for (j = 2; j <= m; j++) { n       for (k = 1; k < i && k <= s[j]; k++) b[N++] = a[j][k]; n       for (; k <= s[j]; k++) l += a[j][k]; n     } n     sort(b, b + N); n     for (j = 0; j < i + N - n; j++) l += b[j]; n     ans = min(ans, l); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 7; n int n, m, a[maxn]; n map<int, int> mp, rmp; n int main() { n   scanf( %d%d , &n, &m); n   int id; n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     if (a[i] == m) id = i; n   } n   int cnt = 0; n   for (int i = id; i >= 1; i--) { n     if (a[i] < m) n       cnt++; n     else if (a[i] > m) n       cnt--; n     mp[cnt]++; n   } n   long long ans = 0; n   cnt = 0; n   for (int i = id; i <= n; i++) { n     if (a[i] < m) n       cnt++; n     else if (a[i] > m) n       cnt--; n     ans += 1LL * mp[-cnt] + 1LL * mp[-cnt - 1]; n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const unsigned long long O = 2e9; n const double E = 1e-9; n const double pi = 3.1415926536; n int DX[] = {1, -1, 0, 0}; n int DY[] = {0, 0, 1, -1}; n int main() { n   int n, m; n   cin >> n >> m; n   int out = n * 2 + 1, in = 1; n   if (m > n * 2) { n     for (int i = 0; i < m; i++) { n       if (i % 2 == 0 && out <= m) n         cout << out++ <<    ; n       else n         cout << in++ <<    ; n     } n   } else n     for (int i = 1; i <= m; i++) cout << i <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 3e5 + 5; n set<int> s; n int x[maxn]; n int n, k; n int main() { n   while (~scanf( %d , &n)) { n     k = 0; n     if (!s.empty()) s.clear(); n     for (int i = 1; i <= n; i++) { n       int p; n       scanf( %d , &p); n       if (s.count(p) == 0) n         s.insert(p); n       else { n         x[k++] = i; n         s.clear(); n       } n     } n     if (k == 0) n       printf( -1 n ); n     else { n       if (x[k - 1] != n) x[k - 1] = n; n       printf( %d n , k); n       printf( 1 %d n , x[0]); n       for (int i = 0; i < k - 1; i++) printf( %d %d n , x[i] + 1, x[i + 1]); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n const int MAXN = 100005; n int n, val[MAXN], ch[MAXN][2], fa[MAXN], maxv[MAXN], size[MAXN], root; n inline int max(int a, int b) { return a > b ? a : b; } n inline int min(int a, int b) { return a > b ? b : a; } n inline void maintain(int x) { n   size[x] = size[ch[x][0]] + size[ch[x][1]] + 1; n   maxv[x] = max(max(maxv[ch[x][0]], maxv[ch[x][1]]), val[x]); n } n void rotate(int x) { n   int y = fa[x], d = ch[y][1] == x; n   if (fa[x] = fa[y]) ch[fa[y]][ch[fa[y]][1] == y] = x; n   if (ch[y][d] = ch[x][d ^ 1]) fa[ch[y][d]] = y; n   fa[ch[x][d ^ 1] = y] = x; n   maintain(y), maintain(x); n } n void splay(int &rt, int x) { n   int f = fa[rt], y, z; n   while ((y = fa[x]) != f && (z = fa[y]) != f) { n     rotate((ch[z][1] == y) == (ch[y][1] == x) ? y : x); n     rotate(x); n   } n   if (y != f) rotate(x); n   rt = x; n } n void select(int &rt, int k) { n   int x = rt; n   while (size[ch[x][0]] + 1 != k) { n     if (k <= size[ch[x][0]]) n       x = ch[x][0]; n     else { n       k -= size[ch[x][0]] + 1; n       x = ch[x][1]; n     } n   } n   splay(rt, x); n } n void getPos(int &rt, int v) { n   int x = rt; n   while (true) { n     if (ch[x][1] && maxv[ch[x][1]] > v) n       x = ch[x][1]; n     else if (ch[x][0] && val[x] <= v) n       x = ch[x][0]; n     else n       break; n   } n   splay(rt, x); n } n void insert(int &rt, int p, int pos) { n   if (!pos) { n     select(rt, 1); n     fa[ch[rt][0] = p] = rt; n     rotate(p); n     rt = p; n     return; n   } n   select(rt, pos); n   int tmp = ch[rt][1]; n   if (!tmp) { n     fa[ch[rt][1] = p] = rt; n     rotate(p); n     rt = p; n     return; n   } n   fa[ch[rt][1] = p] = rt; n   fa[ch[p][1] = tmp] = p; n   splay(rt, tmp); n } n void traverse(int x) { n   if (!x) return; n   traverse(ch[x][0]); n   printf( %d  , x); n   traverse(ch[x][1]); n } n int main() { n   int tmp, i; n   scanf( %d , &n); n   for (i = 1; i <= n; ++i) { n     scanf( %d%d , &val[i], &tmp); n     if (i == 1) { n       size[root = 1] = 1; n       maxv[1] = val[1]; n       continue; n     } n     getPos(root, val[i]); n     if (val[root] < val[i]) n       insert(root, i, i - 1 - min(tmp, i - 1)); n     else n       insert(root, i, i - 1 - min(tmp, size[ch[root][1]])); n   } n   traverse(root); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1000000007; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int c, s, v = 0, m, d, k, y; n     cin >> c >> s; n     m = (s / 2); n     d = (c / s); n     k = (c % s); n     if (m == 0) { n       v = c; n     } else { n       if (k >= m) { n         v = (c - k + m); n       } else { n         v = c; n       } n     } n     cout << v << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6; n vector<long long> v[105]; n map<long long, long long> mp; n int vis[105]; n long long cnt, cnt2; n void dfs(int s) { n   vis[s] = 1; n   cnt += v[s].size(); n   cnt2++; n   for (int i = 0; i < v[s].size(); i++) { n     if (!vis[v[s][i]]) dfs(v[s][i]); n   } n } n int main() { n   ios_base ::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long n, m, b, ans = 0, ans2, x, y, z, i, s; n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     cin >> x >> y; n     v[x].push_back(y); n     v[y].push_back(x); n   } n   for (int i = 1; i <= n; i++) { n     if (!vis[i]) { n       cnt = 0; n       cnt2 = 0; n       dfs(i); n       if (cnt2 * 2 == cnt) { n         if (cnt2 % 2) ans++; n       } n     } n   } n   if ((n - ans) % 2 == 0) n     cout << ans; n   else n     cout << ans + 1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n vector<int> V; n int solve() { n   V.clear(); n   cin >> n; n   for (int i = 0; i < n; ++i) { n     int a; n     cin >> a; n     V.emplace_back(a); n   } n   sort(V.begin(), V.end()); n   V.erase(unique(V.begin(), V.end()), V.end()); n   return V.size(); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int t; n   cin >> t; n   while (t--) cout << solve() <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> v[201]; n long long arr[201]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long n, i, j, c, max1, min1, temp, temp1, n1; n   cin >> n; n   for (i = 0; i < 2 * n; i++) { n     cin >> arr[i]; n     v[arr[i]].push_back(i); n   } n   for (i = 1; i <= n; i++) { n     if (abs(v[i][0] - v[i][1]) == 1) { n       continue; n     } else { n       break; n     } n   } n   if (i > n) { n     cout <<  0 ; n     exit(0); n   } n   c = 0; n   while (1) { n     for (i = 0; i < 2 * n; i++) { n       if (abs(v[arr[i]][0] - v[arr[i]][1]) == 1) { n         continue; n       } else { n         max1 = max(v[arr[i]][0], v[arr[i]][1]); n         min1 = min(v[arr[i]][0], v[arr[i]][1]); n         c += max1 - min1 - 1; n         for (j = max1; j > min1 + 1; j--) { n           if (v[arr[j]][0] == j) { n             temp = 0; n           } else { n             temp = 1; n           } n           if (v[arr[j - 1]][0] == j - 1) { n             temp1 = 0; n           } else { n             temp1 = 1; n           } n           v[arr[j]][temp] = j - 1; n           v[arr[j - 1]][temp1] = j; n           swap(arr[j], arr[j - 1]); n         } n       } n     } n     for (i = 1; i <= n; i++) { n       if (abs(v[i][0] - v[i][1]) == 1) { n         continue; n       } else { n         break; n       } n     } n     if (i > n) { n       cout << c; n       exit(0); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long n; n const long long maxn = 2e5 + 100; n const long long mod = 1e9 + 7; n vector<long long> G[maxn]; n long long dp[maxn]; n long long oth[maxn]; n long long fs[maxn], ts[maxn]; n long long ans[maxn]; n void myinit() { n   cin >> n; n   for (long long i = 2; i <= n; i++) { n     long long u; n     cin >> u; n     G[u].push_back(i); n   } n   memset(dp, 0, sizeof(dp)); n   memset(oth, 0, sizeof(oth)); n } n void dfs1(long long x) { n   long long s = 1; n   for (long long i = 0; i < G[x].size(); i++) { n     long long tmp = G[x][i]; n     dfs1(tmp); n     s = s * (dp[tmp] + 1); n     s %= mod; n   } n   dp[x] = s; n } n void dfs2(long long x) { n   fs[0] = 1; n   for (long long i = 0; i < G[x].size(); i++) { n     long long tmp = G[x][i]; n     fs[i + 1] = fs[i] * (dp[tmp] + 1); n     fs[i + 1] %= mod; n   } n   ts[G[x].size() + 1] = 1; n   for (long long i = (long long)G[x].size() - 1; i >= 0; i--) { n     long long tmp = G[x][i]; n     ts[i + 1] = (ts[i + 2] * (dp[tmp] + 1)) % mod; n   } n   for (long long i = 0; i < G[x].size(); i++) { n     oth[G[x][i]] = ((fs[i] * ts[i + 2] % mod) * oth[x] + 1) % mod; n   } n   for (long long i = 0; i < G[x].size(); i++) { n     dfs2(G[x][i]); n   } n } n void dfs3(long long x) { n   for (long long i = 0; i < G[x].size(); i++) { n     long long tmp = G[x][i]; n     ans[tmp] = (oth[tmp] * dp[tmp]) % mod; n     dfs3(tmp); n   } n } n void solve() { n   myinit(); n   dfs1(1); n   oth[1] = 1; n   dfs2(1); n   ans[1] = dp[1]; n   dfs3(1); n   for (long long i = 1; i <= n; i++) { n     cout << ans[i] <<    ; n   } n } n int main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void print2dvector(vector<vector<T> > v) { n   cout <<  A 2d vector:  << endl; n   int a = v.size(); n   int b = v[0].size(); n   if (a <= 15 && b <= 15) { n     for (int i = 0; i < a; i++) { n       for (int j = 0; j < b; j++) { n         cout << setw(4) << v[i][j]; n       } n       cout << endl; n     } n   } n   if (a > 15 && b > 15) { n     for (int i = 0; i < 9; i++) { n       for (int j = 0; j < 9; j++) { n         cout << setw(4) << v[i][j]; n       } n       cout <<       n            <<  .   .   .  << setw(4) << v[i][b - 3] << setw(4) << v[i][b - 2] n            << setw(4) << v[i][b - 1] << endl; n     } n     for (int i = 0; i < 15; i++) { n       cout << setw(4) <<  . ; n     } n     cout << endl; n     for (int i = a - 3; i < a; i++) { n       for (int j = 0; j < 9; j++) { n         cout << setw(4) << v[i][j]; n       } n       cout <<       n            <<  .   .   .  << setw(4) << v[i][b - 3] << setw(4) << v[i][b - 2] n            << setw(4) << v[i][b - 1] << endl; n     } n   } n   if (a > 15) { n     for (int i = 0; i < 9; i++) { n       for (int j = 0; j < b; j++) { n         cout << setw(4) << v[i][j]; n       } n       cout << endl; n     } n     for (int i = 0; i < b; i++) { n       cout << setw(4) <<  . ; n     } n     cout << endl; n     for (int i = a - 3; i < a; i++) { n       for (int j = 0; j < b; j++) { n         cout << setw(4) << v[i][j]; n       } n       cout << endl; n     } n   } n   if (b > 15) { n     for (int i = 0; i < a; i++) { n       for (int j = 0; j < 9; j++) { n         cout << setw(4) << v[i][j]; n       } n       cout <<       n            <<  .   .   .  << setw(4) << v[i][b - 3] << setw(4) << v[i][b - 2] n            << setw(4) << v[i][b - 1] << endl; n     } n   } n } n template <typename T> n void printvector(vector<T> v) { n   cout <<  A 1d vector:  << endl; n   int a = v.size(); n   if (a <= 15) { n     for (int i = 0; i < a; i++) { n       cout << setw(4) << v[i]; n     } n   } else { n     for (int i = 0; i < 9; i++) { n       cout << setw(4) << v[i]; n     } n     cout <<       n          <<  .   .   .  << setw(4) << v[a - 3] << setw(4) << v[a - 2] << setw(4) n          << v[a - 1]; n   } n   cout << endl; n } n int main() { n   int n, m; n   scanf( %d %d , &n, &m); n   vector<int> v(n, 0); n   for (int i = 0; i < n; i++) { n     scanf( %d , &v[i]); n   } n   double ans = 0; n   int a, b, c; n   for (int i = 0; i < m; i++) { n     scanf( %d %d %d , &a, &b, &c); n     ans = max(ans, 1.0 * (v[a - 1] + v[b - 1]) / c); n   } n   printf( %.12lf , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[200005], b[200005], vis[200005]; n bool cmp(int a, int b) { return a > b; } n int main() { n   int n; n   scanf( %d , &n); n   int numa = 0, numb = 0, flag = 0; n   for (int i = 0; i < n; i++) { n     int x; n     scanf( %d , &x); n     if (vis[x]) n       a[numa++] = x, vis[x]++; n     else n       b[numb++] = x, vis[x]++; n     if (vis[x] > 2) flag = 1; n   } n   if (flag) n     printf( NO n ); n   else { n     printf( YES n ); n     sort(a, a + numa); n     sort(b, b + numb, cmp); n     printf( %d n , numa); n     int flag1 = 0; n     for (int i = 0; i < numa; i++) { n       if (flag1) printf(   ); n       flag1 = 1; n       printf( %d , a[i]); n     } n     printf(  n ); n     flag1 = 0; n     printf( %d n , numb); n     for (int i = 0; i < numb; i++) { n       if (flag1) printf(   ); n       flag1 = 1; n       printf( %d , b[i]); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double pi = acos(-1); n const int N = 1005; n int cnt[N]; n bitset<N> dp; n int n, m; n int idx[N]; n int par[N]; n int xx[N]; n bool vis[N]; n int find(int x) { n   if (par[x] == x) return x; n   return par[x] = find(par[x]); n } n int main() { n   for (int i = 0; i <= 1000; i += 1) cnt[i] = 0; n   cin >> n >> m; n   int ix = 1; n   for (int i = 0; i <= n; i += 1) par[i] = i; n   int id = -1, tmp, x; n   bool flag = false; n   for (int i = 1; i <= n; i += 1) { n     cin >> xx[i]; n     if (xx[i] != 0) par[i] = find(par[xx[i]]); n     if (xx[i] == 0) { n       idx[i] = ix++; n     } n   } n   for (int i = 1; i <= n; i += 1) { n     if (find(par[i]) == find(par[m])) { n       continue; n     } n     cnt[idx[find(par[i])]]++; n   } n   int now = m; n   while (now != 0) { n     cnt[idx[par[m]]]++; n     now = xx[now]; n   } n   dp[cnt[idx[par[m]]]] = 1; n   for (int i = 1; i <= ix - 1; i += 1) { n     if (i != idx[par[m]]) { n       dp = (dp << cnt[i]) | dp; n     } n   } n   for (int i = 0; i <= n; i += 1) { n     if (dp[i] == 1) { n       cout << i << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MaxN = 33; n template <class T> n inline void relax(T &a, const T &b) { n   if (b > a) a = b; n } n int main() { n   int n; n   static int a[MaxN][MaxN]; n   cin >> n; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n; j++) scanf( %d , &a[i][j]); n   int l = (n + 1) / 2; n   int res = INT_MIN; n   for (int lastR = 0; lastR < (1 << l); lastR++) { n     int cur = 0; n     for (int r = 0; r < l; r++) { n       int resR = INT_MIN; n       for (int tv = 0; tv <= 1; tv++) { n         int curR = 0; n         for (int c = 0; c < l; c++) { n           int resRC = INT_MIN; n           for (int cv = 0; cv <= 1; cv++) { n             if (r == l - 1 && cv != (lastR >> c & 1)) continue; n             if (c == l - 1 && cv != tv) continue; n             int curRC = 0; n             curRC += cv == 0 ? a[r][c] : -a[r][c]; n             if (r + l < n) n               curRC += n                   (cv ^ (lastR >> c & 1)) == 0 ? a[r + l][c] : -a[r + l][c]; n             if (c + l < n) curRC += (cv ^ tv) == 0 ? a[r][c + l] : -a[r][c + l]; n             if (r + l < n && c + l < n) n               curRC += n                   (cv ^ (lastR >> c & 1) ^ tv ^ (lastR >> (l - 1) & 1)) == 0 n                       ? a[r + l][c + l] n                       : -a[r + l][c + l]; n             relax(resRC, curRC); n           } n           if (resRC == INT_MIN) { n             curR = INT_MIN; n             break; n           } n           curR += resRC; n         } n         relax(resR, curR); n       } n       cur += resR; n     } n     relax(res, cur); n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline void fast() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n } n long long gcd(long long n, long long m) { return m == 0 ? n : gcd(m, n % m); } n inline long long Set(long long n, long long pos) { n   return n = n | (1LL << pos); n } n inline long long Reset(long long n, long long pos) { n   return n = n & ~(1LL << pos); n } n inline bool Check(long long n, long long pos) { n   return (bool)(n & (1LL << pos)); n } n inline long long Count(long long n) { return __builtin_popcountll(n); } n long long const N = 3e5 + 5; n long long const MOD = 998244353; n long long color[N]; n vector<long long> adj[N]; n long long dp[N]; n long long res[N]; n long long dfs(long long node, long long p) { n   long long col = 0; n   for (auto c : adj[node]) { n     if (p == c) continue; n     long long temp = dfs(c, node); n     col += max(temp, 0LL); n   } n   col += color[node]; n   return dp[node] = col; n } n void adfs(long long node, long long p, long long up) { n   up = max(up, 0LL); n   res[node] = dp[node] + up; n   for (auto c : adj[node]) { n     if (p == c) continue; n     long long temp = dp[node] - max(dp[c], 0LL) + up; n     adfs(c, node, temp); n   } n } n void solve() { n   long long n; n   cin >> n; n   for (long long i = 1; i <= n; i++) { n     cin >> color[i]; n     if (color[i] == 0) color[i] = -1; n   } n   for (long long i = 1; i < n; i++) { n     long long u, v; n     cin >> u >> v; n     adj[u].push_back(v); n     adj[v].push_back(u); n   } n   dfs(1, -1); n   adfs(1, -1, 0); n   for (long long i = 1; i <= n; i++) cout << res[i] <<    ; n } n int32_t main() { n   fast(); n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n const long long INF = 1LL << 60; n const long long N = 2e5 + 5; n long long a[N]; n void solve() { n   long long n; n   cin >> n; n   vector<long long> vect; n   for (long long i = 1; i < n + 1; i++) { n     cin >> a[i]; n     if (a[i] != i) { n       vect.push_back(i); n     } n   } n   if ((long long)vect.size() == 0) { n     cout << 0; n     return; n   } n   bool fl = true; n   for (long long i = 1; i < (long long)vect.size(); i++) { n     if (vect[i] - vect[i - 1] > 1) fl = false; n   } n   if (fl) n     cout << 1; n   else n     cout << 2; n } n signed main() { n   std::ios::sync_with_stdio(false); n   cin.tie(NULL); n   long long t = 1; n   cin >> t; n   while (t--) { n     solve(); n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k, l, c, d, p, nl, np; n   cin >> n >> k >> l >> c >> d >> p >> nl >> np; n   int mls = k * l; n   int slices = c * d; n   int drinks = min(mls / nl, min(p / np, slices)) / n; n   cout << drinks << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int a[500010]; n char s[500010], temp[500010]; n int n; n long long Max = -1000, sum = 0; n void init() { n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   for (int i = 0; i < n; i++) { n     cin >> s[i]; n   } n } n void work() { n   for (int i = 0; i < n; i++) { n     if (s[i] ==  B ) sum += a[i]; n   } n   Max = sum; n   long long temp = sum; n   for (int i = n - 1; i >= 0; i--) { n     if (s[i] ==  A ) { n       sum += a[i]; n       if (sum > Max) Max = sum; n     } else { n       sum -= a[i]; n     } n   } n   sum = temp; n   for (int i = 0; i < n; i++) { n     if (s[i] ==  A ) { n       sum += a[i]; n       if (sum > Max) Max = sum; n     } else { n       sum -= a[i]; n     } n   } n } n int main() { n   init(); n   work(); n   cout << Max << endl; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /stack:200000000 ) n #pragma GCC optimize( Ofast ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) n #pragma GCC optimize( unroll-loops ) n using namespace std; n int main() { n   cin.tie(0); n   cout.tie(0); n   ios_base::sync_with_stdio(false); n   int n, m, q, p; n   cin >> n >> m >> q >> p; n   queue<pair<pair<int, int>, int>> qu; n   vector<vector<int>> g(n, vector<int>(m, 0)); n   vector<vector<char>> d(n, vector<char>(m)); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) { n       cin >> d[i][j]; n       if (d[i][j] ==  * ) g[i][j] = -1; n     } n   int si[4]{0, 0, -1, 1}; n   int sj[4]{1, -1, 0, 0}; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) n       if (d[i][j] !=  .  && d[i][j] !=  * ) { n         int cur = (int(d[i][j]) - int( A ) + 1) * q; n         g[i][j] += cur; n         qu.push({{i, j}, cur}); n         vector<vector<bool>> used(n, vector<bool>(m, false)); n         used[i][j] = true; n         while (!qu.empty()) { n           int x = qu.front().first.first; n           int y = qu.front().first.second; n           int qq = qu.front().second; n           for (int c = 0; c < 4; c++) { n             int tx = x + si[c]; n             int ty = y + sj[c]; n             if (tx > -1 && tx < n && ty > -1 && ty < m) n               if (!used[tx][ty] && g[tx][ty] != -1) { n                 g[tx][ty] += qq / 2; n                 used[tx][ty] = true; n                 if (qq > 1) qu.push({{tx, ty}, qq / 2}); n               } n           } n           qu.pop(); n         } n       } n   int ans = 0; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) n       if (g[i][j] > p) ans++; n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int r, g, b; n   cin >> r >> g >> b; n   if (r % 2 == 0) n     r = r / 2; n   else n     r = r / 2 + 1; n   if (g % 2 == 0) n     g = g / 2; n   else n     g = g / 2 + 1; n   if (b % 2 == 0) n     b = b / 2; n   else n     b = b / 2 + 1; n   int tmp1 = 3 * (r - 1); n   int tmp2 = 3 * (g - 1) + 1; n   int tmp3 = 3 * (b - 1) + 2; n   tmp1 = max(tmp1, tmp2); n   tmp1 = max(tmp1, tmp3); n   cout << tmp1 + 30; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 4e6 + 7; n const int inf = INT_MAX; n const long long inff = 1e18; n const long long mod = 1e9 + 7; n int d, s; n pair<int, int> dp[maxn]; n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> d >> s; n   for (int i = 0; i <= (d + 1) * (s + 1); i++) dp[i] = make_pair(-1, 0); n   queue<int> q; n   q.push(0); n   while (!q.empty()) { n     int now = q.front(); n     int sum = now / d, r = now % d; n     q.pop(); n     for (int i = 0; i < 10; i++) { n       if (sum + i <= s && (dp[(sum + i) * d + (r * 10 + i) % d].first == -1)) { n         dp[(sum + i) * d + (r * 10 + i) % d] = make_pair(now, i); n         q.push((sum + i) * d + (r * 10 + i) % d); n       } n     } n   } n   int now = s * d; n   if (dp[now].first == -1) n     return cout << -1 <<   n , 0; n   else { n     vector<int> v; n     while (now) { n       v.push_back(dp[now].second); n       now = dp[now].first; n     } n     reverse((v).begin(), (v).end()); n     for (int i : v) cout << i; n     cout <<   n ; n   } 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 n, r, c, i, d, j = 0, a = 0, b = 0; n   string s; n   cin >> s; n   n = s.size(); n   r = 1; n   c = n; n   while (c > 20) { n     r++; n     c = ceil((float)n / (float)r); n   } n   d = r * c - n; n   cout << r <<     << c << endl; n   while (j < r && d > 0) { n     string s1(s, a, c - 1); n     cout <<  *  << s1 << endl; n     a = a + c - 1; n     j++; n     d--; n   } n   while (j < r) { n     string s1(s, a, c); n     cout << s1 << endl; n     a = a + c; n     j++; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, p, m; n vector<vector<int> > g; n vector<char> used; n vector<long long> c, vs, es; n pair<int, int> dfs(int v) { n   used[v] = true; n   pair<int, int> ans(1, (int)g[v].size()); n   for (int u : g[v]) { n     if (used[u]) { n       continue; n     } n     pair<int, int> a = dfs(u); n     ans.first += a.first; n     ans.second += a.second; n   } n   return ans; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m >> p; n   g.resize(n); n   used.resize(n); n   c = vs = es = vector<long long>(p); n   for (int k = 0; k < p; k++) { n     cin >> c[k]; n     c[k]--; n   } n   for (int k = 0; k < m; k++) { n     int v, u; n     cin >> v >> u; n     v--; n     u--; n     g[v].push_back(u); n     g[u].push_back(v); n   } n   int i = -1; n   for (int k = 0; k < p; k++) { n     pair<int, int> ans = dfs(c[k]); n     vs[k] = ans.first; n     es[k] = ans.second / 2; n     if (i == -1 || vs[k] > vs[i]) { n       i = k; n     } n   } n   int rem = 0; n   for (int k = 0; k < n; k++) { n     if (!used[k]) { n       pair<int, int> ans = dfs(k); n       vs[i] += ans.first; n       es[i] += ans.second / 2; n     } n   } n   long long ans = 0; n   for (int k = 0; k < p; k++) { n     ans += vs[k] * (vs[k] - 1) / 2 - es[k]; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, max; n vector<int> a[100000]; n bool visited[100000] = {0}; n char col[100000] = { Z }; n void f(int x) { n   queue<int> q; n   q.push(x); n   visited[x] = 1; n   char temp =  A ; n   col[x] = temp; n   while (!q.empty()) { n     int p = q.front(); n     q.pop(); n     temp = col[p]; n     if (temp ==  A ) n       temp =  B ; n     else n       temp =  A ; n     for (int i = 0; i < a[p].size(); i++) { n       if (visited[a[p][i]] == false) { n         q.push(a[p][i]); n         visited[a[p][i]] = true; n         col[a[p][i]] = temp; n       } else if (col[a[p][i]] != temp) { n         puts( -1 ); n         exit(0); n       } n     } n   } n } n int main() { n   cin >> n >> m; n   int x, y; n   for (int i = 0; i < m; i++) { n     cin >> x >> y; n     x--; n     y--; n     a[x].push_back(y); n     a[y].push_back(x); n   } n   f(x); n   for (int i = 0; i < 100000; i++) { n     if (a[i].size() && visited[i] == 0) f(i); n   } n   int c1 = 0; n   for (int i = 0; i < 100000; i++) n     if (col[i] ==  B ) c1++; n   cout << c1 <<   n ; n   for (int i = 0; i < 100000; i++) n     if (col[i] ==  B ) cout << i + 1 <<    ; n   cout <<   n ; n   c1 = 0; n   for (int i = 0; i < 100000; i++) n     if (col[i] ==  A ) c1++; n   cout << c1 <<   n ; n   for (int i = 0; i < 100000; i++) n     if (col[i] ==  A ) cout << i + 1 <<    ; n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1e6 + 1; n const long long inf = 1e9 + 1; n const long long mod = 1e9 + 7; n const long double eps = 1e-6; n mt19937 rnd(58); n struct point { n   long double x, y; n   bool operator==(const point &right); n }; n bool point::operator==(const point &b) { return (x == b.x && y == b.y); } n long double dist(point a, point b) { n   return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y); n } n long double mindist(point A1, point A2, point B1, point B2) { n   long double x0 = A1.x - B1.x, y0 = A1.y - B1.y; n   long double k = A2.x - A1.x - (B2.x - B1.x), s = A2.y - A1.y - (B2.y - B1.y); n   if (A1 == A2 && B1 == B2) return dist(A1, B1); n   long double v = -(x0 * k + y0 * s) / (k * k + s * s); n   long double ans = min(dist(A1, B1), dist(A2, B2)); n   if (v > 0 && v < 1) n     ans = min(ans, (k * k + s * s) * v * v + 2 * v * (x0 * k + y0 * s) + n                        x0 * x0 + y0 * y0); n   return ans; n } n point A[N], B[N]; n void solve() { n   long long i, j, k, n, d1, d2; n   cin >> n >> d1 >> d2; n   d1 = d1 * d1; n   d2 = d2 * d2; n   for (i = 0; i < n; ++i) { n     cin >> A[i].x >> A[i].y >> B[i].x >> B[i].y; n   } n   long long ans = 0; n   bool canHello = 1; n   for (i = 0; i < n - 1; ++i) { n     if (dist(A[i], B[i]) >= d2 + eps) canHello = 1; n     if (canHello && mindist(A[i], A[i + 1], B[i], B[i + 1]) <= d1 + eps) { n       ++ans; n       canHello = 0; n     } n   } n   cout << ans; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s1, s2; n   int l; n   vector<int> v; n   cin >> s1 >> s2; n   for (int i = 0; i < s2.length(); i++) { n     if ((s2[i] -  0 ) > 0) v.push_back(s2[i] -  0 ); n   } n   sort(v.begin(), v.end()); n   int k = v.size() - 1; n   for (int i = 0; i < s1.length(); i++) { n     for (int j = k; j >= 0; j--) { n       if ((s1[i] -  0 ) < v[j]) { n         k--; n         s1[i] = char(v[j] +  0 ); n       } n       break; n     } n   } n   cout << s1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<long long int, int> mp; n map<long long int, int> mpp; n long long int a[100007]; n long long int b[100007]; n long long int bb[100007]; n int main() { n   int m, p; n   scanf( %d %d , &m, &p); n   int i, j; n   for (i = 0; i < m; i++) scanf( %lld , a + i); n   for (i = 0; i < p; i++) scanf( %lld , b + i); n   sort(b, b + p); n   long long int ans = 0; n   long long int anss = 0; n   long long int ansss; n   bb[0] = 0; n   mp[0] = 1; n   mpp[0] = 1; n   for (i = 1; i < m; i++) anss = anss + a[i], mp[anss]++, bb[i] = anss; n   for (i = 0; i < m; i++) { n     anss = bb[i]; n     if (mpp[b[0] - anss - a[0]]) continue; n     mpp[b[0] - anss - a[0]] = 1; n     ansss = 1; n     j = 1; n     while (j < p) { n       anss = anss + b[j] - b[j - 1]; n       if (mp[anss]) { n         if (!(b[j] - anss - a[0])) ansss = 0; n       } else { n         ansss = 0; n         break; n       } n       j++; n     } n     ans = ans + ansss; n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize(2) n using namespace std; n const int inf = 1e9; n const int NINF = 0xc0c0c0c0; n const int maxn = 1e5 + 7; n const int bas = maxn / 2; n const int Maxn = 1e7 + 7; n const double eps = 1e-6; n const int mod = (1 << 31) - 1; n template <typename T> n void read(T &x) { n   x = 0; n   char c = getchar(); n   T flag = 1; n   while (c <  0  || c >  9 ) { n     if (c ==  - ) flag = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     x = x * 10 + c -  0 ; n     c = getchar(); n   } n   x = x * flag; n } n struct edge { n   int v, next; n } e[maxn << 1]; n int head[maxn], cnt; n void add(int a, int b) { n   e[++cnt] = edge{b, head[a]}; n   head[a] = cnt; n } n int f[maxn][21], dep[maxn], tot, dp[maxn], treez = 0, root; n void dfs(int u, int fa) { n   dep[u] = dep[fa] + 1; n   f[u][0] = fa; n   for (int i = 1; i <= 20; i++) { n     f[u][i] = f[f[u][i - 1]][i - 1]; n   } n   for (int i = head[u]; i; i = e[i].next) { n     int v = e[i].v; n     if (v == fa) continue; n     dfs(v, u); n     treez = max(treez, dp[u] + dp[v] + 1); n     dp[u] = max(dp[u], dp[v] + 1); n   } n } n int getlca(int a, int b) { n   if (dep[a] < dep[b]) swap(a, b); n   for (int i = 20; ~i; i--) { n     int x = f[a][i]; n     if (dep[x] >= dep[b]) a = x; n   } n   if (a == b) return a; n   for (int i = 20; ~i; i--) { n     if (f[a][i] != f[b][i]) { n       a = f[a][i]; n       b = f[b][i]; n     } n   } n   return f[a][0]; n } n int k; n int work(int a, int lca) { n   for (int i = 20; ~i; i--) { n     if ((1 << i) <= k && dep[f[a][i]] >= dep[lca]) { n       a = f[a][i]; n       k -= (1 << i); n     } n   } n   return a; n } n void solve() { n   int n, a, b, da, db; n   cin >> n >> a >> b >> da >> db; n   for (int i = 1; i <= n; i++) head[i] = 0, dp[i] = 0, dep[i] = 0; n   cnt = 0; n   treez = 0; n   for (int i = 1; i < n; i++) { n     int u, v; n     cin >> u >> v; n     add(u, v); n     add(v, u); n   } n   if (da * 2 >= db) { n     puts( Alice ); n     return; n   } n   dfs(1, 0); n   int lca = getlca(a, b); n   if (treez <= da * 2) { n     puts( Alice ); n     return; n   } n   if (dep[a] + dep[b] - dep[lca] * 2 <= da) { n     puts( Alice ); n     return; n   } n   puts( Bob ); n } n signed main() { n   int T = 1; n   scanf( %d , &T); n   while (T--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n long long n, l, r; n inline long long query(long long l, long long r) { n   for (long long i = 62; i >= 1; i--) { n     if ((1ll << i) - 1 >= l && (1ll << i) - 1 <= r) return (1ll << i) - 1; n   } n   for (long long i = 62; i >= 0; i--) n     if (((1ll << i) & l) > 0 && ((1ll << i) & r) > 0) n       return (1ll << i) + query(l - (1ll << i), r - (1ll << i)); n } n int main() { n   scanf( %lld , &n); n   while (n--) { n     scanf( %lld%lld , &l, &r); n     if (l > r) { n       long long t = l; n       l = r; n       r = t; n     } n     if (l == r) { n       printf( %lld n , l); n       continue; n     } n     printf( %lld n , query(l, r)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool nextper(int num[], int n) { n   int i; n   for (i = n - 1; i > 0; i--) { n     if (num[i] > num[i - 1]) { n       for (int j = n - 1; j >= i; j--) { n         if (num[j] > num[i - 1]) { n           int t = num[j]; n           num[j] = num[i - 1]; n           num[i - 1] = t; n           for (int m = i, k = n - 1; m < k; m++, k--) { n             t = num[m]; n             num[m] = num[k]; n             num[k] = t; n           } n           break; n         } n       } n       break; n     } n   } n   if (i > 0) { n     return true; n   } else n     return false; n } n int order[8] = {0, 1, 2, 3, 4, 5, 6, 7}; n char s[8][9]; n int main() { n   int n, k, i, j, min, max, num, ans = 2147483647; n   scanf( %d %d , &n, &k); n   for (i = 0; i < n; i++) scanf( %s , s[i]); n   do { n     min = 2147483647; n     max = 0; n     for (i = 0; i < n; i++) { n       num = 0; n       for (j = 0; j < k; j++) num = num * 10 + (s[i][order[j]] -  0 ); n       if (min > num) min = num; n       if (max < num) max = num; n     } n     if (max - min < ans) ans = max - min; n   } while (nextper(order, k)); n   printf( %d n , ans); n } n


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


#include <bits/stdc++.h> n using namespace std; n long long bd(string n) { n   string num = n; n   long long dec_value = 0; n   long long base = 1; n   long long len = num.length(); n   for (long long i = len - 1; i >= 0; i--) { n     if (num[i] ==  1 ) dec_value += base; n     base = base * 2; n   } n   return dec_value; n } n long long db(long long n) { n   for (long long i = 31; i >= 0; i--) { n     long long k = n >> i; n     if (k & 1) n       cout <<  1 ; n     else n       cout <<  0 ; n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long l, r; n   cin >> l >> r; n   if (l < r) { n     long long x = 64 - (__builtin_clzll(l ^ r)); n     long long y = 1; n     long long ans = y << x; n     ans--; n     cout << ans; n   } else n     cout << 0; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double eps(1e-8); n long long a[100]; n long long f[100]; n long long dp[100]; n long long l, r; n long long calc(long long i) { n   long long ret = 0; n   for (long long j = (1); j <= (i - 1); ++j) { n     if (i % j == 0) ret += f[j]; n   } n   return ret; n } n long long solve(long long n) { n   long long tmp = n; n   long long top = 0; n   while (tmp) { n     a[++top] = tmp % 2; n     tmp /= 2; n   } n   if (top == 1) return 0; n   long long ret = 0; n   for (long long i = (1); i <= (top - 1); ++i) ret += calc(i); n   for (long long i = (1); i <= (top - 1); ++i) n     if (top % i == 0) { n       long long now = a[top]; n       dp[i] = 0; n       for (long long j = (2); j <= (i); ++j) { n         if (a[top - j + 1]) dp[i] += 1 << (i - j); n         now = now * 2 + a[top - j + 1]; n       } n       long long tmp = now; n       for (long long j = (2); j <= (top / i); ++j) tmp = (tmp << i) + now; n       if (tmp <= n) dp[i]++; n       for (long long j = (1); j <= (i - 1); ++j) n         if (i % j == 0) dp[i] -= dp[j]; n       ret += dp[i]; n     } n   return ret; n } n void cal(long long n) { n   f[1] = 1; n   for (long long i = (2); i <= (n); ++i) { n     f[i] = 1 << (i - 1); n     for (long long j = (1); j <= (i - 1); ++j) n       if (i % j == 0) f[i] -= f[j]; n   } n } n int main() { n   cal(70); n   scanf( %I64d %I64d , &l, &r); n   printf( %I64d n , solve(r) - solve(l - 1)); n } n


#include <bits/stdc++.h> n static const int N = 1 << 17; n struct Fenwick { n   int n; n   int t[N * 4]; n   void set(int n) { this->n = n; } n   void add(int x, int v) { n     for (int i = x; i <= n; i += (i & -i)) t[i] += v; n   } n   void clear(int x) { n     for (int i = x; i <= n; i += (i & -i)) t[i] = 0; n   } n   int query(int x) { n     int ans = 0; n     for (int i = x; i > 0; i -= (i & -i)) ans += t[i]; n     return ans; n   } n   int query(int l, int r) { return query(r) - query(l - 1); } n } fenwick; n int readInt() { n   int x = 0, f = 1; n   int c = getchar(); n   while (c <  0  || c >  9 ) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     x = (x << 1) + (x << 3) + c -  0 ; n     c = getchar(); n   } n   return x * f; n } n struct node { n   long long x, R, l, r, q; n   bool operator<(node const& o) const { return R < o.R; } n } s[N]; n bool cmp(const node& a, const node& b) { return a.q < b.q; } n long long ans; n int n, k, cc; n long long d[N * 4]; n void merge(int l, int r) { n   if (l == r) return; n   int m = (l + r) >> 1; n   merge(l, m); n   merge(m + 1, r); n   int st = m + 1, e = m; n   for (int i = l; i <= m; ++i) { n     while (e < r && s[e + 1].q <= s[i].q + k) { n       ++e; n       fenwick.add(s[e].x, +1); n     } n     while (st <= e && s[st].q < s[i].q - k) { n       fenwick.add(s[st].x, -1); n       ++st; n     } n     ans += fenwick.query(s[i].l, s[i].r); n   } n   for (int i = m + 1; i <= r; ++i) fenwick.clear(s[i].x); n   std::inplace_merge(s + l, s + m + 1, s + r + 1, cmp); n } n int solve() { n   n = readInt(); n   k = readInt(); n   cc = 0; n   for (int i = 1; i <= n; ++i) { n     s[i].x = readInt(); n     s[i].R = readInt(); n     s[i].q = readInt(); n     d[cc++] = s[i].x; n     d[cc++] = s[i].x - s[i].R; n     d[cc++] = s[i].x + s[i].R; n   } n   std::sort(d, d + cc); n   cc = std::unique(d, d + cc) - d; n   for (int i = 1; i <= n; ++i) { n     s[i].l = std::lower_bound(d, d + cc, s[i].x - s[i].R) - d; n     s[i].r = std::lower_bound(d, d + cc, s[i].x + s[i].R) - d; n     s[i].x = std::lower_bound(d, d + cc, s[i].x) - d; n     ++s[i].l; n     ++s[i].r; n     ++s[i].x; n   } n   std::sort(s + 1, s + n + 1); n   fenwick.set(cc); n   ans = 0; n   merge(1, n); n   printf( %lld n , ans); n   return 0; n } n int main(int argc, char* argv[]) { solve(); } n


#include <bits/stdc++.h> n using namespace std; n int N, P[3], a[3], b[3]; n map<char, char> h; n int p[] = {666013, 100003, 544513}; n int m[] = {123456, 555123, 100055}; n string s, t; n int main() { n   std::ios_base::sync_with_stdio(false); n   h[ N ] =  S ; n   h[ S ] =  N ; n   h[ E ] =  W ; n   h[ W ] =  E ; n   cin >> N >> s >> t; n   --N; n   for (int i = 0; i < N; ++i) { n     s[i] = h[s[i]]; n   } n   P[0] = 1; n   P[1] = 1; n   P[2] = 1; n   for (int i = N - 1; i >= 0; --i) { n     bool ok = 1; n     for (int j = 0; j < 3; ++j) { n       a[j] = (1LL * p[j] * a[j] + s[i]) % m[j]; n       b[j] = (1LL * P[j] * t[i] + b[j]) % m[j]; n       if (a[j] != b[j]) ok = 0; n       P[j] = (1LL * P[j] * p[j]) % m[j]; n     } n     if (ok) { n       cout <<  NO ; n       return 0; n     } n   } n   cout <<  YES ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, q; n long long a[755], mx[755]; n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cin >> n >> q; n   for (long long i = 1; i <= n; i++) cin >> a[i]; n   for (long long i = 0; i <= n; i++) { n     long long l = 0, r = 1000000000000000, ans = 0; n     while (l <= r) { n       long long m = (l + r + 1) / 2; n       long long sum = m, cnt = 0; n       priority_queue<long long, vector<long long>, greater<long long>> pq; n       for (long long j = 1; j <= n; j++) { n         sum += a[j]; n         pq.push(a[j]); n         while (sum < 0 && (long long)pq.size()) { n           sum -= pq.top(); n           pq.pop(); n           cnt++; n         } n       } n       if (cnt <= i) { n         ans = m; n         r = m - 1; n       } else n         l = m + 1; n     } n     mx[i] = ans; n   } n   while (q--) { n     long long r; n     cin >> r; n     for (long long i = 0; i <= n; i++) n       if (r >= mx[i]) { n         cout << i <<   n ; n         break; n       } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int root[1000005], weight[1000005]; n vector<int> vec[26]; n int findroot(int x) { n   while (x != root[x]) { n     root[x] = root[root[x]]; n     x = root[x]; n   } n   return x; n } n void merge(int x, int y) { n   x = findroot(x); n   y = findroot(y); n   if (x == y) return; n   if (weight[x] > weight[y]) n     weight[x] += weight[y], root[y] = x; n   else n     weight[y] += weight[x], root[x] = y; n } n int main() { n   int n, i, j, ans = 0; n   cin >> n; n   string s; n   for (i = 0; i < n; i++) { n     root[i] = i; n     weight[i] = 1; n   } n   for (i = 0; i < n; i++) { n     cin >> s; n     for (char c : s) vec[c -  a ].push_back(i); n   } n   for (i = 0; i < 26; i++) { n     for (j = 1; j < vec[i].size(); j++) merge(vec[i][j - 1], vec[i][j]); n   } n   for (i = 0; i < n; i++) n     if (root[i] == i) ans++; n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n bool done[333333]; n int main(int argc, char *argv[]) { n   int N; n   scanf( %d , &N); n   int pt = N; n   printf( 1 ); n   for (int i = 0; i < (N); i++) { n     int a; n     scanf( %d , &a); n     done[a] = true; n     while (done[pt]) pt--; n     printf(  %d , (i + 2) - (N - pt)); n   } n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int fast_expo(long long int x, long long int p) { n   if (p == 0) n     return 1; n   else if (p % 2 == 0) { n     long long int t = fast_expo(x, p / 2) % 998244353; n     return (t * t) % 998244353; n   } else n     return (x * (fast_expo(x, p - 1)) % 998244353) % 998244353; n } n int main() { n   string s; n   cin >> s; n   int n = s.size(); n   int flag = 0; n   for (int i = 0; i < n; i++) { n     int r = i + 25; n     if (r < n) { n       set<char> st; n       int q = 0; n       for (int j = i; j <= r; j++) { n         if (s[j] ==  ? ) n           q++; n         else n           st.insert(s[j]); n       } n       int sz = 26 - st.size(); n       if (q == sz) { n         set<char> st2; n         for (int j = 0; j < 26; j++) { n           char ch = j +  A ; n           if (st.find(ch) == st.end()) st2.insert(ch); n         } n         auto it = st2.begin(); n         for (int j = i; j <= r; j++) { n           if (s[j] ==  ? ) { n             char ch = *it; n             s[j] = ch; n             it++; n           } n         } n         flag = 1; n       } n     } n     if (flag) break; n   } n   char ch =  A ; n   for (int i = 0; i < n; i++) { n     if (s[i] ==  ? ) s[i] = ch; n   } n   if (flag) n     cout << s << endl; n   else n     cout << -1 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long int max(long long int a, long long int b) { n   if (a > b) n     return a; n   else n     return b; n } n void solve() { n   vector<pair<int, int> > points; n   vector<int> xval, yval; n   for (int i = 0; i < 3; i++) { n     int x, y; n     cin >> x >> y; n     points.push_back({x, y}); n     xval.push_back(x); n     yval.push_back(y); n   } n   sort(xval.begin(), xval.end()); n   sort(yval.begin(), yval.end()); n   pair<int, int> med = {xval[1], yval[1]}; n   set<pair<int, int> > ans; n   for (int i = 0; i < 3; i++) { n     int x = points[i].first; n     int y = points[i].second; n     ans.insert({x, y}); n     if (x >= med.first) { n       while (x != med.first) { n         ans.insert({--x, y}); n       } n     } else { n       while (x != med.first) { n         ans.insert({++x, y}); n       } n     } n     if (y >= med.second) { n       while (y != med.second) { n         ans.insert({x, --y}); n       } n     } else { n       while (y != med.second) { n         ans.insert({x, ++y}); n       } n     } n   } n   cout << ans.size() << endl; n   for (auto c : ans) { n     cout << c.first <<     << c.second << endl; n   } n } n int main() { solve(); } n


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


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n int n, root; n vector<int> adj[N]; n bool ok; n int dfs(int v, int p = -1) { n   int ret = 0; n   int cnt = 2; n   if (v == root) cnt++; n   for (int u : adj[v]) { n     if (u == p) continue; n     int res = dfs(u, v); n     if (res) ret = 2; n     if (res == 2) cnt--; n   } n   if (cnt <= 0) { n     ok = false; n   } n   if (adj[v].size() == 3) ret = max(1, ret); n   if (adj[v].size() > 3) ret = 2; n   return ret; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i < n; i++) { n     int a, b; n     scanf( %d%d , &a, &b); n     a--; n     b--; n     adj[a].push_back(b); n     adj[b].push_back(a); n   } n   ok = true; n   root = 0; n   for (int i = 0; i < n; i++) n     if (adj[root].size() < adj[i].size()) root = i; n   dfs(root); n   if (!ok && adj[root].size() == 3) { n     ok = true; n     for (int u : adj[root]) { n       if (adj[u].size() == 3) { n         root = u; n         break; n       } n     } n     dfs(root); n   } n   if (ok) n     puts( Yes ); n   else n     puts( No ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e6 + 10; n const int mod = 1e9 + 7; n const int P = 257; n int H[MAXN]; n char s[MAXN]; n char t[MAXN]; n int iP[MAXN]; n int add(int a, int b) { return (a + b) % mod; } n int mul(long long a, long long b) { return (a * b) % mod; } n int range(int l, int r) { n   if (l == 0) return H[r]; n   return mul(add(H[r], mod - H[l - 1]), iP[l]); n } n int n, m; n bool check(int x, int y) { n   int pos = 0; n   int hx = -1, hy = -1; n   for (int i = 0; i < n; ++i) { n     if (s[i] ==  1 ) { n       if (hx == -1) n         hx = range(pos, pos + x - 1); n       else if (hx != range(pos, pos + x - 1)) n         return 0; n       pos += x; n     } else { n       if (hy == -1) n         hy = range(pos, pos + y - 1); n       else if (hy != range(pos, pos + y - 1)) n         return 0; n       pos += y; n     } n   } n   if (x == y) return hx != hy; n   return 1; n } n int ex(int a, int b) { n   int r = 1; n   while (b > 0) { n     if (b & 1) r = mul(r, a); n     a = mul(a, a); n     b >>= 1; n   } n   return r; n } n int main() { n   scanf( %s , s); n   scanf( %s , t); n   n = strlen(s); n   m = strlen(t); n   int c1 = 0, c0 = 0; n   for (int i = 0; i < n; ++i) { n     if (s[i] ==  0 ) n       c0 += 1; n     else n       c1 += 1; n   } n   int h = 0; n   int ip = ex(P, mod - 2); n   int p = 1; n   for (int i = 0; i < m; ++i) { n     h = add(h, mul(t[i], p)); n     p = mul(p, P); n     H[i] = h; n   } n   iP[0] = 1; n   for (int i = 1; i < m; ++i) { n     iP[i] = mul(iP[i - 1], ip); n   } n   int ans = 0; n   for (int x = 1; x * c1 < m; ++x) { n     if ((m - c1 * x) % c0 == 0) { n       int y = (m - c1 * x) / c0; n       ans += check(x, y); n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a, ta, b, tb, x, y, sum, A, AA, B, BB; n char ch; n int main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   ; n   cin >> a >> ta >> b >> tb >> x >> ch >> y; n   A = 60 * x + y; n   AA = A + ta; n   for (int i = 5 * 60; i <= 1439; i += b) { n     B = i; n     BB = B + tb; n     int maxx = max(A, B); n     int mini = min(AA, BB); n     if (maxx < mini) sum++; n   } n   cout << sum << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, i; n vector<string> pos; n string s; n set<vector<string> > st; n set<vector<string> >::iterator iter; n bool srch(vector<string> pos) { n   if (pos.size() == 1) return true; n   iter = st.find(pos); n   if (iter != st.end()) return false; n   int x = pos.size() - 1; n   if (pos.size() >= 4) { n     if (pos[x][0] == pos[x - 3][0] || pos[x][1] == pos[x - 3][1]) { n       vector<string> nv = pos; n       nv[x - 3] = nv[x]; n       nv.pop_back(); n       if (srch(nv)) return true; n       st.insert(nv); n     } n   } n   if (pos[x][0] == pos[x - 1][0] || pos[x][1] == pos[x - 1][1]) { n     vector<string> nv = pos; n     nv[x - 1] = nv[x]; n     nv.pop_back(); n     if (srch(nv)) return true; n     st.insert(nv); n   } n   return false; n } n int main() { n   cin >> n; n   for (i = 0; i < n; i++) { n     cin >> s; n     pos.push_back(s); n   } n   if (srch(pos)) n     cout <<  YES  << endl; n   else n     cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct block { n   int x, y; n   inline bool operator<(const block &cpr) const { n     if (x != cpr.x) return x < cpr.x; n     return y < cpr.y; n   } n   inline bool operator==(const block &cpr) const { n     return ((x == cpr.x) && (y == cpr.y)); n   } n }; n struct nblock { n   int x, y, id; n   inline bool operator<(const nblock &cpr) const { n     if (y != cpr.y) return y < cpr.y; n     return x < cpr.x; n   } n }; n block bl[100005]; n nblock nbl[100005]; n int vis[100005]; n int n, m; n queue<int> pq; n int mov[8][2]{ n     1, 1, 1, 0, 1, -1, 0, -1, 0, 1, -1, 1, -1, 0, -1, -1, n }; n int main() { n   scanf( %d , &n); n   scanf( %d , &m); n   for (int i = 1; i <= m; i++) scanf( %d%d , &bl[i].x, &bl[i].y); n   sort(bl + 1, bl + 1 + m); n   for (int i = 1; i <= m; i++) { n     nbl[i].x = bl[i].x; n     nbl[i].y = bl[i].y; n     nbl[i].id = i; n   } n   sort(nbl + 1, nbl + 1 + m); n   for (int i = 1; i <= m; i++) n     if (bl[i].x == n || bl[i].y == 1) { n       vis[i] = 1; n       pq.push(i); n     } n   while (pq.size()) { n     block cur; n     int cid; n     cid = pq.front(); n     cur = bl[cid]; n     pq.pop(); n     if (cur.x > 1) { n       block nw; n       nw = cur; n       nw.x--; n       int top, bot, tr; n       top = m; n       bot = 1; n       while (top > bot) { n         tr = (top + bot + 1) / 2; n         if (bl[tr] < nw) n           bot = tr; n         else n           top = tr - 1; n       } n       if (bl[top].x == nw.x) n         if (bl[top].y < nw.y) n           if (vis[top] == 0) { n             vis[top] = 1; n             pq.push(top); n           } n     } n     if (cur.y < n) { n       nblock nw; n       nw.x = cur.x; n       nw.y = cur.y + 1; n       int top, bot, tr; n       top = m; n       bot = 1; n       while (top > bot) { n         tr = (top + bot) / 2; n         if (nw < nbl[tr]) n           top = tr; n         else n           bot = tr + 1; n       } n       if (nbl[top].y == nw.y) n         if (nbl[top].x > nw.x) n           if (vis[nbl[top].id] == 0) { n             vis[nbl[top].id] = 1; n             pq.push(nbl[top].id); n           } n     } n     for (int dr = 0; dr < 8; dr++) { n       block nw; n       nw = cur; n       nw.x += mov[dr][0]; n       nw.y += mov[dr][1]; n       int top, bot, tr; n       top = m; n       bot = 1; n       while (top > bot) { n         tr = (top + bot + 1) / 2; n         if (nw < bl[tr]) n           top = tr - 1; n         else n           bot = tr; n       } n       if (nw == bl[top]) n         if (vis[top] == 0) { n           vis[top] = 1; n           pq.push(top); n         } n     } n   } n   for (int i = 1; i <= m; i++) n     if (vis[i] == 1) n       if (bl[i].x == 1 || bl[i].y == n) { n         printf( -1 n ); n         return 0; n       } n   printf( %d n , 2 * (n - 1)); n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXn = 100; n int adj[MAXn + 5][MAXn + 5]; n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     int u, v, c; n     cin >> u >> v >> c; n     u--; n     v--; n     adj[u][v] = c; n     adj[v][u] = -1 * c; n   } n   bool dir = 0; n   int sum[2] = {0, 0}, u = -1, v = 0; n   for (int i = 0; i < n; i++) n     if (adj[v][i] > 0) dir = 1; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) n       if (adj[v][j] != 0 && j != u) { n         if (dir) { n           if (adj[v][j] > 0) n             sum[dir] += adj[v][j]; n           else { n             sum[!dir] += -1 * adj[v][j]; n             dir = !dir; n           } n         } else { n           if (adj[v][j] > 0) { n             sum[!dir] += adj[v][j]; n             dir = !dir; n           } else n             sum[dir] += -1 * adj[v][j]; n         } n         u = v; n         v = j; n         break; n       } n   } n   cout << min(sum[0], sum[1]) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n class trio { n  public: n   long long first, second, third; n }; n const long long MOD = 163577857; n long long gcdExtended(long long a, long long b, long long *x, long long *y) { n   if (a == 0) { n     *x = 0, *y = 1; n     return b; n   } n   long long x1, y1; n   long long gcd = gcdExtended(b % a, a, &x1, &y1); n   *x = y1 - (b / a) * x1; n   *y = x1; n   return gcd; n } n long long modInverse(long long a, long long m) { n   long long x, y; n   long long g = gcdExtended(a, m, &x, &y); n   long long res = (x % m + m) % m; n   return res; n } n long long power(long long x, long long y) { n   long long ans = 1, second = x; n   while (y) { n     if (y & 1) { n       ans *= second; n       ans %= MOD; n     } n     second *= second; n     second %= MOD; n     y >>= 1; n   } n   return ans; n } n bool comp(trio &a, trio &b) { return a.first < b.first; } n vector<long long> a; n long long m, n, k, third; n vector<trio> trap; n bool func(long long mid) { n   if (mid == m + 1) return 0; n   if (mid <= 0) return 1; n   long long val = a[mid - 1]; n   long long ans = 0; n   long long j = 1, i = 0, tr = 0; n   while (i <= n) { n     bool fl = 0; n     while (tr < k && trap[tr].first <= j) { n       if (trap[tr].third <= val) { n         tr++; n         continue; n       } n       j = max(trap[tr].second, j); n       fl = 1; n       tr++; n     } n     if (!fl) n       ans += 1; n     else { n       ans += (j - i) * 3; n     } n     i = j; n     j++; n   } n   return ans <= third; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   cin >> m >> n >> k >> third; n   a.resize(m); n   trap.resize(k); n   for (long long i = 0; i < m; ++i) { n     cin >> a[i]; n   } n   for (long long i = 0; i < k; ++i) { n     cin >> trap[i].first >> trap[i].second >> trap[i].third; n   } n   sort(a.begin(), a.end(), greater<long long>()); n   sort(trap.begin(), trap.end(), comp); n   long long l = 0, h = m, mid; n   while (l <= h) { n     mid = (l + h) / 2; n     bool t1 = func(mid); n     bool t2 = func(mid + 1); n     if (t1 && !t2) break; n     if (t1) n       l = mid + 1; n     else n       h = mid - 1; n   } n   cout << mid; n   return 0; n } n