#include <bits/stdc++.h> n using namespace std; n const int M = 1e9 + 7; n const int MOD = 1e9 + 7; n const double PI = 3.141592653589793238460; n long long power(long long a, long long b) { n   long long res = 1; n   if (a == 0) return 0; n   if (a == 1) return 1; n   for (; b > 0; b >>= 1) { n     if (b & 1) res = (res * a); n     if (res > MOD) res %= MOD; n     a = (a * a); n     if (a > MOD) a = a % MOD; n   } n   return res; n } n bool isPrime[1000000 + 1]; n vector<long long> prime; n void seive(int limit) { n   isPrime[0] = isPrime[1] = 1; n   for (int i = 2; i * i <= limit; i++) { n     if (!isPrime[i]) n       for (int j = i * i; j <= limit; j += i) { n         isPrime[j] = 1; n       } n   } n   for (int i = 2; i <= limit; i++) n     if (!isPrime[i]) prime.push_back(i); n } n int ls; n int maxi = 0; n int mark[300000]; n int visited[300000]; n void dfsl(int x, vector<int> adj[], int n, int p = -1, int mx = 0) { n   if (mx > maxi) { n     maxi = mx; n     ls = x; n   } n   for (int y : adj[x]) { n     if (y == p) continue; n     dfsl(y, adj, n, x, mx + 1); n   } n } n int parent[300000]; n pair<int, int> dfs(int x, vector<int> adj[], int d = 0, int p = -1) { n   pair<int, int> res = {d, x}; n   parent[x] = p; n   for (int y : adj[x]) { n     if (y == p) continue; n     res = max(res, dfs(y, adj, d + 1, x)); n   } n   return res; n } n int main() { n   ios_base::sync_with_stdio(false), cin.tie(0); n   int tt; n   tt = 1; n   while (tt--) { n     int n; n     cin >> n; n     vector<int> adj[n + 1]; n     for (int i = 0; i < n - 1; i++) { n       int u, v; n       cin >> u >> v; n       adj[v].push_back(u); n       adj[u].push_back(v); n     } n     pair<int, int> da = dfs(1, adj); n     pair<int, int> db = dfs(da.second, adj); n     int a = da.second; n     vector<int> v; n     int x = db.second; n     int b = x; n     while (x != a) { n       v.push_back(x); n       x = parent[x]; n     } n     v.push_back(a); n     if (v.size() == n) { n       cout << n - 1 <<   n ; n       cout << a <<     << v[1] <<     << b; n       return 0; n     } n     queue<int> q; n     for (int i = 0; i < v.size(); i++) q.push(v[i]); n     vector<int> d(n + 1, -1); n     for (int i = 0; i < v.size(); i++) d[v[i]] = 0; n     while (!q.empty()) { n       int x = q.front(); n       q.pop(); n       for (int y : adj[x]) { n         if (d[y] == -1) { n           d[y] = d[x] + 1; n           q.push(y); n         } n       } n     } n     pair<int, int> ans = {0, 0}; n     for (int i = 1; i <= n; i++) { n       ans = max(ans, {d[i], i}); n     } n     cout << (int)v.size() - 1 + ans.first <<   n ; n     cout << a <<     << ans.second <<     << b <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int x, y, a; n   cin >> x >> y; n   vector<int> v; n   vector<int> v2; n   for (int i = 0; i < x; i++) { n     cin >> a; n     v.push_back(a); n   } n   for (int i = 0; i < y; i++) { n     cin >> a; n     v2.push_back(a); n   } n   int key = 0, ct = 0, ii = 0, jj = 0, sum = 0; n   for (; ii < x; ii++) { n     key += v[ii]; n     for (; jj <= y;) { n       if (sum > key) { n         break; n       } n       if (sum == key) { n         ct++; n         key = 0; n         sum = 0; n         break; n       } n       sum += v2[jj++]; n       if (sum == key) { n         ct++; n         key = 0; n         sum = 0; n         break; n       } n       if (sum > key) { n         break; n       } n     } n   } n   cout << ct << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, k, t = 0, a, e = 0; n int main() { n   cin >> n >> k; n   for (long long i = 1; i <= n; i++) { n     cin >> a; n     if (i > 1 && t - (n - i) * a * (i - e - 1) < k) { n       cout << i <<   n ; n       e++; n     } else n       t += a * (i - 1 - e); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxx = 1e6 + 100; n vector<pair<int, int> > in[maxx], out[maxx]; n struct node { n   int l; n   int r; n   long long sum; n   long long num; n } p[maxx << 2]; n int n, m, k; n void init() { n   for (int i = 0; i <= n; i++) { n     in[i].clear(); n     out[i].clear(); n   } n } n inline void pushup(int cur) { n   p[cur].num = p[cur << 1].num + p[cur << 1 | 1].num; n   p[cur].sum = p[cur << 1].sum + p[cur << 1 | 1].sum; n } n inline void build(int l, int r, int cur) { n   p[cur].l = l; n   p[cur].r = r; n   p[cur].num = 0; n   p[cur].sum = 0; n   if (l == r) return; n   int mid = l + r >> 1; n   build(l, mid, cur << 1); n   build(mid + 1, r, cur << 1 | 1); n } n inline void update(int num, int pos, int cur) { n   int L = p[cur].l; n   int R = p[cur].r; n   if (L == R) { n     p[cur].num += num; n     p[cur].sum += (long long)num * (long long)L; n     return; n   } n   int mid = L + R >> 1; n   if (pos <= mid) n     update(num, pos, cur << 1); n   else n     update(num, pos, cur << 1 | 1); n   pushup(cur); n } n inline long long query(int num, int cur) { n   int L = p[cur].l; n   int R = p[cur].r; n   if (L == R) return (long long)L * min(p[cur].num, (long long)num); n   if (num <= p[cur << 1].num) n     return query(num, cur << 1); n   else n     return (p[cur << 1].sum + query(num - p[cur << 1].num, cur << 1 | 1)); n } n int main() { n   int l, r, x, y; n   while (~scanf( %d%d%d , &n, &k, &m)) { n     init(); n     for (int i = 1; i <= m; i++) { n       scanf( %d%d%d%d , &l, &r, &x, &y); n       in[l].push_back(make_pair(x, y)); n       out[r].push_back(make_pair(x, y)); n     } n     long long ans = 0; n     build(1, maxx, 1); n     for (int i = 1; i <= n; i++) { n       for (int j = 0; j < in[i].size(); j++) n         update(in[i][j].first, in[i][j].second, 1); n       ans += query(k, 1); n       for (int j = 0; j < out[i].size(); j++) n         update(-out[i][j].first, out[i][j].second, 1); n     } n     printf( %lld n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   scanf( %d , &t); n   while (t--) { n     int n; n     scanf( %d , &n); n     if (n == 1 || n == 2) n       printf( 1 n ); n     else if (n == 3) n       printf( 2 n ); n     else { n       if (n % 2 == 1) n         printf( %d n , (n - 1) / 2 + 1); n       else n         printf( %d n , n / 2); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n #pragma GCC target( sse4 ) n using namespace std; n template <typename T> n void etch(T V) { n   for (auto x : V) cout << x <<    ; n   cout <<   n ; n } n vector<string> vec_splitter(string s) { n   s +=  , ; n   vector<string> res; n   while (!s.empty()) { n     res.push_back(s.substr(0, s.find( , ))); n     s = s.substr(s.find( , ) + 1); n   } n   return res; n } n void debug_out(vector<string> __attribute__((unused)) args, n                __attribute__((unused)) long long int idx, n                __attribute__((unused)) long long int LINE_NUM) { n   cerr << endl; n } n template <typename Head, typename... Tail> n void debug_out(vector<string> args, long long int idx, long long int LINE_NUM, n                Head H, Tail... T) { n   if (idx > 0) n     cerr <<  ,  ; n   else n     cerr <<  Line(  << LINE_NUM <<  )  ; n   stringstream ss; n   ss << H; n   cerr << args[idx] <<   =   << ss.str(); n   debug_out(args, idx + 1, LINE_NUM, T...); n } n const long long int MOD = 1e9 + 7; n const long long int MOD1 = 998244353; n const long long int N = 2e5 + 5; n const long long int INF = 1000111000111000111LL; n const long double EPS = 1e-12; n const long double PI = 3.141592653589793116; n long long int d[3005][3005], p[3005][3005]; n void solve() { n   long long int n; n   cin >> n; n   long long int a[n + 5]; n   for (long long int i = 1; i <= n; i++) cin >> a[i]; n   for (long long int i = 0; i <= n + 2; i++) { n     for (long long int j = 0; j <= n + 2; j++) { n       d[i][j] = p[i][j] = 0; n     } n   } n   for (long long int i = 1; i <= n; i++) { n     d[i][a[i]]++; n     for (long long int j = 0; j <= n; j++) { n       d[i][j] += d[i - 1][j]; n     } n   } n   for (long long int i = n; i >= 1; i--) { n     p[i][a[i]]++; n     for (long long int j = 0; j <= n; j++) { n       p[i][j] += p[i + 1][j]; n     } n   } n   long long int ans = 0; n   for (long long int i = 1; i <= n; i++) { n     for (long long int j = i + 1; j <= n; j++) { n       ans += (d[i - 1][a[j]] * p[j + 1][a[i]]); n     } n   } n   cout << ans <<   n ; n } n int32_t main() { n   std::ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int T; n   cin >> T; n   while (T--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MaxN = 4010; n const int MOD = 1000000009; n int n, w, b; n long long p[MaxN], r[MaxN]; n long long power(long long x, long long y) { n   if (!y) return 1; n   long long t = power(x, y / 2); n   t = t * t % MOD; n   if (y & 1) t = t * x % MOD; n   return t; n } n long long C(long long n, long long m) { n   return p[n] * r[m] % MOD * r[n - m] % MOD; n } n int main() { n   cin >> n >> w >> b; n   for (int i = p[0] = r[0] = 1; i < MaxN; ++i) { n     p[i] = p[i - 1] * i % MOD; n     r[i] = power(p[i], MOD - 2); n   } n   long long ans = 0; n   for (int i = max(1, n - w); i <= min(n - 2, b); ++i) n     ans += p[w] * p[b] % MOD * (n - i - 1) % MOD * C(b - 1, i - 1) % MOD * n            C(w - 1, n - i - 1) % MOD; n   cout << ans % MOD << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   vector<long long int> v(1000005); n   long long int i, n, j; n   for (i = 2; i < 1000005; i++) { n     j = i * 2; n     while (j <= 1000005) { n       v[j] = 1; n       j += i; n     } n   } n   cin >> n; n   j = 1; n   for (j = 1; j < 1001; j++) { n     if (v[n * j + 1] != 0) { n       cout << j; n       break; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n const int maxn = 1e5 + 5; n int n, a[maxn]; n int b[maxn]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cin >> n; n   int sum = 0; n   map<int, int> mp; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     mp[a[i]]++; n   } n   bool poss = false; n   for (int i = 0; i < n; i++) { n     if (a[i] > 0) { n       if (--mp[a[i]] == 0) mp.erase(a[i]); n       ++mp[a[i] - 1]; n       if (mp.size() == n) { n         poss = true; n       } n       if (--mp[a[i] - 1] == 0) mp.erase(a[i] - 1); n       ++mp[a[i]]; n     } n   } n   if (!poss) { n     cout <<  cslnb  <<   n ; n     return 0; n   } n   sort(a, a + n); n   b[0] = 0; n   for (int i = 1; i < n; i++) { n     b[i] = min(a[i], b[i - 1] + 1); n   } n   for (int i = 0; i < n; i++) { n     sum += (a[i] - b[i] + 2) % 2; n     sum %= 2; n   } n   cout << (sum == 1 ?  sjfnb  :  cslnb ) <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> pf(const string& s) { n   int n = s.size(); n   vector<int> pi(n); n   for (int i = 1; i < n; i++) { n     int j = pi[i - 1]; n     while (j > 0 && s[i] != s[j]) j = pi[j - 1]; n     if (s[i] == s[j]) j++; n     pi[i] = j; n   } n   return pi; n } n int main() { n   ios_base::sync_with_stdio(false); n   string s; n   string t; n   cin >> s >> t; n   auto a = pf(t); n   int val = t.length() - a.back(); n   int cyc = val; n   int c1 = 0, c0 = 0; n   for (int i = 0; i < s.length(); i++) { n     if (s[i] ==  1 ) n       c1++; n     else n       c0++; n   } n   int p = 0; n   while (true) { n     if (t[p] ==  0 ) { n       if (c0) { n         cout << 0; n         c0--; n       } else n         break; n     } else { n       if (c1) { n         cout << 1; n         c1--; n       } else n         break; n     } n     p++; n     p %= cyc; n   } n   while (c1) { n     cout << 1; n     c1--; n   } n   while (c0) { n     cout << 0; n     c0--; n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n; n   while (scanf( %d , &n) != EOF) { n     int sum = 0, i, num, he = 0, div = n + 1; n     for (i = 1; i <= n; i++) { n       scanf( %d , &num); n       sum += num; n     } n     for (i = 1; i <= 5; i++) { n       if ((sum + i) % div == 1) he++; n     } n     printf( %d n , 5 - he); n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <class A, class B> n inline bool mina(A &first, B second) { n   return (first > second) ? (first = second, 1) : 0; n } n template <class A, class B> n inline bool maxa(A &first, B second) { n   return (first < second) ? (first = second, 1) : 0; n } n map<char, int> cnt; n char c[] = { B ,  G ,  R }; n int main() { n   int N; n   cin >> N; n   string s; n   cin >> s; n   for (int(i) = 0; (i) < (N); ++(i)) { n     cnt[s[i]]++; n   } n   if (cnt.size() == 3) { n     cout <<  BGR  << endl; n     return 0; n   } else if (cnt.size() == 1) { n     cout << cnt.begin()->first << endl; n   } else { n     for (int(i) = 0; (i) < (3); ++(i)) { n       int cc = cnt[c[i]]; n       if (cc == 0) { n         printf( %c , c[i]); n       } else { n         bool good = false; n         for (int(j) = 0; (j) < (3); ++(j)) { n           if (j != i && cnt[c[j]] >= 2) { n             good = true; n           } n         } n         if (good) printf( %c , c[i]); n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long double PI = acos(-1); n long long powm(long long a, long long b) { n   long long res = 1; n   while (b) { n     if (b & 1) res = (res * a) % 1000000007; n     a = (a * a) % 1000000007; n     b >>= 1; n   } n   return res; n } n long long gcd(long long a, long long b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n const long long maxn = 2 * 1e5 + 10; n map<long long, long long> dif; n void solve() { n   long long n; n   cin >> n; n   long long a[n + 2]; n   for (long long i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   sort(a + 1, a + n + 1); n   for (long long i = 2; i <= n; i++) { n     long long cur = a[i] - a[i - 1]; n     dif[cur]++; n   } n   if (dif.size() > 2) { n     cout << 0 << endl; n   } else if (n == 1) { n     cout << -1 << endl; n   } else if (n == 2) { n     if ((a[2] - a[1]) % 2 == 0 && a[2] != a[1]) { n       cout << 3 << endl; n       cout << a[1] - (a[2] - a[1]) <<     << a[1] + (a[2] - a[1]) / 2 <<     n            << a[2] + (a[2] - a[1]) << endl; n     } else if (a[1] == a[2]) { n       cout << 1 << endl; n       cout << a[1] << endl; n     } else { n       cout << 2 << endl; n       cout << a[1] - (a[2] - a[1]) <<     << a[2] + (a[2] - a[1]) << endl; n     } n   } else if (dif.size() == 2 && n > 1) { n     long long mx = 0, d = 1000000000, z = 0; n     vector<long long> s; n     for (auto i : dif) { n       if (i.first == 0) z = 1; n       if (mx <= i.second) { n         mx = i.second; n         d = min(d, i.first); n       } n       s.push_back(i.first); n     } n     if (z || dif[s[0]] < n - 2 || s[1] % 2 != 0 || n         (s[1] % 2 == 0 && s[1] / 2 != s[0])) n       cout << 0 << endl; n     else { n       for (long long i = 2; i <= n; i++) { n         if (a[i] - a[i - 1] != d) { n           if ((a[i] - a[i - 1]) > d && (a[i] - a[i - 1]) % 2 == 0 && n               (a[i] - a[i - 1]) / 2 == d) { n             cout << 1 << endl; n             cout << a[i - 1] + d << endl; n           } else { n             cout << 0 << endl; n             break; n           } n         } n       } n     } n   } else if (dif.size() == 1) { n     long long d = 0; n     for (auto i : dif) { n       d = i.first; n     } n     if (d) { n       cout << 2 << endl; n       cout << a[1] - (a[2] - a[1]) <<     << a[n] + (a[2] - a[1]) << endl; n     } else { n       cout << 1 << endl; n       cout << a[1] << endl; n     } n   } else n     cout << 0 << endl; n } n signed main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   long long t = 1; n   for (long long i = 1; i <= t; i++) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, ans = 0; n   cin >> n; n   long long a[n + 1]; n   vector<long long> last; n   last.push_back(-1); n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     if (i > 0 && a[i] <= a[i - 1]) last.push_back(i - 1); n   } n   last.push_back(n - 1); n   for (int i = 1; i < (int)last.size(); i++) { n     if (last[i - 1] == -1) n       ans = max(last[i] + 1, ans); n     else n       ans = max(ans, last[i] - last[i - 1]); n     if (i < (int)last.size() - 1 && n         (a[last[i] + 1] > a[last[i] - 1] || a[last[i] + 2] > a[last[i]])) { n       if (i > 1) n         ans = max(last[i + 1] - last[i - 1] - 1, ans); n       else n         ans = max(ans, last[i + 1]); n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1000000007; n long long powmod(long long a, long long b) { n   long long res = 1; n   a %= mod; n   for (; b; b >>= 1) { n     if (b & 1) res = res * a % mod; n     a = a * a % mod; n   } n   return res; n } n int n, m, q; n int visited[205][205]; n int table[205][205]; n int g1, g2; n vector<pair<int, int> > ab; n set<int> cans; n string s; n pair<int, int> nxt[205][205]; n vector<pair<int, int> > prv[205][205]; n int cycleNum[205][205]; n vector<pair<int, int> > cycle[40005]; n bool has[40005][10]; n int idxCycle; n bool addTrigger; n void addCycle(int i, int j) { n   if (visited[i][j] == 2) return; n   if (visited[i][j] == 1) { n     addTrigger = true; n     cycle[idxCycle].push_back(make_pair(i, j)); n     has[idxCycle][table[i][j]] = 1; n     cycleNum[i][j] = idxCycle; n     visited[i][j] = 2; n     return; n   } n   visited[i][j] = 1; n   addCycle(nxt[i][j].first, nxt[i][j].second); n   if (visited[i][j] == 1 && addTrigger) { n     cycle[idxCycle].push_back(make_pair(i, j)); n     has[idxCycle][table[i][j]] = 1; n     cycleNum[i][j] = idxCycle; n   } else n     addTrigger = false; n   visited[i][j] = 2; n } n bool query(string& s, int pos, int x, int y) { n   if (pos >= 0 && table[x][y] == s[pos] -  0 ) pos--; n   if (pos == -1) return true; n   for (int i = 0; i < (int)prv[x][y].size(); i++) { n     if (query(s, pos, prv[x][y][i].first, prv[x][y][i].second)) return true; n   } n   return false; n } n void query(string& s, vector<int> idxs) { n   int n = s.length(); n   memset(visited, 0, sizeof(visited)); n   for (int i = 0; i < (int)idxCycle; i++) { n     int pos = -1; n     for (int j = 0; j < (int)10; j++) { n       if (!has[i][j] && idxs[j] != n) { n         pos = max(pos, idxs[j]); n       } n     } n     if (pos == -1) { n       cout <<  YES  << endl; n       return; n     } n     for (int j = 0; j < (int)cycle[i].size(); j++) { n       if (query(s, pos, cycle[i][j].first, cycle[i][j].second)) { n         cout <<  YES  << endl; n         return; n       } n     } n   } n   cout <<  NO  << endl; n } n int main() { n   memset(has, 0, sizeof(has)); n   memset(cycleNum, -1, sizeof(cycleNum)); n   memset(visited, 0, sizeof(visited)); n   ab.resize(10); n   cin >> n >> m >> q; n   for (int i = 0; i < (int)n; i++) { n     cin >> s; n     for (int j = 0; j < (int)m; j++) { n       table[i][j] = s[j] -  0 ; n     } n   } n   for (int i = 0; i < (int)10; i++) cin >> ab[i].first >> ab[i].second; n   for (int i = 0; i < (int)n; i++) { n     for (int j = 0; j < (int)m; j++) { n       int ni = i + ab[table[i][j]].first, nj = j + ab[table[i][j]].second; n       if (ni >= 0 && ni < n && nj >= 0 && nj < m) { n         nxt[i][j] = make_pair(ni, nj); n       } else { n         nxt[i][j] = make_pair(i, j); n       } n     } n   } n   idxCycle = 0; n   for (int i = 0; i < (int)n; i++) { n     for (int j = 0; j < (int)m; j++) { n       if (!visited[i][j]) { n         addCycle(i, j); n         if (cycle[idxCycle].size()) idxCycle++; n       } n     } n   } n   for (int i = 0; i < (int)n; i++) { n     for (int j = 0; j < (int)m; j++) { n       if (cycleNum[i][j] == -1) { n         prv[nxt[i][j].first][nxt[i][j].second].push_back(make_pair(i, j)); n       } n     } n   } n   for (int i = 0; i < (int)q; i++) { n     cin >> s; n     string t =   ; n     vector<int> idxs(10, s.length()); n     for (int j = s.length() - 1; j >= 0; j--) idxs[(s[j] -  0 )] = j, t += s[j]; n     query(s, idxs); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 3e5 + 3e4; n long long lp[N], rp[N], l, r, first, second, m, so, k, n; n char ch; n int main() { n   cin >> n; n   for (long long i = (long long)1; i <= (long long)n; i++) { n     cin >> ch >> k; n     if (ch ==  ? ) { n       first = l - lp[k]; n       second = r - rp[k]; n       m = l + r; n       so = 1e15; n       if (rp[k]) { n         so = min(so, r - rp[k]); n         so = min(so, m - second - 1); n       } n       if (lp[k]) { n         so = min(so, l - lp[k]); n         so = min(so, m - first - 1); n       } n       cout << so <<   n ; n     } else { n       if (ch ==  L ) lp[k] = ++l; n       if (ch ==  R ) rp[k] = ++r; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long n, s; n void dfs(long long x, long long s4, long long s7) { n   if (n <= x && (x < s || s == 0) && s4 == s7) s = x; n   if (x <= n * 100) { n     dfs(x * 10 + 4, s4 + 1, s7); n     dfs(x * 10 + 7, s4, s7 + 1); n   } n } n int main() { n   cin >> n; n   dfs(0, 0, 0); n   if (n <= 47) s = 47; n   cout << s; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long t; n   cin >> t; n   long long n, d; n   while (t--) { n     cin >> n >> d; n     long long arr[n]; n     for (long long i = 0; i < n; i++) cin >> arr[i]; n     int count = 0; n     int i = 1; n     while (i < n && d > 0 && d >= i) { n       if ((arr[i] * i) <= d) { n         count += arr[i]; n         d = d - (arr[i] * i); n       } else { n         int t = d / i; n         count += t; n         d = d % i; n       } n       i++; n     } n     cout << arr[0] + count << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n,q,a[100005],np[100005],cnt[100005],nx[19][100005],p1,p2; n vector<int> p,fac[100005]; n bool check(int ind) { n     for (int i=0; i<fac[ind].size(); i++) { n         if (cnt[fac[ind][i]]) return 0; n     } n     for (int i=0; i<fac[ind].size(); i++) cnt[fac[ind][i]]++; n     return 1; n } n void rem(int ind) { n     for (int i=0; i<fac[ind].size(); i++) { n         cnt[fac[ind][i]]--; n     } n } n void fun() { n     int l=0,r=0; n     while (l<n) { n         while (r<n and check(r)) r++; n         nx[0][l]=r; n         rem(l); l++; n     } n     nx[0][n]=n; n     for (int i=1; i<19; i++) { n         for (int j=0; j<=n; j++) { n             nx[i][j]=nx[i-1][nx[i-1][j]]; n         } n     } n } n int getans(int l,int r) { n     int ind=18,ret=0,cur=l; n     while (ind>=0) { n         if (nx[ind][cur]<=r) { n             ret+=(1<<ind); cur=nx[ind][cur]; n         } n         ind--; n     } n     return ret+1; n } n int main() { n     for (int i=2; i<=1e5; i++) { n         if (np[i]) continue; n         p.push_back(i); n         for (int j=2*i; j<=1e5; j+=i) { n             np[j]=1; n         } n     } n     cin>>n>>q; n     for (int i=0; i<n; i++) cin>>a[i]; n     for (int i=0; i<n; i++) { n         int ind=0,x=a[i]; n         while (p[ind]<318) { n             bool s=0; n             while (x%p[ind]==0) { n                 x/=p[ind]; s=1; n             } n             if (s) fac[i].push_back(p[ind]); n             ind++; n         } n         if (x!=1) { n             fac[i].push_back(x); n         } n     } n     fun(); n     for (int i=0; i<q; i++) { n         cin>>p1>>p2; p1--; p2--; n         cout<<getans(p1,p2)<<  n ; n     } n }


#include <bits/stdc++.h> n using namespace std; n const int MAX_N = int(1e5 + 3); n template <typename T> n void printV(vector<T> v, char sep) { n   for (long long unsigned int i = 0; i < v.size() - 1; i++) { n     cout << v[i] << sep; n   } n   cout << v[v.size() - 1]; n } n void printVPCI(vector<pair<char, int>> s) { n   for (long long unsigned int i = 0; i < s.size(); i++) { n     cout << s[i].first <<     << s[i].second <<   n ; n   } n } n void cdPr(bool a) { cout << (a ?  YES  :  NO ); } n bool isPrime(int n) { n   for (int i = 2; i <= n / 2; ++i) { n     if (n % i == 0) { n       return false; n     } n   } n   return true; n } n template <typename T> n bool vFind(vector<T> v, T x) { n   return find(v.begin(), v.end(), x) != v.end(); n } n vector<pair<char, int>> strInfo(string s) { n   vector<char> f; n   vector<pair<char, int>> r; n   for (long long unsigned int i = 0; i < s.size(); i++) { n     if (!vFind(f, s[i])) { n       f.push_back(s[i]); n       r.push_back(make_pair(s[i], count(s.begin(), s.end(), s[i]))); n     } n   } n   return r; n } n vector<char> distinctChars(string s) { n   vector<char> f; n   for (long long unsigned int i = 0; i < s.size(); i++) { n     if (!vFind(f, s[i])) f.push_back(s[i]); n   } n   return f; n } n void solve() { n   int n; n   cin >> n; n   int pos[MAX_N] = {0}; n   int t = n; n   int a; n   cin >> a; n   int i = 1; n   n--; n   int min = 99999999; n   int r = 0; n   pos[0] = a; n   int x; n   while (n > 0) { n     cin >> x; n     pos[i] = x; n     if (abs(a - x) < min) { n       min = abs(a - x); n       r = i; n     } n     a = x; n     i++; n     n--; n   } n   if (abs(x - pos[0]) < min) { n     r = t; n   } n   cout << (r) <<     << ((r + 1 > t) ? (r + 1 - t) : (r + 1)) <<   n ; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:64000000 ) n using namespace std; n const int infi = 1e9 + 7; n const long long infl = 1e18 + 7; n int ar[101]; n bitset<101> dp0[10500]; n bitset<101> dp[10500]; n bitset<101> ndp[10500]; n int main() { n   cin.sync_with_stdio(false); n   cin.tie(0); n   int n; n   cin >> n; n   for (int i = 0; i < (n); ++i) cin >> ar[i]; n   vector<pair<int, int> > tmp; n   sort(ar, ar + n); n   int lst = ar[0]; n   int sum = ar[0]; n   int cnt = 1; n   for (int i = 1; i < n; ++i) { n     sum += ar[i]; n     if (lst == ar[i]) n       ++cnt; n     else { n       tmp.emplace_back(lst, cnt); n       lst = ar[i]; n       cnt = 1; n     } n   } n   tmp.emplace_back(lst, cnt); n   if (tmp.size() <= 2) { n     cout << n; n     return 0; n   } n   for (auto cur : tmp) { n     for (int cnt = 1; cnt <= cur.second; ++cnt) { n       int second = cnt * cur.first; n       for (int w = 0; w < (10500); ++w) { n         ndp[w + second] |= dp0[w] << cnt; n         ndp[w + second] |= dp[w] << cnt; n       } n     } n     for (int cnt = 1; cnt <= cur.second; ++cnt) { n       dp0[cnt * cur.first][cnt] = 1; n     } n     for (int i = 0; i < (10500); ++i) dp[i] |= ndp[i]; n   } n   int ans = 0; n   for (auto cur : tmp) { n     for (int cnt = 1; cnt <= cur.second; ++cnt) { n       int second = cnt * cur.first; n       if (dp[second][cnt]) continue; n       ans = max(ans, cnt); n       if (tmp.size() == 2 && cnt == cur.second) ans = n; n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize(3,  Ofast ,  inline ) n #pragma GCC target( avx,avx2 ) n using namespace std; n template <class t> n inline t read(t &x) { n   char c = getchar(); n   bool f = 0; n   x = 0; n   while (!isdigit(c)) f |= c ==  - , c = getchar(); n   while (isdigit(c)) x = (x << 1) + (x << 3) + (c ^ 48), c = getchar(); n   if (f) x = -x; n   return x; n } n template <class t> n inline void write(t x) { n   if (x < 0) n     putchar( - ), write(-x); n   else { n     if (x > 9) write(x / 10); n     putchar( 0  + x % 10); n   } n } n const long long N = 1005, mod = 1e9 + 7; n long long f[N][N][2][2], g[N], ans[N], n, m, fac[N], inv[N]; n long long C(long long n, long long m) { n   return fac[n] * inv[m] % mod * inv[n - m] % mod; n } n signed main() { n   read(n); n   read(m); n   fac[0] = inv[0] = inv[1] = 1; n   for (long long i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % mod; n   for (long long i = 2; i <= n; i++) n     inv[i] = inv[mod % i] * (mod - mod / i) % mod; n   for (long long i = 1; i <= n; i++) inv[i] = inv[i] * inv[i - 1] % mod; n   f[1][0][0][0] = f[1][1][0][1] = 1; n   for (long long i = 2; i <= n; i++) n     for (long long j = 0; j < i; j++) { n       f[i][j][0][0] = n           (f[i][j][0][0] + f[i - 1][j][0][0] + f[i - 1][j][1][0]) % mod; n       f[i][j][1][0] = n           (f[i][j][1][0] + f[i - 1][j][0][1] + f[i - 1][j][1][1]) % mod; n       f[i][j + 1][0][0] = f[i - 1][j][0][0]; n       f[i][j + 1][0][1] = (f[i - 1][j][0][0] + f[i - 1][j][1][0]) % mod; n       f[i][j + 1][1][0] = f[i - 1][j][0][1]; n       f[i][j + 1][1][1] = (f[i - 1][j][0][1] + f[i - 1][j][1][1]) % mod; n     } n   for (long long i = 0; i <= n; i++) n     g[i] = (f[n][i][0][0] + f[n][i][1][0]) * fac[n - i] % mod; n   for (long long i = 0; i <= n; i++) n     for (long long j = i, op = 1; j <= n; j++, op *= -1) n       ans[i] = (ans[i] + mod + op * C(j, i) * g[j] % mod) % mod; n   write(ans[m]); n } n


#include <bits/stdc++.h> n using namespace std; n long long int mod = 1e9 + 9; n const double error = 1e-7; n const double PI = acos(-1); n mt19937 rng( n     (unsigned int)chrono::system_clock::now().time_since_epoch().count()); n inline long long int MOD(long long int x, long long int m = mod) { n   long long int y = x % m; n   return (y >= 0) ? y : y + m; n } n const int inf = 1e9 + 2; n const long long int infl = 1e18 + 1; n const int nmax = 1e6 + 10; n int master[nmax]; n void init(int n) { n   for (int i = 1; i <= n; i++) master[i] = i; n } n int fnd(int u) { n   if (master[u] == u) return u; n   return master[u] = fnd(master[u]); n } n int unite(int a, int b) { n   a = fnd(a), b = fnd(b); n   if (rng() & 1) swap(a, b); n   master[b] = a; n   return a; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   ; n   int n, m; n   cin >> n >> m; n   init(n); n   long long int two = 1; n   for (int i = 0; i < m; i++) { n     int a, b; n     cin >> a >> b; n     if (fnd(a) == fnd(b)) { n       two *= 2; n       two %= mod; n     } else n       unite(a, b); n     cout << MOD(two - 1) <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 3e5 + 10; n int n, m, cnt, k; n struct node { n   int l, r, sum = 1999999999; n } g[maxn * 20]; n inline int read() { n   register int x = 0, f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch <=  9  && ch >=  0 ) x = x * 10 + ch -  0 , ch = getchar(); n   return x * f; n } n struct value { n   int x, y, id; n } x[maxn]; n bool cmp(value v1, value v2) { return v1.x > v2.x; } n int root[maxn]; n void update(int num, int wht, int &rt, int l, int r) { n   g[++cnt] = g[rt]; n   if (l == r) { n     g[cnt].sum = min(g[rt].sum, wht); n     rt = cnt; n     return; n   } n   rt = cnt; n   int mid = l + r >> 1; n   if (num <= mid) n     update(num, wht, g[rt].l, l, mid); n   else n     update(num, wht, g[rt].r, mid + 1, r); n   g[rt].sum = max(g[g[rt].l].sum, g[g[rt].r].sum); n } n int query(int rt, int l, int r, int lb, int rb) { n   if (lb >= l && rb <= r) return g[rt].sum; n   if (lb > r || l > rb) return -1999999999; n   int mid = lb + rb >> 1; n   return max(query(g[rt].l, l, r, lb, mid), query(g[rt].r, l, r, mid + 1, rb)); n } n int main() { n   cnt = 1; n   scanf( %d%d%d , &n, &m, &k); n   for (int i = 1; i <= k; i++) { n     x[i].x = read(), x[i].y = read(), x[i].id = read(); n   } n   sort(x + 1, x + 1 + k, cmp); n   for (int i = 1; i <= k; i++) { n     root[i] = root[i - 1]; n     update(x[i].id, x[i].y, root[i], 1, n); n   } n   while (m--) { n     int A = read(), B = read(), X = read(), Y = read(); n     int lb = 1, asn = -1, rb = k; n     while (lb <= rb) { n       int mid = lb + rb >> 1; n       if (x[mid].x >= X) { n         lb = mid + 1; n         asn = mid; n       } else { n         rb = mid - 1; n       } n     } n     int ans = query(root[asn], A, B, 1, n); n     if (ans <= Y) n       printf( yes n ); n     else n       printf( no n ); n     fflush(stdout); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k = 0; n   cin >> n; n   vector<int> adj[n]; n   for (int i = 1, u, v; i < n; i++) { n     cin >> u >> v; n     u--; n     v--; n     adj[u].push_back(v); n     adj[v].push_back(u); n     k = max(k, max((int)adj[u].size(), (int)adj[v].size()) + 1); n   } n   cout << k << endl; n   int nd_clr[n], prnt[n]; n   queue<int> Q; n   Q.push(0); n   nd_clr[0] = 0; n   prnt[0] = 0; n   while (!Q.empty()) { n     int u = Q.front(); n     Q.pop(); n     int clr = nd_clr[u]; n     for (int i = 0; i < adj[u].size(); i++) { n       int v = adj[u][i]; n       if (v != prnt[u]) { n         int c = (clr + 1) % k; n         if (c == nd_clr[prnt[u]]) c = (c + 1) % k; n         nd_clr[v] = c; n         clr = c; n         prnt[v] = u; n         Q.push(v); n       } n     } n   } n   for (int i = 0; i < n; i++) cout << nd_clr[i] + 1 <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   for (int i = 1; i <= t; i++) { n     long long n; n     cin >> n; n     long long s = sqrt(n); n     long long c = cbrt(n); n     long long p = sqrt(cbrt(n)); n     cout << s + c - p <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int res = 0, f = 1; n   char ch; n   while (ch = getchar(), ch <  0  || ch >  9 ) n     if (ch ==  - ) f = -1; n   while (ch >=  0  && ch <=  9 ) res = res * 10 + ch -  0 , ch = getchar(); n   return res * f; n } n const int N = 14, M = 4782969, P = 1e9 + 7; n inline int qpow(int a, int b) { n   int ans = 1, base = a; n   while (b) { n     if (b & 1) ans = 1ll * ans * base % P; n     base = 1ll * base * base % P; n     b >>= 1; n   } n   return ans; n } n int rev[1 << N], cnt[1 << N], n, A, a[N]; n int F[1 << N], G[1 << N], v[N][1 << N]; n inline int get(int S, int T) { n   if (!S) return 1; n   if (G[S]) return G[S]; n   return G[S] = 1ll * get(S & (S - 1), T) * v[rev[S & -S]][T] % P; n } n int main() { n   n = read(), A = (1 << n) - 1; n   for (int i = 0; i < n; ++i) a[i] = read(), rev[1 << i] = i; n   for (int i = 0; i < n; ++i) { n     v[i][0] = 1; n     for (int S = 1; S < (1 << n); ++S) n       v[i][S] = 1ll * v[i][S & (S - 1)] * a[i] % P * n                 qpow((a[rev[S & -S]] + a[i]) % P, P - 2) % P; n   } n   for (int S = 1; S < (1 << n); ++S) { n     int tmp = 0; n     cnt[S] = cnt[S >> 1] + (S & 1); n     for (int T = (S - 1) & S; T; T = (T - 1) & S) n       (tmp += 1ll * F[T] * get(S ^ T, A ^ S) % P) %= P; n     F[S] = (get(S, A ^ S) - tmp + P) % P; n     for (int T = S; T; T = (T - 1) & S) G[T] = 0; n   } n   int res = 0; n   for (int S = 1; S < (1 << n); ++S) (res += 1ll * F[S] * cnt[S] % P) %= P; n   printf( %d , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int x = 0, f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) f = ch ==  -  ? -1 : 1, ch = getchar(); n   while (ch >=  0  && ch <=  9 ) x = x * 10 + ch - 48, ch = getchar(); n   return x * f; n } n const int maxn = 205; n int tot; n int R, G, B; n int r[maxn]; n int g[maxn]; n int b[maxn]; n int f[maxn][maxn][maxn]; n struct Stick { n   int v, c; n } a[maxn << 3]; n inline bool cmp(int u, int v) { return u > v; } n int main() { n   R = read(), G = read(), B = read(); n   for (int i = (1); i <= (R); i++) r[i] = read(); n   for (int i = (1); i <= (G); i++) g[i] = read(); n   for (int i = (1); i <= (B); i++) b[i] = read(); n   sort(r + 1, r + R + 1, cmp), sort(g + 1, g + G + 1, cmp), n       sort(b + 1, b + B + 1, cmp); n   f[1][1][0] = r[1] * g[1]; n   f[1][0][1] = r[1] * b[1]; n   f[0][1][1] = g[1] * b[1]; n   int ans = 0; n   for (int i = (0); i <= (R); i++) n     for (int j = (0); j <= (G); j++) n       for (int k = (0); k <= (B); k++) { n         if (i && j) n           f[i][j][k] = max(f[i][j][k], f[i - 1][j - 1][k] + r[i] * g[j]); n         if (i && k) n           f[i][j][k] = max(f[i][j][k], f[i - 1][j][k - 1] + r[i] * b[k]); n         if (j && k) n           f[i][j][k] = max(f[i][j][k], f[i][j - 1][k - 1] + g[j] * b[k]); n         ans = max(ans, f[i][j][k]); n       } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool comp(const pair<int, int> &a, const pair<int, int> &b) { n   if (a.first == b.first) return a.second < b.second; n   return a.first < b.first; n } n bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) { n   if (a.second == b.second) return b.first > a.first; n   return (a.second < b.second); n } n bool compmx(int a, int b) { return (a < b); } n long long int bin(long long int n) { n   long long int B_Number = 0; n   long long int cnt = 0; n   while (n != 0) { n     long long int rem = n % 2; n     long long int c = pow(10, cnt); n     B_Number += rem * c; n     n /= 2; n     cnt++; n   } n   return B_Number; n } n long long int gcd(long long int a, long long int b) { n   if (b == 0) n     return a; n   else n     return gcd(b, a % b); n } n bool is_prime(long long int x) { n   if (x == 1) return false; n   if (x == 2 || x == 3) return true; n   for (long long int i = 2; i * i <= x; i++) { n     if (x % i == 0) return false; n   } n   return true; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long int d, n; n   cin >> d >> n; n   ; n   long long int a[n]; n   for (long long int i = 0; i < n; i++) { n     cin >> a[i]; n   }; n   long long int ans = 0; n   long long int x; n   for (long long int i = 1; i < n; i++) { n     x = a[i - 1] + 1; n     if (x > d) x = 1; n     if (x == 1) n       continue; n     else n       ans += (d - x + 1); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1073741824; n const int N = 100005; n const double PI = 4 * atan(1); 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 gcd(long long a, long long b) { return (b == 0 ? a : gcd(b, a % b)); } n long long lcm(long long a, long long b) { return a * (b / gcd(a, b)); } n bool issquare(long long w) { return trunc(sqrt(w)) * trunc(sqrt(w)) == w; } n bool isprime(long long u) { n   for (long long i = 2; i <= (int)sqrt(u); i++) { n     if (u % i == 0) return 0; n   } n   return 1; n } n long long mod(long long to_mod) { n   to_mod %= MOD; n   while (to_mod < 0) to_mod += MOD; n   return to_mod % MOD; n } n long long moduloMultiplication(long long a, long long b, long long mod) { n   long long res = 0; n   a %= mod; n   while (b) { n     if (b & 1) res = (res + a) % mod; n     a = (2 * a) % mod; n     b >>= 1; n   } n   return res; n } n long long power(long long x, long long y) { n   long long res = 1; n   x = x; n   while (y > 0) { n     if (y & 1) res = (res * x); n     y = y >> 1; n     x = (x * x); n   } n   return res; n } n long long _sieve_size; n bitset<1000010> bs; n vector<long long> primes; n void sieve(long long upperbound) { n   _sieve_size = upperbound + 1; n   bs.set(); n   bs[0] = bs[1] = 0; n   for (long long i = 2; i <= _sieve_size; i++) n     if (bs[i]) { n       for (long long j = i * i; j <= _sieve_size; j += i) bs[j] = 0; n       primes.push_back(i); n     } n } n long long numDiv(long long N) { n   long long PF_idx = 0, PF = primes[PF_idx], ans = 1; n   while (PF * PF <= N) { n     long long power = 0; n     while (N % PF == 0) { n       N /= PF; n       power++; n     } n     ans *= (power + 1); n     PF = primes[++PF_idx]; n   } n   if (N != 1) ans *= 2; n   return ans; n } n int main() { n   sieve(1000000); n   long long t[1000005]; n   for (int i = 1; i <= 1000000; i++) { n     t[i] = numDiv(i); n   } n   long long a, b, c, ans = 0; 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         ans = mod(ans + t[i * j * k]); n       } n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int main() { n   ios_base::sync_with_stdio(false); n   while (cin >> n) { n     vector<int> inside(1000006, 0), ans; n     int stack = 0, length = 0, x, valid = 1, j = 1; n     unordered_map<int, int> used; n     for (int i = 0; i < n; ++i) { n       cin >> x; n       if ((x > 0 && inside[x]) || (x < 0 && !inside[-x]) || n           (x > 0 && used[x])) { n         valid = 0; n       } else if (x > 0) { n         ++inside[x]; n         ++stack; n         ++length; n         used[x] = j++; n       } else if (x < 0) { n         --inside[-x]; n         --stack; n         ++length; n       } n       if (!stack && valid) { n         ans.emplace_back(length); n         length = 0; n         used.clear(); n       } n     } n     if (stack) valid = 0; n     if (valid) { n       cout << ans.size() << endl; n       for (int i : ans) cout << i <<    ; n       cout << endl; n     } else { n       cout << -1 << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n uint64_t ans, fact[21]; n long long t, n, cont, val; n bool us[30]; n vector<uint64_t> v; n uint64_t calc() { n   uint64_t tmp = 1; n   uint64_t freq[30]; n   uint64_t ind = 0; n   for (int i = 0; i < 30; i++) freq[i] = 0; n   for (int i = 0; i < v.size(); i++) { n     while (ind < v.size() && v[ind] < (i + 2)) { n       ind++; n     } n     freq[v[i]]++; n     tmp *= max((uint64_t)0, ind - i); n   } n   for (int i = 0; i < 30; i++) { n     tmp /= fact[freq[i]]; n   } n   return tmp; n } n int main() { n   fact[0] = 1; n   for (int i = 1; i < 21; i++) fact[i] = fact[i - 1] * uint64_t(i); n   cin >> t; n   while (t--) { n     cin >> n; n     v.clear(); n     ans = 0; n     memset(us, false, sizeof(us)); n     cont = 2; n     while (n) { n       v.push_back(n % cont); n       n /= cont; n       cont++; n     } n     sort(v.begin(), v.end()); n     for (int i = 0; i < v.size(); i++) { n       if (v[i] != 0 && us[v[i]] == false) { n         val = v[i]; n         v.erase(v.begin() + i); n         ans += calc(); n         v.insert(v.begin() + i, val); n         us[val] = true; n       } n     } n     cout << ans - 1 <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int m, n, cn, nw, res, ans; n int used[533]; n int x[1020], y[1020]; n int a[1020][1020]; n int asjkjas[1020]; n bool kuhn(int x) { n   if (used[x] == nw) return false; n   used[x] = nw; n   for (int i = (0); i < (n); ++i) n     if (a[x][i] && i != cn && (asjkjas[i] == -1 || kuhn(asjkjas[i]))) { n       asjkjas[i] = x; n       return true; n     } n   return false; n } n int main() { n   ios::sync_with_stdio(false); n   cin >> n >> m; n   for (int i = (0); i < (m); ++i) { n     cin >> x[i] >> y[i]; n     x[i]--; n     y[i]--; n     a[x[i]][y[i]] = 1; n   } n   int res = 1000000000; n   for (int c = (0); c < (n); ++c) { n     cn = c; n     int ans = 0; n     for (int i = (0); i < (n); ++i) { n       if (i != c) n         ans += 2 - a[i][c] - a[c][i]; n       else n         ans += 1 - a[i][i]; n     } n     for (int i = (0); i < (n); ++i) used[i] = -1, asjkjas[i] = -1; n     int ep = 0; n     for (int i = (0); i < (n); ++i) { n       if (i != c) { n         nw = i; n         if (kuhn(i)) ++ep; n       } n     } n     ans += (n - 1) - ep; n     for (int i = (0); i < (m); ++i) n       if (x[i] != c && y[i] != c) ans += 1; n     ans -= ep; n     res = min(res, ans); n   } n   cout << res << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int odd, even; n int miny[400001], maxy[400001], min2y[400001], max2y[400001]; n vector<int> p[400001]; n int main() { n   ios::sync_with_stdio(false); n   cin >> n; n   for (int i = 0; i <= 2e5; i++) { n     maxy[i] = -1, miny[i] = 2e5 + 1; n   } n   for (int i = 1; i <= n; i++) { n     int u, v, x, y; n     cin >> u >> v; n     x = v - u; n     y = u + v; n     x += 2e5; n     y += 2e5; n     p[x].push_back(y); n   } n   for (int i = 0; i <= 4e5; i++) { n     sort(p[i].begin(), p[i].end()); n     miny[i] = min2y[i] = 4e5 + 1; n     maxy[i] = max2y[i] = -1; n   } n   for (int i = 1; i <= 4e5; i++) { n     if (i > 1) maxy[i] = maxy[i - 2]; n     if (i > 1) miny[i] = miny[i - 2]; n     if (!p[i - 1].empty()) maxy[i] = max(maxy[i], p[i - 1].back() - 1); n     if (!p[i - 1].empty()) miny[i] = min(miny[i], p[i - 1][0] + 1); n   } n   long long ans = 0; n   for (int i = 4e5 - 1; i >= 0; i--) { n     if (i < 4e5 - 1) max2y[i] = max2y[i + 2]; n     if (i < 4e5 - 1) min2y[i] = min2y[i + 2]; n     if (!p[i + 1].empty()) max2y[i] = max(max2y[i], p[i + 1].back() - 1); n     if (!p[i + 1].empty()) min2y[i] = min(min2y[i], p[i + 1][0] + 1); n     maxy[i] = min(maxy[i], max2y[i]); n     miny[i] = max(miny[i], min2y[i]); n     if (maxy[i] >= miny[i]) { n       ans += (maxy[i] - miny[i]) / 2 + 1; n     } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n template <class C> n void mini(C& a4, C b4) { n   a4 = min(a4, b4); n } n template <class C> n void maxi(C& a4, C b4) { n   a4 = max(a4, b4); n } n template <class T1, class T2> n ostream& operator<<(ostream& out, pair<T1, T2> pair) { n   return out <<  (  << pair.first <<  ,   << pair.second <<  ) ; n } n template <class A, class B, class C> n struct Triple { n   A first; n   B second; n   C third; n }; n template <class T> n void ResizeVec(T&, vector<long long>) {} n template <class T> n void ResizeVec(vector<T>& vec, vector<long long> sz) { n   vec.resize(sz[0]); n   sz.erase(sz.begin()); n   if (sz.empty()) { n     return; n   } n   for (T& v : vec) { n     ResizeVec(v, sz); n   } n } n void Pol() { n   cout <<  Polycarp n ; n   exit(0); n } n void Vas() { n   cout <<  Vasiliy n ; n   exit(0); n } n int main() { n   ios_base::sync_with_stdio(0); n   cout << fixed << setprecision(10); n   double beg_clock = 1.0 * clock() / CLOCKS_PER_SEC; n   long long xp, yp, xv, yv; n   cin >> xp >> yp >> xv >> yv; n   ; n   if (xv < yv) { n     swap(xp, yp); n     swap(xv, yv); n   } n   if (xp <= xv && yp <= yv) { n     Pol(); n   } n   if (xp + yp <= max(xv, yv)) { n     Pol(); n   } n   if (xv < xp) { n     Vas(); n   } n   long long tv = max(yv, xv - xp); n   long long tp = yp; n   if (tp <= tv) { n     Pol(); n   } else { n     Vas(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100005; n set<int> g[maxn]; n set<int> vis; n int u[maxn]; n void dfs(int x) { n   u[x] = 1; n   vis.erase(x); n   vector<int> ret; n   for (int p : vis) { n     if (!g[x].count(p)) { n       ret.push_back(p); n     } n   } n   for (int t : ret) { n     vis.erase(t); n   } n   for (int t : ret) { n     u[t] = 1; n     dfs(t); n   } n } n int main() { n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     int x, y; n     cin >> x >> y; n     x--, y--; n     g[x].insert(y); n     g[y].insert(x); n   } n   for (int i = 0; i < n; i++) { n     vis.insert(i); n   } n   int ans = 0; n   for (int i = 0; i < n; i++) { n     if (!u[i]) { n       ans++; n       dfs(i); n     } n   } n   cout << ans - 1 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a[6]; n   for (int i = 0; i < 6; i++) cin >> a[i]; n   a[5]++; n   int mn = 100000; n   for (int i = 0; i < 6; i++) { n     if (i == 4) continue; n     if (mn > a[i]) mn = a[i]; n   } n   cout << (mn > a[4] ? mn - a[4] : 0) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, s[100005]; n char t[100005]; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %d , &s[i]); n   scanf( %s , t); n   int tot = 0, ans = 0; n   for (int i = 0; i < n; i++) { n     if (t[i] ==  1 ) ans = max(tot, ans + s[i]); n     tot += s[i]; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3fffffff; n const int SINF = 0x7fffffff; n const long long LINF = 0x3fffffffffffffff; n const long long SLINF = 0x7fffffffffffffff; n const int MAXN = 2007; n int n, m; n vector<int> es[MAXN]; n bitset<MAXN> bs[MAXN]; n bool hv[MAXN]; n int dp[MAXN]; n void init(); n void input(); n void work(); n int main() { n   init(); n   int T; n   scanf( %d , &T); n   while (T--) { n     input(); n     work(); n   } n } n void init() { ios::sync_with_stdio(false); } n void input() { n   scanf( %d%d , &n, &m); n   int u, v; n   for (int i = 1; i <= n; ++i) es[i].clear(); n   for (int i = 1; i <= m; ++i) { n     scanf( %d%d , &u, &v); n     es[u].push_back(v); n     es[v].push_back(u); n   } n } n void work() { n   bool flag = true; n   for (int i = 1; i <= n; ++i) { n     if (es[i].size() & 1) { n       flag = false; n       break; n     } n   } n   if (flag) { n     printf( 1 n ); n     for (int i = 1; i <= n; ++i) printf( 1  ); n     putchar(  n ); n     return; n   } n   bitset<MAXN> nb; n   memset(hv, false, sizeof(hv)); n   for (int i = 1; i <= n; ++i) bs[i] = 0; n   for (int i = 1; i <= n; ++i) { n     nb = 0; n     for (auto x : es[i]) nb[x] = 1; n     nb[i] = es[i].size() & 1; n     nb[n + 1] = es[i].size() & 1; n     for (int j = 1; j <= n; ++j) { n       if (nb[j]) { n         if (hv[j]) n           nb ^= bs[j]; n         else { n           hv[j] = true; n           bs[j] = nb; n           break; n         } n       } n     } n   } n   memset(dp, 0, sizeof(dp)); n   for (int i = n; i > 0; --i) { n     if (hv[i]) { n       dp[i] = bs[i][n + 1]; n       for (int j = i + 1; j <= n; ++j) { n         if (bs[i][j] && hv[j]) dp[i] ^= dp[j]; n       } n     } n   } n   printf( 2 n ); n   for (int i = 1; i <= n; ++i) printf( %d  , dp[i] + 1); n   putchar(  n ); n } n


#include <bits/stdc++.h> n using namespace std; n template <typename A, typename B> n ostream& operator<<(ostream& os, const pair<A, B>& x) { n   return os <<  (  << x.first <<  ,  << x.second <<  ) ; n } n const int MOD = 998244353; n long long modex(long long a, long long p, long long m = MOD) { n   if (!p) return 1; n   if (p & 1) return a * modex(a, p - 1, m) % m; n   long long v = modex(a, p >> 1, m); n   return v * v % m; n } n namespace fft { n const int mod = 998244353; n const int gen = 3; n vector<int> rev(1, 0), roots; n int base = 0; n void precompute(int nbase) { n   if (nbase <= base) return; n   rev.resize(1 << nbase); n   for (int i = 0; i < (1 << nbase); ++i) { n     rev[i] = rev[i >> 1] >> 1 | ((i & 1) << nbase - 1); n   } n   roots.resize(1 << nbase); n   for (; base < nbase; ++base) { n     int len = 1 << base; n     long long root = modex(gen, (mod - 1) >> base + 1, mod); n     roots[len] = 1; n     for (int i = 1; i < len; ++i) n       roots[len + i] = roots[len + i - 1] * root % mod; n   } n } n void fft(vector<int>& a) { n   int n = a.size(); n   assert((n & (n - 1)) == 0); n   int zeros = __builtin_ctz(n); n   precompute(zeros); n   int shift = base - zeros; n   for (int i = 0; i < n; ++i) { n     if (i < (rev[i] >> shift)) swap(a[i], a[rev[i] >> shift]); n   } n   for (int len = 1; len < n; len <<= 1) n     for (int i = 0; i < n; i += 2 * len) n       for (int j = 0; j < len; ++j) { n         int u = a[i + j], v = (long long)a[i + j + len] * roots[len + j] % mod; n         a[i + j] = u + v >= mod ? u + v - mod : u + v; n         a[i + j + len] = u - v < 0 ? u - v + mod : u - v; n       } n } n vector<int> multiply(vector<int> a, vector<int> b) { n   if (a.empty() || b.empty()) return {}; n   int sz = a.size() + b.size() - 1; n   int n = sz == 1 ? 1 : 1 << (32 - __builtin_clz(sz - 1)); n   bool eq = (a == b); n   a.resize(n); n   fft(a); n   if (eq) n     b = a; n   else { n     b.resize(n); n     fft(b); n   } n   long long inv = modex(n, mod - 2, mod); n   for (int i = 0; i < n; ++i) a[i] = (long long)a[i] * b[i] % mod * inv % mod; n   fft(a); n   reverse(a.begin() + 1, a.end()); n   a.resize(sz); n   return a; n } n vector<int> multiply(const vector<int>& a, const vector<int>& b, int m) { n   assert(m == mod); n   return multiply(a, b); n } n };  // namespace fft n vector<int> B; n vector<int> multiplyAll(int s, int e) { n   if (s == e) { n     return {1, B[s]}; n   } n   int m = (s + e) >> 1; n   auto a = multiplyAll(s, m); n   auto b = multiplyAll(m + 1, e); n   return fft::multiply(a, b); n } n int sub(int x, int y) { n   x -= y; n   if (x < 0) x += MOD; n   return x; n } n int main() { n   int n, k; n   scanf( %d%d , &n, &k); n   vector<int> A(n); n   B = vector<int>(n); n   for (int i = 0; i < n; ++i) { n     scanf( %d , &A[i]); n     A[i] %= MOD; n   } n   int Q; n   scanf( %d , &Q); n   while (Q--) { n     int t, q, a, b, d; n     scanf( %d%d%d%d , &t, &q, &a, &b); n     if (t == 1) { n       b %= MOD; n       for (int i = 0; i < n; ++i) B[i] = sub(q, A[i]); n       B[a - 1] = sub(q, b); n     } else { n       scanf( %d , &d); n       d %= MOD; n       for (int i = 0; i < n; ++i) B[i] = sub(q, A[i]); n       for (int i = a - 1; i < b; ++i) B[i] = sub(B[i], d); n     } n     auto p = multiplyAll(0, n - 1); n     printf( %d n , p[k]); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long double EPS = 1e-10; n const long long MOD = 1000000007ll; n const long long mod1 = 1000000009ll; n const long long mod2 = 1100000009ll; n int INF = (int)1e9 + 5; n long long INFINF = (long long)1e18; n int main() { n   int n; n   scanf( %d , &n); n   int a, b, diff; n   diff = INF; n   a = -1; n   b = -1; n   for (int i = 1; i <= n; i++) { n     if (n % i == 0) { n       int a_t, b_t; n       a_t = i; n       b_t = n / i; n       if (abs(a_t - b_t) < diff) { n         diff = abs(a_t - b_t); n         a = min(a_t, b_t); n         b = max(a_t, b_t); n       } n     } n   } n   printf( %d  , a); n   printf( %d n , b); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long modpow(long long a, long long p, long long mod) { n   long long ret = 1; n   while (p) { n     if (p & 1) ret = (ret * a) % mod; n     a = (a * a) % mod; n     p /= 2; n   } n   return ret; n } n long long power(long long a, long long p) { n   long long ret = 1; n   while (p) { n     if (p & 1) ret = (ret * a); n     a = (a * a); n     p /= 2; n   } n   return ret; n } n int n, m; n int dx[] = {-1, 1, 0, 0}; n int dy[] = {0, 0, -1, 1}; n bool check(int i, int j) { n   if (i >= 0 && j >= 0 && i < n && j < m) return 1; n   return 0; n } n int v[2015][2015] = {0}, deg[2015][2015] = {0}; n string mat[2015]; n queue<pair<int, int> > q; n pair<int, int> p; n int main() { n   int i, j, tmp; n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cin >> n >> m; n   int x, y, xx, yy; n   for (i = 0; i < n; i++) cin >> mat[i]; n   for (i = 0; i < n; i++) { n     for (j = 0; j < m; j++) { n       if (mat[i][j] ==  . ) n         for (x = 0; x < 4; x++) { n           y = x; n           if (check(i + dx[x], j + dy[y]) && mat[i + dx[x]][j + dy[y]] ==  . ) n             deg[i][j]++; n         } n       if (deg[i][j] == 1) q.push(make_pair(i, j)); n     } n   } n   while (!q.empty()) { n     pair<int, int> p; n     p = q.front(); n     q.pop(); n     i = p.first; n     j = p.second; n     for (x = 0; x < 4; x++) { n       y = x; n       if (check(i + dx[x], j + dy[y]) && mat[i + dx[x]][j + dy[y]] ==  . ) { n         if (dx[x] == 1) { n           mat[i][j] =  ^ ; n           mat[i + dx[x]][j + dy[y]] =  v ; n         } else if (dx[x] == -1) { n           mat[i][j] =  v ; n           mat[i + dx[x]][j + dy[y]] =  ^ ; n         } else if (dy[y] == 1) { n           mat[i][j] =  < ; n           mat[i + dx[x]][j + dy[y]] =  > ; n         } else if (dy[y] == -1) { n           mat[i][j] =  > ; n           mat[i + dx[x]][j + dy[y]] =  < ; n         } n         for (xx = 0; xx < 4; xx++) { n           yy = xx; n           if (check(i + dx[xx], j + dy[yy])) { n             deg[i + dx[xx]][j + dy[yy]]--; n             if (mat[i + dx[xx]][j + dy[yy]] ==  .  && n                 deg[i + dx[xx]][j + dy[yy]] == 1) n               q.push(make_pair(i + dx[xx], j + dy[yy])); n           } n           if (check(i + dx[x] + dx[xx], j + dy[y] + dy[yy])) { n             deg[i + dx[x] + dx[xx]][j + dy[y] + dy[yy]]--; n             if (mat[i + dx[x] + dx[xx]][j + dy[y] + dy[yy]] ==  .  && n                 deg[i + dx[x] + dx[xx]][j + dy[y] + dy[yy]] == 1) n               q.push(make_pair(i + dx[x] + dx[xx], j + dy[y] + dy[yy])); n           } n         } n         break; n       } n     } n   } n   int f = 1; n   for (i = 0; i < n; i++) n     for (j = 0; j < m; j++) n       if (mat[i][j] ==  . ) f = 0; n   if (f) { n     for (i = 0; i < n; i++) { n       for (j = 0; j < m; j++) cout << mat[i][j]; n       cout << endl; n     } n   } else n     cout <<  Not unique n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool sortmahstyle(const pair<int, int> &a, const pair<int, int> &b) { n   if (a.first == b.first && a.second < b.second) return true; n   return false; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int n, m, d; n   cin >> n >> m >> d; n   long long int a[n * m]; n   for (long long int i = 0; i < n * m; i++) cin >> a[i]; n   long long int ans = 0; n   sort(a, a + (n * m)); n   for (long long int i = 0; i < n * m; i++) ans += abs(a[i] - a[n * m / 2]); n   if (ans % d == 0) n     cout << ans / d <<   n ; n   else n     cout <<  -1 n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 10010; n vector<pair<short int, short int> > G[MAXN]; n vector<short int> cul[MAXN], row[MAXN]; n pair<short int, short int> p[MAXN]; n void add_edge(int u, int v, int d) { G[u].push_back(make_pair(v, d)); } n void init(int n) { n   for (int i = 1; i <= n; i++) { n     G[i].clear(); n   } n } n bool vis[MAXN]; n int dis[MAXN]; n void dijkstra(int s, int n) { n   for (int i = 1; i <= n; i++) vis[i] = 0; n   for (int i = 1; i <= n; i++) dis[i] = (i == s ? 0 : 0x3FFFFFFF); n   priority_queue<pair<short int, short int>, n                  vector<pair<short int, short int> >, n                  greater<pair<short int, short int> > > n       q; n   q.push(make_pair(dis[s], s)); n   while (!q.empty()) { n     pair<short int, short int> p = q.top(); n     int x = p.second; n     q.pop(); n     if (vis[x]) continue; n     vis[x] = 1; n     for (int i = 0; i < G[x].size(); i++) { n       int y = G[x][i].first; n       int d = G[x][i].second; n       if (!vis[y] && dis[x] + d < dis[y]) { n         dis[y] = dis[x] + d; n         q.push(make_pair(dis[y], y)); n       } n     } n   } n } n map<pair<short int, short int>, short int> mp; n int main() { n   int n, m, k, a, b; n   scanf( %d%d%d , &n, &m, &k); n   for (int i = 1; i <= k; i++) { n     scanf( %d%d , &a, &b); n     p[i].first = a; n     p[i].second = b; n     mp[p[i]] = i; n     cul[p[i].second].push_back(p[i].first); n     row[p[i].first].push_back(p[i].second); n   } n   init(k); n   for (int i = 1; i <= k; i++) { n     short int x = p[i].first, y = p[i].second; n     for (int j = 0; j < row[x].size(); j++) n       if (row[x][j] != y) n         add_edge(i, mp[make_pair(x, row[x][j])], n                  row[x][j] - 1 != y && row[x][j] + 1 != y); n     for (int j = 0; j < cul[y].size(); j++) n       if (cul[y][j] != x) n         add_edge(i, mp[make_pair(cul[y][j], y)], n                  cul[y][j] - 1 != x && cul[y][j] + 1 != x); n     for (int l = -1; l <= 1; l += 2) { n       for (int j = 0; j < row[x + l].size(); j++) n         if (row[x + l][j] != y) n           add_edge(i, mp[make_pair(x + l, row[x + l][j])], 1); n       for (int j = 0; j < cul[y + l].size(); j++) n         if (cul[y + l][j] != x) n           add_edge(i, mp[make_pair(cul[y + l][j], y + l)], 1); n     } n     for (int l = -2; l <= 2; l = l + 4) { n       if (x + l > 0 && x + l <= m + 1) n         for (int j = 0; j < row[x + l].size(); j++) n           add_edge(i, mp[make_pair(x + l, row[x + l][j])], 1); n       if (y + l > 0 && y + l <= n + 1) n         for (int j = 0; j < cul[y + l].size(); j++) n           add_edge(i, mp[make_pair(cul[y + l][j], y + l)], 1); n     } n   } n   dijkstra(1, k); n   int ans = 0x3FFFFFFF; n   for (int i = 1; i <= k; i++) { n     if (dis[i] == 0x3FFFFFFF || dis[i] > ans) continue; n     if (n - p[i].first + m - p[i].second <= 1) n       ans = min(ans, dis[i] + (mp[make_pair(n, m)] == 0)); n     else if (n - p[i].first <= 1 || m - p[i].second <= 1) n       ans = min(ans, dis[i] + 1); n   } n   if (ans == 0x3FFFFFFF) n     cout << -1 << endl; n   else n     cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int sum = 0; n   while (n) { n     if (n % 8 == 1) { n       sum++; n     } n     n = n / 8; n   } n   cout << sum; n   return 0; n } n


#include <bits/stdc++.h> n #define maxn 10 n  n using namespace std; n  n int n, m; n map<vector<int>, int> f, g; n int a[maxn], b[maxn], c[maxn][maxn]; n  n int main(){ n  scanf( %d%d , &n, &m); n  vector<int> v; n  for(int i = 1;i <= n;i++) scanf( %d , &a[i]); n  for(int i = 1;i <= m;i++) scanf( %d , &b[i]); n  for(int i = 1;i <= n;i++) for(int j = 1;j <= m;j++) scanf( %d , &c[i][j]); n  f[vector<int>(n + 1)] = 0; n  for(int j = 1;j <= m;j++){ n   for(map<vector<int>, int>::iterator it = f.begin();it != f.end();++it){ n    vector<int> v = it->first; n    v[n] = 0; n    if(g.find(v) == g.end()) g[v] = it->second; n    else g[v] = min(g[v], it->second); n   } n   swap(f, g), g.clear(); n   for(int i = 1;i <= n;i++){ n    for(map<vector<int>, int>::iterator it = f.begin();it != f.end();++it){ n     vector<int> v = it->first;  n     for(int k = 0;k <= min(a[i] - v[i - 1], b[j] - v[n]);k++){ n      vector<int> w = v; n      w[i - 1] += k, w[n] += k;  n      if(g.find(w) == g.end()) g[w] = it->second + (k ? c[i][j] : 0); n      else g[w] = min(g[w], it->second + (k ? c[i][j] : 0));    n     } n    } n    swap(f, g), g.clear(); n   } n  } n  int ans = 1e9; n  for(map<vector<int>, int>::iterator it = f.begin();it != f.end();++it){ n   vector<int> v = it->first;  n   bool tag = false; n   for(int i = 0;i < n;i++){ n    if(v[i] < a[i + 1]){ n     tag = true; n     break; n    } n   } n   if(!tag) ans = min(ans, it->second); n  } n  printf( %d , ans == 1e9 ? -1 : ans); n }


#include <bits/stdc++.h> n using namespace std; n void FAST_IO(string filein =   , string fileout =   , string fileerr =   ) { n   if (fopen(filein.c_str(),  r )) { n     freopen(filein.c_str(),  r , stdin); n     freopen(fileout.c_str(),  w , stdout); n   } n   cin.tie(0), cout.tie(0)->sync_with_stdio(0); n } n void Hollwo_Pelw(); n signed main() { n   FAST_IO( hollwo_pelw.inp ,  hollwo_pelw.out ); n   int testcases = 1; n   for (int test = 1; test <= testcases; test++) { n     Hollwo_Pelw(); n   } n   return 0; n } n const int N = 2e5 + 5, L = 18; n struct edge { n   int t, c, u, v; n   edge(int _t = 0, int _c = 0, int _u = 0, int _v = 0) n       : t(_t), c(_c), u(_u), v(_v) {} n   bool operator<(const edge &o) const { return c != o.c ? c > o.c : t > o.t; } n }; n int n, q, res_enj[N], res_tol[N], h[N]; n vector<pair<int, int>> adj[N]; n pair<int, int> jmp[N][L]; n void pre_dfs(int u, int p = 0) { n   h[u] = h[p] + 1; n   for (int i = 1; i < 18; i++) { n     auto [v1, w1] = jmp[u][i - 1]; n     auto [v2, w2] = jmp[v1][i - 1]; n     jmp[u][i] = {v2, max(w1, w2)}; n   } n   for (auto [v, w] : adj[u]) n     if (v != p) { n       jmp[v][0] = {u, w}; n       pre_dfs(v, u); n     } n } n inline int get_toll(int u, int v) { n   int res = 0; n   if (h[u] < h[v]) swap(u, v); n   for (int i = 17; ~i; i--) n     if (h[u] - h[v] >> i & 1) { n       auto [nu, wu] = jmp[u][i]; n       u = nu, res = max(res, wu); n     } n   if (u == v) return res; n   for (int i = 17; ~i; i--) n     if (jmp[u][i].first ^ jmp[v][i].first) { n       auto [nu, wu] = jmp[u][i]; n       u = nu, res = max(res, wu); n       auto [nv, wv] = jmp[v][i]; n       v = nv, res = max(res, wv); n     } n   return max({res, jmp[u][0].second, jmp[v][0].second}); n } n int par[N], max_enj[N], max_pos[N], toll_in[N]; n struct disjoint_set { n   disjoint_set(int n = N) { n     iota(par, par + n, 0); n     iota(max_pos, max_pos + n, 0); n   } n   inline int find(int u) { return par[u] == u ? u : par[u] = find(par[u]); } n   inline void merge(int u, int v) { n     assert((u = find(u)) != (v = find(v))); n     par[v] = u; n     if (max_enj[u] < max_enj[v]) { n       max_enj[u] = max_enj[v]; n       max_pos[u] = max_pos[v]; n       toll_in[u] = toll_in[v]; n     } else if (max_enj[u] == max_enj[v]) { n       toll_in[u] = n           max({toll_in[u], toll_in[v], get_toll(max_pos[u], max_pos[v])}); n     } n   } n }; n void Hollwo_Pelw() { n   cin >> n >> q; n   for (int i = 1; i <= n; i++) cin >> max_enj[i]; n   vector<edge> queries(n + q - 1); n   for (int i = 0, u, v, c, t; i < n - 1; i++) { n     cin >> u >> v >> c >> t; n     adj[u].emplace_back(v, t); n     adj[v].emplace_back(u, t); n     queries[i] = edge(t, c, u, v); n   } n   for (int i = 0, u, c; i < q; i++) { n     cin >> c >> u; n     queries[i + n - 1] = {0, c, u, i}; n   } n   pre_dfs(1); n   sort(queries.begin(), queries.end()); n   disjoint_set dsu(n + 1); n   for (auto [t, c, u, v] : queries) { n     if (t) { n       dsu.merge(u, v); n     } else { n       int pu = dsu.find(u); n       res_enj[v] = max_enj[pu]; n       res_tol[v] = max(toll_in[pu], get_toll(u, max_pos[pu])); n     } n   } n   for (int i = 0; i < q; i++) cout << res_enj[i] <<     << res_tol[i] <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n struct cell { n   long long root_vertix, com_len; n   cell() {} n   cell(long long x, long long y) { n     root_vertix = x; n     com_len = y; n   } n   bool operator<(const cell &x) const { return com_len < x.com_len; } n }; n long long root[100002]; n long long n, m, p, q; n long long find_root(long long x) { n   if (root[x] <= 0) return x; n   return root[x] = find_root(root[x]); n } n bool merge(long long x, long long y, long long len) { n   x = find_root(x); n   y = find_root(y); n   if (x == y) { n     root[x] -= len; n     return false; n   } else { n     root[x] = root[x] - len + root[y]; n     root[y] = x; n     return 1; n   } n } n int main() { n   cin >> n >> m >> p >> q; n   long long components = n; n   fill(root, root + n + 1, 0); n   for (int i = 0; i < m; i++) { n     long long x, y, z; n     cin >> x >> y >> z; n     if (merge(x, y, z)) components--; n   } n   if (components < q || p < (components - q) || n       (components == n && q == n && p > 0)) { n     cout <<  NO  << endl; n     return 0; n   } else n     cout <<  YES  << endl; n   priority_queue<cell> qq; n   for (int i = 1; i <= n; i++) n     if (root[i] <= 0) qq.push(cell(i, root[i])); n   long long diff = components - q; n   long long same = p - diff; n   while (diff--) { n     cell x, y; n     x = qq.top(); n     qq.pop(); n     y = qq.top(); n     qq.pop(); n     long long len = min((long long)1000000000, -x.com_len - y.com_len + 1); n     cout << x.root_vertix <<     << y.root_vertix << endl; n     merge(x.root_vertix, y.root_vertix, len); n     x.com_len = root[x.root_vertix]; n     qq.push(x); n   } n   long long vertix; n   for (int i = 1; i <= n; i++) n     if (root[i] > 0) { n       vertix = i; n       break; n     } n   while (same--) cout << vertix <<     << root[vertix] << endl; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n bool tu[305][305]; n long long pri[305]; n int d[305]; n int n, m, t; n int bg[1100000]; n int top[110000]; n int main() { n   scanf( %d%d%d , &n, &m, &t); n   for (int i = 1; i <= n; i++) scanf( %I64d , &pri[i]); n   for (int i = 1; i <= m; i++) { n     int b, c; n     scanf( %d%d , &b, &c); n     tu[b][c] = 1; n     d[c]++; n   } n   for (int kk = 1; kk <= n; kk++) { n     int now = 0; n     for (int i = 1; i <= n; i++) n       if (d[i] == 0) now = i; n     if (!now) { n       printf( 0 n ); n       return 0; n     } n     d[now] = -1; n     for (int i = 1; i <= n; i++) n       if (tu[now][i]) d[i]--; n     top[++top[0]] = now; n   } n   for (int i = 1; i <= top[0]; i++) { n     int x = top[i]; n     for (int j = 1; j <= n; j++) { n       if (tu[x][j]) pri[j] += pri[x], t -= pri[x]; n       if (t < 0) { n         printf( 0 n ); n         return 0; n       } n     } n   } n   if (t < 0) { n     printf( 0 n ); n     return 0; n   } n   bg[0] = 1; n   for (int i = 1; i <= n; i++) n     for (long long j = pri[i]; j <= t; j++) n       bg[j] = (bg[j] + bg[j - pri[i]]) % 1000000007; n   printf( %d n , t >= 0 ? bg[t] : 0); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int vis[1005], a[1005], b[25][25]; n int main() { n   ios::sync_with_stdio(false); n   int p, n, r, cnt, x, i, k = 0, j, flag = 0, s, t; n   cin >> n; n   for (i = 0; i < n * n; i++) { n     cin >> x; n     if (!vis[x]) { n       a[k++] = x; n     } n     vis[x]++; n   } n   if (n % 2 == 0) { n     for (i = 0; i < k; i++) { n       if (vis[a[i]] % 4 != 0) { n         flag = 1; n         break; n       } n     } n     if (flag == 1) n       cout <<  NO  << endl; n     else { n       cout <<  YES  << endl; n       s = n / 2; n       r = 0; n       t = vis[a[r]] / 4; n       for (i = 0; i < s; i++) { n         for (j = 0; j < s; j++) { n           b[i][j] = a[r]; n           t--; n           if (t == 0) { n             r++; n             t = vis[a[r]] / 4; n           } n         } n       } n       for (i = 0; i < s; i++) { n         cnt = s - 1; n         for (j = s; j < n; j++) { n           b[i][j] = b[i][cnt]; n           cnt--; n         } n       } n       for (i = 0; i < s; i++) { n         for (j = 0; j < n; j++) { n           if (j != 0) cout <<    ; n           cout << b[i][j]; n         } n         cout << endl; n       } n       for (i = s - 1; i >= 0; i--) { n         for (j = 0; j < n; j++) { n           if (j != 0) cout <<    ; n           cout << b[i][j]; n         } n         cout << endl; n       } n     } n   } else { n     cnt = 0; n     if (n == 1) { n       cout <<  YES  << endl << a[0] << endl; n       return 0; n     } n     for (i = 0; i < k; i++) { n       if (vis[a[i]] % 2 == 1) { n         p = a[i]; n         cnt++; n         vis[p]--; n       } n     } n     if (cnt != 1) { n       cout <<  NO  << endl; n     } else { n       s = n / 2; n       r = 0; n       while (vis[a[r]] < 4) { n         r++; n         if (r >= k) { n           break; n         } n       } n       if (r >= k) { n         cout <<  NO  << endl; n         return 0; n       } n       t = vis[a[r]] / 4; n       for (i = 0; i < s; i++) { n         for (j = 0; j < s; j++) { n           if (r >= k) { n             flag = 1; n             break; n           } n           b[i][j] = a[r]; n           t--; n           vis[a[r]] -= 4; n           if (t == 0) { n             r++; n             while (vis[a[r]] < 4) { n               r++; n               if (r >= k) break; n             } n             t = vis[a[r]] / 4; n           } n         } n       } n       if (flag == 1) { n         cout <<  NO  << endl; n         return 0; n       } n       for (i = 0; i < s; i++) { n         cnt = s - 1; n         for (j = s + 1; j < n; j++) { n           b[i][j] = b[i][cnt--]; n         } n       } n       r = 0; n       while (vis[a[r]] == 0) { n         r++; n       } n       t = vis[a[r]] / 2; n       for (i = 0; i < s; i++) { n         b[i][s] = a[r]; n         vis[a[r]] -= 2; n         t--; n         if (t == 0) { n           while (vis[a[r]] == 0) { n             r++; n           } n           t = vis[a[r]] / 2; n         } n       } n       cout <<  YES  << endl; n       for (i = 0; i < s; i++) { n         for (j = 0; j < n; j++) { n           if (j != 0) cout <<    ; n           cout << b[i][j]; n         } n         cout << endl; n       } n       b[s][s] = p; n       r = 0; n       while (vis[a[r]] == 0) { n         r++; n       } n       t = vis[a[r]] / 2; n       for (i = 0; i < s; i++) { n         if (i != 0) cout <<    ; n         b[s][i] = a[r]; n         cout << a[r]; n         vis[a[r]] -= 2; n         t--; n         if (t == 0) { n           while (vis[a[r]] == 0) { n             r++; n             if (r >= k) break; n           } n           t = vis[a[r]] / 2; n         } n       } n       cout <<     << p; n       for (i = s - 1; i >= 0; i--) { n         cout <<     << b[s][i]; n       } n       cout << endl; n       for (i = s - 1; i >= 0; i--) { n         for (j = 0; j < n; j++) { n           if (j != 0) cout <<    ; n           cout << b[i][j]; n         } n         cout << endl; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() n { n     long long tests, a, b, c, i,sum; n     cin >> tests; n     while (tests--) n     { n         cin >> a >> b >> c; n         if (c / b + 1 >= a) n             printf( %lld n ,(a*a-a)/2); n         else n             printf( %lld n ,(c/b)*(c/b+1)/2+(a-c/b-1)*(c/b)); n     } n }


#include <bits/stdc++.h> n using namespace std; n const int Maxn = 100020; n char s[Maxn]; n int a[Maxn]; n int k, n; n bool check(int m) { n   for (int i = 1; i <= n; i++) { n     if (s[i] ==  1 ) continue; n     if (a[i - 1] - a[max(0, i - m - 1)] + a[min(n, i + m)] - a[i] >= k) n       return 1; n   } n   return 0; n } n int main() { n   scanf( %d%d , &n, &k); n   scanf( %s , s + 1); n   for (int i = 1; i <= n; i++) a[i] = a[i - 1] + (s[i] ==  0 ); n   int l = 1, r = n; n   while (l <= r) { n     int mid = (l + r) >> 1; n     if (check(mid)) n       r = mid - 1; n     else n       l = mid + 1; n   } n   printf( %d n , l); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string x, y, z; n   cin >> x; n   y += tolower(x[0]); n   z += toupper(x[0]); n   for (int i = 1; i < x.size(); i++) { n     y += toupper(x[i]); n     z += toupper(x[i]); n   } n   if (x == y || x == z || x.size() == 1) { n     for (int i = 0; i < x.size(); i++) { n       if (x[i] == toupper(x[i])) n         x[i] = tolower(x[i]); n       else n         x[i] = toupper(x[i]); n     } n   } n   cout << x << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, a, x, b, y; n   cin >> n >> a >> x >> b >> y; n   int flag = 0; n   while (flag == 0) { n     if (a == b) { n       flag = 1; n       break; n     } n     if (a == x) { n       break; n     } n     if (b == y) { n       break; n     } n     a++; n     b--; n     if (a == n + 1) { n       a = 1; n     } n     if (b == 0) { n       b = n; n     } n   } n   if (flag == 1) { n     cout <<  YES  n          <<   n ; n   } else if (flag == 0) { n     cout <<  NO  n          <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optmize(2) n using namespace std; n inline int read() { n   int X = 0, w = 0; n   char ch = 0; n   while (!isdigit(ch)) w |= ch ==  - , ch = getchar(); n   while (isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar(); n   return w ? -X : X; n } n int f[1000010]; n vector<int> G[1000010]; n int vis[1000010]; n int tot, s, cnt; n int dfs(int i) { n   int res = 1; n   vis[i] = 1; n   for (int j = 0; j < G[i].size(); j++) { n     if (!vis[G[i][j]]) res += dfs(G[i][j]); n   } n   return res; n } n multiset<int> ss; n int n = read(), m = read(), k = read(); n int main() { n   for (int i = 1; i <= m; i++) { n     int x = read(), y = read(); n     G[x].push_back(y), G[y].push_back(x); n   } n   for (int i = 1; i <= n; i++) n     if (!vis[i]) f[++tot] = dfs(i); n   sort(f + 1, f + tot + 1); n   reverse(f + 1, f + tot + 1); n   int total = 0; n   for (int i = 1; i <= tot; i++) { n     f[i] = min(f[i], k); n     total += f[i]; n     ss.insert(f[i]); n   } n   for (int i = 0;; i++) { n     if (total >= 2 * ((int)ss.size() - 1)) { n       cout << i << endl; n       return 0; n     } n     int a = *ss.begin(); n     ss.erase(ss.begin()); n     int b = *ss.begin(); n     ss.erase(ss.begin()); n     total = total - a - b + min(a + b, k); n     ss.insert(min(a + b, k)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int m = 10; n int a[10], b[10], ap[10], bp[10]; n int n; n int main() { n   memset(a, 0, sizeof(a)); n   memset(b, 0, sizeof(b)); n   string s; n   cin >> s; n   n = int(s.size()); n   for (int i = 0; i < n; ++i) { n     int num = s[i] -  0 ; n     ++ap[num]; n     ++bp[num]; n   } n   int ans = 0; n   int ln = -1; n   for (int i = 0; i < m; ++i) { n     for (int j = 0; j < m; ++j) { n       a[j] = ap[j]; n       b[j] = bp[j]; n     } n     int p = 0; n     int pans = 0; n     if (i == 0 && a[0] > 0 && b[0] > 0) { n       --a[0]; n       --b[0]; n       ++pans; n     } else { n       if (i != 0 && a[i] > 0 && b[m - i] > 0) { n         --a[i]; n         --b[m - i]; n         p = 1; n         ++pans; n       } else n         continue; n     } n     for (int j = 0; j < n; ++j) { n       for (int k = 0; k < m; ++k) { n         if (k == 0 && p == 0 && a[0] > 0 && b[0] > 0) { n           --a[0]; n           --b[0]; n           ++pans; n           break; n         } n         if (a[k] > 0 && b[m - p - k] > 0) { n           --a[k]; n           --b[m - p - k]; n           ++pans; n           p = 1; n           break; n         } n       } n     } n     pans += min(a[0], b[0]); n     if (pans > ans) { n       ans = pans; n       ln = i; n     } n   } n   for (int j = 0; j < m; ++j) { n     a[j] = ap[j]; n     b[j] = bp[j]; n   } n   string sa, sb; n   { n     int p = 0; n     int i = ln; n     if (i == 0 && a[0] > 0 && b[0] > 0) { n       --a[0]; n       --b[0]; n       sa +=  0 ; n       sb +=  0 ; n     } else { n       if (i != 0 && a[i] > 0 && b[m - i] > 0) { n         --a[i]; n         --b[m - i]; n         p = 1; n         sa += char(i +  0 ); n         sb += char(m - i +  0 ); n       } else { n         cout << s << endl << s << endl; n         return 0; n       } n     } n     for (int j = 0; j < n; ++j) { n       for (int k = 0; k < m; ++k) { n         if (k == 0 && p == 0 && a[0] > 0 && b[0] > 0) { n           --a[0]; n           --b[0]; n           sa +=  0 ; n           sb +=  0 ; n           break; n         } n         if (a[k] > 0 && b[m - p - k] > 0) { n           --a[k]; n           --b[m - p - k]; n           sa += char(k +  0 ); n           sb += char(m - p - k +  0 ); n           p = 1; n           break; n         } n       } n     } n   } n   int T = min(a[0], b[0]); n   a[0] -= T; n   b[0] -= T; n   for (int i = 0; i < m; ++i) { n     while (a[i] > 0) { n       --a[i]; n       sa += i +  0 ; n     } n   } n   for (int i = 0; i < m; ++i) { n     while (b[i] > 0) { n       --b[i]; n       sb += i +  0 ; n     } n   } n   reverse(sa.begin(), sa.end()); n   reverse(sb.begin(), sb.end()); n   cout << sa; n   for (int i = 0; i < T; ++i) printf( 0 ); n   cout << endl; n   cout << sb; n   for (int i = 0; i < T; ++i) printf( 0 ); n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   int n, x, y; n   cin >> n >> x >> y; n   char c; n   long long s = 0; n   bool b = 0; n   for (int i = 0; i < n; i++) { n     cin >> c; n     if (b == 1 && c ==  1 ) { n       b = 0; n       s++; n     } else if (b == 0 && c ==  0 ) { n       b = 1; n     } n   } n   if (c ==  0 ) s++; n   if (s > 0 && x < y) { n     cout << (s - 1) * x + y; n   } else n     cout << s * y; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n; n   cin >> n; n   string s; n   cin >> s; n   vector<long long int> ind; n   long long int t = 0; n   ind.push_back(0); n   for (long long int i = 1; i < n; i++) { n     if (s[i] != s[ind[t]]) { n       t++; n       ind.push_back(i); n     } n   } n   long long int f = 0; n   long long int last = 0; n   for (long long int i = 1; i <= t; i++) { n     if (ind[i] - ind[i - 1] - 1 >= 2) { n       f = 1; n       break; n     } n     if (ind[i] - ind[i - 1] - 1 >= 1) { n       last++; n     } n   } n   if (n - ind[t] - 1 >= 2) f = 1; n   if (n - ind[t] - 1 >= 1) last++; n   if (last >= 2) f = 1; n   if (f == 1) { n     cout << ind.size() + 2 <<   n ; n   } else if (last >= 1) { n     cout << ind.size() + 1 <<   n ; n   } else { n     cout << ind.size() <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int d[100][110]; n int grad[110]; n int dp[1 << 16]; n int main() { n   int n, m, a, b, c, sum(0); n   ios_base ::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> m; n   for (int i(0); i < n; i++) n     for (int j(0); j < n; j++) n       if (i != j) d[i][j] = 1e9; n   while (m--) { n     cin >> a >> b >> c; n     a--; n     b--; n     sum += c; n     grad[a]++, grad[b]++; n     d[a][b] = d[b][a] = min(d[a][b], c); n   } n   for (int k(0); k < n; k++) n     for (int i(0); i < n; i++) n       for (int j(0); j < n; j++) d[i][j] = min(d[i][j], d[i][k] + d[k][j]); n   for (int i(0); i < n; i++) { n     if (grad[i] && d[0][i] >= 1e9) { n       cout << -1; n       return 0; n     } n   } n   int mask(0); n   for (int i(0); i < n; i++) n     if (grad[i] & 1) mask |= (1 << i); n   for (int i(0); i <= mask; i++) dp[i] = 1e9; n   dp[mask] = 0; n   while (mask) { n     for (int i(0); i < n; i++) { n       if (!(mask & (1 << i))) continue; n       for (int j(i + 1); j < n; j++) n         if (mask & (1 << j)) n           dp[mask ^ (1 << i) ^ (1 << j)] = n               min(dp[mask ^ (1 << i) ^ (1 << j)], dp[mask] + d[i][j]); n     } n     mask--; n   } n   cout << dp[0] + sum <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int arr[10] = {0}; n   char S[100001]; n   cin >> S; n   for (int i = 0; S[i]; i++) { n     int j = 0; n     if (S[i] ==  L ) { n       while (arr[j] == 1) j++; n       arr[j] = 1; n     } n     j = 10 - 1; n     if (S[i] ==  R ) { n       while (arr[j] == 1) j--; n       arr[j] = 1; n     } n     if (S[i] >= 48 && S[i] <= 57) { n       arr[S[i] - 48] = 0; n     } n   } n   for (int i = 0; i < 10; i++) cout << arr[i]; n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n string s; n int n; n long long ans; n vector<int> ls, rr; n long long cal(int l, int r) { n   long long val = 0; n   long long tmp = s[l] -  0 ; n   for (int i = l + 1; i < r; i += 2) { n     if (s[i] ==  * ) n       tmp *= s[i + 1] -  0 ; n     else { n       val += tmp; n       tmp = s[i + 1] -  0 ; n     } n   } n   val += tmp; n   long long res = 0; n   tmp = s[0] -  0 ; n   if (l == 0) tmp = val; n   for (int i = (l == 0) ? r : 1; i < n; i += 2) { n     if (i + 1 == l) { n       tmp *= val; n       i = r - 2; n       continue; n     } n     if (s[i] ==  * ) n       tmp *= (long long)(s[i + 1] -  0 ); n     else { n       res += tmp; n       tmp = (long long)(s[i + 1] -  0 ); n     } n   } n   res += tmp; n   return res; n } n int main() { n   cin >> s; n   n = s.length(); n   ls.push_back(0); n   rr.push_back(n); n   for (int i = 1; i < n; i += 2) { n     if (s[i] ==  * ) { n       ls.push_back(i + 1); n       rr.push_back(i); n     } n   } n   for (int l = 0; l < ls.size(); l++) n     for (int r = 0; r < rr.size(); r++) { n       if (ls[l] >= rr[r]) continue; n       ans = max(ans, cal(ls[l], rr[r])); n     } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int read() { n   int x = 0, f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   return x * f; n } n int n, l[200100], r[200100], L[200100], R[200100]; n int main() { n   n = read(); n   for (int i = 1; i <= n; i++) { n     l[i] = read(); n     r[i] = read(); n     r[i] += l[i]; n   } n   L[0] = R[n + 1] = 1e9 + 10; n   for (int i = 1; i <= n; i++) L[i] = min(r[i], L[i - 1] + 1); n   for (int i = n; i >= 1; i--) R[i] = min(r[i], R[i + 1] + 1); n   long long ans = 0, f = 0; n   for (int i = 1; i <= n; i++) { n     if (min(L[i], R[i]) < l[i]) f = 1; n     ans += min(L[i], R[i]) - l[i]; n   } n   if (f) { n     puts( -1 ); n     return 0; n   } n   printf( %I64d n , ans); n   for (int i = 1; i <= n; i++) printf( %d  , min(L[i], R[i])); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[100005], c[100005]; n vector<pair<long long, long long>> p[100005]; n long long check(long long x, long long i) { n   long long v = 0; n   for (auto j : p[i]) { n     long long v1 = j.first, v2 = j.second; n     if (v1 != -1) v += abs(v1 - x); n     if (v2 != -1) v += abs(v2 - x); n   } n   return v; n } n int main() { n   long long n, m, sum = 0; n   cin >> n >> m; n   for (long long i = 1; i <= m; i++) { n     scanf( %lld , &a[i]); n   } n   if (m == 1) { n     cout << 0 << endl; n     return 0; n   } n   a[0] = a[1], a[m + 1] = a[m]; n   for (long long i = 1; i <= m; i++) { n     long long x = a[i - 1], y = a[i + 1]; n     if (x == a[i]) x = -1; n     if (y == a[i]) y = -1; n     p[a[i]].push_back({min(x, y), max(x, y)}); n   } n   for (long long i = 2; i <= m; i++) { n     sum += abs(a[i] - a[i - 1]); n   } n   for (long long i = 1; i <= n; i++) { n     c[i] = check(i, i); n   } n   long long res = 2e18; n   for (long long i = 1; i <= n; i++) { n     if (p[i].size() == 0) continue; n     long long l = 1, r = n, ans = 2e18; n     while (r >= l) { n       long long midl = l + (r - l) / 3, numl = check(midl, i); n       long long midr = r - (r - l) / 3, numr = check(midr, i); n       if (numl <= numr) n         r = midr - 1, ans = min(ans, numl); n       else n         l = midl + 1, ans = min(ans, numr); n     } n     res = min(res, sum - c[i] + ans); n   } n   cout << res << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mxn = 1e5 + 7; n int main() { n   long long n, mx = 0, a[mxn]; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     int x; n     cin >> x; n     a[x]++; n   } n   for (int i = 1; i <= 101; i++) { n     mx = max(mx, a[i]); n   } n   cout << mx; n } n


#include <bits/stdc++.h> n using namespace std; n int na, ma, nb, mb; n char s[55][55]; n char t[55][55]; n int main() { n   while (~scanf( %d%d , &na, &ma)) { n     for (int i = 0; i < na; i++) scanf( %s , s[i]); n     scanf( %d%d , &nb, &mb); n     for (int i = 0; i < nb; i++) scanf( %s , t[i]); n     int ans = 0, xx = 0, yy = 0; n     int n = max(na, nb), m = max(ma, mb); n     for (int x = -n; x <= n; x++) n       for (int y = -m; y <= m; y++) { n         int tmp = 0; n         for (int i = 0; i < na; i++) n           for (int j = 0; j < ma; j++) n             if (i + x < nb && j + y < mb && i + x >= 0 && j + y >= 0) n               tmp += (s[i][j] ==  1 ) & (t[i + x][j + y] ==  1 ); n         if (ans < tmp) ans = tmp, xx = x, yy = y; n       } n     printf( %d %d n , xx, yy); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; ++i) putchar( 1 ); n   puts(  ); n   for (int i = 1; i < n; ++i) putchar( 8 ); n   puts( 9 ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int l[10100]; n int r[10100]; n int per[100100]; n int pr(int x) { n   if (x == per[x]) return x; n   return per[x] = pr(per[x]); n } n int main() { n   ios::sync_with_stdio(0); n   int n; n   cin >> n; n   int t = 0; n   for (int i = 1; i <= n; i++) { n     per[i] = i; n     cin >> l[i]; n     cin >> r[i]; n     t += l[i] == 0; n   } n   for (int i = 1; i <= n; i++) { n     if (l[i]) { n       r[l[i]] = i; n       per[pr(i)] = pr(l[i]); n     } n     if (r[i]) { n       l[r[i]] = i; n       per[pr(i)] = pr(r[i]); n     } n   } n   for (int i = 1; i <= n; i++) { n     if (l[i] == 0) { n       if (t == 1) continue; n       for (int j = 1; j <= n; j++) { n         if (i == j) continue; n         if (pr(i) == pr(j)) continue; n         if (r[j] == 0) { n           r[j] = i; n           l[i] = j; n           per[pr(i)] = pr(j); n           break; n         } n       } n       t--; n     } n   } n   for (int i = 1; i <= n; i++) { n     cout << l[i] <<     << r[i] <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n const int maxn = 100001; n int n, a[maxn]; n struct Segment { n   int val, tag; n } seg[maxn << 1 | 1]; n inline int seg_idx(int L, int R) { return (L + R) | (L < R); } n void seg_down(int rt, int lch, int rch) { n   if (!seg[rt].tag) return; n   seg[lch].val += seg[rt].tag; n   seg[lch].tag += seg[rt].tag; n   seg[rch].val += seg[rt].tag; n   seg[rch].tag += seg[rt].tag; n   seg[rt].tag = 0; n } n void seg_up(int rt, int lch, int rch) { n   seg[rt].val = std::max(seg[lch].val, seg[rch].val); n } n void seg_upd(int L, int R, int x, int v) { n   int rt = seg_idx(L, R); n   if (R <= x) { n     seg[rt].val += v; n     seg[rt].tag += v; n     return; n   } n   int M = (L + R) >> 1, lch = seg_idx(L, M), rch = seg_idx(M + 1, R); n   seg_down(rt, lch, rch); n   seg_upd(L, M, x, v); n   if (x > M) seg_upd(M + 1, R, x, v); n   seg_up(rt, lch, rch); n } n int seg_que(int L, int R) { n   int rt = seg_idx(L, R); n   if (L == R) return L; n   int M = (L + R) >> 1, lch = seg_idx(L, M), rch = seg_idx(M + 1, R), ret; n   seg_down(rt, lch, rch); n   ret = seg[rch].val <= 0 ? seg_que(L, M) : seg_que(M + 1, R); n   seg_up(rt, lch, rch); n   return ret; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) { n     int pos, typ; n     scanf( %d%d , &pos, &typ); n     if (typ == 1) { n       scanf( %d , a + pos); n       seg_upd(1, n, pos, 1); n     } else n       seg_upd(1, n, pos, -1); n     if (seg[seg_idx(1, n)].val <= 0) n       puts( -1 ); n     else n       printf( %d n , a[seg_que(1, n)]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n bool judge(string& s, string& t) { n   auto f = [](string& x, vector<int>& cnt) { n     ll res = 0; n     for (char c : x) { n       int v = c -  a ; n       for (int i = v + 1; i < 26; i++) res += cnt[i]; n       cnt[v]++; n     } n     return res; n   }; n   vector<int> vecS(26); n   vector<int> vecT(26); n   int nums = f(s, vecS), numt = f(t, vecT); n   return (nums % 2 == numt % 2 || n           *max_element(vecS.begin(), vecS.end()) >= 2) && n          vecS == vecT; n } n int main() { n   int q; n   cin >> q; n   while (q--) { n     int n; n     cin >> n; n     string s, t; n     cin >> s >> t; n     cout << (judge(s, t) ?  YES  :  NO ) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int i, j, n, m, k; n int main() { n   scanf( %d , &n); n   if (n <= 2) { n     printf( %d n , -1); n   } else { n     printf( %d n , 35); n     printf( %d n , 77); n     for (i = 1; i <= n - 2; i++) printf( %d n , i * 55); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-9; n long long read() { n   long long x = 0; n   long long f = 1; n   char c = getchar(); n   while (c <  0  || c >  9 ) { n     if (c ==  - ) f = -f; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     x = x * 10 + c -  0 ; n     c = getchar(); n   } n   x *= f; n   return x; n } n long long n, m; n struct matrix { n   long long s[315][315]; n   void inti() { n     for (int i = 1; i <= n; i++) n       for (int j = 1; j <= n; j++) s[i][j] = -0x3f3f3f3f; n     return; n   } n   matrix operator*(matrix b) const { n     matrix f; n     f.inti(); n     for (int k = 1; k <= n; k++) n       for (int i = 1; i <= n; i++) n         for (int j = 1; j <= n; j++) n           f.s[i][j] = max(f.s[i][j], s[i][k] + b.s[k][j]); n     return f; n   } n } t[10], now, p; n int main() { n   n = read(), m = read(); n   t[0].inti(); n   for (int i = 1; i <= n; i++) t[0].s[i][i] = 0; n   for (int i = 1; i <= m; i++) { n     long long u = read(), v = read(); n     t[0].s[u][v] = read(); n     t[0].s[v][u] = read(); n   } n   for (int i = 1; i < 10; i++) t[i] = t[i - 1] * t[i - 1]; n   now.inti(), p.inti(); n   for (int i = 1; i <= n; i++) now.s[i][i] = 0; n   long long ans = 0; n   for (int i = 10 - 1; ~i; i--) { n     p = now * t[i]; n     bool flag = 0; n     for (int j = 1; j <= n; j++) n       if (p.s[j][j] > 0) { n         flag = 1; n         break; n       } n     if (flag) continue; n     ans += (1 << i), now = p; n   } n   if (ans > n) n     puts( 0 ); n   else n     printf( %lld , ans + 1); n } n


#include <bits/stdc++.h> n int S, T, n, m, l, vol[200020]; n int dis[400040], pos[400040], q[400040 * 2]; n int E, u[200020], v[200020], go[400040], to[400040]; n void AE(int s, int t) { n   to[++E] = t; n   v[s] = u[s] ? go[v[s]] = E : u[s] = E; n } n int spfa(int maxq) { n   register int i, j, k, top, tail; n   memset(dis, 0x7f, sizeof(dis)); n   memset(pos, 0, sizeof(pos)); n   q[1] = S; n   dis[S] = top = 0; n   pos[S] = tail = 1; n   while (++top <= tail) { n     i = q[top]; n     if (dis[i] < maxq) n       for (j = u[i]; j; j = go[j]) { n         int tmp = vol[to[j]] ? 0 : dis[i] + 1; n         if (dis[to[j]] > tmp) { n           dis[to[j]] = tmp; n           if (!pos[to[j]]) { n             q[++tail] = to[j]; n             pos[to[j]] = 1; n           } n         } n       } n     pos[i] = 0; n   } n   return dis[T] < 0x7f7f7f7f; n } n int main() { n   register int i, j, k; n   scanf( %d%d%d , &n, &m, &l); n   for (i = 1; i <= l; i++) { n     scanf( %d , &k); n     vol[k] = 1; n   } n   for (i = 1; i <= m; i++) { n     scanf( %d%d , &j, &k); n     AE(j, k); n     AE(k, j); n   } n   scanf( %d%d , &S, &T); n   int l = 1, r = n, m, ans = -1; n   while (l <= r) { n     m = l + r + 1 >> 1; n     if (spfa(m)) { n       ans = m; n       r = m - 1; n     } else n       l = m + 1; n   } n   printf( %d n , ans < 0 ? -1 : ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int counts = 0, counts2 = 0; n   for (int i = 0; i < n; ++i) { n     int a, b; n     cin >> a >> b; n     if (a < 0) n       counts++; n     else n       counts2++; n   } n   if (counts <= 1 || counts2 <= 1) n     cout <<  Yes ; n   else n     cout <<  No ; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 205, inf = 1 << 27; n int g[maxn][maxn], g1[maxn][maxn]; n int qq[maxn], h, t, n, dis[maxn]; n char col[maxn]; n int aa[maxn], dd[maxn], x[maxn], bb[maxn], l; n int find_min() { n   int i, ind, ind1, ans; n   ind = 1; n   for (i = 2; i <= n; i++) { n     if (aa[i] > aa[ind] || aa[i] == aa[ind] && x[i] > x[ind]) { n       ind = i; n     } n   } n   ans = aa[ind]; n   while (x[ind] < l) { n     ind1 = -1; n     for (i = 1; i <= n; i++) { n       if (x[i] + dd[i] > x[ind] + dd[ind] && n           (ind1 == -1 || aa[i] > aa[ind1] || n            aa[i] == aa[ind1] && dd[i] > dd[ind1])) { n         ind1 = i; n       } n     } n     if (ind1 == -1) break; n     ans = min(ans, (x[ind] + dd[ind] + aa[ind1]) / 2); n     ind = ind1; n   } n   return ans; n } n int main() { n   int m, i, j, k, a, b, w; n   int cur, ans; n   scanf( %d %d , &n, &m); n   for (i = 0; i < m; i++) { n     scanf( %d %d %d , &a, &b, &w); n     g[a][b] = g[b][a] = 2 * w; n   } n   for (i = 1; i <= n; i++) { n     for (j = i + 1; j <= n; j++) { n       g1[i][j] = g1[j][i] = (g[i][j] ? g[i][j] : inf); n     } n   } n   for (k = 1; k <= n; k++) { n     for (i = 1; i <= n; i++) { n       for (j = 1; j <= n; j++) { n         g1[i][j] = min(g1[i][j], g1[i][k] + g1[k][j]); n       } n     } n   } n   ans = inf; n   for (i = 1; i <= n; i++) { n     cur = 0; n     for (j = 1; j <= n; j++) { n       cur = max(cur, g1[i][j]); n     } n     ans = min(ans, cur); n   } n   for (i = 1; i <= n; i++) { n     for (j = i + 1; j <= n; j++) { n       if (!g[i][j]) continue; n       cur = 0; n       l = g[i][j]; n       for (k = 1; k <= n; k++) { n         aa[k] = g1[i][k]; n         bb[k] = g1[j][k]; n         x[k] = (bb[k] + l - aa[k]) / 2; n         dd[k] = aa[k] + x[k]; n       } n       cur = find_min(); n       ans = min(ans, cur); n     } n   } n   printf( %d.%d , ans / 2, 5 * (ans % 2)); n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_H = 100; n const int MAX_W = 100; n char s[MAX_W + 4]; n int main() { n   int h, w; n   scanf( %d%d , &h, &w); n   for (int y = 0; y < h; y++) scanf( %s , s); n   int c = 0; n   for (int i = 0; i < w;) { n     while (i < w && s[i] ==  . ) i++; n     if (i >= w) break; n     while (i < w && s[i] ==  B ) i++; n     c++; n   } n   printf( %d n , c); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 10000; n int n; n set<pair<int, int> > a; n set<pair<int, int> >::iterator it; n pair<int, int> b[MAX]; n void dive(int s, int e) { n   if (e - s == 1) return; n   int mid = s + e; n   mid /= 2; n   dive(s, mid); n   dive(mid, e); n   for (int i = s; i < e; i++) a.insert(make_pair(b[mid].first, b[i].second)); n } n int main() { n   cin >> n; n   int x, y; n   for (int i = 0; i < n; i++) { n     cin >> b[i].first >> b[i].second; n     a.insert(b[i]); n   } n   sort(b, b + n); n   dive(0, n); n   cout << a.size() << endl; n   for (it = a.begin(); it != a.end(); it++) n     cout << it->first <<     << it->second << endl; n   return 0; n } n


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