#include <bits/stdc++.h> n using namespace std; n const int INF = 1e9; n const int maxm = 100005; n int x[maxm], xr[maxm]; n int main() { n   int n, i, j, k, sum, m, cnt = 0, ans, t; n   int x1, x2, y; n   scanf( %d%d , &n, &m); n   for (i = 0; i < n; i++) scanf( %d , &x[i]); n   ans = t = 0; n   sort(x, x + n); n   for (i = 1; i <= m; i++) { n     scanf( %d%d%d , &x1, &x2, &y); n     if (x1 == 1) { n       xr[t++] = x2; n       if (x2 == INF) ans++; n     } n   } n   sort(xr, xr + t); n   ans += n; n   i = 0, j = 0; n   cnt = t; n   while (i < n) { n     while (j < t && xr[j] < x[i]) { n       --cnt; n       ++j; n     } n     ans = min(ans, cnt + i); n     if (ans == 0) break; n     ++i; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1e9 + 7; n const int N = 1e3 + 10; n int len[N]; n int h[N]; n int cost[N]; n int main() { n   memset((cost), (0x3f), sizeof(cost)); n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     int a, b; n     cin >> a >> b >> h[i]; n     len[i] = (a + b) * 2; n   } n   int m; n   cin >> m; n   while (m--) { n     int we, hi, p; n     cin >> hi >> we >> p; n     for (int i = 1; i <= n; i++) { n       if (hi >= h[i]) { n         int o = hi / h[i] * we; n         cost[i] = min(cost[i], (len[i] + o - 1) / o * p); n       } n     } n   } n   long long ans = 0; n   for (int i = 1; i <= n; i++) ans += cost[i]; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n #define pb push_back n using namespace std; n const int N = 3e5 + 5; n vector<int> G[N], G0[N]; n long long day; n int cur, n, pv, pt, a[N], fa[N], dep[N]; n int prv[N], pst[N], np[N], bp[N], ep[N]; n void Build(int u) { n  for(int v : G0[u]) { n   if(v == fa[u]) continue; n   fa[v] = u; n   dep[v] = dep[u] + 1; n   Build(v); n   G[u].pb(v);  n  } n  sort(G[u].begin(), G[u].end(), [](int x, int y){ return a[x] < a[y]; }); n } n void Dfs1(int u) { n  for(int v : G[u]) Dfs1(v); n  pst[u] = ++pt; n  ep[pt] = u; n } n bool Find(int u, int v) { n  if(u == v) return true; n  if(u == 1) return false;  n  return Find(fa[u], v); n } n void Back(int u) { n  if(u == 1) return; n  swap(a[u], a[fa[u]]); n  day++; n  Back(fa[u]);  n } n void Dfs2(int u) { n  if(a[u] < cur) return; n  prv[u] = pv; n  bp[pv++] = u; n  for(int v : G[u]) Dfs2(v); n } n void Dfs3(int u) { n  prv[u] = ++pv; n  for(int v : G[u]) Dfs3(v); n } n int main() { n  ios::sync_with_stdio(false); n  cin >> n; n  for(int i = 1; i <= n; i++) { n   cin >> a[i]; n   np[a[i]] = i;  n  } n  for(int i = 1; i < n; i++) { n   int u, v; n   cin >> u >> v; n   G0[u].pb(v); n   G0[v].pb(u); n  } n  Build(1); n  Dfs1(1); n  if(a[1] == 1) { n   Dfs3(1); n   for(int i = 1; i <= n; i++) { n    if(prv[i] != a[i]) { n     cout <<  NO n ; n     return 0; n    } n   } n   cout <<  YES n0 n ; n   for(int i = 1; i <= n; i++) cout << prv[i] <<    ; n   return 0;  n  } n  cur = a[1] - 1; n  if(!Find(ep[cur], np[cur])) { n   cout <<  NO n ; n   return 0; n  } n  Back(np[cur]); n  for(int i = 1; i <= n; i++) np[a[i]] = i; n  for(int i = 1; i < cur; i++) { n   if(np[i] != ep[i]) { n    cout <<  NO n ; n    return 0; n   } n  } n  pv = cur; n  Dfs2(1); n  for(int i = cur; i <= n; i++) { n   if(np[i] != bp[i]) { n    cout <<  NO n ; n    return 0; n   } n  } n  cout <<  YES n ; n  for(int i = 1; i < cur; i++) day += dep[np[i]]; n  cout << day << endl; n  pv = 0; n  Dfs3(1); n  for(int i = 1; i <= n; i++) cout << prv[i] <<    ; n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   int n, w; n   cin >> n >> w; n   vector<int> a(n), b(n); n   for (int& v : a) cin >> v; n   for (int& v : b) cin >> v; n   double l = 0, r = 2e9; n   for (int t = 0; t < 500; t++) { n     double m = (l + r) / 2; n     double now = m; n     for (int i = 0; i < n && now >= 0; i++) { n       now -= (w + now) / a[i]; n       if (now < 0) break; n       now -= (w + now) / b[(i + 1) % n]; n     } n     if (now >= 0) n       r = m; n     else n       l = m; n   } n   if (r == 2e9) n     cout << -1 <<   n ; n   else n     cout << fixed << setprecision(10) << r <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n char tmp[100001]; n string v[100001][2]; n long long dp[100001][2]; n long long c[100001]; n int n; n long long f(int i, int j) { n   if (i == n - 1) return 0; n   if (dp[i][j] == -1) { n     dp[i][j] = 1000000000000000LL; n     if (v[i][j] <= v[i + 1][0]) dp[i][j] = min(dp[i][j], f(i + 1, 0)); n     if (v[i][j] <= v[i + 1][1]) n       dp[i][j] = min(dp[i][j], f(i + 1, 1) + c[i + 1]); n   } n   return dp[i][j]; n } n int main() { n   ios_base::sync_with_stdio(false); n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %I64d , &c[i]); n   for (int i = 0; i < n; i++) { n     scanf(  %s , tmp); n     v[i][0] = v[i][1] = tmp; n     reverse(v[i][1].begin(), v[i][1].end()); n   } n   memset(dp, -1, sizeof(dp)); n   long long res = min(f(0, 0), f(0, 1) + c[0]); n   if (res == 1000000000000000LL) res = -1; n   printf( %I64d n , res); n } n


#include <bits/stdc++.h> n using namespace std; n int N; n set<pair<int, int>> E[202020]; n signed long long ntot[202020]; n int vis[202020]; n vector<pair<int, signed long long>> V; n map<pair<int, int>, int> M; n vector<signed long long> chi[202020]; n void dfs(int cur, signed long long tot) { n   if (vis[cur]) return; n   vis[cur] = 1; n   V.push_back({cur, tot}); n   for (auto& e : E[cur]) dfs(e.first, tot + e.second); n } n signed long long X[202020], L[202020]; n signed long long Rdia[202020], Rma[202020]; n signed long long Ldia[202020], Lma[202020]; n signed long long tma; n void solve() { n   int i, j, k, l, r, x, y; n   string s; n   cin >> N; n   for (i = 0; i < (N); i++) { n     cin >> x >> y >> r; n     E[x - 1].insert({y - 1, r}); n     E[y - 1].insert({x - 1, r}); n     M[{x - 1, y - 1}] = M[{y - 1, x - 1}] = r; n   } n   queue<int> Q; n   for (i = 0; i < (N); i++) n     if (E[i].size() == 1) Q.push(i); n   while (Q.size()) { n     int cur = Q.front(); n     Q.pop(); n     auto r = *E[cur].begin(); n     E[cur].clear(); n     int tar = r.first; n     int co = r.second; n     E[tar].erase({cur, co}); n     chi[cur].push_back(0); n     chi[cur].push_back(0); n     sort((chi[cur].begin()), (chi[cur].end())); n     reverse((chi[cur].begin()), (chi[cur].end())); n     tma = max(tma, chi[cur][0] + chi[cur][1]); n     chi[tar].push_back(chi[cur][0] + co); n     ntot[cur] = -1; n     if (E[tar].size() == 1) Q.push(tar); n   } n   for (i = 0; i < (N); i++) n     if (ntot[i] >= 0) { n       chi[i].push_back(0); n       chi[i].push_back(0); n       sort((chi[i].begin()), (chi[i].end())); n       reverse((chi[i].begin()), (chi[i].end())); n       tma = max(tma, chi[i][0] + chi[i][1]); n       ntot[i] = chi[i][0]; n     } n   for (i = 0; i < (N); i++) n     if (ntot[i] >= 0) { n       dfs(i, 0); n       break; n     } n   int C = V.size(); n   for (i = 0; i < (C); i++) { n     X[i] = V[i].second; n     L[i] = ntot[V[i].first]; n   } n   signed long long tot = V.back().second + M[{V[0].first, V.back().first}]; n   signed long long mi = 1LL << 60; n   signed long long RR = 0, LL = 1LL << 60; n   for (i = C - 1; i >= 0; i--) { n     Rma[i] = max(Rma[i + 1], tot - X[i] + L[i]); n     Rdia[i] = max({Rdia[i + 1], RR - X[i] + L[i], L[i]}); n     RR = max(RR, X[i] + L[i]); n   } n   for (i = 0; i < (C); i++) { n     Lma[i] = max(i ? Lma[i - 1] : 0, X[i] + L[i]); n     Ldia[i] = max({i ? Ldia[i - 1] : 0, X[i] + L[i] - LL, L[i]}); n     LL = min(LL, X[i] - L[i]); n     if (i == C - 1) { n       mi = min(mi, Ldia[i]); n     } else { n       mi = min(mi, max({Ldia[i], Rdia[i + 1], Lma[i] + Rma[i + 1]})); n     } n   } n   cout << max(tma, mi) << endl; n } n int main(int argc, char** argv) { n   string s; n   int i; n   if (argc == 1) ios::sync_with_stdio(false), cin.tie(0); n   for (i = 0; i < (argc - 1); i++) s += argv[i + 1], s +=   n ; n   for (i = 0; i < (s.size()); i++) ungetc(s[s.size() - 1 - i], stdin); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool cmp(long long a, long long b) { return (a > b); } n long long ceil(const long long &a, const long long &b) { n   if (a % b == 0) return a / b; n   return a / b + 1; n } n long long mod = 1000000007; n const long long N = 105; n string s; n long long n1; n long long res; n long long dp[N][60][2][2 * N]; n long long solve(long long i, long long k, long long cnt = 0, n                 long long z = 100) { n   if (i == n1) { n     if (k % 2 == 0) { n       res = max(res, abs(z - 100)); n     } n     return 0; n   } n   if (dp[i][k][cnt][z] != -1) { n     return dp[i][k][cnt][z]; n   } n   long long k1 = 0, k2 = 0; n   if (s[i] ==  F ) { n     if (k > 0) { n       if (cnt % 2 == 0) { n         k1 = 1 + solve(i + 1, k, 0, z + 1); n         k2 = solve(i + 1, k - 1, 1, z); n       } else { n         k1 = -1 + solve(i + 1, k, 1, z - 1); n         k2 = solve(i + 1, k - 1, 0, z); n       } n     } else { n       if (cnt % 2 == 0) { n         k1 = 1 + solve(i + 1, k, 0, z + 1); n       } else { n         k1 = -1 + solve(i + 1, k, 1, z - 1); n       } n     } n   } else { n     if (k > 0) { n       if (cnt % 2 == 0) { n         k1 = solve(i + 1, k, 1, z); n         k2 = 1 + solve(i + 1, k - 1, 0, z + 1); n       } else { n         k1 = solve(i + 1, k, 0, z); n         k2 = -1 + solve(i + 1, k - 1, 1, z - 1); n       } n     } else { n       if (cnt % 2 == 0) { n         k1 = solve(i + 1, k, 1, z); n       } else { n         k1 = solve(i + 1, k, 0, z); n       } n     } n   } n   return dp[i][k][cnt][z] = max(abs(k1), abs(k2)); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long n; n   cin >> s >> n; n   n1 = s.size(); n   memset(dp, -1, sizeof(dp)); n   long long x = solve(0, n); n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool s[] = {1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, n             0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0}; n int main() { n   string str; n   cin >> str; n   for (int x = 0; x < str.length() / 2; x++) { n     if (str[x] != str[str.length() - 1 - x]) { n       cout <<  NO  << endl; n       return 0; n     } n   } n   for (int x = 0; x < str.length(); x++) { n     if (!s[str[x] -  A ]) { n       cout <<  NO  << endl; n       return 0; n     } n   } n   cout <<  YES  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 1e6 + 5; n int n; n long long int a[MAX], BIT[MAX]; n void update(int idx, long long int val) { n   for (; idx <= n; idx += (idx & (-idx))) { n     BIT[idx] += val; n   } n } n long long int sum(int idx) { n   idx--; n   long long int sumi = 0; n   for (; idx > 0; idx -= (idx & (-idx))) { n     sumi += BIT[idx]; n   } n   return sumi; n } n int main() { n   scanf( %d , &n); n   int l[n], r[n]; n   map<long long int, int> make_pair; n   for (int i = 0; i < n; i++) { n     scanf( %I64d , &a[i]); n     make_pair[a[i]]++; n     l[i] = make_pair[a[i]]; n   } n   make_pair.clear(); n   for (int i = n - 1; i >= 0; i--) { n     make_pair[a[i]]++; n     r[i] = make_pair[a[i]]; n   } n   long long int ans = 0; n   for (int i = n - 1; i >= 0; i--) { n     ans += sum(l[i]); n     update(r[i], 1); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k, sumk; n   cin >> n >> k; n   k--; n   int out[500][500]; n   sumk = 0; n   int current = n * n; n   for (int i = 0; i < n; ++i) { n     for (int j = n - 1; j >= k; --j) { n       out[i][j] = current; n       if (j == k) { n         sumk += current; n       } n       current--; n     } n   } n   for (int i = 0; i < n; ++i) { n     for (int j = k - 1; j >= 0; --j) { n       out[i][j] = current; n       current--; n     } n   } n   cout << sumk << endl; n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < n; ++j) { n       cout << out[i][j] <<    ; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = acos(-1.0); n const double EPS = 1E-9; n const int INF = (int)1E9; n int deg[2005]; n long long w[2005][2005]; n int main() { n   int N, K; n   long long sum = 0, x; n   cin >> N >> K; n   memset(deg, 0, sizeof(deg)); n   for (int i = 0; i < (N); i++) { n     for (int j = (i + 1); j <= (N - 1); j++) { n       scanf( %I64d , &w[i][j]); n       if (w[i][j] != -1) { n         deg[i]++; n         deg[j]++; n       } n     } n   } n   for (int i = 0; i < (N); i++) { n     for (int j = (i + 1); j <= (N - 1); j++) { n       if (w[i][j] != -1) { n         if (K == 1 || K == N - 1) { n           sum += w[i][j] * 2; n         } else { n           sum += w[i][j] * (deg[i] + deg[j] - 2); n         } n       } n     } n   } n   cout << sum / (K == 2 ? N * (N - 1) / 2 : N) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5 * 1e5 + 5; n const int M = 2e6 + 5; n vector<int> a[M], b[M]; n vector<int> c[M], d[M]; n int vis[M]; n int n, w, cnt; n int main() { n   scanf( %d%d , &n, &w); n   w--; n   for (int i = 1; i <= n; i++) { n     int k; n     scanf( %d , &k); n     a[i].resize(k); n     for (int j = 0; j < k; j++) { n       int g; n       scanf( %d , &g); n       a[i][j] = g - 1; n     } n   } n   for (int i = 1; i < n; i++) { n     int up = min(a[i].size(), a[i + 1].size()); n     bool flag = false; n     for (int j = 0; j < up; j++) { n       if (a[i][j] > a[i + 1][j]) { n         b[a[i][j]].push_back(a[i + 1][j]); n         flag = true; n         break; n       } else if (a[i][j] < a[i + 1][j]) { n         b[a[i][j]].push_back(a[i + 1][j]), flag = true; n         break; n       } n     } n     if (!flag && a[i].size() > a[i + 1].size()) { n       puts( -1 ); n       return 0; n     } n   } n   for (int i = 0; i < 1e6; i++) { n     if (!b[i].size()) continue; n     int mx = -1, mn = 1e6 + 5, mx2 = -1, mn2 = 1e6 + 5, add = w - i + 1; n     for (int j : b[i]) { n       mx = max(mx, j); n       mn = min(mn, j); n       mx2 = max(mx2, (j + add) % (w + 1)); n       mn2 = min(mn2, (j + add) % (w + 1)); n     } n     cnt++; n     if (mn > i) { n       c[0].push_back(cnt); n       d[w - mx].push_back(cnt); n       c[w - i + 1].push_back(cnt); n       d[2 * w - mx].push_back(cnt); n     } else { n       c[add].push_back(cnt); n       d[add + w - mx2].push_back(cnt); n     } n   } n   int ans = 0; n   for (int i = 0; i < 1e6; i++) { n     for (int j : c[i]) { n       if (!vis[j]) ans++; n       vis[j]++; n     } n     if (ans == cnt) { n       printf( %d n , i); n       return 0; n     } n     for (int j : d[i]) { n       if (vis[j] == 1) ans--; n       vis[j]--; n     } n   } n   puts( -1 ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename U, typename V> n void Min(U &a, const V &b) { n   if (a > b) a = b; n } n template <typename U, typename V> n void Max(U &a, const V &b) { n   if (a < b) a = b; n } n template <typename U, typename V> n void add(U &a, const V &b) { n   a = (a + b) % 1000000007; n } n template <typename U> n U gcd(U a, U b) { n   if (a == 0) return b; n   if (b == 0) return a; n   if (a >= b) n     return gcd(a % b, b); n   else n     return gcd(a, b % a); n } n int pow(int a, int b) { n   int ans = 1; n   while (b) { n     if (b & 1) ans = 1LL * ans * a % 1000000007; n     a = 1LL * a * a % 1000000007; n     b >>= 1; n   } n   return ans; n } n int pow(int a, int b, int c) { n   int ans = 1; n   while (b) { n     if (b & 1) ans = 1LL * ans * a % c; n     a = 1LL * a * a % c; n     b >>= 1; n   } n   return ans; n } n char s[2005]; n vector<pair<int, int> > ans; n void solve(int l, int r, int K) { n   if (l > r) return; n   if (s[l] !=  ( ) { n     for (int i = r + 1 - 1; i >= l + 1; i--) n       if (s[i] ==  ( ) { n         reverse(s + l, s + i + 1); n         ans.push_back({l, i}); n         break; n       } n   } n   if (s[r] !=  ) ) { n     for (int i = l + 1; i < r; i++) n       if (s[i] ==  ) ) { n         reverse(s + i, s + r + 1); n         ans.push_back({i, r}); n         break; n       } n   } n   if (l + 1 == r) return; n   if (K == 0) { n     int m = (l + r + 1) >> 1; n     int k = r - 1; n     for (int i = l + 1; i < m; i++) n       if (s[i] !=  ( ) { n         while (k > i && s[k] ==  ) ) k--; n         reverse(s + i, s + k + 1); n         ans.push_back({i, k}); n       } n   } else { n     if (s[l + 1] !=  ) ) { n       for (int i = l + 2; i < r; i++) n         if (s[i] ==  ) ) { n           reverse(s + l + 1, s + i + 1); n           ans.push_back({l + 1, i}); n           break; n         } n     } n     solve(l, l + 1, 1); n     solve(l + 2, r, K - 1); n   } n } n int main() { n   int T, i, j, k, m, ca = 0, n, K; n   scanf( %d , &T); n   while (T--) { n     scanf( %d%d%s , &n, &K, s); n     K--; n     ans.clear(); n     solve(0, n - 1, K); n     printf( %d n , int(ans.size())); n     for (auto &o : ans) printf( %d %d n , o.first + 1, o.second + 1); n   } n } n


#include <bits/stdc++.h> n int n, m; n std::vector<std::pair<int, int> > a[2]; n void normalize(std::vector<std::pair<int, int> >& v) { n   if (v.empty()) { n     return; n   } n   std::sort((v).begin(), (v).end()); n   int k = 1; n   for (int i = 1; i < ((int)(v).size()); i++) { n     if (v[i].first != v[i - 1].first) { n       v[k++] = v[i]; n     } else { n       v[k - 1].second += v[i].second; n     } n   } n   v.erase(v.begin() + k, v.end()); n } n int calc(std::vector<std::pair<int, int> >& a, n          std::vector<std::pair<int, int> >& b) { n   if (((int)(a).size()) == 0) { n     return 0; n   } n   int max = a.back().first - a.front().first; n   for (int i = 0; i < ((int)(b).size()); i++) { n     if (b[i].first <= a.front().first) { n       max = std::max(max, n - (a.front().first - b[i].first)); n     } n     if (b[i].first >= a.back().first) { n       max = std::max(max, n - (b[i].first - a.back().first)); n     } n   } n   std::vector<std::pair<int, int> >::iterator itr = n       std::lower_bound((b).begin(), (b).end(), std::make_pair(a[0].first, 0)); n   if (itr != b.end() && itr->first <= a.back().first) { n     max = n; n   } n   return max; n } n long long binom(int n) { return 1LL * n * (n - 1) * (n - 2) / 6; } n long long COUNT(std::vector<std::pair<int, int> >& a, n                 std::vector<std::pair<int, int> >& b, int max) { n   if (calc(a, b) < max) { n     return 0LL; n   } n   long long ways = 0; n   int sum = 0, cnt = 0; n   for (int i = 0; i < ((int)(a).size()); i++) { n     sum += a[i].second; n   } n   for (int i = 0; i < ((int)(b).size()); i++) { n     cnt += b[i].second; n   } n   std::vector<int> suffix(((int)(a).size()) + 1, 0); n   for (int i = ((int)(a).size()) - 1; i >= 0; i--) { n     suffix[i] = suffix[i + 1] + a[i].second; n   } n   if (a.back().first - a.front().first == max) { n     ways += binom(sum); n     ways -= binom(sum - a.front().second) + binom(sum - a.back().second); n     ways += binom(sum - a.front().second - a.back().second); n     ways += 1LL * a.front().second * a.back().second * cnt; n   } n   if (((int)(b).size()) > 0) { n     for (int i = 0; i < ((int)(b).size()); i++) { n       if (b[i].first < a.front().first) { n         int tmp = n - (a.front().first - b[i].first); n         if (tmp == max) { n           ways += n               1LL * a.front().second * (a.front().second - 1) / 2 * b[i].second; n           ways += n               1LL * a.front().second * (sum - a.front().second) * b[i].second; n         } n       } n       if (b[i].first > a.back().first) { n         int tmp = n - (b[i].first - a.back().first); n         if (tmp == max) { n           ways += n               1LL * a.back().second * (a.back().second - 1) / 2 * b[i].second; n           ways += 1LL * a.back().second * (sum - a.back().second) * b[i].second; n         } n       } n     } n     if (max == n) { n       for (int i = 0; i < ((int)(b).size()); i++) { n         std::vector<std::pair<int, int> >::iterator itr = std::lower_bound( n             (a).begin(), (a).end(), std::make_pair(b[i].first, 0)); n         int p = 0, q = 0, r = 0; n         p = suffix[0] - suffix[itr - a.begin()]; n         int t = itr - a.begin(); n         itr = std::lower_bound((a).begin(), (a).end(), n                                std::make_pair(b[i].first + 1, 0)); n         q = suffix[t] - suffix[itr - a.begin()]; n         r = suffix[0] - p - q; n         ways += 1LL * q * p * b[i].second; n         ways += 1LL * q * r * b[i].second; n         ways += 1LL * p * r * b[i].second; n         ways += 1LL * q * (q - 1) / 2 * b[i].second; n       } n     } n   } n   return ways; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < m; i++) { n     int s, f; n     scanf( %d%d , &s, &f); n     a[s].push_back(std::make_pair(f, 1)); n   } n   normalize(a[0]); n   normalize(a[1]); n   int max = std::max(calc(a[0], a[1]), calc(a[1], a[0])); n   if (max == 0) { n     std::cout << binom(m) << std::endl; n   } else { n     std::cout << COUNT(a[0], a[1], max) + COUNT(a[1], a[0], max) << std::endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n bool SR(int &x) { return scanf( %d , &x) == 1; } n bool SR(long long &x) { return scanf( %lld , &x) == 1; } n bool SR(double &x) { return scanf( %lf , &x) == 1; } n bool SR(char *s) { return scanf( %s , s) == 1; } n bool RI() { return true; } n template <typename I, typename... T> n bool RI(I &x, T &...tail) { n   return SR(x) && RI(tail...); n } n void SP(const int x) { printf( %d , x); } n void SP(const long long x) { printf( %lld , x); } n void SP(const double x) { printf( %.16lf , x); } n void SP(const char *s) { printf( %s , s); } n void PL() { puts(  ); } n template <typename I, typename... T> n void PL(const I x, const T... tail) { n   SP(x); n   if (sizeof...(tail)) putchar(   ); n   PL(tail...); n } n const int maxn = 4e6 + 6; n struct Dsu { n   int par[maxn], clc; n   void init(int siz) { n     clc = siz; n     for (int i = 0; i < int(siz); i++) par[i] = i; n   } n   int find(int x) { return x == par[x] ? x : par[x] = find(par[x]); } n   void merge(int a, int b) { n     if (find(a) == find(b)) return; n     par[find(a)] = find(b); n     clc--; n   } n } dsu; n int n, m; n void read() { RI(n, m); } n void build() { n   int tot = n + m + n + m - 4; n   dsu.init(tot); n   int pt = 0; n   for (int i = 0; i < int(tot / 2); i++) { n     dsu.merge(i, pt--); n     if (pt < 0) pt += tot; n   } n   pt = n - 1; n   for (int i = (n - 1); i <= int(n - 1 + tot / 2 - 1); i++) { n     dsu.merge(i, pt--); n     if (pt < 0) pt += tot; n   } n } n void sol() { PL(dsu.clc); } n int main() { n   read(); n   build(); n   sol(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int const N = 13; n int n, m, q; n long long dp[N][1 << N]; n struct lca { n   int u, v, c; n }; n struct edge { n   int u, v; n }; n vector<edge> edges; n vector<lca> lcas; n inline bool valid(int mask, int sub, int u, int v) { n   if (!(mask & (1 << u)) || !(mask & (1 << v))) return false; n   if (bool(sub & (1 << u)) == bool(sub & (1 << v))) return false; n   return true; n } n inline bool ok(int root, int child, int mask, int sub) { n   for (auto ed : edges) { n     int u = ed.u, v = ed.v; n     if (!valid(mask, sub, u, v)) continue; n     if (root == v) swap(u, v); n     if (root != u || child != v) return false; n   } n   for (auto l : lcas) { n     int u = l.u, v = l.v, c = l.c; n     if (!valid(mask, sub, u, v)) continue; n     if (c != root) return false; n   } n   return true; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m >> q; n   for (int i = 0; i < m; i++) { n     int x, y; n     cin >> x >> y; n     x--, y--; n     edges.push_back({x, y}); n   } n   for (int i = 0; i < q; i++) { n     int x, y, c; n     cin >> x >> y >> c; n     x--, y--, c--; n     if (x == y && x != c) return cout <<  0 n , 0; n     lcas.push_back({x, y, c}); n   } n   for (int i = 0; i < n; i++) dp[i][1 << i] = 1; n   for (int mask = 1; mask < (1 << n); mask++) { n     for (int root = 0; root < n; root++) { n       if (!(mask & (1 << root))) continue; n       int p = -1; n       for (int i = 0; i < n; i++) n         if ((mask & (1 << i)) && i != root) p = i; n       if (p == -1) continue; n       for (int sub = mask & (mask - 1); sub > 0; sub = (sub - 1) & mask) { n         if ((sub & (1 << root)) || !(sub & (1 << p))) continue; n         for (int child = 0; child < n; child++) n           if (ok(root, child, mask, sub)) n             dp[root][mask] += dp[child][sub] * dp[root][mask ^ sub]; n       } n     } n   } n   cout << dp[0][(1 << n) - 1] << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 101000; n int fa[maxn], sz[maxn], c[maxn]; n int fd(int f) { return fa[f] == f ? f : fa[f] = fd(fa[f]); } n int n, m, f[maxn]; n bool isok(int x) { n   while (x) { n     int d = x % 10; n     if (d != 4 && d != 7) return false; n     x /= 10; n   } n   return true; n } n void bp(int v, int w) { n   for (int i = n; i >= v; --i) f[i] = min(f[i], f[i - v] + w); n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; ++i) fa[i] = i; n   for (int i = 1; i <= m; ++i) { n     int u, v; n     scanf( %d%d , &u, &v); n     fa[fd(u)] = fd(v); n   } n   for (int i = 1; i <= n; ++i) sz[fd(i)]++; n   for (int i = 1; i <= n; ++i) c[sz[i]]++, f[i] = 1e9; n   for (int i = 1; i <= n; ++i) n     if (c[i]) { n       for (int s = 1; s <= c[i]; s <<= 1) bp(s * i, s), c[i] -= s; n       bp(c[i] * i, c[i]); n     } n   int ans = 1e9; n   for (int i = 1; i <= n; ++i) n     if (isok(i)) ans = min(ans, f[i]); n   printf( %d n , ans == 1e9 ? -1 : ans - 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0), cin.tie(NULL), cout.tie(NULL); n   int n, x, y; n   cin >> n >> x >> y; n   int a[n]; n   for (int j = 0; j < n; j++) cin >> a[j]; n   if (x > y) { n     cout << n; n     return 0; n   } n   int count = 0; n   for (int j = 0; j < n; j++) { n     if (a[j] <= x) count++; n   } n   cout << (count + 1) / 2; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> g[100001]; n long long v[100001]; n pair<long long, long long> r[100001]; n int mark[100001]; n void dfs(int s) { n   mark[s] = 1; n   r[s] = pair<long long, long long>(0, 0); n   for (int i = 0; i < g[s].size(); i++) { n     if (!mark[g[s][i]]) { n       dfs(g[s][i]); n       r[s].first = min(r[s].first, r[g[s][i]].first); n       r[s].second = max(r[s].second, r[g[s][i]].second); n     } n   } n   long long k = v[s] - r[s].first - r[s].second; n   if (k > 0) n     r[s].second = r[s].second + k; n   else n     r[s].first += k; n } n int main() { n   int n; n   memset(mark, 0, sizeof mark); n   cin >> n; n   int a, b; n   for (int i = 0; i < n - 1; i++) { n     cin >> a >> b; n     g[a - 1].push_back(b - 1); n     g[b - 1].push_back(a - 1); n   } n   for (int i = 0; i < n; i++) cin >> v[i]; n   dfs(0); n   long long ans = -r[0].first + r[0].second; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, grid[2500][2500], R[10005], C[10005]; n vector<pair<long long, pair<long long, long long> > > A, B; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n; n   for (long long i = 0; i < n; i++) n     for (long long j = 0; j < n; j++) n       cin >> grid[i][j], R[i + j] += grid[i][j], C[i - j + 5000] += grid[i][j]; n   long long Min = (long long)1e18; n   for (long long i = 0; i < n; i++) { n     for (long long j = 0; j < n; j++) { n       long long Z = R[i + j] + C[i - j + 5000] - grid[i][j]; n       Min = min(Min, Z); n       if (B.size() < 5000 || Z >= Min) n         B.push_back(make_pair(R[i + j] + C[i - j + 5000] - grid[i][j], n                               make_pair(i, j))); n     } n   } n   sort(B.begin(), B.end()); n   reverse(B.begin(), B.end()); n   long long a, b, x, y; n   long long Max = 0; n   for (long long i = 0; i < min((long long)B.size(), 5000LL); i++) { n     for (long long j = 0; j < min((long long)B.size(), 3000LL); j++) { n       if (B[i].first + B[j].first >= Max && n           ((B[i].second.first + B[i].second.second - B[j].second.first + n             B[j].second.second)) % n               2 && n           ((B[i].second.first - B[i].second.second - n             (B[j].second.first - B[j].second.second))) % n               2) { n         a = B[i].second.first, b = B[i].second.second; n         x = B[j].second.first, y = B[j].second.second; n         Max = B[i].first + B[j].first; n       } n     } n   } n   cout << Max << endl; n   cout << a + 1 <<     << b + 1 <<     << x + 1 <<     << y + 1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long n; n   cin >> n; n   long long now = 0; n   long long mx = -1; n   while (true) { n     long long rem = (n - (now * 7)); n     if (rem < 0) { n       break; n     } else { n       if (rem % 4 == 0) { n         mx = max(mx, now); n       } n     } n     now++; n   } n   if (mx == -1) { n     cout << -1; n     return 0; n   } else { n     long long four = n - (mx * 7); n     four /= 4; n     for (long long i = 0; i < four; i++) { n       cout << 4; n     } n     for (long long i = 0; i < mx; i++) { n       cout << 7; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int a[100005]; n int Gcd(int x, int y) { n   if (y == 0) return x; n   return Gcd(y, x % y); n } n int main(void) { n   int n, i, ans; n   scanf( %d , &n); n   for (i = 1; i <= n; i++) scanf( %d , &a[i]); n   sort(a + 1, a + n + 1); n   ans = 0; n   for (i = 1; i <= n; i++) ans = Gcd(ans, a[i]); n   if (((a[n] / ans) - (n - 1)) % 2) n     printf( Bob n ); n   else n     printf( Alice n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   char mat[n][n]; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       cin >> mat[i][j]; n     } n   } n   char a = mat[0][0]; n   char b = mat[0][1]; n   if (a == b) { n     cout <<  NO  << endl; n     return 0; n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       if ((i == j || i == (n - j - 1)) && mat[i][j] != a) { n         cout <<  NO  << endl; n         return 0; n       } else if ((!(i == j || i == (n - j - 1)) && mat[i][j] != b)) { n         cout <<  NO  << endl; n         return 0; n       } n     } n   } n   cout <<  YES  << endl; n } n


#include <bits/stdc++.h> n long long prod_dig(long long n) { n   long long prod = 1; n   int a; n   while (n) { n     a = n % 10; n     if (a) { n       prod *= a; n     } n     n /= 10; n   } n   return prod; n } n int arr[10][1000000 + 1]; n int main() { n   int t; n   for (int i = 1; i <= 1000000; i++) { n     t = i; n     while (t / 10) { n       t = prod_dig(t); n     } n     for (int j = 0; j < 10; j++) { n       arr[j][i] = arr[j][i - 1]; n     } n     arr[t][i]++; n   } n   int Q, l, r, x, key; n   scanf( %d , &Q); n   while (Q--) { n     scanf( %d%d%d , &l, &r, &key); n     printf( %d n , arr[key][r] - arr[key][l - 1]); n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline T Abs(T a) { n   return a > 0 ? a : -a; n } n void split(string s, string seps, vector<string> &res, bool doEmpty = false) { n   int start = 0; n   res.clear(); n   for (int i = 0; i < s.size(); i++) { n     if (seps.find(s[i]) != string::npos) { n       if (doEmpty || i - start > 0) res.push_back(s.substr(start, i - start)); n       start = i + 1; n     } n   } n   if (doEmpty || s.size() - start > 0) n     res.push_back(s.substr(start, s.size() - start)); n } n string Union(vector<string> s, int start = 0) { n   string res; n   for (int i = start; i < s.size(); i++) res.append(s[i]); n   return res; n } n int main() { n   string s; n   int n; n   cin >> s >> n; n   vector<string> b(n); n   vector<int> fn(s.size()); n   for (int i = 0; i < n; i++) cin >> b[i]; n   for (int i = 0; i < n; i++) { n     int k = 0; n     while (k < s.length()) { n       int r = s.find(b[i], k); n       if (r == s.npos) break; n       fn[r + b[i].size() - 1] = max(fn[r + b[i].size() - 1], r + 1); n       k = r + 1; n     } n   } n   int best = 0; n   int bi = 0; n   int cur = 0; n   int ci = 0; n   for (int i = 0; i < s.length(); i++) { n     if (fn[i]) { n       int lci = ci; n       ci = -1; n       int g = fn[i]; n       int j = i; n       while (j >= g) { n         g = max(g, fn[j]); n         ci = j; n         j--; n       } n       if (ci == -1) ci = i + 1; n       cur = i - ci; n     } n     cur++; n     if (cur > best) { n       best = cur; n       bi = ci; n     } n   } n   cout << best <<     << bi; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int t; n   cin >> t; n   while (t > 0) { n     string s1, s2; n     cin >> s1 >> s2; n     long long int f = 0; n     if (s1.length() > s2.length()) { n       cout <<  NO  << endl; n       f = -1; n     } else { n       long long int i = 0, j = 0; n       while (i < s1.length() || j < s2.length()) { n         if (s2[j] == s1[i] && s2[j + 1] == s1[i + 1]) { n           j++; n           i++; n         } else if (s2[j] == s1[i]) n           j++; n         else if (s2[j] == s1[i + 1] && j > 0) n           i++; n         else { n           cout <<  NO  << endl; n           f = -1; n           break; n         } n       } n       if (f != -1) cout <<  YES  << endl; n     } n     t--; n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void read(T &x) { n   x = 0; n   bool f = 0; n   char c = getchar(); n   for (; !isdigit(c); c = getchar()) n     if (c ==  - ) f = 1; n   for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); n   if (f) x = -x; n } n template <typename F> n inline void write(F x, char ed =   n ) { n   static short st[30]; n   short tp = 0; n   if (x < 0) putchar( - ), x = -x; n   do st[++tp] = x % 10, x /= 10; n   while (x); n   while (tp) putchar( 0  | st[tp--]); n   putchar(ed); n } n template <typename T> n inline void Mx(T &x, T y) { n   x < y && (x = y); n } n template <typename T> n inline void Mn(T &x, T y) { n   x > y && (x = y); n } n const int N = 105; n const int P = 1e9 + 7; n long long dp[N][N][2], f[N][2], siz[N]; n int h[N], ne[N << 1], to[N << 1], tot; n inline void add(int x, int y) { ne[++tot] = h[x], to[h[x] = tot] = y; } n void dfs(int x, int fa) { n   dp[x][0][0] = dp[x][0][1] = siz[x] = 1; n   for (int i = h[x], y; i; i = ne[i]) { n     if ((y = to[i]) == fa) continue; n     dfs(y, x); n     memcpy(f, dp[x], sizeof(f)); n     memset(dp[x], 0, sizeof(dp[x])); n     for (int j = 0; j < siz[x] + siz[y]; j++) { n       for (int k = 0; k <= min(siz[x] - 1, (long long)j); k++) { n         dp[x][j][0] = (dp[x][j][0] + f[k][0] * dp[y][j - k][1]) % P; n         dp[x][j][1] = (dp[x][j][1] + f[k][1] * dp[y][j - k][1]) % P; n         if (j != k) { n           dp[x][j][0] = (dp[x][j][0] + f[k][0] * dp[y][j - k - 1][0]) % P; n           dp[x][j][1] = (dp[x][j][1] + f[k][1] * dp[y][j - k - 1][0] + n                          f[k][0] * dp[y][j - k - 1][1]) % n                         P; n         } n       } n     } n     siz[x] += siz[y]; n   } n } n long long fpw(long long x, long long mi) { n   long long res = 1; n   for (; mi; mi >>= 1, x = x * x % P) n     if (mi & 1) res = res * x % P; n   return res; n } n long long mi[N], C[N][N], n; n int main() { n   read(n); n   for (int i = 1, x, y; i < n; i++) read(x), read(y), add(x, y), add(y, x); n   dfs(1, 0); n   mi[0] = fpw(n, P - 2) % P; n   for (int i = 1; i <= n; i++) mi[i] = mi[i - 1] * n % P; n   C[0][0] = 1; n   for (int i = 1; i <= n; i++) { n     C[i][0] = 1; n     for (int j = 1; j <= n; j++) C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % P; n   } n   for (int i = 0; i <= n - 1; i++) n     dp[1][i][1] = dp[1][i][1] * mi[n - i - 1] % P; n   for (int i = 0; i <= n - 1; i++) { n     long long ans = 0; n     for (int k = i; k <= n - 1; k++) n       if ((k - i) & 1) n         ans -= C[k][i] * dp[1][k][1] % P; n       else n         ans += C[k][i] * dp[1][k][1] % P; n     write(((ans % P) + P) % P,    ); n   } n   return 0; n } n


#include<bits/stdc++.h> n using namespace std; n   n #define FIO             ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0) n mt19937                 rng(chrono::steady_clock::now().time_since_epoch().count()); n #define endl              n  n #define mp              make_pair n #define pb              push_back n #define ins             insert n #define lb              lower_bound n #define ub              upper_bound n #define ff              first n #define ss              second n #define sz(x)           (ll)(x).size() n #define vi              vector<int> n #define mt              make_tuple n #define mii             map<int,int> n #define um              unordered_map<int,int> n #define pii             pair<int,int>   n #define maxpq           priority_queue<int> n #define minpq           priority_queue<int,vi,greater<int>> n #define setbits(x)      __builtin_popcountll(x) n #define ps(x,y)         fixed<<setprecision(y)<<x n #define all(x)          x.begin(), x.end() n #define allr(x)         rbegin(x), rend(x) n #define w(x)            int x; cin>>x; while(x--)   n #define goog(tno)       cout <<  Case #  << tno << :   n #define trace(x)        cerr<<#x<< :  <<x<<   <<endl; n   n typedef long double ld; n #define ll  long long int n #define ull unsigned ll n const int MOD = 1000000007; n const ll INF = 1e18; n const ld pi =3.14159265359;  n // const int mod = 1000003; n   n template <class T> ll maxpower(T a , T b){ll ans = 0;while(a > 0 && a % b == 0){ans++;a /= b;}return ans;} n template <class T> T mceil(T a, T b){if(a % b == 0) return a/b; else return a/b + 1;} n   n //--------------------------------------------------------------------------- n // cin>> vector<T> n template <class T> istream &operator>>(istream &in, vector<T> &a){for (auto &i : a)cin >> i;return in;} n // cout << vector<T> n template <class T> ostream &operator<<(ostream &ostream, const vector<T> &c){for (auto &it : c) cout << it <<    ;return ostream;} n //--------------------------------------------------------------------------- n   n void __print(int x) {cerr << x;} n void __print(long x) {cerr << x;} n void __print(long long x) {cerr << x;} n void __print(unsigned x) {cerr << x;} n void __print(unsigned long x) {cerr << x;} n void __print(unsigned long long x) {cerr << x;} n void __print(float x) {cerr << x;} n void __print(double x) {cerr << x;} n void __print(long double x) {cerr << x;} n void __print(char x) {cerr <<      << x <<     ;} n void __print(const char *x) {cerr <<     << x <<    ;} n void __print(const string &x) {cerr <<     << x <<    ;} n void __print(bool x) {cerr << (x ?  true  :  false );} n   n template<typename T, typename V> n void __print(const pair<T, V> &x) {cerr <<  { ; __print(x.first); cerr <<  , ; __print(x.second); cerr <<  } ;} n template<typename T> n void __print(const T &x) {int f = 0; cerr <<  { ; for (auto &i: x) cerr << (f++ ?  ,  :   ), __print(i); cerr <<  } ;} n void _print() {cerr <<  ] n ;} n template <typename T, typename... V> n void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr <<  ,  ; _print(v...);} n   n #ifndef ONLINE_JUDGE n #define debug(x...) cerr <<  [  << #x <<  ] = [ ; _print(x) n #else n #define debug(x...) n #endif n  n int main(){ n     FIO; n     w(t){ n         ll n; n         cin>>n; n  n         ll sum = 0; n  n         for(ll i=0;i<n;i++){ n             ll x; n             cin>>x; n  n             sum += x; n         } n  n         cout<<sum%n*(n-sum%n)<<endl; n     } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100010, M = 1505, mod = 1e9 + 7; n inline int qpow(int x, int y = mod - 2) { n   int res = 1; n   for (; y; y >>= 1, x = 1ll * x * x % mod) n     if (y & 1) res = 1ll * res * x % mod; n   return res; n } n int fac[N], inv[N]; n inline int C(int x, int y) { n   if (y < 0 || y > x) return 0; n   return 1ll * fac[x] * inv[y] % mod * inv[x - y] % mod; n } n void C_init(int n) { n   fac[0] = 1; n   for (int i = 1; i <= n; i++) fac[i] = 1ll * fac[i - 1] * i % mod; n   inv[n] = qpow(fac[n]); n   for (int i = n - 1; ~i; i--) inv[i] = inv[i + 1] * (i + 1ll) % mod; n } n int n, m, p, np, k, B[M]; n int pre_b[M], pre_bl[M]; n void init() { n   int a, b; n   cin >> n >> m >> a >> b >> k; n   p = 1ll * a * qpow(b) % mod, np = (1 - p + mod) % mod; n   C_init(k); n   for (int i = 0; i <= min(m - 1, k); ++i) n     B[i] = 1ll * C(k, i) * qpow(p, i) % mod * qpow(np, k - i) % mod; n   for (int i = 1; i <= m; ++i) pre_b[i] = (pre_b[i - 1] + B[i - 1]) % mod; n } n inline void add(int &x, int y) { n   x += y; n   if (x >= mod) x -= mod; n } n inline void sub(int &x, int y) { n   x -= y; n   if (x < 0) x += mod; n } n int L[M], F = 1; n void trans() { n   static int S[M]; n   for (int i = 1; i <= m; ++i) n     pre_bl[i] = (pre_bl[i - 1] + 1ll * B[i - 1] * L[i]) % mod; n   for (int x = 1; x <= m; ++x) n     S[x] = 1ll * B[m - x] * n            (1ll * (F - L[m - x + 1] + mod) * pre_b[x] % mod - pre_bl[x] + mod) % n            mod; n   F = 0; n   for (int i = 1; i <= m; ++i) n     L[i] = (L[i - 1] + S[i - 1]) % mod, F = (F + S[i]) % mod; n } n int main() { n   init(); n   while (n--) trans(); n   cout << F << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5e5 + 5; n const long long L = 1e18 + 18; n int Z; n struct edge { n   int s; n   int t; n   long long W; n   int ind; n   edge(int a, int b, long long c, int I) { n     s = a; n     t = b; n     W = c; n     ind = I; n   } n }; n struct cmp { n   bool operator()(edge a, edge b) { return a.W > b.W; } n }; n long long odl[N]; n vector<edge> G[N]; n vector<pair<long long, long long> > D[N]; n void dfs(int v, int oj, int k); n int main() { n   Z = 0; n   int n, m, k; n   scanf( %d%d%d , &n, &m, &k); n   for (long long i = 1; i <= n; i++) odl[i] = L; n   for (long long i = 1; i <= m; i++) { n     long long a, b, c; n     scanf( %lld%lld%lld , &a, &b, &c); n     G[a].push_back(edge(a, b, c, i)); n     G[b].push_back(edge(b, a, c, i)); n   } n   priority_queue<edge, vector<edge>, cmp> kol; n   odl[1] = 0; n   for (auto z : G[1]) kol.push(z); n   while (!kol.empty()) { n     edge X = kol.top(); n     kol.pop(); n     if (odl[X.t] < L) continue; n     odl[X.t] = X.W; n     D[X.s].push_back(make_pair(X.t, X.ind)); n     for (auto z : G[X.t]) { n       if (odl[z.t] > X.W + z.W) { n         kol.push(edge(z.s, z.t, z.W + X.W, z.ind)); n       } n     } n   } n   k = min(k, n - 1); n   printf( %d n , k); n   dfs(1, 1, k); n   if (k > 0) printf(  n ); n } n void dfs(int v, int oj, int k) { n   for (auto z : D[v]) { n     if (z.first == oj) continue; n     if (Z >= k) break; n     printf( %lld  , z.second); n     Z++; n     dfs(z.first, v, k); n   } n } n


#include <bits/stdc++.h> n using namespace std; n #pragma GCC optimize( Ofast ) n #pragma GCC target( avx,avx2,fma ) n #pragma GCC optimization( unroll-loops ) n template <typename T, size_t N> n int SIZE(const T (&t)[N]) { n   return N; n } n template <typename T> n int SIZE(const T &t) { n   return t.size(); n } n string to_string(const string s, int x1 = 0, int x2 = 1e9) { n   return     + ((x1 < s.size()) ? s.substr(x1, x2 - x1 + 1) :   ) +    ; n } n string to_string(const char *s) { return to_string((string)s); } n string to_string(const bool b) { return (b ?  true  :  false ); } n string to_string(const char c) { return string({c}); } n template <size_t N> n string to_string(const bitset<N> &b, int x1 = 0, int x2 = 1e9) { n   string t =   ; n   for (int __iii__ = min(x1, SIZE(b)), __jjj__ = min(x2, SIZE(b) - 1); n        __iii__ <= __jjj__; ++__iii__) { n     t += b[__iii__] +  0 ; n   } n   return     + t +    ; n } n template <typename A, typename... C> n string to_string(const A(&v), int x1 = 0, int x2 = 1e9, C... coords); n int l_v_l_v_l = 0, t_a_b_s = 0; n template <typename A, typename B> n string to_string(const pair<A, B> &p) { n   l_v_l_v_l++; n   string res =  (  + to_string(p.first) +  ,   + to_string(p.second) +  ) ; n   l_v_l_v_l--; n   return res; n } n template <typename A, typename... C> n string to_string(const A(&v), int x1, int x2, C... coords) { n   int rnk = rank<A>::value; n   string tab(t_a_b_s,    ); n   string res =   ; n   bool first = true; n   if (l_v_l_v_l == 0) res +=   n ; n   res += tab +  [ ; n   x1 = min(x1, SIZE(v)), x2 = min(x2, SIZE(v)); n   auto l = begin(v); n   advance(l, x1); n   auto r = l; n   advance(r, (x2 - x1) + (x2 < SIZE(v))); n   for (auto e = l; e != r; e = next(e)) { n     if (!first) { n       res +=  ,  ; n     } n     first = false; n     l_v_l_v_l++; n     if (e != l) { n       if (rnk > 1) { n         res +=   n ; n         t_a_b_s = l_v_l_v_l; n       }; n     } else { n       t_a_b_s = 0; n     } n     res += to_string(*e, coords...); n     l_v_l_v_l--; n   } n   res +=  ] ; n   if (l_v_l_v_l == 0) res +=   n ; n   return res; n } n void dbgm() { ; } n template <typename Heads, typename... Tails> n void dbgm(Heads H, Tails... T) { n   cerr << to_string(H) <<   |  ; n   dbgm(T...); n } n const long long mod = 1e9 + 7; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cerr <<  ...............Console is yours! :).................  n        <<   n ; n   long long n; n   int m; n   cin >> n >> m; n   string num = to_string(n); n   int l = num.length(); n   long long dp[1 << l][m]; n   memset(dp, 0, sizeof(dp)); n   long long fac = 1; n   int count[10]; n   memset(count, 0, sizeof(count)); n   for (int i = 0; i < l; i++) fac *= ++count[num[i] -=  0 ]; n   dp[0][0] = 1; n   for (int i = 0; i < 1 << l; i++) { n     for (int j = 0; j < l; j++) { n       if (!(i & (1 << j)) && (i || num[j])) { n         for (int k = 0; k < m; k++) n           dp[i | (1 << j)][(k * 10 + num[j]) % m] += dp[i][k]; n       } n     } n   } n   cout << dp[(1 << l) - 1][0] / fac <<   n ; n   cerr <<  ......^_^.....  n        <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using Int = long long; n template <typename T1, typename T2> n inline void chmin(T1 &a, T2 b) { n   if (a > b) a = b; n } n template <typename T1, typename T2> n inline void chmax(T1 &a, T2 b) { n   if (a < b) a = b; n } n struct FastIO { n   FastIO() { n     cin.tie(0); n     ios::sync_with_stdio(0); n   } n } fastio_beet; n signed main() { n   Int n; n   cin >> n; n   vector<Int> bs(n); n   for (Int i = 0; i < n; i++) cin >> bs[i]; n   map<Int, Int> sum; n   for (Int i = 0; i < n; i++) sum[bs[i] - i] += bs[i]; n   Int ans = 0; n   for (auto p : sum) chmax(ans, p.second); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int LOG = 22; n int32_t main() { n   ios::sync_with_stdio(false); n   int n; n   cin >> n; n   vector<int> tab(n); n   vector<int> dp(1 << LOG); n   vector<pair<int, int> > samples(1 << LOG, {-1, -1}); n   auto update = [&](int a, int i) { n     if (i == -1) return; n     if (samples[a].first != -1) samples[a].second = i; n     samples[a].first = i; n   }; n   for (int i = 0; i < n; i++) { n     cin >> tab[i]; n     dp[tab[i]]++; n     update(tab[i], i); n   } n   for (int i = 0; i < LOG; i++) { n     for (int m = 0; m < (1 << LOG); m++) { n       if (m & (1 << i)) { n         dp[m] += dp[m ^ (1 << i)]; n         update(m, samples[m ^ (1 << i)].first); n         update(m, samples[m ^ (1 << i)].second); n       } n     } n   } n   for (int i = 0; i < n; i++) { n     int a = tab[i] ^ ((1 << LOG) - 1); n     if (samples[a].first == i) n       cout << (samples[a].second == -1 ? -1 : tab[samples[a].second]) <<    ; n     else n       cout << (samples[a].first == -1 ? -1 : tab[samples[a].first]) <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int s, x1, x2, t1, t2, p, d; n int main() { n   cin >> s >> x1 >> x2 >> t1 >> t2 >> p >> d; n   int t = 0, flag1 = 0, flag2 = 0; n   while (flag1 == 0 || flag2 == 0) { n     if (flag1) { n       if (p == x2) break; n     } else if (p == x1) n       flag1 = 1; n     if (p == 0 || p == s) d = -d; n     p += d; n     t++; n   } n   cout << min(t * t1, abs(x1 - x2) * t2) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 998244353; n void run(istream &cin, ostream &cout) { n   int n, m; n   cin >> n >> m; n   string a, b; n   cin >> a >> b; n   int diff = n - m; n   if (diff > 0) { n     b = string(diff,  0 ) + b; n   } else { n     a = string(-diff,  0 ) + a; n   } n   n = max(n, m); n   vector<long long> suff(n); n   for (int i = 0; i < n; i++) { n     suff[i] = (b[i] ==  1 ); n     if (i > 0) { n       suff[i] += suff[i - 1]; n     } n   } n   long long pw = 1, ans = 0; n   for (int i = n - 1; i >= 0; i--) { n     if (a[i] ==  1 ) { n       ans = (ans + pw * suff[i] % mod) % mod; n     } n     pw = pw * 2 % mod; n   } n   cout << ans; n } n int main() { n   cin.tie(0); n   ios::sync_with_stdio(0); n   run(cin, cout); n   return 0; n } n


#include <bits/stdc++.h> n int n, m, k; n int kex[101][101] = {0}; n int main() { n   scanf( %d%d%d , &n, &m, &k); n   int i; n   int x, y; n   int have = 0; n   for (i = 0; i < k; i++) { n     scanf( %d%d , &x, &y); n     if (x <= 5 || x >= n - 4 || y <= 5 || y >= m - 4) have = 1; n   } n   if (have) n     printf( YES ); n   else n     printf( NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int testcases; n   cin >> testcases; n   while (testcases) { n     int n, s, t; n     cin >> n >> s >> t; n     if (s > t) n       cout << (s - (s + t - n) + 1) <<   n ; n     else n       cout << (t - (s + t - n) + 1) <<   n ; n     testcases--; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize(2) n #pragma G++ optimize(2) n using namespace std; n const int MAX_N = 1e5 + 5; n long long n, k; n long long a[MAX_N], b[MAX_N]; n bool use[MAX_N]; n long long cal(int i, long long x) { return a[i] - 3 * x * x + 3 * x - 1; } n long long check(long long delta) { n   long long sum = 0; n   for (register int i = 1; i <= n; ++i) { n     if (cal(i, 1) < delta) { n       b[i] = 0, use[i] = false; n       continue; n     } n     long long l = 1, r = a[i], mid, ans; n     while (l <= r) { n       mid = (l + r) >> 1; n       if (cal(i, mid) >= delta) n         l = mid + 1, ans = mid; n       else n         r = mid - 1; n     } n     b[i] = ans; n     use[i] = (cal(i, ans) == delta); n     sum += ans; n   } n   return sum; n } n int main() { n   scanf( %lld , &n), scanf( %lld , &k); n   for (register int i = 1; i <= n; ++i) scanf( %lld , &a[i]); n   long long l = -4e18, r = 1e9, mid, ans; n   while (l <= r) { n     mid = (l + r) >> 1; n     if (check(mid) >= k) n       l = mid + 1, ans = mid; n     else n       r = mid - 1; n   } n   k -= check(ans); n   for (int i = 1; i <= n && k < 0; i++) n     if (use[i] && b[i]) ++k, --b[i]; n   for (register int i = 1; i <= n; ++i) n     printf( %lld%c , b[i], i == n ?   n  :    ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 10; n int n; n long long k; n int a[N]; n int main() { n   cin >> n >> k; n   k -= n * 1LL * (n + 1) / 2; n   if (k < 0) { n     printf( -1 n ); n     return 0; n   } n   iota(a + 1, a + n + 1, 1); n   reverse(a + 1, a + n + 1); n   long long added = 0; n   for (int i = 1; i <= n / 2; i++) added += a[i] - i; n   long long diff = max(0LL, added - k); n   printf( %lld n , n * 1LL * (n + 1) / 2 + added - diff); n   for (int i = 1; i <= n / 2; i++) { n     if (diff >= a[i] - i) { n       diff -= (a[i] - i); n       swap(a[i], a[n + 1 - i]); n     } n   } n   if (diff == 1) { n     if (n % 2 == 1) { n       a[n / 2] = n / 2 + 1; n       a[n / 2 + 1] = n / 2; n       a[n / 2 + 2] = n / 2 + 2; n     } else { n       a[n / 2 - 1] = n / 2; n       a[n / 2] = n / 2 - 1; n       a[n / 2 + 1] = n / 2 + 2; n       a[n / 2 + 2] = n / 2 + 1; n     } n   } n   for (int i = 1; i <= n; i++) printf( %d  , i); n   printf(  n ); n   for (int i = 1; i <= n; i++) printf( %d  , a[i]); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   vector<pair<double, string> > p(6); n   p[0].first = 6 % 6; n   p[0].second =  1/1 ; n   p[1].first = 5 % 6; n   p[1].second =  5/6 ; n   p[2].first = 4 % 6; n   p[2].second =  2/3 ; n   p[3].first = 3 % 6; n   p[3].second =  1/2 ; n   p[4].first = 2 % 6; n   p[4].second =  1/3 ; n   p[5].first = 1 % 6; n   p[5].second =  1/6 ; n   vector<int> x(2); n   for (int i = 0; i < 2; i++) cin >> x[i]; n   sort(x.begin(), x.end()); n   double prob = (7 - x[1]) % 6; n   for (int i = 0; i < 6; i++) { n     if (p[i].first == prob && x[1] <= 6) { n       cout << p[i].second << endl; n       return 0; n     } n   } n   cout <<  0/1  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, b; n string a, c; n int main() { n   cin >> n; n   int l = -2e9, r = 2e9; n   for (; n--;) { n     cin >> a >> b >> c; n     if (a ==  >  && c ==  Y  || a ==  <=  && c ==  N ) n       l = max(l, b + 1); n     else if (a ==  >=  && c ==  Y  || a ==  <  && c ==  N ) n       l = max(l, b); n     else if (a ==  <  && c ==  Y  || a ==  >=  && c ==  N ) n       r = min(r, b - 1); n     else n       r = min(r, b); n   } n   if (l > r) n     puts( Impossible ); n   else n     printf( %d , l); n } n


#include <bits/stdc++.h> n using namespace std; n long long int n; n set<long long int> nos; n long long powmod(long long x, long long y, long long m) { n   long long res = 1LL; n   while (y) { n     if (y & 1) res = (res * x) % m; n     x = (x * x) % m; n     y /= 2; n   } n   return res; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cin >> n; n   long long int ans = 1; n   long long int buyt = 0; n   long long int selt = INT64_MAX; n   for (long long int i = 1; i <= n; i++) { n     string s; n     long long int val; n     cin >> s >> val; n     if (s ==  ADD ) n       nos.insert(val); n     else { n       nos.erase(val); n       if (buyt < val && selt > val) n         ans = (ans * 2) % 1000000007; n       else if (buyt == val || selt == val) n         ans = (ans * 1) % 1000000007; n       else n         ans = 0; n       auto it = nos.upper_bound(val); n       auto it1 = nos.lower_bound(val); n       if (it != nos.end()) n         selt = *it; n       else n         selt = INT64_MAX; n       if (it1 != nos.begin()) { n         it1--; n         buyt = *it1; n       } else { n         buyt = 0; n       } n     } n   } n   long long int m = 0; n   for (auto it = nos.begin(); it != nos.end(); it++) { n     long long int temp = *it; n     if (temp > buyt && temp < selt) m++; n   } n   ans = (ans * (m + 1)) % 1000000007; n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> cri[100100], notcri[100100]; n int totalnot = 0; n int crinum[100100] = {}, notcrinum[100100] = {}; n bool cand[100100]; n int candnum = 0; n int claim[100100]; n int main() { n   int N, M; n   scanf( %d%d , &N, &M); n   for (int i = 1; i <= N; i++) { n     scanf( %d , claim + i); n     int num = claim[i]; n     if (num > 0) { n       cri[num].push_back(i); n       crinum[num]++; n     } else { n       notcri[-num].push_back(i); n       notcrinum[-num]++; n       totalnot++; n     } n   } n   for (int c = 1; c <= N; c++) { n     int lie = crinum[c] + totalnot - notcrinum[c]; n     if (lie != M) { n       cand[c] = false; n       continue; n     } n     cand[c] = true; n     candnum++; n   } n   if (candnum == 1) { n     int criminal; n     for (int i = 1; i <= N; i++) n       if (cand[i] == true) criminal = i; n     for (int i = 1; i <= N; i++) { n       if (claim[i] > 0 && claim[i] == criminal) n         printf( Truth n ); n       else if (claim[i] > 0) n         printf( Lie n ); n       if (claim[i] < 0 && claim[i] == -criminal) n         printf( Lie n ); n       else if (claim[i] < 0) n         printf( Truth n ); n     } n   } else { n     for (int i = 1; i <= N; i++) { n       if (claim[i] > 0) { n         if (cand[claim[i]] == false) n           printf( Lie n ); n         else n           printf( Not defined n ); n       } else { n         if (cand[-claim[i]] == false) n           printf( Truth n ); n         else n           printf( Not defined n ); n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long M = 1000000007; n const double pi = 3.141592653589793238463; n bool vow(char ch) { n   return ch ==  a  || ch ==  e  || ch ==  i  || ch ==  o  || ch ==  u ; n } n bool isPrime(long long n) { n   for (long long i = 2; i * i <= n; i++) n     if (n % i == 0) return false; n   return true; n } n long long fast_exp(long long x, long long n) { n   long long ans = 1; n   while (n) { n     if (n & 1) { n       ans *= x; n       ans %= M; n     } n     n = n >> 1; n     x = (x * x) % M; n   } n   return ans; n } n long long inverse(long long x) { return fast_exp(x, M - 2); } n long long comb(long long n, long long r) { n   if (r == 0) n     return 1; n   else { n     long long ans = n % M; n     ans *= comb(n - 1, r - 1) % M; n     ans %= M; n     ans *= inverse(r) % M; n     ans %= M; n     return ans; n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   long long n, m, k; n   cin >> n >> m >> k; n   cout << (comb(n - 1, 2 * k) % M * comb(m - 1, 2 * k) % M) % M; n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } n int l3, l4, l5; n double calc3(double r) { n   double x = r / 2; n   double y = r / 6 * sqrt(3); n   double z = sqrt(r * r - x * x - y * y); n   double ret = z * r * r / 12 * sqrt(3); n   return ret; n } n double calc4(double r) { n   double x = r / 2; n   double y = r / 2; n   double z = sqrt(r * r - x * x - y * y); n   double ret = z * r * r / 3; n   return ret; n } n double calc5(double r) { n   double x = r / 2; n   double y = r / 2 * (sqrt(5) + 1) / sqrt(10 - 2 * sqrt(5)); n   double z = sqrt(r * r - x * x - y * y); n   double ret = z * r * r * 5 / 4 / sqrt(5 - sqrt(20)) / 3; n   return ret; n } n void run() { n   scanf( %d%d%d , &l3, &l4, &l5); n   printf( %.12lf n , calc3(l3) + calc4(l4) + calc5(l5)); n } n int main() { n   run(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 1e5 + 4; n string s, des[3]; n int n, q, next_occur[26][MAX], dp[255][255][255]; n void makeNO() { n   for (int j = 0; j < 26; j++) next_occur[j][n + 1] = next_occur[j][n] = n; n   for (int i = n - 1; i >= 0; i--) { n     for (int j = 0; j < 26; j++) { n       next_occur[j][i] = (s[i] -  a  == j ? i : next_occur[j][i + 1]); n     } n   } n } n void makeDp(int a, int b, int c) { n   int& val = dp[a][b][c]; n   val = n; n   if (a) val = min(val, next_occur[des[0][a - 1] -  a ][dp[a - 1][b][c] + 1]); n   if (b) val = min(val, next_occur[des[1][b - 1] -  a ][dp[a][b - 1][c] + 1]); n   if (c) val = min(val, next_occur[des[2][c - 1] -  a ][dp[a][b][c - 1] + 1]); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   ; n   cin >> n >> q >> s; n   makeNO(); n   dp[0][0][0] = -1; n   while (q--) { n     char o, c; n     int rel_id; n     cin >> o >> rel_id; n     rel_id--; n     if (o ==  + ) { n       cin >> c; n       des[rel_id] += c; n       int max0 = des[0].size(), max1 = des[1].size(), max2 = des[2].size(); n       int min0 = rel_id == 0 ? max0 : 0; n       int min1 = rel_id == 1 ? max1 : 0; n       int min2 = rel_id == 2 ? max2 : 0; n       for (int b = min1; b <= max1; b++) n         for (int a = min0; a <= max0; a++) n           for (int c = min2; c <= max2; c++) makeDp(a, b, c); n     } else { n       des[rel_id].pop_back(); n     } n     bool answer = dp[des[0].size()][des[1].size()][des[2].size()] < n; n     cout << (answer ?  YES n  :  NO n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n, k; n     cin >> n >> k; n     vector<int> s[k]; n     for (int i = 0; i < k; ++i) { n       int c; n       cin >> c; n       for (int j = 0; j < c; ++j) { n         int p; n         cin >> p; n         s[i].push_back(p); n       } n     } n     cout <<  ?   << n <<    ; n     for (int i = 1; i <= n; i++) cout << i <<    ; n     cout <<   n ; n     int max; n     cin >> max; n     int l = 1, r = n; n     while (l < r) { n       int mid = (l + r) / 2; n       cout <<  ?   << mid - l + 1 <<    ; n       for (int i = l; i <= mid; i++) cout << i <<    ; n       cout <<   n ; n       int temp; n       cin >> temp; n       if (temp == max) n         r = mid; n       else n         l = mid + 1; n     } n     int sind = 0; n     for (int i = 0; i < k; ++i) { n       for (int j : s[i]) { n         if (j == l) { n           sind = i; n           break; n         } n       } n     } n     int smax; n     cout <<  ?   << n - s[sind].size() <<    ; n     for (int i = 1; i <= n; i++) { n       bool flag = true; n       for (int j : s[sind]) n         if (j == i) flag = false; n       if (flag) cout << i <<    ; n     } n     cout <<   n ; n     cin >> smax; n     cout <<  !  ; n     for (int i = 0; i < k; i++) { n       if (i == sind) n         cout << smax <<    ; n       else n         cout << max <<    ; n     } n     cout <<   n ; n     string x; n     cin >> x; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1000 + 10; n struct fridge { n   int w, id; n } f[maxn]; n struct edge { n   int u, v; n } e[maxn]; n bool cmp(fridge a, fridge b) { return a.w < b.w; } n int n, m, a[maxn], ans, fd[maxn]; n int main() { n   int t; n   cin >> t; n   while (t--) { n     ans = 0; n     cin >> n >> m; n     for (int i = 1; i <= n; i++) { n       scanf( %d , &a[i]); n       ans += a[i]; n     } n     if (m < n || n == 2) { n       printf( -1 n ); n       continue; n     } n     cout << ans * 2 << endl; n     for (int i = 1; i < n; i++) { n       printf( %d %d n , i, i + 1); n     } n     printf( %d %d n , n, 1); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MX = 1e5 + 9; n long long par[MX], num[MX]; n void MS(long long v) { par[v] = v, num[v] = 1; } n long long FS(long long v) { n   if (par[v] == v) return v; n   return par[v] = FS(par[v]); n } n void US(long long a, long long b) { n   a = FS(a), b = FS(b); n   if (a == b) return; n   if (num[a] < num[b]) swap(a, b); n   par[b] = a; n   num[a] += num[b]; n } n bool lucky(string s) { n   for (long long i = 0; i < s.size(); i++) n     if (s[i] !=  4  && s[i] !=  7 ) return 0; n   return 1; n } n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   long long n; n   cin >> n; n   for (long long i = 1; i <= n; i++) MS(i); n   for (long long i = 1; i < n; i++) { n     long long u, v; n     string s; n     cin >> u >> v >> s; n     if (!lucky(s)) US(u, v); n   } n   long long ans = 0; n   for (long long i = 1; i <= n; i++) { n     long long sz = n - num[FS(i)]; n     ans += sz * (sz - 1); n   } n   cout << ans; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int main() { n   map<string, vector<string> > mp; n   vector<string> v; n   string pat; n   cin >> pat; n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     string tx; n     cin >> tx; n     v.push_back(tx); n   } n   sort(v.begin(), v.end()); n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < v[i].length(); j++) { n       string pr = v[i].substr(0, j + 1); n       mp[pr].push_back(v[i]); n     } n   } n   map<string, vector<string> >::iterator itr = mp.begin(); n   { n     if (mp.find(pat) != mp.end()) { n       cout << mp[pat][0] << endl; n     } else { n       cout << pat << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e6 + 7; n const int inf = INT_MAX; n const long long inff = 1e18; n const int mod = 1e9 + 7; n string s, t; n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   cout << 0 <<     << 0 << endl; n   cout << flush; n   cin >> s; n   if (n == 1) { n     cout << 0 <<     << 1000000000 <<     << 1000000000 <<     << 0 << endl; n     cout << flush; n     return 0; n   } n   cout << 1000000000 <<     << 1000000000 << endl; n   cout << flush; n   cin >> t; n   if (n == 2) { n     if (s == t) n       cout << 1 <<     << 0 <<     << 1000000000 <<     << 1000000000 - 1 n            << endl; n     else n       cout << 0 <<     << 1000000000 <<     << 1000000000 <<     << 0 << endl; n     cout << flush; n     return 0; n   } n   if (s != t) { n     int l = 1, r = 1000000000 - 1; n     for (int i = 1; i <= n - 2; i++) { n       int m = (l + r) >> 1; n       cout << m <<     << 0 << endl; n       cout << flush; n       string now; n       cin >> now; n       if (now == s) n         l = m + 1; n       else n         r = m - 1; n     } n     cout << 0 <<     << l <<     << l <<     << 0 << endl; n     cout << flush; n     return 0; n   } else { n     int l = 1, r = 1000000000 - 1; n     for (int i = 1; i <= n - 2; i++) { n       int m = (l + r) >> 1; n       cout << m <<     << 0 << endl; n       cout << flush; n       string now; n       cin >> now; n       if (now == s) n         l = m + 1; n       else n         r = m - 1; n     } n     cout << l <<     << 0 <<     << 1000000000 <<     << 1000000000 - l << endl; n     cout << flush; n     return 0; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a1, b1, a2, b2; n   scanf( %d%d%d%d , &a1, &b1, &a2, &b2); n   int i; n   int two1 = 0, three1 = 0, two2 = 0, three2 = 0; n   long long s1 = (long long)a1 * b1, s2 = (long long)a2 * b2; n   while (!(s1 % 2)) { n     two1++; n     s1 /= 2; n   } n   while (!(s1 % 3)) { n     three1++; n     s1 /= 3; n   } n   while (!(s2 % 2)) { n     two2++; n     s2 /= 2; n   } n   while (!(s2 % 3)) { n     three2++; n     s2 /= 3; n   } n   int t1 = 0, th1 = 0, t2 = 0, th2 = 0; n   if (s1 != s2) { n     printf( -1 n ); n     return 0; n   } n   int ans = 0; n   while (three1 > three2) { n     three1--; n     th1++; n     two1++; n     t1--; n     ans++; n     if (a1 % 3 == 0) { n       a1 /= 3; n       a1 *= 2; n     } else if (b1 % 3 == 0) { n       b1 /= 3; n       b1 *= 2; n     } n   } n   while (three1 < three2) { n     three2--; n     th2++; n     two2++; n     t2--; n     ans++; n     if (a2 % 3 == 0) { n       a2 /= 3; n       a2 *= 2; n     } else if (b2 % 3 == 0) { n       b2 /= 3; n       b2 *= 2; n     } else { n       puts( -1 ); n       return 0; n     } n   } n   while (two1 > two2) { n     two1--; n     t1++; n     ans++; n     if (a1 % 2 == 0) { n       a1 /= 2; n     } else if (b1 % 2 == 0) { n       b1 /= 2; n     } else { n       puts( -1 ); n       return 0; n     } n   } n   while (two1 < two2) { n     two2--; n     t2++; n     ans++; n     if (a2 % 2 == 0) { n       a2 /= 2; n     } else if (b2 % 2 == 0) { n       b2 /= 2; n     } else { n       puts( -1 ); n       return 0; n     } n   } n   cout << ans << endl; n   printf( %d %d n , a1, b1); n   printf( %d %d n , a2, b2); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     int i = -1, j = -1, k = -1, a[n]; n     for (int l = 0; l < n; l++) { n       cin >> a[l]; n     } n     for (int l = 1; l < n - 1; l++) { n       j = l; n       for (int m = j - 1; m >= 0; m--) { n         if (a[m] < a[j]) { n           i = m; n           break; n         } n       } n       for (int p = j + 1; p < n; p++) { n         if (a[p] < a[j]) { n           k = p; n           break; n         } n       } n       if (i != -1 && k != -1) { n         cout <<  YES n  << i + 1 <<     << j + 1 <<     << k + 1 <<   n ; n         break; n       } else { n         i = -1; n         k = -1; n       } n     } n     if (i == -1 or k == -1) { n       cout <<  NO  n            <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n int p[100005]; n int main(void) { n   int n, m, i, x; n   scanf( %d%d , &m, &n); n   for (i = 1; i <= n; i++) { n     scanf( %d , &x); n     x %= m; n     if (p[x] == 0) n       p[x] = 1; n     else { n       printf( %d n , i); n       return 0; n     } n   } n   return 0 * printf( -1 n ); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n, m; n   cin >> n >> m; n   map<string, string> mp; n   for (int i = 0; i < m; i++) { n     string s1, s2; n     cin >> s1 >> s2; n     if (s2.size() < s1.size()) n       mp[s1] = s2; n     else n       mp[s1] = s1; n   } n   for (int i = 0; i < n; i++) { n     string s; n     cin >> s; n     for (auto it = mp.begin(); it != mp.end(); it++) { n       if (it->first == s) { n         cout << it->second <<    ; n         break; n       } n     } n   } n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int Inf = 0x3f3f3f3f; n const long long INF = 0x3f3f3f3f3f3f3f3fll; n struct Shuffle { n   vector<int> S; n   Shuffle() {} n   Shuffle(int len) { n     S.resize(len); n     iota((S).begin(), (S).end(), 0); n   } n   friend inline Shuffle operator*(const Shuffle &a, const Shuffle &b) { n     static Shuffle res; n     res = Shuffle(((int)(a.S).size())); n     for (int i = 0; i < (((int)(a.S).size())); ++i) { n       res.S[i] = b.S[a.S[i]]; n     } n     return res; n   } n }; n int n, m; n char s[1 << 20], t[1 << 20]; n int main() { n   scanf( %s , s); n   n = strlen(s); n   scanf( %d , &m); n   for (int i = 1; i <= (m); ++i) { n     int k, d; n     scanf( %d%d , &k, &d); n     Shuffle a(n), b(n); n     for (int j = 0; j < (n); ++j) a.S[j] = (j + n - 1) % n; n     vector<vector<int> > ms(d); n     for (int j = 0; j < (k); ++j) ms[j % d].push_back(j); n     int cnt = 0; n     for (int j = 0; j < (d); ++j) n       for (int k = 0; k < (((int)(ms[j]).size())); ++k) { n         b.S[ms[j][k]] = cnt++; n       } n     a = b * a; n     b = Shuffle(n); n     int tms = n - k + 1; n     while (tms) { n       if (tms & 1) b = b * a; n       tms >>= 1; n       a = a * a; n     } n     for (int j = 0; j < (n); ++j) t[b.S[j]] = s[j]; n     for (int j = 0; j < (n); ++j) s[j] = t[(j + k - 1) % n]; n     printf( %s n , s); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool major, minor; n map<string, int> P; n string x, y, z; n int k1, k2; n void go(string x, string y, string z) { n   if (P[x] <= P[y]) n     k1 = P[y] - P[x]; n   else n     k1 = 12 - P[x] + P[y]; n   if (P[y] <= P[z]) n     k2 = P[z] - P[y]; n   else n     k2 = 12 - P[y] + P[z]; n   if (k1 == 4 && k2 == 3) n     major = true; n   else if (k1 == 3 && k2 == 4) n     minor = true; n } n int main(int argc, char *argv[]) { n   P[ C ] = 1; n   P[ C# ] = 2; n   P[ D ] = 3; n   P[ D# ] = 4; n   P[ E ] = 5; n   P[ F ] = 6; n   P[ F# ] = 7; n   P[ G ] = 8; n   P[ G# ] = 9; n   P[ A ] = 10; n   P[ B ] = 11; n   P[ H ] = 12; n   major = false; n   minor = false; n   cin >> x >> y >> z; n   go(x, y, z); n   go(x, z, y); n   go(y, x, z); n   go(y, z, x); n   go(z, x, y); n   go(z, y, x); n   if (major) n     cout <<  major  << endl; n   else if (minor) n     cout <<  minor  << endl; n   else n     cout <<  strange  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k; n vector<int> p, q; n vector<char> d; n int main() { n   cin >> n >> k; n   p.resize(n); n   q.resize(n); n   d.resize(n); n   for (int i = 0; i < n; i++) { n     cin >> d[i] >> p[i] >> q[i]; n   } n   map<int, int> b, s; n   for (int i = 0; i < n; i++) { n     if (d[i] ==  S ) { n       if (s.find(p[i]) == s.end()) n         s[p[i]] = q[i]; n       else n         s[p[i]] += q[i]; n     } else { n       if (b.find(-p[i]) == b.end()) n         b[-p[i]] = q[i]; n       else n         b[-p[i]] += q[i]; n     } n   } n   int cnt = s.size(); n   for (map<int, int>::reverse_iterator it = s.rbegin(); it != s.rend(); it++) { n     if (cnt <= k) { n       cout <<  S   << it->first <<     << it->second <<   n ; n     } n     cnt--; n   } n   cnt = 0; n   for (map<int, int>::iterator it = b.begin(); it != b.end(); it++) { n     if (cnt < k) { n       cout <<  B   << -it->first <<     << it->second <<   n ; n     } else n       break; n     cnt++; n   } n   cin >> n; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n struct node { n   T sum, val; n   int prior, cnt; n   node *l, *r; n   node() { n     cnt = 1; n     prior = (rand() << 16) + rand(); n     l = r = NULL; n   } n }; n template <class T> n class treap { n   node<T> *root; n   inline T getsum(node<T> *v) { n     if (v) n       return v->sum; n     else n       return *(new T); n   } n   inline int getcnt(node<T> *v) { n     if (v) n       return v->cnt; n     else n       return 0; n   } n   inline void upd(node<T> *v) { n     if (!v) return; n     v->sum = getsum(v->l) + getsum(v->r) + v->val; n     v->cnt = getcnt(v->l) + getcnt(v->r) + 1; n   } n   node<T> *merge(node<T> *v1, node<T> *v2) { n     if (!v1) n       return v2; n     else if (!v2) n       return v1; n     if (!(v1->val < v2->val)) swap(v1, v2); n     if (v1->prior > v2->prior) { n       v1->r = merge(v1->r, v2); n       upd(v1); n       return v1; n     } else { n       v2->l = merge(v1, v2->l); n       upd(v2); n       return v2; n     } n   } n   void split(T x, node<T> *tree, node<T> *&l, node<T> *&r) { n     if (!tree) { n       l = NULL, r = NULL; n       return; n     } else { n       if (tree->val < x) { n         split(x, tree->r, l, r); n         tree->r = l; n         upd(tree); n         l = tree; n       } else { n         split(x, tree->l, l, r); n         tree->l = r; n         upd(tree); n         r = tree; n       } n     } n   } n   void insert(node<T> *n, node<T> *&tree) { n     if (!tree) n       tree = n; n     else { n       if (n->prior > tree->prior) { n         split(n->val, tree, n->l, n->r); n         tree = n; n       } else if (n->val < tree->val) n         insert(n, tree->l); n       else n         insert(n, tree->r); n       upd(tree); n     } n   } n   void del(T c, node<T> *&tree) { n     if (!tree) return; n     if (tree->val.i == c.i && tree->val.c == c.c) { n       tree = merge(tree->l, tree->r); n     } else { n       if (c < tree->val) n         del(c, tree->l); n       else n         del(c, tree->r); n     } n     upd(tree); n   } n   T getminsum(int cnt, node<T> *tree) { n     if (!tree) n       return *(new T); n     else if (tree->cnt <= cnt) n       return tree->sum; n     else { n       T ans; n       ans = ans + getminsum(cnt, tree->l); n       if (cnt > getcnt(tree->l)) ans = ans + tree->val; n       if (cnt > getcnt(tree->l) + 1) n         ans = ans + getminsum(cnt - getcnt(tree->l) - 1, tree->r); n       return ans; n     } n   } n  n  public: n   void insert(T n) { n     node<T> *nn = new node<T>; n     nn->sum = nn->val = n; n     insert(nn, root); n   } n   void erase(T n) { del(n, root); } n   T getminsum(int cnt) { return getminsum(cnt, root); } n }; n struct cost { n   long long c; n   int i; n   cost() : c(0), i(0) {} n }; n inline cost operator+(const cost &a, const cost &b) { n   cost c; n   c.c = a.c + b.c; n   return c; n } n inline bool operator<(const cost &a, const cost &b) { n   if (a.c == b.c) n     return (a.i < b.i); n   else n     return (a.c < b.c); n } n const int maxn = 300000; n vector<pair<int, pair<int, int> > > levels; n int n, w, taskans[maxn]; n treap<cost> tree; n void insert(long long a, int b) { n   cost c; n   c.c = a, c.i = b; n   tree.insert(c); n } n int main() { n   cin >> n >> w; n   for (int i = 0; i < n; i++) { n     int a, b; n     cin >> a >> b; n     levels.push_back(make_pair(b, make_pair(a, i))); n   } n   sort(levels.begin(), levels.end()); n   long long ans = 1LL << 50; n   int maxi = 0; n   long long sum = 0; n   for (int i = 0; i < n; i++) n     insert(levels[i].second.first, levels[i].second.second); n   for (int i = 0; i <= n; i++) { n     if (w < i) break; n     if (w - i <= n) { n       long long summm = tree.getminsum(w - i).c; n       if (ans > sum + summm) ans = sum + summm, maxi = i; n     } n     if (i != n) { n       cost d; n       d.c = levels[i].second.first, d.i = levels[i].second.second; n       tree.erase(d); n       insert(levels[i].first - levels[i].second.first, levels[i].second.second); n       sum += d.c; n     } n   } n   cout << ans << endl; n   set<pair<int, int> > s; n   for (int i = 0; i < maxi; i++) n     taskans[levels[i].second.second]++, n         s.insert(make_pair(levels[i].first - levels[i].second.first, n                            levels[i].second.second)); n   for (int i = maxi; i < n; i++) n     s.insert(make_pair(levels[i].second.first, levels[i].second.second)); n   int j = 0; n   for (set<pair<int, int> >::iterator i = s.begin(); n        i != s.end() && j < w - maxi; i++, j++) { n     taskans[i->second]++; n   } n   for (int i = 0; i < n; i++) cout << taskans[i]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n = 0; n   char ch; n   vector<bool> v(26, 0); n   while (cin >> ch) { n     cin >> ch; n     if (ch ==  {  || ch ==  }  || ch ==  ,  || ch ==    ) continue; n     ch -= 97; n     if (!v[ch]) { n       v[ch] = true; n       n++; n     } n   } n   cout << n; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1000000007; n const int INF = 2e9; n const long long INFLL = 1e18; n const int MAX_N = 20; n int N, M, Q; n string str1[MAX_N + 1], str2[MAX_N + 1]; n int main() { n   scanf( %d%d , &N, &M); n   for (int i = 0; i < N; i++) { n     cin >> str1[i]; n   } n   for (int j = 0; j < M; j++) { n     cin >> str2[j]; n   } n   scanf( %d , &Q); n   for (int i = 1; i <= Q; i++) { n     int y; n     scanf( %d , &y); n     y--; n     cout << str1[y % N] << str2[y % M] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int a, b, c, d, e, f, L; n   scanf( %d%d%d%d%d%d , &a, &b, &c, &d, &e, &f); n   L = a + b + c; n   printf( %d n , L * L - a * a - c * c - e * e); n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e9 + 7; n const int MOD = 1e9 + 7; n const int MAXN = 1e6 + 3; n long long gcd(long long a, long long b) { n   if (b == 0) n     return a; n   else n     return gcd(b, a % b); n } n long long power(long long x, long long y) { n   long long res = 1; n   while (y > 0) { n     if (y & 1) res = res * x; n     y = y >> 1; n     x = x * x; n   } n   return res; n } n long long n; n const int maxn = 1e3 + 3; n long long dp1[2 * maxn][2 * maxn]; n long long dp2[2 * maxn][2 * maxn]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n; n   n *= 2; n   dp1[0][0] = 0ll; n   dp2[0][0] = 1ll; n   for (long long i = 1; i <= n; i++) { n     for (long long j = 0; j <= i; j++) { n       if (j == 0) { n         dp2[i][j] = (1 + dp1[i - 1][j + 1]) % MOD; n         dp1[i][j] = max(dp1[i - 1][j + 1], dp2[i - 1][j + 1]); n       } else if (j == i) { n         dp2[i][j] = (1 + dp1[i - 1][j - 1]) % MOD; n         dp1[i][j] = max(dp1[i - 1][j - 1], dp2[i - 1][j - 1]); n       } else { n         dp2[i][j] = (1 + dp1[i - 1][j - 1] + dp1[i - 1][j + 1]) % MOD; n         dp1[i][j] = max(dp1[i - 1][j - 1] + dp2[i - 1][j + 1], n                         dp1[i - 1][j + 1] + dp2[i - 1][j - 1]) % n                     MOD; n       } n     } n   } n   cout << dp1[n][0] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int inX[1000001] = {0}; n int used[1000001] = {0}; n vector<int> ans; n int main() { n   int n; n   scanf( %d , &n); n   vector<int> given(n, 0); n   for (int i = 0; i < n; i++) { n     scanf( %d , &given[i]); n     inX[given[i]] = 1; n     used[given[i]] = 1; n   } n   vector<int> pairLeft; n   int reqdPairs = 0; n   for (int i = 1; i <= 1000000; i++) { n     if (inX[i]) { n       if (inX[1000000 + 1 - i] == 0) { n         used[1000000 + 1 - i] = 1; n         ans.push_back(1000000 + 1 - i); n       } else { n         reqdPairs += 1; n       } n     } else { n       if (inX[i] == inX[1000000 + 1 - i]) pairLeft.push_back(i); n     } n   } n   for (int i = 0; 2 * i < reqdPairs; i++) { n     ans.push_back(pairLeft[i]); n     ans.push_back(1000000 + 1 - pairLeft[i]); n   } n   printf( %d n , (int)ans.size()); n   for (int i = 0; i < (int)ans.size(); i++) printf( %d  , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   int a, b, c; n   cin >> n; n   int tong = 0, tong1 = 0, tong2 = 0; n   for (int i = 0; i < n; i++) { n     cin >> a >> b >> c; n     tong += a; n     tong1 += b; n     tong2 += c; n   } n   if (tong == 0 && tong1 == 0 && tong2 == 0) n     cout <<  YES ; n   else n     cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(nullptr); n   int n, k; n   cin >> n >> k; n   if ((n - 1) / 2 < k) n     cout << -1 <<   n ; n   else { n     cout << (n * k) <<   n ; n     ; n     for (int i = 1; i <= n; i++) n       for (int j = 0; j < k; j++) cout << i <<     << (i + j) % n + 1 <<   n ; n     ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[1003], dp[1003][1003], ans = 1e9 + 10, fr[1003][1003], now; n vector<pair<int, int> > res; n void dfs(int x, int wants) { n   if (x == 1) return; n   if (x == wants) { n     res.push_back(make_pair(x - 1, fr[x][wants])); n     dfs(x - 2, fr[x][wants]); n   } n   if (wants + 1 == x) { n     res.push_back(make_pair(x, fr[x][wants])); n     dfs(x - 2, fr[x][wants]); n   } n   if (x != wants && x != wants + 1) { n     res.push_back(make_pair(x, x - 1)); n     dfs(x - 2, fr[x][wants]); n   } n } n int main() { n   for (int i = 0; i < 1003; i++) n     for (int j = 0; j < 1003; j++) dp[i][j] = 1e9 + 10; n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   if (n == 1) { n     cout << a[1]; n     cout <<   n1 ; n     return 0; n   } n   if (n == 2) { n     cout << max(a[1], a[2]); n     cout <<   n1 2 ; n     return 0; n   } n   dp[1][1] = 0; n   for (int i = 3; i <= n; i += 2) n     for (int j = 1; j < i - 1; j++) { n       if (dp[i][j] > dp[i - 2][j] + max(a[i], a[i - 1])) n         dp[i][j] = dp[i - 2][j] + max(a[i], a[i - 1]), fr[i][j] = j; n       if (dp[i][i - 1] > dp[i - 2][j] + max(a[i], a[j])) n         dp[i][i - 1] = dp[i - 2][j] + max(a[i], a[j]), fr[i][i - 1] = j; n       if (dp[i][i] > dp[i - 2][j] + max(a[i - 1], a[j])) n         dp[i][i] = dp[i - 2][j] + max(a[i - 1], a[j]), fr[i][i] = j; n     } n   if (n % 2 == 1) { n     for (int i = 1; i <= n; i++) n       if (ans > dp[n][i] + a[i]) ans = dp[n][i] + a[i], now = i; n   } else { n     for (int i = 1; i < n; i++) n       if (ans > dp[n - 1][i] + max(a[i], a[n])) n         ans = dp[n - 1][i] + max(a[i], a[n]), now = i; n   } n   cout << ans <<   n ; n   if (n % 2 == 1) { n     dfs(n, now); n     reverse(res.begin(), res.end()); n     for (int i = 0; i < res.size(); i++) n       cout << res[i].first <<     << res[i].second <<   n ; n     cout << now <<   n ; n   } else { n     dfs(n - 1, now); n     reverse(res.begin(), res.end()); n     for (int i = 0; i < res.size(); i++) n       cout << res[i].first <<     << res[i].second <<   n ; n     cout << now <<     << n <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int T; n   cin >> T; n   for (int i = 0; i < T; i++) { n     int N; n     cin >> N; n     vector<int> vec(N); n     for (int j = 0; j < N; j++) { n       cin >> vec.at(j); n     } n     if (vec.at(0) + vec.at(1) <= vec.at(N - 1)) { n       cout << 1 <<     << 2 <<     << N << endl; n     } else { n       cout << -1 << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long t, n, a, b, i, same, unique, ans; n   cin >> t; n   while (t--) { n     cin >> n; n     same = 0; n     unique = 0; n     unordered_map<long long, long long> m; n     for (i = 0; i < n; i++) { n       cin >> a; n       m[a]++; n       if (m[a] == 1) { n         unique++; n       } n       same = max(same, m[a]); n     } n     if (same < unique + 1) unique--; n     ans = min(same, unique); n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int gcd(int a, int b) { n   if (a > b) { n     swap(a, b); n   } n   if (a == 0) return b; n   return gcd(b % a, a); n } n int invertBits(int num) { n   int x = log2(num) + 1; n   for (int i = 0; i < x; i++) num = (num ^ (1 << i)); n   return num; n } n string add(string f, string s) { n   reverse(f.begin(), f.end()); n   reverse(s.begin(), s.end()); n   int i = 0, j = 0; n   string t =   ; n   int carry = 0; n   while (i < f.size() && j < s.size()) { n     int k = (f[i] -  0 ) + (s[j] -  0 ) + carry; n     carry = k / 10; n     int sum = k % 10; n     t += (sum +  0 ); n     i++; n     j++; n   } n   while (i < f.size()) { n     int k = (f[i] -  0 ) + carry; n     carry = k / 10; n     int sum = k % 10; n     t += (sum +  0 ); n     i++; n   } n   while (j < s.size()) { n     int k = (s[j] -  0 ) + carry; n     carry = k / 10; n     int sum = k % 10; n     t += (sum +  0 ); n     j++; n   } n   if (carry) { n     t += (carry +  0 ); n   } n   reverse(t.begin(), t.end()); n   return t; n } n int mulmod(int a, int b, int mod) { n   int res = 0; n   a = a % mod; n   while (b > 0) { n     if (b % 2 == 1) res = (res + a) % mod; n     a = (a * 2) % mod; n     b /= 2; n   } n   return res % mod; n } n int main() { n   long long int n; n   long long int m = 998244353; n   cin >> n; n   long long int i, a[n]; n   for (i = 0; i < n; i++) { n     cin >> a[i]; n   } n   long long int ans = 0; n   for (i = 0; i < n; i++) { n     long long int k = a[i]; n     long long int mul = 1; n     while (k != 0) { n       long long int r = k % 10; n       ans = (ans % m + ((r % m) * (mul % m) * (n % m)) % m) % m; n       mul = mul * 10; n       mul = mul % m; n       ans = (ans % m + ((r % m) * (mul % m) * (n % m)) % m) % m; n       mul = mul * 10; n       mul = mul % m; n       k = k / 10; n     } n   } n   cout << (ans + m) % m; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long const mod = 1e9 + 7; n long long const long_max = LONG_LONG_MAX; n int interact(int i, int j) { n   cout <<  ?   << i + 1 <<     << j + 1 <<   n ; n   ; n   cout.flush(); n   int x; n   cin >> x; n   return x; n } n void solve() { n   int n; n   cin >> n; n   int ind = 0; n   vector<int> b(n); n   for (int i = 1; i < n; i++) { n     int x = interact(ind, i); n     int y = interact(i, ind); n     if (x < y) { n       b[i] = y; n     } else { n       b[ind] = x; n       ind = i; n     } n   } n   b[ind] = n; n   cout <<  !  ; n   for (auto i : b) cout << i <<    ; n   cout <<   n ; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int t = 1, p; n   p = t; n   t = 1; n   while (p--) { n     solve(); n     t++; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long read() { n   char c = getchar(); n   long long u = 0, f = 1; n   while (c !=  -  && (c <  0  ||  9  < c)) c = getchar(); n   if (c ==  - ) { n     f = -1; n     c = getchar(); n   } n   while ( 0  <= c && c <=  9 ) { n     u = u * 10 + c -  0 ; n     c = getchar(); n   } n   return u * f; n } n inline char readc() { n   for (;;) { n     char c = getchar(); n     if ( A  <= c && c <=  Z  ||  a  <= c && c <=  z ) return c; n   } n } n inline int read_digit() { n   for (;;) { n     char c = getchar(); n     if ( 0  <= c && c <=  9 ) return c -  0 ; n   } n } n inline int max(int a, int b) { return a > b ? a : b; } n inline int min(int a, int b) { return a < b ? a : b; } n template <typename T> n inline void updmax(T &a, T b) { n   if (b > a) a = b; n } n template <typename T> n inline void updmin(T &a, T b) { n   if (b < a) a = b; n } n inline void swap(int &a, int &b) { n   int t = a; n   a = b; n   b = t; n } n inline void fileio(string s) { n   if (s ==  txt ) { n     freopen( input.txt ,  r , stdin); n     freopen( output.txt ,  w , stdout); n   } else { n     freopen((s +  .in ).c_str(),  r , stdin); n     freopen((s +  .out ).c_str(),  w , stdout); n   } n } n char a[1000010]; n map<int, int> cnt; n int main() { n   int n; n   scanf( %d , &n); n   long long ans = 0; n   for (int i = 1; i <= n; i++) { n     scanf( %s , a); n     int l = strlen(a); n     int b[26] = {0}; n     for (int j = 0; j < l; j++) b[a[j] -  a ]++; n     int num = 0, id = 1; n     for (int j = 0; j < 26; j++) { n       if (b[j] % 2 == 1) num += id; n       id <<= 1; n     } n     ans += cnt[num]; n     for (int j = 0; j < 26; j++) { n       int p = (1 << j); n       if (num & p) n         ans += cnt[num - p]; n       else n         ans += cnt[num + p]; n     } n     cnt[num]++; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_N = 500 * 1000 + 10; n vector<int> adj[MAX_N], vec[MAX_N]; n int n, m, cnt; n queue<int> q; n set<int> s; n void readInput() { n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     int u, v; n     cin >> u >> v; n     u--, v--; n     adj[u].push_back(v); n     adj[v].push_back(u); n   } n   for (int i = 0; i < n; i++) sort(adj[i].begin(), adj[i].end()), s.insert(i); n } n void solve() { n   while (!s.empty()) { n     q.push(*s.begin()), s.erase(s.begin()); n     while (!q.empty()) { n       int u = q.front(); n       q.pop(), vec[cnt].push_back(u); n       int cur = 0; n       vector<int> tmp; n       for (auto v : s) { n         while (cur < adj[u].size() && adj[u][cur] < v) cur++; n         if (cur == adj[u].size() || adj[u][cur] != v) tmp.push_back(v); n       } n       for (auto v : tmp) q.push(v), s.erase(v); n     } n     cnt++; n   } n } n void writeOutput() { n   cout << cnt << endl; n   for (int i = 0; i < cnt; i++) { n     cout << vec[i].size() <<    ; n     for (auto u : vec[i]) cout << u + 1 <<    ; n     cout << endl; n   } n } n int main() { n   ios_base ::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); n   readInput(), solve(), writeOutput(); n   return 0; n } n


#include <bits/stdc++.h> n int N, K, A, B, Q, a[200001], TA[524288], TB[524288]; n void P(int x) { n   int p = 1, l = 1, r = N; n   while (l < r) { n     int m = l + r >> 1; n     if (x <= m) n       r = m, p <<= 1; n     else n       l = m + 1, p = p << 1 | 1; n   } n   TA[p] = std::min(a[x], A); n   TB[p] = std::min(a[x], B); n   while (p > 1) { n     p >>= 1; n     TA[p] = TA[p << 1] + TA[p << 1 | 1]; n     TB[p] = TB[p << 1] + TB[p << 1 | 1]; n   } n } n int G(int *T, int p, int l, int r, int L, int R) { n   if (L <= l && r <= R) return T[p]; n   int m = l + r >> 1; n   if (R <= m) return G(T, p << 1, l, m, L, R); n   if (L > m) return G(T, p << 1 | 1, m + 1, r, L, R); n   return G(T, p << 1, l, m, L, R) + G(T, p << 1 | 1, m + 1, r, L, R); n } n int main() { n   scanf( %d%d%d%d%d , &N, &K, &A, &B, &Q); n   while (Q--) { n     int ty, u, w; n     scanf( %d%d , &ty, &u); n     if (ty == 1) { n       scanf( %d , &w); n       a[u] += w; n       P(u); n     } else n       printf( %d n , (u > 1 ? G(TB, 1, 1, N, 1, u - 1) : 0) + n                          (u > N - K ? 0 : G(TA, 1, 1, N, u + K, N))); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string n; n int ar[123], sum = 0; n int main() { n   getline(cin, n); n   for (int i = 1; i < n.size(); i += 3) { n     ar[(int)n[i]] = 1; n   } n   for (int i = 0; i < 123; i++) { n     sum += ar[i]; n   } n   cout << sum; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n using namespace std; n long long int **sparse; n void buildSparseTable(long long int arr[], long long int n, n                       long long int **sparse) { n   for (long long int i = 0; i < n; i++) { n     sparse[i][0] = i; n   } n   for (long long int j = 1; pow(2, j) <= n; j++) { n     for (long long int i = 0; i + pow(2, j) - 1 < n; i++) { n       if (arr[sparse[i][j - 1]] >= n           arr[sparse[i + (long long int)pow(2, j - 1)][j - 1]]) { n         sparse[i][j] = sparse[i][j - 1]; n       } else { n         sparse[i][j] = sparse[i + (long long int)pow(2, j - 1)][j - 1]; n       } n     } n   } n } n long long int query(long long int l, long long int r, long long int **sparse, n                     long long int arr[]) { n   long long int len = r - l + 1; n   long long int k = (log(len) / log(2.0)); n   long long int m; n   if (arr[sparse[l][k]] >= arr[sparse[l + len - (long long int)pow(2, k)][k]]) { n     return sparse[l][k]; n   } else n     return sparse[l + len - (long long int)pow(2, k)][k]; n } n int main() { n   long long int n; n   cin >> n; n   long long int arr[n]; n   sparse = new long long int *[n]; n   for (long long int i = 0; i < n; i++) { n     sparse[i] = new long long int[(long long int)floor(log(n) / log(2.0)) + 1]; n   } n   arr[n - 1] = n - 1; n   for (long long int i = 0; i < n - 1; i++) { n     cin >> arr[i]; n     arr[i]--; n   } n   buildSparseTable(arr, n, sparse); n   long long int dp[n]; n   dp[n - 1] = 0; n   long long int sum = 0; n   for (long long int i = n - 2; i >= 0; i--) { n     long long int m = query(i + 1, arr[i], sparse, arr); n     dp[i] = dp[m] - (arr[i] - m) + n - 1 - i; n     sum += dp[i]; n   } n   cout << sum << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxN = 1e5 + 21; n long long n, m, a[maxN], cnt[maxN], ans; n bool p[maxN], f = true; n vector<long long> g[maxN]; n int32_t main() { n   cin >> n >> m; n   for (long long i = 0; i < m; i++) { n     long long x, y; n     cin >> x >> y; n     g[x].push_back(y); n     g[y].push_back(x); n   } n   queue<long long> q; n   for (long long i = 1; i <= n; i++) { n     cin >> a[i]; n     if (!a[i]) q.push(i); n   } n   while (!q.empty()) { n     long long i = q.front(); n     q.pop(); n     if (a[i] == cnt[i]) { n       cnt[i]++, p[i] = true, ans++; n       for (long long j = 0; j < g[i].size(); j++) { n         long long u = g[i][j]; n         cnt[u]++; n         if (a[u] == cnt[u]) q.push(u); n       } n     } n   } n   for (long long i = 1; i <= n; i++) n     if (a[i] == cnt[i]) f = false; n   if (!f) n     cout << -1 << endl; n   else { n     cout << ans << endl; n     for (long long i = 1; i <= n; i++) { n       if (p[i]) cout << i <<    ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, h, a, b, k, t1, f1, t2, f2; n   scanf( %lld%lld%lld%lld%lld , &n, &h, &a, &b, &k); n   while (k--) { n     scanf( %lld%lld%lld%lld , &t1, &f1, &t2, &f2); n     if (t1 == t2) n       printf( %lld n , abs(f2 - f1)); n     else { n       long long int ans = abs(t1 - t2); n       if (f1 > b) { n         ans += f1 - b; n         f1 = b; n       } else if (f1 < a) { n         ans += a - f1; n         f1 = a; n       } n       ans += abs(f2 - f1); n       printf( %lld n , ans); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N; n long long a[100013]; n long long mpow(long long x, int k) { n   long long res = 1; n   while (k) { n     if (k & 1) { n       res *= x; n       res %= 1000000007; n     } n     x *= x; n     x %= 1000000007; n     k >>= 1; n   } n   return res; n } n int main() { n   scanf( %d , &N); n   long long a = 1, b = -1; n   for (int i = int(0); i < int(N); i++) { n     long long x; n     scanf( %I64d , &x); n     a *= x % (1000000007 - 1); n     a %= (1000000007 - 1); n     if ((x & 1) == 0) { n       b = 1; n     } n   } n   if (a == 0ll) { n     a = 1000000007 - 1; n   } n   a--; n   a = mpow(2, a); n   long long x = a + b; n   x *= mpow(3, 1000000007 - 2); n   x %= 1000000007; n   printf( %I64d/%I64d n , x, a); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int l[200010], r[200010], a[200010], b[200010], n, m; n long long sum = 1; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < n; i++) scanf( %d , &a[i]); n   for (int i = 0; i < m; i++) scanf( %d , &b[i]); n   int p = n - 1; n   for (int i = m - 1; i >= 0; i--) { n     while (p >= 0 && a[p] != b[i] && a[p] > b[i]) p--; n     r[i] = p; n     while (p >= 0 && a[p] >= b[i]) p--; n     l[i] = p; n     if (i > 0) n       sum = 1LL * sum * (r[i] - l[i]) % 998244353; n     else { n       if (l[i] != -1 || r[i] == -1) sum = 0; n     } n   } n   printf( %lld n , sum); n } n


#include <bits/stdc++.h> n using namespace std; n int main(int argc, char **argv) { n   int n; n   unsigned long long ans; n   string str; n   cin >> n >> str; n   map<int, vector<int> > myMap; n   myMap[0].push_back(0); n   int cur = 0; n   ans = 0; n   for (int i = 0; i < str.size(); i++) { n     if (str[i] ==  0 ) { n     } else { n       cur++; n     } n     if (cur >= n && myMap.count(cur - n) != 0) { n       ans += myMap[cur - n].size(); n     } n     myMap[cur].push_back(i); n   } n   cout << ans << endl; n   return 0; n } n