#include <bits/stdc++.h> n using namespace std; n const int MAX_N = (int)2e5 + 100; n const int mod = (int)1e9 + 7; n int cnt[MAX_N]; n inline int mul(int x, int y) { return (long long)x * y % mod; } n inline int powMod(int x, int n) { n   int ret = 1; n   while (n > 0) { n     if (n & 1) ret = mul(ret, x); n     x = mul(x, x); n     n >>= 1; n   } n   return ret; n } n int main() { n   int m; n   scanf( %d , &m); n   memset(cnt, 0, sizeof(cnt)); n   for (int i = 0; i < m; ++i) { n     int p; n     scanf( %d , &p); n     ++cnt[p]; n   } n   vector<int> toMul; n   for (int i = 0; i < MAX_N; ++i) n     if (cnt[i] > 0) { n       toMul.push_back(cnt[i] + 1); n     } n   bool perfectSq = true; n   for (int i = 0; i < toMul.size(); ++i) { n     if (toMul[i] % 2 == 0) { n       toMul[i] /= 2; n       perfectSq = false; n       break; n     } n   } n   int p1 = 1; n   for (int i = 0; i < toMul.size(); ++i) { n     p1 = (long long)p1 * toMul[i] % (mod - 1); n   } n   if (!perfectSq) { n     int num = 1; n     for (int i = 0; i < MAX_N; ++i) n       if (cnt[i] > 0) { n         num = mul(num, powMod(i, cnt[i])); n       } n     printf( %d n , powMod(num, p1)); n   } else { n     int root = 1; n     for (int i = 0; i < MAX_N; ++i) n       if (cnt[i] > 0) { n         root = mul(root, powMod(i, cnt[i] / 2)); n       } n     printf( %d n , powMod(root, p1)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   bool vi[1002]; n   memset(vi, 0, sizeof vi); n   int a = 0, i = 0; n   while (i < n) { n     a = (a + i % n) % n; n     if (vi[a]) { n       cout <<  NO  << endl; n       return 0; n     }; n     vi[a] = 1; n     i++; n   } n   cout <<  YES  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int diez(int x) { n   int x1 = 0; n   int s = 0; n   while (x != 0) { n     x1 = x % 10; n     x = x / 10; n     s += x1; n   } n   if (s == 10) n     return (1); n   else n     return (0); n } n int main() { n   int n; n   while (cin >> n) { n     int s = 18; n     int s1 = 0; n     while (n) { n       s++; n       if (diez(s) == 1) { n         s1 = s; n         n--; n       } n     } n     cout << s1 << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long op[1000010]; n int main() { n   op[1] = 1LL; n   for (int i = 2; i <= 1000000; i++) { n     op[i] = op[i - 1] * 2 + 1; n     op[i] %= 1000000007; n   } n   string s; n   cin >> s; n   long long cnt = 0; n   int a = 0; n   int b = 0; n   for (int i = 0; i < s.size(); i++) { n     if (s[i] ==  a ) n       a++; n     else { n       cnt += op[a]; n       cnt %= 1000000007; n     } n   } n   cout << cnt << endl; n } n


#include <bits/stdc++.h> n using namespace std; n string rows[1000]; n int dist[1000][1000]; n bool disc[1000][1000]; n int r, c; n void bfs(int row, int col) { n   queue<pair<int, int> > q; n   q.push(make_pair(row, col)); n   while (!q.empty()) { n     pair<int, int> cur = q.front(); n     disc[cur.first][cur.second] = true; n     q.pop(); n     if (cur.first > 0 && !disc[cur.first - 1][cur.second] && n         rows[cur.first - 1][cur.second] !=  T ) { n       dist[cur.first - 1][cur.second] = dist[cur.first][cur.second] + 1; n       disc[cur.first - 1][cur.second] = true; n       q.push(make_pair(cur.first - 1, cur.second)); n     } n     if (cur.first < r - 1 && !disc[cur.first + 1][cur.second] && n         rows[cur.first + 1][cur.second] !=  T ) { n       dist[cur.first + 1][cur.second] = dist[cur.first][cur.second] + 1; n       disc[cur.first + 1][cur.second] = true; n       q.push(make_pair(cur.first + 1, cur.second)); n     } n     if (cur.second < c - 1 && !disc[cur.first][cur.second + 1] && n         rows[cur.first][cur.second + 1] !=  T ) { n       dist[cur.first][cur.second + 1] = dist[cur.first][cur.second] + 1; n       disc[cur.first][cur.second + 1] = true; n       q.push(make_pair(cur.first, cur.second + 1)); n     } n     if (cur.second > 0 && !disc[cur.first][cur.second - 1] && n         rows[cur.first][cur.second - 1] !=  T ) { n       dist[cur.first][cur.second - 1] = dist[cur.first][cur.second] + 1; n       disc[cur.first][cur.second - 1] = true; n       q.push(make_pair(cur.first, cur.second - 1)); n     } n   } n } n int main(void) { n   cin >> r >> c; n   for (int i = 0; i < r; i++) cin >> rows[i]; n   memset(disc, 0, sizeof(disc)); n   for (int i = 0; i < r; i++) n     for (int j = 0; j < c; j++) n       if (rows[i][j] ==  E ) bfs(i, j); n   pair<int, int> start; n   for (int i = 0; i < r; i++) { n     for (int j = 0; j < c; j++) { n       if (rows[i][j] ==  S ) { n         start.first = i; n         start.second = j; n       } n     } n   } n   int kiek = 0; n   for (int i = 0; i < r; i++) { n     for (int j = 0; j < c; j++) { n       if (isdigit(rows[i][j]) && n           dist[i][j] <= dist[start.first][start.second] && disc[i][j]) { n         kiek += int(rows[i][j] -  0 ); n       } n     } n   } n   cout << kiek << endl; n } n


#include <bits/stdc++.h> n using namespace std; n constexpr int mod = (int)998244353; n long long mod_mul(long long a, long long b) { n   a = a % mod; n   b = b % mod; n   return (((a * b) % mod) + mod) % mod; n } n long long mod_add(long long a, long long b) { n   a = a % mod; n   b = b % mod; n   return (((a + b) % mod) + mod) % mod; n } n long long binpow(long long a, long long b, long long mod) { n   long long res = 1; n   a %= mod; n   while (b > 0) { n     if (b & 1) res = (res * a) % mod; n     a = (a * a) % mod; n     b >>= 1; n   } n   return res; n } n long long binmul(long long a, long long b) { n   long long res = 0; n   a %= mod; n   while (b > 0) { n     if (b & 1) res = (res + a) % mod; n     a = (a + a) % mod; n     b >>= 1; n   } n   return res; n } n long long area(pair<int, int> a, pair<int, int> b, pair<int, int> c) { n   return abs(a.first * b.second + b.first * c.second + c.first * a.second - n              a.second * b.first - b.second * c.first - c.second * a.first); n } 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 lcm(long long a, long long b) { return a / gcd(a, b) * b; } n long long min(long long a, long long b) { n   if (a < b) { n     return a; n   } n   return b; n } n long long max(long long a, long long b) { n   if (a > b) { n     return a; n   } n   return b; n } n double intlog(long long n, long long base) { n   return (double)log(n) / log(base); n } n long long t, m, temp, temp2, root, q, k, i, j, r, u, v, w, n, l, p; n template <typename A> n ostream &operator<<(ostream &cout, vector<A> const &v); n template <typename A, typename B> n ostream &operator<<(ostream &cout, pair<A, B> const &p) { n   return cout <<  (  << p.first <<  ,   << p.second <<  ) ; n } n template <typename A> n ostream &operator<<(ostream &cout, vector<A> const &v) { n   cout <<  [ ; n   for (int i = 0; i < v.size(); i++) { n     if (i) cout <<  ,  ; n     cout << v[i]; n   } n   return cout <<  ] ; n } n template <typename A, typename B> n istream &operator>>(istream &cin, pair<A, B> &p) { n   cin >> p.first; n   return cin >> p.second; n } n long long mid; n int solve(int i, int x, vector<long long> &a, vector<vector<int> > &dp) { n   if (x < 0 || x > mid) { n     return 0; n   } n   if (i == (int)a.size()) { n     return 1; n   } n   int &res = dp[i][x]; n   if (res != -1) return res; n   if (dp[i][x] != -1) return dp[i][x]; n   int ans = 0; n   ans = ans | solve(i + 1, x - a[i], a, dp); n   ans = ans | solve(i + 1, x + a[i], a, dp); n   return res = ans; n } n bool solve(int mid, vector<long long> &a) { n   vector<bool> dp(mid + 1, 1); n   for (int i = 0; i < (int)a.size(); i++) { n     vector<bool> temp(mid + 1, 0); n     bool flag = 0; n     for (j = 0; j <= mid; j++) { n       if (j + a[i] <= mid) { n         temp[j] = temp[j] || dp[j + a[i]]; n       } n       if (j - a[i] >= 0) { n         temp[j] = temp[j] || dp[j - a[i]]; n       } n       flag = flag || temp[j]; n     } n     if (flag == 0) return 0; n     dp = temp; n   } n   return 1; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> t; n   while (t--) { n     cin >> n; n     vector<long long> a(n); n     for (i = 0; i < n; i++) { n       cin >> a[i]; n     } n     int l = 1; n     int r = 2000; n     long long res = 2000; n     while (l <= r) { n       mid = (r - l) / 2 + l; n       if (solve(mid, a)) { n         r = mid - 1; n         res = min(res, mid); n       } else { n         l = mid + 1; n       } n     } n     cout << res <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n bool flag = 0; n int work(int a, int n, int p) { n   int t = a * 1ll * n / p; n   if (!t) { n     if (!flag) return a; n     return max(a, p - a * n); n   } n   flag = 1; n   int a1, n1, p1; n   a1 = min(p % a, a - (p % a)); n   n1 = t; n   p1 = a; n   if (a * 1ll * n % p < a * 1ll * (p / a - 1)) n1--; n   return work(a1, n1, p1); n } n int main() { n   int t; n   scanf( %d , &t); n   while (t--) { n     int a, n, p, h; n     scanf( %d%d%d%d , &a, &n, &p, &h); n     flag = 0; n     if (work(a % p, n, p) <= h) n       printf( YES n ); n     else n       printf( NO n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n  n using namespace std; n  n #define all(x) (x).begin(), (x).end() n #define fast ios::sync_with_stdio(false);cin.tie(0); n typedef long long ll; n typedef long double ld; n typedef unsigned long long ull; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n  n map<pair<int,int>,bool> e[3]; n  n int main(){ n  fast n  vector<int> n(4); n  for(int i=0;i<4;i++)cin>>n[i]; n  vector<vector<int>> c(4); n  for(int i=0;i<4;i++){ n   c[i].resize(n[i]); n   for(int j=0;j<n[i];j++)cin>>c[i][j]; n  } n  for(int i=0;i<3;i++){ n   int m; n   cin>>m; n   for(int j=0;j<m;j++){ n    int a,b; n    cin>>a>>b; n    a--;b--; n    // cerr << i <<     << a <<     << b <<   n ; n    e[i][{a,b}] = true; n   } n  } n  vector<pair<ll,int>> dp,nxt; n  for(int i=0;i<n[3];i++)dp.push_back({c[3][i],i}); n  for(int lvl=2;lvl>=0;lvl--){ n   sort(all(dp)); n   for(int i=0;i<n[lvl];i++){ n    for(int j=0;j<dp.size();j++){ n     if(e[lvl][{i,dp[j].second}])continue; n     nxt.push_back({c[lvl][i] + dp[j].first , i}); n     break; n    } n   } n   dp = nxt; n   nxt.clear();   n  } n  sort(all(dp)); n  if(dp.empty())cout << -1 <<   n ; n  else cout << dp[0].first <<   n ; n }


#include <bits/stdc++.h> n using namespace std; n const int maxN = 1e5 + 5; n const int maxM = 1e6 + 5; n const long long INF = 1e18; n const long long MOD = 1e9 + 7; n long long gcd(long long a, long long b) { return !b ? a : gcd(b, a % b); } n long long sq(long long x) { return (x * x) % MOD; } n long long modP(long long a, long long b) { n   return (!b ? 1 : (sq(modP(a, b / 2)) * (b % 2 ? a : 1)) % MOD); n } n bool on[maxN]; n int n, id[maxM], TIME; n int f[maxM], sz = 0; n int Q[maxM], lt, rt; n int st[maxM], fn[maxM]; n int trie[maxM][26]; n long long fen[maxM]; n string s[maxN], str; n vector<int> G[maxM]; n void SHAHO() { n   for (int i = 1; i <= n; i++) { n     int curr = 0; n     for (auto ch : s[i]) { n       int c = ch -  a ; n       if (!trie[curr][c]) trie[curr][c] = ++sz; n       curr = trie[curr][c]; n     } n     id[i] = curr; n   } n   for (int i = 0; i < 26; i++) n     if (trie[0][i]) Q[rt++] = trie[0][i]; n   while (lt != rt) { n     int v = Q[lt++]; n     for (int i = 0; i < 26; i++) { n       if (trie[v][i]) { n         Q[rt++] = trie[v][i]; n         f[trie[v][i]] = trie[f[v]][i]; n       } else n         trie[v][i] = trie[f[v]][i]; n     } n   } n   return; n } n void DFS(int v) { n   st[v] = TIME++; n   for (auto u : G[v]) DFS(u); n   fn[v] = TIME; n   return; n } n void add(int p, int x) { n   for (; p < maxM; p += p & -p) fen[p] += x; n   return; n } n long long fetch(int p) { n   long long res = 0; n   for (; p > 0; p -= p & -p) res += fen[p]; n   return res; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int q; n   cin >> q >> n; n   for (int i = 1; i <= n; i++) cin >> s[i], on[i] = 1; n   SHAHO(); n   for (int i = 0; i <= sz; i++) n     if (i != f[i]) G[f[i]].push_back(i); n   DFS(0); n   for (int i = 1; i <= n; i++) { n     add(st[id[i]], +1); n     add(fn[id[i]], -1); n   } n   int v; n   while (q--) { n     char t; n     cin >> t; n     if (t ==  ? ) { n       cin >> str; n       v = 0; n       long long ans = 0; n       for (auto ch : str) { n         v = trie[v][ch -  a ]; n         ans += fetch(st[v]); n       } n       cout << ans <<   n ; n       continue; n     } n     cin >> v; n     if (t ==  + ) { n       add(st[id[v]], 1 - on[v]); n       add(fn[id[v]], on[v] - 1); n       on[v] = 1; n     } else { n       add(st[id[v]], -on[v]); n       add(fn[id[v]], +on[v]); n       on[v] = 0; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n const int prime[8] = {2, 3, 5, 7, 11, 13, 17, 19}; n long long mul(long long x, long long y, long long mo) { n   long long ret = 1; n   while (y) { n     if (y % 2 == 1) ret = ret * x % mo; n     x = x * x % mo; n     y /= 2; n   } n   return ret; n } n bool is_prime(int n) { n   for (int i = 0; i <= 7; i++) { n     if (n == prime[i]) return 1; n     if (mul(prime[i], n - 1, n) != 1) return 0; n   } n   return 1; n } n int main() { n   scanf( %d , &n); n   if (n == 1) { n     puts( YES ); n     puts( 1 ); n     return 0; n   } n   if (n == 4) { n     puts( YES ); n     puts( 1 ); n     puts( 3 ); n     puts( 2 ); n     puts( 4 ); n     return 0; n   } n   if (!is_prime(n)) { n     puts( NO ); n     return 0; n   } n   puts( YES ); n   puts( 1 ); n   for (int i = 2; i <= n; i++) { n     long long now = i * mul(i - 1, n - 2, n) % n; n     if (now == 0) now = n; n     printf( %I64d n , now); n   } n   return 0; n } n


#include <bits/stdc++.h> n const double INF = 1e18, eps = 1e-18; n double a, b, c, x_1, y_1, x2, y2, x3, y3, x4, y4, x5, y5, x6, y6, ans; n double sqr(double x) { return x * x; } n double dis(double x_1, double y_1, double x2, double y2) { n   return sqrt(sqr(x2 - x_1) + sqr(y_1 - y2)); n } n int main() { n   scanf( %lf%lf%lf%lf%lf%lf%lf , &a, &b, &c, &x_1, &y_1, &x6, &y6); n   if (std::abs(a - 0) > eps && std::abs(b - 0) > eps) n     y2 = y_1, x2 = (-b * y2 - c) / a; n   else n     y2 = x2 = INF; n   if (std::abs(a - 0) > eps && std::abs(b - 0) > eps) n     x3 = x_1, y3 = (-a * x3 - c) / b; n   else n     x3 = y3 = INF; n   if (std::abs(a - 0) > eps && std::abs(b - 0) > eps) n     y4 = y6, x4 = (-b * y4 - c) / a; n   else n     x4 = y4 = INF; n   if (std::abs(a - 0) > eps && std::abs(b - 0) > eps) n     x5 = x6, y5 = (-a * x5 - c) / b; n   else n     x5 = y5 = INF; n   ans = std::min( n       std::min( n           std::min( n               dis(x_1, y_1, x2, y2) + dis(x2, y2, x4, y4) + dis(x4, y4, x6, y6), n               dis(x_1, y_1, x2, y2) + dis(x2, y2, x5, y5) + n                   dis(x5, y5, x6, y6)), n           dis(x_1, y_1, x3, y3) + dis(x3, y3, x4, y4) + dis(x4, y4, x6, y6)), n       dis(x_1, y_1, x3, y3) + dis(x3, y3, x5, y5) + dis(x5, y5, x6, y6)); n   ans = std::min(ans, std::abs(x6 - x_1) + std::abs(y6 - y_1)); n   printf( %.10lf , ans); n   return 0; n } n


#include <bits/stdc++.h> n struct Node; n Node* null; n struct Node { n   Node* insert(int l, int r, int k) { n     if (k < l || r < k) { n       return this; n     } n     if (l == r) { n       return new Node{count + 1, null, null}; n     } n     int m = l + r >> 1; n     return new Node{count + 1, left->insert(l, m, k), n                     right->insert(m + 1, r, k)}; n   } n   int count; n   Node *left, *right; n }; n int query(Node* p, Node* q, int l, int r, int limit) { n   if (q->count - p->count <= limit) { n     return INT_MAX; n   } n   if (l == r) { n     return l; n   } n   int m = l + r >> 1; n   return std::min(query(p->left, q->left, l, m, limit), n                   query(p->right, q->right, m + 1, r, limit)); n } n const int N = 300000; n Node* tree[N + 1]; n int main() { n   null = new Node{0, nullptr, nullptr}; n   null->left = null->right = null; n   int n, q; n   while (scanf( %d%d , &n, &q) == 2) { n     tree[0] = null; n     for (int i = 1, a; i <= n; ++i) { n       scanf( %d , &a); n       tree[i] = tree[i - 1]->insert(1, n, a); n     } n     while (q--) { n       int l, r, k; n       scanf( %d%d%d , &l, &r, &k); n       int limit = (r - l + 1) / k; n       int result = query(tree[l - 1], tree[r], 1, n, limit); n       printf( %d n , result == INT_MAX ? -1 : result); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 5; n int T, n, m, u, v, sum, ans; n int col[N]; n bool vis[N]; n int cnt, head[N]; n struct edge { n   int next, to; n } e[N << 1]; n inline void add(int u, int v) { n   cnt++; n   e[cnt].next = head[u]; n   e[cnt].to = v; n   head[u] = cnt; n } n queue<int> q; n int main() { n   scanf( %d , &T); n   while (T--) { n     scanf( %d%d , &n, &m); n     cnt = 0; n     for (register int i = 1; i <= n; ++i) head[i] = 0; n     for (register int i = 1; i <= m; ++i) n       scanf( %d%d , &u, &v), add(u, v), add(v, u); n     for (register int i = 1; i <= n; ++i) vis[i] = false; n     col[1] = 1; n     vis[1] = true; n     q.push(1); n     while (q.size()) { n       u = q.front(); n       q.pop(); n       for (register int i = head[u]; i; i = e[i].next) n         if (!vis[e[i].to]) { n           col[e[i].to] = col[u] ^ 1; n           vis[e[i].to] = true; n           q.push(e[i].to); n         } n     } n     sum = 0; n     for (register int i = 1; i <= n; ++i) sum += col[i]; n     ans = min(sum, n - sum); n     printf( %d n , ans); n     if (ans == sum) { n       for (register int i = 1; i <= n; ++i) n         if (col[i]) printf( %d  , i); n     } else { n       for (register int i = 1; i <= n; ++i) n         if (!col[i]) printf( %d  , i); n     } n     puts(  ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, d, a, b, p[100005], x[100005], y[100005], c, ans[100005]; n long long cmp(long long A, long long B) { n   return x[A] * a + y[A] * b < x[B] * a + y[B] * b; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> d; n   cin >> a >> b; n   for (long long i = 1; i <= n; i++) cin >> x[i] >> y[i], p[i] = i; n   sort(p + 1, p + 1 + n, cmp); n   for (long long i = 1; i <= n; i++) n     if (d >= x[p[i]] * a + y[p[i]] * b) n       d -= x[p[i]] * a + y[p[i]] * b, ans[++c] = p[i]; n   cout << c << endl; n   for (int i = 1; i <= c; i++) cout << ans[i] <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s; n bool mark = false; n int main() { n   cin >> s; n   for (int i = 0; i < s.size(); i++) { n     if (s[i] ==  0  && mark == false) { n       mark = true; n       continue; n     } n     if (mark == false && i == s.size() - 1) return 0; n     cout << s[i]; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k; n struct trie { n   int cnt, whole; n   trie *next[26]; n   trie() { n     cnt = 0; n     for (int i = 0; i < 26; ++i) next[i] = NULL; n   } n } *root = new trie(); n void ins(char *s) { n   trie *p = root; n   for (int i = 0; s[i]; ++i) { n     int c = s[i] -  a ; n     if (p->next[c] == NULL) p->next[c] = new trie(); n     p = p->next[c]; n   } n   p->cnt++; n } n vector<int> *dp(trie *p, int dep) { n   int pos = -1, cnt = 0; n   for (int i = 0; i < 26; ++i) { n     if (p->next[i] != NULL) { n       pos = i; n       cnt++; n     } n   } n   if (cnt == 0) { n     vector<int> *ret = new vector<int>(p->cnt + 1); n     for (int i = 0; i <= p->cnt; ++i) (*ret)[i] = i * (i - 1) * dep / 2; n     return ret; n   } else if (cnt == 1 && p->whole == p->next[pos]->whole) { n     return dp(p->next[pos], dep + 1); n   } else { n     vector<int> *ret = new vector<int>(p->whole + 1, 0); n     for (int i = 0; i <= p->cnt; ++i) (*ret)[i] = i * (i - 1) * dep / 2; n     int lim = p->cnt; n     for (int i = 0; i < 26; ++i) { n       if (p->next[i] != NULL) { n         vector<int> *tmp = dp(p->next[i], dep + 1); n         for (int j = lim; j >= 0; --j) n           for (int k = 1; n                k < (int)tmp->size() && j + k <= lim + p->next[i]->whole; ++k) n             (*ret)[j + k] = n                 max((*ret)[j + k], (*ret)[j] + (*tmp)[k] + (j * k * dep)); n         lim += p->next[i]->whole; n       } n     } n     return ret; n   } n } n int dfs(trie *p) { n   p->whole = p->cnt; n   for (int i = 0; i < 26; ++i) n     if (p->next[i] != NULL) p->whole += dfs(p->next[i]); n   return p->whole; n } n int main() { n   char s[512]; n   scanf( %d%d , &n, &k); n   for (int i = 0; i < n; ++i) { n     scanf( %s , s); n     ins(s); n   } n   dfs(root); n   printf( %d n , (*dp(root, 0))[k]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n function<void(void)> ____ = []() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n }; n const int MAXN = 4e5 + 7; n int n; n pair<int, int> A[MAXN]; n struct Disjoint_Set_Union { n   int fa[MAXN], sz[MAXN]; n   void clear() { n     for (int i = 1; i < MAXN; i++) fa[i] = i, sz[i] = 0; n   } n   int findx(int x) { return x == fa[x] ? x : fa[x] = findx(fa[x]); } n   void merge(int x, int y) { n     x = findx(x), y = findx(y); n     if (x != y) sz[y] += sz[x], fa[x] = y; n   } n } dsu; n struct SegmentTree { n   int root[MAXN], sz[MAXN << 4], tot, ls[MAXN << 4], rs[MAXN << 4]; n   long long sig[MAXN << 4], sum[MAXN << 4]; n   void pushup(int rt) { n     sz[rt] = sz[ls[rt]] + sz[rs[rt]]; n     sig[rt] = sig[ls[rt]] + sig[rs[rt]]; n     sum[rt] = sig[ls[rt]] * sz[rs[rt]] + sum[ls[rt]] + sum[rs[rt]]; n   } n   void modify(int &rt, int pos, int L, int R) { n     rt = ++tot; n     if (L + 1 == R) { n       sz[rt]++; n       sig[rt] += pos; n       return; n     } n     int mid = (L + R) >> 1; n     if (pos < mid) n       modify(ls[rt], pos, L, mid); n     else n       modify(rs[rt], pos, mid, R); n     pushup(rt); n   } n   int merge(int u, int v, int L, int R) { n     if (!u or !v) return u ^ v; n     if (L + 1 == R) { n       sz[u] += sz[v]; n       sig[u] += sig[v]; n       return u; n     } n     int mid = (L + R) >> 1; n     ls[u] = merge(ls[u], ls[v], L, mid); n     rs[u] = merge(rs[u], rs[v], mid, R); n     pushup(u); n     return u; n   } n } seg; n void solve() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d %d , &A[i].first, &A[i].second); n   dsu.clear(); n   long long ret = 0; n   for (int i = 1; i <= n; i++) { n     int a = A[i].first, b = A[i].second; n     ret -= 1ll * a * b; n     int rt = dsu.findx(a); n     int pos = rt + dsu.sz[rt]; n     dsu.sz[pos] = 1; n     seg.modify(seg.root[pos], b, 1, n + 1); n     if (pos == rt and pos != 1 and dsu.sz[pos - 1]) rt = dsu.findx(rt - 1); n     if (pos != rt) { n       ret -= seg.sum[seg.root[rt]]; n       ret -= rt * seg.sig[seg.root[rt]]; n       dsu.merge(pos, rt); n       seg.root[rt] = seg.merge(seg.root[rt], seg.root[pos], 1, n + 1); n     } n     if (dsu.sz[pos + 1]) { n       ret -= seg.sum[seg.root[pos + 1]]; n       ret -= (pos + 1) * seg.sig[seg.root[pos + 1]]; n       dsu.merge(pos + 1, rt); n       seg.root[rt] = seg.merge(seg.root[rt], seg.root[pos + 1], 1, n + 1); n     } n     ret += seg.sum[seg.root[rt]]; n     ret += rt * seg.sig[seg.root[rt]]; n     cout << ret <<   n ; n   } n } n int main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 998244353; n const int maxn = 300009; n long long fac[maxn], invfac[maxn], A[33], B[33], in[maxn], idx[maxn]; n vector<int> L[maxn], R[maxn]; n int n, m; n long long mpw(long long a, long long b) { n   long long c = 1; n   while (b) { n     if (b & 1ll) c = c * a % mod; n     a = a * a % mod; n     b >>= 1; n   } n   return c; n } n void init() { n   fac[0] = 1; n   for (int i = 1; i < maxn; ++i) fac[i] = fac[i - 1] * i % mod; n   invfac[maxn - 1] = mpw(fac[maxn - 1], mod - 2); n   for (int i = maxn - 2; i >= 0; --i) invfac[i] = invfac[i + 1] * (i + 1) % mod; n } n long long C(int a, int b) { n   if (a < b || b < 0) return 0; n   return fac[a] * invfac[b] % mod * invfac[a - b] % mod; n }; n int main() { n   ios_base::sync_with_stdio(false), cout.tie(0), cin.tie(0); n   cin >> n >> m; n   vector<std::vector<int>> add(n + 1), del(n + 1); n   for (int i = 1; i <= n; ++i) { n     int l, r; n     cin >> l >> r; n     L[l].push_back(i); n     R[r].push_back(i); n     add[l].push_back(i - 1); n     del[r].push_back(i - 1); n   } n   int idxtot = 0, cc = 0; n   memset(idx, -1, sizeof idx); n   std::vector<int> a(m), b(m); n   std::vector<int> id(n, -1); n   for (int i = 0; i < m; ++i) { n     cin >> A[i] >> B[i]; n     a[i] = A[i] - 1; n     b[i] = B[i] - 1; n     if (idx[A[i]] == -1) idx[A[i]] = idxtot++; n     if (idx[B[i]] == -1) idx[B[i]] = idxtot++; n     if (id[a[i]] == -1) id[a[i]] = cc++; n     if (id[b[i]] == -1) id[b[i]] = cc++; n   } n   init(); n   long long premask = 0, tot = 0, masktot = 0, curmask = 0, res = 0; n   vector<long long> cnt(m + 1); n   cnt[0] = 1; n   std::vector<int> ways(m + 1); n   std::vector<int> in2(n); n   ways[0] = 1; n   long long mask = 0; n   int tot2 = 0, tp = 0; n   int ans = 0; n   for (int i = 1; i <= n; ++i) { n     for (int j : L[i]) { n       in[j] = 1; n       if (idx[j] == -1) n         tot++; n       else n         masktot++; n     } n     for (auto x : add[i]) { n       in2[x] = 1; n       ++(id[x] == -1 ? tot2 : tp); n     } n     curmask = 0; n     for (int j = 0; j < m; ++j) { n       if (in[A[j]]) curmask |= 1ll << idx[A[j]]; n       if (in[B[j]]) curmask |= 1ll << idx[B[j]]; n     } n     long long nMask = 0; n     for (int j = 0; j < m; ++j) { n       if (in2[a[j]]) nMask |= 1ll << id[a[j]]; n       if (in2[b[j]]) nMask |= 1ll << id[b[j]]; n     } n     if (nMask != curmask) { n       cout << 1 <<   n ; n       return 0; n     } n     if (mask != premask) { n       cout << 3; n       return 0; n     } n     if (tp != masktot) { n       cout << 4; n       return 0; n     } n     if (nMask != mask) { n       mask = nMask; n       std::fill(ways.begin(), ways.end(), 0); n       std::function<void(int, int64_t, int)> dfs1 = [&](int x, int64_t s, n                                                         int coef) { n         if (x == m) { n           int p = __builtin_popcountll(s); n           for (int j = 0; j <= m; ++j) n             ways[j] = (ways[j] + 1ll * coef * C(tp - p, j - p)) % mod; n           return; n         } n         if (in2[a[x]] && in2[b[x]]) n           dfs1(x + 1, s | 1ll << id[a[x]] | 1ll << id[b[x]], mod - coef); n         dfs1(x + 1, s, coef); n       }; n       dfs1(0, 0, 1); n     } n     if (premask != curmask) { n       for (int j = 0; j <= m; ++j) cnt[j] = 0; n       std::function<void(int, int64_t, int)> dfs1 = [&](int x, int64_t s, n                                                         int coef) { n         if (x == m) { n           int p = __builtin_popcountll(s); n           for (int j = 0; j <= m; ++j) n             cnt[j] = (cnt[j] + 1ll * coef * C(tp - p, j - p)) % mod; n           return; n         } n         if (in[A[x]] && in[B[x]]) n           dfs1(x + 1, s | 1ll << idx[A[x]] | 1ll << idx[B[x]], mod - coef); n         dfs1(x + 1, s, coef); n       }; n       dfs1(0, 0, 1); n     } n     premask = curmask; n     for (int j = 0; j <= m; ++j) n       if (cnt[j] != ways[j]) { n         for (int x = 0; x < n; ++x) assert(in[x + 1] == in2[x]); n         cout << 2; n         return 0; n       } n     for (int j = 0; j <= m; ++j) n       ans = (ans + 1ll * ways[j] * C(tot2, i - j)) % mod; n     for (int j = 0; j <= m; ++j) n       res = (res + cnt[j] * C(tot, i - j) % mod) % mod; n     assert(ans == res); n     for (int j : R[i]) { n       in[j] = 0; n       if (idx[j] == -1) n         tot--; n       else n         masktot--; n     } n     for (auto x : del[i]) { n       in2[x] = 0; n       --(id[x] == -1 ? tot2 : tp); n     } n   } n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1e+18; n int main() { n   cin.tie(0); n   ios::sync_with_stdio(0); n   int n, c = 0; n   cin >> n; n   while (n != 0) { n     string str = to_string(n); n     int maxi = 0; n     for (int i = 0; i < str.size(); i++) { n       if ((str[i] -  0 ) > maxi) { n         maxi = str[i] -  0 ; n       } n     } n     n -= maxi; n     c++; n   } n   cout << c <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n struct Point { n   int x, y; n   Point() {} n   Point(int a, int b) { n     x = a; n     y = b; n   } n }; n double hypt(Point A, Point B) { n   return sqrt((B.x - A.x) * (B.x - A.x) + (B.y - A.y) * (B.y - A.y)); n } n int main() { n   int k, n; n   cin >> n >> k; n   vector<Point> v(n); n   for (int i = 0; i < n; i++) { n     cin >> v[i].x >> v[i].y; n   } n   double ans = 0; n   for (int i = 1; i < n; i++) { n     ans += hypt(v[i], v[i - 1]); n   } n   ans *= k; n   ans /= 50; n   printf( %.10lf , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   vector<pair<int, int>> arr; n   vector<int> t; n   for (int i = 0; i < n; i++) { n     int a; n     cin >> a; n     t.push_back(a); n     arr.push_back({a, i}); n   } n   sort(arr.begin(), arr.end()); n   int i, j; n   for (i = 0; i < arr.size(); i++) { n     if (arr[i].second != i) break; n   } n   for (j = arr.size() - 1; j >= 0; j--) { n     if (arr[j].second != j) break; n   } n   if (i == arr.size() || j < 0) n     cout << 0 <<     << 0 << endl; n   else { n     vector<int> res; n     for (int k = 0; k < i; k++) res.push_back(t[k]); n     for (int k = j; k >= i; k--) res.push_back(t[k]); n     for (int k = j + 1; k < n; k++) res.push_back(t[k]); n     bool flag = false; n     for (int k = 0; k < n - 1; k++) { n       if (res[k] >= res[k + 1]) { n         flag = true; n         break; n       } n     } n     if (!flag) n       cout << arr[j].second + 1 <<     << arr[i].second + 1 << endl; n     else n       cout << 0 <<     << 0 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct point { n   long double x, y; n }; n point a1, a2, a3, a4, b1, b2, b3, b4, c; n int l, r, u, d; n bool corner(point b) { n   if (b.x <= r && b.x >= l && b.y <= u && b.y >= d) n     return true; n   else n     return false; n } n pair<long double, long double> line(point a, point b) { n   long double m, c; n   m = ((long double)b.y - a.y) / ((long double)b.x - a.x); n   c = a.y - m * a.x; n   return {m, c}; n } n point foot(pair<long double, long double> line) { n   point p; n   p.x = n       (line.first * (c.y + line.second) + c.x) / (1 + line.first * line.first); n   p.y = line.first * p.x + line.second; n   return p; n } n bool inside(point p, pair<long double, long double> l1, n             pair<long double, long double> l2) { n   int k = l1.first / l2.first; n   if (k == -1) { n     l1.first *= -1; n     l1.second *= -1; n   } n   if ((p.x * l1.first + l1.second - p.y) * (p.x * l2.first + l2.second - p.y) <= n       0) n     return true; n   return false; n } n int main() { n   ios::sync_with_stdio(0); n   cin >> a1.x >> a1.y >> a2.x >> a2.y >> a3.x >> a3.y >> a4.x >> a4.y; n   c.x = (a1.x + a2.x + a3.x + a4.x) / 4; n   c.y = (a1.y + a2.y + a3.y + a4.y) / 4; n   l = min(min(a1.x, a2.x), a3.x); n   r = max(max(a1.x, a2.x), a3.x); n   d = min(min(a1.y, a2.y), a3.y); n   u = max(max(a1.y, a2.y), a3.y); n   cin >> b1.x >> b1.y >> b2.x >> b2.y >> b3.x >> b3.y >> b4.x >> b4.y; n   int flag = 0; n   if (corner(b1) || corner(b2) || corner(b3) || corner(b4)) flag = 1; n   pair<long double, long double> l1, l2, l3, l4; n   l1 = line(b1, b2); n   l2 = line(b2, b3); n   l3 = line(b3, b4); n   l4 = line(b4, b1); n   long double d = (r - l) / 2 * sqrt((long double)2); n   if ((inside(a1, l1, l3) && inside(a1, l2, l4)) || n       (inside(a2, l1, l3) && inside(a2, l2, l4)) || n       (inside(a3, l1, l3) && inside(a3, l2, l4)) || n       (inside(a4, l1, l3) && inside(a4, l2, l4)) || n       (inside(c, l1, l3) && inside(c, l2, l4))) n     flag = 1; n   if (flag) n     cout <<  YES  << endl; n   else n     cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); n   srand(time(NULL)); n   ; n   int T = 1; n   cin >> T; n   while (T--) { n     int n; n     cin >> n; n     cout << setprecision(10) n          << cos(3.141592653589793238 / (4 * n)) / n                 sin(3.141592653589793238 / (2 * n)); n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int get(long long int x) { n   long long int y = 1, ans = 0; n   while (y < x) { n     y = y * 2; n     if (y <= x) n       ans++; n     else n       break; n   } n   return ans; n } n int main() { n   int t = 1; n   cin >> t; n   while (t--) { n     long long int n, m, k, a = 0, b = 0, ans = 1, x = 0, y = 0; n     cin >> a >> b >> m; n     if (a == b) { n       cout <<  1   << a <<   n ; n       continue; n     } n     n = get(b / (a + 1)); n     long long int arr[51] = {}; n     long long int rem = b - (1ll << n) * (a + 1); n     long long int cur = a + 1, sum = a; n     vector<long long int> sol; n     sol.push_back(n + 2); n     sol.push_back(a); n     for (int i = 0; i < n; i++) { n       x = (1ll << (n - i - 1)); n       if (rem >= x) { n         k = min(m - 1, rem / x); n         rem -= k * x; n         cur += k; n       } n       sol.push_back(cur); n       sum += cur; n       cur = sum + 1; n     } n     if (rem >= 0 && rem < m) { n       sol.push_back(cur + rem); n       for (auto i : sol) cout << i <<    ; n     } else { n       cout <<  -1 ; n     } n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int debug_ = 0; n const int maxn = 200000; n const long long inf = 998244353; n int unum[33], dnum[33]; n long long dp[33][2][2][2]; n long long dfs(int pos, int u, int e, int d) { n   if (pos == -1) { n     return 2 - e; n   } n   if (dp[pos][u][e][d] != -1) { n     return dp[pos][u][e][d]; n   } n   long long& ret = dp[pos][u][e][d]; n   ret = 0; n   for (int i = 0; i < 3; ++i) { n     int x = i / 2, y = i % 2; n     if (x == 1 && u && unum[pos] == 0) { n       continue; n     } n     if (y == 1 && e) { n       continue; n     } n     if (y == 0 && d && dnum[pos] == 1) { n       continue; n     } n     int nu = (u && x == unum[pos]); n     int ne = (e && y == x); n     int nd = (d && y == dnum[pos]); n     ret += dfs(pos - 1, nu, ne, nd); n   } n   return ret; n } n int main() { n   int tes, u, d; n   cin >> tes; n   while (tes--) { n     cin >> d >> u; n     for (int i = 0; i < 31; ++i) { n       dnum[i] = (d >> i) & 1; n       unum[i] = (u >> i) & 1; n     } n     memset(dp, -1, sizeof(dp)); n     cout << dfs(32, 1, 1, 1) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 205, P = 1e9 + 7ll; n struct node { n   long long x, y; n   node operator-(const node &t) const { n     node c; n     c.x = x - t.x; n     c.y = y - t.y; n     return c; n   } n   long long operator*(const node &t) const { return x * t.y - y * t.x; } n } p[N]; n long long n, dp[N][N]; n long long search(long long x, long long y) { n   long long ans = 0; n   if (dp[x][y] != -1) return dp[x][y]; n   if (x + 1 == y) return dp[x][y] = 1; n   for (int i = x + 1; i < y; i++) n     if ((p[x] - p[i]) * (p[y] - p[i]) > 0) n       ans = (ans + search(x, i) * search(i, y) % P) % P; n   return dp[x][y] = ans; n } n int main() { n   memset(dp, -1, sizeof dp); n   scanf( %dll , &n); n   for (int i = 1; i <= n; i++) scanf( %lld%lld , &p[i].x, &p[i].y); n   p[n + 1] = p[1]; n   long long s = 0; n   for (int i = 1; i <= n; i++) s += p[i + 1] * p[i]; n   if (s < 0) n     for (int i = 1, j = n; i < j; swap(p[i++], p[j--])) n       ; n   printf( %lld , search(1, n)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long quick_pow(long long a, long long b, long long mod) { n   long long ans = 1, base = a; n   while (b) { n     if (b & 1) (ans *= base) %= mod; n     b >>= 1; n     (base *= base) %= mod; n   } n   return ans; n } n struct node { n   int pos, len; n   bool operator<(const node& k) const { return pos < k.pos; } n }; n const int N = 1e5 + 5; n int X[N], Y[N]; n int A[N], B[N]; n int main() { n   int n, m; n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) { n     scanf( %d %d , &X[i], &Y[i]); n     A[i] = X[i]; n     B[i] = Y[i]; n   } n   sort(X + 1, X + n + 1); n   sort(Y + 1, Y + n + 1); n   m = (n + 1) >> 1; n   int mid_x = X[m], mid_y = Y[m]; n   int dif_x = X[m + 1] - X[m], dif_y = Y[m + 1] - Y[m]; n   if (n & 1) { n     dif_x = min(dif_x, X[m] - X[m - 1]); n     dif_y = min(dif_y, Y[m] - Y[m - 1]); n   } n   long long ans = 0; n   bool F0 = false, F1 = false, Tag = false; n   for (int i = 1; i <= n; ++i) { n     ans += abs(X[i] - X[m]) + abs(Y[i] - Y[m]); n     F0 |= A[i] > mid_x && B[i] > mid_y; n     F1 |= A[i] > mid_x && B[i] <= mid_y; n     Tag |= A[i] == mid_x && B[i] == mid_y; n   } n   if (F0 && F1 && (~n & 1 || Tag)) ans -= min(dif_x, dif_y); n   printf( %I64d n , ans << 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long parent[200001]; n long long urank[200001]; n void create(long long x) { n   parent[x] = x; n   urank[x] = 1; n } n long long find(long long x) { n   if (parent[x] != x) { n     parent[x] = find(parent[x]); n   } n   return parent[x]; n } n void merge(long long x, long long y) { n   long long xroot = find(x); n   long long yroot = find(y); n   if (urank[xroot] <= urank[yroot]) { n     parent[xroot] = yroot; n     urank[yroot] = urank[yroot] + urank[xroot]; n   } else { n     parent[yroot] = xroot; n     urank[xroot] = urank[xroot] + urank[yroot]; n   } n } n int main() { n   std::ios::sync_with_stdio(false); n   int T; n   T = 1; n   while (T--) { n     int n, i, j, m; n     cin >> n >> m; n     long long ar[n + 1], a, b, c; n     vector<pair<long long, int> > v; n     vector<pair<long long, pair<int, int> > > v2; n     for (i = 1; i <= n; ++i) { n       create(i); n       cin >> ar[i]; n       v.push_back({ar[i], i}); n     } n     sort(v.begin(), v.end()); n     long long ans = 0; n     for (i = 1; i < n; ++i) { n       v2.push_back({v[0].first + v[i].first, {v[0].second, v[i].second}}); n     } n     while (m--) { n       cin >> a >> b >> c; n       v2.push_back({c, {a, b}}); n     } n     sort(v2.begin(), v2.end()); n     for (i = 0; i < v2.size(); ++i) { n       a = find(v2[i].second.first); n       b = find(v2[i].second.second); n       if (a == b) continue; n       ans = ans + v2[i].first; n       merge(a, b); n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long double eps = 1e-9; n const long double pi = acos(-1.0); n const long long inf = 1e+9; n const long long inf64 = inf * inf; n #pragma comment(linker,  /STACK:36777216 ) n template <typename T> n istream &operator>>(istream &, vector<T> &); n template <typename T> n ostream &operator<<(ostream &, const vector<T> &); n void solve() { n   int m; n   cin >> m; n   string s; n   cin >> s; n   int n = s.size(); n   vector<int> cnt(26); n   for (int i = 0; i < n; i++) cnt[s[i] -  a ]++; n   for (char t =  a ; t <=  z ; t++) { n     int last = -1; n     bool fl = 1; n     int count = 0; n     for (int i = 0; i < n; i++) { n       if (s[i] < t) { n         if (i - last > m) { n           fl = 0; n           break; n         } n         last = i; n       } n       if (s[i] == t) { n         int pos = -1; n         int pos2 = -1; n         for (int j = last + 1; j < n && j < last + 1 + m; j++) { n           if (s[j] < t) pos = j; n           if (s[j] == t) pos2 = j; n         } n         if (pos > i) { n           last = pos; n           i = pos; n           continue; n         } n         if (pos2 != -1) { n           i = pos2; n           last = pos2; n           count++; n         } else { n           fl = 0; n           break; n         } n       } n       if (n - last <= m) break; n     } n     if (fl && n - last <= m) { n       string ans =   ; n       for (char z =  a ; z < t; z++) ans += string(cnt[z -  a ], z); n       ans += string(count, t); n       cout << ans << endl; n       return; n     } n   } n } n int main() { n   solve(); n   return 0; n } n template <typename T> n istream &operator>>(istream &is, vector<T> &v) { n   for (int i = 0; i < v.size(); ++i) is >> v[i]; n   return is; n } n template <typename T> n ostream &operator<<(ostream &os, const vector<T> &v) { n   for (int i = 0; i < v.size(); ++i) os << v[i]; n   return os; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x7f7f7f7f; n int main() { n   string s; n   cin >> s; n   int ans = 0; n   stack<char> S; n   for (int i = 0; i < s.length(); i++) { n     if (S.empty()) { n       S.push(s[i]); n     } else if (S.top() == s[i]) { n       ans++; n       S.pop(); n     } else { n       S.push(s[i]); n     } n   } n   if (ans % 2 == 0) n     cout <<  No  << endl; n   else n     cout <<  Yes  << endl; n   return 0; n } n


#include <bits/stdc++.h> n long long int x, y, z, i, flag, flag2, top; n long long int A[200010]; n int main() { n   scanf( %lld , &x); n   for (i = 1; i <= x; i++) { n     scanf( %lld , &A[i]); n     if (A[i] == 1 && flag == 0) { n       y = i + 1; n       flag = 1; n     } n   } n   for (i = y; i <= x; i++) { n     if (A[i] == 0) z++; n   } n   for (i = 1; i <= x; i++) { n     if (A[i] == 1) { n       top += z; n       flag2 = 1; n     } else if (A[i] == 0 && flag2 == 1) n       z--; n   } n   printf( %lld , top); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 200005, INF = 1 << 31; n struct A { n   int x, w; n } a[MAXN]; n int b[MAXN], c[MAXN], n, d[MAXN], f[MAXN]; n bool cmp(const A &x, const A &y) { return x.x < y.x; } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) scanf( %d%d , &a[i].x, &a[i].w); n   sort(a, a + n, cmp); n   memset(f, 0, sizeof f); n   memset(d, 0x7f, sizeof d); n   for (int i = 0; i < n; ++i) { n     b[i] = a[i].x - a[i].w; n     c[i] = a[i].x + a[i].w; n   } n   int ans = 0; n   for (int i = 0; i < n; ++i) { n     int t = upper_bound(d + 1, d + ans + 1, b[i]) - d - 1; n     d[t + 1] = min(c[i], d[t + 1]); n     if (ans == t) ans = t + 1; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 10, mod = 1e9 + 7; n const long long inf = 1e18; n long long tn[20], fv[20]; n long long Mul(long long a, long long b, long long md) { n   long long ans = 0; n   while (b) { n     if (b & 1) ans = (ans + a) % md; n     a = (a + a) % md; n     b >>= 1; n   } n   return ans; n } n long long Pow(long long a, long long b, long long md) { n   long long ans = 1; n   while (b) { n     if (b & 1) ans = Mul(ans, a, md); n     a = Mul(a, a, md); n     b >>= 1; n   } n   return ans; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   tn[0] = fv[0] = 1; n   for (int i = 1; i < 19; i++) tn[i] = tn[i - 1] * 10, fv[i] = fv[i - 1] * 5; n   int q; n   cin >> q; n   while (q--) { n     long long x; n     cin >> x; n     long long xx = x; n     int n = 0; n     while (xx > 0) n++, xx /= 10; n     long long y = (-tn[17 - n] * x) % (1ll << 17); n     if (y < 0) y += (1ll << 17); n     if (y % 5 == 0) y += (1ll << 17); n     x = tn[17 - n] * x + y; n     long long ans = 0; n     if (x % 5 == 2) ans = 1; n     if (x % 5 == 4) ans = 2; n     if (x % 5 == 3) ans = 3; n     for (int i = 2; i <= 17; i++) n       while (Pow(2, ans, fv[i]) != (x % fv[i])) n         ans = (ans + 4ll * fv[i - 2]) % (4ll * fv[i - 1]); n     ans += 4ll * fv[16]; n     cout << ans <<   n ; n   } n } n


#include <bits/stdc++.h> n template <typename T> n inline bool MIN(T &a, const T &b) { n   return a > b ? a = b, 1 : 0; n } n template <typename T> n inline bool MAX(T &a, const T &b) { n   return a < b ? a = b, 1 : 0; n } n inline int plu(int a, int b) { n   return a + b >= 1000000007 ? a + b - 1000000007 : a + b; n } n inline int sub(int a, int b) { return a - b < 0 ? a - b + 1000000007 : a - b; } n inline int mul(int a, int b) { return (long long)a * b % 1000000007; } n inline int inc(int &a, int b) { n   a += b; n   if (a >= 1000000007) a -= 1000000007; n } n inline int dec(int &a, int b) { n   a -= b; n   if (a < 0) a += 1000000007; n } n using namespace std; n int n, A[200010], m; n char s1[200010], s2[200010]; n int ok(int L) { n   int p = 1; n   for (int i = 1; i <= n; ++i) n     if (A[i] <= L) { n       if (s1[i] == s2[p]) ++p; n       if (p > m) return 1; n     } n   return 0; n } n int main() { n   scanf( %s%s , s1 + 1, s2 + 1); n   n = strlen(s1 + 1); n   m = strlen(s2 + 1); n   for (int i = 1, a; i <= n; ++i) scanf( %d , &a), A[a] = n - i + 1; n   int l = 1, r = n + 1; n   while (l < r) { n     int mid = l + r >> 1; n     if (ok(mid)) n       r = mid; n     else n       l = mid + 1; n   } n   printf( %d n , n - l); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n unsigned int inet4[100010]; n int main(int argc, char* argv[]) { n   ios_base::sync_with_stdio(false); n   int n, m; n   scanf( %d%d , &n, &m); n   for (int i = 0; i < n; i++) { n     char* str; n     int inet[4]; n     scanf( %d.%d.%d.%d , &inet[0], &inet[1], &inet[2], &inet[3]); n     inet4[i] = 0; n     for (int j = 0; j < 4; j++) inet4[i] = inet4[i] * 256 + inet[j]; n   } n   unsigned int mask = 0x80000000; n   bool flag = false; n   for (int i = 1; i < 32; i++) { n     set<int> hh; n     for (int j = 0; j < n; j++) { n       hh.insert(inet4[j] & mask); n     } n     if (hh.size() == m) { n       int ans[4]; n       for (int j = 3; j >= 0; j--) { n         ans[j] = mask % 256; n         mask /= 256; n       } n       printf( %d.%d.%d.%d n , ans[0], ans[1], ans[2], ans[3]); n       flag = true; n       break; n     } n     mask = mask | (mask >> 1); n   } n   if (!flag) printf( -1 n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[114514]; n int l[114514], r[114514]; n bool solve(int n) { n   int curr = a[1]; n   l[1] = a[1]; n   for (int i = 2; i <= n; i++) { n     if (a[i] < a[i - 1]) curr -= a[i - 1] - a[i]; n     if (curr < 0) return false; n     l[i] = curr; n   } n   return true; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     for (int i = 1; i <= n; i++) cin >> a[i]; n     if (solve(n)) n       cout <<  YES  << endl; n     else n       cout <<  NO  << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int t[95]; n int main() { n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> t[i]; n   } n   t[0] = 0; n   int langkah; n   langkah = 0; n   for (int i = 1; i <= n; i++) { n     int a; n     a = t[i] - t[i - 1]; n     if (a <= 15) n       langkah++; n     else { n       break; n     } n   } n   if (t[langkah] + 15 >= 90) n     cout << 90 << endl; n   else { n     cout << t[langkah] + 15 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, nn = 0, pp = 0; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     int x; n     scanf( %d , &x); n     if (x < 0) n       nn++; n     else if (x > 0) n       pp++; n   } n   if (nn >= n / 2 + n % 2) n     printf( -1 ); n   else if (pp >= n / 2 + n % 2) n     printf( 1 ); n   else n     printf( 0 ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N, M, K, R; n char arr[102][102]; n int best[102][102][1002]; n int solve(int r, int c, int p) { n   if (r < 0) { n     if (p % K == 0) return p; n     return -1 << 30; n   } n   if (best[r][c][p] != -1) return best[r][c][p]; n   int ret = -1 << 30; n   if (c - 1 >= 0) ret = max(ret, solve(r - 1, c - 1, p + (arr[r][c] -  0 ))); n   if (c + 1 < M) ret = max(ret, solve(r - 1, c + 1, p + (arr[r][c] -  0 ))); n   return best[r][c][p] = ret; n } n void solve2(int r, int c, int p) { n   if (r == 0) { n     printf(  n ); n     return; n   } n   int ret = -1 << 30; n   if (c - 1 >= 0 && best[r - 1][c - 1][p + (arr[r][c] -  0 )] == R) n     printf( L ), solve2(r - 1, c - 1, p + (arr[r][c] -  0 )); n   else if (c + 1 < M && best[r - 1][c + 1][p + (arr[r][c] -  0 )] == R) n     printf( R ), solve2(r - 1, c + 1, p + (arr[r][c] -  0 )); n   return; n } n int main() { n   scanf( %d %d %d , &N, &M, &K); n   K++; n   for (int i = 0; i < N; i++) scanf( %s , arr[i]); n   int ret = -1 << 30, ind; n   memset(best, -1, sizeof(best)); n   for (int i = 0; i < M; i++) { n     int x = solve(N - 1, i, 0); n     if (x > ret) ret = x, ind = i; n   } n   if (ret == -1 << 30) n     printf( -1 n ); n   else n     printf( %d n , ret); n   R = ret; n   if (ret != -1 << 30) { n     printf( %d n , ind + 1); n     solve2(N - 1, ind, 0); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int N, M, i, X, A[100005]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> N >> M; n   for (i = 0; i < 2 * M; i++) { n     cin >> X; n     A[X]++; n   } n   if (N == M) { n     for (i = 1; 1; i++) { n       if (A[i] != 2) { n         break; n       } n       if (i == N) { n         cout <<  ring topology n ; n         return 0; n       } n     } n   } n   if (N - 1 == M) { n     for (i = 1; i <= N; i++) { n       if (A[i] == N - 1) { n         cout <<  star topology n ; n         return 0; n       } n     } n     for (i = 1; 1; i++) { n       if (A[i] > 2 || A[i] == 0) { n         break; n       } n       if (i == N) { n         cout <<  bus topology n ; n         return 0; n       } n     } n   } n   cout <<  unknown topology n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int hour, minute, sleeph, sleepm; n   char temp; n   scanf( %d%c%d , &hour, &temp, &minute); n   scanf( %d%c%d , &sleeph, &temp, &sleepm); n   int shour, smin; n   smin = minute - sleepm; n   if (smin < 0) { n     smin += 60; n     hour--; n   } n   shour = hour - sleeph; n   while (shour < 0) { n     shour += 24; n   } n   if (shour < 10) cout <<  0 ; n   cout << shour <<  : ; n   if (smin < 10) cout <<  0 ; n   cout << smin << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> x, y, z; n inline int sqr(int x) { return x * x; } n inline double dist(int i, int j) { n   return sqrt(sqr(x[i] - x[j]) + sqr(y[i] - y[j]) + sqr(z[i] - z[j])); n } n int main() { n   int n; n   cin >> n; n   x.resize(n), y.resize(n), z.resize(n); n   for (int i = 0; i < n; ++i) cin >> x[i] >> y[i] >> z[i]; n   double shortestTime = 1e9; n   for (int i = 1; i < n; ++i) { n     for (int j = i + 1; j < n; ++j) { n       double d = dist(0, i) + dist(i, j) + dist(j, 0); n       shortestTime = min(shortestTime, d / 2); n     } n   } n   cout << setprecision(10) << shortestTime << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long modpow(long long b, long long e, long long mod) { n   long long ans = 1; n   for (; e; b = b * b % mod, e /= 2) n     if (e & 1) ans = ans * b % mod; n   return ans; n } n const int N = 5050; n int dp[N][N][2], c[N], n; n bool vis[N][N][2]; n int solve(int x, int y, int z) { n   if (x < 0 || y > n || x > y) return (int)1e9; n   if (x == 1 && y == n) return 0; n   if (vis[x][y][z]) return dp[x][y][z]; n   vis[x][y][z] = true; n   return dp[x][y][z] = n              min(solve(x - 1, y, 0) + (c[z ? y : x] == c[x - 1] ? 0 : 1), n                  solve(x, y + 1, 1) + (c[z ? y : x] == c[y + 1] ? 0 : 1)); n } n int32_t main() { n   cin >> n; n   for (int i = 0; i < (n); ++i) cin >> c[i + 1]; n   int ans = (int)1e9; n   for (int i = 0; i < (n); ++i) ans = min(ans, solve(i + 1, i + 1, 0)); n   cout << ans << endl; n } n


n #include<bits/stdc++.h> n using namespace std; n #define endl   n  n #define ll long long n int main() n { ios_base::sync_with_stdio(false);  n     cin.tie(NULL); n  ll t; n cin>>t; n while(t--) n { ll n; n     cin>>n; n if(n<=3) n cout<<n-1<<endl; n else n { n     if(n%2==0) n     cout<<2<<endl; n     else n     { n         cout<<3<<endl; n     } n      n } n  n  n } n  n   n   n   n   n  n  n       n  } n      n  n  n    n   n    n  n    n        n  n  n


#include <bits/stdc++.h> n using namespace std; n  n #define rep(i, a) for(int i=0;i<a;i++) n #define repA(i,a,b)for(int i=a;i<b;i++) n #define l list n #define v vector n #define u_m unordered_map n typedef long long ll; n const int mxN=101,MOD=1e9+7; n void solve(){ n  int n,m,count=1,r=0; n  cin>>n>>m; n  string s; n  cin>>s; n  while(count>0&&r<m){ n   string cop=s; n   count=0; n   rep(i,n){ n    if(s[i]== 0 ){ n    int cr=0; n    if(i-1>=0&&s[i-1]== 1 )cr++; n    if(i+1<n&&s[i+1]== 1 )cr++; n    if(cr==1){ n     cop[i]= 1 ; n     count++; n    } n    } n   } n   s=cop; n   r++; n  } n  cout<<s<<endl; n } n int main(){ n  ios::sync_with_stdio(0); n     cin.tie(0); n     cout.tie(0); n  int t; n  cin>>t; n  while(t--){ n   solve(); n  } n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1e13; n void solve() { n   long long n; n   cin >> n; n   vector<pair<long long, long long> > a(n); n   for (long long i = 0; i < n; i++) { n     cin >> a[i].first; n     a[i].second = i; n   } n   sort(a.begin(), a.end()); n   long long cnt = 1, ans = 1, w = 1; n   for (long long i = 1; i < n; i++) { n     if (a[i].first == a[i - 1].first) continue; n     w++; n     if (a[i].second > a[i - 1].second) n       cnt++; n     else { n       ans = max(ans, cnt); n       cnt = 1; n     } n   } n   ans = max(ans, cnt); n   cout << w - ans <<   n ; n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long q; n   cin >> q; n   while (q--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int tosub = 0; n   int pls = 1; n   int mins = 0; n   string s; n   char a; n   cin >> a; n   while (a !=  = ) { n     if (a ==  + ) { n       s.push_back(a); n       pls++; n     } else if (a ==  - ) { n       mins++; n       s.push_back(a); n     } n     cin >> a; n   } n   int tmp; n   cin >> tmp; n   int mn = pls - mins * tmp; n   int mx = pls * tmp - mins; n   if (tmp < mn || tmp > mx) { n     cout <<  Impossible n ; n     return 0; n   } n   cout <<  Possible n ; n   int curr = 0; n   for (int i = (-1); i < (int)s.size(); ++i) { n     if (!(~i) || s[i] ==  + ) { n       if (i >= 0) { n         cout << s[i] <<    ; n       } n       for (int j = 1; j <= tmp; ++j) { n         int mn = (pls - 1) - mins * tmp; n         int mx = (pls - 1) * tmp - mins; n         if (mn <= tmp - (curr + j) && mx >= tmp - (curr + j)) { n           cout << j <<    ; n           pls--; n           curr += j; n           break; n         } n       } n     } else { n       cout << s[i] <<    ; n       for (int j = 1; j <= tmp; ++j) { n         int mn = pls - (mins - 1) * tmp; n         int mx = pls * tmp - (mins - 1); n         if (mn <= tmp - (curr - j) && mx >= tmp - (curr - j)) { n           cout << j <<    ; n           mins--; n           curr -= j; n           break; n         } n       } n     } n   } n   cout <<   =   << tmp <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 5e5 + 100; n int head[maxn], cnt; n vector<int> pos[maxn]; n int len[maxn]; n int val[maxn], pre[maxn], vis[maxn]; n char str[1100]; n char pat[1100]; n struct edge { n   int from, to, flow, next, cost; n } e[2 * maxn]; n void addedge(int from, int to, int flow, int cost) { n   e[cnt].from = from; n   e[cnt].to = to; n   e[cnt].flow = flow; n   e[cnt].cost = cost; n   e[cnt].next = head[from]; n   head[from] = cnt++; n } n void line(int from, int to, int flow, int cost) { n   addedge(from, to, flow, cost); n   addedge(to, from, 0, -cost); n } n int dis[maxn]; n queue<int> q; n int spfa(int s, int t, int st) { n   while (!q.empty()) q.pop(); n   memset(vis, 0, sizeof(vis)); n   memset(pre, 0, sizeof(pre)); n   for (int i = 0; i <= st; i++) dis[i] = 1; n   q.push(s); n   vis[s] = 1, dis[s] = 0; n   while (!q.empty()) { n     int now = q.front(); n     vis[now] = 0; n     q.pop(); n     for (int i = head[now]; i != -1; i = e[i].next) { n       if (!e[i].flow) continue; n       if (dis[now] + e[i].cost < dis[e[i].to]) { n         dis[e[i].to] = dis[now] + e[i].cost; n         pre[e[i].to] = i; n         if (!vis[e[i].to]) { n           vis[e[i].to] = 1; n           q.push(e[i].to); n         } n       } n     } n   } n   if (dis[t] >= 0) return 1; n   int u = t; n   while (u != s) { n     int xy = pre[u]; n     e[xy].flow -= 1; n     e[xy ^ 1].flow += 1; n     u = e[xy].from; n   } n   return dis[t]; n } n int MFMC(int s, int t, int ct) { n   int sum = 0; n   int a; n   while ((a = spfa(s, t, ct)) < 0) { n     sum += a; n   } n   return sum; n } n struct Ac { n   int next[maxn][26], fail[maxn]; n   int size; n   queue<int> q; n   void insert(char *S, int xi) { n     int len = strlen(S); n     int now = 0; n     for (int i = 0; i < len; i++) { n       char c = S[i]; n       if (next[now][c -  a ] == 0) { n         next[now][c -  a ] = ++size; n         now = size; n       } else { n         now = next[now][c -  a ]; n       } n     } n     pos[now].push_back(xi); n   } n   void build() { n     for (int i = 0; i < 26; i++) { n       if (next[0][i]) q.push(next[0][i]); n     } n     while (!q.empty()) { n       int now = q.front(); n       q.pop(); n       for (int i = 0; i < 26; i++) { n         if (next[now][i]) { n           fail[next[now][i]] = next[fail[now]][i]; n           q.push(next[now][i]); n         } else n           next[now][i] = next[fail[now]][i]; n       } n     } n   } n   void match(char *S) { n     int le = strlen(S); n     int now = 0; n     for (int i = 0; i < le; i++) { n       int x = S[i] -  a ; n       now = next[now][x]; n       int temp = now; n       while (temp) { n         for (int j = 0; j < pos[temp].size(); j++) { n           int k = pos[temp][j]; n           line(i - len[k] + 1, i + 1, 1, -val[k]); n         } n         temp = fail[temp]; n       } n     } n   } n } ac; n int main() { n   memset(head, -1, sizeof(head)); n   cnt = 0; n   int n; n   scanf( %d , &n); n   scanf( %s , pat); n   int m; n   scanf( %d , &m); n   for (int i = 0; i < m; i++) { n     scanf( %s %d , str, &val[i]); n     len[i] = strlen(str); n     ac.insert(str, i); n   } n   int x; n   scanf( %d , &x); n   int s = n + 1, t = n + 2; n   line(s, 0, x, 0); n   line(n, t, x, 0); n   for (int i = 0; i < n; i++) { n     line(i, i + 1, x, 0); n   } n   ac.build(); n   ac.match(pat); n   int ans = -MFMC(s, t, n + 2); n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline const int read() { n   int r = 0, k = 1; n   char c = getchar(); n   for (; c <  0  || c >  9 ; c = getchar()) n     if (c ==  - ) k = -1; n   for (; c >=  0  && c <=  9 ; c = getchar()) r = r * 10 + c -  0 ; n   return k * r; n } n long long n; n int k, a[110]; n bool vis[1000010]; n long long dp[110][100000]; n long long dfs(int pos, long long n) { n   if (pos == k + 1) return n; n   if (n <= 0) return 0; n   if (n < 100000 && dp[pos][n] != -1) return dp[pos][n]; n   long long ans = dfs(pos + 1, n) - dfs(pos + 1, n / a[pos]); n   if (n < 100000) dp[pos][n] = ans; n   return ans; n } n void input() { n   memset(dp, -1, sizeof(dp)); n   cin >> n >> k; n   for (int i = (1), _ = (k); i <= _; i++) a[i] = read(); n   sort(a + 1, a + k + 1, greater<int>()); n } n void solve() { cout << dfs(1, n) << endl; } n int main() { n   input(), solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n int k; n string s, t; n int dp[100005][2], cnt[2]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> s >> t; n   cin >> k; n   int n = s.length(); n   for (int i = 1; i < n; i++) { n     int l = n - i; n     string tmp = s.substr(i, l) + s.substr(0, i); n     if (tmp == t) n       cnt[0]++; n     else n       cnt[1]++; n   } n   if (s == t) n     dp[0][0]++, cnt[0]++; n   else n     dp[0][1]++, cnt[1]++; n   for (int i = 1; i <= k; i++) { n     dp[i][0] = n         (dp[i - 1][0] * 1ll * (cnt[0] - 1) + dp[i - 1][1] * 1ll * cnt[0]) % mod; n     dp[i][1] = n         (dp[i - 1][0] * 1ll * cnt[1] + dp[i - 1][1] * 1ll * (cnt[1] - 1)) % mod; n   } n   printf( %d n , dp[k][0]); n } n


#include <bits/stdc++.h> n using namespace std; n long long ar[1000000 + 9]; n long long POW1[1000000 + 9]; n long long POW2[1000000 + 9]; n pair<long long, long long> KEEP[1000000 + 9]; n map<pair<long long, long long>, long long> mymap; n struct data { n   long long a, b, val; n }; n vector<data> vec; n long long query(int beg, int end) { n   if (beg == end) return 0; n   int mid = (beg + end) >> 1; n   long long ans, i, val, a, b, len; n   ans = query(beg, mid) + query(mid + 1, end); n   vec.clear(); n   mymap.clear(); n   for (i = mid; i >= beg; i--) { n     if (vec.size() && vec[vec.size() - 1].val == ar[i]) { n       vec.pop_back(); n       a = 0; n       b = 0; n       if (vec.size()) a = vec[vec.size() - 1].a; n       if (vec.size()) b = vec[vec.size() - 1].b; n     } else { n       a = 0; n       b = 0; n       if (vec.size()) a = vec[vec.size() - 1].a; n       if (vec.size()) b = vec[vec.size() - 1].b; n       len = vec.size() + 1; n       a += ar[i] * POW1[len]; n       b += ar[i] * POW2[len]; n       a %= 1000000007; n       b %= 1000000007; n       data curr; n       curr.a = a; n       curr.b = b; n       curr.val = ar[i]; n       vec.push_back(curr); n     } n     val = mymap[make_pair(a, b)]; n     mymap[make_pair(a, b)] = val + 1; n   } n   vec.clear(); n   for (i = mid + 1; i <= end; i++) { n     if (vec.size() && vec[vec.size() - 1].val == ar[i]) { n       vec.pop_back(); n       a = 0; n       b = 0; n       if (vec.size()) a = vec[vec.size() - 1].a; n       if (vec.size()) b = vec[vec.size() - 1].b; n     } else { n       a = 0; n       b = 0; n       if (vec.size()) a = vec[vec.size() - 1].a; n       if (vec.size()) b = vec[vec.size() - 1].b; n       len = vec.size() + 1; n       a += ar[i] * POW1[len]; n       b += ar[i] * POW2[len]; n       a %= 1000000007; n       b %= 1000000007; n       data curr; n       curr.a = a; n       curr.b = b; n       curr.val = ar[i]; n       vec.push_back(curr); n     } n     val = mymap[make_pair(a, b)]; n     ans += val; n   } n   return ans; n } n int main() { n   long long i, j, k, l, n, m = 0, tot, temp, curr, val, prev, a, b, c, t, ans, n                            expon, flag, x, y, siz, w, h, d, ii, jj, xx, yy, ind, n                            len, max_n, q, s, f, g; n   POW1[0] = POW2[0] = 1; n   for (i = 1; i <= 1000000; i++) POW1[i] = (POW1[i - 1] * 31) % 1000000007; n   for (i = 1; i <= 1000000; i++) POW2[i] = (POW2[i - 1] * 37) % 1000000007; n   cin >> q; n   while (q--) { n     scanf( %lld , &n); n     for (i = 1; i <= n; i++) scanf( %lld , &ar[i]); n     ans = query(1, n); n     printf( %lld n , ans); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 10; n const long long INF = (long long)1e17 + 10; n int n, m, s, t; n int a[N], b[N], l[N]; n struct edge { n   int to, cost; n   edge(int to, int cost) : to(to), cost(cost) {} n }; n vector<edge> e; n vector<int> g[N]; n vector<int> g2[N]; n long long diss[N], dist[N]; n long long len; n bool vis[N], ans[N]; n map<int, long long> mp; n set<pair<pair<long long, long long>, int> > st; n void dijkstra(int sc, vector<int> g[N], long long *dis) { n   fill(dis + 1, dis + n + 1, INF); n   priority_queue<pair<long long, int> > pq; n   pq.push(make_pair(0, sc)); n   memset(vis, 0, sizeof(vis)); n   while (!pq.empty()) { n     pair<long long, int> tmp = pq.top(); n     pq.pop(); n     if (vis[tmp.second]) continue; n     vis[tmp.second] = 1; n     dis[tmp.second] = -tmp.first; n     for (auto j : g[tmp.second]) { n       int u = e[j].to; n       int c = e[j].cost; n       if (vis[u]) continue; n       pair<long long, int> nxt = make_pair(-dis[tmp.second] - c, u); n       pq.push(nxt); n     } n   } n } n int main() { n   scanf( %d%d%d%d , &n, &m, &s, &t); n   for (int i = 0; i < m; ++i) { n     scanf( %d%d%d , a + i, b + i, l + i); n     g[a[i]].push_back(e.size()); n     e.push_back(edge(b[i], l[i])); n     g2[b[i]].push_back(e.size()); n     e.push_back(edge(a[i], l[i])); n   } n   dijkstra(s, g, diss); n   dijkstra(t, g2, dist); n   len = diss[t]; n   for (int i = 0; i < m; ++i) { n     if (diss[a[i]] + dist[b[i]] + l[i] == len) { n       st.insert(make_pair(make_pair(diss[a[i]], diss[b[i]]), i)); n       ans[i] = 1; n     } n   } n   long long mx = 0; n   for (auto k : st) { n     mp[k.second] = mx; n     mx = max(k.first.second, mx); n   } n   for (int i = 0; i < m; ++i) { n     if (diss[a[i]] + dist[b[i]] + l[i] == len) { n       st.erase(make_pair(make_pair(diss[a[i]], diss[b[i]]), i)); n       auto it = st.lower_bound(make_pair(make_pair(diss[a[i]], -1), -1)); n       if (!(it == st.end() || it->first.first == diss[b[i]])) { n         ans[i] = 0; n       } n       if (mp[i] > diss[a[i]]) { n         ans[i] = 0; n       } n       st.insert(make_pair(make_pair(diss[a[i]], diss[b[i]]), i)); n     } n   } n   for (int i = 0; i < m; ++i) { n     if (ans[i]) { n       puts( YES ); n     } else { n       long long mn = diss[a[i]] + dist[b[i]]; n       if (len - 1 > mn) { n         printf( CAN %d n , l[i] - (len - 1 - mn)); n       } else { n         puts( NO ); n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = INT_MAX; n const long long INFL = LLONG_MAX; n int N, M, X[1000], a, b, c; n int main() { n   ios_base::sync_with_stdio(0); n   cout.precision(15); n   cin >> N >> M; n   for (int(i) = 1; (i) <= (N); (i)++) cin >> X[i]; n   long double answer = 0; n   for (int(i) = 1; (i) <= (M); (i)++) { n     cin >> a >> b >> c; n     answer = max(answer, (long double)(X[a] + X[b]) / c); n   } n   cout << answer << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, l = 1, a; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a; n     if (l == 1) { n       if (a % 2 == 1) { n         cout << 2 << endl; n         l = 1; n       } else { n         l = 2; n         cout << 1 << endl; n       } n       continue; n     } n     if (l == 2) { n       if (a % 2 == 1) { n         l = 2; n         cout << 1 << endl; n       } else { n         l = 1; n         cout << 2 << endl; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXQ = 2e5; n vector<int> solve(int K, vector<tuple<uint32_t, uint32_t, int> > V) { n   uint32_t mask = (((uint32_t)1) << K) - 1; n   vector<int> HT(1 << K), LT(1 << K); n   for (auto [l, r, v] : V) n     if (v) { n       uint32_t lhi = l >> K, llo = l & mask; n       uint32_t rhi = r >> K, rlo = r & mask; n       if (lhi == rhi) { n         LT[llo]++; n         if (rlo != ((1 << K) - 1)) LT[rlo + 1]--; n       } else { n         LT[llo]++; n         LT[0]++; n         if (rlo != ((1 << K) - 1)) LT[rlo + 1]--; n       } n       HT[lhi]++; n       if (rhi != ((1 << K) - 1)) HT[rhi + 1]--; n     } n   for (int i = 0; i < (1 << K) - 1; ++i) HT[i + 1] += HT[i]; n   for (int i = 0; i < (1 << K); ++i) n     if (HT[i]) HT[i] = 1; n   for (int i = 0; i < (1 << K) - 1; ++i) LT[i + 1] += LT[i]; n   for (int i = 0; i < (1 << K); ++i) n     if (LT[i]) LT[i] = 1; n   auto ans = LT; n   copy(HT.begin(), HT.end(), back_inserter(ans)); n   for (int i = 0; i < (1 << K) - 1; ++i) HT[i + 1] += HT[i]; n   for (int i = 0; i < (1 << K) - 1; ++i) LT[i + 1] += LT[i]; n   auto SH = [&](int L, int R) { n     int ans = HT[R]; n     if (L) ans -= HT[L - 1]; n     return ans; n   }; n   auto SL = [&](int L, int R) { n     int ans = LT[R]; n     if (L) ans -= LT[L - 1]; n     return ans; n   }; n   for (auto [l, r, v] : V) n     if (!v) { n       uint32_t lhi = l >> K, llo = l & mask; n       uint32_t rhi = r >> K, rlo = r & mask; n       if (lhi == rhi) { n         if (SH(lhi, lhi) != 0 && SL(llo, rlo) != 0) return {}; n       } else { n         if (SH(lhi, lhi) != 0 && SL(llo, (1 << K) - 1) != 0) return {}; n         if (SH(lhi + 1, rhi - 1) != 0 && SL(0, (1 << K) - 1) != 0) return {}; n         if (SH(rhi, rhi) != 0 && SL(0, rlo) != 0) return {}; n       } n     } n   return ans; n } n int K, Q; n uint32_t L[MAXQ], R[MAXQ], V[MAXQ]; n int main() { n   scanf( %d%d , &K, &Q); n   for (int i = 0; i < Q; ++i) { n     scanf( %  SCNu32  %  SCNu32  %  SCNu32, L + i, R + i, V + i); n   } n   vector<vector<int> > ans(16); n   for (int i = 0; i < 16; ++i) { n     vector<tuple<uint32_t, uint32_t, int> > P; n     for (int j = 0; j < Q; ++j) P.emplace_back(L[j], R[j], (V[j] >> i) & 1); n     ans[i] = solve(K, P); n     if (ans[i].size() == 0) { n       puts( impossible ); n       return 0; n     } n   } n   puts( possible ); n   for (int i = 0; i < (2 << K); ++i) { n     int r = 0; n     for (int j = 0; j < 16; ++j) n       if (ans[j][i]) r |= 1 << j; n     printf( %d n , r); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int rmax = 15; n const int nmax = 100500; n struct DP { n   int x; n   int y; n   int z; n   int i; n   int j; n   int k; n }; n bool operator<(const DP& l, const DP& r) { n   return l.x < r.x || (l.x == r.x && l.y < r.y) || n          (l.x == r.x && l.y == r.y && l.z < r.z); n } n class SparseTable { n  public: n   SparseTable(vector<pair<int, int> >& vals) { n     N = vals.size(); n     sparse.assign(N, vector<pair<int, int> >(rmax)); n     init(); n     build(vals); n   } n   void build(vector<pair<int, int> >& vals) { n     for (int i = 0; i < vals.size(); ++i) { n       sparse[i][0] = vals[i]; n     } n     for (int r = 1; r < rmax; ++r) { n       for (int i = 0; i < N; ++i) { n         int t = min(N - 1, i + (1 << (r - 1))); n         sparse[i][r] = max(sparse[i][r - 1], sparse[t][r - 1]); n       } n     } n   } n   pair<int, int> getMax(int l, int r) { n     int len = r - l + 1; n     int k = deg[len]; n     return max(sparse[l][k], sparse[l + len - (1 << k)][k]); n   } n   void init() { n     deg.assign(nmax, 0); n     deg[0] = -1; n     for (int i = 1; i < nmax; ++i) { n       deg[i] = deg[i - 1]; n       if (!(i & (i - 1))) { n         ++deg[i]; n       } n     } n   } n  n  private: n   vector<vector<pair<int, int> > > sparse; n   int N = 0; n   vector<int> deg; n }; n int main() { n   ios_base::sync_with_stdio(false); n   int n; n   cin >> n; n   vector<int> vec(n); n   vector<pair<int, int> > a; n   for (int i = 0; i < n; ++i) { n     cin >> vec[i]; n     a.push_back({vec[i], i}); n   } n   sort((a).begin(), (a).end()); n   vector<pair<int, int> > vals; n   vals.push_back({a[0].first, 0}); n   for (int i = 1; i < n; ++i) { n     vals.push_back({a[i].first - a[i - 1].first, i}); n   } n   SparseTable st(vals); n   DP ret; n   ret.x = -1; n   ret.y = -1; n   ret.z = -1; n   ret.i = 0; n   ret.j = 0; n   ret.k = 0; n   for (int i = n - 1; i >= 0; --i) { n     for (int j = i - 1; j >= 0; --j) { n       int len_a = n - 1 - i + 1; n       int len_b = i - 1 - j + 1; n       if (len_a > 2 * len_b || len_b > 2 * len_a) { n         continue; n       } n       int max_len_c = min({j, 2 * len_a, 2 * len_b}); n       int min_len_c = max({1, (len_a + 1) / 2, (len_b + 1) / 2}); n       if (max_len_c < min_len_c) { n         continue; n       } n       pair<int, int> p = st.getMax(j - max_len_c, j - min_len_c); n       DP temp; n       temp.x = a[i].first - a[i - 1].first; n       temp.y = a[j].first - a[j - 1].first; n       temp.z = p.first; n       temp.i = i; n       temp.j = j; n       temp.k = p.second; n       if (ret < temp) { n         ret = temp; n       } n     } n   } n   vector<int> ans(n); n   for (int i = 0; i < ret.k; ++i) { n     ans[a[i].second] = -1; n   } n   for (int i = ret.k; i < ret.j; ++i) { n     ans[a[i].second] = 3; n   } n   for (int i = ret.j; i < ret.i; ++i) { n     ans[a[i].second] = 2; n   } n   for (int i = ret.i; i < n; ++i) { n     ans[a[i].second] = 1; n   } n   for (int i = 0; i < n; ++i) { n     cout << ans[i] <<    ; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC target( sse4,avx ) n const int inf = 100000; n int rec(size_t gl, std::vector<int>& odd, std::vector<bool>& flags, n         std::vector<std::vector<int>>& graph) { n   if (gl == odd.size()) { n     return 0; n   } n   if (flags[gl]) { n     return rec(gl + 1, odd, flags, graph); n   } n   int res = 1000000000; n   for (size_t i = gl + 1; i < odd.size(); i++) { n     if (!flags[i]) { n       flags[i] = true; n       res = std::min(res, n                      graph[odd[gl]][odd[i]] + rec(gl + 1, odd, flags, graph)); n       flags[i] = false; n     } n   } n   return res; n } n void run(std::istream& in, std::ostream& out) { n   int n, m; n   in >> n >> m; n   std::vector<std::vector<int>> graph(n, std::vector<int>(n, inf)); n   for (int i = 0; i < n; i++) graph[i][i] = 0; n   std::vector<int> degs(n); n   int64_t sum = 0; n   for (int i = 0; i < m; i++) { n     int x, y, w; n     in >> x >> y >> w; n     x--; n     y--; n     degs[x]++; n     degs[y]++; n     sum += w; n     graph[x][y] = std::min(graph[x][y], w); n     graph[y][x] = std::min(graph[y][x], w); n   } n   for (size_t i = 0; i < n; i++) { n     for (size_t j = 0; j < n; j++) { n       for (size_t k = 0; k < n; k++) { n         if (graph[j][k] > graph[j][i] + graph[i][k]) { n           graph[j][k] = graph[j][i] + graph[i][k]; n         } n       } n     } n   } n   for (size_t i = 0; i < n; i++) { n     if (graph[0][i] == inf && degs[i] != 0) { n       out <<  -1  << std::endl; n       return; n     } n   } n   std::vector<int> odd; n   for (size_t i = 0; i < n; i++) { n     if ((degs[i] % 2) != 0) { n       odd.push_back(i); n     } n   } n   std::vector<bool> flags(odd.size()); n   sum += rec(0, odd, flags, graph); n   out << sum << std::endl; n } n int main() { n   std::cin.sync_with_stdio(false); n   std::cin.tie(nullptr); n   run(std::cin, std::cout); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void Oli() { n   int a; n   cin >> a; n   vector<pair<int, int>> pr(a); n   for (int i = 0; i < a; i++) { n     cin >> pr[i].first >> pr[i].second; n   } n   int s = 0; n   int k = 1; n   for (int i = 0; i < pr.size(); i++) { n     if (pr[i].second >= 1) { n       k--; n       k += pr[i].second; n       s += pr[i].first; n       pr.erase(pr.begin() + i); n       i--; n     } n   } n   sort(pr.rbegin(), pr.rend()); n   int i = 0; n   while (k > 0 && i < pr.size()) { n     s += pr[i].first; n     i++; n     k--; n   } n   cout << s << endl; n } n int main() { n   int t = 1; n   while (t--) { n     Oli(); n   } n   return 0; n } n


#include <bits/stdc++.h> n int loya[8]; n int tloyal[8]; n int level[8]; n double pb[1 << 8]; n int N, K, A; n void init_search(int state, int B, int deepth, int cnt) { n   if (deepth == N) { n     pb[state] = cnt > (N >> 1) ? 1.0 : A / (double)(A + B); n   } else { n     init_search(state | (1 << deepth), B, deepth + 1, cnt + 1); n     init_search(state, B + level[deepth], deepth + 1, cnt); n   } n } n double calcu(double prb, int state, int deepth) { n   return (deepth == N) n              ? prb * pb[state] n              : calcu(prb * (tloyal[deepth] / 10.0), state | (1 << deepth), n                      deepth + 1) + n                    calcu(prb * (1 - tloyal[deepth] / 10.0), state, deepth + 1); n } n double search(int candy, int deepth) { n   double ans = 0.0; n   if (deepth == N) { n     ans = calcu(1.0, 0, 0); n   } else { n     int i; n     for (i = 0; i <= candy && loya[deepth] + i <= 10; i++) { n       tloyal[deepth] = loya[deepth] + i; n       double tmp = search(candy - i, deepth + 1); n       ans = tmp > ans ? tmp : ans; n     } n   } n   return ans; n } n int main() { n   while (scanf( %d %d %d , &N, &K, &A) == 3) { n     int i; n     for (i = 0; i < N; i++) { n       scanf( %d %d , level + i, loya + i); n       loya[i] /= 10; n     } n     init_search(0, 0, 0, 0); n     printf( %.10f n , search(K, 0)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long h, n; n long long ret = 0LL; n int a[60]; n long long two(int x) { n   if (x == 0) return 1LL; n   long long half = two(x / 2); n   half *= half; n   return x % 2 ? half * 2 : half; n } n void solve() { n   int c = 0; n   --n; n   while (n) { n     a[c++] = n % 2; n     n >>= 1; n   } n   for (int i = c; i < h; ++i) a[i] = 0; n   int e = 0; n   for (int i = h - 1; i >= 0; --i) { n     if (a[i] != e) { n       ret += two(i + 1); n     } n     if (a[i] == e) { n       ret += 1; n       e = !e; n     } n   } n   cout << ret << endl; n } n int main() { n   cin >> h >> n; n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k, a[101], m, ans; n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> k; n     if (k % 2) a[++m] = k; n     ans += k; n   } n   if (m == 0) { n     cout << 0 << endl; n     return 0; n   } n   if (m % 2 == 0) { n     sort(a + 1, a + m + 1); n     ans -= a[1]; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, k; n int a[100010]; n struct node { n   int x, id; n } p[100010]; n int main() { n   while (scanf( %d%d , &n, &k) != EOF) { n     for (int i = 1; i <= n; i++) { n       scanf( %d , &a[i]); n     } n     int ans = 99999999; n     int pos = 1; n     for (int i = 1; i <= n; i++) { n       bool flag = false; n       int cnt = 0; n       for (int j = i; j >= 1; j--) { n         if (a[i] - (i - j) * k == a[j]) continue; n         cnt++; n         if (a[i] - (i - j) * k <= 0) { n           flag = true; n           break; n         } n       } n       if (flag) continue; n       int cnt1 = 0; n       for (int j = i; j <= n; j++) { n         if (a[i] + (j - i) * k == a[j]) continue; n         cnt1++; n       } n       if (cnt1 + cnt < ans) { n         ans = cnt1 + cnt; n         pos = i; n       } n     } n     printf( %d n , ans); n     for (int j = 1; j <= pos; j++) { n       if (a[j] + (pos - j) * k == a[pos]) continue; n       if (a[j] < a[pos] - (pos - j) * k) { n         printf( + %d %d n , j, a[pos] - (pos - j) * k - a[j]); n       } else { n         printf( - %d %d n , j, a[j] - (a[pos] - (pos - j) * k)); n       } n     } n     for (int j = pos; j <= n; j++) { n       if (a[pos] + (j - pos) * k == a[j]) continue; n       if (a[j] < a[pos] + (j - pos) * k) { n         printf( + %d %d n , j, a[pos] + (j - pos) * k - a[j]); n       } else { n         printf( - %d %d n , j, a[j] - (a[pos] + (j - pos) * k)); n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline void upd_min(T& dest, const T& src) { n   if (src < dest) dest = src; n } n template <typename T> n inline void upd_max(T& dest, const T& src) { n   if (dest < src) dest = src; n } n template <typename T, int n> n struct Queue { n   T q[n]; n   int qf, qr; n   Queue() : qf(0), qr(0) {} n   inline void clear() { qf = qr = 0; } n   inline bool empty() { return qf == qr; } n   inline void push(const T& data) { n     q[qr] = data; n     if (++qr == n) qr = 0; n   } n   inline void pop() { n     if (++qf == n) qf = 0; n   } n   inline T& front() { return q[qf]; } n }; n template <int maxN> n struct Disjoint { n   int N; n   int p[maxN]; n   inline void init(int n) { n     N = n; n     for (int i = 0; i < N; ++i) p[i] = i; n   } n   inline int root(int i) { n     for (; i != p[i]; i = p[i]) p[i] = p[p[i]]; n     return i; n   } n   inline void unite(int a, int b) { p[root(b)] = root(a); } n   inline bool query(int a, int b) { return root(a) == root(b); } n }; n template <typename T, int N> n struct Stack { n   T s[N]; n   int index; n   Stack() : index(0) {} n   inline void clear() { index = 0; } n   inline T& top() { return s[index]; } n   inline void pop() { --index; } n   inline bool empty() { return index == 0; } n   inline void push(T data) { s[++index] = data; } n }; n int nextInt() { n   int ret; n   char c; n   while (c = getchar(), c <  0  || c >  9 ) n     ; n   ret = c -  0 ; n   while (c = getchar(), c >=  0  && c <=  9 ) ret = ret * 10 + c -  0 ; n   return ret; n } n const int inf = 0x7f7f7f7f; n int N; n char c[255]; n int main() { n   N = nextInt(); n   for (int t = 1; t <= N; t++) { n     scanf( %s , c); n     int ll = strlen(c); n     int sf(0); n     for (int i = 0; i < ll; i++) n       if (c[i] ==  : ) sf++; n     if (sf == 2 && ll == 2) { n       printf( 0000:0000:0000:0000:0000:0000:0000:0000 n ); n     } else { n       int ind(1), k(1); n       for (int i = 1; i < ll; i++) { n         if (c[i] !=  : ) n           ind++; n         else { n           if (c[i - 1] ==  : ) { n             if (i == 1) sf--; n             for (int j = 1; j <= 8 - sf; j++) { n               if (k != 1) printf( : ); n               printf( 0000 ); n               k++; n             } n           } else { n             if (k != 1) printf( : ); n             k++; n             for (int j = 1; j <= 4 - ind; j++) printf( 0 ); n             for (int j = 1; j <= ind; j++) printf( %c , c[i - ind - 1 + j]); n           } n           ind = 0; n         } n       } n       if (k == 8) { n         printf( : ); n         for (int j = 1; j <= 4 - ind; j++) printf( 0 ); n         for (int j = 1; j <= ind; j++) printf( %c , c[ll - ind - 1 + j]); n       } n       printf(  n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int Max = 2e5 + 10; n int n, k; n int v, u; n vector<int> stl[Max]; n struct node { n   long long cou; n   long long huo; n   node() { cou = huo = 0; } n } point[Max]; n int vis[Max]; n long long tot = 0; n void dfs(int idex, int cou) { n   vis[idex] = 1; n   point[idex].cou = cou; n   int flag = 0; n   for (int i = 0; i < stl[idex].size(); i++) { n     if (vis[stl[idex][i]] == 0) { n       dfs(stl[idex][i], cou + 1); n       flag = 1; n     } n   } n   if (!flag) { n     point[idex].huo = 1; n     return; n   } n   for (int i = 0; i < stl[idex].size(); i++) n     point[idex].huo += point[stl[idex][i]].huo; n   point[idex].huo++; n } n long long ans[Max]; n int main() { n   cin >> n >> k; n   int flag = 1; n   for (int i = 1; i <= n - 1; i++) { n     cin >> v >> u; n     stl[v].push_back(u); n     stl[u].push_back(v); n   } n   dfs(1, 1); n   for (int i = 1; i <= n; i++) { n     ans[i] = point[i].cou - point[i].huo; n   } n   sort(ans + 1, ans + n + 1); n   for (int i = n; i > n - k; i--) { n     tot += ans[i]; n   } n   cout << tot << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, k; n string s; n int a[4][100]; n vector<int> seq; n vector<pair<int, int> > poseq; n bool check() { n   for (int i = 0; i < n; i++) { n     if (a[1][i] != 0 || a[2][i] != 0) { n       return true; n     } n   } n   return false; n } n pair<int, int> next(pair<int, int> pos) { n   if (pos.first == 1) { n     if (pos.second != 0) { n       pos.second -= 1; n     } else { n       pos.first = 2; n     } n   } else { n     if (pos.second < n - 1) { n       pos.second += 1; n     } else { n       pos.first = 1; n     } n   } n   return pos; n } n void rotate() { n   int r, c; n   for (int i = 0; i < n; i++) { n     if (a[1][i] == 0) { n       r = 1; n       c = i; n     } n     if (a[2][i] == 0) { n       r = 2; n       c = i; n     } n   } n   pair<int, int> init = make_pair(r, c), prev = init, curr = next(init); n   while (init != curr) { n     if (a[curr.first][curr.second] != 0) { n       a[prev.first][prev.second] = a[curr.first][curr.second]; n       seq.push_back(a[curr.first][curr.second]); n       a[curr.first][curr.second] = 0; n       poseq.push_back(prev); n     } n     prev = curr; n     curr = next(prev); n   } n } n void park() { n   for (int i = 0; i < n; i++) { n     if (a[1][i] > 0 && a[1][i] == a[0][i]) { n       seq.push_back(a[1][i]); n       poseq.push_back(make_pair(0, i)); n       a[1][i] = 0; n     } n     if (a[2][i] > 0 && a[2][i] == a[3][i]) { n       seq.push_back(a[2][i]); n       poseq.push_back(make_pair(3, i)); n       a[2][i] = 0; n     } n   } n } n void run() { n   cin >> n >> k; n   for (int i = 0; i < 4; i++) { n     for (int j = 0; j < n; j++) { n       cin >> a[i][j]; n     } n   } n   if (k == 2 * n) { n     bool allfull = true; n     for (int i = 0; i < n; i++) { n       if (a[1][i] == a[0][i] || a[2][i] == a[3][i]) { n         allfull = false; n         break; n       } n     } n     if (allfull) { n       cout << setprecision(30) << -1 << endl; n       return; n       ; n     } n   } n   park(); n   while (check()) { n     rotate(); n     park(); n   } n   cout << setprecision(30) << seq.size() << endl; n   ; n   for (int i = 0; i < (int)seq.size(); i++) { n     cout << setprecision(30) << seq[i] <<    ; n     ; n     cout << setprecision(30) << poseq[i].first + 1 <<    ; n     ; n     cout << setprecision(30) << poseq[i].second + 1 << endl; n     ; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   int tests = 1; n   for (int i = 1; i <= tests; i++) run(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k, l, r, m; n   cin >> n >> k; n   deque<pair<int, int>> bfs; n   vector<int> lines; n   vector<int> ans(n); n   --k; n   bfs.push_back({0, n}); n   while (!bfs.empty()) { n     l = bfs[0].first; n     r = bfs[0].second; n     m = (l + r) / 2; n     bfs.pop_front(); n     if (k > 0 && r - l > 1) { n       k -= 2; n       lines.push_back(m); n       bfs.push_back({l, m}); n       bfs.push_back({m, r}); n     } n   } n   if (k != 0) { n     cout << -1; n     return 0; n   } else { n     int num = 1; n     lines.push_back(0); n     lines.push_back(n); n     sort(lines.begin(), lines.end()); n     for (int i = lines.size() - 2; i >= 0; --i) { n       for (int j = lines[i]; j != lines[i + 1]; ++j) { n         ans[j] = num; n         ++num; n       } n     } n   } n   for (int i = 0; i != n; ++i) { n     cout << ans[i] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1000010, mod = 1e9 + 7, INF = 0x3f3f3f3f; n const double eps = 1e-6; n int n, m; n int id[N]; n vector<int> v[N]; n bool st[N]; n bool check() { n   int cnt = 0; n   for (int i = 1; i <= n; i++) n     if (!id[i]) { n       cnt++; n       id[i] = cnt; n       int s = 0; n       if (cnt > 3) return false; n       for (auto x : v[i]) st[x] = 1, s++; n       st[i] = 1; n       for (int j = 1; j <= n; j++) n         if (!st[j]) { n           if (id[j]) return false; n           id[j] = cnt; n           int sum = 0; n           for (auto x : v[j]) { n             if (!st[x]) return false; n             sum++; n           } n           if (sum != s) return false; n         } n       for (auto x : v[i]) st[x] = 0; n       st[i] = 0; n     } n   if (cnt < 3) return false; n   for (int i = 1; i <= n; i++) printf( %d  , id[i]); n   puts(  ); n   return true; n } n int main() { n   scanf( %d%d , &n, &m); n   while (m--) { n     int a, b; n     scanf( %d%d , &a, &b); n     v[a].push_back(b); n     v[b].push_back(a); n   } n   if (!check()) puts( -1 ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int Maxn = 1010; n const int inf = 1 << 23; n int ch[Maxn << 1][3], siz[Maxn << 1], maxs[Maxn << 1], alls, core; n bool vis[Maxn << 1]; n int n, tot, T; n void dfs(int x, int fa) { n   siz[x] = 1; n   maxs[x] = 0; n   for (int i = 0; i < 3; i++) { n     int y = ch[x][i]; n     if (y == fa || !y || vis[y]) continue; n     dfs(y, x); n     if (maxs[x] < siz[y]) maxs[x] = siz[y]; n     siz[x] += siz[y]; n   } n   if (maxs[x] < alls - siz[x]) maxs[x] = alls - siz[x]; n   if (maxs[x] < maxs[core] || (maxs[x] == maxs[core] && x > core)) core = x; n } n void dfs2(int x, int fa) { n   siz[x] = 1; n   for (int i = 0; i < 3; i++) { n     int y = ch[x][i]; n     if (y == fa || !y || vis[y]) continue; n     dfs(y, x); n     siz[x] += siz[y]; n   } n } n int getl(int x) { return x <= n ? x : getl(ch[x][1]); } n void solve(int x) { n   vis[x] = true; n   if (x <= n) { n     tot++; n     int fa = ch[x][0]; n     if (ch[fa][1] == x) n       ch[fa][1] = tot; n     else n       ch[fa][2] = tot; n     ch[tot][0] = fa; n     ch[x][0] = tot; n     ch[tot][1] = x; n     ch[tot][2] = T; n     ch[T][0] = tot; n     return; n   } n   int l1 = getl(ch[x][1]), l2 = getl(ch[x][2]); n   cout << l1 <<     << l2 <<     << T << endl; n   char c; n   cin >> c; n   if (c ==  X ) { n     if (vis[ch[x][0]] || !ch[x][0]) { n       tot++; n       int fa = ch[x][0]; n       if (fa) { n         if (ch[fa][1] == x) n           ch[fa][1] = tot; n         else n           ch[fa][2] = tot; n       } n       ch[tot][0] = fa; n       ch[x][0] = tot; n       ch[tot][1] = x; n       ch[tot][2] = T; n       ch[T][0] = tot; n       return; n     } n     dfs2(ch[x][0], x); n     alls = siz[ch[x][0]]; n     core = 0; n     dfs(ch[x][0], x); n     solve(core); n   } else if (c ==  Y ) { n     if (vis[ch[x][2]]) { n       tot++; n       ch[tot][0] = x; n       ch[tot][1] = ch[x][2]; n       ch[tot][2] = T; n       ch[ch[x][2]][0] = tot; n       ch[x][2] = tot; n       ch[T][0] = tot; n       return; n     } n     dfs2(ch[x][2], x); n     alls = siz[ch[x][2]]; n     core = 0; n     dfs(ch[x][2], x); n     solve(core); n   } else { n     if (vis[ch[x][1]]) { n       tot++; n       ch[tot][0] = x; n       ch[tot][1] = ch[x][1]; n       ch[tot][2] = T; n       ch[ch[x][1]][0] = tot; n       ch[x][1] = tot; n       ch[T][0] = tot; n       return; n     } n     dfs2(ch[x][1], x); n     alls = siz[ch[x][1]]; n     core = 0; n     dfs(ch[x][1], x); n     solve(core); n   } n } n int main() { n   int i, j, k; n   cin >> n; n   tot = n; n   ch[1][0] = ch[2][0] = ++tot; n   ch[tot][1] = 1; n   ch[tot][2] = 2; n   maxs[0] = inf; n   for (T = 3; T <= n; T++) { n     memset(vis, false, sizeof(vis)); n     alls = 2 * T - 3; n     core = 0; n     dfs(1, 0); n     solve(core); n   } n   cout <<  -1  << endl; n   for (i = 1; i <= tot; i++) { n     if (!ch[i][0]) n       cout <<  -1  ; n     else n       cout << ch[i][0] <<    ; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int to[2000]; n char used[2000]; n int a[2000]; n int s; n char res[3000]; n int calc(int x, char &f) { n   f = 0; n   int cnt = 1; n   if (x == s) f = 1; n   while (to[x] != -1) { n     ++cnt; n     x = to[x]; n     if (x == s) { n       f = 1; n       cnt = 1; n     } n   } n   return cnt; n } n int main() { n   int n, x; n   cin >> n >> x; n   --x; n   s = x; n   for (int i = 0; i < n; ++i) to[i] = -1; n   for (int i = 0; i < n; ++i) { n     cin >> to[i]; n     --to[i]; n     if (to[i] >= 0) used[to[i]] = 1; n   } n   int sl = -1; n   int q = 0; n   for (int i = 0; i < n; ++i) n     if (!used[i]) { n       char f; n       int len = calc(i, f); n       if (f) n         sl = len; n       else n         a[q++] = len; n     } n   assert(sl != -1); n   res[sl] = 1; n   for (int i = 0; i < q; ++i) { n     for (int j = n; j >= 0; --j) n       if (res[j]) res[j + a[i]] = 1; n   } n   for (int i = 0; i <= n; ++i) n     if (res[i]) cout << i <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n const int SIZE = 1000005; n char sa[SIZE], sb[SIZE]; n char rab[SIZE * 2], ba[SIZE * 2]; n int f1[SIZE * 2]; n int f2[SIZE * 2]; n int main() { n   while (gets(sa) && gets(sb)) { n     int l1 = strlen(sa); n     int l2 = strlen(sb); n     if (l1 != l2) { n       printf( -1 -1 n ); n       continue; n     } n     strcpy(rab, sa); n     reverse(rab, rab + l1); n     strcat(rab,  # ); n     strcat(rab, sb); n     rab[l1] =   0 ; n     strcpy(ba, sb); n     strcat(ba,  # ); n     strcat(ba, sa); n     ba[l1] =   0 ; n     int n = l1; n     f1[0] = 0; n     for (int i = 1; i <= 2 * n; i++) { n       f1[i] = f1[i - 1]; n       while (f1[i] > 0 && rab[f1[i]] != rab[i]) f1[i] = f1[f1[i] - 1]; n       if (rab[f1[i]] == rab[i]) f1[i]++; n     } n     f2[0] = 0; n     for (int i = 1, l = 0, r = 0; i <= 2 * n; i++) { n       f2[i] = 0; n       if (i <= r) f2[i] = min(f2[i - l], r - i + 1); n       while (i + f2[i] <= 2 * n && ba[f2[i]] == ba[i + f2[i]]) f2[i]++; n       if (i + f2[i] - 1 > r) { n         l = i; n         r = i + f2[i] - 1; n       } n     } n     int r1 = -1, r2 = -1; n     for (int i = 0; i < n - 1; i++) { n       if (sa[i] != sb[n - i - 1]) break; n       int len = f1[2 * n - i - 1]; n       if (f2[n + 1 + i + 1] >= n - i - len - 1) { n         r1 = i; n         r2 = n - len; n       } n     } n     printf( %d %d n , r1, r2); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int CeilIndex(int A[], int l, int r, int key) { n   int m; n   while (r - l > 1) { n     m = l + (r - l) / 2; n     (A[m] <= key ? l : r) = m; n   } n   return r; n } n int LongestIncreasingSubsequenceLength(int A[], int size) { n   int *tailTable = new int[size]; n   int len; n   memset(tailTable, 0, sizeof(tailTable[0]) * size); n   tailTable[0] = A[0]; n   len = 1; n   for (int i = 1; i < size; i++) { n     if (A[i] < tailTable[0]) n       tailTable[0] = A[i]; n     else if (A[i] >= tailTable[len - 1]) n       tailTable[len++] = A[i]; n     else n       tailTable[CeilIndex(tailTable, -1, len - 1, A[i])] = A[i]; n   } n   delete[] tailTable; n   return len; n } n int main() { n   int n, a[100009], c[100009]; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d , &a[i]); n     c[i] = 0; n   } n   int ans = 0; n   ans = LongestIncreasingSubsequenceLength(a, n); n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int weight(char c) { n   if (c ==  Q  || c ==  q ) return 9; n   if (c ==  R  || c ==  r ) return 5; n   if (c ==  B  || c ==  b ) return 3; n   if (c ==  N  || c ==  n ) return 3; n   if (c ==  P  || c ==  p ) return 1; n   return 0; n } n int main() { n   int white = 0, black = 0; n   string board[8]; n   for (int i = 0; i < 8; i++) { n     cin >> board[i]; n     for (int j = 0; j < 8; j++) { n       if (board[i][j] >=  A  && board[i][j] <=  Z ) n         white += weight(board[i][j]); n       else if (board[i][j] >=  a  && board[i][j] <=  z ) n         black += weight(board[i][j]); n     } n   } n   if (white > black) n     cout <<  White ; n   else if (white < black) n     cout <<  Black ; n   else n     cout <<  Draw ; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200 * 1000 + 10; n struct edge { n   int u, v, w; n   edge() {} n   edge(int u, int v, int w) : u(u), v(v), w(w) {} n } e[N]; n vector<int> vec[N]; n int n, m, root[N], mx[35][N], par[35][N], h[N], ans[N]; n bool mst[N], mark[N]; n multiset<int> add[N], rem[N]; n bool cmp(int i, int j) { return e[i].w < e[j].w; } n int dfs(int v) { n   mark[v] = true; n   for (int ind : vec[v]) { n     int u = e[ind].u + e[ind].v - v; n     if (mst[ind]) { n       if (!mark[u]) { n         h[u] = h[v] + 1; n         par[0][u] = v; n         mx[0][u] = e[ind].w; n         ans[ind] = dfs(u); n         if ((int)add[u].size() > (int)add[v].size()) swap(add[u], add[v]); n         for (int x : add[u]) add[v].insert(x); n       } n     } n   } n   for (int x : rem[v]) add[v].erase(add[v].find(x)); n   if (add[v].empty()) return -1; n   return *add[v].begin() - 1; n } n void fill_par() { n   for (int i = 0; i < n; i++) mark[i] = false; n   for (int i = 0; i < 31; i++) n     for (int j = 0; j < n; j++) par[i][j] = -1; n   dfs(0); n   for (int i = 1; i < 31; i++) n     for (int j = 0; j < n; j++) n       if (par[i - 1][j] != -1) { n         mx[i][j] = max(mx[i - 1][j], mx[i - 1][par[i - 1][j]]); n         par[i][j] = par[i - 1][par[i - 1][j]]; n       } n } n int get_root(int v) { n   if (v == root[v]) return v; n   return root[v] = get_root(root[v]); n } n pair<int, int> get_lca(int u, int v) { n   if (h[u] < h[v]) swap(u, v); n   int mxe = 0; n   for (int i = 30; i >= 0; i--) n     if ((h[u] - h[v]) & (1 << i)) { n       mxe = max(mxe, mx[i][u]); n       u = par[i][u]; n     } n   for (int i = 30; i >= 0; i--) n     if (par[i][u] != par[i][v]) { n       mxe = max(mxe, mx[i][v]), mxe = max(mxe, mx[i][u]); n       u = par[i][u], v = par[i][v]; n     } n   if (u != v) { n     mxe = max(mxe, mx[0][v]), mxe = max(mxe, mx[0][u]); n     u = par[0][u]; n   } n   return {u, mxe}; n } n void merge(int ind, int ty) { n   int u = e[ind].u, v = e[ind].v; n   int pu = get_root(u), pv = get_root(v); n   if (pu != pv) { n     mst[ind] = true; n     root[pu] = pv; n   } n   if (pu != pv || !ty) return; n   ans[ind] = (get_lca(u, v)).second - 1; n } n void merge() { n   for (int i = 0; i < n; i++) root[i] = i; n   vector<int> temp(m); n   iota(temp.begin(), temp.end(), 0); n   sort(temp.begin(), temp.end(), cmp); n   for (int i = 0; i < m; i++) merge(temp[i], 0); n   fill_par(); n   for (int i = 0; i < m; i++) n     if (!mst[i]) { n       int u = e[i].u, v = e[i].v; n       int lca = get_lca(u, v).first; n       add[u].insert(e[i].w); n       add[v].insert(e[i].w); n       rem[lca].insert(e[i].w); n       rem[lca].insert(e[i].w); n     } n   fill_par(); n   for (int i = 0; i < n; i++) root[i] = i; n   for (int i = 0; i < m; i++) merge(temp[i], 1); n } n int main() { n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     int u, v, w; n     cin >> u >> v >> w; n     u--, v--; n     e[i] = edge(u, v, w); n     vec[u].push_back(i); n     vec[v].push_back(i); n   } n   merge(); n   for (int i = 0; i < m; i++) cout << ans[i] <<    n [i == m - 1]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   double a, b, m, vx, vy, vz; n   cin >> a >> b >> m >> vx >> vy >> vz; n   double X = a / 2 - m / vy * vx; n   double Z = -m / vy * vz; n   while (X < 0 || X > a) n     if (X > a) n       X = 2 * a - X; n     else n       X = -X; n   while (Z < 0 || Z > b) n     if (Z > b) n       Z = 2 * b - Z; n     else n       Z = -Z; n   printf( %.9lf %.9lf , X, Z); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char table[500 + 10][500 + 10]; n int vis[500 + 10][500 + 10]; n int n, m; n int dirr[4] = {1, -1, 0, 0}; n int dirc[4] = {0, 0, 1, -1}; n vector<tuple<int, int, int> > ans; n int dfs(int i, int j, int depth) { n   if (i < 0 || i >= n || j < 0 || j >= m) return 0; n   if (vis[i][j] || table[i][j] ==  # ) return 0; n   vis[i][j] = 1; n   ans.push_back(make_tuple(0, i + 1, j + 1)); n   for (int d = 0; d < 4; d++) { n     dfs(i + dirr[d], j + dirc[d], depth + 1); n   } n   if (depth > 0) { n     ans.push_back(make_tuple(1, i + 1, j + 1)); n     ans.push_back(make_tuple(2, i + 1, j + 1)); n   } n   return 0; n } n int main() { n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       cin >> table[i][j]; n     } n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (table[i][j] ==  # ) continue; n       if (vis[i][j] == 0) { n         dfs(i, j, 0); n       } n     } n   } n   printf( %d n , ans.size()); n   int a, b, c; n   for (tuple<int, int, int> t : ans) { n     tie(a, b, c) = t; n     if (a == 0) { n       printf( B %d %d n , b, c); n     } else if (a == 1) { n       printf( D %d %d n , b, c); n     } else { n       printf( R %d %d n , b, c); n     } n   } n   return 0; n } n