#include <bits/stdc++.h> n using namespace std; n inline void pisz(int n) { printf( %d n , n); } n template <typename T, typename TT> n ostream &operator<<(ostream &s, pair<T, TT> t) { n   return s <<  (  << t.first <<  ,  << t.second <<  ) ; n } n template <typename T> n ostream &operator<<(ostream &s, vector<T> t) { n   for (int(i) = 0; (i) < (((int)(t.size()))); ++(i)) s << t[i] <<    ; n   return s; n } n template <typename T, typename Aug> n struct AugTreap { n   struct Node { n     T v; n     int pr; n     Node *le, *ri; n     Aug al, ag; n   }; n   Node *root; n   AugTreap() : root(0) {} n   inline static void recalc(Node *&n) { n     Aug left = n->le ? n->le->ag + n->al : n->al; n     n->ag = n->ri ? left + n->ri->ag : left; n   } n   inline void rotle(Node *&n) { n     Node *syn = n->le; n     n->le = syn->ri; n     syn->ri = n; n     n = syn; n     recalc(n->ri); n     recalc(n); n   } n   inline void rotri(Node *&n) { n     Node *syn = n->ri; n     n->ri = syn->le; n     syn->le = n; n     n = syn; n     recalc(n->le); n     recalc(n); n   } n   void insert_aux(Node *&n, const T &x, const Aug &aug) { n     if (!n) { n       n = new Node; n       n->v = x; n       n->pr = rand(); n       n->le = n->ri = 0; n       n->al = n->ag = aug; n     } else if (x < n->v) { n       insert_aux(n->le, x, aug); n       if (n->pr > n->le->pr) n         rotle(n); n       else n         recalc(n); n     } else if (x > n->v) { n       insert_aux(n->ri, x, aug); n       if (n->pr > n->ri->pr) n         rotri(n); n       else n         recalc(n); n     } else { n       n->al = n->al + aug; n       recalc(n); n     } n   } n   void insert(const T &x, const Aug &aug) { insert_aux(root, x, aug); } n   Aug sumgeq_aux(const T &x, Node *n) { n     if (!n) return Aug(); n     if (x > n->v) return sumgeq_aux(x, n->ri); n     if (x < n->v) { n       Aug left = sumgeq_aux(x, n->le) + n->al; n       return n->ri ? left + n->ri->ag : left; n     } n     return n->ri ? n->al + n->ri->ag : n->al; n   } n   Aug sumgeq(const T &x) { return sumgeq_aux(x, root); } n }; n long long a[100007], prefsum[100007]; n int main() { n   long long n, k; n   cin >> n >> k; n   for (int(i) = 1; (i) <= (n); ++(i)) cin >> a[i]; n   prefsum[0] = 0; n   for (int(i) = 1; (i) <= (n); ++(i)) prefsum[i] = prefsum[i - 1] + a[i]; n   long long best = -1000000000; n   best *= 100000; n   long long from = best + 1, to = -best; n   while (from <= to) { n     long long x = (from + to) / 2; n     long long dobrychsum = 0, zab = 0; n     AugTreap<long long, int> tr; n     for (int(i) = 1; (i) <= (n); ++(i)) tr.insert(prefsum[i], 1); n     for (int(st) = 1; (st) <= (n); ++(st)) { n       dobrychsum += tr.sumgeq(x + zab); n       zab += a[st]; n       tr.insert(zab, -1); n     } n     if (dobrychsum >= k) { n       best = x; n       from = x + 1; n     } else { n       to = x - 1; n     } n   } n   cout << best; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int T; n   cin >> T; n   while (T--) { n     long long n, k; n     cin >> n >> k; n     if (k % 3) { n       if (n % 3) n         cout <<  Alice n ; n       else n         cout <<  Bob n ; n     } else { n       n %= (k + 1); n       if (n == k || n % 3) n         cout <<  Alice n ; n       else n         cout <<  Bob n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int fu[2003]; n int jar[2003]; n int main() { n   int n, refresh; n   cin >> n >> refresh; n   for (int(i) = 0; (i) < (n); ++(i)) { n     cin >> jar[i]; n   } n   for (int(i) = 0; (i) < (n); ++(i)) { n     cin >> fu[i]; n   } n   int best = fu[0]; n   int res = 0; n   int gas = 0; n   for (int i = 0; i < n; ++i) { n     gas += fu[i]; n     best = max((best), (fu[i])); n     while (gas < jar[i]) { n       gas += best; n       res += refresh; n     } n     gas -= jar[i]; n     res += jar[i]; n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long cas; n long long a; n long long b; n long long c; n long long d[] = {0, 1, -1}; n int main() { n   scanf( %lld , &cas); n   while (cas--) { n     scanf( %lld %lld %lld , &a, &b, &c); n     long long ans = 21413524368453; n     for (long long i = 0; i < 3; ++i) { n       long long aa = a + i; n       for (long long j = 0; j < 3; ++j) { n         long long bb = b + j; n         for (long long k = 0; k < 3; ++k) { n           long long cc = c + k; n           ans = min(ans, abs(aa - bb) + abs(aa - cc) + abs(bb - cc)); n         } n       } n     } n     printf( %lld n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename TYPE> n void readint(TYPE &x) { n   x = 0; n   int f = 1; n   char c; n   for (c = getchar(); !isdigit(c); c = getchar()) n     if (c ==  - ) f = -1; n   for (; isdigit(c); c = getchar()) x = x * 10 + c -  0 ; n   x *= f; n } n void err(istream_iterator<string> it) {} n template <typename T, typename... Args> n void err(istream_iterator<string> it, T a, Args... args) { n   cerr << *it <<   =   << a << endl; n   err(++it, args...); n } n const long long MOD = 998244353LL, MAX = 3e5 + 5; n const int INF = 2e9; n int n; n long long ans; n long long a[MAX]; n long long fc[MAX]; n long long pot(long long x, long long y) { n   if (!y) return 1LL; n   if (y & 1) n     return (x * pot(x, y - 1)) % MOD; n   else { n     long long k = pot(x, y / 2); n     return (k * k) % MOD; n   } n } n long long inv(long long t) { return pot(t, MOD - 2); } n void read(int w[], int n) { n   for (long long i = 0; i < n; i++) { n     cin >> w[i]; n   } n } n void solve() { n   cin >> n; n   fc[0] = 1LL; n   for (long long i = 0; i < 2 * n; i++) cin >> a[i]; n   if (n == 1) { n     cout << (a[1] - a[0]) * 2; n     return; n   } n   sort(a, a + 2 * n); n   for (long long i = 1; i < n * 2 + 1; i++) fc[i] = (fc[i - 1] * i) % MOD; n   for (long long i = 0; i < n; i++) ans += (a[2 * n - 1 - i] - a[i]); n   ans %= MOD; n   { n     string _s =  ans ; n     replace(_s.begin(), _s.end(),  , ,    ); n     stringstream _ss(_s); n     istream_iterator<string> _it(_ss); n     err(_it, ans); n   }; n   ans = (ans * (fc[n * 2])) % MOD; n   ans *= inv((fc[n] * fc[n]) % MOD); n   ans %= MOD; n   ans %= MOD; n   cout << ans; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<long long int, long long int> mymap, mymap1; n long long int cum[200010], a[200010]; n int main() { n   long long int n, i, j, k; n   scanf( %lld , &n); n   ; n   scanf( %lld , &k); n   ; n   cum[0] = 0; n   mymap[0]++; n   for (i = 1; i <= n; i++) { n     scanf( %lld , &a[i]); n     ; n     cum[i] = cum[i - 1] ^ a[i]; n     long long int temp1 = cum[i], temp2 = ((1 << k) - 1) ^ temp1; n     if (mymap[temp1] == 0) n       mymap[temp2]++; n     else n       mymap[temp1]++; n   } n   long long int ans = (n * (n + 1)) / 2; n   map<long long int, long long int>::iterator it; n   long long int subt = 0; n   for (it = mymap.begin(); it != mymap.end(); it++) { n     long long int val = it->first, freq = it->second; n     long long int temp = freq / 2; n     subt += ((temp * (temp - 1)) / 2); n     temp = freq - temp; n     subt += ((temp * (temp - 1)) / 2); n   } n   cout << ans - subt << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n void pv(T a, T b) { n   for (T i = a; i != b; ++i) cerr << *i <<    ; n   cout << endl; n } n int ans[100000 + 10]; n int main() { n   for (int N; ~scanf( %d , &N);) { n     memset(ans, 0, sizeof(ans)); n     vector<int> a(N), b(N); n     for (int i = 0; i < (int)(N); ++i) scanf( %d , &a[i]); n     for (int i = 0; i < (int)(N); ++i) scanf( %d , &b[i]); n     vector<long long> c(N); n     for (int i = 0; i < (int)(N); ++i) c[i] = a[i] - b[i]; n     for (int i = 1; i < N; i++) c[i] += c[i - 1]; n     int small = *min_element((c).begin(), (c).end()); n     for (int i = 0; i < (int)(N); ++i) n       if (c[i] == small) ans[(i + 1) % N] = 1; n     for (int i = 0; i < (int)(N); ++i) c[i] = a[i] - b[(i - 1 + N) % N]; n     for (int i = N - 2; i >= 0; i--) c[i] += c[i + 1]; n     small = *min_element((c).begin(), (c).end()); n     for (int i = 0; i < (int)(N); ++i) n       if (c[i] == small) ans[(i - 1 + N) % N] = 1; n     cout << count(ans, ans + N, 1) << endl; n     for (int i = 0; i < (int)(N); ++i) n       if (ans[i]) cout << i + 1 <<    ; n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int val[60]; n int n; n int grid[2][60]; n int mask1[25][1200008 + 1], mask2[25][1200008 + 1]; n void solve(int l, int r, int mask[][1200008 + 1]) { n   memset(mask, -1, sizeof mask); n   for (int i = 0; i <= 24; i++) n     for (int j = 0; j < 1200008 + 1; j++) mask[i][j] = -1; n   for (int i = 0; i < (1 << r - l); i++) { n     int s = 0; n     for (int j = 0; j < r - l; j++) n       if ((1 << j) & i) s += val[j + l]; n     mask[__builtin_popcount(i)][s] = i; n   } n } n signed main() { n   scanf( %d , &n); n   n = n * 2; n   for (int i = 0; i < n; i++) scanf( %d , &val[i]); n   sort(val, val + n); n   int sum = 0; n   int base = val[0] + val[1]; n   grid[0][0] = val[0], grid[1][n / 2 - 1] = val[1]; n   n -= 2; n   for (int i = 0; i < n; i++) val[i] = val[i + 2], sum += val[i]; n   solve(0, n / 2, mask1); n   solve(n / 2, n, mask2); n   int ans = 1e9; n   long long ansmask = 0; n   for (int a = 0; a <= n / 2; a++) { n     int b = n / 2 - a; n     int j = 1200008; n     while (j >= 0 and mask2[b][j] == -1) j--; n     for (int i = 0; j >= 0 and i <= 1200008; i++) n       if (mask1[a][i] > -1) { n         while (j >= 0 and (j + i) * 2 > sum) j--; n         while (j >= 0 and mask2[b][j] == -1) j--; n         if (j < 0) break; n         if (sum - (i + j) * 2 < ans) { n           ans = sum - (i + j) * 2; n           ansmask = (1ll * mask2[b][j]) << (n / 2); n           ansmask += mask1[a][i]; n         } n       } n   } n   ans = (sum + ans) / 2; n   vector<int> v1, v2; n   for (int i = 0; i < n; i++) n     if ((1ll << i) & ansmask) n       v1.push_back(val[i]); n     else n       v2.push_back(val[i]); n   sort(v1.begin(), v1.end()); n   sort(v2.begin(), v2.end()); n   reverse(v2.begin(), v2.end()); n   n += 2; n   for (int i = 1; i < n / 2; i++) grid[0][i] = v1[i - 1]; n   for (int i = 0; i < n / 2 - 1; i++) grid[1][i] = v2[i]; n   for (int i = 0; i < 2; i++) { n     for (int j = 0; j < n / 2; j++) cout << grid[i][j] <<    ; n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 2e5 + 5; n const long long NN = 1e9 + 5; n char a[150][150]; n map<char, bool> mp, mp1; n bool used[150][150]; n long long n, m; n vector<char> v; n int main() { n   cin >> n >> m; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       cin >> a[i][j]; n     } n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       for (int l = 1; l <= m; l++) { n         if (l != j && a[i][j] == a[i][l]) { n           used[i][l] = true; n         } n       } n       for (int l = 1; l <= n; l++) { n         if (l != i && a[l][j] == a[i][j]) { n           used[i][j] = true; n         } n       } n     } n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       if (!used[i][j]) { n         v.push_back(a[i][j]); n       } n     } n   } n   for (int i = 0; i < (int)v.size(); i++) { n     cout << v[i]; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long t, n, m, k, x, y, z, a[500005], ans, val; n int main() { n   ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   cin >> n; n   string s; n   cin >> s; n   for (long long i = 0; i < n; i++) { n     if (s[i] ==  ( ) n       x++; n     else n       x--; n     if (x == -1 && !z) n       x = 0, z = 1; n     else if (x == -1) n       break; n   } n   if (x == z) n     cout <<  YES ; n   else n     cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double pi = acos(-1.0); n int pal(string s) { n   int i = 0, j = s.length() - 1; n   while (i < j) { n     if (s[i] != s[j]) return 0; n     i++; n     j--; n   } n   return 1; n } n int main() { n   ios_base::sync_with_stdio(false); n   string str, tp, s; n   cin >> str; n   int l = str.length(); n   for (int i = 0; i <= l; i++) { n     s = str.substr(0, i); n     for (int j = 0; j < 26; j++) { n       char ch =  a  + j; n       tp = s + ch + str.substr(i); n       if (pal(tp)) { n         cout << tp << endl; n         return 0; n       } n     } n   } n   cout <<  NA  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N_MAX = 51000; n int X[N_MAX], Y[N_MAX], Z[N_MAX]; n bool vis[N_MAX]; n bool ff(int i, int j) { n   if (X[i] != X[j]) return X[i] < X[j]; n   if (Y[i] != Y[j]) return Y[i] < Y[j]; n   return Z[i] < Z[j]; n } n int main() { n   int n; n   cin >> n; n   vector<int> P; n   for (int i = 1; i <= n; i++) { n     cin >> X[i] >> Y[i] >> Z[i]; n     P.push_back(i); n   } n   sort(P.begin(), P.end(), ff); n   for (int i = 0; i < n - 1; i++) { n     if (vis[P[i]]) continue; n     if (X[P[i]] == X[P[i + 1]] and Y[P[i]] == Y[P[i + 1]]) { n       vis[P[i]] = 1; n       vis[P[i + 1]] = 1; n       cout << P[i] <<     << P[i + 1] << endl; n     } n   } n   for (int i = 0; i < n - 1; i++) { n     if (vis[P[i]]) continue; n     if (X[P[i]] != X[P[i + 1]]) continue; n     int j = i + 1; n     while (vis[P[j]] and j < n) { n       j++; n     } n     if (j == n or X[P[i]] != X[P[j]]) continue; n     vis[P[i]] = 1; n     vis[P[j]] = 1; n     cout << P[i] <<     << P[j] << endl; n   } n   for (int i = 0; i < n - 1; i++) { n     if (vis[P[i]]) continue; n     int j = i + 1; n     while (vis[P[j]] and j < n) { n       j++; n     } n     vis[P[i]] = 1; n     vis[P[j]] = 1; n     cout << P[i] <<     << P[j] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n; n   scanf( %d , &n); n   int B1, B2; n   int count1 = 0; n   int count2 = 0; n   while (n--) { n     scanf( %d %d , &B1, &B2); n     if (B1 > B2) { n       count1++; n     } n     if (B2 > B1) { n       count2++; n     } n   } n   if (count1 > count2) { n     printf( Mishka ); n   } n   if (count2 > count1) { n     printf( Chris ); n   } n   if (count2 == count1) { n     printf( Friendship is magic!^^ ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 10; n int n, m, s, t, ds, dt, cnt, f; n vector<pair<int, int>> ans; n vector<int> G[N]; n pair<int, int> xx[N], yy[N]; n bool vi[N]; n void dfs(int u) { n   vi[u] = true; n   for (int v : G[u]) n     if (!vi[v]) { n       ans.push_back({u, v}); n       dfs(v); n     } else if (v == s) n       xx[cnt] = {u, v}; n     else if (v == t) n       yy[cnt] = {u, v}; n } n int main() { n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     int x, y; n     cin >> x >> y; n     G[x].push_back(y); n     G[y].push_back(x); n   } n   cin >> s >> t >> ds >> dt; n   vi[s] = true; n   vi[t] = true; n   for (int i = 1; i <= n; i++) n     if (!vi[i]) { n       dfs(i); n       cnt++; n     } n   for (int i = 0; i < cnt; i++) n     if (xx[i] == make_pair(0, 0) && yy[i] == make_pair(0, 0)) { n       cout <<  No  << endl; n       return 0; n     } else if (xx[i] == make_pair(0, 0)) { n       dt--; n       ans.push_back(yy[i]); n       f |= 2; n     } else if (yy[i] == make_pair(0, 0)) { n       ds--; n       ans.push_back(xx[i]); n       f |= 1; n     } n   if (ds <= 0 || dt <= 0) { n     cout <<  No  << endl; n     return 0; n   } n   for (int i = 0; i < cnt; i++) n     if (xx[i] > make_pair(0, 0) && yy[i] > make_pair(0, 0)) { n       ds--; n       dt--; n       ans.push_back(xx[i]); n       ans.push_back(yy[i]); n       f = 3; n       yy[i] = {0, 0}; n       xx[i] = {0, 0}; n       break; n     } n   for (int i = 0; i < cnt; i++) n     if (xx[i] > make_pair(0, 0) && yy[i] > make_pair(0, 0) && ds > 0) { n       ds--; n       ans.push_back(xx[i]); n       f |= 1; n       yy[i] = {0, 0}; n     } n   for (int i = 0; i < cnt; i++) n     if (xx[i] > make_pair(0, 0) && yy[i] > make_pair(0, 0) && dt > 0) { n       dt--; n       ans.push_back(yy[i]); n       f |= 2; n       xx[i] = {0, 0}; n     } n   for (int i = 0; i < cnt; i++) n     if (xx[i] > make_pair(0, 0) && yy[i] > make_pair(0, 0)) { n       cout <<  No  << endl; n       return 0; n     } n   if (ans.size() < n - 1) { n     ans.push_back({s, t}); n     ds--; n     dt--; n   } n   if (ds < 0 || dt < 0) n     cout <<  No  << endl; n   else { n     cout <<  Yes  << endl; n     for (auto z : ans) cout << z.first <<     << z.second << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 2e5 + 30, Mod = 1e9 + 7; n const long long SQ = 330; n int main() { n   ios::sync_with_stdio(0), cin.tie(0); n   long long a, b; n   cin >> a >> b; n   long long ans = 0; n   while (a > 0 && b > 0) { n     if (a <= b) n       a++, b -= 2; n     else n       b++, a -= 2; n     if (a < 0 || b < 0) ans--; n     ans++; n   } n   cout << ans; n   return (0); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout <<  2001 n ; n   for (int i = 1; i <= 1000; i++) { n     cout << i <<   1   << i <<   2 n ; n   } n   cout <<  1 1 1 2 n ; n   for (int i = 1; i <= 1000; i++) { n     cout << i <<   1   << i <<   2 n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int Max = 1e6 + 100; n int pa[Max << 1], son[Max << 1][27]; n int deep[Max << 1], sum[Max << 1], tp[Max << 1], S[Max << 1], cnt, root, last; n char str[Max << 1]; n inline int Newnode(int _deep) { n   deep[++cnt] = _deep; n   return cnt; n } n inline void SAM(int alp) { n   int np = Newnode(deep[last] + 1); n   int u = last; n   S[np] = 1; n   memset(son[np], 0, sizeof(son[np])); n   while (u && !son[u][alp]) son[u][alp] = np, u = pa[u]; n   if (!u) n     pa[np] = root; n   else { n     int v = son[u][alp]; n     if (deep[v] == deep[u] + 1) n       pa[np] = v; n     else { n       int nv = Newnode(deep[u] + 1); n       memcpy(son[nv], son[v], sizeof(son[v])); n       pa[nv] = pa[v], pa[v] = pa[np] = nv; n       while (u && son[u][alp] == v) son[u][alp] = nv, u = pa[u]; n     } n   } n   last = np; n } n inline void toposort() { n   for (int i = 1; i <= deep[last]; i++) sum[i] = 0; n   for (int i = 1; i <= cnt; i++) sum[deep[i]]++; n   for (int i = 1; i <= deep[last]; i++) sum[i] += sum[i - 1]; n   for (int i = 1; i <= cnt; i++) tp[sum[deep[i]]--] = i; n   for (int i = cnt; i >= 1; i--) S[pa[tp[i]]] += S[tp[i]]; n } n inline void pre() { root = last = Newnode(0); } n int vis[Max], Q; n void solve(char *pat, int x) { n   int len = strlen(pat); n   int ans = 0, l = 0, now = root; n   for (int a = 0; a < len; a++) { n     int c = pat[a] -  a ; n     if (son[now][c]) { n       now = son[now][c]; n       l++; n     } else { n       while (now && !son[now][c]) now = pa[now]; n       if (!now) n         now = root, l = 0; n       else n         l = deep[now] + 1, now = son[now][c]; n     } n     if (a >= x && l >= x) { n       int tmp = now; n       while (!(deep[pa[tmp]] + 1 <= x && deep[tmp] >= x)) tmp = pa[tmp]; n       if (vis[tmp] != Q + 1) ans += S[tmp], vis[tmp] = Q + 1; n     } n   } n   printf( %d n , ans); n } n int main() { n   scanf( %s , str); n   pre(); n   int len = strlen(str); n   for (int a = 0; a < len; a++) SAM(str[a] -  a ); n   toposort(); n   scanf( %d , &Q); n   while (Q--) { n     scanf( %s , str); n     len = strlen(str); n     for (int b = 0; b < len; b++) str[b + len] = str[b]; n     str[len + len] = 0; n     solve(str, len); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int kN = 2e5 + 10; n const long long kInf = 1e18 + 10; n int n, q; n long long a[kN], b[kN]; n vector<int> g[kN]; n int in[kN], out[kN], dfs_clock; n pair<long long, long long> p1[kN], p2[kN]; n void dfs(int u) { n   in[u] = ++dfs_clock; n   for (int v : g[u]) { n     a[v] += a[u], b[v] += b[u]; n     dfs(v); n   } n   out[u] = dfs_clock; n } n struct SegmentTree { n   pair<long long, long long> pos[kN << 2]; n   struct Node { n     long long a, b; n     long long wait, tg; n   }; n   Node seg[kN << 2]; n   void init(pair<long long, long long> *a) { n     for (int i = 1; i <= n; i++) pos[i] = a[i]; n   } n   void push_down(int o) { n     if (seg[o].tg == 0) return; n     seg[(o << 1)].tg += seg[o].tg, seg[(o << 1 | 1)].tg += seg[o].tg; n     seg[(o << 1)].wait -= seg[o].tg, seg[(o << 1 | 1)].wait -= seg[o].tg; n     seg[(o << 1)].a += seg[o].tg, seg[(o << 1 | 1)].a += seg[o].tg; n     seg[o].tg = 0; n   } n   void push_up(int o) { n     long long a1 = seg[(o << 1)].a, b1 = seg[(o << 1)].b; n     long long a2 = seg[(o << 1 | 1)].a, b2 = seg[(o << 1 | 1)].b; n     if (a1 * b1 > a2 * b2) n       seg[o].a = a1, seg[o].b = b1; n     else n       seg[o].a = a2, seg[o].b = b2; n     seg[o].wait = min(seg[(o << 1)].wait, seg[(o << 1 | 1)].wait); n     if (b1 != b2) { n       long long tmp = (a2 * b2 - a1 * b1) / (b1 - b2); n       if (tmp >= 0) seg[o].wait = min(seg[o].wait, tmp); n     } n   } n   void build(int l = 1, int r = n, int o = 1) { n     if (l == r) { n       seg[o] = {pos[l].first, pos[l].second, kInf, 0}; n       return; n     } n     int m = (l + r) >> 1; n     build(l, m, (o << 1)), build(m + 1, r, (o << 1 | 1)); n     push_up(o); n   } n   void update(int ql, int qr, int x, int l = 1, int r = n, int o = 1) { n     if (ql > r || qr < l) return; n     if (ql <= l && r <= qr && seg[o].wait >= x) { n       seg[o].tg += x, seg[o].wait -= x, seg[o].a += x; n       return; n     } n     push_down(o); n     int m = (l + r) >> 1; n     if (ql <= m) update(ql, qr, x, l, m, (o << 1)); n     if (qr > m) update(ql, qr, x, m + 1, r, (o << 1 | 1)); n     push_up(o); n   } n   long long query(int ql, int qr, int l = 1, int r = n, int o = 1) { n     if (ql > r || qr < l) return -kInf; n     if (ql <= l && r <= qr) return seg[o].a * seg[o].b; n     push_down(o); n     int m = (l + r) >> 1; n     return max(query(ql, qr, l, m, (o << 1)), n                query(ql, qr, m + 1, r, (o << 1 | 1))); n   } n }; n SegmentTree t1, t2; n int main() { n   scanf( %d%d , &n, &q); n   for (int i = 2, x; i <= n; i++) { n     scanf( %d , &x); n     g[x].push_back(i); n   } n   for (int i = 1; i <= n; i++) scanf( %lld , a + i); n   for (int i = 1; i <= n; i++) scanf( %lld , b + i); n   dfs(1); n   for (int i = 1; i <= n; i++) { n     p1[in[i]] = {a[i], b[i]}, p2[in[i]] = {a[i], -b[i]}; n   } n   t1.init(p1), t2.init(p2); n   t1.build(), t2.build(); n   while (q--) { n     int op, v; n     scanf( %d%d , &op, &v); n     if (op == 1) { n       int x; n       scanf( %d , &x); n       t1.update(in[v], out[v], x); n       t2.update(in[v], out[v], x); n     } else { n       printf( %lld n , max(t1.query(in[v], out[v]), t2.query(in[v], out[v]))); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n unsigned long long pow_of_two[10000]; n int create() { n   unsigned long long i = 0; n   unsigned long long num = 1; n   while (num <= INT_MAX and num >= 0) { n     num = pow(2, i); n     pow_of_two[i] = num; n     i++; n   } n } n int main() { n   int n; n   cin >> n; n   string str; n   cin >> str; n   int len = str.length(); n   int swapi = 0; n   for (int i = len - 1; i >= 0; i--) { n     if (str[i] ==  1 ) { n       for (int j = i - 1; j >= 0; j--) { n         if (str[j] ==  1 ) { n           int left = j; n           int right = i; n           while (left + 1 < right) { n             swap(str[right], str[right - 1]); n             right--; n             swapi++; n           } n           if (str[right] ==  1  && str[right - 1] ==  1 ) { n             str[right] =  4 ; n           } n           break; n         } n       } n     } n   } n   for (int i = 0; i < str.length(); i++) { n     if (str[i] !=  4 ) cout << str[i]; n   } n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   string s; n   cin >> n >> s; n   sort(s.begin(), s.end()); n   cout << s; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string a =   , b =   ; n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < 2220; i++) { n     if (i % 2 == 0) { n       a +=  5 ; n       b +=  4 ; n     } else { n       a +=  4 ; n       b +=  5 ; n     } n   } n   a +=  5 ; n   b +=  5 ; n   cout << a << endl; n   cout << b << endl; n } n


#include <bits/stdc++.h> n using namespace std; n ifstream Cin( input.txt ); n ofstream Cout( output.txt ); n int main() { n   long long n, x, i, num = 0, m[100001], m1[100001]; n   m[0] = m1[0] = 0; n   cin >> n; n   for (i = 1; i <= n; i++) { n     cin >> m[i]; n     m1[i] = m[i]; n   } n   sort(m, m + n + 1); n   for (i = 1; i <= n; i++) { n     if (m[i] != m1[i]) { n       num++; n     } n   } n   cout << (num <= 2 ?  YES  :  NO ); n } n


#include <bits/stdc++.h> n using namespace std; n long long int power(long long int x, unsigned long long int y) { n   long long int res = 1; n   while (y > 0) { n     if (y & 1) { n       res = res * x; n     } n     y = y >> 1; n     x = x * x; n   } n   return res; n } n long long int powermod(long long int x, unsigned long long int y, n                        long long int p) { n   long long int res = 1; n   x = x % p; n   while (y > 0) { n     if (y & 1) { n       res = (res * x) % p; n     } n     y = y >> 1; n     x = (x * x) % p; n   } n   return res; n } n int32_t main() { n   long long int n, m; n   cin >> n >> m; n   vector<long long int> v, suf(m + 1); n   for (long long int i = (long long int)(0); i < (long long int)(m); i++) { n     long long int t; n     cin >> t; n     v.push_back(t); n   } n   long long int ok = 1; n   for (long long int i = (long long int)(m - 1); i >= (long long int)(0); i--) { n     suf[i] = suf[i + 1] + v[i]; n     if (v[i] + i > n) ok = 0; n   } n   if (!ok or suf[0] < n) { n     cout << -1 <<   n ; n     return 0; n   } n   for (long long int i = (long long int)(0); i < (long long int)(m); i++) { n     cout << max(i + 1, n - suf[i] + 1) <<    ; n   } n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100005 * 2; n struct SegmentTree { n   int l, r; n   int sum, lmx, rmn, ansl, ansr, ans, Ans; n } t[N << 2]; n int s[N]; n char ss[N]; n void merge(int p) { n   t[p].sum = t[p * 2].sum + t[p * 2 + 1].sum; n   t[p].lmx = max(t[p * 2].lmx, t[p * 2].sum + t[p * 2 + 1].lmx); n   t[p].rmn = min(t[p * 2 + 1].rmn, t[p * 2 + 1].sum + t[p * 2].rmn); n   t[p].ansl = max(max(t[p * 2].ansl, t[p * 2 + 1].ansl - t[p * 2].sum), n                   t[p * 2].ans + t[p * 2 + 1].lmx); n   t[p].ansr = max(max(t[p * 2 + 1].ansr, t[p * 2].ansr + t[p * 2 + 1].sum), n                   t[p * 2 + 1].ans - t[p * 2].rmn); n   t[p].ans = n       max(t[p * 2].ans + t[p * 2 + 1].sum, t[p * 2 + 1].ans - t[p * 2].sum); n   t[p].Ans = max(max(max(t[p * 2].Ans, t[p * 2 + 1].Ans), n                      t[p * 2 + 1].ansl - t[p * 2].rmn), n                  t[p * 2].ansr + t[p * 2 + 1].lmx); n } n void build(int p, int l, int r) { n   t[p].l = l, t[p].r = r; n   if (l == r) { n     t[p].sum = s[l]; n     t[p].lmx = max(s[l], 0); n     t[p].rmn = min(s[l], 0); n     t[p].ansl = t[p].ansr = t[p].ans = t[p].Ans = 1; n     return; n   } n   int mid = (l + r) / 2; n   build(p * 2, l, mid); n   build(p * 2 + 1, mid + 1, r); n   merge(p); n } n void change(int p, int x, int d) { n   if (t[p].l == t[p].r) { n     t[p].sum = d; n     t[p].lmx = max(d, 0); n     t[p].rmn = min(d, 0); n     t[p].ansl = t[p].ansr = t[p].ans = t[p].Ans = 1; n     return; n   } n   int mid = (t[p].l + t[p].r) / 2; n   if (x <= mid) n     change(p * 2, x, d); n   else n     change(p * 2 + 1, x, d); n   merge(p); n } n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   scanf( %s , ss + 1); n   for (int i = 1; i <= 2 * (n - 1); i++) s[i] = (ss[i] ==  (  ? 1 : -1); n   build(1, 1, 2 * (n - 1)); n   printf( %d n , t[1].Ans); n   for (int i = 1; i <= m; i++) { n     int l, r; n     scanf( %d%d , &l, &r); n     if (s[l] == s[r]) { n       printf( %d n , t[1].Ans); n       continue; n     } n     change(1, l, s[r]); n     change(1, r, s[l]); n     swap(s[l], s[r]); n     printf( %d n , t[1].Ans); n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long a[300005]; n long long s[300005]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(NULL); n   int n, p, k; n   cin >> n >> p >> k; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     a[i] %= p; n   } n   map<long long, long long> cnt; n   for (int i = 1; i <= n; i++) { n     s[i] = a[i] * a[i] % p; n     s[i] = s[i] * a[i] % p; n     s[i] -= k; n     s[i] = (s[i] % p + p) % p; n     s[i] = s[i] * a[i] % p; n     cnt[s[i]]++; n   } n   long long res = 0; n   for (auto i : cnt) { n     long long val = i.second * (i.second - 1) / 2; n     res += val; n   } n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   string s; n   cin >> s; n   vector<string> splitted; n   stringstream ss(s); n   string item; n   while (getline(ss, item,  , )) { n     splitted.push_back(item); n   } n   int MAX = 1001; n   vector<bool> a(MAX + 1); n   for (const string& page : splitted) { n     a[stoi(page)] = true; n   } n   vector<pair<int, int>> ret; n   int p = 0; n   while (p <= MAX) { n     if (a[p]) { n       int from = p; n       int to = from; n       while (a[to]) { n         ++to; n       } n       ret.emplace_back(from, to - 1); n       p = to; n     } else { n       ++p; n     } n   } n   for (int i = 0; i < ret.size(); ++i) { n     int from = ret[i].first; n     int to = ret[i].second; n     cout << from; n     if (to != from) { n       cout <<  -  << to; n     } n     if (i != ret.size() - 1) { n       cout <<  , ; n     } n   } n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using ul = std::uint32_t; n using li = std::int32_t; n using ll = std::int64_t; n using ull = std::uint64_t; n using vul = std::vector<ul>; n using vvul = std::vector<vul>; n using pulul = std::pair<ul, ul>; n using lf = long double; n ul n, m; n const lf pi = std::acos(lf(-1)); n const lf eps = 1e-7; n class point { n  public: n   lf x = 0; n   lf y = 0; n   lf rho = 0; n   lf theta = 0; n }; n class segment { n  public: n   lf s = 0; n   lf t = 0; n   ul id = 0; n }; n point data[100000]; n segment segs[300000]; n std::vector<segment> segv; n ul f[300000]; n ul g[300000]; n lf inner(const point& a, const point& b) { return a.x * b.x + a.y * b.y; } n lf norm2(const point& a) { return inner(a, a); } n lf mysqrt(lf x) { return std::sqrt(std::max(x, lf(0))); } n lf norm(const point& a) { return mysqrt(norm2(a)); } n lf myasin(lf x) { return std::asin(std::max(lf(-1), std::min(lf(1), x))); } n lf myacos(lf x) { return std::acos(std::max(lf(-1), std::min(lf(1), x))); } n lf theta(const point& a) { n   lf rho = norm(a); n   lf x = a.x; n   lf y = a.y; n   if (x < 0) { n     if (y < 0) { n       return pi + myacos(-x / rho); n     } else { n       return pi - myacos(-x / rho); n     } n   } else { n     if (y < 0) { n       return pi + pi - myacos(x / rho); n     } else { n       return myacos(x / rho); n     } n   } n } n int main() { n   std::scanf( %u%u , &n, &m); n   lf mx = 1.5e5; n   for (ul i = 0; i != n; ++i) { n     double tx, ty; n     std::scanf( %lf%lf , &tx, &ty); n     data[i].x = tx; n     data[i].y = ty; n     data[i].rho = norm(data[i]); n     data[i].theta = theta(data[i]); n     mx = std::min(mx, data[i].rho); n     if (data[i].x == 0 && data[i].y == 0) { n       std::printf( %.20lf n , lf(0)); n       return 0; n     } n   } n   lf mn = 0; n   while (mx - mn > eps) { n     lf mid = (mx + mn) * lf(0.5); n     for (ul i = 0; i != n; ++i) { n       lf temp = myacos(mid / data[i].rho); n       segs[i].s = data[i].theta - temp; n       segs[i].t = data[i].theta + temp; n       segs[i + n].s = segs[i].s + pi + pi; n       segs[i + n].t = segs[i].t + pi + pi; n       segs[i + n + n].s = segs[i + n].s + pi + pi; n       segs[i + n + n].t = segs[i + n].t + pi + pi; n     } n     std::sort(segs, segs + n + n + n, n               [](const segment& a, const segment& b) { return a.t < b.t; }); n     segv.resize(0); n     for (ul i = 0; i != n + n + n; ++i) { n       segs[i].id = i; n       segv.push_back(segs[i]); n     } n     ul key = 4e5; n     ul keyid = 0; n     for (ul i = 0, j = 0; i != segv.size(); ++i) { n       for (; j != segv.size() && segv[j].s <= segv[i].t; ++j) { n         g[j] = i - 1; n         if (j >= n && j < n + n) { n           if (key > j - i) { n             key = j - i; n             keyid = j; n           } n         } n       } n       f[i] = j; n     } n     if (ull(key) * ull(m) >= ull(n)) { n       mn = mid; n       continue; n     } n     bool flag = false; n     for (ul i = keyid; i != g[keyid]; --i) { n       ul j; n       ul cnt; n       for (j = i, cnt = 0; n            cnt != m && j != segv.size() && segv[j].id < segv[i].id + n; n            j = f[j], ++cnt) n         ; n       if (j == segv.size() || segv[j].id >= segv[i].id + n) { n         flag = true; n         break; n       } n     } n     if (flag) { n       mn = mid; n     } else { n       mx = mid; n     } n   } n   std::printf( %.20lf n , double(mn)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   int l, r; n } ar[300000]; n bool compare(node a, node b) { n   if (a.r != b.r) n     return a.r < b.r; n   else n     return a.l < b.l; n } n vector<pair<int, int> > vectl, vectn, temp; n int main() { n   int n, i, m, flag = 0; n   scanf( %d%d , &n, &m); n   for (i = 0; i < m; i++) { n     scanf( %d%d , &ar[i].l, &ar[i].r); n   } n   sort(ar, ar + m, compare); n   int col = -1; n   for (int j = 0; j < m; j++) { n     if (ar[j].r != col) { n       ar[i].l = n + 1; n       ar[i].r = ar[j].r; n       col = ar[j].r; n       i++; n     } n   } n   m = i; n   sort(ar, ar + m, compare); n   int row = ar[0].l, lastrow = 1, lastcol = ar[0].r; n   i = 0; n   col = ar[i].r; n   while (ar[i].r == col) { n     if (ar[i].l > lastrow) vectl.push_back(make_pair(lastrow, ar[i].l - 1)); n     lastrow = ar[i].l + 1; n     i++; n     if (col == 1) { n       while (ar[i].r == 1) i++; n       break; n     } n   } n   col = ar[i].r; n   while (i < m) { n     if (ar[i].r > lastcol + 1) { n       int st = vectl[0].first; n       lastrow = 1; n       while (ar[i].r == col) { n         if (ar[i].l > lastrow && ar[i].l > st) { n           vectn.push_back(make_pair(lastrow, ar[i].l - 1)); n         } n         lastrow = ar[i].l + 1; n         i++; n         if (i >= m) break; n       } n     } else { n       int j = 0; n       lastrow = 1; n       while (ar[i].r == col) { n         while (j < vectl.size() && vectl[j].second < lastrow) j++; n         if (j == vectl.size()) { n           while (i < m && ar[i].r == col) i++; n           break; n         } n         lastrow = max(vectl[j].first, lastrow); n         if (ar[i].l > lastrow) { n           vectn.push_back(make_pair(lastrow, ar[i].l - 1)); n         } n         lastrow = ar[i].l + 1; n         i++; n         if (i >= m) break; n       } n     } n     if (i >= m) { n       vectl = vectn; n       lastcol = ar[i - 1].r; n       if (vectl.size() == 0) { n         flag = 1; n       } n       break; n     } n     lastcol = col; n     col = ar[i].r; n     vectl = vectn; n     vectn.clear(); n     if (vectl.size() == 0) { n       flag = 1; n       break; n     } n   } n   if (flag == 1) n     printf( -1 n ); n   else if (lastcol != n) n     printf( %d n , n + n - 2); n   else if (vectl[vectl.size() - 1].second == n) n     printf( %d n , n + n - 2); n   else n     printf( -1 n ); n } n


#include <bits/stdc++.h> n using namespace std; n int n, v[100002], nr; n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> v[i]; n   } n   sort(v + 1, v + n + 1); n   for (int i = 1; i <= n - 2; i++) { n     if (v[i] + v[i + 1] > v[i + 2]) nr++; n   } n   if (nr == 0) n     cout <<  NO n ; n   else n     cout <<  YES n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N, M, T; n char str[1005]; n int main() { n   int i, j, k; n   scanf( %d , &N); n   scanf( %s , str); n   for (i = 0; i < N; i++) n     if (str[i] ==  H ) M++; n   int Mc = 1005; n   k = 0; n   for (i = 0; i < M; i++) n     if (str[i] !=  H ) k++; n   int st = 0; n   if (k < Mc) Mc = k; n   for (i = M; i < N; i++) { n     if (str[st] !=  H ) k--; n     st++; n     if (str[i] !=  H ) k++; n     if (k < Mc) Mc = k; n   } n   M = 0; n   for (i = 0; i < N; i++) n     if (str[i] ==  T ) M++; n   k = 0; n   for (i = 0; i < M; i++) n     if (str[i] ==  H ) k++; n   st = 0; n   if (k < Mc) Mc = k; n   for (i = M; i < N; i++) { n     if (str[st] ==  H ) k--; n     st++; n     if (str[i] ==  H ) k++; n     if (k < Mc) Mc = k; n   } n   printf( %d n , Mc); n   scanf(   ); n } n


#include <bits/stdc++.h> n using namespace std; n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   long long n; n   cin >> n; n   long long store[100005] = {}; n   for (long long i = 1; i <= n; i++) { n     long long x, y; n     cin >> x >> y; n     y = i - y; n     long long ct = 0; n     for (long long j = 1; j * j <= x; j++) { n       if (x % j == 0) { n         if (store[j] < y) ct++; n         if (j != x / j && store[x / j] < y) ct++; n         store[j] = i; n         store[x / j] = i; n       } n     } n     cout << ct <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n T in() { n   char ch; n   T n = 0; n   bool ng = false; n   while (1) { n     ch = getchar(); n     if (ch ==  - ) { n       ng = true; n       ch = getchar(); n       break; n     } n     if (ch >=  0  && ch <=  9 ) break; n   } n   while (1) { n     n = n * 10 + (ch -  0 ); n     ch = getchar(); n     if (ch <  0  || ch >  9 ) break; n   } n   return (ng ? -n : n); n } n template <typename T> n inline T POW(T B, T P) { n   if (P == 0) return 1; n   if (P & 1) n     return B * POW(B, P - 1); n   else n     return (POW(B, P / 2) * POW(B, P / 2)); n } n template <typename T> n inline T Gcd(T a, T b) { n   if (a < 0) return Gcd(-a, b); n   if (b < 0) return Gcd(a, -b); n   return (b == 0) ? a : Gcd(b, a % b); n } n template <typename T> n inline T Lcm(T a, T b) { n   if (a < 0) return Lcm(-a, b); n   if (b < 0) return Lcm(a, -b); n   return a * (b / Gcd(a, b)); n } n long long Bigmod(long long base, long long power, long long MOD) { n   long long ret = 1; n   while (power) { n     if (power & 1) ret = (ret * base) % MOD; n     base = (base * base) % MOD; n     power >>= 1; n   } n   return ret; n } n bool isVowel(char ch) { n   ch = toupper(ch); n   if (ch ==  A  || ch ==  U  || ch ==  I  || ch ==  O  || ch ==  E ) n     return true; n   return false; n } n template <typename T> n long long int isLeft(T a, T b, T c) { n   return (a.x - b.x) * (b.y - c.y) - (b.x - c.x) * (a.y - b.y); n } n long long ModInverse(long long number, long long MOD) { n   return Bigmod(number, MOD - 2, MOD); n } n bool isConst(char ch) { n   if (isalpha(ch) && !isVowel(ch)) return true; n   return false; n } n int toInt(string s) { n   int sm; n   stringstream second(s); n   second >> sm; n   return sm; n } n long long int a[200007]; n int main() { n   int n; n   n = in<int>(); n   for (int i = 1; i < n + 1; i++) a[i] = in<long long int>(); n   int m; n   m = in<int>(); n   long long int last = 0; n   while (m--) { n     int w, h; n     w = in<int>(), h = in<int>(); n     long long int ans = max(last, a[w]); n     printf( %lld n , ans); n     last = ans + h; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, A; n int level[10], loyal[10]; n double maxP; n void backtrack(int p, int k) { n   if (p == n || k == 0) { n     double x = 0; n     for (int mask = 0; mask < (1 << n); mask++) { n       int cnt = 0, B = 0; n       double prob = 1.0; n       for (int i = 0; i < n; i++) { n         if (mask & (1 << i)) { n           prob *= loyal[i] / 100.0; n           cnt++; n         } else { n           prob *= (100 - loyal[i]) / 100.0; n           B += level[i]; n         } n       } n       if (2 * cnt > n) n         x += prob; n       else n         x += prob * A / (A + B); n     } n     maxP = max(maxP, x); n   } else { n     backtrack(p + 1, k); n     if (loyal[p] < 100) { n       loyal[p] += 10; n       backtrack(p, k - 1); n       loyal[p] -= 10; n     } n   } n } n int main() { n   int k; n   while (cin >> n >> k >> A) { n     for (int i = 0; i < n; i++) cin >> level[i] >> loyal[i]; n     maxP = 0; n     backtrack(0, k); n     printf( %.10lf n , maxP); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T, class U> n void Max(T &a, U b) { n   if (a < b) a = b; n } n template <class T, class U> n void Min(T &a, U b) { n   if (a > b) a = b; n } n template <class T, class U> n void add(T &a, U b) { n   a += b; n   if (a >= 1000000007) a -= 1000000007; n } n template <class T, class U> n int min(T a, U b) { n   return a >= b ? b : a; n } n int ans, first[50010], second[50010], a[50010], b[50010], res[50010], g[10240]; n void out(int i, int j) { n   cout <<  ?   << i <<     << j <<   n ; n   cin >> ans; n   fflush(stdout); n } n int main() { n   int i, j, k, ca = 0, n, T, m = 0, K; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     out(i, i); n     b[i] = ans; n     out(i, 0); n     a[i] = ans; n   } n   ans = 0; n   int M = 4096 * 2; n   for (int i = 0; i < n; i++) { n     first[i] = 0; n     for (int j = 0; j < n; j++) g[j] = -1; n     int ok = 1; n     for (int j = 0; j < n; j++) { n       k = a[i] ^ a[j]; n       if (k >= n) { n         ok = 0; n         break; n       } n       g[k] = j; n     } n     if (!ok) continue; n     for (int j = 0; j < n; j++) { n       if (g[j] == -1) { n         ok = 0; n         break; n       } n       k = g[j]; n       first[k] = j; n     } n     if (!ok) continue; n     for (int j = 0; j < n; j++) second[first[j]] = j; n     for (int j = 0; j < n; j++) { n       if ((first[j] ^ second[j]) != b[j]) { n         ok = 0; n         break; n       } n     } n     if (!ok) continue; n     for (int j = 0; j < n; j++) { n       if ((first[j] ^ second[0]) != a[j]) { n         ok = 0; n         break; n       } n     } n     if (!ok) continue; n     ans++; n     for (int j = 0; j < n; j++) res[j] = first[j]; n   } n   printf( ! n%d n , ans); n   for (int i = 0; i < n; i++) printf( %d  , res[i]); n   puts(  ); n } n


#include <bits/stdc++.h> n using namespace std; n int n, A[5001], B[5001], S[5001], ma, man; n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> A[i]; n   for (int i = 1; i <= n; i++) { n     for (int j = i; j <= n; j++) { n       S[A[j]]++; n       if (S[A[j]] > ma || (S[A[j]] == ma && A[j] < man)) { n         ma = S[A[j]]; n         man = A[j]; n       } n       B[man]++; n     } n     ma = 0; n     for (int j = 1; j <= n; j++) S[j] = 0; n   } n   for (int i = 1; i <= n; i++) cout << B[i] <<    ; 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 tt = 1; tt <= t; tt++) { n     int r[100005], g[100005], b[100005]; n     int nr, ng, nb; n     cin >> nr >> ng >> nb; n     for (int i = 0; i < nr; i++) cin >> r[i]; n     for (int i = 0; i < ng; i++) cin >> g[i]; n     for (int i = 0; i < nb; i++) cin >> b[i]; n     sort(r, r + nr); n     sort(g, g + ng); n     sort(b, b + nb); n     long long int ans = LONG_LONG_MAX; n     for (int i = 0; i < nr; i++) { n       int x = r[i]; n       auto y = upper_bound(g, g + ng, x); n       if (y == g + ng) y--; n       auto y2 = y; n       if (y - g != 0) { n         y2--; n       } n       auto z = upper_bound(b, b + nb, x); n       if (z == b + nb) z--; n       auto z2 = z; n       if (z - b != 0) { n         z2--; n       } n       ans = min(ans, (x - (*y)) * 1LL * (x - *y) + (x - *z) * 1LL * (x - *z) + n                          (*z - *y) * 1LL * (*z - *y)); n       ans = min(ans, (x - (*y2)) * 1LL * (x - *y2) + (x - *z) * 1LL * (x - *z) + n                          (*z - *y2) * 1LL * (*z - *y2)); n       ans = n           min(ans, (x - (*y2)) * 1LL * (x - *y2) + (x - *z2) * 1LL * (x - *z2) + n                        (*z2 - *y2) * 1LL * (*z2 - *y2)); n       ans = min(ans, (x - (*y)) * 1LL * (x - *y) + (x - *z2) * 1LL * (x - *z2) + n                          (*z2 - *y) * 1LL * (*z2 - *y)); n     } n     for (int i = 0; i < ng; i++) { n       int x = g[i]; n       auto y = upper_bound(r, r + nr, x); n       if (y == r + nr) y--; n       auto y2 = y; n       if (y - r != 0) { n         y2--; n       } n       auto z = upper_bound(b, b + nb, x); n       if (z == b + nb) z--; n       auto z2 = z; n       if (z - b != 0) { n         z2--; n       } n       ans = min(ans, (x - (*y)) * 1LL * (x - *y) + (x - *z) * 1LL * (x - *z) + n                          (*z - *y) * 1LL * (*z - *y)); n       ans = min(ans, (x - (*y2)) * 1LL * (x - *y2) + (x - *z) * 1LL * (x - *z) + n                          (*z - *y2) * 1LL * (*z - *y2)); n       ans = n           min(ans, (x - (*y2)) * 1LL * (x - *y2) + (x - *z2) * 1LL * (x - *z2) + n                        (*z2 - *y2) * 1LL * (*z2 - *y2)); n       ans = min(ans, (x - (*y)) * 1LL * (x - *y) + (x - *z2) * 1LL * (x - *z2) + n                          (*z2 - *y) * 1LL * (*z2 - *y)); n     } n     for (int i = 0; i < nb; i++) { n       int x = b[i]; n       auto y = upper_bound(g, g + ng, x); n       if (y == g + ng) y--; n       auto y2 = y; n       if (y - g != 0) { n         y2--; n       } n       auto z = upper_bound(r, r + nr, x); n       if (z == r + nr) z--; n       auto z2 = z; n       if (z - r != 0) { n         z2--; n       } n       ans = min(ans, (x - (*y)) * 1LL * (x - *y) + (x - *z) * 1LL * (x - *z) + n                          (*z - *y) * 1LL * (*z - *y)); n       ans = min(ans, (x - (*y2)) * 1LL * (x - *y2) + (x - *z) * 1LL * (x - *z) + n                          (*z - *y2) * 1LL * (*z - *y2)); n       ans = n           min(ans, (x - (*y2)) * 1LL * (x - *y2) + (x - *z2) * 1LL * (x - *z2) + n                        (*z2 - *y2) * 1LL * (*z2 - *y2)); n       ans = min(ans, (x - (*y)) * 1LL * (x - *y) + (x - *z2) * 1LL * (x - *z2) + n                          (*z2 - *y) * 1LL * (*z2 - *y)); n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f; n const int maxn = 132222; n const long long mod = 1000000007; n char s[maxn]; n int main() { n   int n; n   cin >> n; n   cin >> s + 1; n   int len = strlen(s + 1); n   int l = -1, r = -1; n   for (int i = (1); i < (len + 1); i++) { n     if (s[i] ==  L  && l == -1) l = i; n     if (s[i] ==  R  && r == -1) r = i; n   } n   if (l != -1 && r == -1) { n     int x; n     for (int i = len; i >= 1; i--) { n       if (s[i] ==  L ) { n         x = i; n         break; n       } n     } n     cout << x <<     << l - 1 << endl; n     return 0; n   } n   if (l == -1 && r != -1) { n     int x; n     for (int i = len; i >= 1; i--) { n       if (s[i] ==  R ) { n         x = i; n         break; n       } n     } n     cout << r <<     << x + 1 << endl; n     return 0; n   } n   if (l != -1 && r != -1) { n     cout << r <<     << l - 1 << endl; n     return 0; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MaxN = 5e2 + 10; n const int P = 239017; n const int INF = 1e9; n const int MOD = 1e9 + 7; n int dp[MaxN][MaxN][2], n, m, q; n char s[MaxN][MaxN]; n int get(int xl, int yl, int xr, int yr, int c) { n   if (c == 0 && xl > xr) { n     return 0; n   } n   if (c == 1 && yl > yr) { n     return 0; n   } n   return dp[xr][yr][c] + dp[xl - 1][yl - 1][c] - dp[xr][yl - 1][c] - n          dp[xl - 1][yr][c]; n } n int main() { n   scanf( %d%d n , &n, &m); n   for (int i = 1; i <= n; ++i) { n     gets(s[i] + 1); n   } n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= m; ++j) { n       dp[i][j][0] = dp[i - 1][j][0] + dp[i][j - 1][0] - dp[i - 1][j - 1][0] + n                     (s[i - 1][j] == s[i][j] && s[i][j] ==  . ); n       dp[i][j][1] = dp[i - 1][j][1] + dp[i][j - 1][1] - dp[i - 1][j - 1][1] + n                     (s[i][j - 1] == s[i][j] && s[i][j] ==  . ); n     } n   } n   scanf( %d , &q); n   while (q-- > 0) { n     int xl, yl, xr, yr; n     scanf( %d%d%d%d , &xl, &yl, &xr, &yr); n     printf( %d n , get(xl + 1, yl, xr, yr, 0) + get(xl, yl + 1, xr, yr, 1)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n bool uin(T &a, T b) { n   return a > b ? (a = b, true) : false; n } n template <class T> n bool uax(T &a, T b) { n   return a < b ? (a = b, true) : false; n } n const int maxn = 210000; n vector<int> e[maxn]; n int h[maxn]; n void dfsh(int v, int p) { n   h[v] = 0; n   for (int u : e[v]) { n     if (u == p) continue; n     dfsh(u, v); n     uax(h[v], h[u] + 1); n   } n   sort((e[v]).begin(), (e[v]).end(), [&](int u, int w) { return h[u] > h[w]; }); n } n vector<int> ord; n void dfsord(int v, int p, int l) { n   bool anyc = false; n   for (int u : e[v]) { n     if (u == p) continue; n     if (!anyc) n       dfsord(u, v, l + 1), anyc = true; n     else n       dfsord(u, v, 1); n   } n   int r = !anyc ? l : 0; n   ord.push_back(r); n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.precision(10); n   cout << fixed; n   int n, k; n   cin >> n >> k; n   for (int i = 0; i < (int)(n - 1); ++i) { n     int u, v; n     cin >> u >> v; n     --u; n     --v; n     e[u].push_back(v); n     e[v].push_back(u); n   } n   dfsh(0, -1); n   dfsord(0, -1, 1); n   sort((ord).rbegin(), (ord).rend()); n   long long W = n; n   long long ans = -1LL * (n / 2) * (n - n / 2); n   for (int i = 0; i < (int)(k); ++i) { n     W -= ord[i]; n     long long b = n / 2, r = i + 1; n     long long res = 1e18; n     if (b <= W) uin(res, (n - r - b) * (r - b)); n     if (b + 1 <= W) uin(res, (n - r - b - 1) * (r - b - 1)); n     uin(res, (n - r - W) * (r - W)); n     uax(ans, res); n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 510; n const int MOD = 1e9 + 7; n long long dp[2][maxn][maxn]; n int N, M; n char s[maxn][maxn]; n void add(long long &x, long long y) { n   x += y; n   if (x >= MOD) x -= MOD; n } n int main() { n   scanf( %d%d , &N, &M); n   for (int i = 1; i <= N; i++) scanf( %s , s[i] + 1); n   dp[0][1][N] = (s[1][1] == s[N][M]); n   int cur = 0; n   for (int step = 1; step <= (M + N - 2) / 2; step++) { n     cur ^= 1; n     for (int i = 0; i <= N; i++) n       for (int j = 1; j <= N; j++) dp[cur][i][j] = 0; n     for (int x1 = 1; x1 <= N && x1 - 1 <= step; x1++) { n       for (int x2 = N; x2 >= 1 && N - x2 <= step; x2--) { n         int y1 = 1 + step - (x1 - 1); n         int y2 = M - (step - (N - x2)); n         if (s[x1][y1] != s[x2][y2]) continue; n         add(dp[cur][x1][x2], dp[cur ^ 1][x1][x2]); n         add(dp[cur][x1][x2], dp[cur ^ 1][x1][x2 + 1]); n         add(dp[cur][x1][x2], dp[cur ^ 1][x1 - 1][x2]); n         add(dp[cur][x1][x2], dp[cur ^ 1][x1 - 1][x2 + 1]); n       } n     } n   } n   long long ans = 0; n   for (int i = 1; i <= N; i++) { n     add(ans, dp[cur][i][i]); n   } n   if ((N + M) % 2) { n     for (int i = 1; i < N; i++) add(ans, dp[cur][i][i + 1]); n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> v1[3010], v2[3010]; n int dista[3010][3010], distb[3010][3010]; n pair<int, int> besta[3010][3], bestb[3010][3]; n int n, m; n void dijkstra(int x) { n   for (int i = 1; i <= n; i++) dista[x][i] = 1000000009; n   dista[x][x] = 0; n   priority_queue<pair<int, int> > q; n   q.push(make_pair(0, x)); n   while (!q.empty()) { n     pair<int, int> t = q.top(); n     q.pop(); n     int u = t.second; n     for (int i = 0; i < v1[u].size(); i++) { n       if (dista[x][v1[u][i]] > dista[x][u] + 1) { n         dista[x][v1[u][i]] = dista[x][u] + 1; n         q.push(make_pair(-dista[x][v1[u][i]], v1[u][i])); n       } n     } n   } n   for (int i = 1; i <= n; i++) { n     if (dista[x][i] != 1000000009 && dista[x][i] > besta[x][0].first) n       besta[x][2] = besta[x][1], besta[x][1] = besta[x][0], n       besta[x][0] = make_pair(dista[x][i], i); n     else if (dista[x][i] != 1000000009 && dista[x][i] > besta[x][1].first) n       besta[x][2] = besta[x][1], besta[x][1] = make_pair(dista[x][i], i); n     else if (dista[x][i] != 1000000009 && dista[x][i] > besta[x][2].first) n       besta[x][2] = make_pair(dista[x][i], i); n   } n } n void dijkstra2(int x) { n   for (int i = 1; i <= n; i++) distb[x][i] = 1000000009; n   distb[x][x] = 0; n   priority_queue<pair<int, int> > q; n   q.push(make_pair(0, x)); n   while (!q.empty()) { n     pair<int, int> t = q.top(); n     q.pop(); n     int u = t.second; n     for (int i = 0; i < v2[u].size(); i++) { n       if (distb[x][v2[u][i]] > distb[x][u] + 1) { n         distb[x][v2[u][i]] = distb[x][u] + 1; n         q.push(make_pair(-distb[x][v2[u][i]], v2[u][i])); n       } n     } n   } n   for (int i = 1; i <= n; i++) { n     if (distb[x][i] != 1000000009 && distb[x][i] > bestb[x][0].first) n       bestb[x][2] = bestb[x][1], bestb[x][1] = bestb[x][0], n       bestb[x][0] = make_pair(distb[x][i], i); n     else if (distb[x][i] != 1000000009 && distb[x][i] > bestb[x][1].first) n       bestb[x][2] = bestb[x][1], bestb[x][1] = make_pair(distb[x][i], i); n     else if (distb[x][i] != 1000000009 && distb[x][i] > bestb[x][2].first) n       bestb[x][2] = make_pair(distb[x][i], i); n   } n } n int main() { n   cin >> n >> m; n   for (int i = 1; i <= m; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     v1[x].push_back(y); n     v2[y].push_back(x); n   } n   int ans = -1, ansa, ansb, ansc, ansd; n   for (int i = 1; i <= n; i++) dijkstra(i); n   for (int i = 1; i <= n; i++) dijkstra2(i); n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       for (int a = 0; a < 3; a++) { n         for (int d = 0; d < 3; d++) { n           int aa = bestb[i][a].second, dd = besta[j][d].second; n           if (aa == 0 || dd == 0) continue; n           if (i == j || i == aa || i == dd || j == aa || j == dd || aa == dd) n             continue; n           if (bestb[i][a].first == 1000000009 || n               besta[j][d].first == 1000000009 || dista[i][j] == 1000000009) n             continue; n           int val = bestb[i][a].first + besta[j][d].first + dista[i][j]; n           if (val > ans) { n             ans = val; n             ansa = aa; n             ansb = i; n             ansc = j; n             ansd = dd; n           } n         } n       } n     } n   } n   cout << ansa <<     << ansb <<     << ansc <<     << ansd << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 10, mod = 1e9 + 7; n const long long infl = LLONG_MAX / 2; n int n, ans, h[N]; n vector<int> g[N], L; n void dfs(int u = 0, int p = -1) { n   bool leaf = 1; n   for (int x : g[u]) n     if (x - p) { n       h[x] = h[u] + 1; n       dfs(x, u); n       leaf = 0; n     } n   if (leaf) L.push_back(h[u]); n   if (h[u] == 1) { n     sort(L.begin(), L.end()); n     int now = 0; n     for (int i = 0; i < L.size(); ++i) { n       now = max(now + 1, L[i]); n     } n     ans = max(ans, now - 1); n     L.clear(); n   } n } n int main() { n   cin >> n; n   for (int i = 0; i < n - 1; ++i) { n     int u, v; n     scanf( %d %d , &u, &v); n     --u; n     --v; n     g[u].push_back(v); n     g[v].push_back(u); n   } n   dfs(); n   cout << ans + 1; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string x, y, z; n   int a = 0, c = 0; n   cin >> x; n   cin >> y; n   cin >> z; n   for (int i = 0; i < x.length(); i++) { n     for (int j = 0; j < z.length(); j++) { n       if (x[i] == z[j]) { n         c += 1; n         z[j] =  1 ; n         break; n       } n     } n   } n   for (int i = 0; i < y.length(); i++) { n     for (int j = 0; j < z.length(); j++) { n       if (y[i] == z[j]) { n         a += 1; n         z[j] =  1 ; n         break; n       } n     } n   } n   if (c + a == z.length() && a == y.length() && c == x.length()) n     cout <<  YES ; n   else n     cout <<  NO ; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma GCC optimize( Ofast,no-stack-protector,unroll-loops,fast-math ) n long long comb(long long n, long long r) { n   if (r < 0 || r > n) return 0; n   if ((n - r) < r) r = n - r; n   long long vec = 1; n   for (long long i = n; i > n - r; --i) { n     vec = vec * i; n   } n   for (long long i = 1; i < r + 1; ++i) { n     vec = vec / i; n   } n   return vec; n } n template <typename T> n void press(T &v) { n   v.erase(unique(v.begin(), v.end()), v.end()); n } n long long small(vector<long long> &vec, long long first) { n   return lower_bound(vec.begin(), vec.end(), first) - vec.begin(); n } n long long osmall(vector<long long> &vec, long long first) { n   return upper_bound(vec.begin(), vec.end(), first) - vec.begin(); n } n long long big(vector<long long> &vec, long long first) { n   return vec.size() - osmall(vec, first); n } n long long obig(vector<long long> &vec, long long first) { n   return vec.size() - small(vec, first); n } n long long freq(vector<long long> &vec, long long first) { n   return upper_bound(vec.begin(), vec.end(), first) - n          lower_bound(vec.begin(), vec.end(), first); n } n long long maxa(vector<long long> &vec) { n   return max_element(vec.begin(), vec.end()) - vec.begin(); n } n long long mina(vector<long long> &vec) { n   return min_element(vec.begin(), vec.end()) - vec.begin(); n } n long long Set(long long qq, long long pos) { return (qq | (1 << pos)); } n long long chk(long long qq, long long pos) { return (qq & (1 << pos)); } n long long bits(long long qq) { return __builtin_popcount(qq); } n long long p2[32]; n long long modpow(long long first, long long n, long long m) { n   if (n == 0) return 1 % m; n   long long u = modpow(first, n / 2, m); n   u = (u * u) % m; n   if (n % 2 == 1) u = (u * first) % m; n   return u; n } n long long _pow(long long vec, long long b) { n   if (!b) return 1; n   long long temp = _pow(vec, b / 2); n   temp = (temp * temp); n   if (b % 2) return (vec * temp); n   return temp; n } n long long cl(long long vec, long long b) { n   if (vec % b) return 1 + vec / b; n   return vec / b; n } n long long dx[] = {0, 0, -1, 1}; n long long dy[] = {1, -1, 0, 0}; n const long long MOD = 1e9 + 7; n const long long inf = 1e18; n const long long N = 1e4 + 5; n const long long K = 1e3 + 10; n void solve() { n   vector<long long> v; n   long long n, ans = 0; n   cin >> n; n   if (n == 1) { n     cin >> n; n     cout << 0 << endl; n     return; n   } n   for (long long i = 0; i < n; i++) { n     long long val; n     long long s = v.size(); n     cin >> val; n     while (s > 1 && v[s - 1] <= val && v[s - 1] <= v[s - 2]) { n       ans += min(val, v[s - 2]); n       v.pop_back(); n       s--; n     } n     v.push_back(val); n   } n   sort(v.begin(), v.end()); n   for (long long i = 0; i < v.size() - 2; i++) ans += v[i]; n   cout << ans << endl; n } n signed main() { n   for (long long i = 0; i < 32; i++) p2[i] = (1 << i); n   cout << fixed << setprecision(12); n   long long t; n   t = 1; n   for (long long i = 1; i <= t; i++) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n; n bool a[20][2][1 << 17]; n int ans = 1234, ami, apl; n void solve(int ind, int mx, int mi, int pl, int res) { n   if (ind > 18) { n     return; n   } n   if (mx == 0) { n     if (res < ans) { n       ans = res; n       ami = mi; n       apl = pl; n     } n     return; n   } n   for (int j = 0; j < (int)(2); ++j) n     for (int i = 0; i < (int)(mx + 1); ++i) { n       a[ind + 1][j][i] = false; n     } n   bool ok = true; n   for (int j = 0; j < (int)(2); ++j) n     for (int i = 1; i <= mx; i += 2) n       if (a[ind][j][i]) { n         ok = false; n       } n   int nmx = 0; n   if (ok) { n     for (int j = 0; j < (int)(2); ++j) n       for (int i = 0; i < (int)(mx + 1); ++i) n         if (a[ind][j][i]) { n           int ni = i >> 1; n           if (ni != 0) { n             a[ind + 1][j][ni] = true; n             nmx = max(nmx, ni); n           } n         } n     solve(ind + 1, nmx, mi, pl, res); n   } else { n     for (int i = 0; i < (int)(mx + 1); ++i) n       if (a[ind][0][i]) { n         int ni = i >> 1; n         if (ni != 0) { n           a[ind + 1][0][ni] = true; n           nmx = max(nmx, ni); n         } n       } n     for (int i = 0; i < (int)(mx + 1); ++i) n       if (a[ind][1][i]) { n         int ni = i; n         if (ni & 1) { n           ++ni; n         } n         ni >>= 1; n         if (ni != 0) { n           a[ind + 1][1][ni] = true; n           nmx = max(nmx, ni); n         } n       } n     solve(ind + 1, nmx, mi, pl | (1 << ind), res + 1); n     nmx = 0; n     for (int j = 0; j < (int)(2); ++j) n       for (int i = 0; i < (int)(mx + 1); ++i) { n         a[ind + 1][j][i] = false; n       } n     for (int i = 0; i < (int)(mx + 1); ++i) n       if (a[ind][1][i]) { n         int ni = i >> 1; n         if (ni != 0) { n           a[ind + 1][1][ni] = true; n           nmx = max(nmx, ni); n         } n       } n     for (int i = 0; i < (int)(mx + 1); ++i) n       if (a[ind][0][i]) { n         int ni = i; n         if (ni & 1) { n           ++ni; n         } n         ni >>= 1; n         if (ni != 0) { n           a[ind + 1][0][ni] = true; n           nmx = max(nmx, ni); n         } n       } n     solve(ind + 1, nmx, mi | (1 << ind), pl, res + 1); n   } n } n int main() { n   scanf( %d , &n); n   int mx = 0; n   for (int i = 0; i < (int)(n); ++i) { n     int x; n     scanf( %d , &x); n     if (x > 0) { n       a[0][0][x] = true; n       mx = max(mx, x); n     } n     if (x < 0) { n       a[0][1][-x] = true; n       mx = max(mx, -x); n     } n   } n   solve(0, mx, 0, 0, 0); n   printf( %d n , ans); n   for (int i = 0; i < (int)(20); ++i) n     if (apl & (1 << i)) { n       printf( %d  , 1 << i); n     } else if (ami & (1 << i)) { n       printf( %d  , -(1 << i)); n     } n   return 0; n } n


#include <bits/stdc++.h> n const int oo = 1073741819; n using namespace std; n int f[200][500], n, t[500], w[500]; n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> t[i] >> w[i]; n   memset(f, 61, sizeof(f)); n   f[0][0] = 0; n   for (int i = 1; i <= n; i++) n     for (int j = 0; j <= i * 2; j++) { n       if (j >= t[i]) f[i][j] = min(f[i][j], f[i - 1][j - t[i]]); n       f[i][j] = min(f[i][j], f[i - 1][j] + w[i]); n     } n   int ans = 1073741819; n   for (int j = 0; j <= n * 2; j++) n     if (f[n][j] <= j) ans = min(ans, j); n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 2e5 + 5; n const int cipher = -1e9; n template <typename T> n void chkmax(T &x, T y) { n   x = max(x, y); n } n template <typename T> n void chkmin(T &x, T y) { n   x = min(x, y); n } n template <typename T> n void read(T &x) { n   x = 0; n   int f = 1; n   char c = getchar(); n   for (; !isdigit(c); c = getchar()) n     if (c ==  - ) f = -f; n   for (; isdigit(c); c = getchar()) x = x * 10 + c -  0 ; n   x *= f; n } n template <typename T> n void write(T x) { n   if (x < 0) x = -x, putchar( - ); n   if (x > 9) write(x / 10); n   putchar(x % 10 +  0 ); n } n template <typename T> n void writeln(T x) { n   write(x); n   puts(  ); n } n struct SegmentTree { n   struct Node { n     int lc, rc; n     int tag, Max, depth; n   } a[MAXN * 2]; n   int root, size, n, depth[MAXN]; n   void update(int root) { n     a[root].Max = max(a[a[root].lc].Max, a[a[root].rc].Max); n     a[root].depth = max(a[a[root].lc].depth, a[a[root].rc].depth); n   } n   void build(int &root, int l, int r) { n     root = ++size; n     a[root].Max = -2e9; n     a[root].tag = cipher; n     if (l == r) { n       a[root].depth = depth[l]; n       return; n     } n     int mid = (l + r) / 2; n     build(a[root].lc, l, mid); n     build(a[root].rc, mid + 1, r); n     update(root); n   } n   void init(int x, int *y) { n     n = x, size = 0; n     for (int i = 1; i <= n; i++) depth[i] = 2 * y[i]; n     build(root, 1, n); n   } n   void pushdown(int root) { n     if (a[root].tag != cipher && a[root].lc) { n       a[a[root].lc].Max = a[root].tag + a[a[root].lc].depth; n       a[a[root].lc].tag = a[root].tag; n       a[a[root].rc].Max = a[root].tag + a[a[root].rc].depth; n       a[a[root].rc].tag = a[root].tag; n       a[root].tag = cipher; n     } n   } n   void modify(int root, int l, int r, int ql, int qr, int x) { n     if (l == ql && r == qr) { n       a[root].Max = x + a[root].depth; n       a[root].tag = x; n       return; n     } n     pushdown(root); n     int mid = (l + r) / 2; n     if (mid >= ql) modify(a[root].lc, l, mid, ql, min(mid, qr), x); n     if (mid + 1 <= qr) modify(a[root].rc, mid + 1, r, max(mid + 1, ql), qr, x); n     update(root); n   } n   void modify(int l, int r, int x) { modify(root, 1, n, l, r, x); } n   int query(int root, int l, int r, int ql, int qr, int x) { n     pushdown(root); n     int mid = (l + r) / 2; n     if (l == ql && r == qr) { n       if (l == r) { n         if (a[root].Max > x) n           return l; n         else n           return l - 1; n       } n       if (a[a[root].rc].Max > x) n         return query(a[root].rc, mid + 1, r, mid + 1, r, x); n       else n         return query(a[root].lc, l, mid, l, mid, x); n     } n     if (mid >= qr) return query(a[root].lc, l, mid, ql, qr, x); n     if (mid + 1 <= ql) return query(a[root].rc, mid + 1, r, ql, qr, x); n     int tans = query(a[root].rc, mid + 1, r, mid + 1, qr, x); n     if (tans == mid) n       return query(a[root].lc, l, mid, ql, mid, x); n     else n       return tans; n   } n   int query(int l, int r, int x) { return query(root, 1, n, l, r, x); } n } ST; n vector<int> a[MAXN], points[MAXN]; n int y[MAXN], t[MAXN], p[MAXN], ans[MAXN], val[MAXN]; n int n, m, depth[MAXN], size[MAXN], son[MAXN]; n int timer, father[MAXN], dfn[MAXN], up[MAXN]; n void dfs(int pos, int fa) { n   depth[pos] = depth[fa] + 1; n   size[pos] = 1, son[pos] = 0; n   for (auto x : a[pos]) n     if (x != fa) { n       dfs(x, pos); n       size[pos] += size[x]; n       if (size[x] > size[son[pos]]) son[pos] = x; n     } n } n void efs(int pos, int fa, int from) { n   up[pos] = from; n   father[pos] = fa; n   dfn[pos] = ++timer; n   points[from].push_back(pos); n   if (son[pos]) efs(son[pos], pos, from); n   for (auto x : a[pos]) n     if (x != son[pos] && x != fa) efs(x, pos, x); n } n bool cmp(int a, int b) { n   if (t[a] + depth[y[a]] == t[b] + depth[y[b]]) n     return y[a] < y[b]; n   else n     return t[a] + depth[y[a]] < t[b] + depth[y[b]]; n } n int main() { n   read(n), read(m); n   for (int i = 2; i <= n + 1; i++) { n     int x; n     read(x), x++; n     a[x].push_back(i); n   } n   dfs(1, 0); n   efs(1, 0, 1); n   for (int i = 1; i <= m; i++) { n     read(y[i]), y[i]++; n     read(t[i]), p[i] = i; n   } n   sort(p + 1, p + m + 1, cmp); n   static int tmp[MAXN]; n   for (int i = 1; i <= n + 1; i++) tmp[dfn[i]] = depth[i]; n   ST.init(n + 1, tmp); n   ST.modify(1, 1, 2e9); n   for (int i = 1; i <= m; i++) { n     int pos = y[p[i]], x = pos, res = 0; n     while (true) { n       int f = up[x]; n       int tmp = ST.query(dfn[f], dfn[x], depth[pos] + t[p[i]]); n       if (tmp == dfn[f] - 1) n         res += dfn[x] - dfn[f] + 1; n       else { n         res += dfn[x] - tmp; n         break; n       } n       x = father[f]; n     } n     x = pos; n     ans[p[i]] = t[p[i]] + 2 * res; n     int tres = res; n     while (tres) { n       int f = up[x]; n       if (tres <= dfn[x] - dfn[f] + 1) { n         ST.modify(dfn[x] - tres + 1, dfn[x], t[p[i]] + 2 * res - depth[pos]); n         break; n       } else { n         tres -= dfn[x] - dfn[f] + 1; n         ST.modify(dfn[f], dfn[x], t[p[i]] + 2 * res - depth[pos]); n       } n       x = father[f]; n     } n   } n   for (int i = 1; i <= m; i++) printf( %d  , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n T sqr(T x) { n   return x * x; n } n template <class T> n T gcd(T a, T b) { n   return (b != 0 ? gcd<T>(b, a % b) : a); n } n template <class T> n T lcm(T a, T b) { n   return (a / gcd<T>(a, b) * b); n } n template <class T> n inline T bigmod(T p, T e, T M) { n   if (e == 0) return 1; n   if (e % 2 == 0) { n     long long int t = bigmod(p, e / 2, M); n     return (T)((t * t) % M); n   } n   return (T)((long long int)bigmod(p, e - 1, M) * (long long int)p) % M; n } n template <class T> n inline T bigexp(T p, T e) { n   if (e == 0) return 1; n   if (e % 2 == 0) { n     long long int t = bigexp(p, e / 2); n     return (T)((t * t)); n   } n   return (T)((long long int)bigexp(p, e - 1) * (long long int)p); n } n template <class T> n inline T modinverse(T a, T M) { n   return bigmod(a, M - 2, M); n } n int dx4[] = {1, 0, -1, 0}; n int dy4[] = {0, 1, 0, -1}; n int dx8[] = {1, 1, 0, -1, -1, -1, 0, 1}; n int dy8[] = {0, 1, 1, 1, 0, -1, -1, -1}; n int nx8[] = {1, 1, -1, -1, 2, 2, -2, -2}; n int ny8[] = {2, -2, 2, -2, 1, -1, 1, -1}; n int month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; n int n, a[100005], b[100005], c, com; n vector<int> v[100005]; n map<int, int> m; n bool bl[100005]; n void dfs(int p) { n   if (bl[p]) return; n   bl[p] = true; n   v[com].push_back(p); n   dfs(b[p]); n } n int main() { n   scanf( %d , &n); n   for (__typeof(n) i = (1); i <= (n); i++) scanf( %d , &a[i]); n   for (__typeof(n) i = (1); i <= (n); i++) b[i] = a[i]; n   sort(b + 1, b + n + 1); n   for (__typeof(n) i = (1); i <= (n); i++) m[b[i]] = ++c; n   for (__typeof(n) i = (1); i <= (n); i++) b[i] = m[a[i]]; n   for (int i = 1; i <= n; i++) { n     if (bl[i] == false) { n       ++com; n       dfs(i); n     } n   } n   cout << com, printf(  n ); n   for (__typeof(com) i = (1); i <= (com); i++) { n     printf( %d , (int)v[i].size()); n     for (int j = 0; j < (int)v[i].size(); j++) printf(  %d , v[i][j]); n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n int t; n int n; n string a, b; n int op[N]; n int opb[N]; n int bp = 0; n int ans = 0; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> t; n   while (t--) { n     cin >> n; n     ans = 0; n     cin >> a; n     cin >> b; n     if (a == b) { n       cout << 0 << endl; n       continue; n     } n     char st = a[0]; n     for (int i = 1; i < n; i++) { n       if (a[i] != st) { n         op[++ans] = i; n         st = a[i]; n       } n     } n     if (a[n - 1] ==  0 ) op[++ans] = n; n     st = b[0]; n     bp = 0; n     for (int i = 1; i < n; i++) { n       if (b[i] != st) { n         opb[++bp] = i; n         st = b[i]; n       } n     } n     if (b[n - 1] ==  0 ) opb[++bp] = n; n     cout << ans + bp; n     for (int i = 1; i <= ans; i++) cout <<     << op[i]; n     for (int i = bp; i >= 1; i--) cout <<     << opb[i]; n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   long long arr[n]; n   for (int i = 0; i < n; i++) cin >> arr[i]; n   if (n > 5 * m) { n     cout <<  YES ; n     return 0; n   } n   bool dp[n][m + 1]; n   for (int i = 0; i <= m; i++) dp[0][i] = false; n   for (int i = 0; i < n; i++) dp[i][0] = true; n   if (arr[0] % m == 0) n     dp[0][m] = true; n   else n     dp[0][arr[0] % m] = true; n   for (int i = 1; i < n; i++) { n     for (int j = 1; j <= m; j++) n       dp[i][j] = dp[i - 1][j] || dp[i - 1][(j - (arr[i] % m) + m) % m]; n   } n   if (dp[n - 1][m]) n     cout <<  YES ; n   else n     cout <<  NO ; n } n


#include <bits/stdc++.h> n #pragma warning(disable : 4996) n using namespace std; n long long __gcd(long long a, long long b) { n   while (a != 0) { n     long long __t = b % a; n     b = a; n     a = __t; n   } n   return b; n } n bool isSimple(unsigned x) { n   for (unsigned i = 2; i < x; i++) n     if (x % i == 0) return false; n   return true; n } n int main() { n   cin.tie(0); n   cout.tie(0); n   cerr.tie(0); n   ios_base::sync_with_stdio(false); n   long long x; n   cin >> x; n   cout << x - x / 2 - x / 3 - x / 5 - x / 7 + x / 6 + x / 10 + x / 14 + x / 15 + n               x / 21 + x / 35 - x / 30 - x / 42 - x / 70 - x / 105 + x / 210; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, b; n   while (~scanf( %d%d , &n, &b)) { n     int a[2050]; n     int m = -999; n     for (int i = 0; i < n; i++) scanf( %d , a + i); n     for (int i = 0; i < n - 1; i++) { n       int z = b / a[i]; n       for (int j = i + 1; j < n; j++) { n         m = max(m, (a[j] - a[i]) * z + b); n       } n     } n     if (m > b) n       printf( %d n , m); n     else n       printf( %d n , b); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int get(int a, int b) { n   if (a < b) return get(b, a); n   if (a % b == 0) { n     return a / b; n   } n   int c = a / b; n   return get(b, a % b) + c; n } n int main() { n   int N; n   scanf( %d , &N); n   for (int i = 0; i < N; i++) { n     int a, b; n     scanf( %d%d , &a, &b); n     printf( %d n , get(a, b)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char s[200001]; n long long a[26]; n long long cnt = 0; n int main() { n   long long i, j, k, l, m, n, t; n   cin >> t; n   cin >> s; n   l = strlen(s); n   for (i = 0; i < 26; i++) a[i] = 0; n   for (i = 1; i < l; i = i + 2) { n     if ((s[i - 1] - 97) != (s[i] - 65)) { n       if (a[s[i] - 65] > 0) { n         a[s[i] - 65]--; n         a[s[i - 1] - 97]++; n       } else { n         cnt++; n         a[s[i - 1] - 97]++; n       } n     } n   } n   cout << cnt << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( -O3 ) n using namespace std; n const long long mod = 998244353; n struct edge { n   int u, v, cap; n   edge() {} n   edge(int _u, int _v, int _cap) : u(_u), v(_v), cap(_cap) {} n   int get(int t) { return t ^ u ^ v; } n }; n const int inf = 1e9; n struct edkarp { n   vector<edge> edges; n   vector<vector<int>> adj; n   int n; n   void init(int _n) { n     n = _n; n     adj.assign(n, vector<int>(0)); n   } n   inline int comp(int i) { return i ^ 1; } n   inline int par(int i, int ed) { return edges[ed].get(i); } n   void add(int u, int v, int c) { n     adj[u].emplace_back(edges.size()); n     edges.emplace_back(u, v, c); n     adj[v].emplace_back(edges.size()); n     edges.emplace_back(u, v, 0); n   } n   int bfs(int st, int tar, vector<int> &p) { n     p.assign(n, -1); n     p[st] = -2; n     queue<pair<int, int>> q; n     q.push(make_pair(st, inf)); n     while (!q.empty()) { n       int i = q.front().first, flow = q.front().second; n       q.pop(); n       for (auto &x : adj[i]) { n         if (p[par(i, x)] == -1 && edges[x].cap) { n           p[par(i, x)] = x; n           int nf = min(flow, edges[x].cap); n           if (par(i, x) == tar) { n             return nf; n           } n           q.push(make_pair(par(i, x), nf)); n         } n       } n     } n     return 0; n   } n   int maxflow(int st, int tar) { n     vector<int> p(n); n     int flow = 0; n     int nf; n     while (nf = bfs(st, tar, p)) { n       flow += nf; n       int cr = tar; n       while (cr != st) { n         edges[p[cr]].cap -= nf; n         edges[comp(p[cr])].cap += nf; n         cr = par(cr, p[cr]); n       } n     } n     return flow; n   } n }; n struct edgeg { n   int u, v, c, lo; n   bool in_loop = false; n   edgeg() {} n   edgeg(int _u, int _v, int _c) : u(_u), v(_v), c(_c), lo(-1) {} n   int get(int t) { return t ^ u ^ v; } n }; n const int N = 1e4 + 5; n int n, m, u, v, c, vis[N], p[N], vis1[N]; n vector<int> adj[N]; n vector<bool> avai; n vector<vector<int>> loops; n vector<edgeg> edges; n edkarp fo; n inline int geta(int x, int edge_num) { return edges[edge_num].get(x); } n void dfs(int i, int par) { n   vis[i] = 1; n   vis1[i] = 1; n   for (int x : adj[i]) { n     int u = geta(i, x); n     if (u == par) continue; n     if (vis[u]) { n       if (!vis1[u]) continue; n       edges[x].in_loop = true; n       vector<int> clrs; n       clrs.push_back(edges[x].c); n       u = i; n       while (u != geta(i, x)) { n         edges[p[u]].in_loop = true; n         clrs.push_back(edges[p[u]].c); n         u = geta(u, p[u]); n       } n       sort(clrs.begin(), clrs.end()); n       auto it = unique(clrs.begin(), clrs.end()); n       if (it != clrs.end()) { n         for (auto it1 = clrs.begin(); it1 != it; it1++) { n           avai[*it1] = 1; n         } n       } else { n         loops.push_back(clrs); n       } n     } else { n       p[u] = x; n       dfs(u, i); n     } n   } n   vis1[i] = 0; n } n void solve() { n   cin >> n >> m; n   avai.assign(m, 0); n   for (int i = 0; i < m; i++) { n     cin >> u >> v >> c; n     u--, v--, c--; n     adj[u].push_back(edges.size()); n     adj[v].push_back(edges.size()); n     edges.emplace_back(u, v, c); n   } n   fill(vis, vis + n, 0); n   fill(vis1, vis1 + n, 0); n   dfs(0, -1); n   int cr = 0; n   for (int i = 0; i < (int)edges.size(); i++) { n     if (!edges[i].in_loop) { n       avai[edges[i].c] = 1; n     } n   } n   fo.init((int)loops.size() + m + 2); n   for (int i = 0; i < (int)loops.size(); i++) { n     fo.add(0, i + 1, (int)loops[i].size() - 1); n     for (int c : loops[i]) { n       if (avai[c]) continue; n       fo.add(i + 1, (int)loops.size() + 1 + c, 1); n     } n   } n   int flow = 0; n   for (int i = 0; i < m; i++) { n     flow += avai[i]; n   } n   for (int i = (int)loops.size() + 1; i <= (int)loops.size() + m; i++) { n     fo.add(i, (int)loops.size() + m + 1, 1); n   } n   flow += fo.maxflow(0, (int)loops.size() + m + 1); n   cout << flow <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int t = 1; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int a[101]; n int res = 0; n void solve() { n   for (int i = 2; i < n; ++i) { n     if (a[i] == 0) { n       if (a[i - 1] == 1 && a[i + 1] == 1 && a[i + 2] == 0 && a[i + 3] == 1 && n           i - 1 > 0) { n         a[i + 1] = 0; n         ++res; n         i += 2; n       } else if (a[i + 1] == 1 && a[i - 1] == 1 && i - 1 > 0) { n         a[i + 1] = 0; n         ++res; n         ++i; n       } n     } n   } n   cout << res << endl; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) { n     scanf( %d , &a[i]); n   } n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, ans[1010][1010]; n pair<int, int> a[1010]; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i].first), a[i].second = i; n   sort(a + 1, a + n + 1); n   for (int i = 1; i <= n; i++) { n     int j = i; n     while (a[i].first--) ans[j][a[i].second] = 1, j = (j + n) % (n + 1); n   } n   printf( %d n , n + 1); n   for (int i = 0; i <= n; i++, puts(  )) n     for (int j = 1; j <= n; j++) printf( %d , ans[i][j]); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, a, b; n   cin >> n >> a >> b; n   string arr; n   cin >> arr; n   if (arr[a - 1] == arr[b - 1]) n     cout << 0 <<   n ; n   else n     cout << 1 <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   char str[510]; n   int n, i; n   bool rec[27]; n   while (cin >> str) { n     int flag = 0, faill = 0; n     memset(rec, 0, sizeof(rec)); n     int t = strlen(str); n     if (str[0] !=  a ) faill = 1; n     rec[1] = 1; n     rec[0] = 1; n     int sum = 1; n     for (i = 0; i < t; i++) { n       if (rec[str[i] -  a  + 1] == 0) { n         rec[str[i] -  a  + 1] = 1; n         for (int j = i - 1; j >= 1; j--) { n           if (str[j] > str[i]) { n             faill = 1; n             break; n           } n         } n       } n     } n     for (int i = 26; i >= 1; i--) { n       if (flag == 1 && rec[i] == 0) { n         faill = 1; n         break; n       } n       if (rec[i]) flag = 1; n     } n     if (faill) n       cout <<  NO  << endl; n     else n       cout <<  YES  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 0x3fffffff; n double a, b, c, d; n int main() { n   while (cin >> a >> b >> c >> d) { n     double l = 0, r = 2000000000.0; n     double a1, a2, b1, b2, c1, c2, d1, d2; n     double m1, m2, min1, min2; n     for (int i = 0; i < 10000; i++) { n       double mid = (l + r) / 2; n       a1 = a + mid, a2 = a - mid; n       b1 = b + mid, b2 = b - mid; n       c1 = c + mid, c2 = c - mid; n       d1 = d + mid, d2 = d - mid; n       min1 = min(min(a1 * d1, a1 * d2), min(a2 * d1, a2 * d2)); n       min2 = min(min(b1 * c1, b1 * c2), min(b2 * c1, b2 * c2)); n       m1 = max(max(a1 * d1, a1 * d2), max(a2 * d1, a2 * d2)); n       m2 = max(max(b1 * c1, b1 * c2), max(b2 * c1, b2 * c2)); n       if (min1 <= m2 && min2 <= m1) n         r = mid; n       else n         l = mid; n     } n     printf( %.10lf n , l); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int min(int a, int b) { n   if (a > b) { n     return b; n   } else { n     return a; n   } n } n int max(int a, int b) { n   if (a > b) { n     return a; n   } else { n     return b; n   } n } n int main() { n   int n, x0; n   int a, b; n   int d, f; n   cin >> n >> x0 >> a >> b; n   if (a > b) { n     d = b; n     f = a; n   } else { n     d = a; n     f = b; n   } n   int temp1; n   int temp2; n   for (int i = 1; i != n; i++) { n     cin >> a >> b; n     if (a > b) { n       temp1 = a; n       a = b; n       b = temp1; n     } n     if (a > d) { n       if (a > f) { n         cout << -1; n         return 0; n       } n       d = a; n     } n     if (b < f) { n       if (b < d) { n         cout << -1; n         return 0; n       } n       f = b; n     } n   } n   if (x0 >= d && x0 <= f) { n     cout << 0; n   } else { n     if (x0 < d) { n       cout << d - x0; n     } else { n       cout << x0 - f; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, leaf, po; n vector<int> v[100005]; n int main() { n   scanf( %d , &n); n   for (int i = (2); i <= (n); i++) { n     int a, b; n     scanf( %d %d , &a, &b); n     v[a].push_back(b); n     v[b].push_back(a); n   } n   for (int i = (1); i <= (n); i++) n     if (v[i].size() == 1) leaf++; n   po = 1; n   for (int i = (1); i <= (n - leaf); i++) po = (po + po) % 1000000007; n   printf( %d n , (1LL * po * (n - leaf) + 2LL * po * leaf) % 1000000007); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, t, x, y; n   int ar = 0; n   int al = 0; n   int br = 0; n   int bl = 0; n   cin >> n; n   for (int i = 0; n > i; i++) { n     cin >> t; n     cin >> x; n     cin >> y; n     if (t == 1) { n       ar += x; n       al += y; n     } else { n       br += x; n       bl += y; n     } n   } n   if (ar >= al) { n     cout <<  LIVE  << endl; n   } else { n     cout <<  DEAD  << endl; n   } n   if (br >= bl) { n     cout <<  LIVE  << endl; n   } else { n     cout <<  DEAD  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[2 * 2005], pos[2 * 2005], dp[2 * 2005][2 * 2005]; n int call(int p, int len) { n   if (p > 2 * n) return len == n; n   if (len < n) return 0; n   int &ret = dp[p][len]; n   if (~ret) return ret; n   ret = call(p + pos[p], len); n   ret += call(p + pos[p], len - pos[p]); n   return ret; n } n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   int tc; n   cin >> tc; n   while (tc--) { n     cin >> n; n     for (int i = 1; i <= 2 * n; i++) { n       cin >> a[i]; n     } n     for (int i = 1; i <= 2 * n; i++) { n       int cnt = 1; n       for (int j = i + 1; j <= 2 * n; j++) { n         if (a[i] > a[j]) n           cnt++; n         else n           break; n       } n       pos[i] = cnt; n     } n     for (int i = 1; i <= 2 * n; i++) n       for (int j = 1; j <= 2 * n; j++) dp[i][j] = -1; n     if (call(1, 2 * n)) n       cout <<  YES n ; n     else n       cout <<  NO n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 1e9 + 7; n const long long INF = 4 * (long long)1e18 + 7; n const long double eps = 1e-9; n signed main() { n   if (false) { n     freopen( output.txt ,  w , stdout); n     freopen( input.txt ,  r , stdin); n   } n   ios_base::sync_with_stdio(false); n   int n; n   cin >> n; n   vector<int> arr(n); n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n   } n   vector<vector<int>> dp(5, vector<int>(n, -1)); n   for (int i = 0; i < 5; i++) { n     dp[i][0] = -2; n   } n   for (int i = 0; i < n - 1; i++) { n     for (int j = 0; j < 5; j++) { n       if (dp[j][i] != -1) { n         if (arr[i + 1] > arr[i]) { n           for (int m = j + 1; m < 5; m++) { n             dp[m][i + 1] = j; n           } n         } else if (arr[i + 1] < arr[i]) { n           for (int m = 0; m < j; m++) { n             dp[m][i + 1] = j; n           } n         } else { n           for (int m = 0; m < 5; m++) { n             if (m == j) continue; n             dp[m][i + 1] = j; n           } n         } n       } n     } n   } n   vector<int> ans; n   for (int j = 0; j < 5; j++) { n     if (dp[j][n - 1] != -1) { n       int type = j; n       for (int i = n - 1; i >= 0; i--) { n         ans.push_back(type + 1); n         type = dp[type][i]; n       } n       break; n     } n   } n   if (ans.size() == 0) { n     cout << -1 << endl; n   } else { n     for (int i = n - 1; i >= 0; i--) { n       cout << ans[i] <<    ; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int n; n   cin >> n; n   string s; n   cin >> s; n   map<char, vector<int>> ma; n   for (int i = 0; i < n; i++) { n     ma[s[i]].push_back(i + 1); n   } n   int m; n   cin >> m; n   vector<int> ans; n   while (m--) { n     string s1; n     cin >> s1; n     map<char, int> m1; n     for (int i = 0; i < s1.size(); i++) { n       m1[s1[i]]++; n     } n     int a = -1; n     for (auto &i : ma) { n       if (m1.find(i.first) != m1.end()) { n         int b = m1[i.first]; n         a = max(a, i.second[b - 1]); n       } n     } n     ans.push_back(a); n   } n   for (int i = 0; i < ans.size(); i++) { n     cout << ans[i] <<   n ; n   } n } n int main() { n   ios::sync_with_stdio(0); n   int t = 1; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n pair<long long, long long> t[4 * 200009]; n long long a[200009], lazy[4 * 200009], ans[200009]; n void build(long long v, long long tl, long long tr) { n   if (tl == tr) { n     t[v] = {a[tl], tl}; n   } else { n     long long tm = (tl + tr) / 2; n     build(v * 2, tl, tm); n     build(v * 2 + 1, tm + 1, tr); n     if (t[v * 2].first < t[v * 2 + 1].first || n         (t[v * 2].first == t[v * 2 + 1].first && n          t[v * 2].second > t[v * 2 + 1].second)) n       t[v] = t[v * 2]; n     else n       t[v] = t[v * 2 + 1]; n   } n } n void push(long long v) { n   t[v * 2].first += lazy[v]; n   lazy[v * 2] += lazy[v]; n   t[v * 2 + 1].first += lazy[v]; n   lazy[v * 2 + 1] += lazy[v]; n   lazy[v] = 0; n } n void update(long long v, long long tl, long long tr, long long l, long long r, n             long long addend) { n   if (l > r) return; n   if (l == tl && tr == r) { n     t[v].first += addend; n     lazy[v] += addend; n   } else { n     push(v); n     long long tm = (tl + tr) / 2; n     update(v * 2, tl, tm, l, min(r, tm), addend); n     update(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r, addend); n     if (t[v * 2].first < t[v * 2 + 1].first || n         (t[v * 2].first == t[v * 2 + 1].first && n          t[v * 2].second > t[v * 2 + 1].second)) n       t[v] = t[v * 2]; n     else n       t[v] = t[v * 2 + 1]; n   } n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long n; n   cin >> n; n   long long i = 1; n   while (i <= n) cin >> a[i++]; n   build(1, 1, n); n   i = 1; n   long long x; n   while (i <= n) { n     x = t[1].second; n     ans[x] = i; n     update(1, 1, n, x + 1, n, -i); n     update(1, 1, n, x, x, 1e13); n     i++; n   } n   i = 1; n   while (i <= n) { n     cout << ans[i] <<    ; n     i++; n   } n } n


#include <bits/stdc++.h> n using namespace std; n inline void read(int &x) { n   x = 0; n   int f = 1; n   char ch = getchar(); n   while (ch != 45 && (ch >  9  || ch <  0 )) ch = getchar(); n   if (ch == 45) { n     f = -1, ch = getchar(); n   } n   while (ch <=  9  && ch >=  0 ) { n     x = x * 10 + ch - 48; n     ch = getchar(); n   } n   x *= f; n } n int i, j, k, n, s, t, m, flag = 1, ans; n const int N = 2e5 + 10, mod = 1e9 + 7; n int fa[N], e[N][3], col[N], p[N], type[N]; n int find(int x) { n   if (x == fa[x]) return x; n   return fa[x] = find(fa[x]); n } n void merge(int u, int v) { n   u = find(u), v = find(v); n   if (v > u) swap(u, v); n   if (u == v) return; n   fa[u] = v; n } n signed main() { n   memset(type, -1, sizeof(type)); n   read(n), read(m); n   for (i = 1; i <= n; i++) fa[i] = i, fa[i + n] = i + n; n   for (i = 1; i <= m; i++) { n     read(e[i][1]), read(e[i][2]), read(col[i]); n     if (e[i][1] > e[i][2]) swap(e[i][1], e[i][2]); n     if (e[i][1] == 1) { n       type[e[i][2]] = col[i]; n       continue; n     } n     if (col[i] == 1) n       merge(e[i][1], e[i][2]), merge(e[i][1] + n, e[i][2] + n); n     else n       merge(e[i][1], e[i][2] + n), merge(e[i][1] + n, e[i][2]); n   } n   for (i = 1; i <= n; i++) { n     if (type[i] == -1) continue; n     if (type[find(i + n)] == -1) type[find(i + n)] = type[i] ^ 1; n     if (type[find(i + n)] == type[i]) flag = 0; n     if (type[find(i)] == -1) type[find(i)] = type[i]; n     if (type[find(i)] != type[i]) flag = 0; n   } n   for (i = 1; i <= n; i++) { n     if (find(i) == find(i + n)) flag = 0; n   } n   for (i = 2; i <= n; i++) { n     if (find(i) != i) continue; n     ans++; n     if (type[find(i + n)] != -1 || type[i] != -1) ans--; n     type[i] = 1; n   } n   if (flag == 0) { n     printf( 0 n ); n     return 0; n   } n   int now = 1; n   for (i = 1; i <= ans; i++) now = now * 2 % mod; n   cout << now << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool lambs[101]; n int main() { n   int n, m; n   cin >> n >> m; n   while (n--) { n     int x; n     cin >> x; n     while (x--) { n       int y; n       cin >> y; n       lambs[y] = true; n     } n   } n   for (int i = 1; i < m + 1; i++) n     if (lambs[i] == false) { n       cout <<  NO ; n       return 0; n     } n   cout <<  YES ; n   return 0; n } n


#include <bits/stdc++.h> n const long long seed = 131; n const int N = 1000010; n char ac[N], bc[N]; n int main() { n   int n; n   scanf( %d%s%s , &n, ac, bc), n--; n   for (int i = 0; i < n; i++) { n     if (bc[i] ==  N ) n       bc[i] =  S ; n     else if (bc[i] ==  S ) n       bc[i] =  N ; n     else if (bc[i] ==  E ) n       bc[i] =  W ; n     else n       bc[i] =  E ; n   } n   unsigned long long ha = 0, hb = 0, pow = 1; n   for (int l = 1; l <= n; l++) { n     ha += ac[n - l] * pow; n     hb *= 131, hb += bc[n - l]; n     pow *= 131; n     if (ha == hb) { n       printf( NO n ); n       return 0; n     } n   } n   printf( YES n ); n   return 0; n } n


#include <bits/stdc++.h> n int n, m; n int table[(1 << 20) + 10]; n int lowbit[(1 << 20) + 10]; n int ba[35][35]; n int cost[35][35]; n int same[35][35]; n char st[35][35]; n int min(int x, int y) { n   if (x < y) n     return x; n   else n     return y; n } n int main() { n   int i, a, j; n   scanf( %d%d n , &n, &m); n   for (i = 1; i <= n; i++) { n     scanf( %s n , st[i] + 1); n   } n   for (i = 1; i <= n; i++) { n     for (a = 1; a <= m; a++) { n       scanf( %d , &ba[i][a]); n     } n   } n   for (i = 1; i <= n; i++) { n     for (a = 1; a <= m; a++) { n       int coma = 0; n       for (j = 1; j <= n; j++) { n         if (st[i][a] == st[j][a]) { n           same[i][a] |= 1 << (j - 1); n           cost[i][a] += ba[j][a]; n           if (coma < ba[j][a]) coma = ba[j][a]; n         } n       } n       cost[i][a] -= coma; n     } n   } n   for (i = 1; i <= 1 << n; i++) { n     table[i] = 100000000; n     for (a = 1; a <= n; a++) { n       if (i >> (a - 1) & 1) { n         lowbit[i] = a; n         break; n       } n     } n   } n   table[0] = 0; n   for (a = 1; a < 1 << n; a++) { n     for (i = 1; i <= m; i++) { n       table[a] = n           min(table[a], table[a ^ (1 << (lowbit[a] - 1))] + ba[lowbit[a]][i]); n       table[a] = min(table[a], n                      table[a & (a ^ same[lowbit[a]][i])] + cost[lowbit[a]][i]); n     } n   } n   printf( %d n , table[(1 << n) - 1]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 702, M = 4900005; n int main() { n   int n, m, a[55], b, i, j, ans, cnt; n   while (scanf( %d , &n) == 1) { n     for (i = 1; i <= n; i++) scanf( %d , a + i); n     scanf( %d , &m); n     for (i = 1, ans = 0; i <= m; i++) { n       scanf( %d , &b); n       for (j = 1; j <= n; j++) n         if (b % a[j] == 0) { n           if (b / a[j] > ans) { n             ans = b / a[j]; n             cnt = 1; n           } else if (b / a[j] == ans) n             cnt++; n         } n     } n     printf( %d n , cnt); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = (int)1e5 + 55; n const int INF = (int)1e9 + 77; n const int MOD = (int)1e9 + 7; n const double PI = 2 * acos(0.0); n const double EPS = 1e-7; n const int N = (int)1e5 + 5; n vector<vector<int>> adj; n pair<int, int> cost[N]; n int mn; n bool vis[N]; n long long ans; n void dfs(int u) { n   if (vis[u]) return; n   vis[u] = 1; n   mn = min(mn, cost[u].first); n   for (int v : adj[u]) dfs(v); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n, m; n   cin >> n >> m; n   adj.resize(n + 1); n   for (int i = 1; i <= n; ++i) { n     int x; n     cin >> x; n     cost[i] = {x, i}; n   } n   for (int i = 0; i < m; ++i) { n     int x, y; n     cin >> x >> y; n     adj[x].push_back(y); n     adj[y].push_back(x); n   } n   for (int i = 1; i <= n; ++i) { n     int cur = cost[i].second; n     if (vis[cur]) continue; n     mn = INF; n     dfs(cur); n     ans += mn; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   int a, b, c, ty; n } a[100010]; n struct Tnode { n   int x, f, tag, ls, rs; n } t[3000000]; n int n, m, k, K, ans, rt, tot; n bool cmp(node x, node y) { return x.a < y.a; } n int getfull(int l, int r) { n   int s = 0; n   l--; n   for (int j = 1; j <= k; j <<= 1) n     s |= (((r / j - l / j) - (j * 2 <= k ? (r / j / 2 - l / j / 2) : 0)) & 1) * n          j; n   return s; n } n void update(int n) { n   if (t[n].tag) n     t[n].x = t[n].f; n   else n     t[n].x = t[t[n].ls].x ^ t[t[n].rs].x; n } n void modify(int &n, int l, int r, int L, int R, int k) { n   if (!n) n = ++tot, t[n].f = getfull(l, r); n   if ((L <= l) && (r <= R)) n     t[n].tag += k; n   else { n     int mid = (l + r) >> 1; n     if (L <= mid) modify(t[n].ls, l, mid, L, R, k); n     if (mid < R) modify(t[n].rs, mid + 1, r, L, R, k); n   } n   update(n); n } n void work() { n   scanf( %d %d %d , &n, &m, &k); n   for (K = 1; K * 2 <= k; K <<= 1) n     ; n   for (int i = 1, A, B, C, D; i <= m; i++) { n     scanf( %d %d %d %d , &A, &B, &C, &D); n     a[i] = (node){A, B, D, 1}, a[i + m] = (node){C + 1, B, D, -1}; n   } n   sort(a + 1, a + 2 * m + 1, cmp); n   for (int i = 1; i <= 2 * m; i++) { n     if (a[i - 1].a != a[i].a) { n       int x = getfull(a[i - 1].a, a[i].a - 1), y = t[rt].x, sx = 0, sy = 0, s; n       for (int j = K; j; j >>= 1) { n         s = -sx * sy, sx += (x & j) > 0, sy += (y & j) > 0, s += sx * sy; n         if (s & 1) ans ^= j; n       } n     } n     modify(rt, 1, n, a[i].b, a[i].c, a[i].ty); n   } n   puts(ans ?  Hamed  :  Malek ); n } n int main() { n   work(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 * 4; n inline int get() { n   int x; n   cin >> x; n   return x; n } n inline long long getlong() { n   long long x; n   cin >> x; n   return x; n } n inline string getString() { n   string s; n   cin >> s; n   return s; n } n template <typename T> n inline vector<T> getvector(int len) { n   vector<T> a(len); n   for (auto &it : a) cin >> it; n   return a; n } n inline vector<int> getvector(int len) { n   vector<int> a(len); n   for (auto &it : a) cin >> it; n   return a; n } n inline vector<pair<int, int>> getvector_andPairCnt(int len) { n   vector<pair<int, int>> a(len); n   int i = 1; n   for (auto &it : a) { n     it.second = i; n     i++; n     cin >> it.first; n   } n   return a; n } n double power_log(double power, double val) { return log(val) / log(power); } n template <typename T> n int sqr(T x) { n   return x * x; n } n template <typename T> n ostream &operator<<(ostream &os, const vector<T> &v) { n   for (auto it : v) os << it <<    ; n   os <<   n ; n   return os; n } n template <typename T, typename H> n inline vector<pair<T, H>> getvector_andBiPair(int len) { n   vector<pair<T, H>> a(len); n   int i = 1; n   for (auto &it : a) { n     cin >> it.first >> it.second; n   } n   return a; n } n template <typename T> n inline set<T> vector_to_set(const vector<T> &vec) { n   set<T> s; n   for (auto &it : vec) s.insert(it); n   return s; n } n int main() { n   int n = get(), k = get(); n   vector<pair<int, int>> arr(n); n   for (auto &it : arr) cin >> it.first; n   for (auto &it : arr) cin >> it.second; n   sort(arr.begin(), arr.end(), [](pair<int, int> l, pair<int, int> r) { n     return l.first - l.second < r.first - r.second; n   }); n   int ans = 0; n   bool can = true; n   for (int i = 1; i <= n; i++) { n     if (i > k && arr[i - 1].first > arr[i - 1].second) { n       can = false; n     } n     if (can) n       ans += arr[i - 1].first; n     else n       ans += arr[i - 1].second; n   } n   cout << ans; n } n