#include <bits/stdc++.h> n using namespace std; n using llong = long long; n inline char Flip(char x) { return (x ==  0 ) ?  1  :  0 ; } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   vector<int> ans; n   while (t--) { n     int n; n     cin >> n; n     n *= 2; n     vector<int> a(n); n     for (int& i : a) cin >> i; n     vector<int> b; n     int i = n - 1; n     while (i >= 0) { n       int j = max_element(a.begin(), a.begin() + i + 1) - a.begin(); n       b.push_back(i - j + 1); n       i = j - 1; n     } n     bitset<4001> dp; n     dp[0] = 1; n     for (int len : b) dp = dp | (dp << len); n     if (dp[n / 2]) n       cout <<  YES n ; n     else n       cout <<  NO n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, u, v, flag, num, vis[100100]; n vector<int> edge[100100]; n queue<int> sk[100100]; n struct G { n   int a, b, c; n } ans[100100]; n void dfs(int u, int father) { n   vis[u] = true; n   for (int i = 0; i < edge[u].size(); ++i) { n     int v = edge[u][i]; n     if (vis[v] == false) n       dfs(v, u); n     else if (v != father && vis[v] == true) n       sk[v].push(u); n   } n   while (sk[u].size() >= 2) { n     int x = sk[u].front(); n     sk[u].pop(); n     int y = sk[u].front(); n     sk[u].pop(); n     ans[++num] = {x, u, y}; n   } n   if (sk[u].size() == 1) { n     if (father == 0) flag = 1; n     int x = sk[u].front(); n     sk[u].pop(); n     ans[++num] = {x, u, father}; n   } else n     sk[father].push(u); n } n int main() { n   cin >> n >> m; n   if (m % 2 == 1) { n     cout <<  No solution  << endl; n     return 0; n   } n   for (int i = 1; i <= m; ++i) { n     cin >> u >> v; n     edge[u].push_back(v); n     edge[v].push_back(u); n   } n   dfs(1, 0); n   if (flag == 1) n     cout <<  No solution  << endl; n   else { n     for (int i = 1; i <= num; ++i) { n       cout << ans[i].a <<     << ans[i].b <<     << ans[i].c << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n, k, i, start; n   bool b; n   while (scanf( %d%d , &n, &k) != EOF) { n     if (n < k || k == 1 && n > 1) { n       printf( -1 n ); n       continue; n     } n     start = k; n     for (i = b = 0; i < n; i++) { n       if (b && n - i == k - 2) { n         start = 2; n         break; n       } n       if (!b && n - i == k - 1) { n         start = 1; n         break; n       } n       printf(i& 1 ? b = 1,  b  :  a ); n     } n     for (i = start; i < k; i++) printf( %c , i +  a ); n     printf(  n ); n   } n   return 0; n } n


#include<bits/stdc++.h> n #define pb push_back n using namespace std; n typedef long long ll; n const int maxn=1e6+100; n const int M=1000000007; n typedef array<int,41> A; n struct hasher { n     size_t operator()(const A& p) const { n         size_t result = 0; n         for (int i=1;i<=40;i++) result = result * 19260817 + p[i]; n         return result; n     } n }; n bool vis[50]; n int f[50],n,mx,F[50][50],G[50][50],p[50],nf[50],inv[50]; n int ways[50][50][50]; n unordered_map<A,int,hasher> dp[41][41][41]; n void add(int &x,int y){x+=y;if (x>=M)x-=M;} n int C(int x,int y){ n  if (y<0||y>x) return 0; n  return 1ll*f[x]*nf[y]%M*nf[x-y]%M; n } n void init(){ n  inv[1]=1; for (int i=2;i<=n;i++) inv[i]=M-1ll*(M/i)*inv[M%i]%M; n  f[0]=nf[0]=1; for (int i=1;i<=n;i++) f[i]=1ll*f[i-1]*i%M,nf[i]=1ll*nf[i-1]*inv[i]%M; n  F[0][0]=f[n-1]; n  for (int i=0;i<=n-1;i++) n  for (int j=1;j+i<=n-1;j++){ n         F[i][j]=0; n         for (int x=0;x<=i;x++) n    for (int y=0;y<=j;y++){ n     add(F[i][j],1ll*C(i,x)*C(j,y)%M*f[n-1-x-y]%M*f[x+y]%M); n     if (!y) add(G[i][j],1ll*C(i,x)*C(j,y)%M*f[n-1-x-y]%M*f[x+y]%M); n    } n  } n  for (int i=0;i<=n-1;i++) n  for (int j=0;j+i<=n-1;j++){ n   int x=1,y=1; n   for (int k=1;k<=n;k++){ n    x=1ll*x*F[i][j]%M; n    y=1ll*y*G[i][j]%M; n    ways[i][j][k]=(x-y+M)%M; n   } n  } n } n int solve(int lasttot,int tot,int block,A a){ n     if (dp[lasttot][tot][block].find(a)!=dp[lasttot][tot][block].end()){ n   return dp[lasttot][tot][block][a]; n     } n     int ret=0,sum=n; n     for (int i=1;i<=mx;i++) sum-=a[i]*i; n     if (sum==n) return dp[lasttot][tot][block][a]=nf[block]; n  A tmp=a; n     for (int i=1;i<=mx;i++) { n   if (a[i]){ n    tmp[i]--; n             add(ret,1ll*ways[sum-tot-lasttot][lasttot][i]*a[i]%M*solve(lasttot,tot+i,block+1,tmp)%M); n             if (block) add(ret,1ll*ways[sum-tot][tot][i]*a[i]%M*nf[block]%M*solve(tot,i,1,tmp)%M); n    tmp[i]++; n   } n     } n     return dp[lasttot][tot][block][a]=ret; n } n int main(){ n  scanf( %d ,&n); n  init(); n  for (int i=1;i<=n;i++) scanf( %d ,&p[i]); n  A a; n  for (int i=0;i<=40;i++) a[i]=0; n  for (int i=1;i<=n;i++) if (!vis[i]){ n   int w=0,tmp=i; n   while (!vis[tmp]){ n    w++; n    vis[tmp]=1; n    tmp=p[tmp]; n   } n   a[w]++; n   mx=max(mx,w); n  } n  printf( %d n ,solve(0,0,0,a)); n  return 0; n } n  n


#include <bits/stdc++.h> n using namespace std; n const int NN = 1000100; n int link[NN], fail[NN], len[NN], dif[NN], nxt[NN][26]; n int ed, sz, cur; n char s[NN], S[NN], T[NN]; n int tp[NN], dp[NN], pre[NN], pr[NN]; n void clear(int u, int L = 0) { len[u] = L; } n void init() { n   sz = 2; n   ed = 1; n   cur = 0; n   s[cur] = -1; n   clear(0); n   clear(1, -1); n   fail[0] = 1; n } n inline int get_fail(int u) { n   while (s[cur - len[u] - 1] != s[cur]) u = fail[u]; n   return u; n } n int insert(char c) { n   s[++cur] = c -=  a ; n   ed = get_fail(ed); n   if (!nxt[ed][c]) { n     int p1 = sz++; n     clear(p1, len[ed] + 2); n     fail[p1] = nxt[get_fail(fail[ed])][c]; n     nxt[ed][c] = p1; n     dif[p1] = len[p1] - len[fail[p1]]; n     if (dif[p1] == dif[fail[p1]]) n       link[p1] = link[fail[p1]]; n     else n       link[p1] = fail[p1]; n   } n   ed = nxt[ed][c]; n   return ed; n } n int main() { n   scanf( %s%s , S, T); n   int n = strlen(S); n   init(); n   memset(tp, 0x3f, sizeof tp); n   for (int i = 1, now; i <= n * 2; i++) { n     if (i & 1) n       now = insert(S[i - 1 >> 1]); n     else n       now = insert(T[i - 1 >> 1]); n     dp[i] = 0x3f3f3f3f; n     if ((~i & 1) && S[i - 1 >> 1] == T[i - 1 >> 1]) { n       dp[i] = dp[i - 2]; n       pre[i] = i - 2; n     } n     for (int u = now; u; u = link[u]) { n       pr[u] = i - len[link[u]] - dif[u]; n       tp[u] = dp[pr[u]]; n       if (link[u] ^ fail[u]) { n         if (tp[u] > tp[fail[u]]) { n           tp[u] = tp[fail[u]]; n           pr[u] = pr[fail[u]]; n         } n       } n       if (~i & 1) { n         if (dp[i] > tp[u] + 1) { n           dp[i] = tp[u] + 1; n           pre[i] = pr[u]; n         } n       } n     } n   } n   if (dp[n * 2] == 0x3f3f3f3f) return puts( -1 ); n   vector<pair<int, int>> ans; n   int now = n * 2; n   while (now) { n     int pre = ::pre[now]; n     if (now - pre > 2) ans.emplace_back((pre >> 1) + 1, now >> 1); n     now = pre; n   } n   cout << ans.size() << endl; n   for (auto &a : ans) printf( %d %d n , a.first, a.second); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Fenwick { n   vector<long long> BIT; n   long long N; n   Fenwick(long long n) { n     N = n + 2; n     BIT.resize(N + 10); n   } n   void reset() { n     for (long long i = 0; i <= N; i++) { n       BIT[i] = 0; n     } n   } n   void add(long long r, long long value) { n     for (long long i = r; i <= N; i = i | (i + 1)) { n       BIT[i] += value; n     } n   } n   long long qry(long long r) { n     long long ans = 0; n     for (long long i = r; i >= 0; i = (i & (i + 1)) - 1) { n       ans += BIT[i]; n     } n     return ans; n   } n }; n const long long N = (long long)1e6; n void solve() { n   long long n, m; n   cin >> n >> m; n   vector<array<long long, 3>> x(n), y(m); n   for (auto &i : x) { n     cin >> i[0] >> i[1] >> i[2]; n   } n   for (auto &i : y) { n     cin >> i[0] >> i[1] >> i[2]; n   } n   m += 1; n   y.push_back({N, 0, N}); n   long long ans = 0; n   Fenwick sg(N + 10); n   sg.add(N, 1); n   sg.add(0 * 1LL, 1); n   sort(y.begin(), y.end()); n   vector<vector<long long>> add(N + 10), remove(N + 10); n   for (auto &i : x) { n     if (i[1] == 0) { n       add[0].push_back(i[0]); n       remove[i[2]].push_back(i[0]); n     } else { n       add[i[1]].push_back(i[0]); n       remove[N - 1].push_back(i[0]); n     } n   } n   long long cur = 0; n   42; n   for (long long i = 0; i <= N; i++) { n     for (auto &j : add[i]) { n       sg.add(j, 1); n     } n     while (cur < m and y[cur][0] == i) { n       long long u = max(y[cur][1], y[cur][2]); n       long long v = min(y[cur][1], y[cur][2]); n       if (v == 0) { n         ans += (sg.qry(u) - 1); n       } else { n         ans += (sg.qry(u) - sg.qry(v - 1) - 1); n       } n       cur += 1; n     } n     for (auto &j : remove[i]) { n       sg.add(j, -1); n     } n   } n   cout << ans <<   n ; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n const long long LINF = 1e18; n const long long INF = 1e9; n const long long MOD = 1000000007; n const long long MAX = 100005; n #pragma GCC optimize( O3 ) n #pragma GCC optimize( Ofast ) n #pragma GCC optimize( unroll-loops ) n #pragma GCC optimize( -ffloat-store ) n #pragma GCC optimize( -fno-defer-pop ) n long long strtoll(string second) { n   stringstream str(second); n   long long number = 0; n   str >> number; n   return number; n } n string lltostr(long long n) { n   stringstream ss; n   ss << n; n   return ss.str(); n } n bool prime[MAX]; n void sieve() { n   memset(prime, true, sizeof(prime)); n   prime[0] = prime[1] = false; n   for (long long p = 2; p * p <= MAX; p++) { n     if (prime[p] == true) { n       for (long long i = p * 2; i <= MAX; i += p) prime[i] = false; n     } n   } n } n long long power(long long a, long long b, long long m) { n   if (b == 0) return 1; n   if (b == 1) return a % m; n   long long t = power(a, b / 2, m); n   t = (t * t) % m; n   if (b & 1) t = (t * a) % m; n   return t; n } n long long modInverse(long long a, long long m) { return power(a, m - 2, m); } n long long __gcd(long long a, long long b) { n   if (b == 0) return a; n   return __gcd(b, a % b); n } n bool cmp(const pair<long long, long long> &l, n          const pair<long long, long long> &r) { n   return l.first < r.first; n } n void solve() { n   long long n, m; n   cin >> n >> m; n   long long a[n + 1], c[n + 1]; n   for (long long i = 0; i < n; i++) cin >> a[i + 1]; n   for (long long i = 0; i < n; i++) cin >> c[i + 1]; n   map<long long, long long> mm; n   set<pair<long long, long long> > second; n   for (long long i = 1; i <= n; i++) second.insert({c[i], i}); n   for (long long i = 1; i <= n; i++) mm[i] = a[i]; n   while (m--) { n     long long ans = 0; n     long long type, q; n     cin >> type >> q; n     if (mm[type] >= q) { n       ans += c[type] * q; n       mm[type] -= q; n     } else { n       ans += mm[type] * c[type]; n       q -= mm[type]; n       mm[type] = 0; n       while (q && !second.empty()) { n         pair<long long, long long> p = *second.begin(); n         long long x = p.first; n         long long y = p.second; n         if (mm[y] >= q) { n           mm[y] -= q; n           ans += (c[y] * q); n           q = 0; n         } else { n           ans += mm[y] * c[y]; n           q -= mm[y]; n           mm[y] = 0; n           second.erase(second.begin()); n         } n       } n       if (q != 0) ans = 0; n     } n     cout << ans <<   n ; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   ; n   long long t; n   t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   map<int, vector<int> > map1; n   for (int i = 1; i <= n; i++) { n     int a; n     cin >> a; n     map1[i].push_back(a); n     map1[a].push_back(i); n   } n   int counter = 0; n   map<int, bool> check; n   for (int i = 1; i <= n; i++) { n     if (!check[i]) { n       counter++; n       queue<int> que; n       que.push(i); n       while (que.size()) { n         int front = que.front(); n         que.pop(); n         if (!check[front]) n           for (int j = 0; j < map1[front].size(); j++) { n             que.push(map1[front][j]); n           } n         check[front] = true; n       } n     } n   } n   cout << counter; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 2e5 + 10; n const long long inf = 1e18; n const long long mod = 1e9 + 7; n long long n, q; n long long a[maxn]; n long long cost(long long g) { n   long long ans = 0; n   for (long long i = 1; i <= n; i++) { n     if (a[i] < g) n       ans += g - a[i]; n     else n       ans += min(a[i] % g, g - a[i] % g); n   } n   return ans; n } n long long ans = inf; n void DO(long long x) { n   if (x <= 1) return; n   for (long long i = 2; i <= sqrt(x); i++) n     if (x % i == 0) { n       ans = min(ans, cost(i)); n       while (x % i == 0) x /= i; n     } n   if (x > 1) ans = min(ans, cost(x)); n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   for (long long i = 1; i <= n; i++) cin >> a[i]; n   random_shuffle(a + 1, a + n + 1); n   for (long long i = 1; i <= 10; i++) DO(a[i] - 1), DO(a[i]), DO(a[i] + 1); n   for (long long i = n / 3; i <= n / 3 + 15; i++) n     DO(a[i] - 1), DO(a[i]), DO(a[i] + 1); n   for (long long i = 2 * n / 3; i <= 2 * n / 3 + 15; i++) n     DO(a[i] - 1), DO(a[i]), DO(a[i] + 1); n   for (long long i = n; i >= n - 10; i--) DO(a[i] - 1), DO(a[i]), DO(a[i] + 1); n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n long long h, n; n int main() { n   cin >> h >> n; n   long long ans = 0; n   long long l = 1, r = (1LL << h); n   int dir = 0; n   for (int i = h - 1; i >= 0; i--) { n     ans++; n     long long mid = (l + r) / 2; n     if (dir == 0) { n       if (mid < n) { n         ans += (1LL << (i + 1)) - 1; n         dir = 0; n         l = mid + 1; n       } else { n         r = mid; n         dir = 1; n       } n     } else { n       if (n <= mid) { n         ans += (1LL << (i + 1)) - 1; n         dir = 1; n         r = mid; n       } else { n         l = mid + 1; n         dir = 0; n       } n     } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int two = 0, one = 0; n   int x[n + 1000]; n   for (int i = 0; i < n; ++i) cin >> x[i]; n   for (int i = 0; i < n; ++i) { n     if (x[i] == 2) n       ++two; n     else n       ++one; n   } n   if (two == 0) { n     for (int i = 0; i < one; ++i) cout <<  1  ; n   } else if (one == 0) { n     for (int i = 0; i < two; ++i) cout <<  2  ; n   } else { n     --two; n     cout <<  2  ; n     --one; n     cout <<  1  ; n     for (int i = 0; i < two; ++i) cout <<  2  ; n     for (int i = 0; i < one; ++i) cout <<  1  ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n T sqr(T x) { n   return x * x; n } n const int maxn = 3 * (int)1e5 + 5; n int n; n int a[maxn]; n int p[maxn]; n int main() { n   srand(time(NULL)); n   ios_base::sync_with_stdio(0); n   cin >> n; n   for (int i = 1; i <= n; ++i) { n     cin >> a[i]; n     p[a[i]] = i; n   } n   int q; n   cin >> q; n   while (q--) { n     int tst, x, y; n     cin >> tst >> x >> y; n     if (tst == 2) { n       swap(a[x], a[y]); n       p[a[x]] = x; n       p[a[y]] = y; n     } else { n       int ans = 1; n       int last = -1; n       for (int i = x; i <= y; ++i) { n         if (p[i] < last) ++ans; n         last = p[i]; n       } n       cout << ans << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   string colors =  ROYGBIV ; n   string remainder =  GBIVGB ; n   string answer =   ; n   for (int i = 0; i < n / 7; ++i) { n     answer += colors; n   } n   for (int i = 0; i < n % 7; ++i) { n     answer += remainder[i]; n   } n   cout << answer; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7, N = 1e5 + 5; n int n, m, sta[N], tp, tot, siz[N], low[N], dfn[N], be[N], bl, dep[N], f[N][22], n     w[N]; n vector<int> e[N], vec[N]; n int read() { n   int x = 0, f = 1; n   char s; n   while ((s = getchar()) >  9  || s <  0 ) n     if (s ==  - ) f = -1; n   while (s >=  0  && s <=  9 ) n     x = (x << 1) + (x << 3) + (s ^ 48), s = getchar(); n   return x * f; n } n void addEdge(int u, int v) { n   e[u].push_back(v); n   e[v].push_back(u); n } n void tarjan(int u, int fa) { n   sta[++tp] = u; n   dfn[u] = low[u] = ++tot; n   for (int i = 0; i < e[u].size(); ++i) { n     int v = e[u][i]; n     if (v == fa) continue; n     if (!dfn[v]) n       tarjan(v, u), low[u] = min(low[u], low[v]); n     else n       low[u] = min(low[u], dfn[v]); n   } n   if (low[u] == dfn[u]) { n     int v = 0; n     ++bl; n     do { n       v = sta[tp--]; n       be[v] = bl; n       ++siz[bl]; n     } while (v ^ u); n   } n } n int LCA(int u, int v) { n   if (dep[u] < dep[v]) swap(u, v); n   for (int i = 20; i >= 0; --i) n     if (dep[f[u][i]] >= dep[v]) u = f[u][i]; n   if (u == v) return u; n   for (int i = 20; i >= 0; --i) n     if (f[u][i] ^ f[v][i]) u = f[u][i], v = f[v][i]; n   return f[u][0]; n } n void dfs(int u, int fa) { n   dep[u] = dep[fa] + 1; n   f[u][0] = fa; n   w[u] = w[fa] + (siz[u] > 1); n   for (int i = 1; i <= 20; ++i) f[u][i] = f[f[u][i - 1]][i - 1]; n   for (int i = 0; i < vec[u].size(); ++i) n     if (vec[u][i] ^ fa) dfs(vec[u][i], u); n } n int qkpow(int x, int y) { n   int r = 1; n   while (y) { n     if (y & 1) r = 1ll * r * x % mod; n     x = 1ll * x * x % mod; n     y >>= 1; n   } n   return r; n } n int main() { n   n = read(), m = read(); n   for (int i = 1; i <= m; ++i) addEdge(read(), read()); n   tarjan(1, 0); n   for (int i = 1; i <= n; ++i) n     for (int j = 0; j < e[i].size(); ++j) n       if (be[i] ^ be[e[i][j]]) vec[be[i]].push_back(be[e[i][j]]); n   dfs(1, 0); n   int u, v; n   for (int t = read(); t; --t) { n     u = be[read()], v = be[read()]; n     int lca = LCA(u, v); n     printf( %d n , qkpow(2, w[u] + w[v] - w[lca] * 2 + (siz[lca] > 1))); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = 1e9 + 7; n const long long INF = 1e18 + 5; n const long long maxn = 5e5 + 5; n long long n = 100000000000, a[1555555], d[555555], mx = -1000000001; n long long u[1511111], b[1100005], sum, cnt, t[555555]; n long long bi(long long x, long long y) { n   if (y == 0) return 1ll; n   if (y == 1) return x; n   long long z = bi(x, y / 2); n   z = z * z % n; n   if (y & 1) z = z * x % n; n   return z % n; n } n pair<int, int> p[555555]; n vector<pair<int, int> > v, x; n int c[555555], l[555555], y[555555], k[555555]; n long long dp[555555][3][3]; n int main() { n   cin.tie(0); n   cout.tie(0); n   ios_base::sync_with_stdio(NULL); n   int n, x; n   cin >> n >> x; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   for (int i = 1; i <= n; i++) n     for (int j = 0; j <= 2; j++) n       for (int k = 0; k <= 2; k++) dp[i][j][k] = -1e15; n   dp[0][0][0] = 0; n   for (int i = 1; i <= n; i++) n     for (int j = 0; j <= 2; j++) n       for (int k = 0; k <= 2; k++) { n         if (j > 0) dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k]); n         if (k > 0) dp[i][j][k] = max(dp[i][j][k], dp[i][j][k - 1]); n         dp[i][j][k] = max(dp[i][j][k], dp[i - 1][j][k] + (j == 1 ? a[i] : 0) * n                                                              (k == 1 ? x : 1)); n       } n   cout << dp[n][2][2]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double pi = n     3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862; n void gn(int &x) { n   int sg = 1; n   char c; n   while (((c = getchar()) <  0  || c >  9 ) && c !=  - ) n     ; n   if (c ==  - ) n     sg = -1, x = 0; n   else n     x = c -  0 ; n   while ((c = getchar()) >=  0  && c <=  9 ) x = x * 10 + c -  0 ; n   x *= sg; n } n void gn(long long &x) { n   int sg = 1; n   char c; n   while (((c = getchar()) <  0  || c >  9 ) && c !=  - ) n     ; n   if (c ==  - ) n     sg = -1, x = 0; n   else n     x = c -  0 ; n   while ((c = getchar()) >=  0  && c <=  9 ) x = x * 10 + c -  0 ; n   x *= sg; n } n int mo = 1000000007; n int inf = 1061109567; n double eps = 1e-6; n int qp(int a, long long b) { n   int ans = 1; n   do { n     if (b & 1) ans = 1ll * ans * a % mo; n     a = 1ll * a * a % mo; n   } while (b >>= 1); n   return ans; n } n int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } n int dx[4] = {1, 0, -1, 0}; n int dy[4] = {0, 1, 0, -1}; n int a[333]; n int n; n void out(int i, int t) { n   for (int j = 0; j < t; j++) putchar(i - 1 +  a ); n } n int ord[333]; n int main() { n   gn(n); n   for (int i = 1; i <= n; i++) gn(a[i]); n   int su = 0; n   int numodd = 0; n   for (int i = 1; i <= n; i++) numodd += a[i] & 1, su += a[i]; n   if (numodd > 1) { n     printf( 0 n ); n     for (int i = 1; i <= n; i++) out(i, a[i]); n     putchar(  n ); n     return 0; n   } n   int d = 0; n   for (int i = 1; i <= n; i++) d = gcd(d, a[i]); n   int no1 = 0; n   for (int i = 1; i <= n; i++) no1 += (a[i] / d) & 1; n   if (no1 > 1 && d % 2 == 0) d /= 2, no1 = 0; n   for (int i = 1; i <= n; i++) a[i] /= d; n   for (int i = 1; i <= n; i++) ord[i] = i; n   for (int i = 1; i <= n; i++) n     if (a[i] & 1) swap(ord[i], ord[n]); n   if (no1) { n     printf( %d n , d); n   } else n     printf( %d n , d * 2); n   for (int tt = 1; tt <= d; tt++) { n     for (int i = 1; i <= n - 1; i++) { n       out(ord[i], a[ord[i]] / 2); n     } n     out(ord[n], a[ord[n]]); n     for (int i = n - 1; i >= 1; i--) { n       out(ord[i], a[ord[i]] / 2); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int pct(int x) { return __builtin_popcount(x); } n int pct(long long x) { return __builtin_popcountll(x); } n int bt(int x) { return 31 - __builtin_clz(x); } n int bt(long long x) { return 63 - __builtin_clzll(x); } n int cdiv(int a, int b) { return a / b + !(a < 0 || a % b == 0); } n long long cdiv(long long a, long long b) { n   return a / b + !(a < 0 || a % b == 0); n } n int nxt_C(int x) { n   int c = x & -x, r = x + c; n   return (((r ^ x) >> 2) / c) | r; n } n long long nxt_C(long long x) { n   long long c = x & -x, r = x + c; n   return (((r ^ x) >> 2) / c) | r; n } n vector<int> get_bits(int mask) { n   vector<int> bb; n   while (mask) { n     int b = bt(mask); n     bb.push_back(b); n     mask ^= (1 << b); n   } n   reverse(bb.begin(), bb.end()); n   return bb; n } n int get_mask(vector<int> v) { n   int mask = 0; n   for (int x : v) { n     mask ^= (1 << x); n   } n   return mask; n } n template <typename T> n void uniq(vector<T> &v) { n   sort(v.begin(), v.end()); n   v.resize(unique(v.begin(), v.end()) - v.begin()); n } n mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); n long long rand(long long l, long long r) { n   uniform_int_distribution<long long> uid(l, r); n   return uid(rng); n } n void sc() {} n template <typename Head, typename... Tail> n void sc(Head &H, Tail &...T) { n   cin >> H; n   sc(T...); n } n const int mod = 1e9 + 7; n int pwr(int a, long long b) { n   int ans = 1; n   while (b) { n     if (b & 1) ans = (ans * 1LL * a) % mod; n     a = (a * 1LL * a) % mod; n     b >>= 1; n   } n   return ans; n } n const int N = 1005; n vector<int> s[N]; n int in[N]; n int query(vector<int> v) { n   cout <<  ?   << (int)v.size() <<    ; n   for (int x : v) { n     cout << x <<    ; n   } n   cout << endl; n   int y; n   sc(y); n   return y; n } n int go(int l, int r, int maxi) { n   if (l == r) return l; n   int m = (l + r) >> 1; n   vector<int> v; n   for (int i = l; i <= m; i++) { n     v.push_back(i); n   } n   int q = query(v); n   if (q == maxi) { n     return go(l, m, maxi); n   } else { n     return go(m + 1, r, maxi); n   } n } n void solve() { n   memset(in, 0, sizeof in); n   int n, k; n   sc(n, k); n   for (int i = 1; i <= k; i++) { n     s[i].clear(); n     int c; n     sc(c); n     for (int j = 1; j <= c; j++) { n       int x; n       sc(x); n       s[i].push_back(x); n       in[x] = i; n     } n   } n   vector<int> v; n   for (int i = 1; i <= n; i++) { n     v.push_back(i); n   } n   int maxi = query(v); n   int idx = go(1, n, maxi); n   int kk = in[idx]; n   v.clear(); n   for (int i = 1; i <= n; i++) { n     if (in[i] != kk) v.push_back(i); n   } n   int z = query(v); n   cout <<  !  ; n   for (int i = 1; i <= k; i++) { n     if (i == kk) { n       cout << z <<    ; n     } else { n       cout << maxi <<    ; n     } n   } n   cout << endl; n   string s; n   sc(s); n   if (s ==  Incorrect ) { n     exit(0); n   } n } n int main() { n   ios ::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t = 1; n   cin >> t; n   for (int tt = 1; tt <= t; tt++) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct number { n   long long sum, a, b; n   bool operator<(const number& in) const { return sum < in.sum; } n   bool operator>(const number& in) const { return sum > in.sum; } n }; n vector<number> v; n const long long MN = 2000 + 1; n long long a[MN], b[MN]; n long long n, m, suma, sumb; n int main() { n   cin >> n; n   for (long long i = 0; i < n; i++) { n     cin >> a[i]; n     suma += a[i]; n   } n   cin >> m; n   for (long long j = 0; j < m; j++) { n     cin >> b[j]; n     sumb += b[j]; n   } n   for (long long i = 0; i < m; i++) n     for (long long j = i + 1; j < m; j++) { n       number temp; n       temp.a = i; n       temp.b = j; n       temp.sum = 2 * (b[i] + b[j]); n       v.push_back(temp); n     } n   sort(v.begin(), v.end()); n   long long best = abs(suma - sumb), in1 = -1, in2, in3 = -1; n   for (long long i = 0; i < n; i++) n     for (long long j = 0; j < m; j++) n       if (best > abs(suma - sumb - a[i] - a[i] + b[j] + b[j])) { n         best = abs(suma - sumb - a[i] - a[i] + b[j] + b[j]); n         in1 = i; n         in2 = j; n       } n   if (m > 1 && n > 1) { n     for (long long i = 0; i < n; i++) n       for (long long j = i + 1; j < n; j++) { n         long long want = suma - sumb - a[i] - a[j] - a[i] - a[j]; n         want *= -1; n         vector<number>::iterator low, up; n         number temp; n         temp.sum = want; n         low = std::lower_bound(v.begin(), v.end(), temp); n         up = upper_bound(v.begin(), v.end(), temp); n         if (low >= v.begin() && low < v.end()) n           if (best > abs(want - v[low - v.begin()].sum)) { n             best = abs(want - v[low - v.begin()].sum); n             in1 = i; n             in2 = j; n             in3 = low - v.begin(); n           } n         while (up >= v.end()) up--; n         if (best > abs(want - v[up - v.begin()].sum)) { n           best = abs(want - v[up - v.begin()].sum); n           in1 = i; n           in2 = j; n           in3 = up - v.begin(); n         } n       } n   } n   if (in1 == -1) { n     cout << best << endl << 0 << endl; n     return 0; n   } n   if (in3 == -1) { n     cout << best << endl << 1 << endl << in1 + 1 <<     << in2 + 1 << endl; n     return 0; n   } n   cout << best << endl; n   cout << 2 << endl; n   cout << in1 + 1 <<     << v[in3].a + 1 << endl; n   cout << in2 + 1 <<     << v[in3].b + 1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const long long LL_INF = 0x3f3f3f3f3f3f3f3f; n const int xx[4] = {1, -1, 0, 0}; n const int yy[4] = {0, 0, 1, -1}; n int inline nextin() { n   int t; n   if (scanf( %d , &t) != 1) exit(0); n   return t; n } n int inline in() { n   int x = 0, c; n   for (; (unsigned)((c = getchar()) -  0 ) >= 10;) { n     if (c ==  - ) return -in(); n     if (!~c) throw ~0; n   } n   do { n     x = (x << 3) + (x << 1) + (c -  0 ); n   } while ((unsigned)((c = getchar()) -  0 ) < 10); n   return x; n } n int n, k; n string s[10005]; n map<int, int> m; n int rhyme(int pos) { n   string a = s[pos].substr(m[pos], ((int)(s[pos]).size()) - m[pos]); n   string b = n       s[pos + 1].substr(m[pos + 1], ((int)(s[pos + 1]).size()) - m[pos + 1]); n   string c = n       s[pos + 2].substr(m[pos + 2], ((int)(s[pos + 2]).size()) - m[pos + 2]); n   string d = n       s[pos + 3].substr(m[pos + 3], ((int)(s[pos + 3]).size()) - m[pos + 3]); n   if (a == b && b == c && c == d) return 0; n   if (a == b && c == d) return 1; n   if (a == c && b == d) return 2; n   if (a == d && b == c) return 3; n   return -1; n } n const string ans[4] = { aaaa ,  aabb ,  abab ,  abba }; n int main() { n   cin.sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> k; n   for (int i = 0; i < (4 * n); i++) cin >> s[i]; n   for (int i = 0; i < (4 * n); i++) { n     int cnt = 0; n     bool ok = false; n     for (int j = ((int)(s[i]).size()) - 1; j >= 0; j--) { n       if (s[i][j] ==  a  || s[i][j] ==  e  || s[i][j] ==  i  || n           s[i][j] ==  o  || s[i][j] ==  u ) n         cnt++; n       if (cnt == k) { n         m[i] = j; n         ok = true; n         break; n       } n     } n     if (!ok) { n       puts( NO ); n       return 0; n     } n   } n   set<int> Set; n   for (int i = 0; i < (n); i++) { n     Set.insert(rhyme(i * 4)); n   } n   set<int>::iterator it = Set.begin(); n   if (*it == -1) n     puts( NO ); n   else if (((int)(Set).size()) >= 3) n     puts( NO ); n   else if (((int)(Set).size()) == 2) { n     if (*it == 0) { n       it++; n       cout << ans[*it] << endl; n     } else n       puts( NO ); n   } else n     cout << ans[*it] << endl; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const double EPS = 1e-9; n namespace number_theory { n long long gcd(long long x, long long y) { n   if (x == 0) return y; n   if (y == 0) return x; n   return gcd(y, x % y); n } n bool isprime(long long n) { n   if (n <= 1) return false; n   if (n <= 3) return true; n   if (n % 2 == 0 || n % 3 == 0) return false; n   for (long long i = 5; i * i <= n; i += 6) n     if (n % i == 0 || n % (i + 2) == 0) return false; n   return true; n } n bool prime[15000105]; n void sieve(long long n) { n   for (long long i = 0; i <= n; i++) prime[i] = 1; n   for (long long p = 2; p * p <= n; p++) { n     if (prime[p] == true) { n       for (long long i = p * p; i <= n; i += p) prime[i] = false; n     } n   } n   prime[1] = prime[0] = 0; n } n vector<long long> primelist; n bool __primes_generated__ = 0; n void genprimes(long long n) { n   __primes_generated__ = 1; n   sieve(n + 1); n   for (long long i = 2; i <= n; i++) n     if (prime[i]) primelist.push_back(i); n } n vector<long long> factors(long long n) { n   if (!__primes_generated__) { n     cerr <<  Caint genprimes you dope  << endl; n     exit(1); n   } n   vector<long long> facs; n   for (long long i = 0; n        primelist[i] * primelist[i] <= n && i < primelist.size(); i++) { n     if (n % primelist[i] == 0) { n       while (n % primelist[i] == 0) { n         n /= primelist[i]; n         facs.push_back(primelist[i]); n       } n     } n   } n   if (n > 1) { n     facs.push_back(n); n   } n   sort(facs.begin(), facs.end()); n   return facs; n } n vector<long long> getdivs(long long n) { n   vector<long long> divs; n   for (long long i = 1; i * i <= n; i++) { n     if (n % i == 0) { n       divs.push_back(i); n       divs.push_back(n / i); n     } n   } n   { n     sort(divs.begin(), divs.end()); n     divs.erase(unique(divs.begin(), divs.end()), divs.end()); n   }; n   return divs; n } n }  // namespace number_theory n void solve() { n   long long n, k; n   cin >> n >> k; n   string s; n   cin >> s; n   long long win = 0, loss = 0, winning_streak_cnt = 0; n   vector<long long> lossing_streak; n   for (long long i = 0; i < n; i++) { n     if (s[i] ==  W ) { n       win++; n       if (i == 0 or s[i - 1] ==  L ) winning_streak_cnt++; n     } else { n       loss++; n       if (i == 0 or s[i - 1] ==  W ) lossing_streak.push_back(0); n       lossing_streak.back()++; n     } n   } n   if (k >= loss) { n     cout << 2 * n - 1 <<   n ; n     return; n   } n   if (win == 0) { n     if (k == 0) n       cout << 0; n     else n       cout << 2 * k - 1; n     cout <<   n ; n     return; n   } n   win += k; n   if (s[0] ==  L ) lossing_streak[0] = 1e8; n   if (s[n - 1] ==  L ) lossing_streak.back() = 1e8; n   sort(lossing_streak.begin(), lossing_streak.end()); n   for (long long ls : lossing_streak) { n     if (ls > k) break; n     k -= ls; n     winning_streak_cnt--; n   } n   cout << 2 * win - winning_streak_cnt <<   n ; n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   long long t; n   cin >> t; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma warning(disable : 4996) n int main(int argc, char **argv) { n   ifstream from; n   const int nmin = 2, nmax = 300, kmin = 1, kmax = 300; n   int n, rt, rt1, rt2, i, j, j0, j1, k; n   int err = 0; n   char ch; n   cin >> n; n   vector<vector<int>> d(n); n   for (i = 0; i < n; i++) d[i].resize(n); n   for (i = 0; i < n; i++) { n     for (j = 0; j < n; j++) { n       cin >> d[i][j]; n     } n   } n   cin >> k; n   vector<int> a(k); n   vector<int> b(k); n   vector<int> c(k); n   for (i = 0; i < k; i++) { n     cin >> a[i] >> b[i] >> c[i]; n     a[i]--; n     b[i]--; n   } n   vector<long long> q(k); n   for (i = 0; i < k; i++) { n     if (d[a[i]][b[i]] > c[i]) { n       d[a[i]][b[i]] = c[i]; n       d[b[i]][a[i]] = c[i]; n     } n     q[i] = 0; n     for (j0 = 0; j0 < n - 1; j0++) { n       for (j1 = j0 + 1; j1 < n; j1++) { n         rt1 = d[j0][a[i]] + d[a[i]][b[i]] + d[b[i]][j1]; n         rt2 = d[j0][b[i]] + d[a[i]][b[i]] + d[a[i]][j1]; n         rt = (rt1 < rt2) ? rt1 : rt2; n         if (rt < d[j0][j1]) { n           d[j0][j1] = rt; n           d[j1][j0] = rt; n         } n         q[i] += d[j0][j1]; n       } n     } n   } n   for (i = 0; i < k - 1; i++) { n     cout << q[i] <<    ; n   } n   cout << q[k - 1] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, i, j, k, l, sum = 0, flag = 0, t, a[200005], ans = 0; n vector<int> tree[800005]; n void build(int node, int low, int high) { n   if (low == high) { n     tree[node].push_back(a[low]); n     return; n   } n   int mid = (low + high) / 2; n   build(2 * node, low, mid); n   build(2 * node + 1, mid + 1, high); n   merge((tree[2 * node]).begin(), (tree[2 * node]).end(), n         (tree[2 * node + 1]).begin(), (tree[2 * node + 1]).end(), n         back_inserter(tree[node])); n   return; n } n int query(int node, int low, int high, int l, int r, int val) { n   if (l > r) return 0; n   if (low > r || high < l) return 0; n   if (l <= low && high <= r) n     return tree[node].end() - n            lower_bound((tree[node]).begin(), (tree[node]).end(), val); n   int mid = (low + high) / 2; n   return query(2 * node, low, mid, l, r, val) + n          query(2 * node + 1, mid + 1, high, l, r, val); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   for (i = 1; i <= n; i++) cin >> a[i]; n   build(1, 1, n); n   for (i = 1; i <= n; i++) { n     ans += query(1, 1, n, i + 1, min(a[i], n), i); n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma comment(linker,  /stack:200000000 ) n #pragma GCC optimize( Ofast ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) n const long long mod = 1e9 + 7; n class matrix { n  public: n   vector<vector<long long> > v; n   matrix() { v = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}}; } n   vector<long long>& operator[](long long loc) { return v[loc]; } n }; n matrix mult(matrix a, matrix b) { n   matrix c; n   for (long long i = 0; i < 3; i++) n     for (long long j = 0; j < 3; j++) { n       c.v[i][j] = 0; n       for (long long k = 0; k < 3; k++) n         c.v[i][j] = (c.v[i][j] + a.v[i][k] * b.v[k][j]) % mod; n     } n   return c; n } n matrix power(matrix base, long long expo) { n   matrix ans; n   while (expo) { n     if (expo & 1) { n       ans = mult(base, ans); n     } n     base = mult(base, base); n     expo = expo >> 1; n   } n   return ans; n } n int32_t main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   long long n, m; n   cin >> n >> m; n   vector<long long> b(4, 0); n   vector<pair<pair<long long, long long>, long long> > v(n); n   multiset<pair<long long, long long> > s; n   for (long long i = 0; i < n; i++) { n     cin >> v[i].second >> v[i].first.first >> v[i].first.second; n     s.insert({v[i].first.first, v[i].second}); n     s.insert({v[i].first.second + 1, -v[i].second}); n   } n   long long prev = 1; n   matrix ans, c; n   c[0] = {1, 1, 0}; n   c[1] = {1, 1, 1}; n   c[2] = {0, 1, 1}; n   for (auto& i : s) { n     long long loc = abs(i.second); n     long long curr = i.first - 1; n     if (curr > prev) { n       ans = mult(power(c, curr - prev), ans); n       prev = curr; n     } n     long long val = i.second / loc; n     loc--; n     b[loc] += val; n     if (b[loc] <= 0) { n       c[loc] = {loc != 2, 1, loc != 0}; n     } else { n       c[loc] = {0, 0, 0}; n     } n   } n   ans = mult(power(c, m - prev), ans); n   cout << ans[1][1]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct p { n   int s, n; n }; n int comp(struct p a, struct p b) { return (a.s <= b.s); } n int main() { n   vector<long long> a, sb, s, r; n   long long i, j, k, n, ss, sss, rrr; n   cin >> n; n   s.resize(n + 2, 0); n   r.resize(n + 2, 0); n   sb.push_back(0); n   for (i = 1; i <= n; i++) { n     cin >> k; n     a.push_back(k); n     sb.push_back(sb.back() + k); n   } n   ss = sb.back(); n   if (ss % 3 != 0 || n <= 2) { n     cout <<  0  << endl; n     return 0; n   } n   sss = 0; n   rrr = 0; n   for (i = 0; i < n; i++) { n     if (sss == ss / 3 && i != 0) s[i]++; n     if (rrr == ss / 3 && i != 0) r[i]++; n     sss += a[i]; n     rrr += a[n - i - 1]; n     s[i + 1] = s[i]; n     r[i + 1] = r[i]; n   } n   long long c = s[0], var = 0; n   for (i = 1; i < n; i++) { n     if (s[i] != c) { n       c = s[i]; n       var += r[n - i - 1]; n     } n   } n   cout << var << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_N = 2000005; n int n, m; n int in[MAX_N]; n int inv[MAX_N]; n int out[MAX_N], temp[MAX_N]; n int pos2bckt[MAX_N]; n int bckt[MAX_N], relabel[MAX_N]; n int bpos[MAX_N]; n void build() { n   for (int i = 0; i < n; i++) out[i] = i; n   sort(out, out + n, [](int a, int bckt) { return in[a] < in[bckt]; }); n   for (int i = 0, c = 0; i < n; i++) { n     bckt[i] = c; n     if (i + 1 == n || in[out[i]] != in[out[i + 1]]) c++; n   } n   for (int h = 1; h < n; h <<= 1) { n     for (int i = 0; i < n; i++) pos2bckt[out[i]] = bckt[i]; n     for (int i = n - 1; i >= 0; i--) bpos[bckt[i]] = i; n     for (int i = 0; i < n; i++) n       if (out[i] >= n - h) temp[bpos[bckt[i]]++] = out[i]; n     for (int i = 0; i < n; i++) n       if (out[i] >= h) temp[bpos[pos2bckt[out[i] - h]]++] = out[i] - h; n     for (int i = 0, c = 0; i < n; i++) { n       relabel[i] = c; n       if (i + 1 == n) break; n       if (bckt[i] != bckt[i + 1]) n         c++; n       else if (temp[i + 1] >= n - h || temp[i] >= n - h) n         c++; n       else if (pos2bckt[temp[i + 1] + h] != pos2bckt[temp[i] + h]) n         c++; n     } n     memcpy(out, temp, sizeof(temp[0]) * n); n     memcpy(bckt, relabel, sizeof(relabel[0]) * n); n   } n } n char str[MAX_N]; n int cnt[MAX_N], mmn[MAX_N], rmmn[MAX_N]; n int main() { n   scanf( %s , str); n   while (str[n]) n++; n   for (int i = 0; i < n; i++) in[i] = str[i] ==  ) ; n   for (int i = 0; i < n; i++) in[i + n] = in[i]; n   int step = 0, mn = 0; n   for (int i = 0; i < n; i++) { n     if (in[i] == 1) n       step--; n     else n       step++; n     cnt[i + 1] = step; n     if (mn > step) mn = step; n     mmn[i + 1] = mn; n   } n   int p = step; n   for (int i = n - 1; i >= 0; i--) { n     if (in[i] == 1) n       step++; n     else n       step--; n     if (p > step) p = step; n     rmmn[i] = p; n   } n   n *= 2; n   build(); n   n /= 2; n   int c[2] = { n       0, n   }; n   for (int i = 0; i < n; i++) c[in[i]]++; n   if (c[0] >= c[1]) { n     for (int i = 0; i < 2 * n; i++) { n       if (out[i] < n && rmmn[out[i]] - cnt[out[i]] >= 0 && n           cnt[n] - cnt[out[i]] + mmn[out[i]] >= 0) { n         for (int j = 0; j < n; j++) str[j] = in[out[i] + j] == 0 ?  (  :  ) ; n         for (int j = n, k = 0; k < c[0] - c[1]; j++, k++) str[j] =  ) ; n         break; n       } n     } n   } else { n     for (int i = 0; i < 2 * n; i++) { n       if (out[i] < n && rmmn[out[i]] - cnt[out[i]] >= c[0] - c[1] && n           cnt[n] - cnt[out[i]] + mmn[out[i]] >= c[0] - c[1]) { n         for (int j = 0; j < c[1] - c[0]; j++) str[j] =  ( ; n         for (int j = c[1] - c[0], k = 0; k < n; j++, k++) n           str[j] = in[out[i] + k] == 0 ?  (  :  ) ; n         break; n       } n     } n   } n   puts(str); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int powr(long long a, long long b) { n   if (b == 0) { n     return 1; n   } n   if (b % 2 == 0) { n     return powr(((a % 1000000007) * (a % 1000000007)) % 1000000007, b / 2); n   } else { n     if (b == 1) { n       return a; n     } else { n       return (a * powr(((a % 1000000007) * (a % 1000000007) % 1000000007), n                        (b - 1) / 2)) % n              1000000007; n     } n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long s, b; n   cin >> s >> b; n   long long a[s]; n   long long u[b]; n   for (long long i = 0; i < s; i++) { n     cin >> a[i]; n   } n   vector<pair<long long, long long> > p; n   long long c, d; n   for (long long i = 0; i < b; i++) { n     cin >> c >> d; n     p.push_back({c, d}); n     u[i] = c; n   } n   sort(p.begin(), p.end()); n   sort(u, u + b); n   long long y = 0; n   long long sum[b]; n   for (long long i = 0; i < b; i++) { n     y += p[i].second; n     sum[i] = y; n   } n   for (long long i = 0; i < s; i++) { n     pair<long long, long long> cur = {a[i], INT_MAX}; n     auto idx = lower_bound(p.begin(), p.end(), cur); n     if (idx == p.begin()) { n       cout << 0 <<    ; n       continue; n     } n     idx--; n     long long j = idx - p.begin(); n     cout << sum[j] <<    ; n   } n   cerr <<  Time :   << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC n        <<  ms n ; n   return 0; n } n


#include <bits/stdc++.h> n using llong = long long; n using namespace std; n set<llong> factors; n void f(llong num) { n   factors.insert(1); n   llong to = sqrt(num); n   for (auto i = 2; i <= to; ++i) { n     if (num % i == 0) { n       factors.insert(i); n       factors.insert(num / i); n     } n   } n   factors.insert(num); n } n int main() { n   ios_base::sync_with_stdio(false); n   cout.tie(0); n   cin.tie(0); n   llong n; n   cin >> n; n   f(n); n   set<llong> res; n   for (auto e : factors) { n     res.insert((n + 1 + 1) * (n / e + 1) / 2 - (n + 1)); n   } n   for (auto i : res) cout << i <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n typedef struct { n   int u, v, cu, cv; n } Edge; n const int N = 500010, M = 1000010; n pair<int, int> stk[M]; n int top; n Edge e[N]; n int cnt; n int fa[M]; n int co[N]; n int sz[M]; n int st[N]; n int n, m, k; n int find(int x) { n   if (x == fa[x]) n     return x; n   else n     return find(fa[x]); n } n void merge(int u, int v) { n   int fu = find(u), fv = find(v); n   if (sz[fu] > sz[fv]) swap(fu, fv); n   stk[++top] = {fu, fv}; n   fa[fu] = fv; n   sz[fv] += sz[fu]; n } n int cmp(Edge a, Edge b) { n   if (a.cu == b.cu) n     return a.cv < b.cv; n   else n     return a.cu < b.cu; n } n void undo() { n   pair<int, int> t = stk[top--]; n   fa[t.first] = t.first; n   sz[t.second] -= sz[t.first]; n } n int main() { n   cin >> n >> m >> k; n   for (int i = 1; i <= n + n; i++) sz[i] = 1, fa[i] = i; n   for (int i = 1; i <= n; i++) cin >> co[i]; n   for (int i = 1; i <= m; i++) { n     int u, v; n     cin >> u >> v; n     if (co[u] == co[v]) { n       int fu = find(u), fv = find(v); n       if (fu == fv) { n         st[co[u]] = 1; n       } else { n         int fuu = find(u + n), fvv = find(v + n); n         merge(fu, fvv), merge(fv, fuu); n       } n     } else { n       e[++cnt] = {u, v, min(co[u], co[v]), max(co[v], co[u])}; n     } n   } n   long long aa = 0; n   for (int i = 1; i <= k; i++) n     if (!st[i]) aa++; n   long long ans = aa * (aa - 1) / 2; n   sort(e + 1, e + 1 + cnt, cmp); n   int idx = top; n   int flag = 0; n   for (int i = 1; i <= cnt; i++) { n     if (e[i].cv != e[i - 1].cv || e[i].cu != e[i - 1].cu) { n       flag = 0; n       while (idx != top) undo(); n     } n     if (st[e[i].cv] || st[e[i].cu]) continue; n     if (flag) continue; n     int fu = find(e[i].u), fv = find(e[i].v); n     if (fu == fv) { n       ans--; n       flag = 1; n       continue; n     } n     int fuu = find(e[i].u + n), fvv = find(e[i].v + n); n     merge(fu, fvv), merge(fv, fuu); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int mod = 1000000007; n int dv[1000005]; n int cnt[1000006]; n inline void add(int &r, int a) { n   r += a; n   if (r >= mod) r -= mod; n } n int mPow(int a, int x) { n   int ret = 1; n   while (x > 0) { n     if (x & 1) ret = 1LL * ret * a % mod; n     a = 1LL * a * a % mod; n     x >>= 1; n   } n   return ret; n } n int fact[200006], rfact[200006], C[200006]; n int _C(int n, int k) { n   if (k > n || k < 0) return 0; n   return 1LL * fact[n] * rfact[k] % mod * rfact[n - k] % mod; n } n int dp[1000006], n, k, q; n int rem[55] = {0}, base; n int p[1000006]; n int ans = 0, old[1000006]; n inline void go3(const vector<pair<int, int>> &v, int pos, int num) { n   if (pos == v.size()) { n     ++cnt[num]; n     add(ans, mod - 1LL * p[num] * (old[num] - C[cnt[num]] + mod) % mod); n     old[num] = C[cnt[num]]; n     return; n   } n   vector<int> tmp(v[pos].second + 1); n   for (int i = 0; i <= v[pos].second; ++i) { n     tmp[i] = num; n     num *= v[pos].first; n   } n   rem[pos] = 0; n   for (int i = v[pos].second; i >= 0; --i) { n     go3(v, pos + 1, tmp[i]); n     ++rem[pos]; n   } n } n void go1(const vector<pair<int, int>> &v, int pos, int num) { n   if (pos == v.size()) { n     if (base != num) { n       p[base] -= p[num]; n     } n     return; n   } n   for (int i = 0; i <= v[pos].second; ++i) { n     go1(v, pos + 1, num); n     num *= v[pos].first; n   } n } n int main() { n   scanf( %d%d%d , &n, &k, &q); n   fact[0] = rfact[0] = 1; n   for (int i = 1; i <= n + q; ++i) { n     fact[i] = 1LL * fact[i - 1] * i % mod; n     rfact[i] = mPow(fact[i], mod - 2); n   } n   for (int i = 0; i <= n + q; ++i) { n     C[i] = _C(i, k); n   } n   for (int i = 2; i <= 1000000; ++i) { n     if (dv[i] == 0) { n       for (int j = i; j <= 1000000; j += i) { n         if (dv[j] == 0) dv[j] = i; n       } n     } n   } n   for (int i = 1; i <= 1000000; ++i) { n     p[i] = i; n     int aa = i; n     vector<pair<int, int>> v; n     while (aa > 1) { n       int d = dv[aa], cnt = 0; n       while (aa % d == 0) { n         aa /= d; n         ++cnt; n       } n       v.emplace_back(make_pair(d, cnt)); n     } n     base = i; n     go1(v, 0, 1); n   } n   for (int i = 0; i < n + q; ++i) { n     int aa; n     scanf( %d , &aa); n     vector<pair<int, int>> v; n     while (aa > 1) { n       int d = dv[aa], cnt = 0; n       while (aa % d == 0) { n         aa /= d; n         ++cnt; n       } n       v.emplace_back(make_pair(d, cnt)); n     } n     go3(v, 0, 1); n     if (i >= n) { n       printf( %d n , ans); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3fffffff; n const long long _INF = 0x7ffffffffffffffll; n const double EPS = 1e-9; n const double PI = acos(-1.0); n template <class T, class INT> n inline T pow(T x, INT y) { n   T res(1); n   for (T tt(x); y; y /= 2) { n     if (y & 1) res *= tt; n     tt *= tt; n   } n   return res; n } n template <class T, class INT> n inline T pow(T x, INT y, T p) { n   T res(1); n   for (T tt(x); y; y /= 2) { n     if (y & 1) res = res * tt % p; n     tt = tt * tt % p; n   } n   return res; n } n int toInt(string s) { n   int x = 0; n   istringstream sin(s); n   sin >> x; n   return x; n } n template <class T> n inline string toString(T x) { n   ostringstream sout; n   sout << x; n   return sout.str(); n } n template <class INT> n inline INT gcd(INT x, INT y) { n   return y ? gcd(y, x % y) : x; n } n template <class INT> n inline int calc_bits(INT x) { n   return x ? (x & 1) + calc_bits(x >> 1) : 0; n } n const int MAXN = 1000 + 10; n int n, m, cnt, r[MAXN], c[MAXN], u[MAXN], d[MAXN]; n string a[MAXN]; n double ans; n inline long long calc(int x, int n) { n   return (n - x) * (n - x - 1) / 2 + (x + 1) * x / 2; n } n int main(int argc, char* argv[]) { n   cin >> n >> m; n   for (int i = 0; i < (n); ++i) cin >> a[i]; n   cnt = n * m; n   memset(r, 255, sizeof(r)), memset(c, 255, sizeof(c)); n   for (int i = 0; i < (n); ++i) n     for (int j = 0; j < (m); ++j) n       if (a[i][j] ==  X ) r[i] = j, c[j] = i, --cnt; n   for (int i = 0; i < (n); ++i) { n     u[i] = r[i]; n     d[i] = m - r[i] - 1; n     if (r[i] != -1) { n       if (i && r[i - 1] != -1 && r[i - 1] < r[i]) u[i] += u[i - 1]; n       if (i && r[i - 1] != -1 && r[i - 1] > r[i]) d[i] += d[i - 1]; n     } n   } n   for (int i = (n - 1); i > (-1); --i) n     if (r[i] != -1) { n       if (i < n - 1 && r[i + 1] != -1 && r[i + 1] < r[i]) u[i] += u[i + 1]; n       if (i < n - 1 && r[i + 1] != -1 && r[i + 1] > r[i]) d[i] += d[i + 1]; n     } n   for (int i = 0; i < (m); ++i) ans += n * n * calc(i, m); n   for (int i = 0; i < (n); ++i) n     for (int j = 0; j < (n); ++j) { n       ans += (r[i] == -1 ? m : m - 1) * (r[j] == -1 ? m : m - 1) * abs(i - j); n       if (i == j && r[i] != -1) n         ans += 2 * (u[i] * (m - r[i] - 1) + d[i] * r[i]); n       if (r[i] != -1) ans -= calc(r[i], m); n       if (r[j] != -1) ans -= calc(r[j], m); n       if (r[i] != -1 && r[j] != -1) ans += abs(r[i] - r[j]); n     } n   for (int i = 0; i < (m); ++i) { n     u[i] = c[i]; n     d[i] = n - c[i] - 1; n     if (c[i] != -1) { n       if (i && c[i - 1] != -1 && c[i - 1] < c[i]) u[i] += u[i - 1]; n       if (i && c[i - 1] != -1 && c[i - 1] > c[i]) d[i] += d[i - 1]; n     } n   } n   for (int i = (m - 1); i > (-1); --i) n     if (c[i] != -1) { n       if (i < m - 1 && c[i + 1] != -1 && c[i + 1] < c[i]) u[i] += u[i + 1]; n       if (i < m - 1 && c[i + 1] != -1 && c[i + 1] > c[i]) d[i] += d[i + 1]; n     } n   for (int i = 0; i < (m); ++i) n     if (c[i] != -1) ans += 2 * (u[i] * (n - c[i] - 1) + d[i] * c[i]); n   cout << setprecision(8) << fixed << ans / cnt / cnt << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1005; n const long long mod = 1e9 + 7; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long n; n   cin >> n; n   cout << (n + 1) / 2 - 1; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC target( avx,avx2,fma ) n using namespace std; n long long gcd(long long a, long long b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n long long lcm(long long a, long long b) { return a * b / (gcd(a, b)); } n void solve() { n   long long n, ans = 0; n   string s; n   cin >> n >> s; n   for (long long i = 0; i < n - 2; i++) { n     if (s[i] ==  x  && s[i + 1] ==  x  && s[i + 2] ==  x ) ans++; n   } n   cout << ans << endl; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   solve(); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n long long ans, t, s, a, i, b, k; n int main() { n   cin >> k >> a >> b; n   s = (0 - a) / k + 1; n   while (a <= 0) { n     a = a + s * k; n     b = s * k + b; n   } n   ans = b / k - (a - 1) / k; n   cout << ans; n } n


#include <bits/stdc++.h> n int main() { n   int N; n   std::cin >> N; n   std::set<std::pair<int, int>> doubled; n   std::set<std::pair<int, int>> pending; n   int to_double = 0; n   int l_doubled = 0; n   int64_t total_damage = 0; n   int t; n   int d; n   for (int i = 0; i < N; i++) { n     std::cin >> t; n     std::cin >> d; n     total_damage += d; n     to_double += t * (d > 0 ? 1 : -1); n     if (d > 0) { n       pending.insert({d, t}); n     } else { n       auto search = doubled.find({-d, t}); n       if (search != doubled.end()) { n         doubled.erase(search); n         l_doubled -= t; n         total_damage += d; n       } else { n         pending.erase(pending.find({-d, t})); n       } n     } n     if (to_double > doubled.size()) { n       if (pending.size() > 0) { n         auto it_pending = --pending.end(); n         std::pair<int, int> max = *it_pending; n         doubled.insert(max); n         pending.erase(it_pending); n         l_doubled += max.second; n         total_damage += max.first; n       } n     } else if (to_double < doubled.size()) { n       if (doubled.size() > 0) { n         auto it_doubled = doubled.begin(); n         std::pair<int, int> min = *it_doubled; n         doubled.erase(it_doubled); n         pending.insert(min); n         l_doubled -= min.second; n         total_damage -= min.first; n       } n     } else { n       if (to_double > 0 && pending.size() > 0) { n         auto it_doubled = doubled.begin(); n         auto it_pending = --pending.end(); n         std::pair<int, int> max = *it_pending; n         std::pair<int, int> min = *it_doubled; n         if (max > min) { n           doubled.erase(it_doubled); n           pending.erase(it_pending); n           doubled.insert(max); n           pending.insert(min); n           l_doubled += max.second - min.second; n           total_damage += max.first - min.first; n         } n       } n     } n     if (doubled.size() == to_double && l_doubled == to_double && n         to_double > 0) { n       auto it_doubled = doubled.begin(); n       std::pair<int, int> min = *it_doubled; n       doubled.erase(it_doubled); n       total_damage -= min.first; n       l_doubled--; n       if (pending.size() > 0) { n         auto it_pending = --pending.end(); n         std::pair<int, int> max = *it_pending; n         pending.erase(it_pending); n         doubled.insert(max); n         total_damage += max.first; n       } n       pending.insert(min); n     } n     std::cout << total_damage << std::endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxN = 1000000; n long long a1, a2, t0, t1, t2, p1, p2, num, f, T, Tim, tot1, tot2, l, r, ans, n     Num; n bool fl; n void check1(long long my) { n   num = my / p1; n   if (num * a1 >= f) { n     if (t1 * f <= T) fl = 1; n     return; n   } n   for (long long i = num; i >= max(0LL, num - maxN); --i) { n     long long left_money = my - i * p1, left_time = T - i * tot1, n               left_data = f - i * a1; n     if (left_time < 0) return; n     if (left_data <= 0) { n       fl = 1; n       return; n     } n     if (t2 >= t0) { n       Tim = left_data * t0; n       if (Tim <= left_time) fl = 1; n       return; n     } n     Num = left_time % tot2 ? left_time / tot2 + 1 : left_time / tot2; n     Num = min(num, left_money / p2); n     if (Num * a2 >= left_data) n       Tim = left_data * t2; n     else n       Tim = Num * tot2 + (left_data - Num * a2) * t0; n     if (Tim <= left_time) { n       fl = 1; n       return; n     } n     --Num; n     if (Num * a2 >= left_data) n       Tim = left_data * t2; n     else n       Tim = Num * tot2 + (left_data - Num * a2) * t0; n     if (Tim <= left_time) { n       fl = 1; n       return; n     } n   } n } n void check2(long long my) { n   num = my / p2; n   if (num * a2 >= f) { n     if (t2 * f <= T) fl = 1; n     return; n   } n   for (long long i = num; i >= max(0LL, num - maxN); --i) { n     long long left_money = my - i * p2, left_time = T - i * tot2, n               left_data = f - i * a2; n     if (left_time < 0) return; n     if (left_data <= 0) { n       fl = 1; n       return; n     } n     if (t1 >= t0) { n       Tim = left_data * t0; n       if (Tim <= left_time) fl = 1; n       return; n     } n     Num = left_time % tot1 ? left_time / tot1 + 1 : left_time / tot1; n     Num = min(Num, left_money / p1); n     if (Num * a1 >= left_data) n       Tim = left_data * t1; n     else n       Tim = Num * tot1 + (left_data - Num * a1) * t0; n     if (Tim <= left_time) { n       fl = 1; n       return; n     } n     --Num; n     if (Num * a1 >= left_data) n       Tim = left_data * t1; n     else n       Tim = Num * tot1 + (left_data - Num * a1) * t0; n     if (Tim <= left_time) { n       fl = 1; n       return; n     } n   } n } n bool check(long long my) { n   fl = 0; n   check1(my); n   if (fl) return 1; n   check2(my); n   if (fl) return 1; n   return 0; n } n int main() { n   scanf( %lld%lld%lld , &f, &T, &t0); n   scanf( %lld%lld%lld , &a1, &t1, &p1); n   tot1 = a1 * t1; n   scanf( %lld%lld%lld , &a2, &t2, &p2); n   tot2 = a2 * t2; n   if (t0 * f <= T) { n     puts( 0 ); n     return 0; n   } n   num = T % tot1 ? T / tot1 + 1 : T / tot1; n   r = num * p1; n   num = T % tot2 ? T / tot2 + 1 : T / tot2; n   r = max(r, num * p2); n   l = 1; n   r *= 2; n   ans = -1; n   while (l <= r) { n     long long mid = (l + r) >> 1; n     if (check(mid)) n       ans = mid, r = mid - 1; n     else n       l = mid + 1; n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> e[200010]; n vector<int> a; n int n; n int siz[200010]; n int id[200010]; n int d[200010]; n int c[200010]; n int f[200010]; n int dfn; n void dfs(int x, int fa) { n   id[x] = ++dfn; n   siz[x]++; n   for (int i = 0; i < e[x].size(); i++) { n     if (e[x][i] == fa) continue; n     dfs(e[x][i], x); n     siz[x] += siz[e[x][i]]; n   } n } n void dfs(int x, int fa, int *y) { n   int temp = (n - siz[x]); n   bool flag = true; n   for (int i = 0; i < e[x].size(); i++) { n     if (e[x][i] == fa) continue; n     dfs(e[x][i], x, y); n     if (temp != siz[e[x][i]]) { n       flag = false; n     } n   } n   if (flag && siz[x] > 1) { n     *y = x; n   } n } n bool judge(int x, int fa, int dep) { n   if (c[x] != -1 && f[x] != fa && c[x] != fa) return false; n   f[x] = -1; n   for (int i = 0; i < e[x].size(); i++) { n     int nt = e[x][i]; n     if (nt == fa) continue; n     int son_siz = siz[nt]; n     if (id[x] > id[nt]) son_siz = n - siz[x]; n     if (d[dep] == -1) { n       d[dep] = son_siz; n     } n     if (d[dep] != son_siz) { n       c[x] = nt; n       return false; n     } n     f[x] = nt; n   } n   for (int i = 0; i < e[x].size(); i++) { n     int nt = e[x][i]; n     if (nt == fa) continue; n     if (!judge(nt, x, dep + 1)) { n       c[x] = nt; n       return false; n     } n     f[x] = nt; n   } n   f[x] = -1; n   return true; n } n inline bool scan_d(int &num) { n   char in; n   bool IsN = false; n   in = getchar(); n   if (in == EOF) return false; n   while (in !=  -  && (in <  0  || in >  9 )) in = getchar(); n   if (in ==  - ) { n     IsN = true; n     num = 0; n   } else n     num = in -  0 ; n   while (in = getchar(), in >=  0  && in <=  9 ) { n     num *= 10, num += in -  0 ; n   } n   if (IsN) num = -num; n   return true; n } n int main() { n   int x = -1, u, v, k = -1; n   scanf( %d , &n); n   for (int i = 1; i < n; i++) { n     scan_d(u); n     scan_d(v); n     if (k == -1) k = u; n     e[u].push_back(v); n     e[v].push_back(u); n   } n   dfs(1, 1); n   dfs(1, 1, &x); n   for (int i = 1; i <= n; i++) { n     if (e[i].size() <= 1) { n       a.push_back(i); n     } n   } n   if (x != -1) { n     a.push_back(x); n   } n   memset(d, -1, sizeof(d)); n   memset(c, -1, sizeof(c)); n   memset(f, -1, sizeof(f)); n   for (int i = 0; i < a.size(); i++) { n     if (judge(a[i], a[i], 1)) { n       printf( %d n , a[i]); n       return 0; n     } n     for (int i = 1;; i++) { n       if (d[i] == -1) break; n       d[i] = -1; n     } n   } n   printf( -1 n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int dx[] = {1, 0, -1, 0}; n const int dy[] = {0, 1, 0, -1}; n struct Point { n   int x, y; n } Q[2000 * 2000], P, np; n int n, m; n int b[2000][2000]; n int mark[2000][2000], T; n int L[2000 * 2000]; n int flag[2000][2000]; n bool out(int x, int y) { return x < 0 || y < 0 || x >= n || y >= m; } n void Mark(int x, int y) { n   int head, tail; n   int d, i, j, k; n   head = tail = 0; n   Q[tail].x = x; n   Q[tail].y = y; n   tail++; n   while (head < tail) { n     P = Q[head++]; n     for (d = 0; d < 4; d++) { n       np.x = P.x + dx[d]; n       np.y = P.y + dy[d]; n       if (out(np.x, np.y) || b[np.x][np.y] == 0) continue; n       if (mark[np.x][np.y] != T) { n         mark[np.x][np.y] = T; n         Q[tail++] = np; n       } n     } n   } n } n int count(int x, int y) { n   int i, j, nx, ny; n   int cnt(0); n   int dx, dy; n   for (i = -2; i <= 2; i++) n     for (j = -2; j <= 2; j++) { n       nx = x + i; n       ny = y + j; n       if (out(nx, ny)) continue; n       if (mark[nx][ny] == mark[x][y]) cnt++; n     } n   if (cnt != 9) return false; n   for (dx = -1; dx <= 1; dx += 2) n     for (dy = -1; dy <= 1; dy += 2) { n       for (i = 0; i < 3; i++) { n         for (j = 0; j < 3; j++) { n           nx = x + dx * i; n           ny = y + dy * j; n           if (out(nx, ny) || b[nx][ny] == 0) break; n           if (mark[nx][ny] != mark[x][y]) break; n         } n         if (j < 3) break; n       } n       if (i == 3) return true; n     } n   return false; n } n int remove(int x, int y) { n   int head, tail; n   int d, i, j, k; n   head = tail = 0; n   Q[tail].x = x; n   Q[tail].y = y; n   tail++; n   flag[x][y] = true; n   for (k = 0; k < 10; k++) { n     for (i = tail; head < i; head++) { n       P = Q[head]; n       for (d = 0; d < 4; d++) { n         np.x = P.x + dx[d]; n         np.y = P.y + dy[d]; n         if (out(np.x, np.y) || b[np.x][np.y] == 0) continue; n         if (mark[np.x][np.y] != mark[x][y]) continue; n         if (flag[np.x][np.y] == false) { n           flag[np.x][np.y] = true; n           Q[tail++] = np; n         } n       } n     } n   } n   return tail <= 40; n } n int main() { n   int i, j, k, d; n   int cnt, x, y; n   scanf( %d%d , &n, &m); n   for (i = 0; i < n; i++) n     for (j = 0; j < m; j++) scanf( %d , b[i] + j); n   for (i = 0; i < n; i++) { n     for (j = 0; j < m; j++) { n       if (b[i][j] == 0 || mark[i][j]) continue; n       T++; n       Mark(i, j); n     } n   } n   printf( %d n , T); n   for (i = 0; i < n; i++) { n     for (j = 0; j < m; j++) { n       cnt = 0; n       if (!mark[i][j] || flag[i][j]) continue; n       for (d = 0; d < 4; d++) { n         x = i + dx[d]; n         y = j + dy[d]; n         if (out(x, y)) continue; n         cnt += b[x][y]; n       } n       if (cnt >= 3) continue; n       if (count(i, j) && remove(i, j)) { n         L[mark[i][j]]++; n       } n     } n   } n   sort(L + 1, L + T + 1); n   for (i = 1; i <= T; i++) { n     if (i > 1) printf(   ); n     printf( %d , L[i]); n   } n   printf(  n ); n } n


#include <bits/stdc++.h> n using namespace std; n const int mn = 55; n const int mm = 2505; n const int mod = 1000000007; n int n, m, T; n int t[mm], g[mm]; n int a[mn][mm]; n int bc[mn][mn][mm]; n int way[mn][mn][mn][3]; n int cnt[3]; n void init_a() { n   memset(a, -2, sizeof a); n   a[0][0] = 1; n   for (int k = 1; k <= n; k++) { n     if (g[k]) continue; n     for (int i = cnt[0]; i >= 0; i--) { n       for (int j = T - t[k]; j >= 0; j--) { n         if (a[i][j] <= 0) continue; n         int &ans = a[i + 1][j + t[k]]; n         if (ans < 0) ans = 0; n         ans += a[i][j]; n         if (ans >= mod) ans -= mod; n       } n     } n     cnt[0]++; n   } n } n void init_bc() { n   memset(bc, -2, sizeof bc); n   bc[0][0][0] = 1; n   for (int k = 1; k <= n; k++) { n     if (!g[k]) continue; n     for (int i = cnt[1]; i >= 0; i--) { n       for (int j = cnt[2]; j >= 0; j--) { n         for (int l = T - t[k]; l >= 0; l--) { n           if (bc[i][j][l] <= 0) continue; n           int &ans = bc[i + (g[k] == 1)][j + (g[k] == 2)][l + t[k]]; n           if (ans < 0) ans = 0; n           ans += bc[i][j][l]; n           if (ans >= mod) ans -= mod; n         } n       } n     } n     if (g[k] == 1) n       cnt[1]++; n     else n       cnt[2]++; n   } n } n void inc(int &a, int b) { n   if (a < 0) a = 0; n   a += b; n   if (a >= mod) a -= mod; n } n void init_way() { n   memset(way, -2, sizeof(way)); n   way[1][0][0][0] = way[0][1][0][1] = way[0][0][1][2] = 1; n   for (int i = 0; i <= cnt[0]; i++) { n     for (int j = 0; j <= cnt[1]; j++) { n       for (int k = 0; k <= cnt[2]; k++) { n         for (int last = 0; last < 3; last++) { n           for (int nxt = 0; nxt < 3; nxt++) { n             if (nxt == last || way[i][j][k][last] <= 0) continue; n             int uans = way[i][j][k][last]; n             if (nxt == 0) n               inc(way[i + 1][j][k][0], uans); n             else if (nxt == 1) n               inc(way[i][j + 1][k][1], uans); n             else n               inc(way[i][j][k + 1][2], uans); n           } n         } n       } n     } n   } n } n inline int getway(int i, int j, int k) { n   int ans = 0; n   for (int last = 0; last < 3; last++) { n     if (way[i][j][k][last] <= 0) continue; n     inc(ans, way[i][j][k][last]); n   } n   return ans; n } n int fact[105]; n int main() { n   scanf( %d%d , &n, &T); n   for (int i = 1; i <= n; i++) scanf( %d%d , &t[i], &g[i]), g[i]--; n   fact[0] = 1; n   for (int i = 1; i <= 55; i++) fact[i] = (long long)fact[i - 1] * i % mod; n   init_a(); n   init_bc(); n   init_way(); n   long long ans = 0; n   for (int i = 0; i <= cnt[0]; i++) { n     for (int j = 0; j <= cnt[1]; j++) { n       for (int k = 0; k <= cnt[2]; k++) { n         for (int t = 0; t <= T; t++) { n           if (a[i][t] <= 0 || bc[j][k][T - t] <= 0) continue; n           ans = (ans + (long long)a[i][t] * bc[j][k][T - t] % mod * n                            getway(i, j, k) % mod * fact[i] % mod * fact[j] % n                            mod * fact[k]) % n                 mod; n         } n       } n     } n   } n   printf( %lld n , ans); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n bool isPrime(int n) { n   for (int i = 2; (i * i) <= n; i++) { n     if (n % i == 0) return 0; n   } n   return 1; n } n int nerPr(int n) { n   for (int i = n;; i++) { n     if (isPrime(i)) return i; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int n; n   cin >> n; n   vector<int> a; n   int mn = (int)1e9; n   for (int i = 0; i < n; i++) { n     int d; n     cin >> d; n     a.push_back(d); n     mn = min(mn, d); n     if (d == 1) { n       cout <<  -1 n ; n       exit(0); n     } n   } n   cout << 1; n } n


#include <bits/stdc++.h> n using namespace std; n const long long linf = 4000000000000000000LL; n const long long inf = 1000000007; n const long double pi = 3.1415926535; n void pv(vector<int> a) { n   for (auto& x : a) cout << x <<    ; n   cout <<   n ; n } n void pv(vector<long long> a) { n   for (auto& x : a) cout << x <<    ; n   cout <<   n ; n } n void pv(vector<vector<int> > a) { n   for (int i = (0); i < (int(a.size())); ++i) { n     cout << i <<   n ; n     pv(a[i]); n     cout <<   n ; n   } n } n void pv(vector<vector<long long> > a) { n   for (int i = (0); i < (int(a.size())); ++i) { n     cout << i <<   n ; n     pv(a[i]); n   } n   cout <<   n ; n } n void pv(vector<string> a) { n   for (auto& x : a) cout << x <<   n ; n   cout <<   n ; n } n void setIO(string second) { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   if (int(second.size())) { n     freopen((second +  .in ).c_str(),  r , stdin); n     if (second !=  test1 ) freopen((second +  .out ).c_str(),  w , stdout); n   } n } n int n; n vector<long double> a; n vector<vector<int> > adj; n vector<int> parent; n void dfs(int i, int p) { n   parent[i] = p; n   for (auto& x : adj[i]) { n     if (x == p) continue; n     dfs(x, i); n   } n } n int main() { n   setIO(  ); n   cin >> n; n   a.resize(n); n   for (auto& x : a) cin >> x, x = 1 - x; n   adj.resize(n); n   for (int i = (0); i < (n - 1); ++i) { n     int x, y; n     cin >> x >> y; n     adj[x].push_back(y); n     adj[y].push_back(x); n   } n   parent.resize(n); n   dfs(0, -1); n   vector<long double> b(n, 1); n   for (int i = (0); i < (n); ++i) n     for (auto& x : adj[i]) n       if (x != parent[i]) b[i] -= a[x]; n   long double ans = 0; n   for (int i = (0); i < (n); ++i) ans += a[i] * b[i]; n   int testc; n   cin >> testc; n   while (testc--) { n     int i; n     long double x; n     cin >> i >> x; n     x = 1 - x; n     long double y = a[i]; n     if (parent[i] != -1) { n       b[parent[i]] -= x - y; n       ans -= (x - y) * a[parent[i]]; n     } n     ans += (x - y) * b[i]; n     a[i] = x; n     cout << fixed << setprecision(20) << ans <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 6; n const int M = 10010; n const int K = 510; n const int LIT = 2500; n const int INF = 1 << 30; n int n, m[N][N], tot, need; n vector<int> v, vis; n int row[N], col[N], dia1, dia2; n int cr[N], cc[N], cd1, cd2; n void init() { n   memset(m, 0, sizeof(m)); n   memset(row, 0, sizeof(row)); n   memset(col, 0, sizeof(col)); n   dia1 = dia2 = 0; n   memset(cr, 0, sizeof(cr)); n   memset(cc, 0, sizeof(cc)); n   cd1 = cd2 = 0; n   tot = n * n; n   need = 0; n   v = vector<int>(n * n); n   vis = vector<int>(n * n); n   for (int i = 0; i < tot; i++) cin >> v[i]; n   sort(v.begin(), v.end()); n   for (int i = 0; i < tot; i++) need += v[i]; n   need /= n; n } n void insert(int x, int y, int p, int ord) { n   m[x][y] = v[p]; n   vis[p] = (ord > 0) ? 1 : 0; n   int tmp = v[p] * ord; n   row[x] += tmp; n   cr[x] += ord; n   col[y] += tmp; n   cc[y] += ord; n   if (x == y) dia1 += tmp, cd1 += ord; n   if (x + y == n + 1) dia2 += tmp, cd2 += ord; n } n void out(int sum) { n   printf( %d n , sum); n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) printf( %d  , m[i][j]); n     printf(  n ); n   } n } n bool check() { n   int sum = dia1; n   for (int i = 1; i <= n; i++) n     if (sum != row[i]) return 0; n   for (int i = 1; i <= n; i++) n     if (sum != col[i]) return 0; n   if (dia1 != dia2) return 0; n   out(sum); n   return 1; n } n const int nxt[][2] = {{1, 1}, {1, 2}, {1, 3}, {2, 1}, {3, 1}, {1, 4}, n                       {4, 1}, {2, 2}, {3, 3}, {4, 4}, {2, 3}, {2, 4}, n                       {3, 2}, {3, 4}, {4, 2}, {4, 3}}; n const int bnd = 16; n int num[N * N]; n bool check(int cnt, int sum) { n   if (cnt == n) return (sum == need); n   if (cnt == n - 1) { n     for (int i = 0; i < tot; i++) n       if (!vis[i] && sum + v[i] == need) return 1; n     return 0; n   } n   if (cnt == n - 2) { n     int p = 0, x = need - sum; n     ; n     for (int i = 0; i < tot; i++) n       if (!vis[i]) num[p++] = v[i]; n     int l = 0, r = p - 1; n     while (l < r) { n       int tmp = num[l] + num[r]; n       if (tmp == x) return 1; n       if (tmp < x) l++; n       if (tmp > x) r--; n     } n     return 0; n   } n   return 1; n } n bool validate() { n   if (!check(cd1, dia1)) return 0; n   if (!check(cd2, dia2)) return 0; n   for (int i = 1; i <= n; i++) { n     if (!check(cr[i], row[i])) return 0; n     if (!check(cc[i], col[i])) return 0; n   } n   return 1; n } n bool dfs1(int x, int y) { n   if (x > n) return check(); n   int xx = x, yy = y + 1; n   if (yy > n) xx++, yy = 1; n   for (int i = 0; i < tot; i++) { n     if (vis[i]) continue; n     insert(x, y, i, 1); n     if (validate() && dfs1(xx, yy)) return 1; n     insert(x, y, i, -1); n   } n   return 0; n } n bool dfs(int p) { n   if (p >= bnd) return check(); n   int x = nxt[p][0], y = nxt[p][1]; n   for (int i = 0; i < tot; i++) { n     if (vis[i]) continue; n     insert(x, y, i, 1); n     if (validate() && dfs(p + 1)) return 1; n     insert(x, y, i, -1); n   } n   return 0; n } n void solve() { n   if (n <= 3) n     dfs1(1, 1); n   else n     dfs(0); n } n int main() { n   while (cin >> n) { n     init(); n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, x, y, ans; n int p[100010], q[100010], mn[100010]; n vector<int> t[100010], v[100010]; n bool flag[100010]; n inline int dist(int x, int y) { return abs(p[x] - q[y]); } n int main() { n   scanf( %d%d%d%d , &n, &m, &x, &y); n   memset(mn, 63, sizeof(mn)); n   for (int i = 1; i <= n; i++) scanf( %d , &p[i]); n   for (int i = 1; i <= m; i++) scanf( %d , &q[i]); n   q[m + 1] = 1e9; n   q[0] = -1e9; n   int pos = 1; n   for (int i = 1; i <= n; i++) { n     while (pos <= m && q[pos] <= p[i]) pos++; n     pos--; n     if (dist(i, pos) == dist(i, pos + 1)) { n       t[i].push_back(pos); n       t[i].push_back(pos + 1); n     } else if (dist(i, pos) < dist(i, pos + 1)) { n       t[i].push_back(pos); n     } else { n       t[i].push_back(pos + 1); n     } n   } n   for (int i = 1; i <= n; i++) n     if (t[i].size() == 1) { n       int t1 = t[i][0]; n       v[t1].push_back(i); n       mn[t1] = min(mn[t1], dist(i, t1)); n     } n   for (int i = 1; i <= n; i++) n     if (t[i].size() == 1) { n       int t1 = t[i][0]; n       if (dist(i, t1) > mn[t1]) flag[i] = 1; n     } n   for (int i = 1; i <= n; i++) n     if (t[i].size() == 2) { n       bool flag1 = 0, flag2 = 0; n       int cnt1 = 0, cnt2 = 0; n       int t1 = t[i][0], t2 = t[i][1], d = dist(i, t1); n       for (int j = 0; j < v[t1].size(); j++) { n         if (dist(v[t1][j], t1) < d) flag1 = 1; n         if (d < dist(v[t1][j], t1)) n           if (flag[v[t1][j]] == 0) cnt1++; n       } n       for (int j = 0; j < v[t2].size(); j++) { n         if (dist(v[t2][j], t2) < d) flag2 = 1; n         if (d < dist(v[t2][j], t2)) n           if (flag[v[t2][j]] == 0) cnt2++; n       } n       if (cnt1 + flag1 <= cnt2 + flag2) { n         flag[i] = flag1; n         for (int j = 0; j < v[t1].size(); j++) n           if (d < dist(v[t1][j], t1)) flag[v[t1][j]] = 1; n         mn[t1] = min(mn[t1], dist(i, t1)); n         v[t1].push_back(i); n       } else { n         flag[i] = flag2; n         for (int j = 0; j < v[t2].size(); j++) n           if (d < dist(v[t2][j], t2)) flag[v[t2][j]] = 1; n         mn[t2] = min(mn[t2], dist(i, t2)); n         v[t2].push_back(i); n       } n     } n   for (int i = 1; i <= n; i++) ans += flag[i]; n   printf( %d , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void reset(int& n, int first) { n = n & ~(1 << first); } n bool check(int n, int first) { return (bool)(n & (1 << first)); } n set<int> ax, ay; n multiset<int> bx, by; n set<int>::iterator l, r; n int main() { n   int i, j, k, n, w, h; n   while (scanf( %d%d%d , &w, &h, &n) == 3) { n     ax.clear(); n     ay.clear(); n     bx.clear(); n     by.clear(); n     ax.insert(0); n     ax.insert(h); n     ay.insert(0); n     ay.insert(w); n     bx.insert(h); n     by.insert(w); n     for (i = 0; i < n; i++) { n       char ch; n       int v; n       scanf( %c%c%d , &ch, &ch, &v); n       if (ch ==  H ) { n         r = ax.lower_bound(v); n         l = --r; n         r++; n         bx.erase(bx.find(*r - *l)); n         ax.insert(v); n         bx.insert(v - *l); n         bx.insert(*r - v); n       } else { n         r = ay.lower_bound(v); n         l = --r; n         r++; n         by.erase(by.find(*r - *l)); n         ay.insert(v); n         by.insert(v - *l); n         by.insert(*r - v); n       } n       long long ans = (long long)(*bx.rbegin()) * (*by.rbegin()); n       cout << ans << endl; n     } n   } n } n


#include <bits/stdc++.h> n int main() { n   int a, b, c = 0, d, e, i; n   scanf( %d , &a); n   for (i = 1; i <= a; i++) { n     scanf( %d , &b); n     if (b == 1) { n       c++; n     } n   } n   if (c > 0) { n     printf( HARD n ); n   } else { n     printf( EASY n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int mini[10], maxi[10], dist[10]; n int main() { n   int n, i, total; n   scanf( %d , &n); n   total = 0; n   for (i = 0; i < 3; ++i) { n     scanf( %d %d , &mini[i], &maxi[i]); n     total += mini[i]; n     dist[i] = mini[i]; n   } n   i = 0; n   while (n - total > 0) { n     int available, pot; n     available = maxi[i] - mini[i]; n     pot = n - total; n     dist[i] += min(available, pot); n     total += min(available, pot); n     i++; n   } n   for (i = 0; i < 2; ++i) { n     printf( %d  , dist[i]); n   } n   printf( %d n , dist[2]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 200002; n int val[M]; n vector<int> vec[33]; n set<int> st; n struct Nod { n   int v; n   int id; n   bool operator<(const Nod &cmp) const { n     if (v == cmp.v) return id < cmp.id; n     return v > cmp.v; n   } n } p[M]; n int n; n void init() { n   for (int i = 0; i < 32; i++) { n     for (int j = 0; j < n; j++) { n       if (val[j] & (1 << i)) { n         vec[i].push_back(j); n       } n     } n   } n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d , &val[i]); n     p[i].v = val[i]; n     p[i].id = i; n   } n   sort(p, p + n); n   init(); n   long long ans = 0; n   for (int i = 0; i < n; i++) { n     Nod u = p[i]; n     set<int>::iterator it = st.upper_bound(u.id); n     int L, R; n     if (it == st.end()) { n       R = n - 1; n     } else n       R = *it - 1; n     if (it == st.begin()) { n       L = 0; n     } else { n       it--; n       L = *it + 1; n     } n     int l = L, r = R; n     for (int j = 0; j < 32; j++) { n       if ((u.v & (1 << j)) == 0) { n         int tl = upper_bound(vec[j].begin(), vec[j].end(), u.id) - n                  vec[j].begin() - 1; n         if (tl >= 0) l = max(l, vec[j][tl] + 1); n         int tr = n             upper_bound(vec[j].begin(), vec[j].end(), u.id) - vec[j].begin(); n         if (tr < vec[j].size()) r = min(r, vec[j][tr] - 1); n       } n     } n     ans += 1ll * (u.id - l + 1) * (r - u.id + 1); n     st.insert(u.id); n   } n   printf( %I64d n , 1ll * n * (n + 1) / 2 - ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int a[12][2001]; n int dp[2][4096]; n int sos[4096]; n bool use[2001]; n pair<int, int> b[2001]; n void solve() { n   cin >> n >> m; n   for (int j = 1; j <= m; j++) b[j].first = 0; n   for (int i = 0; i < n; i++) { n     for (int j = 1; j <= m; j++) { n       cin >> a[i][j]; n       b[j].first = max(b[j].first, a[i][j]); n       b[j].second = j; n     } n   } n   sort(b + 1, b + m + 1); n   for (int j = m; j >= max(1, m - n + 1); j--) use[b[j].second] = true; n   for (int l = 0; l < (1 << n); l++) dp[0][l] = 0; n   int c = 0, p = 1; n   for (int j = 1; j <= m; j++) { n     if (!use[j]) continue; n     c ^= 1; n     p ^= 1; n     for (int l = 0; l < (1 << n); l++) dp[c][l] = 0; n     for (int k = 0; k < n; k++) { n       int tmp = a[0][j]; n       for (int l = 0; l < n - 1; l++) a[l][j] = a[l + 1][j]; n       a[n - 1][j] = tmp; n       for (int l = 0; l < (1 << n); l++) sos[l] = dp[p][l]; n       for (int i = 0; i < n; i++) { n         for (int l = 0; l < (1 << n); l++) { n           if (l & (1 << i)) sos[l] = max(sos[l], sos[l ^ (1 << i)] + a[i][j]); n         } n       } n       for (int l = 0; l < (1 << n); l++) dp[c][l] = max(dp[c][l], sos[l]); n     } n   } n   for (int j = m; j >= max(1, m - n + 1); j--) use[b[j].second] = false; n   cout << dp[c][(1 << n) - 1] <<   n ; n } n int main() { n   ios::sync_with_stdio(false); n   int t; n   cin >> t; n   while (t--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n int x, y, n; n int xs[30], ys[30]; n int mem[16777227]; n int ALL; n int dt[30][30]; n int calc(int mask) { n   if (mask == ALL) return 0; n   int& res = mem[mask]; n   if (res != -1) return res; n   res = ((int)1e9); n   int f = -1; n   for (int i = 0; i < n; ++i) { n     if ((mask >> i) & 1) continue; n     if (f == -1) { n       f = i; n       res = min(res, calc(mask | (1 << i)) + 2 * dt[n][i]); n     } else { n       int d = dt[n][i] + dt[n][f] + dt[i][f]; n       res = min(res, calc(mask | (1 << i) | (1 << f)) + d); n     } n   } n   return res; n } n int sz; n int sol[50]; n void build(int mask) { n   if (mask == ALL) return; n   int& res = mem[mask]; n   for (int i = 0; i < n; ++i) { n     if ((mask >> i) & 1) continue; n     if (res == calc(mask | (1 << i)) + 2 * dt[n][i]) { n       printf(  %d 0 , i + 1); n       build(mask | (1 << i)); n       return; n     } n     for (int j = 0; j < n; ++j) { n       if (i == j) continue; n       if ((mask >> j) & 1) continue; n       int d = dt[n][i] + dt[n][j] + dt[i][j]; n       if (res == calc(mask | (1 << i) | (1 << j)) + d) { n         printf(  %d %d 0 , i + 1, j + 1); n         build(mask | (1 << i) | (1 << j)); n         return; n       } n     } n   } n   return; n } n inline int dist(int x1, int y1, int x2, int y2) { n   return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2); n } n int main() { n   while (scanf( %d%d , &x, &y) != -1) { n     scanf( %d , &n); n     ALL = (1 << n) - 1; n     for (int i = 0; i < n; ++i) { n       scanf( %d%d , xs + i, ys + i); n       dt[i][n] = dt[n][i] = dist(xs[i], ys[i], x, y); n     } n     for (int i = 0; i < n; ++i) { n       for (int j = i + 1; j < n; ++j) { n         dt[i][j] = dt[j][i] = dist(xs[i], ys[i], xs[j], ys[j]); n       } n     } n     memset(mem, -1, sizeof mem); n     int res = calc(0); n     printf( %d n0 , res); n     build(0); n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long int n; n   cin >> n; n   string s; n   cin >> s; n   long long int mx[n], mn[n]; n   for (long long int i = 0; i < n; i++) { n     mx[i] = i + 1, mn[i] = n - i; n   } n   long long int j = 0; n   for (long long int i = 0; i < n - 1; i = j) { n     while (j < n && s[i] == s[j]) j++; n     if (s[i] ==  < ) n       reverse(mn + i, mn + j + 1); n     else n       reverse(mx + i, mx + j + 1); n   } n   for (long long int x : mn) cout << x <<    ; n   cout << endl; n   for (long long int x : mx) cout << x <<    ; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   long long int T = 1; n   cin >> T; n   while (T--) { n     solve(); n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long n, k; n   cin >> n >> k; n   vector<vector<pair<long long, long long>>> ans(k); n   long long gr = k, cnt = 0; n   bool f = false; n   vector<pair<pair<long long, long long>, long long>> a(n); n   long double c = 0; n   for (long long i = 1; i <= n; ++i) { n     pair<long long, long long> x; n     cin >> x.first >> x.second; n     if (x.second == 1 && gr > 1) gr--; n     if (x.second == 1) cnt++; n     if (x.second == 2) f = true; n     a[i - 1] = {x, i}; n   } n   sort(a.begin(), a.end(), n        greater<pair<pair<long long, long long>, long long>>()); n   k--; n   long long free = 0; n   for (long long i = 0; i < n; ++i) { n     if (k == -1) { n       for (long long j = i; j < n; ++j) { n         ans[free].push_back({a[j].first.first, a[j].second}); n         if (free + 1 < gr) free++; n       } n       break; n     } n     if (a[i].first.second == 1) { n       ans[k].push_back({a[i].first.first, a[i].second}); n       k--; n     } else { n       ans[free].push_back({a[i].first.first, a[i].second}); n       if (free + 1 < gr) free++; n     } n   } n   cout << fixed << setprecision(1); n   k = ans.size(); n   for (long long i = 0; i < ans.size(); ++i) { n     long long mn = 1e9; n     sort(ans[i].begin(), ans[i].end()); n     for (long long j = 0; j < ans[i].size(); ++j) { n       if (j == 0 && k - cnt - 1 < i) n         c += (long double)ans[i][j].first / 2; n       else n         c += ans[i][j].first; n     } n   } n   cout << c <<   n ; n   for (auto i : ans) { n     cout << i.size() <<    ; n     for (auto j : i) cout << j.second <<    ; n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   int n; n   cin >> n; n   map<int, int> m; n   int ma = -1; n   int ans = 0; n   for (int i = 0; i < n; i++) { n     int x; n     cin >> x; n     if (m[x] > ma) { n       ma = m[x]; n       ans = x; n     } n     m[x]++; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n string s; n string change(string s, int l, int r, char c1, char c2) { n   for (int i = l - 1; i < r; i++) { n     if (s[i] == c1) { n       s[i] = c2; n     } n   } n   return s; n } n int main() { n   cin >> n >> m; n   cin >> s; n   for (int i = 0; i < m; i++) { n     int l, r; n     char c1, c2; n     cin >> l >> r >> c1 >> c2; n     s = change(s, l, r, c1, c2); n   } n   cout << s; n } n


// #pragma GCC optimize( Ofast,unroll-loops ) n // #pragma GCC target( avx,avx2,fma ) n  n #include <bits/stdc++.h> n using namespace std; n  n #define ll long long n #define ull unsigned long long n #define dd double n #define ld long double n #define sl(n) scanf( %lld , &n) n #define si(n) scanf( %d , &n) n #define sd(n) scanf( %lf , &n) n #define pll pair <ll, ll> n #define pii pair <int, int> n #define mp make_pair n #define pb push_back n #define all(v) v.begin(), v.end() n #define inf (1LL << 61) n #define loop(i, start, stop, inc) for(ll i = start; i <= stop; i += inc) n #define for1(i, stop) for(ll i = 1; i <= stop; ++i) n #define for0(i, stop) for(ll i = 0; i < stop; ++i) n #define rep1(i, start) for(ll i = start; i >= 1; --i) n #define rep0(i, start) for(ll i = (start-1); i >= 0; --i) n #define ms(n, i) memset(n, i, sizeof(n)) n #define casep(n) printf( Case %lld: , ++n) n #define pn printf(  n ) n #define pf printf n #define EL   n  n #define fastio std::ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); n  n // === Debug macro starts here === n #ifndef ONLINE_JUDGE n #define DEBUG n #define SYS_COL system( COLOR ) n #endif n  n int recur_depth = 0; n #ifdef DEBUG n #define dbg(x) {++recur_depth; auto x_=x; --recur_depth; SYS_COL;   n                 cerr<<string(recur_depth,   t )<<  [91m <<__func__<< :    n                 <<__LINE__<<  t <<#x<<  =  <<x_<<  [39m <<endl;} n  n template<typename Ostream, typename Cont> n typename enable_if<is_same<Ostream,ostream>::value, n             Ostream&>::type operator<<(Ostream& os,  const Cont& v) { n  os<< [ ; n  for(auto& x:v){os<<x<< ,  ;} n  return os<< ] ; n } n template<typename Ostream, typename ...Ts> n Ostream& operator<<(Ostream& os,  const pair<Ts...>& p){ n  return os<< { <<p.first<< ,  <<p.second<< } ; n } n #else n #define dbg(x) n #endif n // === Debug macro ends here === n  n #define ff first n #define ss second n  n const ll sz = 1e5 + 10, mod = 1e9 + 7; n ll fact[sz], inv[sz]; n  n ll fastPow(ll x, ll n, ll MOD) n { n     ll ret = 1; n     while (n) n     { n         if (n & 1) ret = (ret * x) % MOD; n         x = (x * x) % MOD; n         n >>= 1; n     } n     return ret % MOD; n } n  n inline ll ncr(ll n, ll r) n { n     if(n < r) return 0; n     ll den = (inv[r] * inv[n-r]) % mod; n     return (fact[n] * den) % mod; n } n  n int main() n { n     fact[0] = 1; n     for1(i, sz-1) fact[i] = (fact[i-1] * i) % mod; n  n     inv[sz-1] = fastPow(fact[sz-1], mod-2, mod); n     rep0(i, sz-1) inv[i] = (inv[i+1] * (i+1)) % mod; n  n     ll t; n     cin >> t; n  n     while(t--) { n         ll n, k; n         sl(n), sl(k); n  n         ll ans = 0; n  n         for1(p, n) { n             ll posCell = n - (k-1)*(p-1); n             if(posCell <= 0) break; n  n             ll prob = (fact[p] * inv[n]) % mod; n             prob = (prob * fact[n-p]) % mod; n  n             ans += (ncr(posCell, p) * prob) % mod; n             if(ans >= mod) ans -= mod; n         } n         pf( %lld n , (ans+1)%mod); n     } n  n     return 0; n } n  n


#include <bits/stdc++.h> n using namespace std; n const int N = 3e5 + 10; n int a[N], n, l, r, ans; n bool check(int k) { n   for (int i = 1; i <= n; ++i) { n     if (1LL * k * (i - 1) > a[i]) return false; n     if (1LL * k * (n - i) > a[i]) return false; n   } n   return true; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) scanf( %d , &a[i]); n   l = 0, r = 1e9, ans = 0; n   while (l <= r) { n     int mid = l + r >> 1; n     if (check(mid)) { n       ans = mid; n       l = mid + 1; n     } else n       r = mid - 1; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1e5 + 5; n long long v[N], mn[N]; n int main() { n   long long n; n   long long ans = 0; n   cin >> n; n   for (long long i = 0; i < n; i++) cin >> v[i]; n   for (long long i = 0; i < n; i++) mn[i] = v[i] + 1; n   for (long long i = 1; i < n; i++) mn[i] = max(mn[i - 1], mn[i]); n   for (long long i = n - 2; i >= 0; i--) mn[i] = max(mn[i], mn[i + 1] - 1); n   for (long long i = 0; i < n; i++) ans += mn[i] - v[i] - 1; n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int P = 1e9 + 7; n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n long long qpow(long long a, long long n) { n   long long r = 1 % P; n   for (a %= P; n; a = a * a % P, n >>= 1) n     if (n & 1) r = r * a % P; n   return r; n } n long long inv(long long x) { return x <= 1 ? 1 : inv(P % x) * (P - P / x) % P; } n const int N = 2e5 + 10; n int a[N], h[N], f[N], n, m, k, p; n struct _ { n   long long day, id; n   bool operator<(const _& rhs) const { return day > rhs.day; } n }; n priority_queue<_> q; n bool check(long long x) { n   memset(f, 0, sizeof f); n   while (!q.empty()) q.pop(); n   for (int i = 1; i <= n; ++i) n     if (x - (long long)a[i] * m < h[i]) q.push({x / a[i], i}); n   for (int i = 1; i <= m; ++i) n     for (int j = 1; j <= k; ++j) { n       if (q.empty()) return 1; n       _ u = q.top(); n       q.pop(); n       if (u.day < i) return 0; n       if (x + (long long)p * ++f[u.id] - (long long)m * a[u.id] < h[u.id]) { n         q.push({(x + (long long)p * f[u.id]) / a[u.id], u.id}); n       } n     } n   return q.empty(); n } n int main() { n   scanf( %d%d%d%d , &n, &m, &k, &p); n   for (int i = 1; i <= n; ++i) scanf( %d%d , h + i, a + i); n   long long l = 0, r = 1e15, ans; n   while (l <= r) n     check((l + r >> 1))                        ? ans = (l + r >> 1), n                           r = (l + r >> 1) - 1 : l = (l + r >> 1) + 1; n   cout << ans << endl; n } n


#include <bits/stdc++.h> n const long long int m = 1000000007; n using namespace std; n int main() { n   int tc; n   cin >> tc; n   while (tc--) { n     int x1, y1, x2, y2; n     cin >> x1 >> y1 >> x2 >> y2; n     if (x1 == x2 || y1 == y2) n       cout << abs(x1 - x2) + abs(y2 - y1) <<   n ; n     else n       cout << abs(x2 - x1) + abs(y2 - y1) + 2 <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int n, q; n   cin >> n >> q; n   long long int arr[n]; n   long long int aftern[n]; n   long long int maxi[n]; n   for (long long int i = 0; i < n; i++) { n     cin >> arr[i]; n     if (i == 0) n       maxi[i] = i; n     else { n       if (arr[i] > arr[maxi[i - 1]]) n         maxi[i] = i; n       else n         maxi[i] = maxi[i - 1]; n     } n   } n   long long int i = n - 1; n   long long int e = n - 1; n   while (i >= 0) { n     long long int maxin = maxi[i]; n     for (long long int j = maxin; j < e; j++) { n       aftern[j] = arr[j + 1]; n     } n     aftern[e] = arr[maxin]; n     i = maxin - 1; n     e = i; n   } n   long long int max_in = maxi[n - 1]; n   for (long long int i = 0; i < q; i++) { n     long long int x; n     cin >> x; n     if (x > (n - 1)) { n       long long int t = x % (n - 1); n       if (t != 0) n         cout << arr[max_in] <<     << aftern[t - 1] <<   n ; n       else n         cout << arr[max_in] <<     << aftern[n - 2] <<   n ; n     } else { n       cout << arr[maxi[x - 1]] <<     << arr[x] <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n template <typename T> n void chmin(T &a, T b) { n   if (a > b) a = b; n } n template <typename T> n void chmax(T &a, T b) { n   if (a < b) a = b; n } n template <typename T> n void add(T &a, T b) { n   a += b; n   if (a < 0) a += MOD; n   if (a >= MOD) a -= MOD; n } n void FastIn(int &T) { n   char C; n   bool F = 0; n   for (; C = getchar(), C <  0  || C >  9 ;) n     if (C ==  - ) F = 1; n   for (T = C -  0 ; C = getchar(), C >=  0  && C <=  9 ; T = T * 10 + C -  0 ) n     ; n   F && (T = -T); n } n int in() { n   int x; n   scanf( %d , &x); n   return x; n } n long long lin() { n   long long x; n   scanf( %lld , &x); n   return x; n } n double din() { n   double x; n   scanf( %lf , &x); n   return x; n } n long long p2[63]; n void initP2() { n   p2[0] = 1; n   for (int i = (1); i <= (62); ++i) { n     p2[i] = p2[i - 1] * 2; n   } n } n int countBitsBaseB(long long n, long long b) { n   if (n == 0) return 1; n   int ret = 0; n   while (n) { n     ret++; n     n /= b; n   } n   return ret; n } n int count1(long long n) { n   int ret = 0; n   while (n) { n     ret += n & 1; n     n >>= 1; n   } n   return ret; n } n long long c[1024][1024] = {0}; n void initC() { n   for (int i = 0; i < (1024); ++i) { n     for (int j = 0; j < (i + 1); ++j) { n       if (j == 0 || j == i) { n         c[i][j] = 1; n       } else { n         c[i][j] = c[i - 1][j - 1]; n         add(c[i][j], c[i - 1][j]); n       } n     } n   } n } n bool IsPrime(int n) { n   if (n < 2) return false; n   for (int i = 2; i * i <= n; i++) { n     if (n % i == 0) return false; n   } n   return true; n } n void init() { n   initP2(); n   initC(); n } n const unsigned INVALID = 2000000001; n struct CMD { n   int cmd, x, y; n } q[(int)(2e5 + 10)]; n struct DSU { n   struct INFO { n     int x, f, h; n     INFO() {} n     INFO(int x_, int f_, int h_) : x(x_), f(f_), h(h_) {} n   }; n   int f[(int)(2e5 + 10)]; n   int h[(int)(2e5 + 10)]; n   INFO stk[(int)(2e5 + 10) << 1]; n   int stkSize = 0; n   void init(int n) { n     for (int i = (1); i <= (n); ++i) { n       f[i] = i; n       h[i] = 1; n     } n     stkSize = 0; n   } n   int find(int x) { return x == f[x] ? x : find(f[x]); } n   void merge(int x, int y) { n     x = find(x); n     y = find(y); n     if (x == y) { n       return; n     } n     if (h[x] < h[y]) { n       stk[stkSize++] = (INFO(x, f[x], h[x])); n       f[x] = y; n     } else if (h[y] < h[x]) { n       stk[stkSize++] = (INFO(y, f[y], h[y])); n       f[y] = x; n     } else { n       stk[stkSize++] = (INFO(x, f[x], h[x])); n       stk[stkSize++] = (INFO(y, f[y], h[y])); n       f[x] = y; n       h[y]++; n     } n   } n   void undo(int size) { n     while (stkSize > size) { n       pop(); n     } n   } n  n  private: n   void pop() { n     stkSize--; n     auto info = stk[stkSize]; n     int x = info.x; n     f[x] = info.f; n     h[x] = info.h; n   } n } dsu; n const int BLOCK = 5000; n int last[(int)(2e5 + 10)]; n pair<int, int> GetEdge(int x, int y, int der, int n) { n   x += der - 1; n   if (x >= n) x -= n; n   y += der - 1; n   if (y >= n) y -= n; n   if (x > y) swap(x, y); n   return make_pair(x + 1, y + 1); n } n void Switch(const pair<int, int> &edge, set<pair<int, int>> &edges) { n   if (edges.find(edge) != edges.end()) { n     edges.erase(edge); n   } else { n     edges.insert(edge); n   } n } n int main() { n   init(); n   int n = in(), m = in(); n   for (int i = (1); i <= (m); ++i) { n     q[i].cmd = in(); n     q[i].x = in(); n     q[i].y = in(); n   } n   last[0] = 0; n   for (int start = 1; start <= m; start += BLOCK) { n     int end = start + BLOCK - 1; n     if (end > m) { n       end = m; n     } n     set<pair<int, int>> nowEdges; n     nowEdges.clear(); n     for (int i0 = (start); i0 <= (end); ++i0) { n       nowEdges.insert(GetEdge(q[i0].x, q[i0].y, 0, n)); n       nowEdges.insert(GetEdge(q[i0].x, q[i0].y, 1, n)); n     } n     set<pair<int, int>> possible; n     possible.clear(); n     set<pair<int, int>> impossible; n     impossible.clear(); n     for (int i1 = (1); i1 <= (start - 1); ++i1) { n       if (q[i1].cmd == 2) { n         continue; n       } n       auto edge0 = GetEdge(q[i1].x, q[i1].y, last[i1 - 1], n); n       if (nowEdges.find(edge0) == nowEdges.end()) { n         Switch(edge0, impossible); n       } else { n         Switch(edge0, possible); n       } n     } n     dsu.init(n); n     for (auto edge1 : impossible) { n       dsu.merge(edge1.first, edge1.second); n     } n     for (int i2 = (start); i2 <= (end); ++i2) { n       if (q[i2].cmd == 1) { n         last[i2] = last[i2 - 1]; n         Switch(GetEdge(q[i2].x, q[i2].y, last[i2 - 1], n), possible); n         continue; n       } else { n         const int top = dsu.stkSize; n         for (auto edge1 : possible) { n           dsu.merge(edge1.first, edge1.second); n         } n         auto queryEdge = GetEdge(q[i2].x, q[i2].y, last[i2 - 1], n); n         last[i2] = dsu.find(queryEdge.first) == dsu.find(queryEdge.second); n         putchar( 0  + last[i2]); n         dsu.undo(top); n       } n     } n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long x[5003], a[5003], b[5003], c[5003], d[5003]; n vector<int> path; n long long weight[5003][5003]; n long long edge(int i, int j) { n   long long ans = abs(x[i] - x[j]); n   if (j < i) ans += (c[i] + b[j]); n   if (i < j) ans += (d[i] + a[j]); n   return ans; n } n int main() { n   int n, s, e; n   cin >> n >> s >> e; n   for (int i = 1; i <= n; i++) cin >> x[i]; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   for (int i = 1; i <= n; i++) cin >> b[i]; n   for (int i = 1; i <= n; i++) cin >> c[i]; n   for (int i = 1; i <= n; i++) cin >> d[i]; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) weight[i][j] = edge(i, j); n   long long ans = weight[s][e]; n   path.push_back(s); n   path.push_back(e); n   for (int i = 1; i <= n; i++) { n     if (i == s || i == e) continue; n     int insert_at = 0; n     long long best = 1e12; n     for (int j = 0; j < path.size() - 1; j++) { n       long long temp = weight[path[j]][i] + weight[i][path[j + 1]] - n                        weight[path[j]][path[j + 1]]; n       if (temp < best) { n         best = temp; n         insert_at = j; n       } n     } n     ans += best; n     vector<int>::iterator ins = find(path.begin(), path.end(), path[insert_at]); n     ins++; n     path.insert(ins, i); n   } n   cout << ans << endl; n } n