#include <bits/stdc++.h> n using namespace std; n const int64_t INF = 0x7FFFFFFF; n const int MAX = 1; n unordered_map<int, int> maps; n unordered_map<int, int> cnt; n int main(int argc, char* args[]) { n   int n; n   scanf( %d , &n); n   for (int(i) = (0), _n = (n - 1); (i) <= _n; (i)++) { n     int x; n     scanf( %d , &x); n     maps[x]++; n   } n   for (auto it : maps) { n     int x = it.second; n     for (int(i) = (1), _n = (x); (i) <= _n; (i)++) { n       cnt[i]++; n     } n   } n   int ans = 0; n   for (auto it : cnt) { n     ans += it.second - 1; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool lucky(char a) { n   if (a ==  4  || a ==  7 ) n     return true; n   else n     return false; n } n int main() { n   long long int a, b, c, d; n   vector<bool> res; n   cin >> a >> b >> c >> d; n   if (abs(c - d) > 1 || min(a, b) < max(c, d)) { n     puts( -1 ); n     return 0; n   } n   if (c > d) { n     for (long long int i = 0; i < ((long long int)(a - d)); i++) cout << 4; n     for (long long int i = 0; i < ((long long int)(d)); i++) cout << 74; n     for (long long int i = 0; i < ((long long int)(b - d)); i++) cout << 7; n   } else if (c < d) { n     cout << 7; n     for (long long int i = 0; i < ((long long int)(a - c)); i++) cout << 4; n     for (long long int i = 0; i < ((long long int)(c - 1)); i++) cout << 74; n     for (long long int i = 0; i < ((long long int)(b - c)); i++) cout << 7; n     cout << 4; n   } else { n     if (a + b < c + d + 1) { n       puts( -1 ); n       return 0; n     } n     for (long long int i = 0; i < ((long long int)(a - c)); i++) cout << 4; n     if (a - c > 0) { n       for (long long int i = 0; i < ((long long int)(c - 1)); i++) cout << 74; n       for (long long int i = 0; i < ((long long int)(b - c + 1)); i++) n         cout << 7; n       cout << 4; n     } else { n       cout << 7; n       for (long long int i = 0; i < ((long long int)(c)); i++) cout << 47; n       for (long long int i = 0; i < ((long long int)(b - 1 - c)); i++) n         cout << 7; n     } n   } n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char buf[1 << 21], *p1 = buf, *p2 = buf; n inline int qread() { n   register char c = n       (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) n            ? EOF n            : *p1++); n   register int x = 0, f = 1; n   while (c <  0  || c >  9 ) { n     if (c ==  - ) f = -1; n     c = (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) n              ? EOF n              : *p1++); n   } n   while (c >=  0  && c <=  9 ) { n     x = (x << 3) + (x << 1) + c - 48; n     c = (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) n              ? EOF n              : *p1++); n   } n   return x * f; n } n inline int Abs(const int& x) { return (x > 0 ? x : -x); } n inline int Max(const int& x, const int& y) { return (x > y ? x : y); } n inline int Min(const int& x, const int& y) { return (x < y ? x : y); } n struct Segtree { n   long long sumv[800005], sumidx[800005]; n   inline void Modify(int p, int pl, int pr, int id) { n     if (pl == pr) { n       sumv[p]++; n       sumidx[p] += id; n       return; n     } n     int mid = pl + pr >> 1; n     if (mid >= id) n       Modify(p << 1, pl, mid, id); n     else n       Modify(p << 1 | 1, mid + 1, pr, id); n     sumv[p] = sumv[p << 1] + sumv[p << 1 | 1]; n     sumidx[p] = sumidx[p << 1] + sumidx[p << 1 | 1]; n   } n   inline int Kth(int p, int pl, int pr, int k) { n     if (pl == pr) return pl; n     int mid = pl + pr >> 1; n     if (k <= sumv[p << 1]) n       return Kth(p << 1, pl, mid, k); n     else n       return Kth(p << 1 | 1, mid + 1, pr, k - sumv[p << 1]); n   } n   inline long long Sumv(int p, int pl, int pr, int l, int r) { n     if (l > r) return 0; n     if (pl == l && pr == r) return sumv[p]; n     int mid = pl + pr >> 1; n     if (mid >= r) n       return Sumv(p << 1, pl, mid, l, r); n     else if (mid + 1 <= l) n       return Sumv(p << 1 | 1, mid + 1, pr, l, r); n     else n       return Sumv(p << 1, pl, mid, l, mid) + n              Sumv(p << 1 | 1, mid + 1, pr, mid + 1, r); n   } n   inline long long Sumidx(int p, int pl, int pr, int l, int r) { n     if (l > r) return 0; n     if (pl == l && pr == r) return sumidx[p]; n     int mid = pl + pr >> 1; n     if (mid >= r) n       return Sumidx(p << 1, pl, mid, l, r); n     else if (mid + 1 <= l) n       return Sumidx(p << 1 | 1, mid + 1, pr, l, r); n     else n       return Sumidx(p << 1, pl, mid, l, mid) + n              Sumidx(p << 1 | 1, mid + 1, pr, mid + 1, r); n   } n }; n Segtree sgt; n int n, a[200005], idx[200005]; n inline void Read() { n   n = qread(); n   for (int i = 1; i <= n; i++) idx[a[i] = qread()] = i; n } n inline long long Sum(int l, int k) { return 1ll * k * (l + l + k - 1) / 2; } n inline void Solve() { n   long long inv = 0; n   for (int i = 1; i <= n; i++) { n     inv += sgt.Sumv(1, 1, n, idx[i] + 1, n); n     sgt.Modify(1, 1, n, idx[i]); n     int midv = sgt.Kth(1, 1, n, i + 1 >> 1); n     long long lsum = sgt.Sumidx(1, 1, n, 1, midv - 1), n               rsum = sgt.Sumidx(1, 1, n, midv + 1, n); n     printf( %lld  , inv + Sum(midv - (i - 1 >> 1), i - 1 >> 1) - lsum + rsum - n                         Sum(midv + 1, i >> 1)); n   } n } n int main() { n   Read(); n   Solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int s[3000005], ps[3000005][5]; n int n, ans; n vector<int> temp; n int po(int a, int b) { n   if (b == 0) return 1; n   return a * po(a, b - 1); n } n void prep() { n   int ptr1 = 1, ptr2 = 1, maxi = 0; n   ps[1][0] = 1; n   for (int i = (2); i <= (3000000); i++) { n     while (po(ptr1, 4) < i) ptr1++; n     while (po(ptr2 + 1, 2) <= i) ptr2++; n     for (int j = (0); j <= (4); j++) ps[i][j] = ps[i - 1][j]; n     while (ps[ptr2][s[i]] - ps[ptr1 - 1][s[i]] > 0) s[i]++; n     ps[i][s[i]]++; n   } n } n int f(long long x) { n   if (x <= 3000000) return s[x]; n   int ptr1 = ceil(pow(x, 0.25)), ptr2 = floor(pow(x, 0.5)); n   int ret = 0; n   while (ps[ptr2][ret] - ps[ptr1 - 1][ret] > 0) ret++; n   return ret; n } n int main() { n   prep(); n   scanf( %d , &n); n   for (int i = (1); i <= (n); i++) { n     long long x; n     scanf( %lld , &x); n     ans ^= f(x); n   } n   if (ans) n     printf( Furlo n ); n   else n     printf( Rublo n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename A, size_t N, typename T> n void Fill(A (&array)[N], const T &val) { n   std::fill((T *)array, (T *)(array + N), val); n } n template <class T> n inline bool chmax(T &a, T b) { n   if (a < b) { n     a = b; n     return true; n   } n   return false; n } n template <class T> n inline bool chmin(T &a, T b) { n   if (a > b) { n     a = b; n     return true; n   } n   return false; n } n int mi = 1000000007; n map<pair<int, int>, int> mp; n int minima[2000000]; n int f(int a, int b) { n   if (mp.find(make_pair(a, b)) != mp.end()) { n     return mp[make_pair(a, b)]; n   } n   cout <<  ?   << a <<     << b << endl; n   int s; n   cin >> s; n   chmin(mi, s); n   return mp[make_pair(a, b)] = s; n } n void SMAWK(vector<int> js, int ib, int ie, int id) { n   if (ib > ie) return; n   vector<int> js2; n   for (int q = 0, i = ib; q < js.size(); ++q) { n     while (!js2.empty() && f(i, js2.back()) >= f(i, js[q])) { n       js2.pop_back(); n       i -= id; n     } n     if (js2.size() != (ie - ib) / id) { n       js2.push_back(js[q]); n       i += id; n     } n   } n   SMAWK(js2, ib + id, ie, id * 2); n   for (int i = ib, q = 0; i <= ie; i += id * 2) { n     int jt = (i + id <= ie ? minima[i + id] : js.back()); n     int fm = 999999999 * 2, fq; n     for (; q < js.size(); ++q) { n       if (fm > (fq = f(i, js[q]))) { n         fm = fq; n         minima[i] = js[q]; n       } n       if (js[q] == jt) break; n     } n   } n } n void rowMinimaTM(int ib, int ie, int jb, int je) { n   vector<int> js; n   for (int j = jb; j <= je; ++j) js.push_back(j); n   SMAWK(js, ib, ie, 1); n } n int main() { n   int n, m; n   cin >> n >> m; n   rowMinimaTM(1, n, 1, m); n   cout <<  !   << mi << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int maxn = 2e5 + 5; n long long int sz[maxn], dp[maxn], ans[maxn]; n vector<long long int> g[maxn]; n long long int n; n long long int dp1(long long int node, long long int parent) { n   sz[node] = 1; n   for (auto& itr : g[node]) n     if (itr != parent) sz[node] += dp1(itr, node); n   return sz[node]; n } n long long int dp2(long long int node, long long int parent) { n   dp[node] = sz[node]; n   for (auto& itr : g[node]) n     if (itr != parent) dp[node] += dp2(itr, node); n   return dp[node]; n } n void dp3(long long int node, long long int parent, n          long long int fromParent = 0) { n   if (parent == -1) n     ans[node] = dp[node]; n   else n     ans[node] = n - 2 * sz[node] + ans[parent]; n   for (auto& itr : g[node]) n     if (itr != parent) dp3(itr, node); n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cin >> n; n   for (long long int i = 1; i < n; i++) { n     long long int u, v; n     cin >> u >> v; n     g[u].push_back(v); n     g[v].push_back(u); n   } n   dp1(1, -1); n   dp2(1, -1); n   dp3(1, -1); n   long long int answer = 0; n   for (long long int i = 1; i <= n; i++) answer = max(answer, ans[i]); n   cout << answer; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int a[16] = {0}; n   int index = 0; n   for (int i = 1; i <= n; i++) { n     string s = to_string(i); n     bool lucky = true; n     for (int j = 0; j < (s.length()); j++) { n       if (s[j] !=  4  && s[j] !=  7 ) { n         lucky = false; n         break; n       } n     } n     if (lucky) { n       a[index] = i; n       index++; n     } n   } n   for (int i = 0; i < 16; i++) { n     if (a[i] != 0) { n       if (n % a[i] == 0) { n         cout <<  YES ; n         return 0; n       } n     } n   } n   cout <<  NO ; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   set<long long> ss; n   for (int i = 1; i <= 1e6; i++) { n     long long need = i * 2; n     long long cards = (i * (i + 1)) / 2; n     long long lft = cards - i; n     need += lft * 3; n     if (need > 1e9) break; n     ss.insert(-need); n   } n   while (t--) { n     long long n; n     cin >> n; n     long long cnt = 0; n     while (true) { n       set<long long>::iterator it = ss.lower_bound(-n); n       if (it == ss.end()) break; n       cnt++; n       n += *it; n     } n     cout << cnt <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct edge { n   int v, nxt, c; n } e[1505 * 1505 * 2]; n int p[1505], eid; n void init() { n   memset(p, -1, sizeof p); n   eid = 0; n } n void insert(int u, int v, int c) { n   e[eid].v = v; n   e[eid].c = c; n   e[eid].nxt = p[u]; n   p[u] = eid++; n } n void add(int u, int v, int c) { n   insert(u, v, c); n   insert(v, u, 0); n } n int ch[10000005][2], cnt[10000005], tot, ys[1505], nxt[10000005], g[755][755], n     L[1505], R[1505], ha[10000005]; n string st; n queue<int> q; n void insert(int id) { n   int p = 0, len = st.length(); n   L[id] = ha[0] + 1; n   for (int i = 0; i < len; i++) { n     ha[++ha[0]] = st[i] -  a ; n     if (!ch[p][st[i] -  a ]) ch[p][st[i] -  a ] = ++tot; n     p = ch[p][st[i] -  a ]; n   } n   cnt[p] = id; n   R[id] = ha[0]; n } n void build() { n   for (int i = 0; i < 2; i++) n     if (ch[0][i]) q.push(ch[0][i]); n   while (q.size()) { n     int u = q.front(); n     q.pop(); n     for (int i = 0; i < 2; i++) { n       if (!ch[u][i]) n         ch[u][i] = ch[nxt[u]][i]; n       else n         nxt[ch[u][i]] = ch[nxt[u]][i], q.push(ch[u][i]); n     } n   } n } n int vis[10000005]; n void run(int id) { n   int p = 0; n   vis[0] = 1; n   for (int i = L[id]; i <= R[id]; i++) { n     p = ch[p][ha[i]]; n     int pp = p, tt; n     while (pp) { n       if (cnt[pp] && cnt[pp] != id) { n         g[id][cnt[pp]] = 1; n         break; n       } n       pp = nxt[pp]; n     } n     int haha = pp; n     pp = p; n     while (pp) { n       if (cnt[pp] && cnt[pp] != id) { n         break; n       } n       tt = pp; n       pp = nxt[pp]; n       nxt[tt] = haha; n     } n   } n } n int dis[1505], S, T, n; n int bfs() { n   memset(dis, -1, sizeof dis); n   dis[S] = 1; n   q.push(S); n   while (q.size()) { n     int u = q.front(); n     q.pop(); n     for (int i = p[u]; i + 1; i = e[i].nxt) { n       int v = e[i].v; n       if (dis[v] == -1 && e[i].c) { n         dis[v] = dis[u] + 1; n         q.push(v); n       } n     } n   } n   return dis[T] != -1; n } n int dfs(int u, int flow) { n   if (u == T) return flow; n   int ret = 0; n   for (int i = p[u]; i + 1; i = e[i].nxt) { n     int c = e[i].c, v = e[i].v; n     if (c && dis[v] == dis[u] + 1) { n       int tmp = dfs(v, min(flow, c)); n       ret += tmp, flow -= tmp; n       e[i].c -= tmp, e[i ^ 1].c += tmp; n       if (!flow) break; n     } n   } n   if (!ret) dis[u] = -1; n   return ret; n } n int Dinic() { n   int ret = 0; n   for (; bfs();) ret += dfs(S, 2147483646 / 2); n   return ret; n } n int main() { n   ios::sync_with_stdio(false); n   init(); n   cin >> n; n   T = 2 * n + 1; n   for (int i = 1; i <= n; i++) cin >> st, insert(i); n   build(); n   for (int i = 1; i <= n; i++) run(i); n   for (int k = 1; k <= n; k++) n     for (int i = 1; i <= n; i++) n       for (int j = 1; j <= n; j++) g[i][j] |= (g[i][k] & g[k][j]); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) n       if (g[i][j] && i != j) add(i, j + n, 1); n   for (int i = 1; i <= n; i++) add(S, i, 1), add(i + n, T, 1), vis[i + n] = 0; n   cout << n - Dinic() << endl; n   for (int i = 1; i <= n; i++) n     if (dis[i] != -1 && dis[i + n] == -1) cout << i <<    ; n   return 0; n } n


#include <bits/stdc++.h> n const int MAXN = 1e5 + 5; n enum { GRASS =  G , WATER =  W , LAVA =  L  }; n enum { G = 5, W = 3, L = 1 }; n long long a[MAXN]; n char s[MAXN]; n int n; n bool water; n long long grass, stamina, t; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) { n     scanf( %lld , &a[i]); n   } n   scanf(  %s , s); n   water = false; n   grass = stamina = t = 0LL; n   for (int i = 0; i < n; ++i) { n     switch (s[i]) { n       case GRASS: n         grass += 2 * a[i]; n         stamina += 2 * a[i]; n         grass = std::min(stamina / 2, grass); n         t += G * a[i]; n         break; n       case WATER: n         water = true; n         stamina += 2 * a[i]; n         t += W * a[i]; n         break; n       case LAVA: n         if (stamina < 2 * a[i]) { n           t += ((water) ? W : G) * (a[i] - stamina / 2); n           stamina = 0; n         } else { n           stamina -= 2 * a[i]; n         } n         t += L * a[i]; n         grass = std::min(stamina / 2, grass); n         break; n     } n   } n   t -= (G - L) / 2 * grass; n   t -= (W - L) / 2 * (stamina / 2 - grass); n   printf( %lld n , t); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n vector<vector<int>> g; n vector<int> w; n vector<int> dp; n void dfs(int node, int p) { n   dp[node] = w[node]; n   for (int u : g[node]) { n     if (u != p) { n       dfs(u, node); n       dp[node] += dp[u]; n     } n   } n } n int main() { n   cin >> n; n   int u, v, t; n   g.assign(n + 1, vector<int>()); n   w.assign(n + 1, 0); n   dp.assign(n + 1, 0); n   vector<int> ans; n   for (int i = 1; i < n; i++) { n     cin >> u >> v >> t; n     g[u].push_back(v); n     g[v].push_back(u); n     if (t == 2) { n       w[u] = 1; n       w[v] = 1; n     } n   } n   dfs(1, 0); n   for (int i = 1; i <= n; i++) { n     if (dp[i] == 1 && w[i] == 1) ans.push_back(i); n   } n   cout << ans.size() << endl; n   for (auto u : ans) cout << u <<    ; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n vector<int> a; n vector<int> b; n int n; n void get_input() { cin >> n; } n void solve() { n   for (int i = 0; i < n; i++) { n     int kom; n     cin >> kom; n     a.push_back(kom); n     b.push_back(kom); n   } n   sort(b.begin(), b.begin() + n); n   int cnt = 0; n   for (int i = 0; i < n; i++) { n     if (b[i] != a[i]) { n       cnt++; n     } n   } n   cout << (cnt > 2 ?  NO  :  YES ); n } n void output() {} n int main() { n   ios ::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   int t = 1; n   while (t--) { n     get_input(), solve(), output(); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /stack:200000000 ) n #pragma GCC optimize( Ofast ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) n #pragma GCC optimize( unroll-loops ) n using namespace std; n const double pi = n     3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342; n int main() { n   int n, k; n   scanf( %d%d , &n, &k); n   int v = 2 * k + 1; n   if (n <= k + 1) n     printf( 1 n1 n ); n   else if (n % v == 0) { n     printf( %d n , n / v); n     int ile = 0, pos = k + 1; n     while (ile < n) { n       printf( %d  , pos); n       pos += v; n       ile += v; n     } n     printf(  n ); n   } else if (n % v > k) { n     int r = n % v; n     int pos = 1 + r - (k + 1); n     int ile = r; n     printf( %d n%d  , n / v + 1, pos); n     pos = ile + k + 1; n     while (ile < n) { n       printf( %d  , pos); n       pos += v; n       ile += v; n     } n     printf(  n ); n   } else { n     int res = n * n; n     for (int i = (k + 1); i <= (v); ++i) n       for (int j = (k + 1); j <= (v); ++j) n         if ((n - i - j) >= 0 && (n - i - j) % v == 0) n           res = min(res, 2 + (n - i - j) / v); n     printf( %d n , res); n     for (int i = (k + 1); i <= (v); ++i) n       for (int j = (k + 1); j <= (v); ++j) n         if ((n - i - j) >= 0 && (n - i - j) % v == 0 && n             2 + (n - i - j) / v == res) { n           int pos = 1 + i - (k + 1); n           int ile = i; n           printf( %d %d  , pos, n - (j - (k + 1))); n           n -= j; n           pos = ile + k + 1; n           while (ile < n) { n             printf( %d  , pos); n             pos += v; n             ile += v; n           } n           printf(  n ); n           return 0; n         } n   } n   return 0; n } n


#include <bits/stdc++.h> n const int mod = 1000000007; n struct uinon_set { n   int father[100003], n, value[100003]; n   int ans; n   void init() { n     ans = 0; n     for (int i = 1; i <= n; i++) { n       father[i] = i; n       value[i] = 0; n     } n     return; n   } n   int getfather(int i) { n     if (father[i] == i) return i; n     int tans = getfather(father[i]); n     value[i] = (value[i] + value[father[i]]) % mod; n     return father[i] = tans; n   } n   void insert(int root, int i, int xi) { n     int temp, tmp = getfather(root); n     temp = (value[root] + xi) % mod; n     father[tmp] = i; n     value[tmp] = temp; n     ans = (ans + temp) % mod; n     return; n   } n } set; n int main() { n   int n, t, x, y, i, j; n   scanf( %d , &n); n   set.n = n; n   set.init(); n   for (i = 1; i <= n; i++) { n     scanf( %d , &t); n     for (j = 0; j < t; j++) { n       scanf( %d%d , &x, &y); n       set.insert(x, i, y); n     } n   } n   printf( %d n , (set.ans + mod) % mod); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MAXN = 5e5 + 5; n bool vis[MAXN]; n long long sp[MAXN]; n long long f[MAXN]; n bool is[MAXN]; n long long ar[MAXN]; n long long POWER[25]; n long long F[25]; n void sieve() { n   for (long long i = 0; i < MAXN; i++) vis[i] = false, sp[i] = 2; n   for (long long i = 3; i < MAXN; i += 2) { n     if (!vis[i]) { n       sp[i] = i; n       for (long long j = i * i; j < MAXN; j += 2 * i) { n         vis[j] = true; n         sp[j] = i; n       } n     } n   } n } n int main() { n   long long n, q; n   cin >> n >> q; n   for (long long i = 1; i <= n; i++) scanf( %lld , &ar[i]); n   sieve(); n   long long added = 0; n   POWER[0] = 1; n   for (long long i = 1; i < 15; i++) POWER[i] = POWER[i - 1] << 1; n   long long ans = 0; n   while (q--) { n     long long x; n     long long temp = 0; n     scanf( %lld , &x); n     long long cc = 0; n     long long val = ar[x]; n     while (val != 1) { n       long long div = sp[val]; n       F[cc++] = div; n       while (val % div == 0) val /= div; n     } n     for (long long i = 1; i < POWER[cc]; i++) { n       long long p = 1; n       long long counter = 0; n       for (long long j = 0; j < cc; j++) n         if (POWER[j] & i) counter++, p *= F[j]; n       if (counter & 1) n         temp += f[p]; n       else n         temp -= f[p]; n     } n     if (is[x]) { n       ans -= added - temp; n       if (ar[x] == 1) ans++; n       is[x] = false; n       for (long long i = 1; i < POWER[cc]; i++) { n         long long p = 1; n         for (long long j = 0; j < cc; j++) n           if (POWER[j] & i) p *= F[j]; n         f[p]--; n       } n       added--; n     } else { n       ans += added - temp; n       is[x] = true; n       for (long long i = 1; i < POWER[cc]; i++) { n         long long p = 1; n         for (long long j = 0; j < cc; j++) n           if (POWER[j] & i) p *= F[j]; n         f[p]++; n       } n       added++; n     } n     printf( %lld n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int x, y; n   string s, t; n   cin >> x >> y >> s >> t; n   int arr[y - x + 1]; n   int mx = -1, res; n   for (int i = 0; i < y - x + 1; i++) { n     int k = i; n     arr[i] = 0; n     for (int j = 0; j < x; j++, k++) { n       if (t[k] == s[j]) arr[i] += 1; n     } n     if (mx < arr[i]) { n       mx = arr[i]; n       res = i; n     } n   } n   cout << x - mx << endl; n   for (int i = res, j = 0; j < x; i++, j++) { n     if (t[i] != s[j]) cout << j + 1 <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k, m; n   cin >> n >> k; n   m = n * (n - 1) / 2; n   if ((double)k > ((double)n - 1) / 2) { n     cout << -1; n     return 0; n   } else { n     cout << k * n << endl; n     for (int i = 1; i <= n; ++i) { n       for (int j = 0; j < k; ++j) { n         cout << i <<     << (i + j) % n + 1 << endl; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const long long inf = 0x3f3f3f3f3f3f3f3f; n const int maxn = 1005; n int main() { n   int N; n   cin >> N; n   cout << 2 * N - 1 <<     << 2 << endl; n   cout << 1 <<     << 2 << endl; 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.tie(0); n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     vector<pair<pair<int, int>, int> > a(2 * n - 1); n     for (int i = 0; i < 2 * n - 1; i++) { n       cin >> a[i].first.first; n       cin >> a[i].first.second; n       a[i].second = i + 1; n     } n     sort(a.begin(), a.end()); n     long long s = 0, s1 = 0, tt = 0, tt1 = 0; n     for (int i = 0; i < 2 * n - 1; i++) { n       if (i % 2 == 0) { n         s += a[i].first.first; n         s1 += a[i].first.second; n       } n       tt += a[i].first.first; n       tt1 += a[i].first.second; n     } n     tt++; n     tt1++; n     tt /= 2; n     tt1 /= 2; n     cout <<  YES  << endl; n     if (s >= tt && s1 >= tt1) { n       for (int i = 0; i < 2 * n - 1; i++) { n         if (i % 2 == 0) cout << a[i].second <<    ; n       } n       cout << endl; n     } else { n       for (int i = 0; i < 2 * n - 1; i++) { n         if (i % 2 == 1) { n           cout << a[i].second <<    ; n         } n       } n       cout << a[2 * n - 2].second << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T1> n void print_list(const T1& a) { n   cerr << a << endl; n } n template <typename T1, typename T2> n void print_list(const T1& a, const T2& b) { n   cerr << a <<     << b << endl; n } n template <typename T1, typename T2, typename T3> n void print_list(const T1& a, const T2& b, const T3& c) { n   cerr << a <<     << b <<     << c << endl; n } n template <typename T1, typename T2, typename T3, typename T4> n void print_list(const T1& a, const T2& b, const T3& c, const T4& d) { n   cerr << a <<     << b <<     << c <<     << d << endl; n } n template <typename T1, typename T2, typename T3, typename T4, typename T5> n void print_list(const T1& a, const T2& b, const T3& c, const T4& d, n                 const T5& e) { n   cerr << a <<     << b <<     << c <<     << d <<     << e << endl; n } n template <typename T1, typename T2, typename T3, typename T4, typename T5, n           typename T6> n void print_list(const T1& a, const T2& b, const T3& c, const T4& d, const T5& e, n                 const T6& f) { n   cerr << a <<     << b <<     << c <<     << d <<     << e <<     << f << endl; n } n template <typename T1, typename T2, typename T3, typename T4, typename T5, n           typename T6, typename T7> n void print_list(const T1& a, const T2& b, const T3& c, const T4& d, const T5& e, n                 const T6& f, const T7& g) { n   cerr << a <<     << b <<     << c <<     << d <<     << e <<     << f <<     n        << g << endl; n } n template <typename A, typename B> n ostream& operator<<(ostream& out, const pair<A, B>& pp) { n   out <<  (  << pp.first <<  ,  << pp.second <<  ) ; n   return out; n } n template <typename A, typename B> n istream& operator<<(istream& in, pair<A, B>& pp) { n   cerr <<  A pair wanted  << endl; n   in >> pp.first >> pp.second; n   return in; n } n template <typename T> n ostream& operator<<(ostream& out, const vector<T>& vect) { n   out <<  length =   << vect.size() << endl; n   for (int i = 0; i < vect.size(); i++) out << vect[i] <<    ; n   out << endl; n   return out; n } n template <typename T> n istream& operator>>(istream& in, vector<T>& vect) { n   vect.clear(); n   int n; n   cerr <<  A integer of length wanted  << endl; n   in >> n; n   vect.resize(n); n   cerr << n <<   elements wanted  << endl; n   for (int i = 0; i < n; i++) in >> vect[i]; n   return in; n } n template <typename T> n ostream& operator<<(ostream& out, const vector<vector<T> >& vect) { n   out <<  row number=  << vect.size() << endl; n   for (int i = 0; i < vect.size(); i++) { n     out <<  row #  << i <<  : ; n     for (int j = 0; j < vect[i].size(); j++) out <<     << vect[i][j]; n     out << endl; n   } n   return out; n } n template <typename T> n istream& operator>>(istream& in, vector<vector<T> >& vect) { n   vect.clear(); n   int n, m; n   cerr <<  Two integers wnated  << endl; n   in >> n >> m; n   vect.resize(n); n   cerr <<  A matrix   << n <<   X   << m <<   wanted  << endl; n   for (int i = 0; i < n; i++) { n     vect[i].resize(m); n     for (int j = 0; j < m; j++) in >> vect[i][j]; n   } n   return in; n } n template <typename T> n inline void updateMax(T& a, const T& b) { n   a = max(a, b); n } n template <typename T> n inline void updateMin(T& a, const T& b) { n   a = min(a, b); n } n template <typename T> n inline vector<T> erase(vector<T> table, int ind) { n   assert(ind < table.size()); n   table.erase(table.begin() + ind); n   return table; n } n template <typename T> n vector<T> unique(vector<T> table) { n   sort(table.begin(), table.end()); n   return vector<T>(table.begin(), unique(table.begin(), table.end())); n } n template <class T> n vector<T> parse(const string& ss, const char* cut =    ) { n   vector<T> re; n   for (int j = 0; j < ss.size(); j++) { n     string s; n     while (j < ss.size() && NULL == strchr(cut, ss[j])) s += ss[j++]; n     if (!s.empty()) { n       T tmp; n       istringstream is(s); n       is >> tmp; n       re.push_back(tmp); n     } n   } n   return re; n } n int countBit(int n) { n   int re = 0; n   while (n) re++, n ^= ((((n) ^ ((n)-1)) & n)); n   return re; n } n int MSB(int n) { n   if (n == 0) return 0; n   while (((((n) ^ ((n)-1)) & n)) != n) n ^= ((((n) ^ ((n)-1)) & n)); n   return n; n } n void initHash(int n, int hash[]) { n   for (int i = 0; i < n; i++) hash[((1) << (i))] = i; n } n void initBcnt(int n, int bcnt[]) { n   bcnt[0] = 0; n   for (int i = 1; i < ((1) << (n)); i++) n     bcnt[i] = bcnt[i ^ ((((i) ^ ((i)-1)) & i))] + 1; n } n template <typename T> n T __gcd(T n, T m, T& a, T& b) { n   T a1 = 0, b1 = 1; n   a = 1, b = 0; n   while (m) { n     T c = n / m; n     T r = n - m * c; n     T t; n     t = a; n     a = a1; n     a1 = t - c * a1; n     t = b; n     b = b1; n     b1 = t - c * b1; n     n = m; n     m = r; n   } n   return n; n } n struct Node { n   int r, c, d; n   int v; n   Node(int x, int y, int z, int v1) : r(x), c(y), d(z), v(v1){}; n   bool operator<(const Node& other) const { return v > other.v; }; n }; n priority_queue<Node> heap; n const int dir[][2] = {{-1, 0}, {0, 1},  {1, 0},  {0, -1}, n                       {1, 1},  {1, -1}, {-1, 1}, {-1, -1}}; n const char dname[] =  NWSE ; n const string vv[] = { ......... ,  ....O.... ,  ..O...O.. ,  ..O.O.O.. , n                       O.O...O.O ,  O.O.O.O.O ,  O.OO.OO.O }; n const string hh[] = { ......... ,  ....O.... ,  O.......O ,  O...O...O , n                       O.O...O.O ,  O.O.O.O.O ,  OOO...OOO }; n char grid[2000][2000]; n int dom[1000][1000]; n long long int col1[1000], col2[1000]; n long long int dp[1000]; n const long long int mod = 1000000007; n long long int buf[1000]; n int main() { n   map<string, int> mem; n   for (int i = 0; i < 7; i++) { n     mem[vv[i]] |= 1; n     mem[hh[i]] |= 2; n   } n   int n, m; n   scanf( %d %d , &n, &m); n   for (int i = 0; i < 4 * n + 1; i++) { n     scanf( %s , grid[i]); n   } n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) { n       int x = 4 * i + 1; n       int y = 4 * j + 1; n       string s(9,  . ); n       for (int a = 0; a < 3; a++) n         for (int b = 0; b < 3; b++) { n           s[a * 3 + b] = grid[x + a][y + b]; n         } n       assert(mem.count(s)); n       dom[i][j] = mem[s]; n     } n   for (int i = 0; i < m; i++) { n     col1[i] = 1 - (n & 1); n     for (int j = 1; j < n && col1[i]; j += 2) { n       if (0 == (dom[j][i] & 1) || 0 == (dom[j - 1][i] & 1)) { n         col1[i]--; n         break; n       } n     } n   } n   for (int i = 0; i + 1 < m; i++) { n     dp[0] = 1; n     for (int j = 1; j <= n; j++) { n       dp[j] = 0; n       if ((dom[j - 1][i] & 2) && (dom[j - 1][i + 1] & 2)) { n         dp[j] += dp[j - 1]; n       } n       if (j >= 2 && (dom[j - 1][i] & 1) && (dom[j - 2][i] & 1) && n           (dom[j - 1][i + 1] & 1) && (dom[j - 2][i + 1] & 1)) { n         dp[j] += dp[j - 2]; n       } n       dp[j] %= mod; n     } n     col2[i] = dp[n]; n   } n   dp[0] = 1; n   for (int i = 1; i <= m; i++) { n     dp[i] = 0; n     dp[i] += col1[i - 1] * dp[i - 1]; n     if (i >= 2) { n       dp[i] += (col2[i - 2] + mod - col1[i - 1] * col1[i - 2]) * dp[i - 2]; n     } n     dp[i] %= mod; n   } n   cout << dp[m] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, k; n char s[1005], ans[1005]; n map<long long, bool> dp[1005]; n map<long long, char> pre[1005]; n inline long long read() { n   long long q = 0, f = 1; n   char ch = getchar(); n   while (!isdigit(ch)) { n     if (ch ==  - ) f = 0; n     ch = getchar(); n   } n   while (isdigit(ch)) q = (q << 3) + (q << 1) + (ch ^ 48), ch = getchar(); n   return f ? q : -q; n } n inline void print(long long x) { n   if (x < 0) putchar( - ), x = -x; n   if (x > 9) print(x / 10); n   putchar(x % 10 + 48); n } n signed main() { n   n = read(), k = read(), scanf( %s , s + 1), dp[1][0] = true; n   for (long long i = 2; i <= n + 1; ++i) n     for (long long j = -k + 1; j < k; ++j) n       if (dp[i - 1][j]) { n         if (!(s[i - 1] ^  L )) n           pre[i][j - 1] =  L , dp[i][j - 1] = true; n         else if (!(s[i - 1] ^  W )) n           pre[i][j + 1] =  W , dp[i][j + 1] = true; n         else if (!(s[i - 1] ^  D )) n           pre[i][j] =  D , dp[i][j] = true; n         else n           pre[i][j + 1] =  W , pre[i][j] =  D , pre[i][j - 1] =  L , n                      dp[i][j + 1] = dp[i][j] = dp[i][j - 1] = true; n       } n   if (dp[n + 1][k] | dp[n + 1][-k]) { n     long long j = dp[n + 1][k] ? k : -k; n     for (long long i = n + 1; i > 1; --i) { n       char ch = pre[i][j]; n       ans[i - 1] = ch, ((!(ch ^  W )) ? --j : ((!(ch ^  L )) ? ++j : i)); n     } n     for (long long i = 1; i <= n; ++i) putchar(ans[i]); n   } else { n     putchar( N ), putchar( O ); n     return 0; n   }; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long modpow(long long a, long long b) { n   long long ans = 1; n   while (b) { n     if (b & 1) { n       ans = (ans * a) % 1000000007; n     } n     a = (a * a) % 1000000007; n     b = b / 2; n   } n   return ans; n } n int main() { n   cin.tie(0); n   ios::sync_with_stdio(false); n   int t; n   cin >> t; n   while (t--) { n     long long x, y; n     cin >> x >> y; n     if (x - y == 1) { n       cout <<  NO  << endl; n     } else n       cout <<  YES  << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n list<int> l[100005]; n list<int> son[100005]; n int n, dad[100005], x, y, val[100005], res; n void f(int a) { n   for (list<int>::iterator it = l[a].begin(); it != l[a].end(); it++) n     if ((*it) != dad[a]) { n       dad[*it] = a; n       f(*it); n       son[a].push_back(*it); n     } n } n pair<long long int, long long int> calc(int a) { n   pair<long long int, long long int> ret = n       pair<long long int, long long int>(0, 0); n   pair<long long int, long long int> tmp; n   for (list<int>::iterator it = son[a].begin(); it != son[a].end(); it++) n     if (*it != dad[a]) { n       tmp = calc(*it); n       ret = pair<long long int, long long int>(max(tmp.first, ret.first), n                                                max(tmp.second, ret.second)); n     } n   int change = ret.first - ret.second; n   if (val[a] + change > 0) n     ret.second += abs(val[a] + change); n   else n     ret.first += abs(val[a] + change); n   return ret; n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n - 1; i++) { n     scanf( %d %d , &x, &y); n     l[x].push_back(y); n     l[y].push_back(x); n   } n   for (int i = 1; i <= n; i++) scanf( %d , &val[i]); n   f(1); n   pair<long long int, long long int> res = calc(1); n   cout << res.first + res.second << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   if (fopen( input.txt ,  r )) { n     freopen( input.txt ,  r , stdin); n     freopen( output.txt ,  w , stdout); n   } n   int n, a, b; n   cin >> n; n   bool visit[1000005] = {false}; n   int ans = -1; n   while (n--) { n     cin >> a >> b; n     while (visit[a] || a < ans) { n       a += b; n     } n     ans = max(ans, a); n     visit[a] = true; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n T gcd(T a, T b) { n   if (a == 0) return b; n   return gcd(b % a, a); n } n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   int tt; n   cin >> tt; n   while (tt--) { n     int n; n     int diff = 0; n     cin >> n; n     int a[2 * n]; n     for (int i = 0; i < 2 * n; i++) { n       cin >> a[i]; n       if (a[i] & 1) n         diff++; n       else n         diff--; n     } n     if (diff == 0) { n       cout << 0 <<   n ; n       continue; n     } n     map<int, int> mp; n     mp[0] = 0; n     int cnt = 0; n     for (int i = n; i < 2 * n; i++) { n       if (a[i] & 1) n         cnt++; n       else n         cnt--; n       if (mp.find(cnt) == mp.end()) mp[cnt] = i - (n - 1); n     } n     cnt = 0; n     int ans; n     if (mp.find(diff) == mp.end()) n       ans = 2 * n; n     else n       ans = mp[diff]; n     for (int i = n - 1; i >= 0; i--) { n       if (a[i] & 1) n         cnt++; n       else n         cnt--; n       if (mp.find(diff - cnt) != mp.end()) { n         ans = min(ans, mp[(diff - cnt)] + n - i); n       } n     } n     cout << ans <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T, typename U> n inline void amin(T &x, U y) { n   if (y < x) x = y; n } n template <typename T, typename U> n inline void amax(T &x, U y) { n   if (x < y) x = y; n } n int main() { n   int n, p, k, ai; n   cin >> n >> k >> p; n   vector<int> ods; n   vector<int> evns; n   for (int i = 0; i < n; i++) { n     cin >> ai; n     if (ai % 2 == 0) n       evns.push_back(ai); n     else n       ods.push_back(ai); n   } n   int rem = k - p; n   vector<vector<int> > ans; n   for (int i = 0; i < p; i++) { n     if (evns.size()) { n       vector<int> tmp; n       tmp.push_back(evns.back()); n       ans.push_back(tmp); n       evns.pop_back(); n     } else if (ods.size() >= 2) { n       vector<int> tmp; n       tmp.push_back(ods.back()); n       ods.pop_back(); n       tmp.push_back(ods.back()); n       ods.pop_back(); n       ans.push_back(tmp); n     } else { n       cout <<  NO ; n       return 0; n     } n   } n   for (int i = 0; i < rem; i++) { n     if (ods.size()) { n       vector<int> tmp; n       tmp.push_back(ods.back()); n       ans.push_back(tmp); n       ods.pop_back(); n     } else { n       cout <<  NO ; n       return 0; n     } n   } n   if (ods.size() % 2 == 1) { n     cout <<  NO ; n     return 0; n   } n   if (k == 1) { n     cout <<  YES  << endl; n     int m = ans[0].size() + ods.size() + evns.size(); n     cout << m <<    ; n     for (int j = 0; j < ans[0].size(); j++) cout << ans[0][j] <<    ; n     for (int j = 0; j < ods.size(); j++) cout << ods[j] <<    ; n     for (int j = 0; j < evns.size(); j++) cout << evns[j] <<    ; n     return 0; n   } n   cout <<  YES  << endl; n   for (int i = 0; i < ans.size(); i++) { n     if (ods.size()) { n       int m = ans[i].size() + ods.size(); n       cout << m <<    ; n       for (int j = 0; j < ans[i].size(); j++) cout << ans[i][j] <<    ; n       for (int j = 0; j < ods.size(); j++) cout << ods[j] <<    ; n       ods.clear(); n       cout << endl; n     } else if (evns.size()) { n       int m = ans[i].size() + evns.size(); n       cout << m <<    ; n       for (int j = 0; j < ans[i].size(); j++) cout << ans[i][j] <<    ; n       for (int j = 0; j < evns.size(); j++) cout << evns[j] <<    ; n       evns.clear(); n       cout << endl; n     } else { n       cout << ans[i].size() <<    ; n       for (int j = 0; j < ans[i].size(); j++) cout << ans[i][j] <<    ; n       cout << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int fa[101000][20], h[101000], lev[101000], ans[101000]; n int dp[101000]; n struct EDGE { n   int to, nxt; n } box[2 * 101000]; n int top, K; n void add(int u, int v) { n   box[top].to = v; n   box[top].nxt = h[u]; n   h[u] = top++; n } n void DFS(int u, int pre, int t) { n   lev[u] = lev[pre] + 1; n   fa[u][0] = pre; n   if (!t) n     for (int i = 1; i < 20; i++) fa[u][i] = fa[fa[u][i - 1]][i - 1]; n   for (int p = h[u]; p != -1; p = box[p].nxt) { n     int v = box[p].to; n     if (pre == v) continue; n     DFS(v, u, t); n     if (t) { n       dp[u] += dp[v]; n       ans[p / 2] = dp[v]; n     } n   } n } n int lca(int u, int v) { n   if (lev[u] < lev[v]) swap(u, v); n   int c = lev[u] - lev[v]; n   for (int i = 19; i >= 0; i--) { n     if (c & (1 << i)) { n       u = fa[u][i]; n       c -= (1 << i); n     } n   } n   if (u == v) return u; n   for (int i = 19; i >= 0; i--) { n     if (fa[u][i] != fa[v][i]) { n       u = fa[u][i]; n       v = fa[v][i]; n     } n   } n   return fa[u][0]; n } n int main() { n   int n, k; n   int a, b; n   scanf( %d , &n); n   memset(h, -1, sizeof(h)); n   for (int i = 1; i < n; i++) { n     scanf( %d%d , &a, &b); n     add(a, b); n     add(b, a); n   } n   DFS(1, 1, 0); n   scanf( %d , &k); n   for (int i = 0; i < k; i++) { n     scanf( %d%d , &a, &b); n     int c = lca(a, b); n     dp[a]++; n     dp[b]++; n     dp[c] -= 2; n   } n   DFS(1, 1, 1); n   for (int i = 0; i < n - 1; i++) { n     printf( %d%c , ans[i], i == n - 2 ?   n  :    ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n long long extgcd(long long a, long long b, long long& x, long long& y) { n   if (b == 0) { n     x = 1; n     y = 0; n     return a; n   } n   long long x1, y1; n   long long d = extgcd(b, a % b, x1, y1); n   x = y1; n   y = x1 - y1 * (a / b); n   return d; n } n bool isPrime(long long n) { n   if (n <= 1) return false; n   if (n <= 3) return true; n   if (n % 2 == 0 || n % 3 == 0) return false; n   for (long long i = 5; i * i <= n; i = i + 6) n     if (n % i == 0 || n % (i + 2) == 0) return false; n   return true; n } n bool isPowerOfTwo(long long n) { n   if (n == 0) return false; n   return (ceil(log2(n)) == floor(log2(n))); n } n long long powm(long long a, long long b) { n   long long res = 1; n   while (b) { n     if (b & 1) res = (res * a) % 1000000007; n     a = (a * a) % 1000000007; n     b >>= 1; n   } n   return res; n } n long long divide(long long a, long long b) { n   return (a % 1000000007) * powm(b, 1000000007 - 2) % 1000000007; n } n long long mul(long long a, long long b) { n   return ((a % 1000000007) * (b % 1000000007)) % 1000000007; n } n long long add(long long a, long long b) { n   return (a % 1000000007 + b % 1000000007) % 1000000007; n } n long long Totfun(long long n) { n   long long z = n; n   if (n % 2 == 0) { n     while (n % 2 == 0) { n       n /= 2; n     } n     z /= 2; n   } n   for (long long i = 3; i <= sqrt(n); i += 2) { n     if (isPrime(i) && n % i == 0) { n       while (n % i == 0) { n         n /= i; n       } n       z -= z / i; n     } n   } n   if (n > 1) { n     z -= z / n; n   } n   return z; n } n long long fact[100001]; n long long nCr(long long n, long long r) { n   if (r > n || r < 0) return 0; n   long long z = fact[n]; n   z = mul(z, powm(fact[n - r], 1000000007 - 2)); n   z = mul(z, powm(fact[r], 1000000007 - 2)); n   return z; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long n, a = 0, b = 0, c = 0, z = 1; n   cin >> n; n   string s; n   cin >> s; n   for (long long i = 0; i < n; i++) { n     if (s[i] ==  a ) n       a = add(a, z); n     else if (s[i] ==  b ) n       b = add(b, a); n     else if (s[i] ==  c ) n       c = add(c, b); n     else { n       c = add(3 * c, b); n       b = add(3 * b, a); n       a = add(3 * a, z); n       z = mul(z, 3); n     } n   } n   cout << c <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int n, ar[100001], max_s[100001], max_e[100001], pre_s[100001], n     pre_e[100001], ans = INT_MIN; n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) cin >> ar[i]; n   for (int i = 1; i < n; i++) pre_s[i] = pre_s[i - 1] + ar[i - 1]; n   for (int i = n - 2; i >= 0; i--) pre_e[i] = pre_e[i + 1] + ar[i + 1]; n   max_e[n - 1] = abs(ar[n - 1]); n   max_s[0] = abs(ar[0]); n   for (int i = n - 2; i >= 0; i--) n     max_e[i] = max(ar[i] + max_e[i + 1], -(ar[i] + pre_e[i])); n   for (int i = 1; i < n; i++) n     max_s[i] = max(ar[i] + max_s[i - 1], -(ar[i] + pre_s[i])); n   ans = max(ans, max_e[0]); n   ans = max(ans, max_s[n - 1]); n   for (int i = 0; i < n - 1; i++) ans = max(ans, max_s[i] + max_e[i + 1]); n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma warning(disable : 4996); n long long int n, h[1000007], d[1000007][2], mod = 1e9 + 7; n void solve() { n   long long int i, j; n   cin >> n; n   for (i = 0; i < n; i++) { n     cin >> h[i]; n     h[i] -= 1; n   } n   h[n] = 0; n   d[0][0] = max((long long int)0, h[0] - h[1]); n   d[0][1] = min(h[0], h[1]); n   for (i = 1; i < n; i++) { n     long long int k = min(h[i - 1], h[i]); n     d[i][0] = d[i - 1][1] * max((long long int)0, k - h[i + 1]); n     d[i][0] %= mod; n     d[i][0] = (d[i][0] + max((long long int)0, h[i] - h[i + 1])) % mod; n     d[i][1] = d[i - 1][1] * min(h[i + 1], k); n     d[i][1] %= mod; n     d[i][1] = (d[i][1] + min(h[i], h[i + 1])) % mod; n   } n   long long int ans = 0; n   for (i = 0; i < n; i++) { n     ans = (ans + d[i][0] + d[i][1]) % mod; n   } n   cout << ans; n } n int main() { n #pragma comment(linker,  /STACK:1073741824  n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int setBit(int n, int pos) { return n | (1 << pos); } n int resetBit(int n, int pos) { return n & ~(1 << pos); } n bool checkBit(int n, int pos) { return n & (1 << pos); } n inline long long bigmod(long long p, long long e, long long M) { n   long long ret = 1; n   while (e > 0) { n     if (e % 2) ret = (ret * p) % M; n     p = (p * p) % M; n     e /= 2; n   } n   return ret; n } n inline long long power(long long x, long long y) { n   long long ans = 1; n   while (y > 0) { n     if (y % 2) ans *= x; n     x *= x; n     y /= 2; n   } n   return ans; n } n template <class T> n T gcd(T a, T b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n template <class T> n T lcm(T a, T b) { n   return a * (b / gcd(a, b)); n } n int n, k, a, b, c, d; n int vis[1003]; n int main() { n   cin >> n >> k >> a >> b >> c >> d; n   if (k < n + 1 || n == 4) { n     cout << -1; n     return 0; n   } n   vis[a] = vis[b] = vis[c] = vis[d] = 1; n   printf( %d %d  , a, c); n   for (int i = 1; i <= n; i++) { n     if (vis[i] == 0) { n       printf( %d  , i); n     } n   } n   printf( %d %d n , d, b); n   printf( %d %d  , c, a); n   for (int i = 1; i <= n; i++) { n     if (vis[i] == 0) { n       printf( %d  , i); n     } n   } n   printf( %d %d , b, d); n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,avx ) n using namespace std; n int n, q, fa[66666], aa[66666], kh[66666], a[66666], p[66666], fp[66666], n     siz[66666], ck, pa[66666]; n vector<int> edge[66666]; n int dd[66666]; n void dfs(int u) { n   siz[u] = 1; n   p[++ck] = u; n   fp[u] = ck; n   pa[ck] = fp[fa[u]]; n   dd[ck] = dd[pa[ck]] + 1; n   for (auto v : edge[u]) { n     dfs(v); n     siz[u] += siz[v]; n   } n } n int main() { n   scanf( %d%d , &n, &q); n   for (int i = 1; i <= n; ++i) { n     scanf( %d , aa + i); n   } n   for (int i = 2; i <= n; ++i) { n     scanf( %d , fa + i); n     edge[fa[i]].push_back(i); n   } n   dfs(1); n   for (int i = 1; i <= n; ++i) { n     a[fp[i]] = aa[i]; n   } n   for (int i = 1; i <= q; ++i) { n     int x; n     scanf( %d , &x); n     int y = siz[x]; n     x = fp[x]; n     int pp = i + 1, gg = y >> 3, *jj = kh + x, *diao = dd + x, *aa = a + x; n     int ans = 0, cnt = 0; n     for (int j = 0; j ^ gg; ++j, jj += 8, diao += 8, aa += 8) { n       if (jj[0] < pp) ans += diao[0], jj[0] = i + aa[0], ++cnt; n       if (jj[1] < pp) ans += diao[1], jj[1] = i + aa[1], ++cnt; n       if (jj[2] < pp) ans += diao[2], jj[2] = i + aa[2], ++cnt; n       if (jj[3] < pp) ans += diao[3], jj[3] = i + aa[3], ++cnt; n       if (jj[4] < pp) ans += diao[4], jj[4] = i + aa[4], ++cnt; n       if (jj[5] < pp) ans += diao[5], jj[5] = i + aa[5], ++cnt; n       if (jj[6] < pp) ans += diao[6], jj[6] = i + aa[6], ++cnt; n       if (jj[7] < pp) ans += diao[7], jj[7] = i + aa[7], ++cnt; n     } n     gg <<= 3; n     for (int j = gg; j < y; ++j) { n       if (jj[j - gg] < pp) n         ans += diao[j - gg], jj[j - gg] = i + aa[j - gg], ++cnt; n       ; n     } n     ans = ans - cnt * dd[x]; n     printf( %d %d n , ans, cnt); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b; n   cin >> a >> b; n   for (int i = 1; i <= 1000000000; i++) { n     if (i % 2 == 1) { n       if (a >= i) { n         a -= i; n       } else { n         cout <<  Vladik ; n         return 0; n       } n     } else { n       if (b >= i) { n         b -= i; n       } else { n         cout <<  Valera ; n         return 0; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int f[4005][4005]; n int a[4005]; n int main() { n   int i, j, k, n, m = 0; n   memset(f, 0, sizeof(f)); n   cin >> n; n   for (i = 1; i <= n; i++) { n     cin >> a[i]; n     k = 0; n     for (j = 0; j < i; j++) { n       f[i][j] = f[j][k] + 1; n       if (a[j] == a[i]) { n         k = j; n       } n       m = max(m, f[i][j]); n     } n   } n   cout << m << endl; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long isqrt(long long k) { n   long long r = sqrt(k) + 1; n   while (r * r > k) r--; n   return r; n } n inline long long icbrt(long long k) { n   long long r = cbrt(k) + 1; n   while (r * r * r > k) r--; n   return r; n } n inline long long mnz(long long& a, long long b) { return a = (a > b ? b : a); } n inline long long mxz(long long& a, long long b) { return a = (a < b ? b : a); } n inline string toString(long long n) { n   stringstream ss; n   ss << n; n   return ss.str(); n } n double const eps = 1e-6; n long long const Base = 1e9 + 7, oo = 1e17, MAXN = 1e6; n long long A[MAXN + 5]; n vector<long long> dske[MAXN + 5]; n bitset<MAXN + 5> bi; n long long Solves() { n   long long n, m, k, cnt = 0, ans = 0, x = -1, y = -1, q, c, sum = 0, v, t = -1; n   cin >> n; n   for (long long i = 1; i <= n; i++) dske[i].clear(), bi[i] = 0; n   for (long long i = 1; i <= n; i++) { n     cin >> k; n     c = 0; n     while (k--) { n       cin >> x; n       if (!bi[x] and !c) c = x; n     } n     if (c) n       bi[c] = 1; n     else n       y = i; n   } n   if (y == -1) return cout <<  OPTIMAL  << endl, 0; n   vector<long long> vec; n   for (long long i = 1; i <= n; i++) n     if (!bi[i]) vec.push_back(i); n   map<long long, long long> mm; n   for (long long i = 0; i < dske[y].size(); i++) mm[dske[y][i]] = 1; n   for (long long i = 0; i < vec.size(); i++) { n     if (!mm[vec[i]]) { n       cout <<  IMPROVE  << endl; n       cout << y <<     << vec[i] << endl; n       return 0; n     } n   } n   assert(0); n   return 0; n } n int main() { n   if (fopen( locin.txt ,  r )) freopen( locin.txt ,  r , stdin); n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long test = 1; n   cin >> test; n   for (long long T = 1; T <= test; T++) { n     Solves(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   vector<string> res; n   for (int i = 0; i < t; ++i) { n     long long int a, b; n     cin >> a >> b; n     if (a > b) swap(a, b); n     if ((a + b) % 3 == 0 && 2 * a >= b) n       res.push_back( YES ); n     else n       res.push_back( NO ); n   } n   for (int i = 0; i < t; ++i) { n     cout << res[i] << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1000 + 10; n map<char, int> d, t; n string s; n string know[MAXN]; n int pd[MAXN]; n int n, m, ans, q, x; n int main() { n   cin >> n; n   cin >> s; n   for (int i = 0; i < s.size(); i++) { n     if (s[i] !=  * ) x++; n   } n   if (x == s.size()) { n     cout <<  0  << endl; n     return 0; n   } n   for (int i = 0; i < s.size(); i++) d[s[i]]++; n   cin >> m; n   for (int i = 1; i <= m; i++) cin >> know[i]; n   for (int i = 1; i <= m; i++) { n     int cnt = 0, z = 0; n     memset(pd, 0, sizeof(pd)); n     for (int j = 0; j < know[i].size(); j++) { n       if (s[j] !=  *  && s[j] != know[i][j]) { n         z = 1; n         break; n       } n     } n     if (z) { n       q++; n       continue; n     } n     for (int j = 0; j < know[i].size(); j++) { n       if (d[know[i][j]] && s[j] ==  * ) { n         z = 1; n         break; n       } n     } n     if (z) { n       q++; n       continue; n     } n     for (int j = 0; j < know[i].size(); j++) { n       if (!pd[know[i][j] -  a ] && !d[know[i][j]]) { n         pd[know[i][j] -  a ] = 1; n         t[know[i][j]]++; n         z = 1; n       } n     } n     if (!z) q++; n   } n   for (int i = 0; i <= 25; i++) { n     char k =  a  + i; n     if (!d[k] && t[k] == m - q && t[k]) ans++; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int gcd(int a, int b) { n   int c; n   while (a != 0) { n     c = a; n     a = b % a; n     b = c; n   } n   return b; n } n int main() { n   ios_base::sync_with_stdio(0); n   int n, m, b = 0; n   cin >> n >> m; n   vector<vector<int> > arr(n, vector<int>(4, 0)); n   for (int i = 0; i < n; i++) { n     if (b < m) { n       b++; n       arr[i][0] = b; n     } n     if (b < m) { n       b++; n       arr[i][3] = b; n     } n   } n   for (int i = 0; i < n; i++) { n     if (b < m) { n       b++; n       arr[i][1] = b; n     } n     if (b < m) { n       b++; n       arr[i][2] = b; n     } n   } n   for (int i = 0; i < n; i++) { n     if (arr[i][1] != 0) cout << arr[i][1] <<    ; n     if (arr[i][0] != 0) cout << arr[i][0] <<    ; n     if (arr[i][2] != 0) cout << arr[i][2] <<    ; n     if (arr[i][3] != 0) cout << arr[i][3] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ld = long double; n using ll = long long; n using ull = unsigned long long; n const int INF = 1 << 30; n const ll LINF = 1LL << 60; n const int SZ = 1 << 13; n char buff[SZ], *pos = buff + SZ - 1; n inline ll read() { n   ll x = 0; n   int f = 1, n       c = (++pos == buff + SZ ? fread(pos = buff, 1, SZ, stdin), *pos : *pos); n   for (; !isdigit(c); n        c = (++pos == buff + SZ ? fread(pos = buff, 1, SZ, stdin), *pos : *pos)) n     if (c ==  - ) f = -f; n   for (; isdigit(c); n        c = (++pos == buff + SZ ? fread(pos = buff, 1, SZ, stdin), *pos : *pos)) n     x = (x << 3) + (x << 1) + (c ^ 48); n   return x * f; n } n const int N = 5005; n int n; n int T; n int t[N]; n ld p[N], pw[N]; n ld dp[2][N]; n int main() { n   cout << fixed << setprecision(9); n   n = read(), T = read(); n   for (int i = 1; i <= n; ++i) n     p[i] = read() / 100.0, t[i] = read(), pw[i] = pow(1 - p[i], t[i] - 1); n   ld ans = 0; n   dp[0][0] = 1; n   int pp = 0, qq = 1; n   for (int i = 1; i <= n; ++i) { n     ld sum = 0; n     for (int j = 1; j <= T; ++j) { n       sum = sum * (1 - p[i]) + dp[pp][j - 1]; n       if (j >= t[i]) sum -= dp[pp][j - t[i]] * pw[i]; n       dp[qq][j] = sum * p[i]; n       if (j >= t[i]) dp[qq][j] += dp[pp][j - t[i]] * pw[i]; n       ans += dp[qq][j]; n     } n     swap(pp, qq); n     memset(dp[qq], 0, sizeof dp[qq]); n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200002; n vector<vector<pair<int, int> > > ans; n vector<vector<int> > cs, twoCs, threeCs; n int n, p[N]; n int main() { n   ios::sync_with_stdio(0); n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> p[i]; n   bool mark[N] = {}; n   for (int i = 1; i <= n; i++) n     if (!mark[i] && p[i] != i) { n       vector<int> newC; n       int v = i; n       while (!mark[v]) { n         mark[v] = 1; n         newC.push_back(v); n         v = p[v]; n       } n       cs.push_back(newC); n     } n   for (int i = 0; i < cs.size(); i++) n     while (cs[i].size() >= 4) { n       vector<pair<int, int> > move; n       for (int j = max(0, (int)cs[i].size() - 5); j + 1 < cs[i].size(); j++) n         move.push_back(make_pair(cs[i][j], cs[i][j + 1])); n       move.push_back( n           make_pair(cs[i].back(), cs[i][max((int)cs[i].size() - 5, 0)])); n       ans.push_back(move); n       for (int j = 0; j < 4; j++) cs[i].pop_back(); n     } n   for (int i = 0; i < cs.size(); i++) n     if (cs[i].size() == 2) n       twoCs.push_back(cs[i]); n     else if (cs[i].size() == 3) n       threeCs.push_back(cs[i]); n   while (threeCs.size()) { n     vector<pair<int, int> > move; n     vector<int> &tt = threeCs.back(); n     move.push_back(make_pair(tt[0], tt[1])); n     move.push_back(make_pair(tt[1], tt[2])); n     move.push_back(make_pair(tt[2], tt[0])); n     threeCs.pop_back(); n     if (twoCs.size()) { n       tt = twoCs.back(); n       move.push_back(make_pair(tt[0], tt[1])); n       move.push_back(make_pair(tt[1], tt[0])); n       twoCs.pop_back(); n     } else if (threeCs.size()) { n       tt = threeCs.back(); n       move.push_back(make_pair(tt[0], tt[1])); n       move.push_back(make_pair(tt[1], tt[0])); n       vector<int> newC; n       newC.push_back(tt[0]); n       newC.push_back(tt[2]); n       twoCs.push_back(newC); n       threeCs.pop_back(); n     } n     ans.push_back(move); n   } n   while (twoCs.size()) { n     vector<pair<int, int> > move; n     vector<int> &tt = twoCs.back(); n     move.push_back(make_pair(tt[0], tt[1])); n     move.push_back(make_pair(tt[1], tt[0])); n     twoCs.pop_back(); n     if (twoCs.size()) { n       tt = twoCs.back(); n       move.push_back(make_pair(tt[0], tt[1])); n       move.push_back(make_pair(tt[1], tt[0])); n       twoCs.pop_back(); n     } n     ans.push_back(move); n   } n   cout << ans.size() << endl; n   for (int i = 0; i < ans.size(); i++) { n     cout << ans[i].size() << endl; n     for (int j = 0; j < ans[i].size(); j++) cout << ans[i][j].first <<    ; n     cout << endl; n     for (int j = 0; j < ans[i].size(); j++) cout << ans[i][j].second <<    ; n     cout << endl; n   } n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n const int N = 550; n using namespace std; n int n; n int m; n int k; n long long d[N][N][2]; n long long cost[N][N]; n vector<pair<int, long long> > R[N][N]; n vector<pair<int, long long> > D[N][N]; n vector<pair<int, long long> > nv; n void normal(vector<pair<int, long long> > &v) { n   sort(v.begin(), v.end()); n   reverse(v.begin(), v.end()); n   for (int i = 1; i < v.size(); i++) { n     v[i].second += v[i - 1].second; n   } n } n void upd(long long &x, long long y) { x = min(x, y); } n bool good(vector<pair<int, int> > &g, int i, int t) { n   int cur = g[i].first + g[i].second; n   if (cur >= t && (cur - t) % 4 == i) { n     return true; n   } n   return false; n } n int main() { n   ios_base::sync_with_stdio(0); n   scanf( %d%d%d , &n, &m, &k); n   vector<pair<int, int> > g; n   for (int i = 1, x, y, d, t, e; i <= k; i++) { n     scanf( %d%d%d%d%d , &x, &y, &d, &t, &e); n     g.clear(); n     g.push_back({x, y}); n     g.push_back({x + d, y - d}); n     g.push_back({x + d, y}); n     g.push_back({x, y + d}); n     for (int i = 0; i < 4; i++) { n       if (good(g, i, t)) { n         cost[g[i].first][g[i].second] += e; n       } n     } n     if (good(g, 0, t) && good(g, 3, t)) { n       R[g[3].first][g[3].second].push_back({g[0].second, e}); n     } n     if (good(g, 0, t) && good(g, 2, t)) { n       D[g[2].first][g[2].second].push_back({g[0].first, e}); n     } n     if (good(g, 1, t) && good(g, 2, t)) { n       R[g[2].first][g[2].second].push_back({g[1].second, e}); n     } n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       normal(R[i][j]); n       normal(D[i][j]); n     } n   } n   for (int i = 0; i <= n; i++) { n     for (int j = 0; j <= m; j++) { n       d[i][j][0] = 1e18; n       d[i][j][1] = 1e18; n     } n   } n   d[0][0][0] = 0; n   d[0][0][1] = 0; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       long long cnt = d[i][j][1]; n       for (int h = j + 1; h < m; h++) { n         while (!R[i][h].empty() && R[i][h].back().first < j) { n           R[i][h].pop_back(); n         } n         if (!R[i][h].empty()) { n           cnt -= R[i][h].back().second; n         } n         cnt += cost[i][h]; n         upd(d[i][h][0], cnt); n       } n       cnt = d[i][j][0]; n       for (int h = i + 1; h < n; h++) { n         while (!D[h][j].empty() && D[h][j].back().first < i) { n           D[h][j].pop_back(); n         } n         if (!D[h][j].empty()) { n           cnt -= D[h][j].back().second; n         } n         cnt += cost[h][j]; n         upd(d[h][j][1], cnt); n       } n     } n   } n   printf( %lld n , min(d[n - 1][m - 1][0], d[n - 1][m - 1][1]) + cost[0][0]); n } n


#include <bits/stdc++.h> n using namespace std; n long long n, _C; n long long lo, hi; n set<long long> S; n vector<long long> vec; n int actual; n long long offset; n void ask(long long x) { n   S.insert(x); n   vec.emplace_back(x); n   if (actual) cout <<  ?   << x <<   n ; n   if (actual) n     cin >> x; n   else n     x = abs(vec[(int)vec.size() - 2] - vec.back()) >= _C; n   if ((int)vec.size() > 1) { n     long long d = abs(vec[(int)vec.size() - 2] - vec.back()); n     if (x) n       hi = min(hi, d); n     else n       lo = max(lo, d + 1); n   } n } n long long solve() { n   vec.clear(); n   S.clear(); n   lo = 1; n   hi = n; n   long long cur = offset; n   ask(cur); n   while (lo < hi) { n     long long mid = (lo + hi) / 2; n     int p1 = (lo & 1); n     int p2 = (hi & 1); n     if ((lo & 1) == (hi & 1)) mid--; n     if ((int)S.size() & 1) n       ask(vec.back() - mid); n     else n       ask(vec.back() + mid); n   } n   if (actual) cout <<  =   << lo <<   n ; n   return *S.begin(); n } n int main() { n   ios_base::sync_with_stdio(0); n   int itest; n   cin >> itest; n   while (itest--) { n     cin >> n; n     offset = 0; n     _C = n; n     actual = 0; n     offset = 1 - solve(); n     actual = 1; n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t, n; n   cin >> t; n   while (t--) { n     long long x; n     double sum = 0; n     cin >> n >> x; n     vector<long long> v(n); n     for (int i = 0; i < n; i++) { n       cin >> v[i]; n       sum += v[i]; n     } n     sort(v.begin(), v.end()); n     int ans = sum / n >= x ? n : 0; n     double subSum = 0; n     for (int i = 0; i < n and !ans; i++) { n       subSum += v[i]; n       if ((sum - subSum) / (n - (i + 1)) >= x) { n         ans = n - (i + 1); n         break; n       } n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n const int N = 5e5 + 5; n int n, m; n ll c[N]; n vector<int> adj[N]; n map<vector<int>, ll> mp; n ll custom_gcd(ll a, ll b) { n   ll c; n   while (b > 0) { n     c = a % b; n     a = b; n     b = c; n   } n   return a; n } n void solve() { n   mp.clear(); n   cin >> n >> m; n   for (int i = 1; i <= n; ++i) { n     cin >> c[i]; n     adj[i].clear(); n   } n   for (int i = 1, u, v; i <= m; ++i) { n     cin >> u >> v; n     adj[v].emplace_back(u); n   } n   for (int i = 1; i <= n; ++i) { n     sort(adj[i].begin(), adj[i].end()); n     if (!adj[i].empty()) mp[adj[i]] += c[i]; n   } n   ll ans = 0; n   for (auto &p : mp) { n     ans = custom_gcd(ans, p.second); n   } n   cout << ans <<   n ; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1000 + 1; n char a[N][N]; n int f[N * 2], head[1000001], vis[2 * N], d[N * 2], d1[N * 2], bj[N * 2]; n int ask(int x) { return x == f[x] ? x : f[x] = ask(f[x]); } n struct node { n   int next, to; n } e[N * N]; n int tot; n void add(int x, int y) { n   e[++tot].to = y; n   e[tot].next = head[x]; n   head[x] = tot; n } n int cnt; n int solve() { n   queue<int> q; n   for (int i = 1; i <= cnt; i++) { n     if (d[i] == 0) q.push(i); n   } n   while (!q.empty()) { n     int p = q.front(); n     q.pop(); n     for (int i = head[p]; i; i = e[i].next) { n       int u = e[i].to; n       d[u]--; n       if (d[u] == 0) q.push(u); n     } n   } n   for (int i = 1; i <= cnt; i++) n     if (d[i]) return 1; n   return 0; n } n void dfs(int u, int w) { n   bj[u] = w; n   for (int i = head[u]; i; i = e[i].next) { n     int v = e[i].to; n     if (bj[v] < w + 1) dfs(v, w + 1); n   } n } n void solve1() { n   queue<int> q; n   for (int i = 1; i <= cnt; i++) n     if (!d1[i]) q.push(i), bj[i] = 1; n   while (!q.empty()) { n     int p = q.front(); n     q.pop(); n     for (int i = head[p]; i; i = e[i].next) { n       int u = e[i].to; n       d1[u]--; n       bj[u] = max(bj[u], bj[p] + 1); n       if (!d1[u]) q.push(u); n     } n   } n } n int main() { n   ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   int n, m; n   cin >> n >> m; n   for (int i = 1; i <= n + m; i++) f[i] = i; n   for (int i = 1; i <= n; i++) { n     cin >> a[i] + 1; n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       int f1 = ask(i), f2 = ask(n + j); n       if (a[i][j] !=  = ) { n         if (f1 == f2) { n           cout <<  No n ; n           return 0; n         } n       } else { n         f[f1] = f2; n       } n     } n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       int f1 = ask(i), f2 = ask(n + j); n       if (!vis[f1]) vis[f1] = ++cnt; n       if (!vis[f2]) vis[f2] = ++cnt; n       if (a[i][j] !=  = ) { n         if (f1 == f2) { n           cout <<  No n ; n           return 0; n         } n         if (a[i][j] ==  < ) n           add(vis[f1], vis[f2]), d[vis[f2]]++, d1[vis[f2]]++; n         else n           add(vis[f2], vis[f1]), d[vis[f1]]++, d1[vis[f1]]++; n       } n     } n   } n   if (solve()) { n     cout <<  No n ; n     return 0; n   } n   solve1(); n   cout <<  Yes n ; n   for (int i = 1; i <= n + m; i++) { n     cout << bj[vis[ask(i)]] <<    ; n     if (i == n) cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n   n typedef long double ld; n const long long inf = 1100000000000000000; n #define maxn 1000005 n #define mod 1000000007 //998244353 n #define int long long n #define PB push_back n #define eb emplace_back n #define all(c) (c).begin(),(c).end() n #define pii pair <int,int> n #define vi vector<int> n #define F first n #define S second n #define rep(i,a,b)  for(int i=a;i<b;i++) n #define rep1(i,a,b) for(int i=a;i<=b;i++) n #define repd(i,a,b) for(int i=a;i>=b;--i) n #define sz(x) (int)((x).size())   n #define mem(a,x) memset(a,x,sizeof(a)) n const long double PI=3.141592653589793238462643383279502884197169399375105820974944; n  n void solve(){ n  int n, m; cin>>n>>m; n  int z = 720720; //lcm{1..16} n  int a[n][m]; n  rep(i, 0, n)  n   rep(j, 0, m)  n    cin>>a[i][j]; n  rep(i, 0, n){ n   rep(j, 0, m){ n    int k = z-pow(a[i][j], 4); n    if((i+j)%2) n     cout<<z<<   ; n    else n     cout<<k<<   ; n   }cout<<  n ; n  } n } n  n signed main(){ n     ios_base::sync_with_stdio(false), cin.tie(nullptr);  n     int T=1;// cin>>T;  n     while(T--){ n         solve();cout<<  n ; n     } n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n long n, ps[1000], tp, q, ar[1000], ans, x, y, a, b; n int main() { n   ios_base::sync_with_stdio(0); n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> ar[i]; n     ps[ar[i]] = i; n   } n   cin >> q; n   for (; q; --q) { n     cin >> tp; n     if (tp == 2) { n       cin >> a >> b; n       swap(ar[a], ar[b]); n       ps[ar[a]] = a; n       ps[ar[b]] = b; n     } else { n       cin >> x >> y; n       ans = 1; n       while (x < y) { n         if (ps[x + 1] < ps[x]) ++ans; n         ++x; n       } n       cout << ans << endl; n     } n   } n   cin.get(); n   cin.get(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, a = 0; n int main() { n   cin >> n; n   n = abs(n); n   while ((a * (a + 1)) / 2 < n || ((a * (a + 1)) / 2) % 2 != n % 2) a++; n   cout << a; n   return 0; n } n


#include <bits/stdc++.h> n void solve() { n   int n; n   std::cin >> n; n   int a = 0; n   std::string s; n   std::cin >> s; n   bool flag = false; n   int x, y; n   x = y = 0; n   if (s[0] ==  U ) { n     ++y; n   } else { n     ++x; n   } n   if (x > y) flag = true; n   for (size_t i = 1; i < s.size(); ++i) { n     if (s[i] ==  U ) { n       ++y; n     } else { n       ++x; n     } n     if (x > y && !flag) { n       flag = true; n       ++a; n     } else if (x < y && flag) { n       flag = false; n       ++a; n     } n   } n   std::cout << a; n } n int main() { n   std::ios_base::sync_with_stdio(false); n   std::cin.tie(nullptr); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long d = 0, n, i; n   cin >> n; n   int a[n]; n   for (i = 0; i < n; i++) cin >> a[i]; n   sort(a, a + n); n   for (i = 0; i < n; i++) { n     if (a[i] > d) d++; n   } n   cout << d << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 10; n int pos[N]; n int main() { n   ios::sync_with_stdio(false); n   int n, a; n   while (cin >> n) { n     for (int i = 1; i <= n; ++i) { n       cin >> a; n       pos[a] = i; n     } n     int last = 0; n     for (int i = 1; i <= n; ++i) { n       cin >> a; n       cout << max(pos[a] - last, 0) <<    ; n       last = max(last, pos[a]); 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   vector<long long> a(n + 1, 0); n   long long s1 = 0, s2 = 0, s3 = 0; n   for (int i = 1; i <= n; i++) { n     scanf( %lld , &a[i]); n     a[i] += a[i - 1]; n   } n   if (a[n] % 3) { n     cout <<  0 n ; n     return; n   } n   vector<int> a0, a1; n   for (int i = 1; i < n; i++) { n     if (a[i] == a[n] / 3) a0.push_back(i); n     if (a[i] == 2 * a[n] / 3) a1.push_back(i); n   } n   long long ret = 0; n   while (a0.size() && a1.size()) { n     while (a0.size() && a0.back() >= a1.back()) { n       a0.pop_back(); n     } n     if (!a0.size()) break; n     ret += a0.size(); n     a1.pop_back(); n   } n   cout << ret << endl; n } n int main() { n   solve(); n   return 0; n } n


#include <iostream> n #include <vector> n #include <set> n #include <unordered_set> n #include <map> n #include <unordered_map> n #include <algorithm> n #include <numeric> n #include <queue> n #include <iomanip> n #include <numeric> n #include <cmath> n using namespace std; n  n int main() { n  int t; n  cin >> t; n  for (int tt = 0; tt < t; tt++) { n   int n, m; n   cin >> n >> m; n   vector<int> va(n, 0), vb(n, 0); n   unordered_map<int, vector<int>> tofix; n   unordered_map<int, int> fixed; n   for (int i = 0; i < n; i++) { n    cin >> va[i]; n   } n   for (int i = 0; i < n; i++) { n    cin >> vb[i]; n    if (va[i] == vb[i]) { n     fixed[va[i]] = i; n    } else { n     tofix[vb[i]].push_back(i); n    } n   } n   vector<int> res(m, 0); n   vector<pair<int, int>> redundant; n   for (int i = 0; i < m; i++) { n    int x; n    cin >> x; n    if (tofix.count(x) > 0 and tofix[x].size() > 0) { n     res[i] = tofix[x].back(); n     fixed[x] = tofix[x].back(); n     tofix[x].pop_back(); n     if (tofix[x].size() == 0) { n      tofix.erase(x); n     } n  n     if (redundant.size() > 0) { n      for (auto p : redundant) { n       res[p.first] = fixed[x]; n      } n      redundant.clear(); n     } n    } else { n     if (fixed.count(x) > 0) { n      if (redundant.size() > 0) { n       for (auto p : redundant) { n        res[p.first] = fixed[x]; n       } n       redundant.clear(); n       res[i] = fixed[x]; n      } else { n       res[i] = fixed[x]; n      } n     } else { n      redundant.emplace_back(i, x); n     } n    } n   } n   if (redundant.empty() and tofix.size() == 0) { n    cout <<  YES  << endl; n    for (auto r : res) { n     cout << r + 1 <<    ; n    } n    cout << endl; n   } else { n    cout <<  NO  << endl; n   } n  } n } n  n  n  n


#include <bits/stdc++.h> n using namespace std; n const int N = 2.5 * 1e5 + 5; n int n, m, k; n int d[N]; n struct Edge { n   int num; n   int next; n } edge[4 * N]; n int tot, last[N]; n vector<int> q; n vector<int> ans[N]; n vector<int> path; n int anss; n bool flag; n void Add(int i, int j) { n   tot++; n   edge[tot].num = j; n   edge[tot].next = last[i]; n   last[i] = tot; n } n void dfs(int i, int deep) { n   if (flag) return; n   d[i] = deep; n   q.push_back(i); n   if (deep > n / k) { n     flag = 1; n     for (int j = q.size() - 1; j >= 0; j--) path.push_back(q[j]); n     return; n   } n   bool flag = 0; n   for (int k = last[i]; k != 0; k = edge[k].next) { n     int v = edge[k].num; n     if (d[v] == 0) { n       dfs(v, deep + 1); n       flag = 1; n     } n   } n   if (flag == 0) { n     anss++; n     int sign1 = 0, sign2 = 0; n     bool sign = 0; n     for (int k = last[i]; k != 0; k = edge[k].next) { n       int v = edge[k].num; n       if (d[v] + 1 == d[i]) continue; n       if ((d[i] - d[v]) % 3 == 2) { n         if (sign1 == 0) n           sign1 = v; n         else n           sign2 = v; n       } else { n         for (int k = q.size() - 1; k >= 0; k--) { n           ans[anss].push_back(q[k]); n           if (q[k] == v) break; n         } n         sign = 1; n         break; n       } n     } n     if (sign == 0) { n       bool en = 0; n       ans[anss].push_back(i); n       for (int k = q.size() - 1; k >= 0; k--) { n         if ((q[k] == sign1 || q[k] == sign2) && en == 0) { n           en = 1; n           ans[anss].push_back(q[k]); n           continue; n         } n         if (en) ans[anss].push_back(q[k]); n         if ((q[k] == sign1 || q[k] == sign2) && en == 1) { n           en = 0; n           break; n         } n       } n     } n   } n   q.pop_back(); n } n int main() { n   std::ios::sync_with_stdio(0); n   cin >> n >> m >> k; n   for (int i = 1; i <= m; i++) { n     int u, v; n     cin >> u >> v; n     Add(u, v); n     Add(v, u); n   } n   dfs(1, 1); n   if (flag) { n     cout <<  PATH n  << path.size() << endl; n     for (int j = path.size() - 1; j >= 0; j--) cout << path[j] <<    ; n   } else { n     cout <<  CYCLES n ; n     for (int i = 1; i <= k; i++) { n       cout << ans[i].size() << endl; n       ; n       for (int j = 0; j <= ans[i].size() - 1; j++) cout << ans[i][j] <<    ; n       cout << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int test_case; n   cin >> test_case; n   while (test_case--) { n     int a, b, x, y; n     vector<int> akash; n     cin >> a >> b >> x >> y; n     int temp1 = 0, temp2 = 0, temp3 = 0, temp4 = 0; n     temp1 = ((b - y) - 1) * a; n     temp2 = ((a - x) - 1) * b; n     temp3 = (y - 0) * a; n     temp4 = (x - 0) * b; n     akash.push_back(temp1); n     akash.push_back(temp2); n     akash.push_back(temp3); n     akash.push_back(temp4); n     int max = akash[0]; n     for (int i = 1; i < 4; i++) { n       if (max < akash[i]) { n         max = akash[i]; n       } n     } n     cout << max << endl; n     akash.clear(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[20000]; n int main() { n   ios_base::sync_with_stdio(NULL); n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     int x; n     cin >> x; n     if (x == 1) { n       cout <<  HARD ; n       return 0; n     } n   } n   cout <<  EASY ; n } n


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


#include <bits/stdc++.h> n const int N = 1000000; n int n; n char str[N + 10]; n int cdog = 0, fdog = 0, ldog = 0, cbone = 0, fbone = 0, lbone = 0; n int cntd[N + 10], cntb[N + 10]; n int nxtd[N + 10], nxtb[N + 10]; n int lstd[N + 10], lstb[N + 10]; n int tp1[N + 10], tp2[N + 10][2]; n bool dp[N + 10]; n bool chk(int x) { n   memset(dp, 0, sizeof(dp)); n   dp[0] = true; n   for (int i = n; i >= 0; --i) { n     if (str[i] !=  P ) continue; n     int nd = lstd[std::min(i + x, n)]; n     if (nd > i) n       tp1[i] = tp1[nd]; n     else n       tp1[i] = i + x; n   } n   for (int i = n; i >= 0; --i) { n     if (str[i] !=  P ) continue; n     int nd = lstd[std::min(i + x, n)], bd = nxtd[i + 1]; n     if (!bd) n       tp2[i][0] = n + 1, tp2[i][1] = 0; n     else if (nd == bd) n       tp2[i][0] = bd - x, tp2[i][1] = i + x; n     else n       tp2[i][0] = bd - x, tp2[i][1] = tp1[nd]; n   } n   for (int i = 0; i < n; ++i) { n     if (!dp[i]) continue; n     int nd = nxtd[i + 1]; n     int nb = nxtb[i + 1]; n     if (nd == 0 || nb == 0) break; n     if (nb < nd) { n       if (nb >= nd - x) { n         dp[nd] = true; n       } n       if (nb >= tp2[nd][0]) { n         dp[std::min(tp2[nd][1], n)] = true; n       } n     } else { n       dp[std::min(tp1[nd], n)] = true; n     } n   } n   int lst = 0; n   for (int i = n; i >= 0; --i) n     if (dp[i]) { n       lst = i; n       break; n     } n   return lst >= lbone; n } n int main() { n   scanf( %d , &n); n   scanf( %s , str + 1); n   for (int i = 1; i <= n; ++i) n     if (str[i] ==  P ) { n       ++cdog; n       if (fdog == 0) fdog = i; n       ldog = i; n       cntd[i] = cntd[i - 1] + 1; n       cntb[i] = cntb[i - 1]; n     } else if (str[i] ==  * ) { n       ++cbone; n       if (fbone == 0) fbone = i; n       lbone = i; n       cntd[i] = cntd[i - 1]; n       cntb[i] = cntb[i - 1] + 1; n     } else { n       cntd[i] = cntd[i - 1]; n       cntb[i] = cntb[i - 1]; n     } n   if (cdog == 1) { n     if (cntb[ldog] > cbone - cntb[ldog]) { n       printf( %d %d n , cntb[ldog], ldog - fbone); n     } else { n       printf( %d %d n , cbone - cntb[ldog], lbone - ldog); n     } n     return 0; n   } n   printf( %d  , cbone); n   for (int i = n; i >= 0; --i) { n     if (str[i] ==  P ) n       nxtd[i] = i; n     else n       nxtd[i] = nxtd[i + 1]; n     if (str[i] ==  * ) n       nxtb[i] = i; n     else n       nxtb[i] = nxtb[i + 1]; n   } n   for (int i = 1; i <= n; ++i) { n     if (str[i] ==  P ) n       lstd[i] = i; n     else n       lstd[i] = lstd[i - 1]; n     if (str[i] ==  * ) n       lstb[i] = i; n     else n       lstb[i] = lstb[i - 1]; n   } n   int l = 1, r = n, ans = 0; n   while (l <= r) { n     int mid = (l + r) >> 1; n     if (chk(mid)) { n       ans = mid, r = mid - 1; n     } else { n       l = mid + 1; n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100005, MAXM = 1000005, MAXT = 1 << 18; n struct Edge { n   int to, val, next, id; n } edge[MAXM]; n struct MST { n   int u, v, w, id; n } mst[MAXM]; n int ppar[MAXN], head[MAXN], ist[MAXM], dep[MAXN], n, m, tot; n void addedge(int u, int v, int w, int id) { n   edge[++tot] = (Edge){v, w, head[u], id}; n   head[u] = tot; n } n int find(int x) { return x == ppar[x] ? x : ppar[x] = find(ppar[x]); } n void merge(int x, int y) { n   x = find(x), y = find(y); n   if (x != y) ppar[x] = y; n } n int mmn[MAXT], beg[MAXN], ed[MAXN], ans[MAXM], par[20][MAXN], mx[20][MAXN], n     dcnt; n struct Que { n   int l, r, id; n }; n struct Mod { n   int p, w; n }; n vector<Que> qr1[MAXN], qr2[MAXN]; n vector<Mod> md1[MAXN], md2[MAXN]; n void dfs(int u, int fa) { n   beg[u] = ++dcnt; n   dep[u] = dep[fa] + 1; n   for (int i = head[u]; i; i = edge[i].next) { n     int v = edge[i].to; n     if (v == fa) continue; n     dfs(v, u); n     qr1[beg[v]].push_back((Que){beg[v], ed[v], edge[i].id}); n     qr2[ed[v]].push_back((Que){beg[v], ed[v], edge[i].id}); n     par[0][v] = u; n     mx[0][v] = edge[i].val; n   } n   ed[u] = dcnt; n } n void chk(int &x, int y) { x = x < y ? x : y; } n void cxk(int &x, int y) { x = x > y ? x : y; } n void upd(int x, int w, int l = 1, int r = n, int k = 1) { n   if (l == r) { n     chk(mmn[k], w); n     return; n   } n   int mid = (l + r) >> 1; n   if (x <= mid) n     upd(x, w, l, mid, k << 1); n   else n     upd(x, w, mid + 1, r, k << 1 | 1); n   chk(mmn[k], w); n } n int ask(int a, int b, int l = 1, int r = n, int k = 1) { n   if (a > r || b < l) return 1E9; n   if (a <= l && b >= r) return mmn[k]; n   int mid = (l + r) >> 1; n   return min(ask(a, b, l, mid, k << 1), ask(a, b, mid + 1, r, k << 1 | 1)); n } n int calc(int x, int y) { n   if (dep[x] > dep[y]) swap(x, y); n   int res = 0; n   for (int i = 19; ~i; i--) n     if (dep[par[i][y]] >= dep[x]) cxk(res, mx[i][y]), y = par[i][y]; n   if (x == y) return res; n   for (int i = 19; ~i; i--) n     if (par[i][x] != par[i][y]) n       cxk(res, mx[i][x]), cxk(res, mx[i][y]), x = par[i][x], y = par[i][y]; n   return max(res, max(mx[0][x], mx[0][y])); n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= m; i++) { n     int u, v, w; n     scanf( %d%d%d , &u, &v, &w); n     mst[i] = (MST){u, v, w, i}; n     ans[i] = 1E9; n   } n   sort(mst + 1, mst + 1 + m, n        [&](const MST &a, const MST &b) { return a.w < b.w; }); n   for (int i = 1; i <= n; i++) ppar[i] = i; n   for (int i = 1; i <= m; i++) { n     int u = mst[i].u, v = mst[i].v; n     if (find(u) != find(v)) { n       merge(u, v); n       ist[mst[i].id] = 1; n       addedge(u, v, mst[i].w, mst[i].id); n       addedge(v, u, mst[i].w, mst[i].id); n     } n   } n   for (int i = 0; i < MAXT; i++) mmn[i] = 1E9; n   dfs(1, 0); n   for (int i = 1; i < 20; i++) n     for (int j = 1; j <= n; j++) { n       par[i][j] = par[i - 1][par[i - 1][j]]; n       mx[i][j] = max(mx[i - 1][j], mx[i - 1][par[i - 1][j]]); n     } n   for (int i = 1; i <= m; i++) n     if (!ist[mst[i].id]) { n       int u = mst[i].u, v = mst[i].v; n       if (beg[u] > beg[v]) swap(u, v); n       md1[beg[u]].push_back((Mod){beg[v], mst[i].w}); n       md2[beg[v]].push_back((Mod){beg[u], mst[i].w}); n       ans[mst[i].id] = calc(u, v); n     } n   for (int i = 1; i <= n; i++) { n     for (Que q : qr1[i]) chk(ans[q.id], ask(q.l, q.r)); n     for (Mod d : md1[i]) upd(d.p, d.w); n   } n   for (int i = 0; i < MAXT; i++) mmn[i] = 1E9; n   for (int i = n; i > 0; i--) { n     for (Que q : qr2[i]) chk(ans[q.id], ask(q.l, q.r)); n     for (Mod d : md2[i]) upd(d.p, d.w); n   } n   for (int i = 1; i <= m; i++) printf( %d n , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[17][17]; n int main() { n   int n, m, k, need; n   scanf( %d%d%d%d , &n, &m, &k, &need); n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       int x, y; n       scanf( %d%d , &x, &y); n       a[x][y] = 1; n     } n   } n   int ans = 0; n   for (int x1 = 1; x1 <= n; x1++) { n     for (int y1 = 1; y1 <= m; y1++) { n       for (int x2 = x1; x2 <= n; x2++) { n         for (int y2 = 1; y2 <= m; y2++) { n           int tot = 0; n           for (int x = x1; x <= x2; x++) { n             for (int y = y1; y <= y2; y++) { n               tot += a[x][y]; n             } n           } n           if (tot >= need) ans++; n         } n       } n     } n   } n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 2e5 + 5; n vector<int> G[M]; n int n, A[M], num, ok, res, ans[2][M], C; n bool mark[M]; n void ma(int a, int b) { n   if (a == b || mark[a] && mark[b]) return; n   printf( %d %d n , a, b); n   ans[0][C] = a; n   ans[1][C] = b; n   C++; n   mark[a] = mark[b] = 1; n   ma(A[a], A[b]); n } n int main() { n   scanf( %d , &n); n   res = -1; n   for (int i = 1; i <= n; i++) scanf( %d , A + i); n   for (int i = 1; i <= n; i++) { n     int nw = A[i], has = 0; n     while (!mark[nw]) { n       has = 1; n       mark[nw] = 1; n       G[num].push_back(nw); n       nw = A[nw]; n     } n     if (G[num].size() == 1) ok = A[i]; n     if (G[num].size() == 2) res = num; n     if (has) num++; n   } n   memset(mark, 0, sizeof(mark)); n   if (ok) { n     puts( YES ); n     for (int i = 1; i <= n; i++) n       if (i != ok) printf( %d %d n , ok, i); n   } else if (~res) { n     for (int i = 0; i < num; i++) n       if (G[i].size() & 1) { n         puts( NO ); n         return 0; n       } n     puts( YES ); n     printf( %d %d n , G[res][0], A[G[res][0]]); n     for (int i = 0; i < num; i++) n       if (res != i) ma(G[res][0], G[i][0]); n   } else n     puts( NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n const int INF = 1e9 + 10; n const int N = 1e2 + 5; n int mark[N]; n int ans; n int main() { n   int n, k; n   cin >> n >> k; n   for (int i = 0; i < n; i++) { n     int x; n     cin >> x; n     mark[x] = i + 1; n   } n   for (int i = 0; i < N; i++) { n     if (mark[i] != 0) ans++; n   } n   if (ans >= k) { n     cout <<  YES  <<   n ; n     int cnt = 0; n     for (int i = 0; i < N; i++) { n       if (cnt < k and mark[i] != 0) { n         cout << mark[i] <<    ; n         cnt++; n       } n     } n   } else n     cout <<  NO ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int i, j, len; n   char s[105]; n   cin >> s; n   len = strlen(s); n   for (i = 0; i < len; i++) { n     for (j = 0; j < len - i - 2; j += 2) { n       if (s[j] > s[j + 2]) { n         swap(s[j], s[j + 2]); n       } n     } n   } n   printf( %s , s); n   return 0; n } n


#include <bits/stdc++.h> n int c[100]; n int main() { n   int n, s, max = 0, i, j; n   scanf( %d , &n); n   for (i = 0; i < n; i++) { n     scanf( %d , &s); n     c[s] += 1; n     if (s > max) max = s; n   } n   for (i = 0; i <= max; i++) n     for (j = 0; j < c[i]; j++) printf( %d  , i); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, k, m, d, i = 0, b = 1, ans = 0; n int main(void) { n   cin >> n >> k >> m >> d; n   for (i = 1; i <= d && b <= n; i++, b += k) { n     ans = max(ans, min(m, n / b) * i); n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   long long x; n   bool s; n   node(long long a, bool b) { x = a, s = b; } n }; n bool operator<(node a, node b) { return a.x > b.x; } n priority_queue<node> pq; n int main() { n   int n; n   long long temp; n   cin >> n; n   long long ans = 0; n   for (int i = 1; i <= n; i++) { n     cin >> temp; n     if (pq.empty() || pq.top().x > temp) { n       pq.push(node(temp, 0)); n     } else { n       long long now = pq.top().x; n       bool se = pq.top().s; n       pq.pop(); n       if (se) pq.push(node(now, 0)); n       ans += temp - now, pq.push(node(temp, 1)); n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   long long n; n   cin >> n; n   long long ar[n + 1]; n   for (long long i = 1; i <= n; i++) { n     cin >> ar[i]; n   } n   for (long long i = 1; i <= n; i++) { n     long long f = ar[i]; n     long long t = ar[f]; n     if (ar[t] == i) { n       return cout <<  YES , 0; n     } n   } n   cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n const int MOD = 1e9 + 7, MAX = 1e6 + 5; n const long long INF = 1e18 + 5; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   while (t--) { n     int n, q; n     cin >> n >> q; n     vector<int> A(n + 2); n     for (int in = 1; in < 1 + n; in++) cin >> A[in]; n     A[0] = -n - 1; n     A[n + 1] = -n - 1; n     long long ans = 0; n     set<int> Pos; n     set<int> Neg; n     for (int i = 1; i <= n; i++) { n       if (A[i] > A[i - 1] && A[i] > A[i + 1]) { n         ans += A[i]; n         Pos.insert(A[i]); n       } n       if (A[i] < A[i - 1] && A[i] < A[i + 1]) { n         ans -= A[i]; n         Neg.insert(A[i]); n       } n     } n     cout << ans <<   n ; n     while (q--) { n       int u, v; n       cin >> u >> v; n       swap(A[u], A[v]); n       long long diff = 0; n       for (int i = u - 1; i <= u + 1; i++) { n         if (i <= 0) continue; n         if (i > n) continue; n         if (A[i] > A[i - 1] && A[i] > A[i + 1]) { n           if (Neg.count(A[i])) { n             Neg.erase(A[i]); n             Pos.insert(A[i]); n             diff += 2 * A[i]; n           } else if (!Pos.count(A[i])) { n             Pos.insert(A[i]); n             diff += A[i]; n           } n         } else if (A[i] < A[i - 1] && A[i] < A[i + 1]) { n           if (Pos.count(A[i])) { n             Pos.erase(A[i]); n             Neg.insert(A[i]); n             diff -= 2 * A[i]; n           } else if (!Neg.count(A[i])) { n             Neg.insert(A[i]); n             diff -= A[i]; n           } n         } else { n           if (Pos.count(A[i])) { n             Pos.erase(A[i]); n             diff -= A[i]; n           } else if (Neg.count(A[i])) { n             Neg.erase(A[i]); n             diff += A[i]; n           } n         } n       } n       for (int i = v - 1; i <= v + 1; i++) { n         if (i <= u + 1) continue; n         if (i <= 0) continue; n         if (i > n) continue; n         if (A[i] > A[i - 1] && A[i] > A[i + 1]) { n           if (Neg.count(A[i])) { n             Neg.erase(A[i]); n             Pos.insert(A[i]); n             diff += 2 * A[i]; n           } else if (!Pos.count(A[i])) { n             Pos.insert(A[i]); n             diff += A[i]; n           } n         } else if (A[i] < A[i - 1] && A[i] < A[i + 1]) { n           if (Pos.count(A[i])) { n             Pos.erase(A[i]); n             Neg.insert(A[i]); n             diff -= 2 * A[i]; n           } else if (!Neg.count(A[i])) { n             Neg.insert(A[i]); n             diff -= A[i]; n           } n         } else { n           if (Pos.count(A[i])) { n             Pos.erase(A[i]); n             diff -= A[i]; n           } else if (Neg.count(A[i])) { n             Neg.erase(A[i]); n             diff += A[i]; n           } n         } n       } n       ans += diff; n       cout << ans <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main(void) { n   int a[10] = {0}; n   int n; n   string d; n   cin >> n; n   cin >> d; n   for (int i = 0; i < d.size(); i++) { n     if (d[i] ==  L ) { n       for (int j = 0; j < 10; j++) { n         if (a[j] == 0) { n           a[j] = 1; n           break; n         } n       } n     } else if (d[i] ==  R ) { n       for (int j = 9; j >= 0; j--) { n         if (a[j] == 0) { n           a[j] = 1; n           break; n         } n       } n     } else { n       a[d[i] -  0 ] = 0; n     } n   } n   for (int i = 0; i < 10; i++) printf( %d , a[i]); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 10, mod = 1e9 + 7; n int n, K, ans, cnt; n char s[5]; n struct DSU { n   int fa[N]; n   bool mark[N]; n   int getfa(int x) { return fa[x] == x ? x : fa[x] = getfa(fa[x]); } n   bool merge(int x, int y) { n     int fx = getfa(x), fy = getfa(y); n     if (getfa(x ^ 1) == fy || getfa(y ^ 1) == fx) return 0; n     if (fx != fy) fa[fx] = fy; n     return 1; n   } n   bool Set(int l, int r, int c) { n     if (!c) n       return merge(l * 2, r * 2) && merge(l * 2 + 1, r * 2 + 1); n     else n       return merge(l * 2 + 1, r * 2) && merge(l * 2, r * 2 + 1); n   } n } T; n int qpow(int a, int b) { n   int ret = 1; n   for (; b; b >>= 1) { n     if (b & 1) ret = 1ll * ret * a % mod; n     a = 1ll * a * a % mod; n   } n   return ret; n } n int main() { n   cin >> n >> K; n   for (int i = 0; i <= (n + 2) * 2; i++) T.fa[i] = i; n   for (int i = 1; i <= K; i++) { n     int x, y; n     scanf( %d%d%s , &x, &y, s + 1); n     --x, --y; n     int l = x - y, r = x + y; n     if (l < 0) l = -l; n     if (r > n - 1) r = (n - 1) * 2 - r; n     if (!T.Set(l, r + 2, s[1] ==  o )) { n       return puts( 0 ), 0; n     } n   } n   for (int i = 0; i <= (n + 1) * 2 + 1; i++) ans += (T.getfa(i) == i); n   printf( %d n , qpow(2, ans / 2 - 2)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100005; n const double eps = 1e-9; n const double pi = acos(-1); n struct P { n   int x, y; n } a[N]; n int n, vi[N], t[N]; n double l[N], r[N], q[N]; n long long k; n void change(int k, int v) { n   for (; k; k -= k & (-k)) t[k] += v; n } n int ask(int k) { n   int s = 0; n   for (; k < N; k += k & (-k)) s += t[k]; n   return s; n } n struct node { n   double p1, p2; n   int v; n } op[N * 2]; n bool cmp(node a, node b) { return a.p1 < b.p1; } n long long count(double R) { n   int top = 0, op_top = 0; n   memset(t, 0, sizeof(t)); n   for (int i = (int)(1); i <= (int)(n); i++) { n     l[i] = r[i] = -1; n     double d = sqrt(a[i].x * a[i].x + a[i].y * a[i].y); n     if (d < R + eps) continue; n     double theta = atan2(a[i].y, a[i].x); n     double delta = acos(R / d); n     l[i] = theta - delta; n     r[i] = theta + delta; n     for (; l[i] < 0; l[i] += 2 * pi) n       ; n     for (; r[i] < 0; r[i] += 2 * pi) n       ; n     for (; l[i] >= 2 * pi; l[i] -= 2 * pi) n       ; n     for (; r[i] >= 2 * pi; r[i] -= 2 * pi) n       ; n     if (l[i] > r[i]) swap(l[i], r[i]); n     q[++top] = l[i]; n     op[++op_top] = (node){l[i], l[i], 1}; n     op[++op_top] = (node){r[i], l[i], -1}; n   } n   sort(q + 1, q + top + 1); n   sort(op + 1, op + op_top + 1, cmp); n   long long sum = 0; n   for (int i = (int)(1); i <= (int)(op_top); i++) { n     int p = lower_bound(q + 1, q + top + 1, op[i].p2) - q; n     change(p, op[i].v); n     if (op[i].v == -1) sum += ask(p); n   } n   return sum; n } n int main() { n   scanf( %d%lld , &n, &k); n   for (int i = (int)(1); i <= (int)(n); i++) scanf( %d%d , &a[i].x, &a[i].y); n   double l = 0, r = 15000; n   for (int T = (int)(1); T <= (int)(50); T++) { n     double mid = (l + r) / 2; n     if (1ll * n * (n - 1) / 2 - count(mid) < k) n       l = mid; n     else n       r = mid; n   } n   printf( %.10lf n , (l + r) / 2); n } n


#include <bits/stdc++.h> n #pragma warning(disable : 4996) n using namespace std; n int n; n long long a[1005][2]; n long long mi = ((1ll << 62) - 1); n void calc(long long x) { n   int i; n   long long sum = 0; n   int cnt = 0; n   vector<pair<int, int> > v; n   for (i = 0; i < n; i++) { n     if (a[i][0] <= x && a[i][1] <= x) { n       if (a[i][0] <= a[i][1]) n         sum += a[i][0]; n       else n         v.push_back(pair<int, int>(a[i][1] - a[i][0], i)); n     } else if (a[i][0] <= x) n       sum += a[i][1], cnt++; n     else if (a[i][1] <= x) n       sum += a[i][0]; n     else n       return; n   } n   if (cnt > n / 2) return; n   sort(v.begin(), v.end()); n   for (auto e : v) { n     if (cnt < n / 2) n       sum += a[e.second][1], cnt++; n     else n       sum += a[e.second][0]; n   } n   mi = min(mi, x * sum); n } n int main() { n   int i, j, k; n   cin >> n; n   for (i = 0; i < n; i++) n     for (j = 0; j < 2; j++) cin >> a[i][j]; n   for (i = 0; i < n; i++) n     for (j = 0; j < 2; j++) calc(a[i][j]); n   cout << mi; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void reverse_(vector<int> num) { n   vector<int> a; n   for (int i = num.size() - 1; i > -1; i--) a.push_back(num[i]); n   num = a; n } n int main() { n   int n, x; n   cin >> n >> x; n   vector<int> num(n), used(1e5 + 7, 0), step(1e5 + 7, 1e9); n   set<int> all; n   for (int i = 0; i < n; i++) { n     cin >> num[i]; n     used[num[i]] = 1; n     all.insert(num[i]); n   } n   if (all.size() < n) { n     cout <<  0 n ; n     return 0; n   } n   int ans = 1e9; n   sort(num.begin(), num.end()); n   reverse_(num); n   for (int i = 0; i < n; i++) { n     int now = num[i]; n     int prev = -1; n     int shag = 0; n     while (now >= 0 && prev != now) { n       if (now != num[i]) { n         if (used[now]) { n           ans = min(ans, shag); n         } n       } n       if (step[now] != 1e9) { n         ans = min(step[now] + shag, ans); n       } n       step[now] = min(step[now], shag); n       prev = now; n       now = now & x; n       shag++; n     } n   } n   if (ans == 1e9) { n     cout <<  -1 n ; n     return 0; n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n; n   long long p[n], y; n   set<long long> c[4]; n   set<long long>::iterator it; n   c[1].insert(-1); n   c[2].insert(-1); n   c[3].insert(-1); n   int temp; n   for (int i = 0; i < n; i++) cin >> p[i]; n   for (int i = 0; i < n; i++) { n     cin >> temp; n     c[temp].insert(p[i]); n   } n   for (int i = 0; i < n; i++) { n     cin >> temp; n     c[temp].insert(p[i]); n   } n   cin >> m; n   for (int i = 0; i < m; i++) { n     cin >> temp; n     it = c[temp].begin(); n     it++; n     if (it == c[temp].end()) { n       cout <<  -1  ; n       continue; n     } else { n       y = *it; n       cout << y <<    ; n     } n     c[1].erase(y); n     c[2].erase(y); n     c[3].erase(y); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int edge[510][510], vis[510], con, N, M, f, t; n void dfs(int x) { n   vis[x] = 1; n   for (int i = 1; i <= N; i++) n     if (edge[x][i]) { n       if (!vis[i]) n         dfs(i); n       else if (vis[i] == 1) n         con++; n       if (con >= 2) break; n     } n   vis[x] = 2; n } n int main() { n   scanf( %d%d , &N, &M); n   for (int i = 1; i <= M; i++) { n     scanf( %d%d , &f, &t); n     edge[f][t] = 1; n   } n   for (int i = 1; i <= N; i++) { n     con = 0; n     memset(vis, 0, sizeof vis); n     dfs(i); n     for (int j = 1; j <= N; j++) n       if (j != i) { n         if (!vis[j]) dfs(j); n         if (con >= 2) break; n       } n     if (con <= 1) return puts( YES ), 0; n   } n   return puts( NO ), 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, i, g, a[100001], p[100001], maxi; n   vector<long long> v; n   cin >> n; n   for (i = 0; i < n; i++) { n     cin >> a[i]; n     p[a[i]] = i + 1; n   } n   g = 1; n   for (i = 1; i < n; i++) { n     if (p[i] <= p[i + 1]) { n       g++; n     } else { n       v.push_back(g); n       g = 1; n     } n   } n   v.push_back(g); n   maxi = 0; n   for (i = 0; i < v.size(); i++) { n     maxi = max(maxi, v[i]); n   } n   cout << n - maxi; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100005; n int n, i, j, k, cnt, tot, f[maxn]; n char s[maxn]; n int main() { n   scanf( %d%s , &n, s + 1); n   if (n & 1) return puts( 0 ), 0; n   tot = n >> 1; n   f[0] = 1; n   for (i = 1; i <= n; i++) { n     if (s[i] ==  ? ) n       for (j = i >> 1, k = max(1, tot - (n - i)); j >= k; j--) n         f[j] = f[j] + f[j - 1]; n     else n       cnt++; n   } n   if (cnt > tot) return puts( 0 ), 0; n   int ans = f[tot]; n   for (i = tot - cnt; i; i--) ans *= 25; n   printf( %u , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     int a[n]; n     for (int i = 0; i < n; i++) { n       cin >> a[i]; n     } n     sort(a, a + n); n     int count = 0; n     int groups = 0; n     for (int i = 0; i < n; i++) { n       if (++count == a[i]) { n         count = 0; n         groups++; n       } n     } n     cout << groups << endl; n   } n } n