#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 10; n int T, n, a, b, c; n string s; n int shi, bu, jian; n int main() { n   cin >> T; n   while (T--) { n     shi = bu = jian = 0; n     cin >> n; n     cin >> a >> b >> c; n     cin >> s; n     for (int i = 0; i < s.length(); i++) { n       if (s[i] ==  R ) n         shi++; n       else if (s[i] ==  P ) n         bu++; n       else n         jian++; n     } n     if (min(a, jian) + min(b, shi) + min(c, bu) < (n + 1) / 2) { n       cout <<  NO  << endl; n       continue; n     } n     cout <<  YES  << endl; n     string t =   ; n     for (int i = 0; i < s.length(); i++) { n       if (s[i] ==  R  && b) { n         t +=  P ; n         b--; n       } else if (s[i] ==  P  && c) { n         t +=  S ; n         c--; n       } else if (s[i] ==  S  && a) { n         a--; n         t +=  R ; n       } else { n         t +=  _ ; n       } n     } n     for (int i = 0; i != n; ++i) { n       if (t[i] !=  _ ) continue; n       if (a) { n         t[i] =  R ; n         a--; n       } else if (b) { n         t[i] =  P ; n         b--; n       } else { n         t[i] =  S ; n         c--; n       } n     } n     cout << t <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1000 * 100 + 3, inf = 1000 * 1000 * 100; n int n, m, a[MAXN], b[MAXN], t[MAXN], ans = 0, y, z; n int main() { n   cin >> n >> m >> y >> z; n   for (int i = 0; i < n; i++) cin >> a[i]; n   b[0] = -inf; n   b[m + 1] = inf; n   for (int i = 1; i <= m; i++) cin >> b[i]; n   for (int i = 0; i <= m; i++) t[i] = -1; n   for (int i = 0; i < n; i++) { n     int ind = lower_bound(b, b + m + 2, a[i]) - b; n     int nex = b[ind], prev = b[ind - 1]; n     if (nex - a[i] < a[i] - prev) { n       if (t[ind] != -1) ans++; n       t[ind] = nex - a[i]; n     } else if (nex - a[i] > a[i] - prev) { n       if (t[ind - 1] != -1 && t[ind - 1] < a[i] - prev) { n         ans++; n       } else { n         if (t[ind - 1] > a[i] - prev) ans++; n         t[ind - 1] = a[i] - prev; n       } n     } else { n       if (t[ind - 1] == -1 || t[ind - 1] == a[i] - prev) { n         t[ind - 1] = a[i] - prev; n       } else { n         t[ind] = nex - a[i]; n       } n     } n   } n   cout << ans << endl; n   return 0; n } n


/* n  * yaswanth phani kommineni n  */ n #include <bits/stdc++.h> n using namespace std; n typedef long long int ll; n #define endl   n ; n  n  n void solve(){ n  ll n; n  cin >> n; n  n*=2; n  vector <ll> v(n); n  map <ll,ll> m; n  for(ll i=0;i<n;i++){ n   cin >> v[i]; n   m[v[i]]++; n  } n  sort(v.begin(),v.end()); n  for(ll i=0;i<n-1;i++){ n   auto mp = m; n   auto V = v; n   vector <pair<ll,ll>> ans; n   ans.push_back(make_pair(v[n-1],v[i])); n   mp[v[n-1]]--; n   mp[v[i]]--; n   ll cur = v[n-1]; n   while(true){ n    while(!V.empty() && !mp[V.back()]){ n     V.pop_back(); n    } n    if(V.empty()) break; n    ll last = V.back(); n    mp[last]--; n    if(!mp[cur-last]){ n     break; n    } n    ans.push_back(make_pair(last,cur-last)); n    mp[cur-last]--; n    cur = last; n   } n   if((ll)ans.size()==n/2){ n    cout <<  YES  << endl; n    cout << ans.front().first + ans.front().second << endl; n    for(auto x : ans){ n     cout << x.second <<     << x.first << endl; n    } n    return; n   } n  } n  cout <<  NO  << endl; n } n  n int main(){ n  //freopen( input.txt , r ,stdin); n  //freopen( output.txt , w ,stdout); n  ios_base::sync_with_stdio(false); n  cin.tie(0); n  cout.tie(0); n  int tc; n  tc = 1; n  cin >> tc; n  for(int i=1;i<=tc;i++){ n   solve(); n  } n  return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int toDelete = 0, arr[1500010], N; n int main() { n   cin.sync_with_stdio(0); n   cin.tie(0); n   cin >> N; n   for (int i = 0; i < N; ++i) { n     int x; n     cin >> x; n     arr[x]++; n   } n   int ans = 0; n   for (int i = 0; i <= 1500000; ++i) { n     int val = arr[i]; n     int id = i; n     arr[i] = 0; n     while (val > 0) { n       arr[id] += val % 2; n       id++; n       val /= 2; n     } n     if (arr[i]) ans++; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n ostream& operator<<(ostream& out, vector<T>& v) { n   out <<  [ ; n   for (auto k : v) out << k <<    ; n   out <<  ] ; n   return out; n } n vector<int> g[500]; n string color; n bool posible = true; n void DFS(int n, int p, char c) { n   if (color[n] !=  -  && color[n] != c) { n     posible = false; n     return; n   } n   if (color[n] !=  - ) return; n   color[n] = c; n   char nc =  1  - c +  0 ; n   for (auto k : g[n]) n     if (k != p) DFS(k, n, nc); n } n int main() { n   int n; n   string in; n   cin >> n >> in; n   color.resize(n,  - ); n   for (int i = 0; i < int(n); i++) { n     for (int j = int(i + 1); j < int(n); j++) { n       if (in[i] > in[j]) { n         g[i].push_back(j); n         g[j].push_back(i); n       } n     } n   } n   for (int i = 0; i < int(n); i++) n     if (color[i] ==  - ) DFS(i, -1,  0 ); n   if (posible) n     cout <<  YES  << endl << color << endl; n   else n     cout <<  NO  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n vector<vector<pair<int, int> > > v(400005); n int parent[400005], low[400005], disc[400005], TIME; n bool visited[400005], road[400005]; n set<int> bridge; n int SIZE; n pair<int, int> EDGE[400005]; n void dfs(int a) { n   TIME++; n   visited[a] = true; n   disc[a] = TIME, low[a] = TIME; n   for (int i = 0; i < v[a].size(); i++) { n     if (!visited[v[a][i].first]) { n       parent[v[a][i].first] = a; n       dfs(v[a][i].first); n       low[a] = min(low[a], low[v[a][i].first]); n       if (low[v[a][i].first] > disc[a]) bridge.insert(v[a][i].second); n     } else if (v[a][i].first != parent[a]) n       low[a] = min(low[a], disc[v[a][i].first]); n   } n } n void dfs2(int a) { n   visited[a] = true; n   SIZE++; n   for (int i = 0; i < v[a].size(); i++) n     if (bridge.count(v[a][i].second) == 0 and !visited[v[a][i].first]) n       dfs2(v[a][i].first); n } n void solve(int a) { n   for (int i = 0; i < v[a].size(); i++) { n     if (!road[v[a][i].second]) { n       road[v[a][i].second] = true; n       if (bridge.count(v[a][i].second) == 0) n         EDGE[v[a][i].second] = make_pair(a, v[a][i].first); n       else n         EDGE[v[a][i].second] = make_pair(v[a][i].first, a); n       solve(v[a][i].first); n     } n   } n } n int main() { n   int n, m; n   cin >> n >> m; n   int MAXSIZE = 0, NODE; n   for (int i = 0; i < m; i++) { n     int x, y; n     scanf( %d %d , &x, &y); n     v[x].push_back(make_pair(y, i)), v[y].push_back(make_pair(x, i)); n   } n   dfs(1); n   memset(visited, false, sizeof(visited)); n   for (int i = 0; i < n; i++) { n     SIZE = 0; n     if (!visited[i + 1]) dfs2(i + 1); n     if (SIZE > MAXSIZE) MAXSIZE = SIZE, NODE = i + 1; n   } n   solve(NODE); n   cout << MAXSIZE << endl; n   for (int i = 0; i < m; i++) n     cout << EDGE[i].first <<     << EDGE[i].second << endl; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n const int INF = 0x3f3f3f3f; n const ll LINF = 0x3f3f3f3f3f3f3f3fLL; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout << setprecision(10) << fixed; n   int n, k; n   cin >> n >> k; n   vector<double> p(n); n   for (auto& x : p) cin >> x; n   int null = (int)count(begin(p), end(p), 0.0); n   if (n - null < k) { n     for (int u = 0; u < n; ++u) { n       double res = p[u] == 0.0 ? 0.0 : 1.0; n       cout << res <<   n ; n     } n     exit(0); n   } n   vector<int> popcount(1 << n, 0); n   vector<double> sum(1 << n, 0); n   for (int u = 0; u < n; ++u) sum[1 << u] = p[u]; n   for (int mask = 1; mask < (1 << n); ++mask) { n     int lsb = mask & -mask; n     popcount[mask] = 1 + popcount[mask ^ lsb]; n     if (mask != lsb) sum[mask] = sum[lsb] + sum[mask ^ lsb]; n   } n   vector<double> dp(1 << n, 0); n   dp[0] = 1; n   for (int mask = 0; mask < (1 << n); ++mask) { n     for (int u = 0; u < n; ++u) { n       if ((mask >> u & 1) || p[u] == 0) continue; n       int newmask = mask | (1 << u); n       dp[newmask] += dp[mask] * p[u] / (1 - sum[mask]); n     } n   } n   for (int u = 0; u < n; ++u) { n     double res = 0; n     for (int mask = 0; mask < (1 << n); ++mask) { n       if (popcount[mask] == k && (mask >> u & 1)) res += dp[mask]; n     } n     cout << res <<   n ; n   } n   exit(0); n } n


#include <bits/stdc++.h> n using namespace std; n int dis[305][305], lca[305], val[305]; n int main() { n   int n, x; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &x); n     lca[x] = i; n     val[i] = x; n   } n   char s[305]; n   memset(dis, 0, sizeof dis); n   for (int i = 1; i <= n; i++) { n     scanf( %s , s + 1); n     for (int j = 1; j <= n; j++) n       if (s[j] ==  1 ) dis[i][j] = dis[j][i] = 1; n   } n   for (int k = 1; k <= n; k++) n     for (int i = 1; i <= n; i++) n       for (int j = 1; j <= n; j++) n         if (dis[i][j] == 0) dis[i][j] = dis[i][k] & dis[k][j]; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) { n       if (dis[i][lca[j]] == 0) continue; n       val[lca[j]] = val[i]; n       lca[val[i]] = lca[j]; n       val[i] = j; n       lca[j] = i; n     } n   for (int i = 1; i <= n; i++) printf( %d%s , val[i], i == n ?   n  :    ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e3 + 7; n long long read() { n   long long res = 0, f = 1; n   char c = getchar(); n   while (!isdigit(c) && c !=  - ) c = getchar(); n   if (c ==  - ) f = -1, c = getchar(); n   while (isdigit(c)) res = (res << 1) + (res << 3) + c - 48, c = getchar(); n   return res * f; n } n long long ans; n int n, m; n long long f[maxn][maxn], g[maxn][maxn]; n int main() { n   n = read(), m = read(); n   for (int i = 2; i <= m; i++) f[1][i] = 1; n   for (int i = 2; i <= n; i++) n     for (int j = 2; j <= m; j++) n       f[i][j] = (f[i - 1][j] + 2 * f[i][j - 1] - f[i][j - 2] + 1000000007) % n                 1000000007; n   for (int i = 1; i <= n; i++) n     for (int j = 2; j <= m; j++) g[i][j] = (g[i - 1][j] + f[i][j]) % 1000000007; n   for (int i = 1; i <= n; i++) n     for (int j = 2; j <= m; j++) n       (ans += f[i][j] * g[n - i + 1][j] % 1000000007 * (m - j + 1) % n               1000000007) %= 1000000007; n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n = 0, l = 0, r = 0, x = 0, c[16]; n   cin >> n >> l >> r >> x; n   for (int i = 0; i < n; i++) cin >> c[i]; n   sort(c, c + n); n   int total = 0, cnt = 0, ret = 0, easy = 0, hard = 0; n   for (int i = 0; i < (1 << n); i++) { n     total = 0, cnt = 0; n     for (int j = 0; j < n; j++) { n       if ((1 << j) & i) { n         if (cnt == 0) { n           easy = c[j]; n         } n         total += c[j]; n         cnt++; n         hard = c[j]; n       } n     } n     if (cnt > 1 && total >= l && total <= r && hard - easy >= x) { n       ret++; n     } n   } n   cout << ret; n   return 0; n } n


#include <bits/stdc++.h> n char putnb[20]; n using namespace std; n const long long mod = 1e9 + 7; n const long long mod2 = 1999999973; n const int infs = 1e8; n const long long inf = 1e18; n const int N = 200000; n long long A[N + 5]; n long long dp[N + 5][2]; n bool vis[N + 5][2]; n bool solved[N + 5][2]; n long long n; n long long solve(long long pos, int state) { n   if (pos <= 0 || pos > n) return 0; n   if (vis[pos][state] && !solved[pos][state]) return -1; n   vis[pos][state] = true; n   if (pos == 1) { n     if (state) return 0 - inf; n     return -1; n   } n   if (solved[pos][state]) return dp[pos][state]; n   if (!state) { n     long long v1 = solve(pos + A[pos], 1); n     if (v1 == -1) n       dp[pos][state] = -1; n     else n       dp[pos][state] = v1 + A[pos]; n   } else { n     long long v2 = solve(pos - A[pos], 0); n     if (v2 == -1) n       dp[pos][state] = -1; n     else n       dp[pos][state] = v2 + A[pos]; n   } n   solved[pos][state] = true; n   return dp[pos][state]; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   ; n   cin >> n; n   for (int i = 2; i <= n; ++i) cin >> A[i]; n   for (int i = 1; i <= n - 1; ++i) { n     long long res = solve(1 + i, 1); n     if (res == -1) n       cout << res <<   n ; n     else if (res >= 0) n       cout << res + i <<   n ; n     else if (res < 0) n       cout << (res + inf + 2 * i) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int X = 1e5 + 9; n const long long inf = (1ll << 60); n int n, sum, a[109], b[109], c[109]; n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     b[a[i]]++; n   } n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     c[a[i]]++; n   } n   for (int i = 1; i <= 5; i++) { n     if ((b[i] + c[i]) % 2 == 1) { n       puts( -1 ); n       return 0; n     } n   } n   for (int i = 1; i <= 5; i++) { n     sum += (abs(c[i] - b[i]) / 2); n   } n   cout << sum / 2 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int MaxN = 200010, MaxB = 500, MaxA = 1000010, mod = 1000000007; n struct Query { n   int l, r, id; n   bool operator<(const Query &a) const { return r < a.r; } n } q[MaxN]; n int n, m, a[MaxN]; n vector<pair<int, int> > f[MaxN]; n int last[MaxA]; n int prime[MaxA], ptot, minp[MaxA]; n int inv[MaxA]; n bool notp[MaxA]; n void init(int n) { n   for (int i = 2; i <= n; ++i) { n     if (!notp[i]) { n       prime[++ptot] = i; n       minp[i] = i; n     } n     for (int j = 1; j <= ptot && prime[j] * i <= n; ++j) { n       notp[prime[j] * i] = 1; n       minp[prime[j] * i] = prime[j]; n       if (i % prime[j] == 0) break; n     } n   } n   inv[1] = 1; n   for (int i = 2; i <= n; ++i) n     inv[i] = mod - 1ll * (mod / i) * inv[mod % i] % mod; n } n int ans[MaxN]; n int c[MaxN]; n void modify(int x, int v) { n   for (; x <= n; x += (x & -x)) c[x] = 1ll * c[x] * v % mod; n } n int query(int x) { n   int ret = 1; n   for (; x; x -= (x & -x)) ret = 1ll * ret * c[x] % mod; n   return ret; n } n int main() { n   scanf( %d , &n); n   int mx = 0; n   for (int i = 1; i <= n; ++i) { n     scanf( %d , a + i); n     mx = max(mx, a[i]); n   } n   init(mx); n   for (int i = 1; i <= n; ++i) { n     int x = a[i]; n     while (x != 1) { n       int prod = 1, t = minp[x]; n       while (x % t == 0) { n         x /= t; n         prod *= t; n       } n       f[i].push_back(make_pair(t, prod)); n     } n   } n   scanf( %d , &m); n   for (int i = 1; i <= m; ++i) { n     scanf( %d%d , &q[i].l, &q[i].r); n     q[i].id = i; n   } n   sort(q + 1, q + m + 1); n   for (int i = 0; i <= n; ++i) c[i] = 1; n   int node = 0; n   for (int i = 1; i <= n; ++i) { n     for (auto t : f[i]) { n       modify(1, t.second); n       modify(last[t.first] + 1, 1ll * (t.first - 1) * inv[t.first] % mod); n       modify(i + 1, inv[t.second / t.first * (t.first - 1)]); n       last[t.first] = i; n     } n     while (node < m && q[node + 1].r == i) { n       ++node; n       ans[q[node].id] = query(q[node].l); n     } n   } n   for (int i = 1; i <= m; ++i) printf( %d n , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   long long x, y; n   cin >> t; n   for (int i = 0; i < t; i++) { n     cin >> x >> y; n     cout << x <<     << 2 * x << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n inline void splay(int &v) { n   v = 0; n   char c = 0; n   int p = 1; n   while (c <  0  || c >  9 ) { n     if (c ==  - ) p = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     v = (v << 3) + (v << 1) + c -  0 ; n     c = getchar(); n   } n   v *= p; n } n const int N = 800010; n int v[N], a[N], n, m, mi; n vector<int> vv[N]; n void build(int l, int r, int w) { n   if (l == r) return v[w] = a[l], void(); n   int mid = l + r >> 1; n   build(l, mid, w << 1); n   build(mid + 1, r, w << 1 | 1); n   v[w] = min(v[w << 1], v[w << 1 | 1]); n } n void get(int l, int r, int w, int L, int R) { n   if (L <= l && r <= R) { n     mi = min(mi, v[w]); n     return; n   } n   int mid = l + r >> 1; n   if (L <= mid) get(l, mid, w << 1, L, R); n   if (R > mid) get(mid + 1, r, w << 1 | 1, L, R); n } n int getmi(int l, int r) { n   mi = 1 << 30; n   if (l > r) return mi; n   get(1, n, 1, l, r); n   return mi; n } n int now = -1; n int main() { n   splay(n), splay(m); n   a[0] = 1; n   for (int i = 1; i <= n; i++) { n     splay(a[i]); n     if (!a[i]) { n       a[i] = a[i - 1]; n       now = i; n     } n     vv[a[i]].push_back(i); n   } n   build(1, n, 1); n   for (int i = 1; i <= m; i++) { n     for (int j = 0; j < (int)(vv[i].size()) - 1; j++) { n       int x = getmi(vv[i][j] + 1, vv[i][j + 1] - 1); n       if (x < i) puts( NO ), exit(0); n     } n   } n   bool flag = 0; n   for (int i = 1; i <= n; i++) { n     if (a[i] == m) flag = 1; n   } n   if (!flag) { n     if (now != -1) n       a[now] = m; n     else n       puts( NO ), exit(0); n   } n   puts( YES ); n   for (int i = 1; i <= n; i++) { n     printf( %d%c , a[i],    n [i == n]); n   } n } n


#include <bits/stdc++.h> n using LL = long long; n const int maxn = 1011000; n const LL mod = 1e9 + 7; n int seed = 29, n; n char str[maxn]; n LL base[maxn], Hash[maxn]; n int ans[maxn]; n void init() { n   memset(base, 0, sizeof(base)); n   base[0] = 1; n   for (int i = 1; i < maxn; i++) { n     base[i] = base[i - 1] * seed; n     base[i] %= mod; n   } n } n void init_hash() { n   memset(Hash, 0, sizeof(Hash)); n   for (int i = n - 1; i >= 0; i--) { n     Hash[i] = Hash[i + 1] * seed + str[i] -  a  + 1; n     Hash[i] %= mod; n   } n } n LL get_hash(int i, int len) { n   auto ans = Hash[i] - Hash[i + len] * base[len]; n   ans %= mod; n   if (ans < 0) ans += mod; n   return ans; n } n int main() { n   init(); n   while (std::cin >> n) { n     std::cin >> str; n     init_hash(); n     memset(ans, -1, sizeof(ans)); n     int cur = -1; n     for (int i = (n - 1) / 2; i >= 0; i--) { n       cur += 2; n       if (2 * i == n - 1) { n         cur = -1; n       } n       while (cur != -1 && get_hash(i, cur) != get_hash(n - i - cur, cur)) n         cur -= 2; n       ans[i] = cur; n     } n     for (int i = 0; i <= (n - 1) / 2; i++) { n       std::cout << ans[i] <<    ; n     } n     std::cout << std::endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long read() { n   long long sum = 0, ff = 1; n   char ch = getchar(); n   while (!isdigit(ch)) { n     if (ch ==  - ) ff = -1; n     ch = getchar(); n   } n   while (isdigit(ch)) sum = sum * 10 + (ch ^ 48), ch = getchar(); n   return sum * ff; n } n const long long N = 5005; n long long A, B, n, lcs[N][N], f[N]; n char ch[N]; n signed main() { n   n = read(); n   A = read(); n   B = read(); n   scanf( %s , ch + 1); n   for (long long i = 1; i <= n; i++) n     for (long long j = 1; j <= n; j++) n       if (ch[i] == ch[j]) n         lcs[i][j] = lcs[i - 1][j - 1] + 1; n       else n         lcs[i][j] = 0; n   for (long long i = 1; i <= n; i++) { n     f[i] = f[i - 1] + A; n     for (long long j = 1; j < i; j++) n       if (lcs[i][j] <= i - j) f[i] = min(f[i], f[i - lcs[i][j]] + B); n   } n   printf( %lld n , f[n]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long gi() { n   char cc = getchar(); n   long long cn = 0, flus = 1; n   while (cc <  0  || cc >  9 ) { n     if (cc ==  - ) flus = -flus; n     cc = getchar(); n   } n   while (cc >=  0  && cc <=  9 ) cn = cn * 10 + cc -  0 , cc = getchar(); n   return cn * flus; n } n const long long P = 998244353; n long long fpow(long long x, long long k) { n   long long ans = 1, base = x; n   while (k) { n     if (k & 1) ans = 1ll * ans * base % P; n     base = 1ll * base * base % P, k >>= 1; n   } n   return ans; n } n const long long N = 3e5 + 5; n long long m, n, a[N], fac[N], inv[N]; n long long C(long long x, long long y) { n   return fac[x] * inv[y] % P * inv[x - y] % P; n } n signed main() { n   m = gi(), n = m * 2; n   for (register long long i = (1); i <= (n); ++i) a[i] = gi(); n   sort(a + 1, a + n + 1); n   fac[0] = inv[0] = 1; n   for (register long long i = (1); i <= (n); ++i) fac[i] = fac[i - 1] * i % P; n   for (register long long i = (1); i <= (n); ++i) inv[i] = fpow(fac[i], P - 2); n   long long ans = 0; n   for (register long long i = (1); i <= (n); ++i) { n     long long z = a[i] * C(n - 1, m - 1); n     if (i <= m) n       ans = (ans - z + P) % P; n     else n       ans = (ans + z + P) % P; n   } n   cout << ans * 2 % P << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 10; n const int M = (1e5 + 10 + N) * 2; n const int inf = 0x3f3f3f3f; n int n, m, S, T; n struct node { n   int u, v, w; n } Edge[N]; n bool cmp(node &a, node &b) { return a.w < b.w; } n int h[N], tot, e[M], f[M], ne[M]; n int d[N], cur[N]; n void add(int u, int v, int w) { n   e[tot] = v, f[tot] = w, ne[tot] = h[u], h[u] = tot++; n   e[tot] = u, f[tot] = 0, ne[tot] = h[v], h[v] = tot++; n } n bool bfs() { n   queue<int> q; n   q.push(S); n   memset(d, -1, sizeof d); n   d[S] = 0; n   cur[S] = h[S]; n   while (!q.empty()) { n     int u = q.front(); n     q.pop(); n     for (int i = h[u]; ~i; i = ne[i]) { n       int v = e[i]; n       if (d[v] == -1 && f[i]) { n         d[v] = d[u] + 1; n         cur[v] = h[v]; n         if (v == T) return true; n         q.push(v); n       } n     } n   } n   return false; n } n int dfs(int u, int limit) { n   if (u == T) return limit; n   int flow = 0; n   for (int i = cur[u]; ~i && flow < limit; i = ne[i]) { n     cur[u] = i; n     int v = e[i]; n     if (d[v] == d[u] + 1 && f[i]) { n       int t = dfs(v, min(f[i], limit - flow)); n       flow += t; n       f[i] -= t; n       f[i ^ 1] += t; n     } n   } n   return flow; n } n int dinic() { n   int flow, res = 0; n   while (bfs()) { n     while (flow = dfs(S, inf)) res += flow; n   } n   return res; n } n bool check(int mid) { n   S = 0, T = 2 * n + 1; n   for (int i = 0; i <= T; i++) h[i] = -1; n   tot = 0; n   for (int i = 1; i <= n; i++) add(S, i, 1); n   for (int j = 1; j <= n; j++) add(n + j, T, 1); n   for (int i = 1; i <= m; i++) { n     if (Edge[i].w > mid) break; n     add(Edge[i].u, n + Edge[i].v, Edge[i].w); n   } n   return dinic() == n; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= m; i++) n     scanf( %d%d%d , &Edge[i].u, &Edge[i].v, &Edge[i].w); n   sort(Edge + 1, Edge + 1 + m, cmp); n   int l = 1, r = Edge[m].w; n   int ans = -1; n   while (l <= r) { n     int mid = (l + r) / 2; n     if (check(mid)) { n       r = mid - 1; n       ans = mid; n     } else n       l = mid + 1; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int NMAX = 1000 + 5; n int n; n int mat[NMAX][NMAX]; n vector<pair<int, int> > sol; n void DEI(int l, int r, int insert_type) { n   if (r - l + 1 < 3) return; n   int k; n   for (k = l + 1; k <= r - 1; ++k) n     if (mat[l][k] && mat[k][r]) break; n   if (insert_type == 0) sol.push_back(make_pair(l, r)); n   mat[l][r] = mat[r][l] = false; n   mat[1][k] = mat[k][1] = true; n   DEI(l, k, insert_type); n   DEI(k, r, insert_type); n   if (insert_type == 1) sol.push_back(make_pair(1, k)); n } n void solve(int type) { n   memset(mat, 0, sizeof mat); n   vector<int> graph1; n   for (int i = 1; i < n - 2; ++i) { n     int a, b; n     cin >> a >> b; n     mat[a][b] = mat[b][a] = true; n   } n   for (int i = 1; i <= n; ++i) mat[i][i % n + 1] = mat[i % n + 1][i] = true; n   for (int i = 1; i <= n; ++i) n     if (mat[1][i]) graph1.push_back(i); n   for (int i = 1; i < graph1.size(); ++i) DEI(graph1[i - 1], graph1[i], type); n } n int main() { n   cin >> n; n   solve(0); n   solve(1); n   cout << sol.size() <<   n ; n   for (auto it : sol) cout << it.first <<     << it.second <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename C> n void ma(C& a, C b) { n   if (a < b) a = b; n } n template <typename C> n void mi(C& a, C b) { n   if (a > b) a = b; n } n int cas, n, l[251], r[251], w[251], roz[251]; n void licz_roz(int nr) { n   if (w[nr] == -1) { n     licz_roz(l[nr]); n     licz_roz(r[nr]); n     if (roz[r[nr]] & 1) swap(l[nr], r[nr]); n     roz[nr] = 1 + roz[l[nr]] + roz[r[nr]]; n   } else n     roz[nr] = 0; n } n int dp[251][2][3]; n int ak[251][2][3]; n inline int mima(int a, int b, bool jak) { return ((a > b) ^ jak) ? a : b; } n inline int mima3(int a, int b, int c, bool jak) { n   return mima(mima(a, b, jak), c, jak); n } n int dfs(int nr, bool kto, int pas) { n   if (w[nr] != -1) return w[nr]; n   if (roz[nr] == 1) return mima(w[l[nr]], w[r[nr]], kto ^ (pas == 2)); n   int& res = dp[nr][kto][pas]; n   if (ak[nr][kto][pas] == cas) return res; n   ak[nr][kto][pas] = cas; n   if (pas) { n     if (roz[nr] & 1) { n       res = dfs(nr, !kto, 0); n       if (roz[l[nr]] & 1) { n         int pom = n             mima(dfs(l[nr], kto, 0), n                  dfs(r[nr], !kto, roz[l[nr]] == 1 && pas == 1 ? 2 : 1), !kto); n         res = mima(res, pom, kto); n         pom = mima(dfs(r[nr], kto, 0), n                    dfs(l[nr], !kto, roz[r[nr]] == 1 && pas == 1 ? 2 : 1), !kto); n         res = mima(res, pom, kto); n       } else { n         if (roz[r[nr]]) { n           int pom = n               mima(dfs(l[nr], !kto, 0), n                    dfs(r[nr], kto, roz[l[nr]] == 0 && pas == 1 ? 2 : 1), !kto); n           res = mima(res, pom, kto); n         } n         if (roz[l[nr]]) { n           int pom = n               mima(dfs(r[nr], !kto, 0), n                    dfs(l[nr], kto, roz[r[nr]] == 0 && pas == 1 ? 2 : 1), !kto); n           res = mima(res, pom, kto); n         } n       } n     } else { n       res = mima(dfs(r[nr], kto, 0), n                  dfs(l[nr], kto, roz[r[nr]] == 0 && pas == 1 ? 2 : 1), kto); n     } n   } else { n     if (roz[nr] & 1) { n       if (roz[l[nr]] & 1) n         res = mima(dfs(l[nr], kto, 1), dfs(r[nr], kto, 1), kto); n       else n         res = mima(dfs(l[nr], kto, 0), dfs(r[nr], kto, 0), kto); n     } else { n       int pom = mima(dfs(l[nr], kto, 0), dfs(r[nr], !kto, 0), !kto); n       int pom2 = mima(dfs(l[nr], !kto, 1), dfs(r[nr], kto, 1), !kto); n       res = mima(pom, pom2, kto); n     } n   } n   return res; n } n void test() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d , &w[i]); n     if (w[i] == -1) { n       scanf( %d , &l[i]); n       scanf( %d , &r[i]); n     } n   } n   licz_roz(0); n   printf( %d n , dfs(0, 0, 0)); n } n int main() { n   int _; n   scanf( %d , &_); n   for (cas = 1; cas <= _; cas++) test(); n } n


#include <bits/stdc++.h> n using namespace std; n int cost[15][2][2]; n string a[15]; n int dp[15][2], topFloor; n int memoize(int floor, int dir) { n   if (floor > topFloor) { n     return 0; n   } n   if (floor == topFloor) { n     return cost[floor][dir][dir]; n   } n   if (dp[floor][dir] != -1) return dp[floor][dir]; n   int ans = 2000000000; n   ans = min(memoize(floor + 1, dir ^ 1) + 1 + cost[floor][dir][dir ^ 1], n             memoize(floor + 1, dir) + 1 + cost[floor][dir][dir]); n   return dp[floor][dir] = ans; n } n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   reverse(a, a + n); n   m += 2; n   topFloor = -1; n   for (int i = 0; i < n; i++) { n     bool present = false; n     int mini = m + 5, maxi = -1; n     for (int j = 0; j < m; j++) { n       if (a[i][j] ==  1 ) { n         present = true; n         mini = min(mini, j); n         maxi = max(maxi, j); n       } n     } n     if (present) { n       topFloor = i; n       cost[i][0][0] = maxi * 2; n       cost[i][1][1] = (m - 1 - mini) * 2; n     } n     cost[i][0][1] = m - 1; n     cost[i][1][0] = m - 1; n   } n   if (topFloor != -1) { n     cost[topFloor][0][0] /= 2; n     cost[topFloor][1][1] /= 2; n   } n   memset(dp, -1, sizeof(dp)); n   int ans = memoize(0, 0); n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n set<string> S; n string rotate1(string s) { n   string s1 = s; n   s1[1] = s[2]; n   s1[2] = s[4]; n   s1[4] = s[5]; n   s1[5] = s[1]; n   return s1; n } n string rotate2(string s) { n   string s1 = s; n   s1[0] = s[1]; n   s1[1] = s[3]; n   s1[3] = s[4]; n   s1[4] = s[0]; n   return s1; n } n set<pair<string, bool> > poszlo; n void go(string s, bool sel) { n   if (!poszlo.insert(make_pair(s, sel)).second) return; n   S.insert(s); n   if (sel) { n     s = rotate2(s); n     go(s, 0); n     s = rotate2(s); n     go(s, 0); n     s = rotate2(s); n     go(s, 0); n   } else { n     s = rotate1(s); n     go(s, 1); n     s = rotate1(s); n     go(s, 1); n     s = rotate1(s); n     go(s, 1); n   } n } n int main() { n   string s; n   cin >> s; n   sort(s.begin(), s.end()); n   int res = 0; n   do { n     if (S.insert(s).second) res++; n     poszlo.clear(); n     go(s, 0); n     go(s, 1); n   } while (next_permutation(s.begin(), s.end())); n   printf( %d n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int INF = 0x3f3f3f3f; n int INF2 = 0x7fffffff; n long long LNF = 0x3f3f3f3f3f3f3f3f; n long long LNF2 = 0x7fffffffffffffff; n long long n, k, M, D; n long long ans; n int main() { n   scanf( %I64d%I64d%I64d%I64d , &n, &k, &M, &D); n   for (int i = (1); i <= (D); ++i) { n     if (i * k - k + 1 > n) break; n     long long c = n / (i * k - k + 1); n     c = min(c, M); n     ans = max(ans, c * i); n   } n   printf( %I64d , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, r; n   cin >> n >> m >> r; n   int min1 = 1001; n   int max1 = 0; n   int a, b; n   for (int i = 0; i < n; i++) { n     cin >> a; n     min1 = min(a, min1); n   } n   for (int i = 0; i < m; i++) { n     cin >> b; n     max1 = max(b, max1); n   } n   int d = r / min1; n   if (min1 > max1) { n     cout << r << endl; n     return 0; n   } n   int cost = (d * max1 + r % min1); n   if (cost > r) { n     cout << cost << endl; n   } else { n     cout << r << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   float square = sqrt(n); n   float csquare = ceil(square); n   if ((csquare == square) || (csquare - square <= 0.5)) { n     cout << 4 * csquare; n   } else if (csquare - square > 0.5) { n     cout << 2 * (csquare + (csquare - 1)); n   } n   return 0; n } n


#include <bits/stdc++.h> n const int Mod = (int)1e9 + 7; n const int MX = 1073741822; n const long long MXLL = 4611686018427387903; n const int Sz = 2e5 + 1; n using namespace std; n inline void Read_rap() { n   ios_base ::sync_with_stdio(0); n   cin.tie(0); n } n int n; n int a[Sz]; n long long dp[Sz][20][2], lca[Sz][20][2], sum[Sz][20][2]; n vector<int> g[Sz]; n void dfs(int v, int pr) { n   for (int i = 0; i < 20; i++) dp[v][i][((a[v] & (1 << i)) > 0)]++; n   for (auto to : g[v]) { n     if (to != pr) { n       dfs(to, v); n       for (int i = 0; i < 20; i++) { n         for (int j = 0; j < 2; j++) { n           bool bit = (a[v] & (1 << i)) > 0; n           bool b = (j ^ bit); n           dp[v][i][b] += dp[to][i][j]; n         } n       } n     } n   } n   for (auto to : g[v]) { n     if (to != pr) { n       for (int i = 0; i < 20; i++) { n         for (int j = 0; j < 2; j++) { n           for (int k = 0; k < 2; k++) { n             bool bit = (a[v] & (1 << i)) > 0; n             bool b = (j ^ k ^ bit); n             lca[v][i][b] += sum[v][i][j] * dp[to][i][k]; n           } n         } n         for (int j = 0; j < 2; j++) sum[v][i][j] = sum[v][i][j] + dp[to][i][j]; n       } n     } n   } n } n int main() { n   Read_rap(); n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   for (int i = 1; i < n; i++) { n     int x, y; n     cin >> x >> y; n     g[x].push_back(y); n     g[y].push_back(x); n   } n   dfs(1, 1); n   long long ans = 0; n   for (int i = 0; i < 20; i++) n     for (int v = 1; v <= n; v++) n       ans = (ans + (dp[v][i][1] + lca[v][i][1]) * (1ll << i)); n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[55][55]; n int main() { n   int cs; n   scanf( %d , &cs); n   while (cs--) { n     int n, m; n     scanf( %d %d , &n, &m); n     int col = 0, row = 0; n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < m; j++) { n         scanf( %d , &a[i][j]); n       } n     } n     for (int i = 0; i < n; i++) { n       bool flag = true; n       for (int j = 0; j < m; j++) { n         if (a[i][j] & 1) { n           flag = false; n           break; n         } n       } n       if (flag) row++; n     } n     for (int i = 0; i < m; i++) { n       bool flag = true; n       for (int j = 0; j < n; j++) { n         if (a[j][i] & 1) { n           flag = false; n           break; n         } n       } n       if (flag) col++; n     } n     if (min(col, row) % 2) n       puts( Ashish ); n     else n       puts( Vivek ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[500000], o, ans; n int main() { n   cin >> n; n   ans = 1; n   for (int i = 2; i <= n + 1; i++) { n     if (a[i] == 0) { n       a[i] = 1; n       o = 1; n       for (int j = i + i; j <= n + 1; j += i) { n         o = 2; n         ans = max(ans, o); n         a[j] = o; n       } n     } n   } n   cout << ans << endl; n   for (int i = 2; i <= n + 1; i++) cout << a[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 5; n char s[200]; n int main() { n   int T; n   scanf( %d , &T); n   for (int t = 1; t <= T; t++) { n     bool num = 0, xx = 0, dx = 0; n     vector<int> res; n     scanf( %s , s + 1); n     int len = strlen(s + 1); n     for (int i = 1; i <= len; i++) { n       if (isdigit(s[i])) { n         if (!num) n           num = 1; n         else n           res.push_back(i); n       } n       if (isalpha(s[i])) { n         if (s[i] == tolower(s[i])) { n           if (!xx) n             xx = 1; n           else n             res.push_back(i); n         } else { n           if (!dx) n             dx = 1; n           else n             res.push_back(i); n         } n       } n     } n     int cnt = 1; n     if (num == 0) s[res.back()] =  1 , res.pop_back(); n     if (xx == 0) s[res.back()] =  a , res.pop_back(); n     if (dx == 0) s[res.back()] =  A , res.pop_back(); n     printf( %s n , s + 1); n   } n } n


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


#include <bits/stdc++.h> n void swap(int *, int *); n int main() { n   int i, j, k; n   int n; n   scanf( %d , &n); n   int arr[n]; n   for (i = 0; i < n; i++) { n     scanf( %d , &arr[i]); n   } n   int limit = n / 2; n   for (i = 0; i < limit; i++) { n     if (i % 2 == 0) { n       swap(&arr[i], &arr[n - i - 1]); n     } n   } n   for (i = 0; i < n; i++) { n     printf( %d  , arr[i]); n   } n } n void swap(int *a, int *b) { n   int t; n   t = *a; n   *a = *b; n   *b = t; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   scanf( %d %d , &n, &m); n   int a, b, a_min, b_min; n   scanf( %d %d , &a_min, &b_min); n   for (int i = 1; i < n; i++) { n     scanf( %d %d , &a, &b); n     if (a * b_min < a_min * b) { n       a_min = a; n       b_min = b; n     } n   } n   double c = m * a_min / (b_min * 1.0f); n   printf( %f n , c); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5; n struct P { n   long long x, y; n   bool a; n   P() {} n   P(long long _x, long long _y, bool _a) : x(_x), y(_y), a(_a) {} n   long long operator^(const P &t) const { return x * t.y - y * t.x; } n   bool operator<(const P &t) const { n     if (y == t.y) return x < t.x; n     return y < t.y; n   } n   P operator-(const P &t) const { return P(x - t.x, y - t.y, 0); } n } p[N]; n int n, m; n bool flag[N]; n int st[N], tops; n void granhamScan() { n   st[0] = 0; n   tops = 1; n   for (int i = 1; i < m; ++i) { n     while (tops >= 2 && ((p[st[tops - 1]] - p[st[tops - 2]]) ^ n                          (p[i] - p[st[tops - 1]])) < 0) { n       flag[st[tops - 1]] = false; n       tops--; n     } n     st[tops++] = i; n     flag[i] = true; n   } n   for (int i = m - 1; i >= 0; --i) { n     if (flag[i]) continue; n     while (tops >= 2 && ((p[st[tops - 1]] - p[st[tops - 2]]) ^ n                          (p[i] - p[st[tops - 1]])) < 0) { n       flag[st[tops - 1]] = false; n       tops--; n     } n     st[tops++] = i; n     flag[i] = true; n   } n } n bool check() { n   for (int i = 0; i < tops; ++i) { n     if (!p[st[i]].a) return 0; n   } n   return 1; n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) { n     int x, y; n     scanf( %d%d , &x, &y); n     p[i] = P(x, y, 1); n   } n   scanf( %d , &m); n   m += n; n   for (int i = n; i < m; ++i) { n     int x, y; n     scanf( %d%d , &x, &y); n     p[i] = P(x, y, 0); n   } n   sort(p, p + m); n   p[m] = p[0]; n   granhamScan(); n   if (check()) n     printf( YES n ); n   else n     printf( NO n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const long double EPS = 1e-6; n const int N = 200020, MOD = 998244353; n int n, k; n long long a[N + 20], fact[N + 20], inv[N + 20]; n long long qpow(long long a, long long b, long long p = MOD) { n   long long ans = 1; n   for (; b; b >>= 1) { n     if (b & 1) ans = ans * a % p; n     a = a * a % p; n   } n   return ans % p; n } n void prework() { n   fact[0] = 1; n   for (int i = 1; i <= N; i++) fact[i] = fact[i - 1] * i % MOD; n   inv[N - 1] = qpow(fact[N - 1], MOD - 2) % MOD; n   for (int i = N - 2; ~i; i--) inv[i] = inv[i + 1] * (i + 1) % MOD; n } n long long C(int n, int m) { n   if (n - m < 0 || m < 0) return 0; n   return fact[n] % MOD * inv[m] % MOD * inv[n - m] % MOD % MOD; n } n int main() { n   prework(); n   cin >> n >> k; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   long long ans = 0; n   int c = 0, m = 0; n   a[n + 1] = a[1]; n   for (int i = 1; i <= n; i++) n     if (a[i] != a[i + 1]) m++; n   for (int i = 0; i <= m / 2; i++) { n     ans += C(m, i) % MOD * C(m - i, i) % MOD * qpow(k - 2, m - 2 * i) % MOD; n     ans %= MOD; n   } n   ans = (qpow(k, m) - ans + MOD) % MOD * qpow(2, MOD - 2) % MOD; n   cout << ans % MOD * qpow(k, n - m) % MOD << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n vector<int> A; n vector<pair<int, int> > M; n vector<int> PP; n int turn; n set<int> Go; n int ans(int x) { n   cout << x + 1 << endl; n   if (Go.empty()) exit(0); n   cin >> x; n   return x - 1; n } n signed main() { n   ; n   cin >> n >> m; n   A.resize(n << 1); n   for (auto &x : A) cin >> x; n   M.resize(m); n   for (auto &x : M) cin >> x.first >> x.second, x.first--, x.second--; n   PP.assign(n << 1, -1); n   for (auto x : M) PP[x.first] = x.second, PP[x.second] = x.first; n   for (int i = 0; i < n << 1; i++) { n     Go.insert(i); n   } n   cin >> turn; n   if (turn == 1) { n     for (auto x : M) { n       if (A[x.first] > A[x.second]) { n         ans(x.first); n         Go.erase(x.first); n         Go.erase(x.second); n       } else { n         ans(x.second); n         Go.erase(x.first); n         Go.erase(x.second); n       } n     } n     while (Go.size()) { n       int mx = -1, r = -1; n       for (auto &x : Go) { n         if (mx < A[x]) mx = A[x], r = x; n       } n       Go.erase(r); n       Go.erase(ans(r)); n     } n   } else { n     int x; n     cin >> x; n     x--; n     Go.erase(x); n     while (PP[x] != -1) { n       Go.erase(PP[x]); n       x = ans(PP[x]); n       Go.erase(x); n     } n     for (auto x : M) { n       if (!Go.count(x.first)) continue; n       if (A[x.first] > A[x.second]) { n         ans(x.first); n         Go.erase(x.first); n         Go.erase(x.second); n       } else { n         ans(x.second); n         Go.erase(x.first); n         Go.erase(x.second); n       } n     } n     while (Go.size()) { n       int mx = -1, r = -1; n       for (auto &x : Go) { n         if (mx < A[x]) mx = A[x], r = x; n       } n       Go.erase(r); n       Go.erase(ans(r)); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int ans[500][500], st[500][500]; n vector<pair<int, int> > go[500][500]; n vector<vector<int> > cc; n vector<int> c; n int mv[1 << 20]; n int it; n void pr(int n, int k) { n   if (k == 1) { n     c.push_back(n); n     mv[(((c[4] * 16 + c[3]) * 16 + c[2]) * 16 + c[1]) * 16 + c[0]] = it; n     cc.push_back(c); n     it++; n     c.pop_back(); n     return; n   } n   for (int j = 0; j <= n; j++) { n     c.push_back(j); n     pr(n - j, k - 1); n     c.pop_back(); n   } n } n int kkk[5] = {1, 16, 256, 4096, 65536}; n void get_int(int &ans) { n   ans = 0; n   char c = getchar(); n   while (c <  0  || c >  9 ) c = getchar(); n   while (c >=  0  && c <=  9 ) { n     ans = ans * 10 + c -  0 ; n     c = getchar(); n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   pr(8, 5); n   queue<pair<int, int> > pp; n   for (int j = 0; j < cc.size(); j++) { n     vector<int> vv = cc[j]; n     int cnum = (((vv[4] * 16 + vv[3]) * 16 + vv[2]) * 16 + vv[1]) * 16 + vv[0]; n     bool ok = true; n     for (int q = 0; q < cc.size(); q++) { n       bool ok1 = true; n       for (int ii = 1; ii < 5; ii++) { n         if (cc[j][ii]) { n           ok = false; n           for (int jj = 1; jj < 5; jj++) { n             if (cc[q][jj]) { n               ok1 = false; n               cnum -= kkk[ii]; n               cnum += kkk[(ii + jj) % 5]; n               go[q][mv[cnum]].push_back(make_pair(j, q)); n               st[j][q]++; n               cnum += kkk[ii]; n               cnum -= kkk[(ii + jj) % 5]; n             } n           } n           if (ok) { n             ans[j][q] = 1; n             pp.push(make_pair(j, q)); n           } else if (ok1) { n             ans[j][q] = 2; n             pp.push(make_pair(j, q)); n           } n         } n       } n     } n   } n   while (!pp.empty()) { n     int j = pp.front().first; n     int q = pp.front().second; n     pp.pop(); n     for (auto v : go[j][q]) { n       if (ans[v.first][v.second]) continue; n       if (ans[j][q] == 2) { n         ans[v.first][v.second] = 1; n         pp.push(v); n         continue; n       } n       st[v.first][v.second]--; n       if (st[v.first][v.second] == 0) { n         ans[v.first][v.second] = 2; n         pp.push(v); n         continue; n       } n     } n   } n   int t; n   cin >> t; n   for (int i = 0; i < t; i++) { n     int f; n     cin >> f; n     int a = 0, b = 0; n     for (int j = 0; j < 8; j++) { n       int k; n       cin >> k; n       a += (1 << (k * 4)); n     } n     for (int j = 0; j < 8; j++) { n       int k; n       cin >> k; n       b += (1 << (k * 4)); n     } n     int k1 = mv[a], k2 = mv[b]; n     if (f == 0) { n       if (ans[k1][k2] == 1) cout <<  Alice  <<   n ; n       if (ans[k1][k2] == 2) cout <<  Bob  <<   n ; n       if (ans[k1][k2] == 0) cout <<  Deal  <<   n ; n     } else { n       swap(k1, k2); n       if (ans[k1][k2] == 2) cout <<  Alice  <<   n ; n       if (ans[k1][k2] == 1) cout <<  Bob  <<   n ; n       if (ans[k1][k2] == 0) cout <<  Deal  <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, i; n int a, b, c, d; n vector<long long> v; n long long ans; n void add(int a, int b) { n   ans += a - b; n   v.push_back(a + b); n } n void add_stack() { n   if (a + b >= c + d) { n     add(a, b); n     add(c, d); n     return; n   } n   if (a > d) add(a - d, d - a); n   if (b > c) add(c - b, b - c); n } n int main() { n   scanf( %d , &n); n   for (i = 1; i <= n; i++) { n     scanf( %d%d%d%d , &a, &b, &c, &d); n     add_stack(); n   } n   sort(v.begin(), v.end()); n   reverse(v.begin(), v.end()); n   long long sgn = 1; n   for (auto e : v) { n     ans += sgn * e; n     sgn *= -1; n   } n   printf( %lld , ans / 2); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 4005; n int read() { n   int x = 0, f = 1; n   char c; n   while ((c = getchar()) <  0  || c >  9 ) { n     if (c ==  - ) f = -1; n   } n   while (c >=  0  && c <=  9 ) { n     x = (x << 3) + (x << 1) + (c ^ 48); n     c = getchar(); n   } n   return x * f; n } n int n, m, t, a[M], s[M], siz[M], ch[M][2]; n long long dp[M][M]; n void upd(long long &x, long long y) { x = max(x, y); } n void dfs(int u) { n   dp[u][0] = 0; n   if (!u) return; n   int ls = ch[u][0], rs = ch[u][1]; n   dfs(ls); n   dfs(rs); n   siz[u] = 1 + siz[ls] + siz[rs]; n   for (int i = 0; i <= siz[ls]; i++) n     for (int j = 0; j <= siz[rs]; j++) n       upd(dp[u][i + j], dp[ls][i] + dp[rs][j] - 2ll * i * j * a[u]); n   for (int i = siz[u] - 1; i >= 0; i--) n     upd(dp[u][i + 1], dp[u][i] + (m - 2 * i - 1ll) * a[u]); n } n signed main() { n   n = read(); n   m = read(); n   memset(dp, -0x3f, sizeof dp); n   for (int i = 1; i <= n; i++) { n     a[i] = read(); n     while (t > 0 && a[s[t]] > a[i]) ch[i][0] = s[t--]; n     if (t) ch[s[t]][1] = i; n     s[++t] = i; n   } n   dfs(s[1]); n   printf( %lld n , dp[s[1]][m]); n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, q, l, r; n double d, s, t, x, y; n int tree[2000010 * 4], pl[2000010 * 4], pr[2000010 * 4], cnt = 1; n bool flag[2000010 * 4]; n void change(int l, int r, int id, int l1, int r1) { n   if (l >= l1 && r <= r1) { n     tree[id] = r - l + 1; n     flag[id] = 1; n     return; n   } n   if (flag[id]) return; n   int mid = (l + r) >> 1; n   if (l1 <= mid) { n     if (pl[id] == 0) pl[id] = ++cnt; n     change(l, mid, pl[id], l1, r1); n   } n   if (r1 > mid) { n     if (pr[id] == 0) pr[id] = ++cnt; n     change(mid + 1, r, pr[id], l1, r1); n   } n   tree[id] = tree[pl[id]] + tree[pr[id]]; n } n int dist(int l, int r, int l1, int r1) { n   int l2 = max(l, l1); n   int r2 = min(r, r1); n   return r2 - l2 + 1; n } n int query(int l, int r, int id, int l1, int r1) { n   if (l1 > r1) return 0; n   if (l >= l1 && r <= r1) return tree[id]; n   if (flag[id]) return dist(l, r, l1, r1); n   int mid = (l + r) >> 1; n   int res = 0; n   if (l1 <= mid && pl[id]) res += query(l, mid, pl[id], l1, r1); n   if (r1 > mid && pr[id]) res += query(mid + 1, r, pr[id], l1, r1); n   return res; n } n int main() { n   scanf( %lf%lf%lf , &d, &s, &t); n   d *= -1.0; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d%d , &l, &r); n     change(1, 0x3FFFFFFF, 1, l, r - 1); n   } n   scanf( %d , &q); n   for (int i = 1; i <= q; i++) { n     scanf( %lf%lf , &x, &y); n     double ts = (y + d) / y; n     double l1 = x + (s - x) / ts; n     double r1 = x + (t - x) / ts; n     int l2 = ceil(l1); n     int r2 = floor(r1); n     double ans = 0; n     if (l2 == r2 + 1) { n       if (query(1, 0x3FFFFFFF, 1, l2 - 1, l2 - 1)) ans = r1 - l1; n     } else { n       ans += query(1, 0x3FFFFFFF, 1, l2, r2 - 1); n       if (query(1, 0x3FFFFFFF, 1, l2 - 1, l2 - 1)) ans += l2 * 1.0 - l1; n       if (query(1, 0x3FFFFFFF, 1, r2, r2)) ans += r1 - r2 * 1.0; n     } n     ans *= ts; n     printf( %.6lf n , ans); n   } n } n


#include <bits/stdc++.h> n using namespace std; n map<string, string> m; n int main() { n   int n; n   string j, x; n   cin >> n; n   while (n--) { n     cin >> j >> x; n     if (m.count(j) == 0) n       m[x] = j; n     else { n       m[x] = m[j]; n       m.erase(j); n     } n   } n   cout << m.size() << endl; n   map<string, string>::iterator it; n   for (it = m.begin(); it != m.end(); it++) { n     cout << it->second <<     << it->first << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1000010; n int cnt, lst, fa[N * 2], len[N * 2], ch[N * 2][26]; n long long w[N * 2], ans; n int od[N * 2], rk[N * 2], l = 1; n char s[N]; n void Init() { cnt = lst = 1; } n void Insert(int c) { n   int p = lst, np = lst = ++cnt; n   len[np] = len[p] + 1; n   while (p && ch[p][c] == 0) ch[p][c] = np, p = fa[p]; n   if (!p) n     fa[np] = 1; n   else { n     int q = ch[p][c], nq; n     if (len[q] == len[p] + 1) n       fa[np] = q; n     else { n       len[nq = ++cnt] = len[p] + 1; n       fa[nq] = fa[q]; n       fa[q] = fa[np] = nq; n       memcpy(ch[nq], ch[q], sizeof(ch[q])); n       while (ch[p][c] == q) ch[p][c] = nq, p = fa[p]; n     } n   } n } n int n; n int main() { n   Init(); n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %s , s + l); n     l += strlen(s + l); n     s[l++] =  # ; n   } n   for (int i = 1, j = 0, x, p; i <= n; i++) { n     lst = 1; n     for (j += 1; s[j] !=  # ; j++) { n       Insert(s[j] -  a ); n     } n   } n   for (int i = 1, j = 0, x, p; i <= n; i++) { n     scanf( %d , &x); n     for (p = 1, j += 1; s[j] !=  # ; j++) { n       p = ch[p][s[j] -  a ]; n       w[p] += x; n     } n   } n   for (int i = 1; i <= cnt; i++) rk[len[i]] += 1; n   for (int i = 1; i <= cnt; i++) rk[i] += rk[i - 1]; n   for (int i = 1; i <= cnt; i++) od[rk[len[i]]--] = i; n   for (int i = cnt; i >= 1; i--) w[fa[od[i]]] += w[od[i]]; n   for (int i = 2; i <= cnt; i++) ans = max(ans, len[i] * w[i]); n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int t, n, m, ans; n vector<vector<int>> gh, used; n int dx[8] = {1, 1, 1, 0, 0, -1, -1, -1}; n int dy[8] = {1, 0, -1, 1, -1, 1, 0, -1}; n bool onseg(double x1, double y1, double x2, double y2, double x, double y) { n   double d1 = sqrt((x - x1) * (x - x1) + (y - y1) * (y - y1)); n   double d2 = sqrt((x - x2) * (x - x2) + (y - y2) * (y - y2)); n   double d3 = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); n   return abs(d3 - d2 - d1) < 1e-9; n } n void bfs(int y, int x) { n   if (gh[y][x] == 0) return; n   if (used[y][x]) return; n   used[y][x] = true; n   vector<pair<int, int>> todo, temp, bunch; n   int cx, cy, nx, ny; n   todo.push_back({y, x}); n   bunch.push_back({y, x}); n   while (true) { n     if (todo.empty()) break; n     for (int i = 0; i < todo.size(); i++) { n       cy = todo[i].first, cx = todo[i].second; n       for (int j = 0; j < 8; j++) { n         ny = cy + dy[j]; n         nx = cx + dx[j]; n         if (ny >= 0 && ny < n && nx >= 0 && nx < m) { n           if (used[ny][nx] == 0) { n             temp.push_back({ny, nx}); n             bunch.push_back({ny, nx}); n             used[ny][nx] = 1; n           } n         } n       } n     } n     todo = temp; n     temp.clear(); n   } n   if (bunch.size() % 4) return; n   int len = bunch.size() / 4; n   int topx, topy, btx, bty; n   topx = bunch[0].second, topy = bunch[0].first; n   btx = topx + len, bty = topy + len; n   bool sq = true; n   for (int j = 1; j < bunch.size(); j++) { n     int nx = bunch[j].second, ny = bunch[j].first; n     if (!onseg(topx, topy, btx, topy, nx, ny) && n         !onseg(topx, topy, topx, bty, nx, ny) && n         !onseg(btx, bty, btx, topy, nx, ny) && n         !onseg(btx, bty, topx, bty, nx, ny)) { n       sq = false; n       break; n     } n   } n   if (sq) { n     ans++; n     return; n   } n   sq = true; n   topx = bunch[0].second, topy = bunch[0].first; n   btx = topx, bty = topy + 2 * len; n   for (int j = 1; j < bunch.size(); j++) { n     int nx = bunch[j].second, ny = bunch[j].first; n     if (!onseg(topx, topy, topx - len, topy + len, nx, ny) && n         !onseg(topx, topy, topx + len, topy + len, nx, ny) && n         !onseg(btx, bty, topx - len, topy + len, nx, ny) && n         !onseg(btx, bty, topx + len, topy + len, nx, ny)) { n       sq = false; n       break; n     } n   } n   if (sq) { n     ans++; n   } n } n void solve() { n   cin >> n >> m; n   gh = used = vector<vector<int>>(n, vector<int>(m)); n   ans = 0; n   char ch; n   int val; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       cin >> ch; n       val = ch -  0 ; n       if (val == 0) used[i][j] = 1; n       gh[i][j] = val; n     } n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) n       if (used[i][j] == 0) bfs(i, j); n   } n   cout << ans << endl; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cin >> t; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_N = 2000; n int N; n int board[MAX_N][MAX_N]; n int cur[MAX_N][MAX_N]; n int row[MAX_N], col[MAX_N]; n char buf[MAX_N + 10]; n void pb(int arr[MAX_N][MAX_N]) { n   for (int i = 0; i < N; i++) { n     for (int j = 0; j < N; j++) { n       printf( %d , arr[i][j]); n     } n     puts(  ); n   } n } n int solve() { n   int ans = 0; n   for (int i = N - 1; i > 0; i--) { n     for (int j = 0; j < N - i; j++) { n       int y = i + j, x = j; n       cur[y][x] = row[y] ^ col[x]; n       if (cur[y][x] ^ board[y][x]) { n         ans++; n         cur[y][x] ^= 1; n         row[y] ^= 1; n         col[x] ^= 1; n       } n     } n   } n   for (int i = 0; i < N; i++) { n     if (col[i] ^ row[i]) { n       board[i][i] ^= 1; n     } n   } n   memset(col, 0, sizeof(col)); n   memset(row, 0, sizeof(row)); n   for (int j = N - 1; j > 0; j--) { n     for (int i = 0; i < N - j; i++) { n       int y = i, x = j + i; n       cur[y][x] = row[y] ^ col[x]; n       if (cur[y][x] ^ board[y][x]) { n         ans++; n         cur[y][x] ^= 1; n         row[y] ^= 1; n         col[x] ^= 1; n       } n     } n   } n   for (int i = 0; i < N; i++) { n     if (row[i] ^ col[i] ^ board[i][i]) { n       ans++; n     } n   } n   return ans; n } n int main() { n   scanf(  %d  , &N); n   for (int i = 0; i < N; i++) { n     scanf(  %s  , buf); n     for (int j = 0; j < N; j++) { n       board[i][j] = buf[j] & 1; n     } n   } n   printf( %d n , solve()); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 2e3 + 5; n const int P = 998244353; n template <typename T> n void chkmax(T &x, T y) { n   x = max(x, y); n } n template <typename T> n void chkmin(T &x, T y) { n   x = min(x, y); n } n template <typename T> n void read(T &x) { n   x = 0; n   int f = 1; n   char c = getchar(); n   for (; !isdigit(c); c = getchar()) n     if (c ==  - ) f = -f; n   for (; isdigit(c); c = getchar()) x = x * 10 + c -  0 ; n   x *= f; n } n template <typename T> n void write(T x) { n   if (x < 0) x = -x, putchar( - ); n   if (x > 9) write(x / 10); n   putchar(x % 10 +  0 ); n } n template <typename T> n void writeln(T x) { n   write(x); n   puts(  ); n } n struct BinaryIndexTree { n   int n, a[MAXN]; n   void init(int x) { n     n = x; n     memset(a, 0, sizeof(a)); n   } n   void modify(int x, int d) { n     for (int i = x; i <= n; i += i & -i) a[i] += d; n   } n   int query(int x) { n     int ans = 0; n     for (int i = x; i >= 1; i -= i & -i) ans += a[i]; n     return ans; n   } n   int query(int l, int r) { n     int ans = 0; n     for (int i = r; i >= 1; i -= i & -i) ans += a[i]; n     for (int i = l - 1; i >= 1; i -= i & -i) ans -= a[i]; n     return ans; n   } n } BIT[3]; n int n, a[MAXN][MAXN], dp[MAXN][MAXN], power[MAXN]; n void update(int &x, int y) { n   x += y; n   if (x >= P) x -= P; n } n int main() { n   read(n), dp[0][0] = 1; n   for (int i = 1; i <= n; i++) { n     dp[i][0] = 1ll * i * dp[i - 1][0] % P; n     for (int j = 1; j <= i; j++) n       dp[i][j] = (dp[i][j - 1] - dp[i - 1][j - 1] + P) % P; n   } n   power[0] = 1; n   for (int i = 1; i <= n; i++) power[i] = 1ll * power[i - 1] * dp[n][n] % P; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) read(a[i][j]); n   int ans = 0; n   BIT[0].init(n); n   for (int i = n; i >= 1; i--) { n     BIT[0].modify(a[1][i], 1); n     update(ans, 1ll * BIT[0].query(a[1][i] - 1) * dp[n - i][0] % P * n                     power[n - 1] % P); n   } n   for (int i = 2; i <= n; i++) { n     bool visa[MAXN], visb[MAXN]; n     memset(visa, 0, sizeof(visa)); n     memset(visb, 0, sizeof(visb)); n     BIT[0].init(n), BIT[1].init(n); n     for (int j = n; j >= 1; j--) { n       visb[a[i][j]] = true; n       if (!visa[a[i][j]]) n         BIT[0].modify(a[i][j], 1); n       else n         BIT[1].modify(a[i][j], 1); n       int cnt = BIT[1].query(n); n       update(ans, 1ll * BIT[0].query(a[i][j] - 1) * dp[n - j][cnt] % P * n                       power[n - i] % P); n       if (cnt) n         update(ans, 1ll * BIT[1].query(a[i][j] - 1) * dp[n - j][cnt - 1] % P * n                         power[n - i] % P); n       if (a[i][j] > a[i - 1][j] && visb[a[i - 1][j]]) { n         if (visa[a[i - 1][j]]) n           update(ans, P - 1ll * dp[n - j][cnt - 1] * power[n - i] % P); n         else n           update(ans, P - 1ll * dp[n - j][cnt] * power[n - i] % P); n       } n       if (!visb[a[i - 1][j]]) n         visa[a[i - 1][j]] = true; n       else { n         visa[a[i - 1][j]] = true; n         BIT[0].modify(a[i - 1][j], -1); n         BIT[1].modify(a[i - 1][j], 1); n       } n     } n   } n   writeln(ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 5; n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   long long n, x; n   cin >> n, x = n; n   long long cnt = 0, d = n; n   for (int i = 2; 1LL * i * i <= x; i++) { n     if (x % i == 0) { n       cnt++, d = i; n       while (x % i == 0) x /= i; n     } n     if (cnt > 2) break; n   } n   if (x > 1) cnt++; n   if (cnt <= 1) n     cout << d; n   else n     cout << 1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double EPS = 1E-8; n inline bool eq(double a, double b) { return a - b < EPS && b - a < EPS; } n inline bool ne(double a, double b) { return a + EPS < b || b + EPS < a; } n inline bool ls(double a, double b) { return a + EPS < b; } n inline bool gr(double a, double b) { return b + EPS < a; } n inline bool le(double a, double b) { return a - EPS < b; } n inline bool ge(double a, double b) { return b - EPS < a; } n int arr[1111][1111]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       scanf( %d , &arr[i][j]); n     } n   } n   int sum = 0; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       sum = (sum + arr[i][j] * arr[j][i]) & 1; n     } n   } n   int q; n   scanf( %d , &q); n   for (int i = 0; i < q; i++) { n     int ctrl; n     scanf( %d , &ctrl); n     if (ctrl == 3) n       putchar( 0  + sum); n     else { n       scanf( %d , &ctrl); n       sum = 1 - sum; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline char gc() { n   static char buf[100000], *p1 = buf, *p2 = buf; n   return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) n              ? EOF n              : *p1++; n } n inline long long read() { n   long long x = 0; n   char ch = getchar(); n   bool positive = 1; n   for (; !isdigit(ch); ch = getchar()) n     if (ch ==  - ) positive = 0; n   for (; isdigit(ch); ch = getchar()) x = x * 10 + ch -  0 ; n   return positive ? x : -x; n } n inline void write(long long a) { n   if (a < 0) { n     a = -a; n     putchar( - ); n   } n   if (a >= 10) write(a / 10); n   putchar( 0  + a % 10); n } n inline void wri(long long a) { n   write(a); n   putchar(   ); n } n inline void writeln(long long a) { n   write(a); n   puts(  ); n } n const int N = 1 << 17 | 2, mod = 998244353; n int sum, A[N], B[N], C[N], f[N]; n long long ksm(long long a, int b) { n   a %= mod; n   int ans = 1; n   for (; b; b >>= 1) { n     if (b & 1) ans = ans * a % mod; n     a = a * a % mod; n   } n   return ans; n } n void FWT(int *a, int n) { n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < (1 << n); j++) n       if (j >> i & 1) { n         int x = a[j]; n         a[j] = a[j ^ (1 << i)] >= x ? a[j ^ (1 << i)] - x n                                     : a[j ^ (1 << i)] - x + mod; n         a[j ^ (1 << i)] = a[j ^ (1 << i)] + x >= mod ? a[j ^ (1 << i)] + x - mod n                                                      : a[j ^ (1 << i)] + x; n       } n   } n } n void ba(int &a) { n   if (a > mod / 2) a = a - mod; n } n signed main() { n   int n = read(), k = read(); n   int x = read(), y = read(), z = read(); n   for (int i = 0; i < n; i++) { n     int a = read(), b = read() ^ a, c = read() ^ a; n     sum ^= a; n     A[b]++; n     B[c]++; n     C[b ^ c]++; n   } n   FWT(A, k); n   FWT(B, k); n   FWT(C, k); n   for (int i = 0; i < (1 << k); i++) { n     int t1 = n, t2 = A[i], t3 = B[i], t4 = C[i]; n     ba(t2); n     ba(t3); n     ba(t4); n     f[i] = ksm((long long)x + y + z, (t1 + t2 + t3 + t4) / 4) * n            ksm((long long)x + y - z, (t1 + t2 - t3 - t4) / 4) % mod * n            ksm((long long)x - y + z, (t1 - t2 + t3 - t4) / 4) % mod * n            ksm((long long)x - y - z, (t1 - t2 - t3 + t4) / 4) % mod; n     f[i] = (f[i] + mod) % mod; n   } n   FWT(f, k); n   long long in = ksm(1 << k, mod - 2); n   for (int i = 0; i < (1 << k); i++) f[i] = f[i] * in % mod; n   for (int i = 0; i < (1 << k); i++) { n     wri(f[i ^ sum]); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 28; n vector<int> g[N]; n int vis[2000000]; n int main() { n   int k, n; n   string ss, s; n   cin >> k >> ss; n   for (int i = 0; i < k; ++i) s += ss; n   for (int i = 0; i != s.size(); ++i) g[s[i] -  a ].push_back(i); n   cin >> n; n   vector<int>::iterator it; n   for (int i = 0; i < n; ++i) { n     cin >> k >> ss; n     it = g[ss[0] -  a ].begin(); n     g[ss[0] -  a ].erase(it + k - 1); n   } n   memset(vis, 0, sizeof(vis)); n   for (int i = 0; i < 26; ++i) n     for (int j = 0; j < g[i].size(); ++j) vis[g[i][j]] = 1; n   for (int i = 0; i != s.size(); ++i) n     if (vis[i]) cout << s[i]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e3 + 5; n int n, m; n int adj[N][N], d[N][N]; n bool check(int a, int b) { n   if (a >= 0 && a < n && b >= 0 && b < m && adj[a][b] != -1) return 1; n   return 0; n } n void bfs(int x, int y) { n   d[x][y] = 0; n   queue<pair<int, int> > q; n   q.push({x, y}); n   while (!q.empty()) { n     int dir[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; n     pair<int, int> p = q.front(); n     q.pop(); n     for (int i = 0; i < 4; i++) { n       int ii = dir[i][0] + p.first; n       int jj = dir[i][1] + p.second; n       if (check(ii, jj) && d[ii][jj] == -1) { n         q.push({ii, jj}); n         d[ii][jj] = d[p.first][p.second] + 1; n       } n     } n   } n } n int main() { n   cin >> n >> m; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) d[i][j] = -1; n   pair<int, int> e, s; n   char c; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       cin >> c; n       if (c ==  E ) { n         e.first = i; n         e.second = j; n         adj[i][j] = 0; n       } else if (c ==  S ) { n         s.first = i; n         s.second = j; n         adj[i][j] = 0; n       } else if (c ==  T ) n         adj[i][j] = -1; n       else n         adj[i][j] = (c -  0 ); n     } n   } n   bfs(e.first, e.second); n   int len = d[s.first][s.second]; n   long long int ans = 0; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) n       if (d[i][j] != -1 && d[i][j] <= len) ans += adj[i][j]; n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n inline string GetString() { n   char S[1000005]; n   scanf( %s , S); n   string ret = S; n   return ret; n } n inline char getc() { n   char c =    ; n   while (c ==     || c ==   t  || c ==   r  || c ==   n ) c = getchar(); n   return c; n } n template <typename t> n t getnum() { n   t res = 0; n   char c; n   int sign = 1; n   while (1) { n     c = getchar(); n     if (c ==     || c ==   n ) n       continue; n     else n       break; n   } n   if (c ==  + ) { n   } else if (c ==  - ) n     sign = -1; n   else n     res = c -  0 ; n   while (1) { n     c = getchar(); n     if (c >=  0  && c <=  9 ) n       res = res * 10 + c -  0 ; n     else n       break; n   } n   return res * sign; n } n int main(int argc, char** argv) { n   int n = getnum<int>(); n   int m = getnum<int>(); n   int i = getnum<int>(); n   int j = getnum<int>(); n   int a = getnum<int>(); n   int b = getnum<int>(); n   int satu = 10000000, dua = 10000000, tiga = 10000000, empat = 10000000; n   if ((i - 1) % a == 0 && (j - 1) % b == 0) { n     int x = (i - 1) / a, y = (j - 1) / b; n     if (x % 2 == y % 2) satu = max(x, y); n   } n   if ((i - 1) % a == 0 && (m - j) % b == 0) { n     int x = (i - 1) / a, y = (m - j) / b; n     if (x % 2 == y % 2) dua = max(x, y); n   } n   if ((n - i) % a == 0 && (j - 1) % b == 0) { n     int x = (n - i) / a, y = (j - 1) / b; n     if (x % 2 == y % 2) tiga = max(x, y); n   } n   if ((n - i) % a == 0 && (m - j) % b == 0) { n     int x = (n - i) / a, y = (m - j) / b; n     if (x % 2 == y % 2) empat = max(x, y); n   } n   int ans = min(satu, min(dua, min(tiga, empat))); n   if (ans != 0 && (a > n - 1 || b > m - 1)) ans = 10000000; n   if (ans == 10000000) n     puts( Poor Inna and pony! ); n   else n     printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int arr[1003][1003]; n int n, m; n int dp[1003][1003]; n int vis[1003][1003]; n int dx[4] = {0, 1, 0, -1}; n int dy[4] = {1, 0, -1, 0}; n int dfs(int i, int j, int x) { n   if (vis[i][j] == 2) { n     printf( Poor Inna! n ); n     exit(0); n   } n   if (dp[i][j] != -1) return dp[i][j]; n   vis[i][j] = 2; n   int temp2 = 0; n   int temp3 = 0; n   if (arr[i][j] == 3) temp3 = 1; n   for (int k = 0; k < 4; k++) { n     int ee = i + dx[k]; n     int rr = j + dy[k]; n     if (ee >= 0 & ee < n && rr >= 0 && rr < m && arr[ee][rr] == (x + 1) % 4) { n       temp2 = max(temp2, dfs(ee, rr, (x + 1) % 4)); n     } n   } n   temp2 += temp3; n   vis[i][j] = 1; n   if (arr[i][j] == 0) dp[i][j] = temp2; n   return temp2; n } n int main() { n   char temp; n   for (int i = 0; i < 1003; i++) n     for (int j = 0; j < 1003; j++) { n       dp[i][j] = -1; n       vis[i][j] = 0; n     } n   scanf( %d %d , &n, &m); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) { n       cin >> temp; n       if (temp ==  D ) arr[i][j] = 0; n       if (temp ==  I ) arr[i][j] = 1; n       if (temp ==  M ) arr[i][j] = 2; n       if (temp ==  A ) arr[i][j] = 3; n     } n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) { n       if (arr[i][j] == 0 && vis[i][j] == 0) { n         int temp1 = dfs(i, j, 0); n       } n     } n   int ans = 0; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) n       if (ans < dp[i][j]) ans = dp[i][j]; n   if (ans == 0) n     printf( Poor Dima! n ); n   else n     printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int ex, ey; n bool a[207][207]; n bool vis[207][207]; n int dx[4] = {0, 0, 1, -1}; n int dy[4] = {-1, 1, 0, 0}; n int bfs() { n   queue<pair<int, int>> q; n   vis[100][100] = true; n   q.push(make_pair(100, 100)); n   int dep = 0; n   while (int(q.size())) { n     int size = int(q.size()); n     while (size--) { n       pair<int, int> top = q.front(); n       q.pop(); n       vis[top.first][top.second] = true; n       if (top.first == ex && top.second == ey) return dep; n       for (int i = 0; i < int(4); i++) { n         int nx = top.first + dx[i]; n         int ny = top.second + dy[i]; n         if (a[nx][ny] && !vis[nx][ny]) { n           q.push(make_pair(nx, ny)); n         } n       } n     } n     dep++; n   } n   return -1; n } n int main() { n   string second; n   cin >> second; n   ex = ey = 100; n   for (int i = 0; i < int(int(second.size())); i++) { n     a[ex][ey] = true; n     if (second[i] ==  R ) ex++; n     if (second[i] ==  L ) ex--; n     if (second[i] ==  U ) ey--; n     if (second[i] ==  D ) ey++; n     if (a[ex][ey]) { n       puts( BUG ); n       return 0; n     } n   } n   a[ex][ey] = true; n   int ans = bfs(); n   ans < int(second.size()) ? puts( BUG ) : puts( OK ); n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 513; n int N, ans[MAXN]; n multiset<int> m; n int gcd(int a, int b) { n   if (a < b) swap(a, b); n   if (b == 0) return a; n   return gcd(b, a % b); n } n void solve() { n   for (int k = N; k >= 1; k--) { n     ans[k] = -(*m.begin()); n     m.erase(m.find(-ans[k])); n     for (int i = k + 1; i <= N; i++) { n       m.erase(m.find(-gcd(ans[k], ans[i]))); n       m.erase(m.find(-gcd(ans[k], ans[i]))); n     } n   } n } n int main() { n   scanf( %d , &N); n   int x; n   for (int i = 1; i <= N * N; i++) { n     scanf( %d , &x); n     m.insert(-x); n   } n   solve(); n   for (int i = 1; i <= N; i++) printf( %d%c , ans[i],    n [i == N]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n static const double EPS = 1e-6; n const long double pi = acos(-1.0); n int dx[] = {0, 1, 0, -1, 1, 1, -1, -1, 0}, n     dy[] = {1, 0, -1, 0, 1, -1, 1, -1, 0}; n int aq[5][5]; n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < (int)(n); ++i) { n     int x, y; n     cin >> x >> y; n     aq[x - 1][y - 1] = aq[y - 1][x - 1] = 1; n   } n   int ap = 0, nap = 0; n   for (int i = 0; i < (int)(5); ++i) n     for (int j = 0; j < (int)(i); ++j) n       for (int k = 0; k < (int)(j); ++k) { n         if (aq[i][j] && aq[j][k] && aq[k][i]) ap = 1; n         if (!aq[i][j] && !aq[j][k] && !aq[k][i]) nap = 1; n       } n   if (ap || nap) n     cout <<  WIN  << endl; n   else n     cout <<  FAIL  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int64_t INF = 0x3f3f3f3f; n const int64_t INFLL = 0x3f3f3f3f3f3f3f3f; n const int64_t MOD = 1e9 + 7; n const double EPS = 1e-9; n const double PI = acos(-1); n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n int64_t rand(int64_t x, int64_t y) { n   return uniform_int_distribution<int64_t>(x, y)(rng); n } n int64_t mul(int64_t a, int64_t b) { n   int64_t ret = (1ll * (a % MOD) * (b % MOD)) % MOD; n   return (ret + MOD) % MOD; n } n int64_t add(int64_t a, int64_t b) { n   int64_t ret = (1ll * (a % MOD) + (b % MOD)) % MOD; n   return (ret + MOD) % MOD; n } n int64_t pow_exp(int64_t n, int64_t p) { n   if (!p) return 1; n   if (p & 1) return mul(n, pow_exp(n, p - 1)); n   int64_t tmp = pow_exp(n, p / 2); n   return mul(tmp, tmp); n } n const int64_t MAX = 2e5 + 10; n const int64_t NMAX = 2e5 + 10; n const int64_t MMAX = 2e5 + 10; n const int64_t LOG_MAX = ceil(log2(double(NMAX))); n const int64_t BLOCK = ceil(sqrt(double(NMAX))); n int64_t n; n string str[20]; n map<int64_t, vector<int64_t> > mp[20]; n int64_t tot[20]; n int64_t mem[1 << 20]; n int64_t go(int64_t msk, int64_t sm) { n   if (msk + 1 == 1 << n) { n     return 0; n   } n   int64_t &ret = mem[msk]; n   if (ret + 1) return ret; n   int64_t ans = 0; n   for (int64_t i = 0; i < n; i++) n     if (((msk >> i) & 1) == 0) { n       int64_t mn = mp[i].begin()->first; n       if (sm + mn >= 0) { n         int64_t cans = 0; n         cans += go(msk ^ (1 << i), sm + tot[i]); n         auto it = mp[i].find(-sm); n         if (it != mp[i].end()) cans += it->second.size(); n         ans = max(ans, cans); n       } else { n         auto it = mp[i].find(-sm); n         if (it == mp[i].end()) { n           continue; n         } n         if (it == mp[i].begin()) { n           int64_t cans = it->second.size(); n           ans = max(ans, cans); n         } else { n           auto &v = mp[i][-sm]; n           int64_t r = (--it)->second.front(); n           int64_t cans = lower_bound(v.begin(), v.end(), r) - v.begin(); n           ans = max(ans, cans); n         } n       } n     } n   return ret = ans; n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   cin >> n; n   for (int64_t i = 0; i < n; i++) { n     cin >> str[i]; n   } n   for (int64_t i = 0; i < n; i++) { n     int64_t sm = 0; n     for (int64_t j = 0; j < str[i].size(); j++) { n       sm += str[i][j] ==  (  ? +1 : -1; n       mp[i][sm].push_back(j); n     } n     tot[i] = sm; n   } n   memset(mem, -1, sizeof mem); n   cout << go(0, 0) <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100005; n const double inf = 1e10; n int n, m, q, Eu[N], Ev[N], all[N], vis[N]; n vector<int> V; n map<pair<int, int>, int> F, S; n struct P { n   double x, y; n } p[N]; n inline P operator+(const P& a, const P& b) { return (P){a.x + b.x, a.y + b.y}; } n inline P operator-(const P& a, const P& b) { return (P){a.x - b.x, a.y - b.y}; } n inline double operator*(const P& a, const P& b) { n   return a.x * b.y - b.x * a.y; n } n inline double operator/(const P& b, const P& a) { n   return atan2(b.y - a.y, b.x - a.x); n } n inline bool operator<(const P& a, const P& b) { n   return a.x != b.x ? a.x < b.x : a.y < b.y; n } n inline double crs(P p, P a, P b) { return (a - p) * (b - p); } n struct E { n   int v; n   double a; n }; n inline bool operator<(const E& a, const E& b) { return a.a < b.a; } n vector<E> G[N]; n inline void add(int u, int v) { n   G[u].push_back((E){v, p[v] / p[u]}); n   F[make_pair(u, v)] = 0; n } n int dfs(int x, int p) { n   vis[x] = 1; n   int s = 1; n   for (int i = 0; i < G[x].size(); i++) { n     int y = G[x][i].v; n     if (!vis[y]) s += dfs(y, x); n   } n   if (p) { n     F[make_pair(p, x)] += s; n     F[make_pair(x, p)] -= s; n   } n   return s; n } n inline int calc(int a, int b, int c) { n   return ((p[b] / p[a]) < (p[c] / p[a]) ? 0 : all[a]) + S[make_pair(a, c)] - n          S[make_pair(a, b)] - F[make_pair(a, c)]; n } n int main() { n   scanf( %d%d , &n, &m); n   int T = n + 1; n   p[T] = (P){-inf, 0}; n   for (int i = 1; i <= m; i++) scanf( %d%d , &Eu[i], &Ev[i]); n   for (int i = 1; i <= n; i++) scanf( %lf%lf , &p[i].x, &p[i].y); n   for (int i = 1; i <= m; i++) add(Eu[i], Ev[i]), add(Ev[i], Eu[i]); n   int x = 1; n   for (int i = 2; i <= n; i++) n     if (p[i] < p[x]) x = i; n   add(T, x); n   add(x, T); n   dfs(T, 0); n   for (int i = 1; i <= n + 1; i++) sort(G[i].begin(), G[i].end()); n   for (int i = 1; i <= n + 1; i++) { n     int x = i; n     S[make_pair(i, i)] = 0; n     for (int j = 0; j < G[i].size(); j++) { n       int y = G[i][j].v; n       all[i] += F[make_pair(i, y)]; n       S[make_pair(i, y)] = S[make_pair(i, x)] + F[make_pair(i, y)]; n       x = y; n     } n   } n   scanf( %d , &q); n   while (q--) { n     int k; n     scanf( %d , &k); n     V.clear(); n     for (int i = 1, x; i <= k; i++) scanf( %d , &x), V.push_back(x); n     int ans = 0, cc = V.size(); n     double s = 0; n     p[0] = (P){0, 0}; n     for (int i = 0; i < cc; i++) n       s += crs(p[0], p[V[i ? i - 1 : cc - 1]], p[V[i]]); n     if (s > 0) reverse(V.begin(), V.end()); n     for (int i = 0; i < cc; i++) n       ans += calc(V[i], V[i ? i - 1 : cc - 1], V[(i + 1) % cc]); n     printf( %d n , ans + cc); n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int a, b, c, d, flag = 0; n   scanf( %d %d %d %d , &a, &b, &c, &d); n   if (a + b > c && b + c > a && c + a > b) n     flag = 1; n   else if (b + c > d && c + d > b && d + b > c) n     flag = 1; n   else if (c + d > a && d + a > c && a + c > d) n     flag = 1; n   else if (a + b > d && b + d > a && d + a > b) n     flag = 1; n   if (flag == 1) n     printf( TRIANGLE ); n   else { n     if (a + b == c || b + c == a || c + a == b) n       flag = 2; n     else if (b + c == d || c + d == b || d + b == c) n       flag = 2; n     else if (c + d == a || d + a == c || a + c == d) n       flag = 2; n     else if (a + b == d || b + d == a || d + a == b) n       flag = 2; n   } n   if (flag == 2) n     printf( SEGMENT ); n   else if (flag == 0) n     printf( IMPOSSIBLE ); n } n


#include <bits/stdc++.h> n const int MAXN = 1000; n int ba[MAXN], bb[MAXN]; n int a, b, da, db, t, x, l1, l2; n int main() { n   scanf( %d %d %d %d %d %d , &x, &t, &a, &b, &da, &db); n   int tm = 0; n   while (a >= 0) { n     ba[l1++] = a; n     a -= da; n     tm++; n     if (tm >= t) break; n   } n   tm = 0; n   while (b >= 0) { n     bb[l2++] = b; n     b -= db; n     tm++; n     if (tm >= t) break; n   } n   for (int i = 0; i < l1; i++) n     if (ba[i] == x) { n       printf( YES n ); n       return 0; n     } n   if (x == 0) { n     printf( YES n ); n     return 0; n   } n   for (int i = 0; i < l2; i++) n     if (bb[i] == x) { n       printf( YES n ); n       return 0; n     } n   for (int i = 0; i < l1; i++) n     for (int j = 0; j < l2; j++) n       if (ba[i] + bb[j] == x) { n         printf( YES n ); n         return 0; n       } n   printf( NO n ); n   return 0; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Item { n   int id, t, d, p; n   Item(int _id = 0, int _t = 0, int _d = 0, int _p = 0) n       : id(_id), t(_t), d(_d), p(_p) {} n   bool operator<(const Item &rhs) const { return d < rhs.d; } n }; n constexpr int kN = 101, kT = 2001; n Item a[kN]; n int dp[kN][kT]; n int tk[kN][kT]; n int main() { n   int n; n   scanf( %d , &n); n   int t, d, p; n   for (int i = 0; i < n; ++i) { n     scanf( %d %d %d , &t, &d, &p); n     a[i] = Item(i + 1, t, d, p); n   } n   sort(a, a + n); n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j + a[i].t < a[i].d; ++j) { n       if (dp[i][j] + a[i].p > dp[i + 1][j + a[i].t]) { n         dp[i + 1][j + a[i].t] = dp[i][j] + a[i].p; n         tk[i + 1][j + a[i].t] = 1; n       } n     } n     for (int j = 0; j < kT; ++j) { n       if (dp[i][j] > dp[i + 1][j]) { n         dp[i + 1][j] = dp[i][j]; n         tk[i + 1][j] = 0; n       } n     } n   } n   int x = 0; n   for (int i = 0; i < a[n - 1].d; ++i) { n     if (dp[n][i] > dp[n][x]) { n       x = i; n     } n   } n   printf( %d n , dp[n][x]); n   vector<int> ans; n   for (int i = n; i > 0; --i) { n     if (tk[i][x]) { n       ans.push_back(a[i - 1].id); n       x -= a[i - 1].t; n     } n   } n   reverse(ans.begin(), ans.end()); n   printf( %d n , ans.size()); n   for (int i = 0; i < ans.size(); ++i) { n     printf( %d  , ans[i]); n   } n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   set<int> s; n   while (1) { n     int k = s.size(); n     s.insert(n); n     int m = s.size(); n     if (k == m) break; n     n = n + 1; n     while (n % 10 == 0) { n       n = n / 10; n     } n   } n   cout << s.size() << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool comp(pair<long long, long long> a, pair<long long, long long> b) { n   return a.first > b.first; n } n long long findBest(vector<pair<long long, long long> >& ps, long long x) { n   long long res = 0; n   for (long long i = 0; i < ps.size(); i++) { n     long long num = ps[i].second; n     long long diff = ps[i].first; n     if (x - num >= 0) { n       res += (num * diff); n       x -= num; n     } else { n       res += (x * diff); n       break; n     } n   } n   return res; n } n int main() { n   long long n, s; n   cin >> n >> s; n   vector<pair<long long, long long> > ps; n   long long total = 0; n   long long positives = 0; n   long long pieces = 0; n   for (long long i = 0; i < n; i++) { n     long long num, a, b; n     cin >> num >> a >> b; n     ps.push_back(make_pair(a - b, num)); n     total += num * b; n     pieces += num; n     if (a - b > 0) { n       positives += num; n     } n   } n   sort(ps.begin(), ps.end(), comp); n   long long pizzas = (pieces % s == 0) ? (pieces / s) : (pieces / s + 1); n   long long extra = pizzas * s - pieces; n   long long best = total; n   if (positives > 0) { n     best = max(best, findBest(ps, positives / s * s) + total); n     if (positives % s != 0) { n       long long x = max(positives, (positives / s + 1) * s - extra); n       best = max(best, findBest(ps, x) + total); n     } n   } n   cout << best << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   int i; n   int num; n } a[1005]; n bool cmp(node x, node y) { return x.num < y.num; } n int main() { n   int t; n   scanf( %d , &t); n   while (t--) { n     int n, m; n     scanf( %d %d , &n, &m); n     int i, j; n     int ans = 0; n     for (i = 1; i <= n; i++) { n       scanf( %d , &a[i].num); n       a[i].i = i; n       ans += a[i].num; n     } n     if (m < n || n <= 2) { n       printf( -1 n ); n       continue; n     } n     ans *= 2; n     sort(a + 1, a + 1 + n, cmp); n     int num = a[1].num + a[2].num; n     ans += (m - n) * num; n     printf( %d n , ans); n     for (i = 2; i <= n; i++) { n       printf( %d %d n , i - 1, i); n     } n     printf( 1 %d n , n); n     for (i = 0; i < (m - n); i++) { n       if (a[1].i > a[2].i) n         printf( %d %d n , a[2].i, a[1].i); n       else n         printf( %d %d n , a[1].i, a[2].i); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1000 + 7, MAX_SUM = 10 * 1000 + 7, n           DELTA = 1000 * 1000 * 1000 + 7; n long long inputArray[MAXN], numberOfSubarrays[MAXN][2 * MAX_SUM], PSum; n int main() { n   int numberOfInput; n   cin >> numberOfInput; n   for (int i = 0; i < numberOfInput; ++i) { n     cin >> inputArray[i]; n     PSum += inputArray[i]; n   } n   numberOfSubarrays[0][MAX_SUM + inputArray[0]] = n       numberOfSubarrays[0][MAX_SUM - inputArray[0]] = 1; n   for (int i = 1; i < numberOfInput; ++i) { n     for (int j = 0; j < 2 * MAX_SUM; ++j) { n       numberOfSubarrays[i][j] += numberOfSubarrays[i - 1][j - inputArray[i]]; n     } n     for (int j = 2 * MAX_SUM - 1; j >= 0; --j) { n       numberOfSubarrays[i][j] += numberOfSubarrays[i - 1][j + inputArray[i]]; n       numberOfSubarrays[i][j] %= DELTA; n     } n     numberOfSubarrays[i][MAX_SUM + inputArray[i]] = n         numberOfSubarrays[i][MAX_SUM + inputArray[i]] + 1; n     numberOfSubarrays[i][MAX_SUM - inputArray[i]] = n         numberOfSubarrays[i][MAX_SUM - inputArray[i]] + 1; n   } n   long long ans = 0; n   for (int i = 0; i < numberOfInput; ++i) { n     ans += numberOfSubarrays[i][MAX_SUM]; n     ans %= DELTA; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100005; n struct heap { n   vector<pair<int, int> > v; n   int loc[N]; n   heap() { n     v.push_back(make_pair(-1, -1)); n     memset(loc, -1, sizeof loc); n   } n   void push(pair<int, int> p) { n     int cur; n     cur = v.size(); n     loc[p.second] = cur; n     v.push_back(p); n     while (v[cur] < v[cur >> 1]) { n       swap(v[cur], v[cur >> 1]); n       swap(loc[v[cur].second], loc[v[cur >> 1].second]); n       cur >>= 1; n     } n   } n   void pop() { n     if (empty()) return; n     swap(v[1], v.back()); n     loc[v[1].second] = 1; n     loc[v.back().second] = -1; n     v.pop_back(); n     int cur = 1; n     while ((cur << 1) < v.size()) { n       if ((cur << 1) + 1 == v.size()) { n         if (v[cur] > v[cur << 1]) n           swap(v[cur], v[cur << 1]), n               swap(loc[v[cur].second], loc[v[cur << 1].second]); n         break; n       } n       if (v[cur] < min(v[cur << 1], v[(cur << 1) + 1])) break; n       if (v[cur << 1] < v[(cur << 1) + 1]) { n         swap(v[cur], v[cur << 1]); n         swap(loc[v[cur].second], loc[v[cur << 1].second]); n         cur <<= 1; n       } else { n         swap(v[cur], v[(cur << 1) + 1]); n         swap(loc[v[cur].second], loc[v[(cur << 1) + 1].second]); n         cur = (cur << 1) + 1; n       } n     } n   } n   pair<int, int> top() { return v[1]; } n   bool empty() { return v.size() == 1; } n }; n int n, m; n vector<int> bad[N]; n vector<pair<int, int> > v[N]; n int d[N]; n heap pq; n int main() { n   ios::sync_with_stdio(false); n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     int a, b, c; n     cin >> a >> b >> c; n     --a, --b; n     v[a].push_back(make_pair(b, c)); n     v[b].push_back(make_pair(a, c)); n   } n   for (int i = 0; i < n; i++) { n     int num; n     cin >> num; n     while (num--) { n       int a; n       cin >> a; n       bad[i].push_back(a); n     } n   } n   for (int i = 0; i < n; i++) d[i] = 0x3f3f3f3f; n   d[0] = 0; n   pq.push(make_pair(0, 0)); n   while (!pq.empty()) { n     pair<int, int> tp = pq.top(); n     pq.pop(); n     int t = tp.first; n     int cur = tp.second; n     if (t != d[cur]) continue; n     int j = 0; n     while (j < bad[cur].size() && bad[cur][j] < t) j++; n     while (j < bad[cur].size() && bad[cur][j] == t) t++, j++; n     for (int i = 0; i < v[cur].size(); i++) { n       int to = v[cur][i].first, cost = v[cur][i].second; n       if (t + cost < d[to]) { n         d[to] = t + cost; n         pq.push(make_pair(d[to], to)); n       } n     } n   } n   if (d[n - 1] >= 0x3f3f3f3f) n     puts( -1 ); n   else n     cout << d[n - 1] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1111; n const int INF = 0x7f7f7f7f; n int cases; n const double EPS = 1e-8; n inline int sgn(double a) { return a < -EPS ? -1 : a > EPS; } n int main() { n   double s, a, b, c; n   cin >> s >> a >> b >> c; n   if (!sgn(a) && !sgn(b) && !sgn(c)) { n     puts( 1.0 0.0 0.0 ); n   } else { n     double ts = s / (a + b + c); n     printf( %.10f %.10f %.10f n , a * ts, b * ts, c * ts); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1000000007; n int cnt[4]; n int main() { n   int n; n   scanf( %d , &n); n   getchar(); n   for (int i = 0; i < n; ++i) { n     char ch = getchar(); n     if (ch ==  A ) { n       ++cnt[0]; n     } else if (ch ==  C ) { n       ++cnt[1]; n     } else if (ch ==  G ) { n       ++cnt[2]; n     } else if (ch ==  T ) { n       ++cnt[3]; n     } n   } n   int mx = *max_element(cnt, cnt + 4); n   int nMax = count(cnt, cnt + 4, mx); n   long long ans = 1; n   for (int i = 0; i < n; ++i) { n     ans = (ans * nMax) % mod; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n const long long MAX = 1e6 + 10; n long long a[100005]; n int main() { n   long long n, k, m, sum, t, len; n   string s, d; n   while (cin >> n) { n     int flag = 1; n     sum = 0; n     if (n % 2 == 0) { n       cout << n / 2 << endl; n       for (long long i = (1); i <= (n / 2); ++i) cout <<  2  <<    ; n       cout << endl; n     } else { n       cout << (n - 3) / 2 + 1 << endl; n       for (long long i = (1); i <= ((n - 3) / 2); ++i) cout <<  2  <<    ; n       cout <<  3  << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[200010], t; n int n; n long long b[200010]; n int g[200010]; n bool no[200010]; n int main() { n   scanf( %d %I64d , &n, &t); n   for (int i = 1; i <= n; i++) scanf( %I64d , &a[i]); n   for (int i = 1; i <= n; i++) { n     int x; n     scanf( %d , &x); n     if (x < i) { n       printf( No n ); n       return 0; n     } n     g[i]++; n     g[x]--; n     no[x] = 1; n   } n   int now = 0; n   b[0] = 0; n   for (int i = 1; i <= n; i++) { n     now += g[i]; n     if (now > 0) { n       if (no[i]) { n         printf( No n ); n         return 0; n       } n       b[i] = max(a[i + 1] + t, b[i - 1] + 1); n     } else { n       b[i] = max(a[i] + t, b[i - 1] + 1); n       if (no[i] && (b[i] >= a[i + 1] + t && i != n)) { n         printf( No n ); n         return 0; n       } n     } n   } n   printf( Yes n ); n   for (int i = 1; i <= n; i++) printf( %I64d  , b[i]); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 998244353; n const int maxn = 2e5 + 100; n long long dp[52][52][52]; n bool vis[52][52][52]; n long long val[52][52][52]; n int a[maxn], w[maxn]; n long long qmod(long long a, long long b) { n   long long ans = 1; n   while (b) { n     if (b & 1) ans = ans * a % mod; n     b >>= 1; n     a = a * a % mod; n   } n   return ans; n } n int flag; n int dfs(int n, int i, int j, int k, long long p, long long v, long long sp, n         long long sn) { n   if (!n) { n     return 1LL * p * v % mod; n   } n   if (vis[i][j][k]) { n     return val[i][j][k] * qmod(dp[i][j][k], mod - 2) % mod * p % mod; n   } n   dp[i][j][k] = p; n   val[i][j][k] += n       dfs(n - 1, i + 1, j, k, n           1LL * p * sp % mod * qmod(v + sp + sn, mod - 2) % mod, v, sp + 1, sn); n   val[i][j][k] += dfs(n - 1, i, j + 1, k, n                       1LL * p * sn % mod * qmod(v + sp + sn, mod - 2) % mod, v, n                       sp, max(sn - 1, 0LL)); n   val[i][j][k] += dfs(n - 1, i, j, k + 1, n                       1LL * p * v % mod * qmod(v + sp + sn, mod - 2) % mod, n                       max(v + flag, 0LL), sp, sn); n   val[i][j][k] %= mod; n   vis[i][j][k] = 1; n   return val[i][j][k]; n } n int main() { n   ios::sync_with_stdio(false); n   long long sn = 0, sp = 0; n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n   } n   for (int i = 0; i < n; ++i) { n     cin >> w[i]; n     if (a[i]) { n       sp += w[i]; n     } else n       sn += w[i]; n   } n   for (int i = 0; i < n; ++i) { n     memset(dp, 0, sizeof dp); n     memset(vis, 0, sizeof vis); n     memset(val, 0, sizeof val); n     if (a[i]) { n       flag = 1; n       sp -= w[i]; n     } else { n       flag = -1; n       sn -= w[i]; n     } n     cout << dfs(m, 0, 0, 0, 1, w[i], sp, sn) << endl; n     if (a[i]) { n       sp += w[i]; n     } else n       sn += w[i]; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ii = pair<int, int>; n const int N = 200005; n const int M = 300; n const int OFF = N * M; n int _cnt[N + N * M]; n int *cnt = _cnt + OFF; n int main() { n   char s[N]; n   scanf( %s , s); n   int n = strlen(s); n   int psum[N]; n   for (int i = 0; i < n; i++) { n     s[i] -=  0 ; n     psum[i] = s[i]; n     if (i) psum[i] += psum[i - 1]; n   } n   int sq = min(M, (int)sqrt(n)); n   ll ans = 0; n   for (int j = 1; j < sq; j++) { n     cnt[-1] = 1; n     for (int i = 0; i < n; i++) { n       int val = i - j * psum[i]; n       if (psum[i]) { n         ans += cnt[val]; n       } n       cnt[val]++; n     } n     for (int i = 0; i < n; i++) { n       int val = i - j * psum[i]; n       cnt[val]--; n     } n   } n   int nxt[N]; n   nxt[n] = n; n   for (int i = n - 1; i >= 0; i--) { n     nxt[i] = nxt[i + 1]; n     if (s[i] == 1) nxt[i] = i; n   } n   for (int i = 0; i < n; i++) { n     int r = nxt[i]; n     int q = 1; n     while (r < n && q * sq <= n - i) { n       int p = nxt[r + 1]; n       int rig = max(r, i + q * sq - 1); n       if (rig <= p) { n         ans += (p - i) / q - (rig - i) / q; n       } n       r = p; n       q++; n     } n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 2005, MOD = 998244353; n char ch[MAXN]; n long long cnt[MAXN], dp[MAXN][MAXN]; n long long KSM(long long a, long long t) { n   long long ans = 1; n   while (t) { n     if (t & 1) ans = ans * a % MOD; n     t >>= 1; n     a = a * a % MOD; n   } n   return ans; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> (ch + 1); n   int n = strlen(ch + 1); n   for (int i = 1; i <= n; i++) cnt[i] = cnt[i - 1] + (ch[i] ==  ? ); n   for (int j = 1; j <= n; j++) n     for (int i = j - 1; i; i--) { n       if (ch[i] !=  )  && ch[j] !=  ( ) n         dp[i][j] = n             (dp[i][j] + dp[i + 1][j - 1] + KSM(2, cnt[j - 1] - cnt[i])) % MOD; n       if (ch[i] !=  ( ) dp[i][j] = (dp[i][j] + dp[i + 1][j]) % MOD; n       if (ch[j] !=  ) ) dp[i][j] = (dp[i][j] + dp[i][j - 1]) % MOD; n       if (ch[i] !=  (  && ch[j] !=  ) ) n         dp[i][j] = (dp[i][j] - dp[i + 1][j - 1] + MOD) % MOD; n     } n   cout << dp[1][n]; n } n


#include <bits/stdc++.h> n int main() { n   int i, j, l, x = 0; n   char s[100005]; n   gets(s); n   l = strlen(s); n   for (i = 0, j = l - 1; i < l / 2; i++, j--) { n     if (s[i] != s[j]) { n       x = 1; n       break; n     } n   } n   if (x == 1) { n     printf( NO n ); n   } else { n     for (i = 0; i < l; i++) { n       if (s[i] ==  A  || s[i] ==  H  || s[i] ==  I  || s[i] ==  M  || n           s[i] ==  O  || s[i] ==  T  || s[i] ==  U  || s[i] ==  V  || n           s[i] ==  W  || s[i] ==  X  || s[i] ==  Y ) { n         x = 0; n       } else { n         x = 1; n         break; n       } n     } n     if (x == 1) { n       printf( NO n ); n     } else { n       printf( YES n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n using namespace std; n const int MAX = 2e3 + 5; n const long long MAX2 = 11; n const long long MOD = 998244353; n const long long MOD2 = 1000005329; n const long long INF = 2e18; n const int dr[] = {1, 0, -1, 0, 1, 1, -1, -1, 0}; n const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1, 0}; n const double pi = acos(-1); n const double EPS = 1e-9; n const int block = 2000; n int q, x, y, cnt; n map<string, pair<long long, long long>> ans, dp; n map<string, bool> jen; n map<string, vector<string>> isi; n string s, ty, c; n pair<long long, long long> solve(string o) { n   if (!jen.count(o)) return ans[o]; n   if (isi[o].empty()) return {0, 0}; n   if (dp.count(o)) return dp[o]; n   int hit = isi[o].size() - 1; n   long long mx = 0, ttl = 0; n   pair<long long, long long> res; n   if (jen[o]) { n     for (auto i : isi[o]) { n       res = solve(i); n       mx = max(mx, res.second); n       ttl += res.first; n     } n     return dp[o] = {ttl + 2 * ans[o].first + hit * ans[o].second, n                     mx + 2 * ans[o].first}; n   } n   { n     for (auto i : isi[o]) { n       res = solve(i); n       mx = max(mx, res.first); n       ttl += res.second; n     } n     return dp[o] = {mx + 2 * ans[o].first, n                     ttl + 2 * ans[o].first + hit * ans[o].second}; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> q; n   while (q--) { n     cin >> ty; n     x = y = cnt = 0; n     if (ty ==  Widget ) { n       cin >> s; n       c =   ; n       for (auto i : s) { n         if (i ==  (  || i ==  )  || i ==  , ) n           ++cnt; n         else if (cnt == 1) n           x = x * 10 + i -  0 ; n         else if (cnt == 2) n           y = y * 10 + i -  0 ; n         else n           c.push_back(i); n       } n       ans[c] = {x, y}; n     } else if (ty ==  VBox ) { n       cin >> s; n       ans[s]; n       jen[s]; n     } else if (ty ==  HBox ) { n       cin >> s; n       ans[s]; n       jen[s] = 1; n     } else { n       s = c =   ; n       for (auto i : ty) { n         if (i ==  . ) n           ++cnt; n         else if (i ==  ( ) { n           if (c ==  pack ) n             cnt = 2; n           else if (c ==  set_border ) n             cnt = 3; n           else if (c ==  set_spacing ) n             cnt = 4; n           c =   ; n         } else if (i ==  ) ) n           continue; n         else if (cnt == 0) n           s.push_back(i); n         else if (cnt == 1 || cnt == 2) n           c.push_back(i); n         else if (cnt == 3 || cnt == 4) n           x = x * 10 + i -  0 ; n         else n           return 1; n       } n       if (cnt == 2) n         isi[s].push_back(c); n       else if (cnt == 3) n         ans[s].first = x; n       else n         ans[s].second = x; n     } n   } n   pair<long long, long long> res; n   for (auto i : ans) { n     res = solve(i.first); n     cout << i.first <<     << res.first <<     << res.second <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int inf = 1e18; n int n, m, k, x, p; n int mat[1005][1005]; n void solve(int tc) { n   cin >> n >> m >> k >> p; n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < m; ++j) { n       cin >> mat[i][j]; n     } n   } n   multiset<int, greater<int> > msRow, msCol; n   for (int r = 0; r < n; ++r) { n     long long int rowSum = 0LL; n     for (int i = 0; i < m; ++i) { n       rowSum += mat[r][i]; n     } n     msRow.insert(rowSum); n   } n   for (int c = 0; c < m; ++c) { n     long long int colSum = 0LL; n     for (int i = 0; i < n; ++i) { n       colSum += mat[i][c]; n     } n     msCol.insert(colSum); n   } n   vector<long long int> rows(k + 1, 0LL), cols(k + 1, 0LL); n   for (int i = 1; i <= k; ++i) { n     rows[i] = *msRow.begin(); n     msRow.erase(msRow.begin()); n     msRow.insert(rows[i] - (1LL * p * m)); n     rows[i] += rows[i - 1]; n     cols[i] = *msCol.begin(); n     msCol.erase(msCol.begin()); n     msCol.insert(cols[i] - (1LL * p * n)); n     cols[i] += cols[i - 1]; n   } n   long long int ans = -inf; n   for (int i = 0; i <= k; ++i) { n     long long int tp = rows[i] + cols[k - i]; n     tp -= 1LL * p * i * (k - i); n     if (tp > ans) ans = tp; n   } n   cout << ans <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int tt = 1; n   for (int i = 1; i <= tt; ++i) { n     solve(i); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double pi = 3.1415926535; n const double eps = 1e-6; n struct TE { n   int a, b, c; n } E[501000]; n int tr[501000], n, x[501000], m; n bool cmp(TE a, TE b) { return a.a < b.a; } n int low(int a) { return (a & (-a)); } n int ask(int a) { n   int ret = 1992837465; n   while (a) { n     ret = min(ret, tr[a]); n     a -= low(a); n   } n   return ret; n } n void ins(int a, int b) { n   while (a <= n + 5) { n     tr[a] = min(tr[a], b); n     a += low(a); n   } n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %d , &E[i].a); n   for (int i = 0; i < n; i++) scanf( %d , &E[i].b); n   for (int i = 0; i < n; i++) scanf( %d , &E[i].c); n   for (int i = 0; i < n; i++) { n     E[i].a = -E[i].a; n     E[i].b = -E[i].b; n     E[i].c = -E[i].c; n     x[i] = E[i].b; n   } n   sort(x, x + n); n   m = unique(x, x + n) - x; n   for (int i = 0; i < n; i++) E[i].b = (lower_bound(x, x + m, E[i].b) - x) + 1; n   sort(E, E + n, cmp); n   for (int i = 0; i <= n + 5; i++) tr[i] = 1992837465; n   int ans = 0; n   for (int i = 0; i < n; i++) { n     int h = i; n     while (i < n && E[i].a == E[h].a) { n       if (ask(E[i].b - 1) < E[i].c) { n         ans++; n       } n       i++; n     } n     i--; n     for (int j = h; j <= i; j++) ins(E[j].b, E[j].c); n   } n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 300005; n int n, u, v, x, trie[maxn << 1][26], sz[maxn << 1], tot, cnt[maxn], res; n char op[2]; n int merge(int a, int b) { n   if (!a || !b) return a + b; n   int rt = ++tot; n   memset(trie[rt], 0, sizeof trie[rt]); n   sz[rt] = 1; n   for (int i = 0; i < 26; ++i) { n     trie[rt][i] = merge(trie[a][i], trie[b][i]); n     sz[rt] += sz[trie[rt][i]]; n   } n   return rt; n } n void dfs(int u, int dep) { n   sz[u] = 1; n   for (int i = 0; i < 26; ++i) n     if (trie[u][i]) { n       dfs(trie[u][i], dep + 1); n       sz[u] += sz[trie[u][i]]; n     } n   cnt[dep] += sz[u]; n   tot = n; n   int rt = 0; n   for (int i = 0; i < 26; ++i) rt = merge(rt, trie[u][i]); n   cnt[dep] -= max(1, sz[rt]); n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n - 1; ++i) { n     scanf( %d%d%s , &u, &v, op); n     trie[u][op[0] -  a ] = v; n   } n   dfs(1, 0); n   res = 0; n   for (int i = 1; i < n; ++i) n     if (cnt[i] > cnt[res]) res = i; n   printf( %d n%d n , sz[1] - cnt[res], res + 1); n   return 0; n } n