#include <bits/stdc++.h> n using namespace std; n template <class T> n inline bool Up(T &a, const T &b) { n   if (a < b) { n     a = b; n     return true; n   } n   return false; n } n template <class T> n inline bool Down(T &a, const T &b) { n   if (a > b) { n     a = b; n     return true; n   } n   return false; n } n const int MAXN = 100023; n int N, M, color[MAXN], ans[MAXN]; n vector<int> fir[MAXN], side[2]; n int tot = 2; n void BFS() { n   static int Q[MAXN]; n   int head = 0, tail = 0; n   memset(color, -1, sizeof(color)); n   for (int S = (1); S <= (N); ++S) n     if (color[S] == -1) n       for (color[S] = 0, Q[++tail] = S; head < tail;) { n         int x = Q[++head], y; n         side[color[x]].push_back(x); n         for (vector<int>::iterator itr(fir[x].begin()); itr != fir[x].end(); n              ++itr) n           if (color[y = *itr] == -1) { n             Q[++tail] = y; n             color[y] = color[x] ^ 1; n           } n       } n } n void Print() { n   for (int c = (0); c <= (1); ++c) n     for (vector<int>::iterator itr(side[c].begin()); itr != side[c].end(); n          ++itr) { n       int x = *itr; n       if (!ans[x]) ans[x] = ++tot; n     } n   puts( YES ); n   for (int i = (1); i <= (N); ++i) n     printf( %d%c , ans[i] / 3, i == N ?   n  :    ); n } n void Paint(int x) { n   static int forbid[MAXN]; n   memset(forbid, 0, sizeof(forbid)); n   for (vector<int>::iterator itr(fir[x].begin()); itr != fir[x].end(); ++itr) n     forbid[*itr] = 1; n   int cnt = 0; n   ans[x] = ++tot; n   for (vector<int>::iterator itr(side[color[x] ^ 1].begin()); n        itr != side[color[x] ^ 1].end(); ++itr) { n     int y = *itr; n     if (!ans[y] && !forbid[y]) { n       ans[y] = ++tot; n       if (++cnt == 2) break; n     } n   } n } n int main() { n   memset(ans, 0, sizeof(ans)); n   scanf( %d %d , &N, &M); n   for (int i = (1); i <= (M); ++i) { n     int a, b; n     scanf( %d %d , &a, &b); n     fir[a].push_back(b); n     fir[b].push_back(a); n   } n   BFS(); n   if (side[0].size() % 3 == 0) { n     Print(); n     return 0; n   } n   if (side[0].size() % 3 == 2) { n     side[0].swap(side[1]); n     for (int i = (1); i <= (N); ++i) color[i] ^= 1; n   } n   for (vector<int>::iterator itr(side[0].begin()); itr != side[0].end(); n        ++itr) { n     int x = *itr; n     if (fir[x].size() + 2 <= side[1].size()) { n       Paint(x); n       Print(); n       return 0; n     } n   } n   int cnt = 0; n   for (vector<int>::iterator itr(side[1].begin()); itr != side[1].end(); n        ++itr) { n     int x = *itr; n     if (fir[x].size() + 2 <= side[0].size()) { n       Paint(x); n       if (++cnt == 2) { n         Print(); n         return 0; n       } n     } n   } n   puts( NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n vector<pair<int, int> > a; n set<pair<int, int> > d; n void solve(int l, int r) { n   if (r - l <= 1) return; n   int m = (l + r) / 2; n   solve(l, m); n   solve(m, r); n   for (int i = l; i < r; ++i) { n     d.insert({a[m].first, a[i].second}); n   } n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int n; n   cin >> n; n   a.resize(n); n   for (int i = 0; i < n; ++i) { n     cin >> a[i].first >> a[i].second; n     d.insert(a[i]); n   } n   sort(a.begin(), a.end()); n   solve(0, n); n   cout << d.size() <<   n ; n   for (auto i : d) { n     cout << i.first <<     << i.second <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> g[300011]; n long long ans[300011]; n struct P { n   long long dis = -1, val = 0; n } temp; n vector<P> m[300011]; n map<long long, long long> bit; n void add(long long i, long long val) { n   i = 1000000000000000 - i; n   for (; i < 1000000000000000; i += (i & (-i))) bit[i] += val; n } n long long get(long long i) { n   long long ret = 0; n   i = 1000000000000000 - i; n   for (; i > 0; i -= (i & (-i))) ret += bit[i]; n   return ret; n } n void dfs(int u, int par = 0, long long lev = 1) { n   for (auto i : m[u]) add(lev + i.dis, i.val); n   ans[u] = get(lev); n   for (auto v : g[u]) { n     if (v != par) dfs(v, u, lev + 1); n   } n   for (auto i : m[u]) add(lev + i.dis, -i.val); n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n, x, y, v, d; n   cin >> n; n   for (int i = 0; i < n - 1; i++) { n     cin >> x >> y; n     g[x].push_back(y); n     g[y].push_back(x); n   } n   int q; n   cin >> q; n   while (q--) { n     cin >> v >> d >> x; n     temp.dis = d; n     temp.val = x; n     m[v].push_back(temp); n   } n   dfs(1); n   for (int i = 1; i <= n; i++) cout << ans[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void print(int a) { cout << a <<    ; } n void print(long long a) { cout << a <<    ; } n void print(double a) { cout << a <<    ; } n void print(string a) { cout << a <<    ; } n template <typename T1, typename T2> n void print(pair<T1, T2> a) { n   print(a.first); n   cout <<   :::  ; n   print(a.second); n } n template <typename Container> n void print(const Container& c) { n   for (auto it = c.begin(); it != c.end(); it++) { n     print(*it); n   } n   cout << endl; n } n vector<long long> tot_comb(51, 0); n vector<long long> fac(51, 1); n int CALC_MAX = 0; n long long INF = 2e18; n long long get_fac(int x) { n   if (x <= 0) return 1; n   return fac[x]; n } n long long mul(long long x, long long y) { n   if (x >= INF || y >= INF || x * y >= INF || x * y < 0) return INF; n   return x * y; n } n long long add(long long x, long long y) { n   if (x >= INF || y >= INF || x + y >= INF) return INF; n   return x + y; n } n void fill_fac() { n   fac[0] = 1; n   fac[1] = 1; n   for (int i = 2; i <= 50; i++) { n     fac[i] = mul(fac[i - 1], i); n   } n } n void fill_tot_comb() { n   tot_comb[0] = 1; n   tot_comb[1] = 1; n   tot_comb[2] = 2; n   tot_comb[3] = 4; n   for (int i = 4; i <= 50; i++) { n     tot_comb[i] = 0; n     for (int j = 1; j <= i; j++) { n       tot_comb[i] = add(tot_comb[i], mul(get_fac(j - 2), tot_comb[i - j])); n     } n   } n } n void KthPermhelp(int k, long long n, int st) { n   vector<int> perm(k + 1, 0); n   vector<int> pos(k + 1, 0); n   perm[1] = k; n   pos[k] = 1; n   long long cnt = n; n   vector<bool> taken(k + 1, false); n   taken[k] = true; n   for (int i = 2; i <= k; i++) { n     for (int j = 1; j < k; j++) { n       if (!taken[j]) { n         int jk = i; n         while (taken[jk]) jk = pos[jk]; n         if (j == jk && i != k) continue; n         long long tot_with_j_i = get_fac(k - 1 - i); n         if (cnt > tot_with_j_i) { n           cnt -= tot_with_j_i; n         } else { n           perm[i] = j; n           taken[j] = true; n           pos[j] = i; n           break; n         } n       } n     } n   } n   if (k == 2) { n     perm[1] = 2; n     perm[2] = 1; n   } n   for (int i = 1; i <= k; i++) cout << perm[i] + st <<    ; n } n void KthPerm(int n, long long k, int st) { n   if (n == 0) return; n   if (tot_comb[n] < k) { n     cout << -1; n     return; n   } n   for (int i = 1; i <= n; i++) { n     if (mul(get_fac(i - 2), tot_comb[n - i]) >= k) { n       KthPermhelp(i, (k / tot_comb[n - i]) + (k % tot_comb[n - i] > 0), st); n       KthPerm(n - i, n               (k % tot_comb[n - i]) + n                   ((k % tot_comb[n - i] == 0) ? tot_comb[n - i] : 0), n               st + i); n       break; n     } n     k -= mul(get_fac(i - 2), tot_comb[n - i]); n   } n } n int main() { n   fill_fac(); n   fill_tot_comb(); n   int t; n   cin >> t; n   while (t--) { n     int n; n     long long int k; n     cin >> n >> k; n     KthPerm(n, k, 0); n     cout << endl; n   } n } n


#include <bits/stdc++.h> n  n #define pb push_back n #define int long long  n #define debug(n) cerr << #n <<   =   << n <<   n ; n  n using namespace std; n  n const int MaxN = 1e6 + 10; n const int Mod = 1e9 + 7; n const int inf = 1e18; n int cnt[100] , a[MaxN]; n  n void solve() { n  int n; n  cin >> n; n  for(int i = 0; i < n; i++) { n   cin >> a[i]; n   for(int j = 0; j <= 60; j++)  n    if(a[i] & (1LL << j)) cnt[j]++; n  } n  int ans = 0; n  for(int i = 0; i < n; i++) { n   int x = 0 , y = 0; n   for(int j = 0; j <= 60; j++) { n    int p = (1LL << j) % Mod; n    if(a[i] & (1LL << j)) { n     x = (x + p * cnt[j]) % Mod; n     y = (y + p * n) % Mod; n    } else y = (y + p * cnt[j]) % Mod; n   } n   ans = (ans + x * y) % Mod; n  } n  cout << ans <<   n ; n  fill(cnt , cnt + 100 , 0); n } n  n int32_t main() { n  ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); n  int t = 1; n  cin >> t; n  while(t--)  n   solve(); n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n const int mo = 998244353; n int fac[300005], inv[300005]; n void init() { n   fac[0] = inv[0] = inv[1] = 1; n   for (int i = (int)(2); i <= (int)(300005 - 1); i++) n     inv[i] = 1ll * inv[mo % i] * (mo - mo / i) % mo; n   for (int i = (int)(1); i <= (int)(300005 - 1); i++) { n     fac[i] = 1ll * fac[i - 1] * i % mo; n     inv[i] = 1ll * inv[i - 1] * inv[i] % mo; n   } n } n int power(int x, int y) { n   int s = 1; n   for (; y;) { n     if (y & 1) s = 1ll * s * x % mo; n     if (y /= 2) x = 1ll * x * x % mo; n   } n   return s; n } n const int N = 100005; n int freq[N], cnt[N]; n void add(int x) { n   cnt[freq[x]]--; n   cnt[++freq[x]]++; n } n void del(int x) { n   cnt[freq[x]]--; n   cnt[--freq[x]]++; n } n vector<int> heavy; n int n, m, Q; n int e[N], ans[N]; n struct que { n   int l, r, k, id; n } q[N]; n const int blk = 200; n bool cmpk(que a, que b) { n   return pair<int, int>(a.k, a.l - a.r) < pair<int, int>(b.k, b.l - b.r); n } n bool cmpmo(que a, que b) { n   return pair<int, int>(a.l / blk, a.r) < pair<int, int>(b.l / blk, b.r); n } n int main() { n   init(); n   scanf( %d%d%d , &n, &m, &Q); n   for (int i = (int)(1); i <= (int)(n); i++) { n     scanf( %d , &e[i]); n     cnt[e[i]]++; n   } n   for (int i = (int)(1); i <= (int)(m); i++) n     if (cnt[i] >= blk) heavy.push_back(i); n   for (int i = (int)(1); i <= (int)(Q); i++) { n     scanf( %d%d%d , &q[i].l, &q[i].r, &q[i].k); n     q[i].id = i; n   } n   sort(q + 1, q + Q + 1, cmpk); n   for (int r = 1; r <= Q;) { n     int l = r, kk = q[l].k, res = 1, las = n, v = 1; n     for (; r <= Q && kk == q[r].k; r++) n       ; n     for (int i = (int)(1); i <= (int)(m); i++) n       res = 1ll * res * fac[kk + cnt[i]] % mo; n     for (int i = (int)(l); i <= (int)(r - 1); i++) { n       int len = q[i].r - q[i].l + 1; n       for (; las > len; v = 1ll * (1ll * m * kk + n - (--las)) % mo * v % mo) n         ; n       ans[q[i].id] = 1ll * res * v % mo; n     } n   } n   sort(q + 1, q + Q + 1, cmpmo); n   int l = 1, r = 0; n   memset(cnt, 0, sizeof(cnt)); n   ; n   cnt[0] = m; n   for (int i = (int)(1); i <= (int)(n); i++) add(e[i]); n   for (int i = (int)(1); i <= (int)(Q); i++) { n     for (; q[i].l < l; del(e[--l])) n       ; n     for (; q[i].l > l; add(e[l++])) n       ; n     for (; q[i].r < r; add(e[r--])) n       ; n     for (; q[i].r > r; del(e[++r])) n       ; n     int res = 1; n     for (int j = (int)(0); j <= (int)(blk - 1); j++) n       if (cnt[j]) res = 1ll * res * power(inv[q[i].k + j], cnt[j]) % mo; n     for (int j : heavy) n       if (freq[j] >= blk) res = 1ll * res * inv[q[i].k + freq[j]] % mo; n     ans[q[i].id] = 1ll * ans[q[i].id] * res % mo; n   } n   for (int i = (int)(1); i <= (int)(Q); i++) printf( %d n , ans[i]); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = (int)1e6 + 7; n const int MOD = (int)1e9 + 7; n const long long INF = (long long)1e18 + 7; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   vector<int> p(n + 1, 0); n   for (int i = 2; i * i <= n; i++) n     if (p[i] == 0) { n       p[i] = i; n       for (int j = i * i; j <= n; j += i) n         if (!p[j]) p[j] = i; n     } n   for (int i = 1; i <= n; i++) n     if (!p[i]) p[i] = i; n   vector<int> ans; n   for (int i = 1; i <= n; i++) ans.push_back(i / p[i]); n   sort(ans.begin(), ans.end()); n   for (int i = 1; i < n; i++) cout << ans[i] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int k, p; n   double s, n; n   cin >> k >> n >> s >> p; n   for (int i = 0; i < 10000000000000000; i++) { n     if (((i * p) / k) >= n / s) { n       cout << i; n       break; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, q, a[300003], near[300003][20], last[300003]; n signed main() { n   cin >> n >> q; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   for (int i = n; i >= 1; i--) { n     for (int k = 0; k < 20; k++) { n       near[i][k] = n + 1; n       if (((1 << k) & a[i]) && last[k]) near[i][k] = last[k]; n       for (int j = 0; j < 20; j++) { n         if (last[j] && ((a[i] >> j) & 1)) { n           near[i][k] = min(near[i][k], near[last[j]][k]); n         } n       } n       if (near[i][k] == n + 1) near[i][k] = i; n     } n     for (int k = 0; k < 20; k++) n       if ((a[i] >> k) & 1) last[k] = i; n   } n   while (q--) { n     int l, r; n     cin >> l >> r; n     for (int i = 0; i < 20; i++) { n       if (((a[r] >> i) & 1) && near[l][i] != l && near[l][i] <= r) { n         puts( Shi ); n         l = r; n         break; n       } n     } n     if (l < r) puts( Fou ); n   } n } n


#include<bits/stdc++.h> n using namespace std; n #define    ll    long long int n #define pi              2*acos(0.0) n #define gcd(a,b)        __gcd(a,b) n #define lcm(a,b)        (a*b)/gcd(a,b) n map<ll,ll>mp,mk; n //map<string,ll>mp; n set<ll> s1,s2; n vector<int> v,w; n string s=  ,qq=  ,ss=  ,pp=  ; n char ch; n ll m,n,i,j,l,r,x,t,y,u,g,h,mn,mx,d,z,c,f,e,k,o; n int main () n { n     cin>>t; n     while(t--) n     { n         cin>>n>>k; n         for(i=0; i<k-3; i++) n             cout<< 1  ; n         n=n-k+3; n         c=n-2; n         o=n/2; n         if(n%2) n             cout<<n/2<<   <<n/2<<   <<1; n         else if(o%2==0) n             cout<<n/2<<   <<o/2<<   <<o/2; n         else if(o%2) n             cout<<c/2<<   <<c/2<<   <<2; n         cout<<  n ; n     } n } n


#include <bits/stdc++.h> n int dx[] = {0, 0, 1, -1, 2, 2, -2, -2, 1, -1, 1, -1}; n int dy[] = {1, -1, 0, 0, 1, -1, 1, -1, 2, 2, -2, -2}; n using namespace std; n vector<long long int> V, V2, V3, edj[200010]; n bitset<200010> color; n long long int root, cnt = 0; n void dfs(long long int n, long long int ck) { n   color[n] = true; n   for (long long int i : edj[n]) { n     if (color[i] == false) { n       color[i] = true; n       long long int tm = ck ^ 1; n       if (tm == 0) { n         cnt++; n       } n       dfs(i, tm); n     } n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long int tcase = 1; n   for (long long int test = 1; test <= tcase; test++) { n     long long int n; n     cin >> n; n     for (long long int i = 0; i < n - 1; i++) { n       long long int u, v; n       cin >> u >> v; n       edj[u].push_back(v); n       edj[v].push_back(u); n     } n     for (long long int i = 1; i <= n; i++) { n       if ((long long int)edj[i].size() == 1) { n         root = i; n         break; n       } n     } n     dfs(root, 0); n     long long int ans = cnt; n     cnt = 0; n     for (long long int i = 0; i <= n; i++) color[i] = false; n     root = edj[root][0]; n     dfs(root, 0); n     ans = min(ans, cnt); n     cout << ans <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const double pi = 3.141592653589793238463; n int main() { n   cin.tie(0); n   cin.sync_with_stdio(0); n   long long a, b; n   cin >> a >> b; n   while (a != 0 && b != 0) { n     if (a < 2 * b && b < 2 * a) break; n     if (a >= 2 * b) n       a %= 2 * b; n     else n       b %= 2 * a; n   } n   cout << a <<     << b <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int i, j, l, n, k, d, max = 0, gen, e = 0; n   long long p; n   scanf( %d %d , &n, &k); n   vector<vector<int> > v(n); n   for (i = 0; i < n; i++) { n     scanf( %d , &d); n     v[d].push_back(i); n     if (d > max) max = d; n   } n   p = 1; n   for (i = 0; i <= max; i++) { n     gen = v[i].size(); n     if (i > 0) e += gen; n     if (gen > p || gen <= 0) break; n     p = gen; n     if (i == 0) n       p = k; n     else n       p = p * (k - 1); n   } n   if (i <= max) n     printf( -1 n ); n   else { n     printf( %d n , e); n     for (i = 0; i < max; i++) { n       gen = 0; n       if (i == 0) n         e = k; n       else n         e = k - 1; n       for (j = 0; j < (int)(v[i].size()); j++) { n         for (l = gen; l < gen + e && l < (int)(v[i + 1].size()); l++) { n           printf( %d %d n , v[i][j] + 1, v[i + 1][l] + 1); n         } n         gen += e; n         if (gen >= v[i + 1].size()) break; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 7; n int n, m, q; n vector<int> E[maxn]; n int op[maxn], a[maxn], b[maxn], c[maxn]; n int ans[maxn]; n bitset<1001> Bit[1001]; n bitset<1001> C; n void dfs(int x) { n   if (x == 0) { n     for (int i = 0; i < E[x].size(); i++) { n       ans[E[x][i]] = ans[x]; n       dfs(E[x][i]); n     } n   } n   if (op[x] == 1) { n     int flag = 0; n     if (!Bit[a[x]][b[x]]) { n       flag = 1; n       ans[x]++; n       Bit[a[x]][b[x]] = 1; n     } n     for (int i = 0; i < E[x].size(); i++) { n       ans[E[x][i]] = ans[x]; n       dfs(E[x][i]); n     } n     if (flag) Bit[a[x]][b[x]] = 0; n   } n   if (op[x] == 2) { n     int flag = 0; n     if (Bit[a[x]][b[x]]) { n       flag = 1; n       ans[x]--; n       Bit[a[x]][b[x]] = 0; n     } n     for (int i = 0; i < E[x].size(); i++) { n       ans[E[x][i]] = ans[x]; n       dfs(E[x][i]); n     } n     if (flag) Bit[a[x]][b[x]] = 1; n   } n   if (op[x] == 3) { n     ans[x] = ans[x] - Bit[a[x]].count(); n     Bit[a[x]] ^= C; n     ans[x] = ans[x] + Bit[a[x]].count(); n     for (int i = 0; i < E[x].size(); i++) { n       ans[E[x][i]] = ans[x]; n       dfs(E[x][i]); n     } n     Bit[a[x]] ^= C; n   } n   if (op[x] == 4) { n     for (int i = 0; i < E[x].size(); i++) { n       ans[E[x][i]] = ans[x]; n       dfs(E[x][i]); n     } n   } n } n int main() { n   scanf( %d%d%d , &n, &m, &q); n   for (int i = 1; i <= m; i++) C[i] = 1; n   for (int i = 1; i <= q; i++) { n     scanf( %d , &op[i]); n     if (op[i] == 1) scanf( %d%d , &a[i], &b[i]); n     if (op[i] == 2) scanf( %d%d , &a[i], &b[i]); n     if (op[i] == 3) scanf( %d , &a[i]); n     if (op[i] == 4) n       scanf( %d , &a[i]), E[a[i]].push_back(i); n     else n       E[i - 1].push_back(i); n   } n   dfs(0); n   for (int i = 1; i <= q; i++) printf( %d n , ans[i]); n } n


#include <bits/stdc++.h> n using namespace std; n long long a, b, c, i, l, j, x, s, y, ans; n int main() { n   cin >> a >> b >> c >> l; n   s = 0; n   for (i = l + 1; i >= 1; i--) s += i * (i + 1) / 2; n   ans = 0; n   for (i = 0; i <= l; i++) { n     x = a + i; n     if (b > x || c > x) continue; n     y = min(l - i, x - b - c) + 1; n     if (y < 0) y = 0; n     ans += (1 + y) * y / 2; n   } n   for (i = 0; i <= l; i++) { n     x = b + i; n     if (a > x || c > x) continue; n     y = min(l - i, x - a - c) + 1; n     if (y < 0) y = 0; n     ans += (1 + y) * y / 2; n   } n   for (i = 0; i <= l; i++) { n     x = c + i; n     if (a > x || b > x) continue; n     y = min(l - i, x - a - b) + 1; n     if (y < 0) y = 0; n     ans += (1 + y) * y / 2; n   } n   cout << s - ans; n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:256000000 ) n using namespace std; n const int MAXN = 3005; n int r, c, n, k; n pair<int, int> v[MAXN]; n int prv[MAXN], nxt[MAXN]; n vector<int> y[MAXN], x[MAXN]; n int pos[MAXN]; n int num[MAXN]; n int cnt; n long long ans; n int res; n bool cmp_x(int num1, int num2) { return v[num1].first < v[num2].first; } n bool cmp_y(int num1, int num2) { return v[num1].second < v[num2].second; } n int trivia(int x1, int x2) { n   int cnt[MAXN]; n   memset(cnt, 0, sizeof(cnt)); n   for (int yy = 1; yy <= c; yy++) { n     for (int i = 0; i < y[yy].size(); i++) { n       if (x1 <= v[y[yy][i]].first && v[y[yy][i]].first <= x2) { n         cnt[yy]++; n       } n     } n   } n   int res = 0; n   for (int yy1 = 1; yy1 <= c; yy1++) { n     int cur = 0; n     for (int y2 = yy1; y2 <= c; y2++) { n       cur += cnt[y2]; n       if (cur >= k) { n         res++; n       } n     } n   } n   return res; n } n void solve() { n   scanf( %d%d%d%d , &r, &c, &n, &k); n   for (int i = 1; i <= n; i++) { n     scanf( %d%d , &v[i].first, &v[i].second); n     y[v[i].second].push_back(i); n     x[v[i].first].push_back(i); n   } n   for (int i = 1; i <= r; i++) { n     sort(x[i].begin(), x[i].end(), cmp_y); n   } n   for (int i = 1; i <= c; i++) { n     sort(y[i].begin(), y[i].end(), cmp_x); n   } n   v[n + 1].second = c + 1; n   for (int i = 1; i <= r; i++) { n     cnt = 0; n     res = 0; n     for (int j = 1; j <= c; j++) { n       for (int h = (int)y[j].size() - 1; h >= 0; h--) { n         int curnum = y[j][h]; n         if (v[curnum].first < i) { n           break; n         } n         pos[curnum] = ++cnt; n         num[cnt] = curnum; n         prv[cnt] = cnt - 1; n         nxt[cnt - 1] = cnt; n       } n     } n     nxt[cnt] = cnt + 1; n     num[cnt + 1] = n + 1; n     nxt[cnt + 1] = 0; n     for (int j = k; j <= cnt; j++) { n       res += (v[num[j + 1]].second - v[num[j]].second) * n              (v[num[j - k + 1]].second); n     } n     for (int j = r; j >= i; j--) { n       ans += res; n       for (int h = 0; h < x[j].size(); h++) { n         vector<int> all; n         all.reserve(k << 1); n         int back = 0; n         int p = prv[pos[x[j][h]]]; n         while (p && all.size() < k - 1) { n           all.push_back(p); n           p = prv[p]; n         } n         back = all.size(); n         reverse(all.begin(), all.end()); n         if (p) { n           res += n               (v[num[nxt[pos[x[j][h]]]]].second - v[num[pos[x[j][h]]]].second) * n               (v[num[p]].second); n         } n         p = pos[x[j][h]]; n         while (p && all.size() <= k + back) { n           all.push_back(p); n           p = nxt[p]; n         } n         for (int l = max(k - 1, back); l + 1 < all.size(); l++) { n           res -= (v[num[all[l + 1]]].second - v[num[all[l]]].second) * n                  (v[num[all[l - k + 1]]].second); n         } n         for (int l = max(k, back + 1); l + 1 < all.size(); l++) { n           res += (v[num[all[l + 1]]].second - v[num[all[l]]].second) * n                  (v[num[all[l - k]]].second); n         } n         prv[nxt[pos[x[j][h]]]] = prv[pos[x[j][h]]]; n         nxt[prv[pos[x[j][h]]]] = nxt[pos[x[j][h]]]; n       } n     } n   } n   printf( %I64d n , ans); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   srand(1313); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MAXN = 2e6; n long long cnt[MAXN]; n signed main() { n   long long n, m, k; n   ios_base::sync_with_stdio(false); n   cin >> n >> m >> k; n   vector<long long> f(n); n   vector<pair<long long, long long> > s(m); n   for (long long i = 0; i < n; i++) cin >> f[i]; n   for (long long i = 0; i < m; i++) cin >> s[i].first; n   for (long long i = 0; i < m; i++) s[i].second = i; n   sort(s.begin(), s.end()); n   reverse(s.begin(), s.end()); n   fill(cnt, cnt + MAXN, 0); n   for (auto ff : f) { n     if (ff < MAXN) { n       cnt[ff]++; n     } n   } n   long long taken = 0; n   bool flag = true; n   for (long long i = 0; flag && i < (n + k - 1) / k; i++) { n     taken += cnt[i]; n     long long needed = min(n, (i + 1) * k); n     flag &= (taken <= needed); n   } n   if (!flag) { n     cout << -1 << endl; n   } else { n     long long left = 0, right = m + 1; n     while (left + 1 < right) { n       long long med = (left + right) / 2; n       for (long long i = 0; i < med; i++) { n         if (s[i].first < MAXN) { n           cnt[s[i].first]++; n         } n       } n       taken = 0; n       bool flag = true; n       for (long long i = 0; flag && i < (n + k + med - 1) / k; i++) { n         taken += cnt[i]; n         long long needed = min(n + med, (i + 1) * k); n         flag &= (taken <= needed); n       } n       for (long long i = 0; i < med; i++) { n         if (s[i].first < MAXN) { n           cnt[s[i].first]--; n         } n       } n       if (flag) { n         left = med; n       } else { n         right = med; n       } n     } n     cout << left << endl; n     for (long long i = 0; i < left; i++) { n       cout << s[i].second + 1 <<    ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int s, v1, v2, t1, t2, i1, i2; n   cin >> s >> v1 >> v2 >> t1 >> t2; n   i1 = s * v1 + 2 * t1; n   i2 = s * v2 + 2 * t2; n   if (i1 < i2) n     cout <<  First ; n   else if (i1 > i2) n     cout <<  Second ; n   else n     cout <<  Friendship ; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 500 + 10; n const int P = 998244353; n int N, X, f[MAXN][MAXN], pw[MAXN][MAXN], binom[MAXN][MAXN]; n void add(int& a, int b) { a = a + b >= P ? a + b - P : a + b; } n int dif(int a, int b) { return a - b < 0 ? a - b + P : a - b; } n int main() { n   for (int i = (0), iend = (MAXN); i < iend; ++i) { n     pw[i][0] = (i > 0); n     for (int j = (1), jend = (MAXN); j < jend; ++j) n       pw[i][j] = 1ll * pw[i][j - 1] * i % P; n   } n   binom[0][0] = 1; n   for (int i = (1), iend = (MAXN); i < iend; ++i) { n     binom[i][0] = 1; n     for (int j = (1), jend = (i + 1); j < jend; ++j) n       binom[i][j] = binom[i - 1][j - 1], add(binom[i][j], binom[i - 1][j]); n   } n   for (int i = (1), iend = (MAXN); i < iend; ++i) f[0][i] = 1; n   for (int i = (2), iend = (MAXN); i < iend; ++i) n     for (int j = (1), jend = (MAXN); j < jend; ++j) { n       for (int x = i - 1; x < j; x += i - 1) { n         for (int k = (1), kend = (i); k < kend; ++k) n           add(f[i][j], n               1ll * binom[i][k] * f[i - k][j - x] % P * pw[i - 1][k] % P); n       } n       for (int x = 0; x < j; x += i - 1) add(f[i][j], pw[min(i - 1, j - x)][i]); n     } n   scanf( %d %d , &N, &X); n   printf( %d n , f[N][X]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 10; n vector<int> e[maxn], re[maxn], List; n int n, m, id[maxn], tt, cnt[maxn], ans; n bool vis[maxn]; n void input() { n   scanf( %d%d , &n, &m); n   for (int u, v, i = 1; i <= m; i++) { n     scanf( %d%d , &u, &v); n     e[u].push_back(v); n     re[v].push_back(u); n   } n } n void dfs(int u) { n   vis[u] = 1; n   for (vector<int>::iterator it = e[u].begin(); it != e[u].end(); it++) { n     if (!vis[*it]) { n       dfs(*it); n     } n   } n   List.push_back(u); n } n void rdfs(int u, int k) { n   id[u] = k; n   cnt[k]++; n   for (vector<int>::iterator it = re[u].begin(); it != re[u].end(); it++) { n     if (!id[*it]) { n       rdfs(*it, k); n     } n   } n } n void scc() { n   tt = 0; n   for (int i = 1; i <= n; i++) n     if (!vis[i]) dfs(i); n   reverse(List.begin(), List.end()); n   for (vector<int>::iterator it = List.begin(); it != List.end(); it++) n     if (!id[*it]) { n       tt++; n       rdfs(*it, tt); n     } n } n void bfs(int u) { n   queue<int> q; n   while (!q.empty()) q.pop(); n   q.push(u); n   vis[u] = 1; n   int c = 0, x = 0; n   bool ok = 0; n   while (!q.empty()) { n     u = q.front(); n     q.pop(); n     if (cnt[id[u]]) { n       c += cnt[id[u]] - 1; n       x++; n       if (cnt[id[u]] > 1) ok = 1; n       cnt[id[u]] = 0; n     } n     for (vector<int>::iterator it = e[u].begin(); it != e[u].end(); it++) { n       if (!vis[*it]) { n         vis[*it] = 1; n         q.push(*it); n       } n     } n     for (vector<int>::iterator it = re[u].begin(); it != re[u].end(); it++) { n       if (!vis[*it]) { n         vis[*it] = 1; n         q.push(*it); n       } n     } n   } n   ans += c + x - 1; n   if (ok) ans++; n } n void solve() { n   memset(vis, 0, sizeof vis); n   for (int i = 1; i <= n; i++) n     if (!vis[i]) { n       bfs(i); n     } n   printf( %d , ans); n } n int main() { n   input(); n   scc(); n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n const long long int mod = 1e9 + 7; n const long long int inf = 1e18; n const double EPS = 1E-9; n int n, m; n int main(void) { n   cin >> n >> m; n   if (m > n) { n     int temp = m; n     m = n; n     n = temp; n   } n   if (m == 1) { n     cout << n << endl; n   } else if (m == 2) { n     cout << 2 * (2 * (n / 4) + min(n % 4, 2)) << endl; n   } else if (m >= 3) { n     if ((n == 3 && m == 3) || (n == 3 && m == 5) || (n == 3 && m == 6) || n         (n == 4 && m == 4)) { n       if (n == 3 && m == 3) { n         cout << 5 << endl; n       } else if (n == 3 && m == 5) { n         cout << 7 << endl; n       } else if (n == 3 && m == 6) { n         cout << 7 << endl; n       } else { n         cout << 8 << endl; n       } n     } else { n       cout << m * n - (m * n) / 2 << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 11111111; n const long long INF = (long long)(1e12); n const long long mod = 998244353; n const double eps = 1e-9; n long long n, mx = -1, inv[N], a[N], k, all, cnt, ans; n map<long long, long long, greater<long long> > M, S; n long long get(long long x) { return (x) * (x + 1) / 2; } n int main() { n   ios_base ::sync_with_stdio(false); n   cin >> n >> k; n   all = (1ll << k) - 1; n   ans = get(n); n   M[0]++; n   for (long long i = 1; i <= n; ++i) { n     cin >> a[i]; n     a[i] ^= a[i - 1]; n     inv[i] = (all ^ a[i]); n     if (M[a[i]] < M[inv[i]]) { n       ans -= M[a[i]]; n       M[a[i]]++; n     } else { n       ans -= M[inv[i]]; n       M[inv[i]]++; n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename TYPE> n inline TYPE sqr(const TYPE& a) { n   return (a) * (a); n } n const int INF = 1000 * 1000 * 1000; n const long double EPS = 1e-9; n const long double PI = 2 * acos(0.0); n const int N = 100100; n int top; n int j; n int a[N]; n string ans; n bool f() { n   if (j >= top) return false; n   if (a[j] == 0) { n     ++j; n     ans.append( int ); n     return true; n   } n   ++j; n   ans.append( pair< ); n   if (!f()) return false; n   ans.append( , ); n   if (!f()) return false; n   ans.append( > ); n   return true; n } n int main() { n   int n; n   scanf( %d n , &n); n   string s; n   getline(cin, s); n   for (int i = 0; i < int(s.size()); ++i) n     if (s[i] ==  t ) n       a[top++] = 0; n     else if (s[i] ==  p ) n       a[top++] = 1; n   if (!f() || j < top) { n     puts( Error occurred ); n   } else { n     puts(ans.c_str()); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = 3.1415926535897932238460; n bool arr[361]; n void angles() { n   for (int i = 3; i < 361; i++) n     if ((i - 2) * 180 % i == 0) { n       arr[(i - 2) * 180 / i] = true; n     } n } n int main() { n   int t; n   cin >> t; n   angles(); n   while (t--) { n     int k; n     cin >> k; n     if (arr[k]) n       cout <<  YES  n            <<   n ; n     else n       cout <<  NO  n            <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n bool a[1100][26]; n vector<bool> v(1100, false); n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     string s; n     cin >> s; n     for (auto c : s) a[i][c - 97] = true; n   } n   for (int i = 0; i < n; i++) { n     if (v[i]) continue; n     for (int j = i + 1; j < n; j++) { n       bool b = true; n       for (int g = 0; g < 26; g++) n         if (a[i][g] != a[j][g]) b = false; n       if (b) { n         v[j] = true; n       } n     } n   } n   int ans = 0; n   for (int i = 0; i < n; i++) n     if (!v[i]) ans++; n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n template <class T> n void read(T &x) { n   x = 0; n   int f = 0; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) f |= (ch ==  - ), ch = getchar(); n   while (ch <=  9  && ch >=  0 ) n     x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar(); n   x = f ? -x : x; n   return; n } n const int maxn = 5007; n char c[maxn], a[maxn]; n int t[2][2]; n int main() { n   int n; n   read(n); n   scanf( %s , c + 1); n   scanf( %s , a + 1); n   int len = n / 2; n   for (int i = 1; i <= n; i++) n     if (c[i] ==  1  && a[i] ==  1 ) n       t[1][1]++; n     else if (c[i] ==  1  && a[i] ==  0 ) n       t[1][0]++; n     else if (c[i] ==  0  && a[i] ==  1 ) n       t[0][1]++; n     else n       t[0][0]++; n   for (int i = 0; i <= len; i++) n     for (int j = 0; j <= i; j++) { n       if (j > t[1][1]) continue; n       int k = i - j; n       if (k > t[1][0]) continue; n       int re11 = t[1][1] - j; n       if (re11 > i) continue; n       int gal = i - re11; n       if (t[0][1] >= gal) { n         int x = j, y = k, z = t[0][1] - gal; n         int res = len - i - z; n         if (res < 0 || res > t[0][0]) continue; n         for (int q = 1; q <= n; q++) { n           if (c[q] ==  1  && a[q] ==  1  && x > 0) { n             printf( %d  , q); n             x--; n           } n           if (c[q] ==  1  && a[q] ==  0  && y > 0) { n             printf( %d  , q); n             y--; n           } n           if (c[q] ==  0  && a[q] ==  1  && z > 0) { n             printf( %d  , q); n             z--; n           } n           if (c[q] ==  0  && a[q] ==  0  && res > 0) { n             printf( %d  , q); n             res--; n           } n         } n         return 0; n       } n     } n   printf( -1 n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 40; n void solve() { n   long long int n, m, k; n   cin >> n >> m >> k; n   long long int beg = 1, end = n * m; n   long long int fa; n   while (beg <= end) { n     long long int mid = (beg + end) / 2; n     long long int cnt = 0; n     for (int i = 1; i <= n; i++) { n       cnt += min(m, mid / i); n     } n     if (cnt >= k) { n       end = mid - 1; n       fa = mid; n     } else n       beg = mid + 1; n   } n   cout << fa <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int T = 1; n   while (T--) { n     solve(); n     if (T) { n       char c; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e9; n const int N = 1e7 + 100; n unordered_map<int, int> vis, Cnt; n vector<int> Mice; n vector<int> Cheese; n int main() { n   int n, m, y0, y1; n   scanf( %d%d%d%d , &n, &m, &y0, &y1); n   for (int i = 0; i < n; i++) { n     int x; n     scanf( %d , &x); n     Mice.push_back(x); n   } n   for (int i = 0; i < m; i++) { n     int x; n     scanf( %d , &x); n     Cheese.push_back(x); n   } n   int Ans = 0; n   for (int x : Mice) { n     int L = INF, R = INF; n     auto it = lower_bound(Cheese.begin(), Cheese.end(), x); n     if (it != Cheese.end()) R = *it; n     if (it != Cheese.begin()) it--; n     L = *it; n     int cur = min(abs(x - L), abs(R - x)); n     if (x - L == R - x) { n       if (!vis.count(L)) n         vis[L] = x - L, Cnt[L] = 1; n       else { n         if (vis[L] == x - L) n           Cnt[L]++; n         else { n           if (!vis.count(R)) n             vis[R] = cur, Cnt[R] = 1; n           else if (vis[R] > cur) { n             Ans += vis[R]; n             vis[R] = cur; n             Cnt[R] = 1; n           } else if (vis[R] == cur) n             Cnt[R]++; n           else n             Ans++; n         } n       } n     } else { n       int d = (x - L < R - x ? L : R); n       if (!vis.count(d)) n         vis[d] = cur, Cnt[d] = 1; n       else { n         if (vis[d] > cur) { n           Ans += Cnt[d]; n           vis[d] = cur; n           Cnt[d] = 1; n         } else if (vis[d] < cur) n           Ans++; n         else n           Cnt[d]++; n       } n     } n   } n   printf( %d n , Ans); n   return 0; n } n


#include<bits/stdc++.h> n #define x first n #define y second n  n using namespace std; n  n typedef long long LL; n typedef long double LD; n typedef pair<int,int> PII; n  n const int N=100005; n  n int n,m,T; n  n int main(){ n  cin>>T; n     int k1,k2,t1,t2,x1,x2; n     while(T--){ n         cin>>n>>k1>>k2; n         cin>>t1>>t2; n         x1=(k1+k2)/2; n         if((k1+k2)&1) n--; n         x2=n-x1; n         if( t1<=x1 && t2<=x2 ) cout<< YES <<endl; n         else cout<< NO <<endl; n     } n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n int n, size[200010] = {0}; n vector<int> w[200010]; n int dpMax(int, int); n int dpMin(int, int); n int child(int x) { n   int ret = 0; n   for (int i = 0; i < w[x].size(); i++) ret += child(w[x][i]); n   return size[x] = ret + !w[x].size(); n } n int dpMax(int x, int t) { n   int ret1, ret2; n   ret1 = ret2 = !w[x].size(); n   for (int i = 0; i < w[x].size(); i++) { n     if (!t) n       ret1 = max(ret1, size[x] - size[w[x][i]] + dpMin(w[x][i], 0)); n     else n       ret2 += (dpMin(w[x][i], 1)); n   } n   return !t ? ret1 : ret2; n } n int dpMin(int x, int t) { n   int ret1, ret2; n   ret1 = 1; n   ret2 = 1 + (1 << 28) * (w[x].size() > 0); n   for (int i = 0; i < w[x].size(); i++) { n     if (!t) n       ret1 += (dpMax(w[x][i], 0) - 1); n     else n       ret2 = min(ret2, (dpMax(w[x][i], 1))); n   } n   return !t ? ret1 : ret2; n } n int main() { n   scanf( %d , &n); n   for (int i = 0, a, b; i < n - 1; i++) { n     scanf( %d%d , &a, &b); n     w[a].push_back(b); n   } n   child(1); n   printf( %d %d n , dpMax(1, 0), dpMax(1, 1)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int P = 1e9 + 7; n int n, m; n namespace flow { n int n, s, t; n int head[200306], to[200306 << 1], nex[200306 << 1], wto[200306 << 1], ecn = 1; n int ade(int u, int v, int w) { n   to[++ecn] = v, nex[ecn] = head[u], wto[ecn] = w, head[u] = ecn; n   to[++ecn] = u, nex[ecn] = head[v], wto[ecn] = 0, head[v] = ecn; n   return ecn; n } n queue<int> Q; n int dep[200306], cur[200306]; n const int inf = 1e9; n void clear() { n   for (int i = (1), iend = (n); i <= iend; ++i) head[i] = dep[i] = cur[i] = 0; n   ecn = 1; n } n bool bfs() { n   Q.push(s); n   for (int i = (1), iend = (n); i <= iend; ++i) cur[i] = head[i]; n   memset(dep, 0x3f, sizeof dep); n   dep[s] = 0; n   while (!Q.empty()) { n     int u = Q.front(); n     Q.pop(); n     for (int i = head[u]; i; i = nex[i]) n       if (dep[to[i]] > inf && wto[i]) dep[to[i]] = dep[u] + 1, Q.push(to[i]); n   } n   return dep[t] < inf; n } n long long dfs(int u, long long lim) { n   if (!lim || u == t) return lim; n   long long flow = 0, f; n   for (int& i = cur[u]; i; i = nex[i]) n     if (dep[to[i]] == dep[u] + 1 && (f = dfs(to[i], min(lim, 1ll * wto[i])))) { n       lim -= f, flow += f, wto[i] -= f, wto[i ^ 1] += f; n       if (!lim) break; n     } n   return flow; n } n long long dinic() { n   long long res = 0; n   while (bfs()) res += dfs(s, 0x3f3f3f3f3f3f3f3f); n   return res; n } n }  // namespace flow n pair<int, int> ed[200306]; n int bc[200306]; n long long chk(int x) { n   for (int i = (1), iend = (n); i <= iend; ++i) flow::ade(n + m + 1, i, x); n   for (int i = (1), iend = (m); i <= iend; ++i) { n     int u = ed[i].first, v = ed[i].second; n     bc[i] = flow::ade(u, i + n, 1); n     flow::ade(v, i + n, 1); n     flow::ade(i + n, n + m + 2, 1); n   } n   long long as = flow::dinic(); n   flow::clear(); n   return as; n } n void solve() { n   cin >> n >> m; n   for (int i = (1), iend = (m); i <= iend; ++i) { n     int u, v; n     scanf( %d%d , &u, &v); n     ed[i] = make_pair(u, v); n   } n   int l = 0, r = n; n   flow::s = n + m + 1, flow::t = n + m + 2, flow::n = n + m + 2; n   while (l <= r) { n     int mid = l + r >> 1; n     long long as = chk(mid); n     if (as == m) { n       r = mid - 1; n     } else n       l = mid + 1; n   } n   cout << l << endl; n   chk(l); n   for (int i = (1), iend = (m); i <= iend; ++i) { n     int u = ed[i].first, v = ed[i].second; n     if (flow::wto[bc[i]]) n       printf( %d %d n , u, v); n     else n       printf( %d %d n , v, u); n   } n } n signed main() { solve(); } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 2e5 + 5; n vector<pair<int, int> > adj[MAXN]; n int n; n bool mark[MAXN]; n int p[MAXN]; n int curv; n void dfs(int v, int ind = -1) { n   mark[v] = 1; n   for (int i = 0; i < adj[v].size(); i++) { n     pair<int, int> u = adj[v][i]; n     if (!mark[u.first]) { n       dfs(u.first, u.second); n     } else if (u.second != ind) { n       curv = u.first; n     } n   } n } n int main() { n   cin >> n; n   int x; n   int d = -1; n   for (int i = 1; i <= n; i++) { n     cin >> x; n     if (x == i) { n       d = i; n     } n     p[i] = x; n     adj[i].push_back({x, i}); n     adj[x].push_back({i, i}); n   } n   int cnt = 0; n   vector<int> ve; n   for (int i = 1; i <= n; i++) { n     if (!mark[i]) { n       dfs(i); n       cnt++; n       ve.push_back(curv); n     } n   } n   if (d != -1) { n     cout << cnt - 1 <<   n ; n     for (int i = 0; i < ve.size(); i++) { n       p[ve[i]] = d; n     } n     for (int i = 1; i <= n; i++) { n       cout << p[i] <<    ; n     } n     return 0; n   } n   cout << cnt <<   n ; n   for (int i = 0; i < ve.size(); i++) { n     p[ve[i]] = ve[0]; n   } n   for (int i = 1; i <= n; i++) { n     cout << p[i] <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 2000; n const int MAXE = 30001; n const int INF = 2000000002; n struct edge { n   int to; n   int w; n   int id; n   edge() {} n   edge(int to, int w, int id) : to(to), w(w), id(id) {} n   bool operator==(const edge& e) const { n     return to == e.to && w == e.w && id == e.id; n   } n }; n int cost[MAXE]; n int conn[MAXN][MAXN]; n int find_path(int s, int t, const vector<vector<edge> >& g, vector<edge>& path, n               vector<int>& used) { n   used[s] = 1; n   if (s == t) { n     return 1; n   } n   for (auto& val : g[s]) { n     int to = val.to; n     if (!used[to]) { n       path.push_back(val); n       int res = find_path(to, t, g, path, used); n       if (res) { n         return res; n       } n       path.pop_back(); n     } n   } n   return 0; n } n int timer, tin[MAXN], fup[MAXN]; n void dfs(int v, const vector<vector<edge> >& g, vector<int>& bridges, n          vector<int>& used, int p = -1) { n   used[v] = true; n   tin[v] = fup[v] = timer++; n   for (auto& val : g[v]) { n     int to = val.to; n     if (to == p) continue; n     if (used[to]) n       fup[v] = min(fup[v], tin[to]); n     else { n       dfs(to, g, bridges, used, v); n       fup[v] = min(fup[v], fup[to]); n       if (fup[to] > tin[v] && conn[v][to] == 1) { n         int id = val.id; n         bridges[id] = 1; n       } n     } n   } n } n void find_bridges(const vector<vector<edge> >& g, vector<int>& bridges) { n   timer = 0; n   vector<int> used(MAXN, 0); n   for (int i = 1; i < g.size(); ++i) n     if (!used[i]) dfs(i, g, bridges, used); n } n pair<int, int> find_best(int s, int t, const vector<vector<edge> >& g) { n   vector<edge> path; n   vector<int> used(MAXN, 0); n   int res = find_path(s, t, g, path, used); n   if (!res) { n     return make_pair(0, -1); n   } n   vector<int> bridges(MAXE, 0); n   find_bridges(g, bridges); n   int min_price = INF; n   int best_id = -1; n   for (auto& val : path) { n     int id = val.id; n     if (bridges[id] && cost[id] < min_price) { n       best_id = id; n       min_price = cost[id]; n     } n   } n   return make_pair(min_price, best_id); n } n void remove(vector<vector<edge> >& g, int cur, edge val) { n   int to = val.to; n   auto it = find(g[cur].begin(), g[cur].end(), val); n   g[cur].erase(it); n   val.to = cur; n   it = find(g[to].begin(), g[to].end(), val); n   g[to].erase(it); n   conn[cur][to]--; n   conn[to][cur]--; n } n void restore(vector<vector<edge> >& g, int cur, edge val) { n   int to = val.to; n   g[cur].push_back(val); n   val.to = cur; n   g[to].push_back(val); n   conn[cur][to]++; n   conn[to][cur]++; n } n int main(int argc, char const* argv[]) { n   vector<vector<edge> > g(MAXN); n   int n, m; n   cin >> n >> m; n   int s, t; n   cin >> s >> t; n   for (int i = 0; i < m; i++) { n     int x, y, w; n     cin >> x >> y >> w; n     g[x].push_back(edge(y, w, i)); n     g[y].push_back(edge(x, w, i)); n     conn[x][y]++; n     conn[y][x]++; n     cost[i] = w; n   } n   vector<edge> path; n   vector<int> used(MAXN, 0); n   int res = find_path(s, t, g, path, used); n   if (!res) { n     cout << 0 <<   n ; n     cout << 0; n     return 0; n   } n   int best_price = INF; n   int cur = s; n   int id1, id2; n   for (auto val : path) { n     int id = val.id; n     int to = val.to; n     remove(g, cur, val); n     pair<int, int> best = find_best(s, t, g); n     if (best.first < INF) { n       int cur_price = val.w + best.first; n       if (cur_price < best_price) { n         id1 = val.id; n         id2 = best.second; n         best_price = cur_price; n       } n     } n     restore(g, cur, val); n     cur = to; n   } n   if (best_price >= INF) { n     cout << -1; n     return 0; n   } n   cout << best_price <<   n ; n   if (id2 == -1) { n     cout << 1 <<   n ; n     cout << id1 + 1; n   } else { n     cout << 2 <<   n ; n     cout << id1 + 1 <<     << id2 + 1 <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 4e5 + 1; n struct dak { n   long long val, pos; n }; n long long l[maxn], r[maxn]; n long long pre[maxn], pre2[maxn]; n vector<dak> p; n vector<pair<long long, long long> > v; n vector<pair<long long, long long> > qr; n long long n, m, q; n long long cur = 0; n long long res[maxn]; n inline long long get(long long r, long long x) { return pre[r] - pre[r - x]; } n inline long long cnt(long long x) { return pre2[x] - pre2[l[x] - 1]; } n void join(long long pos) { n   cur -= get(pos - 1, cnt(pos - 1)) + get(r[pos], cnt(r[pos])) - n          get(r[pos], cnt(r[pos]) + cnt(pos - 1)); n   r[l[pos - 1]] = r[pos]; n   l[r[pos]] = l[pos - 1]; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m >> q; n   for (long long i = 1; i <= n; i++) { n     long long x; n     cin >> x; n     v.push_back({x, 1}); n     cur += x; n   } n   for (long long i = 1; i <= m; i++) { n     long long x; n     cin >> x; n     v.push_back({x, 0}); n   } n   sort(v.begin(), v.end()); n   for (long long i = 1; i <= n + m; i++) { n     l[i] = r[i] = i; n     pre[i] = pre[i - 1] + v[i - 1].first; n     pre2[i] = pre2[i - 1] + v[i - 1].second; n   } n   for (long long i = 1; i < n + m; i++) { n     p.push_back({v[i].first - v[i - 1].first, i + 1}); n   } n   sort(p.begin(), p.end(), n        [](const dak &a, const dak &b) { return a.val < b.val; }); n   for (long long i = 1, x; i <= q; i++) { n     cin >> x; n     qr.push_back({x, i}); n   } n   sort(qr.begin(), qr.end()); n   long long pos = 0; n   for (pair<long long, long long> &query : qr) { n     while (pos < p.size() && p[pos].val <= query.first) { n       join(p[pos].pos); n       pos++; n     } n     res[query.second] = cur; n   } n   for (long long i = 1; i <= q; i++) cout << res[i] <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) n     if (n == i * (i + 1) / 2) { n       cout <<  YES ; n       return 0; n     } n   cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3e5 + 2; n int n, m, a[N], cnt[N], nxt[N]; n bool vis[N]; n int main() { n   int test; n   cin >> test; n   while (test--) { n     cin >> n >> m; n     for (int i = 0; i <= n; ++i) cnt[i] = 0; n     for (int i = 1; i <= n; ++i) { n       cin >> a[i]; n       int t1 = i + n - a[i], t2 = i - a[i] + 1; n       int k1 = -1, k2 = -1; n       if (t1 < n) { n         k1 = t1; n       } n       if (t2 > 0) { n         k2 = t2 - 1; n       } n       if (k1 >= 0) ++cnt[k1]; n       if (k2 >= 0) cnt[k2] += k1 != k2; n     } n     vector<int> ans; n     ans.clear(); n     for (int i = 0; i < n; ++i) { n       if (n - cnt[i] > m * 2) continue; n       for (int j = 1; j <= n; ++j) { n         vis[j] = false; n         if (a[j] <= n - i) { n           nxt[j] = i + a[j]; n         } else { n           nxt[j] = a[j] - (n - i); n         } n       } n       int cycles = 0; n       for (int j = 1; j <= n; ++j) n         if (nxt[j] != j && !vis[j]) { n           int u = j; n           ++cycles; n           vis[j] = true; n           while (nxt[u] != j) { n             u = nxt[u]; n             vis[u] = true; n           } n         } n       if (n - cnt[i] - cycles <= m) { n         ans.push_back(i); n       } n     } n     cout << (int)ans.size() <<    ; n     for (int i : ans) cout << i <<    ; n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long K = 10000000000LL; n vector<long long> L; n void init(long long crr) { n   if (crr > K) return; n   if (crr != 0) L.push_back(crr); n   init(crr * 10 + 4); n   init(crr * 10 + 7); n } n bool is_lucky(long long x) { n   if (x == 0) return false; n   while (x > 0) { n     if (x % 10 != 4 && x % 10 != 7) return false; n     x /= 10; n   } n   return true; n } n const int MAX_M = 20; n long long fact(long long k) { n   long long res = 1; n   for (int i = 1; i <= k; ++i) res *= i; n   return res; n } n vector<int> kth_permutation(int n, long long k) { n   vector<int> res(n), num(n); n   for (int i = 0; i < (int)(n); i++) num[i] = i; n   if (k >= fact(n)) return vector<int>(); n   for (int i = 0; i < (int)(n); i++) { n     long long f = fact(n - i - 1); n     res[i] = num[k / f]; n     num.erase(num.begin() + (k / f)); n     k %= f; n   } n   return res; n } n long long perm_n, perm_m; n vector<int> perm_last; n bool init_permutation(long long n, long long k) { n   perm_n = n; n   perm_last = kth_permutation(min(n, 20LL), k); n   if (perm_last.empty()) return false; n   perm_m = perm_n - perm_last.size(); n   return true; n } n long long get_permutation(long long i) { n   if (i < perm_m) n     return i; n   else n     return perm_m + perm_last[i - perm_m]; n } n int bruteforce(int N, int K) { n   vector<int> A(N); n   for (int i = 0; i < (int)(N); i++) A[i] = i + 1; n   for (int i = 0; i < (int)(K - 1); i++) n     next_permutation((A).begin(), (A).end()); n   int ans = 0; n   for (int i = 0; i < (int)(N); i++) n     if (is_lucky(i + 1) && is_lucky(A[i])) ++ans; n   return ans; n } n int main() { n   init(0); n   long long N, K; n   while (cin >> N >> K) { n     --K; n     if (!init_permutation(N, K)) { n       puts( -1 ); n       continue; n     } n     long long ans = 0; n     for (int i = 0; i < (int)(L.size()); i++) { n       long long l = L[i]; n       if (1 <= l && l <= N && is_lucky(1 + get_permutation(l - 1))) ++ans; n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n namespace my_template { n const long double PI = n     3.14159265358979323846264338327950288419716939937510582097494L; n template <typename T> n pair<T, T> operator+(const pair<T, T> &a, const pair<T, T> &b) { n   return pair<T, T>(a.first + b.first, a.second + b.second); n } n template <typename T> n pair<T, T> operator-(const pair<T, T> &a, const pair<T, T> &b) { n   return pair<T, T>(a.first - b.first, a.second - b.second); n } n template <typename T> n T operator*(const pair<T, T> &a, const pair<T, T> &b) { n   return (a.first * b.first + a.second * b.second); n } n template <typename T> n T operator^(const pair<T, T> &a, const pair<T, T> &b) { n   return (a.first * b.second - a.second * b.first); n } n template <typename T> n void print(vector<T> vec, string name =   ) { n   cout << name; n   for (auto u : vec) cout << u <<    ; n   cout <<   n ; n } n }  // namespace my_template n using namespace my_template; n const int MOD = 1000000007; n const long long INF = std::numeric_limits<long long>::max(); n const int MX = 500101; n int N; n vector<pair<int, int>> sk; n vector<int> color; n struct node { n   int l, r; n   int did; n   int lazy; n   int dp, seg; n   node *left = nullptr; n   node *right = nullptr; n   node(int a, int b) : l(a), r(b), did(0), lazy(0), dp(0), seg(0) { n     if (l != r) { n       left = new node(l, (l + r) / 2); n       right = new node((l + r) / 2 + 1, r); n     } n   } n   inline void push() { n     if (!lazy) return; n     did += lazy; n     if (l != r) { n       left->lazy += lazy; n       right->lazy += lazy; n     } else { n       seg += lazy; n       assert(did == dp + seg); n     } n     lazy = 0; n   } n   void upd(int pos, int val) { n     push(); n     if (l == r) { n       dp = max(dp, val); n       did = dp + seg; n       return; n     } else if (pos <= (l + r) / 2) n       left->upd(pos, val); n     else n       right->upd(pos, val); n     left->push(); n     right->push(); n     did = max(left->did, right->did); n   } n   void add(int a, int b, int val) { n     push(); n     if (r < a or b < l) n       return; n     else if (a <= l and r <= b) { n       lazy += val; n       push(); n       return; n     } else { n       left->add(a, b, val); n       right->add(a, b, val); n       left->push(); n       right->push(); n       did = max(left->did, right->did); n     } n   } n   int get(int a, int b) { n     push(); n     if (r < a or b < l) n       return 0; n     else if (a <= l and r <= b) { n       return did; n     } else { n       return max(left->get(a, b), right->get(a, b)); n     } n   } n }; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> N; n   { n     vector<pair<pair<int, int>, int>> temp(N); n     for (int i = 0; i < N; ++i) { n       cin >> temp[i].first.first >> temp[i].first.second >> temp[i].second; n     } n     sort(temp.begin(), temp.end(), n          [](const pair<pair<int, int>, int> &a, n             const pair<pair<int, int>, int> &b) { n            return a.first.second < b.first.second or n                   (a.first.second == b.first.second and n                    a.first.first > b.first.first); n          }); n     sk.resize(N); n     color.resize(N); n     for (int i = 0; i < N; ++i) { n       sk[i] = temp[i].first; n       color[i] = temp[i].second - 1; n     } n   } n   int big = 0; n   { n     map<int, int> rem; n     for (int i = 0; i < N; ++i) { n       rem[sk[i].first] = 1; n       rem[sk[i].second] = 1; n     } n     int piv = 1; n     for (auto &&u : rem) u.second = piv++; n     for (int i = 0; i < N; ++i) { n       sk[i].first = rem[sk[i].first]; n       sk[i].second = rem[sk[i].second]; n       big = max(big, sk[i].second); n     } n   } n   node *dp[2]; n   dp[0] = new node(0, big + 10); n   dp[1] = new node(0, big + 10); n   int ats = 0; n   for (int i = 0; i < N; ++i) { n     int c = color[i]; n     int first = sk[i].first; n     int second = sk[i].second; n     int curr = dp[1 - c]->get(0, first - 1) + 1; n     dp[1 - c]->add(0, first - 1, 1); n     dp[c]->upd(second, curr); n     ats = max(ats, curr); n   } n   printf( %d n , ats); n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long n, m; n   cin >> n >> m; n   vector<vector<long long> > a(m, vector<long long>(n)); n   for (long long i = 0; i <= n - 1; i++) { n     for (long long j = 0; j <= m - 1; j++) { n       cin >> a[j][i]; n       a[j][i]--; n     } n   } n   long long ans = 0; n   for (long long i = 0; i <= m - 1; i++) { n     vector<long long> temp(n, 0); n     for (long long j = 0; j <= n - 1; j++) { n       if (a[i][j] % m == i) { n         long long x = a[i][j] / m; n         if (x < n) { n           temp[(j - x + n) % n]++; n         } n       } n     } n     long long lol = INT_MAX; n     for (long long j = 0; j <= n - 1; j++) { n       lol = min(lol, (n - temp[j] + j)); n     } n     ans += lol; n   } n   cout << ans <<   n ; n   return; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long t = 1; n   for (long long i = 1; i <= t; i++) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long SIZE = 1e5; n long long n, a[SIZE], dp[SIZE][2][2]; n void Max(long long &a, long long b) { a = max(a, b); } n signed main() { n   cin >> n; n   for (long long i = 0; i < n; i++) { n     cin >> a[i]; n   } n   for (long long i = 0; i < n; i++) { n     for (long long j = 0; j < 2; j++) { n       for (long long k = 0; k < 2; k++) { n         dp[i][j][k] = 1; n       } n     } n   } n   for (long long i = 0; i < (n - 1); i++) { n     if (a[i] < a[i + 1]) { n       Max(dp[i + 1][0][0], dp[i][0][0] + 1); n       Max(dp[i + 1][1][0], dp[i][1][0] + 1); n     } n     Max(dp[i + 1][1][1], dp[i][0][0] + 1); n     if (i && (a[i + 1] - a[i - 1] > 1)) { n       Max(dp[i + 1][1][0], dp[i][1][1] + 1); n     } n   } n   long long ans = 1; n   for (long long i = 0; i < n; i++) { n     Max(ans, min(dp[i][0][0] + 1, n)); n     for (long long j = 0; j < 2; j++) { n       Max(ans, dp[i][1][j]); n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n typedef struct { n   int op, l, r, x, y; n } query; n int n, m, seed, vmax, a[100000]; n query qs[100000]; n int rnd() { n   int ret = seed; n   seed = (1LL * seed * 7LL + 13LL) % 1000000007LL; n   return ret; n } n long long get_num(map<int, long long>& mp, int i) { n   auto it = mp.lower_bound(i); n   if (it == mp.end() || it->first > i) it--; n   return it->second; n } n long long pow(long long a, int b, int mod) { n   if (b == 0) return 1LL; n   if (b == 1) return a % mod; n   long long c = pow(a, b / 2, mod) % mod; n   if (b & 1) return (((c * c) % mod) * (a % mod)) % mod; n   return (c * c) % mod; n } n void debug_range(map<int, long long>& mp) { n   for (auto i = mp.begin(); i != mp.end(); i++) n     printf( (%d %lld)  , i->first, i->second); n   printf(  n ); n } n int main() { n   scanf( %d%d%d%d , &n, &m, &seed, &vmax); n   for (int i = 0; i < n; i++) a[i] = (rnd() % vmax) + 1; n   for (int i = 0; i < m; i++) { n     qs[i].op = (rnd() % 4) + 1; n     qs[i].l = (rnd() % n) + 1 - 1; n     qs[i].r = (rnd() % n) + 1 - 1; n     if (qs[i].l > qs[i].r) swap(qs[i].l, qs[i].r); n     if (qs[i].op == 3) n       qs[i].x = (rnd() % (qs[i].r - qs[i].l + 1)) + 1; n     else n       qs[i].x = (rnd() % vmax) + 1; n     if (qs[i].op == 4) qs[i].y = (rnd() % vmax) + 1; n   } n   map<int, long long> mp; n   mp[0] = a[0]; n   for (int i = 1; i < n; i++) n     if (a[i] != a[i - 1]) mp[i] = a[i]; n   for (int i = 0; i < m; i++) { n     if (qs[i].op == 1) { n       long long tmp = get_num(mp, qs[i].r + 1); n       mp[qs[i].r + 1] = tmp; n       tmp = qs[i].x + get_num(mp, qs[i].l); n       mp[qs[i].l] = tmp; n       for (auto it = mp.lower_bound(qs[i].l + 1); n            it != mp.end() && it->first <= qs[i].r; it++) n         it->second += qs[i].x; n     } else if (qs[i].op == 2) { n       long long tmp = get_num(mp, qs[i].r + 1); n       mp[qs[i].r + 1] = tmp; n       vector<int> era; n       for (auto it = mp.lower_bound(qs[i].l); n            it != mp.end() && it->first <= qs[i].r; it++) n         era.push_back(it->first); n       for (int x : era) mp.erase(x); n       mp[qs[i].l] = qs[i].x; n     } else if (qs[i].op == 3) { n       map<long long, int> smp; n       int r = mp.lower_bound(qs[i].l) != mp.end() n                   ? min(qs[i].r + 1, mp.lower_bound(qs[i].l)->first) n                   : qs[i].r + 1; n       smp[get_num(mp, qs[i].l)] = r - qs[i].l; n       for (auto it = mp.lower_bound(qs[i].l); n            it != mp.end() && it->first <= qs[i].r; it++) { n         auto it2 = it; n         it2++; n         int r = it2 != mp.end() ? min(it2->first, qs[i].r + 1) : qs[i].r + 1; n         smp[it->second] += r - it->first; n       } n       for (auto it = smp.begin(); it != smp.end(); it++) { n         if (it->second >= qs[i].x) { n           printf( %lld n , it->first); n           break; n         } n         qs[i].x -= it->second; n       } n     } else { n       int r = mp.lower_bound(qs[i].l) != mp.end() n                   ? min(qs[i].r + 1, mp.lower_bound(qs[i].l)->first) n                   : qs[i].r + 1; n       long long sum = (pow(get_num(mp, qs[i].l), qs[i].x, qs[i].y) * n                        ((r - qs[i].l) % qs[i].y)) % n                       qs[i].y; n       for (auto it = mp.lower_bound(qs[i].l); n            it != mp.end() && it->first <= qs[i].r; it++) { n         auto it2 = it; n         it2++; n         int r = it2 != mp.end() ? min(it2->first, qs[i].r + 1) : qs[i].r + 1; n         sum = (sum + (pow(it->second, qs[i].x, qs[i].y) * (r - it->first)) % n                          qs[i].y) % n               qs[i].y; n       } n       printf( %lld n , sum % qs[i].y); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long leader[110000], p[110000]; n long long n, m, i, j, k; n bool can(long long cost) { n   for (i = 0, j = 0; i < n && j < m; ++i) { n     long long summ = cost; n     if (leader[i] == p[j] && j < m) ++j; n     if (j >= m) break; n     summ -= abs(leader[i] - p[j]); n     long long temp = leader[i] - p[j]; n     if (summ < 0) continue; n     if (temp > 0) { n       int t = j; n       while (p[j] <= leader[i] && j < m) ++j; n       while (cost >= p[j] - p[t] + min(p[j] - leader[i], leader[i] - p[t]) && n              j < m) n         ++j; n       if (j >= m) break; n     } else { n       while (p[j] <= leader[i] + cost && j < m) ++j; n       if (j >= m) break; n     } n   } n   if (j >= m) n     return 1; n   else n     return 0; n } n int main() { n   long long cost = 0; n   scanf( %I64d%I64d , &n, &m); n   for (i = 0; i < n; ++i) scanf( %I64d , &leader[i]); n   for (i = 0; i < m; ++i) scanf( %I64d , &p[i]); n   i = 0, j = 0; n   long long l = -1, r = max(p[m - 1] * 2, leader[n - 1] * 2); n   if (n != 1) n     while (r - l > 1) { n       long long mid = l + (r - l) / 2; n       if (can(mid)) n         r = mid; n       else n         l = mid; n     } n   if (n != 1) n     printf( %I64d , r); n   else { n     if (leader[0] < p[0]) n       printf( %I64d , p[m - 1] - leader[0]); n     else if (leader[0] > p[m - 1]) n       printf( %I64d , leader[0] - p[0]); n     else { n       printf( %I64d , n              p[m - 1] - p[0] + min(leader[0] - p[0], p[m - 1] - leader[0])); n     } n   } n   return 0; n } n


#include <iostream> n using namespace std; n int main() n { n     ios::sync_with_stdio(0); n     int t; cin >> t; n     while(t--) n     { n         int x,y; n         cin >> x >> y; n         cout << 2*max(x,y) - (min(x,y) != max(x,y)) <<   n ; n     } n     return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int mod = (int)1e9 + 7; n template <typename Arg1> n void __f(const char* name, Arg1&& arg1) { n   cout << name <<   :   << arg1 <<   n ; n } n template <typename Arg1, typename... Args> n void __f(const char* names, Arg1&& arg1, Args&&... args) { n   const char* comma = strchr(names + 1,  , ); n   cout.write(names, comma - names) <<   :   << arg1 <<   |  ; n   __f(comma + 1, args...); n } n void solve() { n   int n; n   cin >> n; n   if (n % 2 == 0) { n     cout <<  -1 n ; n     return; n   } n   for (int i = 0; i < n; i++) { n     cout << i <<    ; n   } n   cout <<   n ; n   for (int i = 0; i < n; i++) { n     cout << (i + 1) % n <<    ; n   } n   cout <<   n ; n   for (int i = 0; i < n; i++) { n     cout << (i + i + 1) % n <<    ; n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n void build(int ss, int se, int si); n long long query(int ss, int se, int si, int qs, int qe); n set<pair<long long, int> > s; n set<pair<long long, int> >::iterator it, last; n long long A[100001], st[400005], dp[100001]; n int main(void) { n   int i, n, c; n   long long sum, ans = 0, min_i; n   cin >> n >> c; n   if (c == 1) { n     cout << 0 << endl; n     return 0; n   } n   for (i = 0; i < n; i++) { n     cin >> A[i]; n     ans = ans + A[i]; n   } n   build(0, n - 1, 0); n   for (i = c - 1; i < n; i++) n     dp[i] = max(dp[i - 1], ((i - c >= 0) ? dp[i - c] : 0) + n                                query(0, n - 1, 0, i - c + 1, i)); n   cout << ans - dp[n - 1] << endl; n } n void build(int ss, int se, int si) { n   if (ss == se) { n     st[si] = A[ss]; n     return; n   } n   int mid = (ss + se) / 2; n   build(ss, mid, 2 * si + 1); n   build(mid + 1, se, 2 * si + 2); n   st[si] = min(st[2 * si + 1], st[2 * si + 2]); n } n long long query(int ss, int se, int si, int qs, int qe) { n   if (ss >= qs && se <= qe) return st[si]; n   if (ss > qe || se < qs) return INT_MAX; n   int mid = (ss + se) / 2; n   return min(query(ss, mid, 2 * si + 1, qs, qe), n              query(mid + 1, se, 2 * si + 2, qs, qe)); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int i, N, bitl, bitr; n   long long l, r; n   scanf( %d , &N); n   while (N--) { n     scanf( %lld%lld , &l, &r); n     bitl = bitr = 0; n     if (l == r) { n       printf( %lld n , l); n       continue; n     } n     while ((1LL << bitl) <= l) ++bitl; n     while ((1LL << bitr) <= r) ++bitr; n     --bitl; n     --bitr; n     if (bitl < bitr) { n       if ((1LL << (bitr + 1)) - 1 == r) n         printf( %lld n , r); n       else n         printf( %lld n , (1LL << bitr) - 1); n       continue; n     } n     for (i = bitr; i >= 0; --i) { n       if (((l >> i) % 2) != ((r >> i) % 2)) break; n     } n     if ((((1LL << (i + 1)) - 1) & r) == (1LL << (i + 1)) - 1) n       printf( %lld n , r); n     else { n       r |= (1LL << i) - 1; n       r &= ~(1LL << i); n       printf( %lld n , r); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void cmax(long long int &a, long long int b) { n   if (a < b) a = b; n } n const int mod = 1000000007; n bool comp(int x, int y) { return x > y; } n void printarr(int arr[], int n) { n   for (int i = 0; i < n; ++i) std::cout << arr[i] << (i < n - 1 ?     :   n ); n } n template <typename T> n void printv(T &a) { n   for (auto it = a.begin(); it != a.end(); ++it) std::cerr << *it <<    ; n   std::cerr <<   n ; n } n int gint() { n   int n; n   cin >> n; n   return n; n } n const int maxn = 200009; n long long int ans; n int arr[maxn]; n void solve() { n   ans = 0; n   bool ok = false; n   int m = gint(); n   int n = gint(); n   long long int sum = 0; n   for (int i = 0; i < n; ++i) { n     arr[i] = i + 1; n     sum += arr[i]; n     if (sum > m) ok = 1; n   } n   if (!ok) { n     for (int i = 0; i < n; ++i) { n       arr[i] += (m - sum) / n; n     } n     int res = (m - sum) % n; n     for (int i = n - 2; i >= 0; --i) { n       int temp; n       arr[i + 1] += temp = min(arr[i] * 2 - arr[i + 1], res); n       res -= temp; n     } n     sum = arr[0]; n     for (int i = 1; !ok && i < n; ++i) { n       ok = (arr[i] > arr[i - 1] * 2); n       sum += arr[i]; n     } n     ok = !(sum == m); n   } n   std::cout << (!ok ?  YES  :  NO ); n   std::cout <<   n ; n   if (!ok) printarr(arr, n); n   std::cout <<   n ; n } n void sol() {} n int main() { n   std::ios_base::sync_with_stdio(false); n   std::cin.tie(nullptr); n   int testcases = 1; n   for (int t = 0; t < testcases; ++t) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = 4e18; n const long long N = 2e5 + 10; n void testCase() { n   long long n; n   cin >> n; n   long long a[n + 1]; n   for (long long i = 1; i <= n; i++) cin >> a[i]; n   vector<long long> sma(n + 2), suf(n + 2); n   for (long long i = n; i >= 1; i--) { n     sma[i] = max(a[i], sma[i + 1] + 1); n     suf[i] = suf[i + 1] + sma[i] - a[i]; n   } n   long long ans = inf; n   long long pma = 0, pref = 0; n   for (long long i = 1; i <= n; i++) { n     pma = max(pma + 1, a[i]); n     long long cur = pref + suf[i + 1] + max(pma, sma[i]) - a[i]; n     ans = min(ans, cur); n     pref += pma - a[i]; n   } n   cout << ans; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr), cout.tie(nullptr); n   long long t_c = 1; n   while (t_c--) testCase(); n } n


#include <bits/stdc++.h> n using namespace std; n int const N = 200001; n int main() { n   int t; n   scanf( %d , &t); n   while (t--) { n     int n; n     string s, t; n     scanf( %d , &n); n     cin >> s >> t; n     set<int> msA[200], msB[200]; n     map<char, int> f; n     for (int i = 0; i < n; ++i) { n       msA[s[i]].insert(i); n       msB[t[i]].insert(i); n       ++f[s[i]], ++f[t[i]]; n     } n     bool ok = true; n     for (char i =  a ; i <=  z ; ++i) n       if (f[i] & 1) { n         ok = false; n         break; n       } n     vector<pair<int, int> > an; n     if (!ok) { n       puts( NO ); n       continue; n     } n     for (int i = 0; i < n; ++i) { n       if (s[i] == t[i]) continue; n       int sp = -1; n       for (int j = i + 1; j < n; ++j) { n         if (s[j] == s[i]) sp = j; n         if (s[j] != t[j] && s[j] == s[i]) { n           sp = j; n           break; n         } n       } n       if (~sp) { n         an.push_back({sp, i}); n         swap(s[sp], t[i]); n         continue; n       } n       for (int j = i + 1; j < n; ++j) { n         if (t[j] == s[i]) sp = j; n         if (s[j] != t[j] && t[j] == s[i]) { n           sp = j; n           break; n         } n       } n       if (~sp) { n         an.push_back({sp, sp}); n         swap(s[sp], t[sp]); n         an.push_back({sp, i}); n         swap(s[sp], t[i]); n         continue; n       } n       break; n     } n     if (s != t || an.size() > 2 * n) n       puts( NO ); n     else { n       printf( YES n%d n , an.size()); n       for (auto i : an) printf( %d %d n , i.first + 1, i.second + 1); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int n = 5000; n const long long mod = 998244353; n long long f[n + 10][n + 10]; n int main() { n   int a, b, c; n   for (int i = 1; i < n + 10; i++) { n     for (int j = 1; j < n + 10; j++) { n       if (i == 1) n         f[i][j] = j + 1; n       else if (j < i) n         f[i][j] = f[j][i]; n       else { n         (f[i][j] += j * f[i - 1][j - 1] % mod) %= mod; n         (f[i][j] += f[i - 1][j]) %= mod; n       } n     } n   } n   while (scanf( %d%d%d , &a, &b, &c) != EOF) { n     long long ans = ((f[a][b] * f[b][c]) % mod) * f[c][a] % mod; n     printf( %lld n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1000055; n const int inf = 0x3f3f3f3f; n const double pi = acos(-1.0); n const int mod = 1e9 + 7; n long long pow_mod(long long a, long long b) { n   long long ans = 1 % mod; n   for (; b; b >>= 1) { n     if (b & 1) ans = (long long)ans * a % mod; n     a = (long long)a * a % mod; n   } n   return ans; n } n long long x, n; n long long p[MAXN], c[MAXN]; n int cnt; n void divide(long long n) { n   cnt = 0; n   int lim = sqrt(n); n   for (int i = 2; i <= lim; ++i) { n     if (n % i == 0) { n       p[++cnt] = i, c[cnt] = 0; n       while (n % i == 0) n /= i, c[cnt]++; n     } n   } n   if (n > 1) p[++cnt] = n, c[cnt] = 1; n } n int main() { n   cin >> x >> n; n   divide(x); n   long long ans = 1; n   for (int i = 1; i <= cnt; ++i) { n     long long k = 1, tmp = 0; n     while ((long long)(n / (long long)pow(p[i], k))) { n       tmp += (long long)(n / (long long)pow(p[i], k)); n       k++; n     } n     ans = ans * pow_mod(p[i], tmp) % mod; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void chmin(T &x, const T &y) { n   if (x > y) x = y; n } n template <typename T> n void chmax(T &x, const T &y) { n   if (x < y) x = y; n } n int read() { n   char c; n   while ((c = getchar()) <  - ) n     ; n   if (c ==  - ) { n     int x = (c = getchar()) -  0 ; n     while ((c = getchar()) >=  0 ) x = x * 10 + c -  0 ; n     return -x; n   } n   int x = c -  0 ; n   while ((c = getchar()) >=  0 ) x = x * 10 + c -  0 ; n   return x; n } n const int N = 2e5 + 5, D = 998244353; n int64_t mi(int64_t x, int y = D - 2) { n   int64_t ans = 1; n   while (y) { n     if (y & 1) ans = ans * x % D; n     x = x * x % D; n     y >>= 1; n   } n   return ans; n } n void operator+=(pair<int64_t, int64_t> &a, const pair<int64_t, int64_t> &b) { n   (a.first += b.first) %= D; n   (a.second += b.second) %= D; n } n void mul(int64_t &a0, int64_t &a1, int64_t m[2][2]) { n   int64_t b0 = (m[0][0] * a0 + m[0][1] * a1) % D, n           b1 = (m[1][0] * a0 + m[1][1] * a1) % D; n   a0 = b0; n   a1 = b1; n } n int64_t m[2][2], m1[2][2]; n namespace SEG { n const int64_t T = N * 4; n int64_t tag[T][2][2], sum[T][2]; n int n; n void mul(int k, int64_t m[2][2]) { n   for (int i = 0; i <= 1; ++i) { n     ::mul(tag[k][0][i], tag[k][1][i], m); n   } n   ::mul(sum[k][0], sum[k][1], m); n } n void push_down(int k) { n   if ((tag[k][0][0] - 1) % D || tag[k][0][1] || tag[k][1][0] || n       (tag[k][1][1] - 1) % D) { n     mul((k * 2), tag[k]); n     mul(((k * 2) + 1), tag[k]); n     for (int i = 0; i <= 1; ++i) n       for (int j = 0; j <= 1; ++j) tag[k][i][j] = i == j; n   } n } n void push_up(int k) { n   for (int i = 0; i <= 1; ++i) n     sum[k][i] = (sum[(k * 2)][i] + sum[((k * 2) + 1)][i]) % D; n } n void add(int x, const pair<int64_t, int64_t> &a) { n   int k = 1, l = 1, r = n; n   while (1) { n     (sum[k][0] += a.first) %= D; n     (sum[k][1] += a.second) %= D; n     if (l == r) break; n     push_down(k); n     if (x <= ((l + r) / 2)) { n       k = (k * 2); n       r = ((l + r) / 2); n     } else { n       k = ((k * 2) + 1); n       l = ((l + r) / 2) + 1; n     } n   } n } n pair<int64_t, int64_t> query(int x) { n   int k = 1, l = 1, r = n; n   pair<int64_t, int64_t> ans = pair<int64_t, int64_t>(0, 0); n   while (l != r) { n     push_down(k); n     if (x <= ((l + r) / 2)) { n       ans += n           pair<int64_t, int64_t>(sum[((k * 2) + 1)][0], sum[((k * 2) + 1)][1]); n       k = (k * 2); n       r = ((l + r) / 2); n     } else { n       k = ((k * 2) + 1); n       l = ((l + r) / 2) + 1; n     } n   } n   ans += pair<int64_t, int64_t>(sum[k][0], sum[k][1]); n   return ans; n } n int ql, qr; n void modify(int k, int l, int r) { n   if (ql > r || qr < l) return; n   if (ql <= l && qr >= r) { n     mul(k, m); n     return; n   } n   push_down(k); n   modify((k * 2), l, ((l + r) / 2)); n   modify(((k * 2) + 1), ((l + r) / 2) + 1, r); n   push_up(k); n } n void modify(int l, int r) { n   ql = l; n   qr = r; n   modify(1, 1, n); n } n };  // namespace SEG n bool cmp(const pair<int, int> &a, const pair<int, int> &b) { n   return 1LL * a.first * b.second < 1LL * a.second * b.first; n } n pair<int, int> q[N]; n int t; n int main() { n   int n = read(); n   static int x[N], v[N], p[N]; n   for (int i = 1; i <= n; ++i) { n     x[i] = read(); n     v[i] = read(); n     p[i] = read() * mi(100) % D; n   } n   t = 1; n   q[1] = pair<int, int>(1, 0); n   for (int i = 2; i <= n; ++i) { n     q[++t] = pair<int, int>(x[i] - x[i - 1], v[i] + v[i - 1]); n     if (v[i] != v[i - 1]) n       q[++t] = pair<int, int>(x[i] - x[i - 1], abs(v[i] - v[i - 1])); n   } n   sort(q + 1, q + t + 1, cmp); n   SEG::n = t; n   SEG::add(t, pair<int64_t, int64_t>(1 - p[1], p[1])); n   for (int i = 2; i <= n; ++i) { n     int j0 = n         lower_bound(q + 1, q + t, n                     pair<int, int>(x[i] - x[i - 1], v[i] + v[i - 1]), cmp) - n         q; n     int j1 = lower_bound(q + 1, q + t, n                          pair<int, int>(x[i] - x[i - 1], abs(v[i] - v[i - 1])), n                          cmp) - n              q; n     pair<int64_t, int64_t> s0 = SEG::query(j0), s1 = SEG::query(j1); n     int64_t np = p[i]; n     for (int i = 0; i <= 1; ++i) n       for (int j = 0; j <= 1; ++j) m[i][j] = (i == 0) ? 1 - np : np; n     SEG::modify(1, j0 - 1); n     for (int i = 0; i <= 1; ++i) n       for (int j = 0; j <= 1; ++j) n         if (j == 1 && i == 0) { n           m1[i][j] = m[i][j]; n           m[i][j] = 0; n         } else n           m1[i][j] = 0; n     mul(s0.first, s0.second, m1); n     SEG::modify(j0, j1 - 1); n     SEG::add(j0, s0); n     int ni = v[i] > v[i - 1] ? 0 : 1; n     for (int i = 0; i <= 1; ++i) n       for (int j = 0; j <= 1; ++j) n         if (i == j && (i == ni)) { n           m1[i][j] = m[i][j]; n           m[i][j] = 0; n         } else n           m1[i][j] = 0; n     mul(s1.first, s1.second, m1); n     SEG::modify(j1, t); n     SEG::add(j1, s1); n   } n   auto S = [&](const pair<int64_t, int64_t> &a) -> int64_t { n     return (a.first + a.second) % D; n   }; n   int64_t ans = 0; n   for (int i = 1; i <= t - 1; ++i) n     (ans += (S(SEG::query(i)) - S(SEG::query(i + 1))) * q[i].first % D * n             mi(q[i].second)) %= D; n   cout << (ans + D) % D << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const double EPS = 1e-8; n const long long SZ = 1010, SSZ = 100010, APB = 26, one = 93, n                 INF = 0x3f3f3f3f3f3f3f3f, mod = 1000000007; n long long n, m, bg, fn, cnt; n long long to[SSZ], wt[SSZ], head[SZ]; n long long nex[SSZ], mk[SSZ]; n long long dfn[SZ], low[SZ], tot; n bool vst[SZ]; n void add(long long u, long long v, long long w) { n   ++cnt; n   nex[cnt] = head[u]; n   head[u] = cnt; n   to[cnt] = v, wt[cnt] = w; n } n bool dfs(long long x) { n   vst[x] = 1; n   if (x == fn) return 1; n   for (long long i = head[x]; i != -1; i = nex[i]) { n     long long t = to[i]; n     if (!vst[t]) { n       mk[i] = 1; n       if (dfs(t)) return 1; n       mk[i] = 0; n     } n   } n   return 0; n } n stack<long long> stk; n void tar(long long x, long long p, long long &res2, long long &eg2) { n   dfn[x] = low[x] = ++tot; n   vst[x] = 1; n   for (long long i = head[x]; i != -1; i = nex[i]) { n     if (i != (p ^ 1) && mk[i] != 2 && mk[i ^ 1] != 2) { n       long long t = to[i], w = wt[i]; n       if (!dfn[t]) { n         tar(t, i, res2, eg2); n         low[x] = min(low[x], low[t]); n       } else n         low[x] = min(low[x], low[t]); n       if (dfn[x] < low[t] && dfn[fn] >= dfn[t]) { n         if (w < res2) { n           res2 = w, eg2 = i; n         } n       } n     } n   } n } n void init() { n   cin >> n >> m >> bg >> fn; n   cnt = -1; n   memset(head, -1, sizeof(head)); n   for (long long i = 1; i <= m; ++i) { n     long long a, b, c; n     cin >> a >> b >> c; n     add(a, b, c); n     add(b, a, c); n   } n   dfs(bg); n   if (vst[fn] == 0) { n     cout << 0 << endl << 0; n     return; n   } n   long long res = INF, vt1 = -1, vt2; n   for (long long i = 0; i <= cnt; ++i) { n     if (mk[i]) { n       mk[i] = 2; n       memset(vst, 0, sizeof(vst)); n       tot = 0; n       memset(dfn, 0, sizeof(dfn)); n       memset(low, 0, sizeof(low)); n       long long res1 = wt[i], res2 = INF, eg1 = i, eg2; n       tar(bg, -1, res2, eg2); n       if (!vst[fn]) res2 = 0, eg2 = -5; n       if (res1 + res2 < res) { n         res = res1 + res2; n         vt1 = eg1 / 2 + 1, vt2 = eg2 / 2 + 1; n       } n       mk[i] = 1; n     } n   } n   if (vt1 == -1) { n     cout << -1 << endl; n     return; n   } n   cout << res << endl; n   cout << (vt2 == -1 ? 1 : 2) << endl; n   cout << vt1 <<    ; n   if (vt2 != -1) cout << vt2 << endl; n } n void work() {} n int main() { n   std::ios::sync_with_stdio(0); n   long long casenum; n   { n     init(); n     work(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   int x; n   cin >> x; n   vector<int> first(x); n   vector<int> second(x); n   int sum_f = 0; n   for (int i = 0; i < x; i++) { n     cin >> first[i]; n     sum_f += first[i]; n   } n   int sum_s = 0; n   for (int i = 0; i < x; i++) { n     cin >> second[i]; n     sum_s += second[i]; n   } n   if (sum_s > sum_f) { n     cout <<  No ; n   } else n     cout <<  Yes ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int tavan(int a, int n, int mod) { n   int res = 1; n   while (n) res *= (n & 1) ? a : 1, res %= mod, n /= 2, a *= a, a %= mod; n   return res; n } n const int MOD = ((int)81671); n const int N = ((int)40); n const int M = ((int)12); n const int X = ((int)2e6); n int n, m, p[M]; n string s[M], now; n bool mark[X]; n vector<string> v; n void solve(int pos, int x) { n   if (x < 0) return; n   if (pos == (int)s[0].size() && x != 0) return; n   if (x == 0) { n     for (int i = pos; i < s[0].size(); i++) n       now[i] = (s[0][i] ==  0 ) ?  1  :  0 ; n     v.push_back(now); n     return; n   } n   now[pos] = s[0][pos]; n   solve(pos + 1, x - 1); n   now[pos] = (s[0][pos] ==  0 ) ?  1  :  0 ; n   solve(pos + 1, x); n   now[pos] = s[0][pos]; n } n int main() { n   cin >> n >> m >> s[0] >> p[0]; n   now = s[0]; n   solve(0, p[0]); n   int ans = v.size(); n   for (int i = 1; i < m; i++) { n     cin >> s[i] >> p[i]; n     for (int j = 0; j < v.size(); j++) n       if (!mark[j]) { n         int num = 0; n         for (int k = 0; k < v[j].size(); k++) n           if (v[j][k] == s[i][k]) num++; n         if (num != p[i]) mark[j] = 1, ans--; n       } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n int main() { n   int T, t1 = 0, t2, maxn = 0, flag = 1, sum = 0, sum1 = 0; n   scanf( %d , &T); n   while (T--) { n     scanf( %d , &t2); n     if (flag && t2 == 0) { n       maxn = max(maxn, sum); n       sum1 = sum; n       sum = 0; n       flag = 0; n       continue; n     } n     if (t2 == 0) { n       maxn = max(maxn, sum); n       sum = 0; n       continue; n     } n     sum++; n     if (T == 0) { n       maxn = max(sum + sum1, maxn); n     } n   } n   printf( %d n , maxn); n } n


#include <bits/stdc++.h> n using namespace std; n int n, k, a[(int)2e5]; n long long sum; n double d, res; n int main() { n   cin >> n >> k; n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n     if (i < k) sum += a[i]; n   } n   d = n - k + 1; n   res += sum / d; n   for (int i = k; i < n; ++i) { n     sum -= a[i - k]; n     sum += a[i]; n     res += sum / d; n   } n   cout << fixed << setprecision(6) << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   int a = 0; n   for (int i = 0; i < s.size(); i++) { n     if (s[i] ==  a ) a++; n   } n   for (int i = s.size(); i >= a; i--) { n     if (a > (i / 2)) { n       cout << i << endl; n       exit(0); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int ABS(int a) { n   if (a < 0) return (-a); n   return a; n } n vector<int> v[50005]; n int main() { n   int n, r1, r2, i; n   scanf( %d , &n); n   scanf( %d , &r1); n   scanf( %d , &r2); n   for (i = (1); i < (n + 1); ++i) { n     if (r1 == i) continue; n     int x; n     scanf( %d , &x); n     v[x].push_back(i); n     v[i].push_back(x); n   } n   int parent[50005] = {0}; n   bool vis[50005] = {0}; n   queue<int> q; n   q.push(r2); n   parent[r2] = -1; n   vis[r2] = 1; n   while (!q.empty()) { n     int x = q.front(); n     q.pop(); n     for (i = (0); i < (v[x].size()); ++i) { n       int next = v[x][i]; n       if (!vis[next]) { n         q.push(next); n         parent[next] = x; n         vis[next] = 1; n       } n     } n   } n   for (i = (1); i < (n + 1); ++i) { n     if (i == r2) continue; n     printf( %d  , parent[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n function<void(void)> ____ = []() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n }; n const int MAXN = 555; n int n, m, match[MAXN]; n bool mp[MAXN][MAXN], vis[MAXN]; n vector<int> G[MAXN]; n bool dfs(int u, int c) { n   vis[u] = true; n   for (int v : G[u]) { n     if (v == c) continue; n     if (match[v] == -1 or (!vis[match[v]] and dfs(match[v], c))) { n       match[v] = u; n       return true; n     } n   } n   return false; n } n int hungary(int c) { n   memset(match, 255, sizeof(match)); n   int mt = 0; n   for (int i = 1; i <= n; i++) { n     if (i == c) continue; n     memset(vis, 0, sizeof(vis)); n     if (dfs(i, c)) mt++; n   } n   return mt; n } n int main() { n   ____(); n   cin >> n >> m; n   for (int i = 1; i <= m; i++) { n     int u, v; n     cin >> u >> v; n     G[u].push_back(v); n     mp[u][v] = true; n   } n   int ret = MAXN * MAXN * MAXN; n   for (int i = 1; i <= n; i++) { n     int centeredge = 0; n     for (int j = 1; j <= n; j++) { n       if (i == j and mp[i][j]) n         centeredge++; n       else if (i != j) { n         if (mp[i][j]) centeredge++; n         if (mp[j][i]) centeredge++; n       } n     } n     int mt = hungary(i); n     ret = min(ret, 3 * n + m - 2 - 2 * centeredge - 2 * mt); n   } n   cout << ret << endl; n   return 0; n } n


#include <bits/stdc++.h> n const int maxn = 1e6 + 10; n using namespace std; n int pre[maxn]; n int main() { n   string a, b; n   cin >> a; n   cin >> b; n   int LA = a.size(); n   int LB = b.size(); n   int flag = 0, ans = 0; n   for (int i = 0; i < LB; i++) { n     if (b[i] ==  1 ) flag++; n   } n   flag %= 2; n   if (a[0] ==  1 ) pre[1] = 1; n   for (int i = 1; i < LA; i++) { n     if (a[i] ==  1 ) n       pre[i + 1] = pre[i] + 1; n     else n       pre[i + 1] = pre[i]; n   } n   int T = LA - LB; n   for (int i = 0; i <= T; i++) { n     if ((pre[i + LB] - pre[i]) % 2 == flag) { n       ans++; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, a, b, sum = 0, sum1 = 0; n   cin >> n >> m >> a >> b; n   sum = n / m; n   sum1 = n - (sum * m); n   if (sum * b < n * a) { n     cout << min((sum * b + sum1 * a), ((sum + 1) * b)); n   } n   if (sum * b >= n * a) { n     cout << n * a; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5e5 + 5; n unordered_map<long long, long long> m1, mp; n long long m, k, n, s; n bool check() { n   for (auto it : mp) { n     if (it.second > m1[it.first]) return false; n   } n   return true; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t = 1; n   while (t--) { n     cin >> m >> k >> n >> s; n     long long a[m]; n     long long i, j; n     for (int i = 0; i <= m - 1; i++) { n       cin >> a[i]; n     } n     long long b[s]; n     for (int i = 0; i <= s - 1; i++) { n       cin >> b[i]; n       mp[b[i]]++; n     } n     long long del = m - n * k; n     long long req = del + k; n     for (i = 0; i < m; i += k) { n       m1.clear(); n       for (j = i; j < min(m, i + req); j++) { n         m1[a[j]]++; n       } n       if (check()) { n         cout << del <<   n ; n         for (j = i; j < min(i + req, m); j++) { n           if (del > 0 && m1[a[j]] > mp[a[j]]) { n             cout << j + 1 <<    ; n             del--; n             m1[a[j]]--; n           } n         } n         return 0; n       } n     } n     cout << -1 <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, s, a[100005], p, w[100005], ans; n multiset<long long> st; n int main() { n   cin >> n >> s; n   for (int k = 1; k <= n; k++) cin >> a[k]; n   cin >> p; n   for (int k = 1; k <= n; k++) cin >> w[k]; n   for (int k = 1; k <= n; k++) { n     st.insert(w[k]); n     while (s < a[k]) { n       if (st.size() == 0) { n         cout << -1; n         return 0; n       } n       ans += *st.begin(); n       st.erase(st.begin()); n       s += p; n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int limite = 1000000; n int n; n long long int a[limite]; n long long int suma[limite]; n map<long long int, int> cuantos; n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     a[i + n] = a[i]; n   } n   for (int i = 1; i <= 2 * n; i++) suma[i] = suma[i - 1] + a[i]; n   for (int i = 1; i <= n; i++) cuantos[suma[i]]++; n   int sol = n; n   for (int i = 1; i <= n; i++) { n     sol = min(sol, n - cuantos[suma[i - 1]]); n     cuantos[suma[i]]--; n     cuantos[suma[i + n]]++; n   } n   cout << sol << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main(int argc, char* argv[]) { n   int n, m, k; n   vector<double> r1, p1, p2; n   cin >> n; n   while (n--) { n     int hlp; n     cin >> hlp; n     r1.push_back(hlp); n   } n   cin >> m; n   while (m--) { n     int hlp; n     cin >> hlp; n     p1.push_back(hlp); n   } n   cin >> k; n   while (k--) { n     int hlp; n     cin >> hlp; n     p2.push_back(hlp); n   } n   double A, B; n   cin >> A >> B; n   double maxr2 = 0; n   sort(r1.begin(), r1.end()); n   sort(p1.begin(), p1.end()); n   sort(p2.begin(), p2.end()); n   { n     maxr2 = r1[r1.size() - 1] * sqrt(p1[p1.size() - 1] / p2[0] / n                                      (A / B + p1[p1.size() - 1] / p2[0])); n   } n   char buf[256]; n   cout << gcvt(maxr2, 50, buf); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1000000; n const int INF = 1000000001; n vector<int> G[MAXN + 1]; n bool vis[MAXN + 1]; n vector<int> CC; n int n, m, k; n int dfs(int v) { n   vis[v] = true; n   int ret = 1; n   for (__typeof((G[v]).begin()) u = (G[v]).begin(); u != (G[v]).end(); ++u) n     if (!vis[*u]) ret += dfs(*u); n   return ret; n } n int main(int argc, char *argv[]) { n   scanf( %d %d %d , &n, &m, &k); n   while (m--) { n     int a, b; n     scanf( %d %d , &a, &b); n     G[a].push_back(b); n     G[b].push_back(a); n   } n   int t = 0; n   int s = 0; n   for (int i = 1; i <= n; ++i) n     if (!vis[i]) n       CC.push_back(min(k, dfs(i))), t += CC.back() == 1 ? 0 : CC.back(), n           s += CC.back() != 1; n   t = t - 2 * s + 2; n   if (k == 1) n     printf( %d n , max(0, (int)CC.size() - 2)); n   else n     printf( %d n , (max(0, ((int)CC.size() - s) - t + 1)) / 2); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int a, b, x, y, n, ans, t; n   cin >> t; n   while (t--) { n     ans = LLONG_MAX; n     cin >> a >> b >> x >> y >> n; n     if (n >= (a - x + b - y)) n       cout << x * y <<   n ; n     else { n       if ((a - n) >= x) n         ans = (a - n) * b; n       else { n         if (b - (x - a + n) >= y) ans = min(ans, x * (b - (x - a + n))); n       } n       if ((b - n) >= y) n         ans = min(ans, (b - n) * a); n       else { n         if (a - (y - b + n) >= x) ans = min(ans, y * (a - (y - b + n))); n       } n       cout << ans <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n int main() { n   long long int a, b = 1, i; n   scanf( %I64d , &a); n   for (i = 1; i <= a; i++) { n     b += 12 * (1 + i - 2); n   } n   if (a == 1) n     printf( 1 ); n   else n     printf( %I64d , b); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n pair<long long int, long long int> rec(long long int n) { n   long long int mini = 1e18, maxi = -1; n   while (n) { n     mini = min(mini, n % 10); n     maxi = max(maxi, n % 10); n     n /= 10; n   } n   return {mini, maxi}; n } n int main() { n   long long int t; n   cin >> t; n   while (t--) { n     long long int n, k; n     cin >> n >> k; n     k--; n     while (k--) { n       pair<long long int, long long int> x = rec(n); n       if (x.first == 0) break; n       n += (x.first * x.second); n     } n     cout << n << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int const maxn = 1e6 + 5; n int a[maxn], b[maxn]; n pair<int, int> c[maxn]; n int all_elem[2 * maxn]; n int cur, inf = 1e9 + 7; n int f[2 * maxn]; n int psh[8 * maxn]; n pair<int, int> imin[4 * maxn]; n inline pair<int, int> unions(pair<int, int> c, pair<int, int> d) { n   if (c.first < d.first || (c.first == d.first && c.second > d.second)) n     return c; n   return d; n } n void build(int i, int l, int r) { n   psh[i] = 0; n   if (r - l == 1) n     imin[i] = {0, l}; n   else { n     int m = (r + l) / 2; n     build(2 * i + 1, l, m); n     build(2 * i + 2, m, r); n     imin[i] = unions(imin[2 * i + 1], imin[2 * i + 2]); n   } n } n inline void push(int i) { n   imin[i].first += psh[i]; n   psh[2 * i + 1] += psh[i]; n   psh[2 * i + 2] += psh[i]; n   psh[i] = 0; n } n void adds(int i, int l, int r, int lq, int rq, int x) { n   push(i); n   if (lq >= r || l >= rq) return; n   if (lq <= l && r <= rq) { n     psh[i] += x; n     push(i); n     return; n   } n   int m = (r + l) / 2; n   adds(2 * i + 1, l, m, lq, rq, x); n   adds(2 * i + 2, m, r, lq, rq, x); n   imin[i] = unions(imin[2 * i + 1], imin[2 * i + 2]); n } n pair<int, int> get(int i, int l, int r, int lq, int rq) { n   push(i); n   if (lq >= r || l >= rq) return {inf, inf}; n   if (lq <= l && r <= rq) return imin[i]; n   int m = (r + l) / 2; n   return unions(get(2 * i + 1, l, m, lq, rq), get(2 * i + 2, m, r, lq, rq)); n } n inline void add(int i) { n   while (i <= cur) { n     f[i]++; n     i = (i | (i + 1)); n   } n } n inline int get(int i) { n   int ans = 0; n   while (i > 0) { n     ans += f[i]; n     i = (i & (i + 1)) - 1; n   } n   return ans; n } 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, m; n     cur = 0; n     cin >> n >> m; n     build(0, 0, n + 1); n     for (int i = 1; i <= n; ++i) cin >> a[i]; n     for (int i = 1; i <= m; ++i) cin >> b[i]; n     sort(b + 1, b + m + 1); n     for (int i = 1; i <= n; ++i) { n       all_elem[++cur] = a[i]; n     } n     for (int i = 1; i <= m; ++i) { n       all_elem[++cur] = b[i]; n     } n     sort(all_elem + 1, all_elem + cur + 1); n     for (int i = 1; i <= n; ++i) { n       a[i] = lower_bound(all_elem + 1, all_elem + cur + 1, a[i]) - all_elem; n     } n     for (int i = 1; i <= m; ++i) { n       b[i] = lower_bound(all_elem + 1, all_elem + cur + 1, b[i]) - all_elem; n     } n     long long ans = 0; n     for (int i = 0; i <= cur; ++i) f[i] = 0; n     for (int i = n; i >= 1; --i) { n       ans += get(a[i]); n       add(a[i] + 1); n     } n     for (int i = 1; i <= n; ++i) c[i] = {a[i], i}; n     sort(c + 1, c + n + 1); n     int pos = n; n     for (int i = 1; i <= n; ++i) { n       adds(0, 0, n + 1, 0, i, 1); n     } n     int lptr = n, rptr = n; n     for (int i = m; i >= 1; --i) { n       if (b[i] != b[i + 1]) { n         while (rptr > lptr) { n           adds(0, 0, n + 1, c[rptr].second, n + 1, 1); n           rptr--; n         } n       } n       while (rptr > 0 && b[i] < c[rptr].first) { n         adds(0, 0, n + 1, 0, c[rptr].second, -1); n         adds(0, 0, n + 1, c[rptr].second, n + 1, 1); n         rptr--; n       } n       lptr = min(lptr, rptr); n       while (lptr > 0 && b[i] == c[lptr].first) { n         adds(0, 0, n + 1, 0, c[lptr].second, -1); n         lptr--; n       } n       pair<int, int> best = get(0, 0, n + 1, 0, pos + 1); n       ans += best.first; n       pos = best.second; n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n int n, m, same; n string s; n int main() { n   cin >> n >> m >> s; n   long long ans = 1LL * n * (m - 1); n   for (int i = 1; i < n; i++) { n     if (s[i] == s[i - 2]) { n       same++; n     } else { n       same = 0; n     } n     if (s[i] != s[i - 1]) { n       ans += 1LL * n * (m - 1) - same - 1; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int i, m, j; n   char a[1000], b[1000]; n   scanf( %s %s , &a, &b); n   if (strlen(a) != strlen(b)) { n     printf( NO ); n     return 0; n   } n   j = strlen(b) - 1; n   for (i = 0; i < strlen(a); i++) { n     if (a[i] != b[j]) { n       printf( NO ); n       return 0; n     } n     j--; n   } n   printf( YES ); n } n