#include <bits/stdc++.h> n using namespace std; n const double PI = acos(-1); n const double eps = 1e-9; n const int inf = 2000000000; n const long long infLL = 9000000000000000000; n template <typename first, typename second> n ostream &operator<<(ostream &os, const pair<first, second> &p) { n   return os <<  (  << p.first <<  ,   << p.second <<  ) ; n } n template <typename T> n ostream &operator<<(ostream &os, const vector<T> &v) { n   os <<  { ; n   for (auto it = v.begin(); it != v.end(); ++it) { n     if (it != v.begin()) os <<  ,  ; n     os << *it; n   } n   return os <<  } ; n } n template <typename T> n ostream &operator<<(ostream &os, const set<T> &v) { n   os <<  [ ; n   for (auto it = v.begin(); it != v.end(); ++it) { n     if (it != v.begin()) os <<  ,  ; n     os << *it; n   } n   return os <<  ] ; n } n template <typename T> n ostream &operator<<(ostream &os, const multiset<T> &v) { n   os <<  [ ; n   for (auto it = v.begin(); it != v.end(); ++it) { n     if (it != v.begin()) os <<  ,  ; n     os << *it; n   } n   return os <<  ] ; n } n template <typename first, typename second> n ostream &operator<<(ostream &os, const map<first, second> &v) { n   os <<  [ ; n   for (auto it = v.begin(); it != v.end(); ++it) { n     if (it != v.begin()) os <<  ,  ; n     os << it->first <<   =   << it->second; n   } n   return os <<  ] ; n } n void faltu() { cerr <<   n ; } n template <typename T> n void faltu(T a[], int n) { n   for (int i = 0; i < n; ++i) cerr << a[i] <<    ; n   cerr <<   n ; n } n template <typename T, typename... hello> n void faltu(T arg, const hello &...rest) { n   cerr << arg <<    ; n   faltu(rest...); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   int n, cnt1 = 0, cnt2 = 0, x; n   cin >> n; n   while (n--) { n     cin >> x; n     if (x == 100) { n       cnt1++; n     } else { n       cnt2++; n     } n   } n   if (cnt1 % 2 == 1) { n     cout <<  NO  <<   n ; n   } else if (cnt1 == 0 && cnt2 % 2 == 1) { n     cout <<  NO  <<   n ; n   } else { n     cout <<  YES  <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int Maxn = 600005; n int minx[Maxn], tip[Maxn]; n int a[Maxn], b[Maxn], c[Maxn]; n int n, m, i, h, L, R, t, ans; n void ins(int p, int l, int r, int pos, int data) { n   if (l == r) { n     minx[p] = data; n     return; n   } n   int mid = (l + r) >> 1; n   if (pos <= mid) n     ins(p * 2 + 1, l, mid, pos, data); n   else n     ins(p * 2 + 2, mid + 1, r, pos, data); n   minx[p] = min(minx[p * 2 + 1], minx[p * 2 + 2]); n } n void push(int p) { n   minx[p * 2 + 1] += tip[p]; n   tip[p * 2 + 1] += tip[p]; n   minx[p * 2 + 2] += tip[p]; n   tip[p * 2 + 2] += tip[p]; n   tip[p] = 0; n } n void add(int p, int l, int r, int L, int R, int dlt) { n   if (L > r || l > R) return; n   if (L <= l && R >= r) { n     minx[p] += dlt; n     tip[p] += dlt; n     return; n   } n   if (tip[p]) push(p); n   int mid = (l + r) >> 1; n   add(p * 2 + 1, l, mid, L, R, dlt); n   add(p * 2 + 2, mid + 1, r, L, R, dlt); n   minx[p] = min(minx[p * 2 + 1], minx[p * 2 + 2]); n } n int main() { n   scanf( %d%d%d , &n, &m, &h); n   for (i = 1; i <= m; i++) scanf( %d , &b[i]); n   sort(b + 1, b + m + 1); n   for (i = 1; i <= m / 2; i++) swap(b[i], b[m - i + 1]); n   for (i = 1; i <= m; i++) b[i] = h - b[i]; n   for (i = 1; i <= n; i++) scanf( %d , &a[i]); n   for (i = 1; i <= m; i++) c[i] = a[i]; n   sort(c + 1, c + m + 1); n   for (i = 1; i <= m; i++) { n     t = lower_bound(c + 1, c + m + 1, b[i]) - c; n     ins(0, 1, m, i, i - t); n   } n   ans += (minx[0] >= 0); n   for (i = m + 1; i <= n; i++) { n     if (a[i] > a[i - m]) { n       L = upper_bound(b + 1, b + m + 1, a[i - m]) - b; n       R = upper_bound(b + 1, b + m + 1, a[i]) - b - 1; n       if (L <= R && L <= m) add(0, 1, m, L, R, 1); n     } else if (a[i] < a[i - m]) { n       L = upper_bound(b + 1, b + m + 1, a[i]) - b; n       R = upper_bound(b + 1, b + m + 1, a[i - m]) - b - 1; n       if (L <= R && L <= m) add(0, 1, m, L, R, -1); n     } n     ans += (minx[0] >= 0); n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int len(char* a) { n   int count; n   for (int i = 0; a[i] !=   0 ; ++i) { n     ++count; n   } n   return count; n } n int main() { n   char a[101], b[101], c[101]; n   int found = 0, i, j; n   fscanf(stdin,  %s%s%s , a, b, c); n   if (len(a) + len(b) != len(c)) { n     fprintf(stdout,  NO ); n     return 0; n   } n   for (i = 0; c[i] !=   0 ; ++i) { n     for (j = 0; a[j] !=   0 ; ++j) { n       if (c[i] == a[j]) { n         found = 1; n         a[j] = 1; n         break; n       } n     } n     if (!found) { n       for (j = 0; b[j] !=   0 ; ++j) { n         if (c[i] == b[j]) { n           found = 1; n           b[j] = 1; n           break; n         } n       } n     } n     if (!found) { n       fprintf(stdout,  NO ); n       return 0; n     } n     found = 0; n   } n   fprintf(stdout,  YES ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 1e6; n const int Linf = 1e9; n const long long LLinf = (long long)1e18; n const int maxn = 1e5 + 5; n int n, a[maxn]; n long long k, res = 0; n int sum[maxn], sum1[maxn], cnt; n map<int, int> mmap; n set<int> s; n void Enter() { n   cin >> n >> k; n   for (int i = (1); i <= (n); ++i) cin >> a[i], s.insert(a[i]); n   cnt = 0; n   for (int c : s) mmap[c] = ++cnt; n   for (int i = (1); i <= (n); ++i) a[i] = mmap[a[i]]; n } n void update(int key, int x, int delta) { n   for (; x <= cnt; x += x & -x) n     if (key) n       sum[x] += delta; n     else n       sum1[x] += delta; n } n int su(int key, int x) { n   int r = 0; n   for (; x > 0; x &= x - 1) n     if (key) n       r += sum[x]; n     else n       r += sum1[x]; n   return r; n } n void Init() { n   int j = 1; n   int i = 1; n   long long now = 0; n   for (int i = (1); i <= (n); ++i) { n     now += i - 1 - su(0, a[i]); n     update(0, a[i], 1); n   } n   while (i <= n) { n     update(1, a[i], 1); n     now += su(0, a[i] - 1) + (su(1, cnt) - su(1, a[i])); n     while ((now > k || j <= i) && j <= n) { n       now -= su(1, cnt) - su(1, a[j]) + su(0, a[j] - 1); n       update(0, a[j], -1); n       ++j; n     } n     ++i; n     res += n - j + 1; n   } n   cout << res; n } n void Solve() {} n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   Enter(); n   Init(); n   Solve(); n } n


#include <bits/stdc++.h> n using namespace std; n char getDiffColor(char a, char b) { n   map<char, bool> mp; n   mp[a] = true, mp[b] = true; n   if (mp.find( R ) == mp.end()) n     return  R ; n   else if (mp.find( G ) == mp.end()) n     return  G ; n   else n     return  B ; n } n int main() { n   int cnt = 0; n   string st; n   cin >> cnt >> st; n   cnt = 0; n   int i = 0; n   while (i + 2 < (st.size())) { n     if (st[i] == st[i + 1] && st[i] == st[i + 2]) n       st[i + 1] = getDiffColor(st[i], st[i]), cnt++; n     i++; n   } n   char pre = st[0]; n   for (int i = 0; i < st.size() - 1; i++) n     if (st[i] == st[i + 1]) n       st[i] = getDiffColor(pre, st[i]), pre = st[i], cnt++; n     else n       pre = st[i]; n   cout << cnt <<   n  << st; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using vi = vector<int>; n using pi = pair<int, int>; n template <class T> n bool ckmin(T& a, const T& b) { n   return b < a ? a = b, 1 : 0; n } n template <class T> n bool ckmax(T& a, const T& b) { n   return b > a ? a = b, 1 : 0; n } n void setIO(string name =   ) { n   cin.tie(0)->sync_with_stdio(0); n   if (int((name).size())) { n     freopen((name +  .in ).c_str(),  r , stdin); n     freopen((name +  .out ).c_str(),  w , stdout); n   } n } n const string yes =  YES n , no =  NO n ; n const int MOD = 1e9 + 7; n const int MX = 2e2 + 10; n const ll INF = 1e18; n const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; n const char dc[4] = { D ,  R ,  U ,  L }; n string S; n int K, A, B; n bool dp[MX][MX]; n vi ans; n int main() { n   setIO(); n   cin >> K >> A >> B >> S; n   dp[0][0] = true; n   for (int i = 1; i <= K; ++i) n     for (int k = 1; k < MX; ++k) n       for (int j = A; j <= B; ++j) n         if (k - j >= 0) dp[i][k] |= dp[i - 1][k - j]; n   if (dp[K][int((S).size())] == false) return cout <<  No solution , 0; n   int num = K, sub = int((S).size()); n   ans.push_back(sub); n   while (num && sub) { n     for (int i = A; i <= B; ++i) { n       if (sub - i >= 0 && dp[num - 1][sub - i]) { n         --num; n         ans.push_back(sub - i); n         sub -= i; n         break; n       } n     } n   } n   reverse(begin(ans), end(ans)); n   for (int i = 0; i < int((ans).size()) - 1; ++i) { n     for (int j = ans[i]; j < ans[i + 1]; ++j) { n       cout << S[j]; n     } n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100000 + 10; n const int maxm = 100000 + 10; n vector<int> adj[maxn]; n int fa[maxn][18], dep[maxn]; n void trans(int prev, int u) { n   dep[u] = dep[prev] + 1; n   fa[u][0] = prev; n   for (int i = 1; (1 << i) <= dep[u]; i++) { n     fa[u][i] = fa[fa[u][i - 1]][i - 1]; n   } n   int len = adj[u].size(); n   for (int i = 0; i < len; i++) { n     int v = adj[u][i]; n     if (v == prev) continue; n     trans(u, v); n   } n } n int lca(int u, int v) { n   if (dep[u] > dep[v]) swap(u, v); n   for (int i = 0; (1 << i) <= dep[v] - dep[u]; i++) { n     if ((dep[v] - dep[u]) & 1 << i) v = fa[v][i]; n   } n   if (u == v) return u; n   for (int i = 18; i >= 0; i--) { n     if ((1 << i) <= dep[u] && fa[u][i] != fa[v][i]) { n       u = fa[u][i]; n       v = fa[v][i]; n     } n   } n   return fa[u][0]; n } n int dis(int u, int v) { n   int fff = lca(u, v); n   return dep[u] + dep[v] - 2 * dep[fff]; n } n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   for (int i = 2; i <= n; i++) { n     int j; n     scanf( %d , &j); n     adj[i].push_back(j); n     adj[j].push_back(i); n   } n   dep[1] = 0; n   trans(1, 1); n   for (int i = 0; i < m; i++) { n     int q[3]; n     for (int i = 0; i < 3; i++) scanf( %d , &q[i]); n     int a[3], ttt = 0; n     for (int i = 0; i < 3; i++) { n       for (int j = i + 1; j < 3; j++) { n         a[ttt++] = lca(q[i], q[j]); n       } n     } n     int k = 0; n     for (int i = 0; i < 3; i++) n       if (dep[a[i]] > dep[a[k]]) k = i; n     k = a[k]; n     int ans = 0; n     for (int i = 0; i < 3; i++) ans = max(ans, dis(k, q[i])); n     printf( %d n , ans + 1); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int gd(const string &a, const string &b) { n   int d = 0; n   for (int i = 0; i < (int)a.size(); i++) n     if (a[i] != b[i]) d++; n   return d; n } n string a[1005]; n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   int mind = 1000; n   for (int i = 0; i < n; i++) n     for (int j = i + 1; j < n; j++) mind = min(mind, gd(a[i], a[j])); n   cout << min(6, (mind - 1) / 2) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool a[4111][4111]; n int n; n bool v[4111] = {0}; n int p[4111] = {0}; n int cnt = 0; n int ans[4111] = {0}; n bool dfs_find(int i, int lt) { n   v[i] = true; n   for (int j = 1; j <= n; j++) { n     if (a[i][j] > 0 && lt != j && i != j) { n       if (v[j]) { n         p[cnt++] = j; n         return true; n       } else { n         p[cnt++] = j; n         if (dfs_find(j, i)) { n           return true; n         } n         cnt--; n       } n     } n   } n   return false; n } n void dfs(int i) { n   for (int j = 1; j <= n; j++) { n     if (i != j && a[i][j] > 0 && ans[j] == -1) { n       ans[j] = ans[i] + 1; n       dfs(j); n     } n   } n } n int main() { n   memset(a, 0, sizeof(a)); n   memset(ans, -1, sizeof(ans)); n   cin >> n; n   for (int i = 1; i <= n; i++) { n     int x, y; n     cin >> x >> y; n     a[x][y] = a[y][x] = 1; n   } n   p[cnt++] = 1; n   dfs_find(1, 0); n   ans[p[cnt - 1]] = 0; n   for (int i = cnt - 2; i >= 0; i--) { n     if (p[i] == p[cnt - 1]) break; n     ans[p[i]] = 0; n   } n   for (int i = 1; i <= n; i++) { n     if (ans[i] == 0) { n       dfs(i); n     } n   } n   for (int i = 1; i <= n; i++) { n     cout << ans[i] <<    ; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_N = 501000; n const int INF = 0x3f3f3f3f; n vector<int> v[MAX_N], w[MAX_N]; n struct skt { n   int x, y; n }; n int dis[MAX_N][2]; n int tox[MAX_N][2], toy[MAX_N][2]; n int n; n int b[MAX_N]; n void bfs() { n   int i; n   queue<int> q; n   skt t; n   memset(dis, INF, sizeof(dis)); n   q.push(n); n   dis[n][0] = 0; n   dis[n][1] = 0; n   while (!q.empty()) { n     int x = q.front(); n     q.pop(); n     for (i = 0; i < v[x].size(); i++) { n       int y = v[x][i]; n       int k = w[x][i]; n       if (dis[y][k] > max(dis[x][0], dis[x][1]) + 1) { n         dis[y][k] = max(dis[x][0], dis[x][1]) + 1; n         q.push(y); n       } n     } n   } n } n int main(void) { n   int m, i, x, y, t; n   scanf( %d%d , &n, &m); n   for (i = 1; i <= m; i++) { n     scanf( %d%d%d , &x, &y, &t); n     if (x == y) continue; n     v[y].push_back(x); n     w[y].push_back(t); n   } n   bfs(); n   if (dis[1][0] == INF || dis[1][1] == INF) n     printf( -1 n ); n   else n     printf( %d n , max(dis[1][0], dis[1][1])); n   for (i = 1; i <= n; i++) { n     if (dis[i][0] > dis[i][1]) n       b[i] = 0; n     else n       b[i] = 1; n     printf( %d , b[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   int n; n   cin >> n; n   vector<int> a(n); n   for (int i = 0; i < n; ++i) cin >> a[i]; n   long long sum = 0; n   for (int i = 0; i < n; ++i) sum += a[i]; n   long long cntWhite = 0; n   for (int i = 0; i < n; ++i) cntWhite += (a[i] + i % 2) / 2; n   cout << min(cntWhite, sum - cntWhite) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s; n map<string, int> mp; n const int N = 1e5 + 5; n int a[N]; n int cost[N]; n int g[N]; n int main() { n   int n, k, m; n   cin >> n >> k >> m; n   for (int i = 1; i <= n; i++) { n     cin >> s; n     mp[s] = i; n   } n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n   } n   int x; n   int minn; n   while (k--) { n     scanf( %d , &x); n     minn = 0x3f3f3f3f; n     for (int i = 0; i < x; i++) { n       scanf( %d , &g[i]); n       minn = min(minn, a[g[i]]); n     } n     for (int i = 0; i < x; i++) { n       cost[g[i]] = minn; n     } n   } n   long long ans = 0; n   for (int i = 0; i < m; i++) { n     cin >> s; n     ans += cost[mp[s]]; n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long md = 1e9 + 7; n int n, k, ans, mx, edg[1010][1010], dis[1001]; n vector<int> v, gr[100000]; n bool vis[1001]; n void dfs(int x, int l) { n   vis[x] = true; n   dis[x] = 0; n   for (int i = 0; i < gr[x].size(); i++) { n     int u = gr[x][i]; n     if (vis[u] == false) { n       dfs(u, l + 1); n     } n     dis[x] = max(dis[x], dis[u] + 1); n     mx = max(mx, dis[x]); n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cin >> n >> k; n   int kk = k; n   while (k--) { n     for (int i = 1; i <= n; i++) { n       int x; n       cin >> x; n       edg[x][k] = i; n     } n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       if (i == j) continue; n       bool t = false; n       for (int z = 0; z < kk; z++) { n         if (edg[i][z] > edg[j][z]) t = true; n       } n       if (t == false) gr[i].push_back(j); n     } n   } n   for (int i = 1; i <= n; i++) { n     dfs(i, 1); n     memset(vis, 0, sizeof vis); n   } n   cout << mx + 1; n } n


#include <bits/stdc++.h> n using namespace std; n ifstream fin( abc.in ); n string str[2]; n long long let[26][500001]; n long long a, b, sum, m; 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 int main() { n   cin >> a >> b; n   cin >> str[0] >> str[1]; n   long long len_mx = str[1].size(), len_mn = str[0].size(), i, j, dif_len; n   int p = 1; n   if (str[0].size() > str[1].size()) n     p = 0, len_mx = str[0].size(), len_mn = str[1].size(); n   dif_len = str[p].size() - str[1 - p].size(); n   long long cmmmc = len_mx * len_mn / gcd(len_mx, len_mn); n   m = a / (cmmmc / str[0].size()); n   long long cmmdc = gcd(len_mn, len_mx); n   long long sx = 0; n   if (dif_len == 0) { n     for (i = 0; i < len_mx; i++) n       if (str[0][i] != str[1][i]) sx += m; n   } else { n     for (i = 0; i < len_mx; i++) { n       char c = str[p][i]; n       let[(long)(c -  a )][i % cmmdc]++; n     } n     for (i = 0; i < len_mn; i++) { n       char c = str[1 - p][i]; n       sx += m * (str[p].size() / cmmdc - let[c -  a ][i % cmmdc]); n     } n   } n   cout << sx; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long power(long long a, long long b) { n   long long prod = 1; n   while (b) { n     if (b & 1) prod = (prod * a); n     a = (a * a); n     b >>= 1; n   } n   return prod; n } n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     vector<int> odd, even; n     for (int i = 0; i < n; i++) { n       int x; n       cin >> x; n       if (x % 2 == 1) { n         odd.push_back(i + 1); n       } else n         even.push_back(i + 1); n     } n     if (even.size() > 0) n       cout << 1 << endl << even[0] << endl; n     else { n       if (odd.size() >= 2) { n         cout << 2 << endl << odd[0] <<     << odd[1] << endl; n       } else { n         cout << -1 << endl; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n const long long maxn = 60; n const long long mod = 1e9 + 7; n const long double PI = acos((long double)-1); n long long pw(long long a, long long b, long long md = mod) { n   long long res = 1; n   while (b) { n     if (b & 1) { n       res = (a * res) % md; n     } n     a = (a * a) % md; n     b >>= 1; n   } n   return (res); n } n long long n, t; n long long c[maxn][maxn]; n int32_t main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> t; n   long long ans = 0; n   if (t == 1) ans--; n   if (__builtin_popcountll(t) != 1) cout << 0, exit(0); n   ; n   c[0][0] = 1; n   for (long long i = 1; i < maxn; i++) { n     c[i][0] = 1; n     for (long long j = 1; j <= i; j++) c[i][j] = c[i - 1][j] + c[i - 1][j - 1]; n   } n   t = __builtin_ctzll(t) + 1; n   n += 1; n   long long cnt = 0; n   for (long long i = 50; i >= 0; i--) { n     if (cnt > t) break; n     if (n & (1LL << i)) ans += c[i][t - cnt], cnt++; n   } n   if (__builtin_popcountll(n) == t) ans++; n   cout << ans; n   return (0); n } n


#include <bits/stdc++.h> n using namespace std; n long long Set(long long N, long long pos) { return N = N | (1 << pos); } n long long reset(long long N, long long pos) { return N = N & ~(1 << pos); } n bool check(long long N, long long pos) { return (bool)(N & (1 << pos)); } n void CI(long long &_x) { scanf( %I64d , &_x); } n void CO(long long &_x) { cout << _x; } n template <typename T> n void getarray(T a[], long long n) { n   for (long long i = 0; i < n; i++) cin >> a[i]; n } n template <typename T> n void prLLIarray(T a[], long long n) { n   for (long long i = 0; i < n - 1; i++) cout << a[i] <<    ; n   cout << a[n - 1] << endl; n } n const double EPS = 1e-9; n const long long INF = 0x7f7f7f7f; n long long dr8[8] = {1, -1, 0, 0, 1, -1, -1, 1}; n long long dc8[8] = {0, 0, -1, 1, 1, 1, -1, -1}; n long long dr4[4] = {0, 0, 1, -1}; n long long dc4[4] = {-1, 1, 0, 0}; n long long kn8r[8] = {1, 2, 2, 1, -1, -2, -2, -1}; n long long kn8c[8] = {2, 1, -1, -2, -2, -1, 1, 2}; n char grid[155][155]; n int main() { n   vector<pair<int, int> > V; n   int n, m; n   cin >> n >> m; n   int last; n   for (int i = 0; i < n; i++) { n     int mini = 1 << 30, maxi = -1; n     for (int j = 0; j < m; j++) { n       cin >> grid[i][j]; n       if (grid[i][j] ==  W ) { n         last = i; n         mini = min(mini, j); n         maxi = max(maxi, j); n       } n     } n     V.push_back(make_pair(mini, maxi)); n   } n   int ans = 0, pos_x = 0, pos_y = 0; n   for (int i = 0; i <= last; i++) { n     int dir = i % 2; n     if (dir == 0) { n       int mm = pos_y; n       mm = max(mm, V[i].second); n       if (i < last) mm = max(mm, V[i + 1].second); n       ans += mm - pos_y + 1; n       pos_y = mm; n     } else { n       int mm = pos_y; n       mm = min(mm, V[i].first); n       if (i < last) mm = min(mm, V[i + 1].first); n       ans += (pos_y - mm) + 1; n       pos_y = mm; n     } n   } n   cout << max(ans - 1, 0) <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n const double PI = acos(-1.0); n long long f[555], n; n string g[] = { < ,  ^ ,  > ,  v }; n string s, t; n int main() { n   cin >> s >> t; n   cin >> n; n   if (n % 2 == 0) { n     cout <<  undefined ; n     return 0; n   } n   for (int i = 0; i < 4; i++) n     if (s == g[i]) { n       if (g[(i + (n % 4)) % 4] == t) n         cout <<  cw ; n       else n         cout <<  ccw ; n     } n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   int v, id, tag; n } a[100010]; n int cmp1(node x, node y) { return x.v < y.v; } n int cmp2(node x, node y) { return x.id < y.id; } n int ccount[100010]; n int main() { n   int n, tt; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i].v), a[i].id = i; n   sort(a + 1, a + n + 1, cmp1); n   a[1].tag = tt = 1; n   ccount[tt] = 1; n   for (int i = 2; i <= n; i++) { n     if (a[i].v != a[i - 1].v) tt++; n     a[i].tag = tt; n     ccount[tt]++; n   } n   sort(a + 1, a + n + 1, cmp2); n   int nowh = 1, maxh = 0, block = 0; n   for (int i = 1; i <= n; i++) { n     ccount[a[i].tag]--; n     maxh = max(maxh, a[i].tag); n     while (!ccount[nowh]) nowh++; n     if (nowh >= maxh) block++; n   } n   printf( %d n , block); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 501; n int n, k; n int C[N]; n bool Dp[N][N][N]; n vector<int> Ans; n int main() { n   scanf( %d%d , &n, &k); n   for (int i = 0; i < n; i++) scanf( %d , C + i); n   Dp[n][0][0] = true; n   for (int idx = n - 1; idx >= 0; idx--) n     for (int Sum = 0; Sum <= k; Sum++) n       for (int X = 0; X <= k; X++) { n         Dp[idx][Sum][X] |= Dp[idx + 1][Sum][X]; n         Dp[idx][Sum][X] |= (Sum >= C[idx] && Dp[idx + 1][Sum - C[idx]][X]); n         Dp[idx][Sum][X] |= (Sum >= C[idx] && X >= C[idx] && n                             Dp[idx + 1][Sum - C[idx]][X - C[idx]]); n       } n   for (int X = 0; X <= k; X++) n     if (Dp[0][k][X]) Ans.push_back(X); n   printf( %d n , Ans.size()); n   for (auto x : Ans) printf( %d  , x); n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, x, f[5005][5005], g[5005], h[5005], cnt[5005]; n long long ans; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) { n     scanf( %d , &x); n     ++cnt[x]; n   } n   m = 5000; n   for (int i = 1; i <= m; ++i) { n     memcpy(f[i], f[i - 1], sizeof f[i]); n     g[i] = g[i - 1]; n     for (int j = 2, x = i; j <= x; ++j) n       while (x % j == 0) { n         x /= j; n         ++f[i][j]; n         ++g[i]; n       } n     for (int j = i; j; --j) n       if (f[i][j] == f[i - 1][j]) n         h[i] += f[i][j]; n       else { n         h[i] += f[i - 1][j]; n         break; n       } n   } n   ans = 0x3f3f3f3f3f3f3f3fLL; n   for (int i = 0; i <= m; ++i) { n     long long sum = 0; n     for (int j = i - 1, k = g[i]; ~j; --j) { n       k = min(k, h[j + 1]); n       sum += (long long)(g[i] + g[j] - k * 2) * cnt[j]; n     } n     for (int j = i + 1, k = g[i]; j <= m; ++j) { n       k = min(k, h[j]); n       sum += (long long)(g[i] + g[j] - k * 2) * cnt[j]; n     } n     ans = min(ans, sum); n     sum = 0; n     for (int j = i - 1, k = h[i]; ~j; --j) { n       k = min(k, h[j + 1]); n       sum += (long long)(h[i] + g[j] - k * 2) * cnt[j]; n     } n     for (int j = i, k = h[i]; j <= m; ++j) { n       k = min(k, h[j]); n       sum += (long long)(h[i] + g[j] - k * 2) * cnt[j]; n     } n     ans = min(ans, sum); n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 100; n int n, m, k, par[N], portal[N]; n long long dist[N]; n vector<pair<int, int> > gr[N]; n vector<pair<int, pair<int, int> > > edges; n vector<pair<long long, pair<int, int> > > new_edges; n inline int get(int x) { n   if (par[x] < 0) return x; n   return par[x] = get(par[x]); n } n inline bool unin(int x, int y) { n   x = get(x); n   y = get(y); n   if (x == y) return false; n   par[x] += par[y]; n   par[y] = x; n   return true; n } n int main() { n   scanf( %d %d , &n, &m); n   for (int i = 0; i < m; i++) { n     int a, b, c; n     scanf( %d %d %d , &a, &b, &c); n     gr[a].push_back({c, b}); n     gr[b].push_back({c, a}); n     edges.push_back({c, {a, b}}); n   } n   for (int i = 1; i <= n; i++) { n     dist[i] = 1e18; n     par[i] = -1; n   } n   set<pair<long long, int> > q; n   scanf( %d , &k); n   for (int i = 0; i < k; i++) { n     int p; n     scanf( %d , &p); n     dist[p] = 0; n     portal[p] = p; n     q.insert({0, p}); n   } n   while (!q.empty()) { n     auto pp = *q.begin(); n     q.erase(pp); n     int v = pp.second; n     for (auto pp : gr[v]) { n       int cost = pp.first, to = pp.second; n       if (dist[to] > dist[v] + cost) { n         q.erase({dist[to], to}); n         dist[to] = dist[v] + cost; n         portal[to] = portal[v]; n         q.insert({dist[to], to}); n       } n     } n   } n   for (int i = 1; i <= n; i++) gr[i].clear(); n   for (auto edge : edges) { n     int cost = edge.first, a = edge.second.first, b = edge.second.second; n     if (portal[a] == portal[b]) continue; n     new_edges.push_back({dist[a] + dist[b] + cost, {portal[a], portal[b]}}); n   } n   sort(new_edges.begin(), new_edges.end()); n   long long ans = dist[1]; n   for (auto edge : new_edges) { n     long long cost = edge.first; n     int a = edge.second.first, b = edge.second.second; n     if (unin(a, b)) { n       ans += cost; n     } n   } n   printf( %lld n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   int i, j; n   cin >> n >> k; n   int arr[n]; n   for (i = 0; i < n; i++) cin >> arr[i]; n   int one, two; n   int m = 0; n   for (i = 0; i < k; i++) { n     one = 0, two = 0; n     for (j = i; j < n; j = j + k) { n       if (arr[j] == 1) n         one++; n       else n         two++; n     } n     m = m + min(one, two); n   } n   cout << m << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long inf = numeric_limits<long long>::max(); n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int cases; n   cin >> cases; n   while (cases--) { n     int n; n     cin >> n; n     if (n % 2 == 0) n       cout << 1; n     else n       cout << 7; n     n -= 2; n     n /= 2; n     while (n--) cout << 1; n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 5010; n vector<int> ne[maxn]; n int cnt[maxn], n; n int dp[maxn], res[maxn]; n void dfs(int v, int prev = -1) { n   cnt[v] = 1; n   vector<int> w; n   int last = n - 1; n   for (int i = 0; i < (int)ne[v].size(); i++) { n     int u = ne[v][i]; n     if (u != prev) { n       dfs(u, v); n       cnt[v] += cnt[u]; n       w.push_back(cnt[u]); n       last -= cnt[u]; n     } n   } n   if (last > 0) { n     w.push_back(last); n   } n   fill(dp, dp + n, 0); n   dp[0] = 1; n   for (int i = 0, sum = 0; i < (int)w.size(); sum += w[i], i++) { n     for (int s = sum; s >= 0; s--) { n       if (dp[s]) { n         dp[s + w[i]] = 1; n       } n     } n   } n   for (int i = 1; i + 1 < n; i++) { n     res[i] |= dp[i]; n   } n } n int main(void) { n   scanf( %d , &n); n   for (int i = 1; i < n; i++) { n     int a, b; n     scanf( %d%d , &a, &b); n     ne[a].push_back(b); n     ne[b].push_back(a); n   } n   dfs(1); n   int res_n = 0; n   for (int i = 1; i + 1 < n; i++) { n     res_n += res[i]; n   } n   printf( %d n , res_n); n   for (int i = 1; i + 1 < n; i++) { n     if (res[i]) { n       printf( %d %d n , i, n - i - 1); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, x; n   cin >> a >> b >> x; n   string s =   ; n   if (a >= b) { n     s.push_back( 0 ); n     a--; n   } else { n     s.push_back( 1 ); n     b--; n   } n   while (x) { n     if (x == 1) { n       if (s.back() ==  1 ) { n         while (b) s.push_back( 1 ), b--; n         while (a) s.push_back( 0 ), a--; n       } else { n         while (a) s.push_back( 0 ), a--; n         while (b) s.push_back( 1 ), b--; n       } n     } else if (s.back() ==  1 ) { n       s.push_back( 0 ); n       a--; n     } else { n       s.push_back( 1 ); n       b--; n     } n     x--; n   } n   cout << s << endl; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> a[2222]; n int dis[2222]; n int main() { n   for (int i = 0; i < 2222; ++i) dis[i] = -1; n   dis[0] = 0; n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < n; ++i) n     for (int j = 0; j < m; ++j) { n       char c; n       cin >> c; n       if (c ==  # ) a[i].push_back(n + j), a[n + j].push_back(i); n     } n   queue<int> q; n   q.push(0); n   while (!q.empty()) { n     int p = q.front(); n     q.pop(); n     for (int i = 0; i < a[p].size(); ++i) n       if (dis[a[p][i]] == -1) { n         q.push(a[p][i]); n         dis[a[p][i]] = dis[p] + 1; n       } n   } n   cout << dis[n - 1] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> ans; n int b; n queue<pair<int, int> > qu; n void OUT() { n   if (ans.size() == 201) ans.pop_back(); n   printf( %d n , ans.size()); n   for (int i = 0; i < ans.size(); i++) printf( %d  , ans[i]); n } n int solve(int last, int i) { n   if (last == 0) return 0; n   qu.push(pair<int, int>(last, i)); n   return (last + b - 1) / b; n } n int main() { n   int n, a, k, cnt = 0, last2 = 0; n   string s; n   scanf( %d %d %d %d , &n, &a, &b, &k); n   cin >> s; n   s +=  1 ; n   int last = 0; n   string tmp = s; n   for (int i = 0; i < s.size(); i++) { n     if (s[i] ==  1 ) { n       for (last = max(last, i - b + 1); last <= i; last++) tmp[last] =  1 ; n     } n   } n   s = tmp; n   last = 0; n   for (int i = 0; i < s.size(); i++) { n     if (s[i] ==  1 ) n       cnt += solve(last, i), last = 0; n     else n       last++; n   } n   while (cnt >= a) { n     ans.push_back(qu.front().second); n     cnt--; n     if (qu.front().first > b) n       qu.front().second -= b, qu.front().first -= b; n     else n       qu.pop(); n   } n   OUT(); n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long r, x1, x2, y1, y2, ans; n   cin >> r >> x1 >> y1 >> x2 >> y2; n   double d1, d2; n   x1 -= x2; n   y1 -= y2; n   d1 = sqrt(1.0 * (x1 * x1 + y1 * y1)); n   d2 = d1; n   d2 /= (2 * r * 1.0); n   if (floor(d2) == ceil(d2)) { n     ans = d2; n   } else { n     ans = d2 + 1; n   } n   cout << ans; n } n int main() { solve(); } n


#include <bits/stdc++.h> n using namespace std; n template <typename _T> n inline void _DBG(const char *s, _T x) { n   cerr << s <<   =   << x <<   n ; n } n template <typename _T, typename... args> n void _DBG(const char *s, _T x, args... a) { n   while (*s !=  , ) cerr << *s++; n   cerr <<   =   << x <<  , ; n   _DBG(s + 1, a...); n } n const int M = 1e9 + 7; n long long P(long long a, int x) { n   long long res = 1; n   while (x--) res = res * a % M; n   return res; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long n, m; n   cin >> n >> m; n   cout << (P(2, m) * P(n + 1, m - 1)) % M * (n + 1 - m) % M <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n const int MaxN = 100000, MaxLog = 17; n struct graph_t { n   int cnte; n   int head[MaxN + 5], to[MaxN * 2 + 5], next[MaxN * 2 + 5]; n   inline void clear() { n     cnte = 0; n     memset(head, 0, sizeof head); n     memset(to, 0, sizeof to); n     memset(next, 0, sizeof next); n   } n   inline void addEdge(int u, int v) { n     cnte++; n     to[cnte] = v; n     next[cnte] = head[u]; n     head[u] = cnte; n   } n }; n int Te, N, Pos_A, Pos_B, L; n int Mx[MaxN + 5], Mmx[MaxN + 5], Mmmx[MaxN + 5], Mupx[MaxN + 5]; n int Fa[MaxLog + 1][MaxN + 5], Dep[MaxN + 5], Deepest[MaxN + 5]; n graph_t Gr; n void init() { n   scanf( %d %d %d , &N, &Pos_A, &Pos_B); n   for (int i = 1; i < N; ++i) { n     int u, v; n     scanf( %d %d , &u, &v); n     Gr.addEdge(u, v); n     Gr.addEdge(v, u); n   } n } n void dfs_getL(int u, int f, int d) { n   if (u == Pos_B) { n     L = d; n     return; n   } n   for (int i = Gr.head[u]; i; i = Gr.next[i]) { n     int v = Gr.to[i]; n     if (v == f) continue; n     dfs_getL(v, u, d + 1); n   } n } n void dfs1(int u, int f) { n   for (int i = Gr.head[u]; i; i = Gr.next[i]) { n     int v = Gr.to[i]; n     if (v == f) continue; n     dfs1(v, u); n     if (Mx[v] + 1 > Mx[u]) { n       Mmmx[u] = Mmx[u]; n       Mmx[u] = Mx[u]; n       Mx[u] = Mx[v] + 1; n     } else if (Mx[v] + 1 > Mmx[u]) { n       Mmmx[u] = Mmx[u]; n       Mmx[u] = Mx[v] + 1; n     } else if (Mx[v] + 1 > Mmmx[u]) { n       Mmmx[u] = Mx[v] + 1; n     } n   } n } n void dfs2(int u, int f) { n   for (int i = Gr.head[u]; i; i = Gr.next[i]) { n     int v = Gr.to[i]; n     if (v == f) continue; n     if (Mx[v] + 1 == Mx[u]) n       Mupx[v] = std::max(Mupx[u], Mmx[u]) + 1; n     else n       Mupx[v] = std::max(Mupx[u], Mx[u]) + 1; n     dfs2(v, u); n   } n } n void dfs3(int u) { n   Deepest[u] = u; n   for (int i = Gr.head[u]; i; i = Gr.next[i]) { n     int v = Gr.to[i]; n     if (v == Fa[0][u]) continue; n     Dep[v] = Dep[u] + 1; n     Fa[0][v] = u; n     for (int j = 1; (1 << j) <= Dep[v]; ++j) Fa[j][v] = Fa[j - 1][Fa[j - 1][v]]; n     dfs3(v); n     if (Dep[Deepest[v]] > Dep[Deepest[u]]) Deepest[u] = Deepest[v]; n   } n } n inline int kthAncestor(int u, int k) { n   for (int i = MaxLog; i >= 0; --i) n     if (k & (1 << i)) u = Fa[i][u]; n   return u; n } n inline bool isAncestor(int u, int v) { n   if (Dep[u] < Dep[v]) std::swap(u, v); n   return kthAncestor(u, Dep[u] - Dep[v]) == v; n } n inline int getLca(int u, int v) { n   if (Dep[u] < Dep[v]) std::swap(u, v); n   int d = Dep[u] - Dep[v]; n   for (int i = MaxLog; i >= 0; --i) n     if (d & (1 << i)) u = Fa[i][u]; n   if (u == v) return u; n   for (int i = MaxLog; i >= 0; --i) n     if (Fa[i][u] != Fa[i][v]) { n       u = Fa[i][u]; n       v = Fa[i][v]; n     } n   return Fa[0][u]; n } n void solve() { n   dfs_getL(Pos_A, 0, 0); n   dfs1(1, 0); n   dfs2(1, 0); n   int pivot = 0; n   for (int i = 1; i <= N; ++i) { n     int cnt = 0; n     if (Mx[i] >= L) cnt++; n     if (Mmx[i] >= L) cnt++; n     if (Mmmx[i] >= L) cnt++; n     if (Mupx[i] >= L) cnt++; n     if (cnt >= 3) { n       pivot = i; n     } n   } n   if (pivot == 0) { n     puts( NO ); n     return; n   } n   dfs3(pivot); n   int a = Pos_A, b = Pos_B, lca = getLca(a, b); n   int maxdepa = Dep[a], maxdepb = Dep[b]; n   for (;;) { n     if (isAncestor(a, b) == true) { n       puts( YES ); n       return; n     } n     if (Dep[Deepest[a]] > maxdepa) { n       int pace = Dep[Deepest[a]] - Dep[a]; n       a = Deepest[a]; n       if (pace >= Dep[b] - Dep[lca]) { n         puts( YES ); n         return; n       } else n         b = kthAncestor(b, pace); n       maxdepa = Dep[a]; n     } else if (Dep[Deepest[b]] > maxdepb) { n       int pace = Dep[Deepest[b]] - Dep[b]; n       b = Deepest[b]; n       if (pace >= Dep[a] - Dep[lca]) { n         puts( YES ); n         return; n       } else n         a = kthAncestor(a, pace); n       maxdepb = Dep[b]; n     } else { n       puts( NO ); n       return; n     } n   } n } n void clear() { n   N = Pos_A = Pos_B = L = 0; n   memset(Mx, 0, sizeof Mx); n   memset(Mmx, 0, sizeof Mmx); n   memset(Mmmx, 0, sizeof Mmmx); n   memset(Mupx, 0, sizeof Mupx); n   memset(Fa, 0, sizeof Fa); n   memset(Dep, 0, sizeof Dep); n   memset(Deepest, 0, sizeof Deepest); n   Gr.clear(); n } n int main() { n   scanf( %d , &Te); n   for (int t = 1; t <= Te; ++t) { n     init(); n     solve(); n     clear(); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n using namespace std; n map<string, long long int> month; n void Month() { n   month[ January ] = 1, month[ February ] = 2, month[ March ] = 3, n   month[ April ] = 4, month[ May ] = 5, month[ June ] = 6; n   month[ July ] = 7, month[ August ] = 8, month[ September ] = 9, n   month[ October ] = 10, month[ November ] = 11, month[ December ] = 12; n } n long long int A[1000000]; n long long int B[1000000]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   vector<long long int> v; n   long long int n, res, x = 0; n   cin >> n; n   res = n; n   while (res != 0) x++, res /= 10; n   x *= 9; n   if (x >= n) n     x = 1; n   else n     x = n - x; n   for (long long int i = x; i <= n; i++) { n     long long int val = i, s = 0; n     while (val != 0) s += (val % 10), val /= 10; n     if ((i + s) == n) v.push_back(i); n   } n   if (v.size()) { n     cout << v.size() << endl; n     for (long long int i = 0; i < v.size(); i++) cout << v[i] << endl; n   } else n     cout << 0 << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n using namespace std; n using ll = long long; n using vi = vector<int>; n using pii = pair<int, int>; n const int BSZ = 600; n const int N = 100228; n const int LG = 17; n int n, m; n vector<vi> g; n vi pred; n vi in, out; n int timer; n int up[LG][N]; n vi ts; n vi evs; n vi sgn; n vector<bool> alive; n vi cnt; n vi dif; n vector<bool> any; n int cur_add; n vi ans; n vi bkt[2 * N]; n vi srt[2 * BSZ]; n void dfs(int v) { n   in[v] = timer++; n   for (int to : g[v]) { n     dfs(to); n   } n   out[v] = timer++; n } n bool upper(int u, int v) { return (in[u] <= in[v] && out[v] <= out[u]); } n int lca(int u, int v) { n   if (in[u] > in[v]) { n     swap(u, v); n   } n   if (upper(u, v)) { n     return u; n   } n   for (int i = LG - 1; i >= 0; i--) { n     if (!upper(up[i][u], v)) { n       u = up[i][u]; n     } n   } n   return up[0][u]; n } n void dfs_calc(int v) { n   cnt[v] = !alive[v]; n   for (int to : g[v]) { n     dfs_calc(to); n     cnt[v] += cnt[to]; n     any[v] = any[v] | any[to]; n   } n   dif[v] = ts[v] - cnt[v]; n   if (!any[v] && alive[v]) { n     cur_add += (dif[v] < 0); n   } n } n int32_t main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   cin >> n >> m; n   pred.resize(n, -1); n   g.resize(n); n   for (int i = 0; i < n - 1; i++) { n     int p; n     cin >> p; n     --p; n     pred[i + 1] = p; n     g[p].push_back(i + 1); n     up[0][i + 1] = p; n   } n   ts.resize(n); n   for (auto& x : ts) { n     cin >> x; n   } n   for (int j = 1; j < LG; j++) { n     for (int i = 0; i < n; i++) { n       up[j][i] = up[j - 1][up[j - 1][i]]; n     } n   } n   in.resize(n); n   out.resize(n); n   dfs(0); n   evs.resize(m); n   sgn.resize(m); n   for (int i = 0; i < m; i++) { n     auto& x = evs[i]; n     cin >> x; n     sgn[i] = (x > 0 ? -1 : 1); n     x = abs(x) - 1; n   } n   alive.resize(n, true); n   cnt.resize(n); n   dif.resize(n); n   any.resize(n); n   ans.resize(m, 0); n   for (int lb = 0; lb < m; lb += BSZ) { n     int rb = min(m, lb + BSZ); n     vi guys(rb - lb); n     fill((any).begin(), (any).end(), false); n     for (int i = lb; i < rb; i++) { n       guys[i - lb] = evs[i]; n       any[guys[i - lb]] = true; n     } n     sort((guys).begin(), (guys).end(), n          [](int u, int v) { return (in[u] < in[v]); }); n     guys.reserve(2 * (int)((guys).size())); n     for (int i = (int)((guys).size()) - 2; i >= 0; i--) { n       guys.push_back(lca(guys[i], guys[i + 1])); n     } n     guys.push_back(0); n     sort((guys).begin(), (guys).end(), n          [](int u, int v) { return (in[u] < in[v]); }); n     guys.resize(unique((guys).begin(), (guys).end()) - guys.begin()); n     vi comp_pred((int)((guys).size()), -1); n     vi st; n     int edge_check = 0; n     for (int i = 0; i < (int)((guys).size()); i++) { n       while (!st.empty() && !upper(guys[st.back()], guys[i])) { n         st.pop_back(); n       } n       if (!st.empty()) { n         comp_pred[i] = st.back(); n         edge_check++; n       } n       st.push_back(i); n     } n     assert(edge_check == (int)((guys).size()) - 1); n     cur_add = 0; n     dfs_calc(0); n     for (int i = lb; i < rb; i++) { n       ans[i] = cur_add; n     } n     for (int i = 0; i < (int)((guys).size()); i++) { n       srt[i].clear(); n     } n     for (int i = 0; i < 2 * N; i++) { n       bkt[i].clear(); n     } n     for (int i = 0; i < (int)((guys).size()); i++) { n       int u = guys[i]; n       bool st = alive[u]; n       int udif = dif[u]; n       for (int j = lb; j < rb; j++) { n         if (upper(u, evs[j])) { n           udif += sgn[j]; n         } n         if (evs[j] == u) { n           st = !st; n         } n         if (st) { n           ans[j] += (udif < 0); n         } n       } n       if (i) { n         int anc = guys[comp_pred[i]]; n         int v = pred[u]; n         if (anc != v) { n           while (v != anc) { n             if (alive[v]) { n               bkt[dif[v] + N].push_back(i); n             } n             v = pred[v]; n           } n         } n       } n     } n     for (int i = 0; i < 2 * N; i++) { n       for (int who : bkt[i]) { n         srt[who].push_back(i - N); n       } n     } n     for (int i = 0; i < (int)((guys).size()); i++) { n       auto u = guys[i]; n       auto& difs = srt[i]; n       vector<pii> pref; n       pref.reserve((int)((difs).size())); n       for (auto x : difs) { n         if (pref.empty()) { n           pref.push_back({x, 1}); n         } else if (x != pref.back().first) { n           pref.push_back({x, pref.back().second + 1}); n         } else { n           pref.back().second++; n         } n       } n       int pt = (int)((pref).size()); n       while (pt > 0 && pref[pt - 1].first >= 0) { n         pt--; n       } n       int bound = 0; n       for (int j = lb; j < rb; j++) { n         if (upper(u, evs[j])) { n           bound -= sgn[j]; n           if (pt < (int)((pref).size()) && pref[pt].first < bound) { n             pt++; n           } n           if (pt > 0 && pref[pt - 1].first >= bound) { n             pt--; n           } n         } n         ans[j] += (pt ? pref[pt - 1].second : 0); n       } n     } n     for (int i = lb; i < rb; i++) { n       int u = evs[i]; n       alive[u] = !alive[u]; n     } n   } n   for (int i = 0; i < m; i++) { n     cout << ans[i] <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n void ex() { n   cout <<  NO n ; n   exit(0); n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n; n   cin >> n; n   vector<pair<bool, int> > q(2 * n); n   for (int i = 0; i < 2 * n; ++i) { n     char t; n     cin >> t; n     if (t ==  + ) { n       q[i] = {true, -1}; n     } else { n       int x; n       cin >> x; n       q[i] = {false, x}; n     } n   } n   reverse(q.begin(), q.end()); n   vector<int> w, e; n   for (int i = 0; i < 2 * n; ++i) { n     if (q[i].first) { n       if (!e.empty()) { n         w.push_back(e.back()); n         e.pop_back(); n       } else { n         ex(); n       } n     } else { n       if (!e.empty() and e.back() < q[i].second) { n         ex(); n       } n       e.push_back(q[i].second); n     } n   } n   if (!e.empty()) { n     ex(); n   } n   cout <<  YES n ; n   for (int i = n - 1; i > -1; --i) { n     cout << w[i] <<    ; n   } n   cout <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int num = 1001; n struct node { n   long long l, r; n   int t; n } a[1008611]; n long long ans[1008611], tm[1008611]; n int main() { n   long long n, m; n   cin >> n >> m; n   for (long long i = 0; i < m; i++) { n     cin >> a[i].t >> a[i].l >> a[i].r; n     if (a[i].t) { n       for (long long k = a[i].l; k < a[i].r; k++) { n         tm[k] = 1; n       } n     } n   } n   ans[1] = num; n   for (long long i = 2; i <= n; i++) { n     if (tm[i - 1] == 0) num--; n     ans[i] = num; n   } n   for (long long j = 0; j < m; j++) { n     int pos = 1; n     for (long long k = a[j].l; k < a[j].r; k++) { n       if (ans[k] > ans[k + 1]) { n         pos = 0; n         break; n       } n     } n     if (pos != a[j].t) { n       cout <<  NO  << endl; n       return 0; n     } n   } n   cout <<  YES  << endl; n   for (int i = 1; i <= n; i++) cout << ans[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long w1, h1, w2, h2, ans = 0; n int main() { n   cin >> w1 >> h1 >> w2 >> h2; n   ans += (h1 + h2) + 2; n   ans += w2 + 1; n   ans += (h1 + h2) + 1; n   ans += w1 - w2; n   ans += w1; n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int NMAX = 1e5; n int t; n int n, m, k; n vector<int> graph[NMAX + 5]; n int gr[NMAX + 5]; n bool active[NMAX + 5]; n bool in[NMAX + 5]; n bool exists(int i, int j) { n   vector<int>::iterator it = lower_bound(graph[i].begin(), graph[i].end(), j); n   if (it != graph[i].end() && *it == j) { n     return true; n   } n   return false; n } n int main() { n   scanf( %d , &t); n   while (t--) { n     scanf( %d %d %d , &n, &m, &k); n     for (int i = 1; i <= n; i++) { n       gr[i] = 0; n       graph[i].clear(); n       active[i] = 1; n       in[i] = 0; n     } n     for (int i = 1; i <= m; i++) { n       int u, v; n       scanf( %d %d , &u, &v); n       graph[u].push_back(v); n       graph[v].push_back(u); n       gr[u]++; n       gr[v]++; n     } n     if (1LL * k * (k - 1) > 2 * m) { n       printf( -1 n ); n       continue; n     } n     queue<int> q; n     for (int i = 1; i <= n; i++) { n       sort(graph[i].begin(), graph[i].end()); n       if (gr[i] < k) { n         q.push(i); n         in[i] = 1; n       } n     } n     while (!q.empty()) { n       int nod = q.front(); n       active[nod] = 0; n       q.pop(); n       for (auto it : graph[nod]) { n         gr[it]--; n         if (in[it] == 0 && gr[it] < k) { n           in[it] = 1; n           q.push(it); n         } n       } n     } n     vector<int> ans; n     for (int i = 1; i <= n; i++) { n       if (active[i] == 1) { n         ans.push_back(i); n       } n     } n     if (ans.empty() == false) { n       printf( 1 %d n , (int)ans.size()); n       for (auto it : ans) { n         printf( %d  , it); n       } n       printf(  n ); n       continue; n     } n     for (int i = 1; i <= n; i++) { n       gr[i] = graph[i].size(); n       active[i] = 1; n       in[i] = 0; n     } n     for (int i = 1; i <= n; i++) { n       if (gr[i] < k) { n         in[i] = 1; n         q.push(i); n       } n     } n     bool found = false; n     while (!q.empty()) { n       int nod = q.front(); n       q.pop(); n       active[nod] = 0; n       if (gr[nod] == k - 1) { n         vector<int> nodes = {nod}; n         for (auto it : graph[nod]) { n           if (active[it] == 1) { n             nodes.push_back(it); n           } n         } n         bool ok = true; n         for (int i = 0; i < (int)nodes.size() && ok; i++) { n           for (int j = i + 1; j < (int)nodes.size() && ok; j++) { n             ok &= exists(nodes[i], nodes[j]); n           } n         } n         if (ok == true) { n           found = true; n           printf( 2 n ); n           for (auto it : nodes) { n             printf( %d  , it); n           } n           printf(  n ); n           break; n         } n       } n       for (auto it : graph[nod]) { n         gr[it]--; n         if (in[it] == 0 && gr[it] < k) { n           in[it] = 1; n           q.push(it); n         } n       } n     } n     if (found == false) { n       printf( -1 n ); n       continue; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string t =  abacaba ; n int get(string a) { n   int cnt = 0; n   for (int i = 0; i < a.size(); i++) { n     if (a.substr(i, t.size()) == t) cnt++; n   } n   return cnt; n } n int main() { n   int T; n   scanf( %d , &T); n   while (T--) { n     int n; n     string a; n     cin >> n >> a; n     int cnt = 0; n     for (int i = 0; i < a.size(); i++) { n       if (a.substr(i, t.size()) == t) cnt++; n     } n     if (cnt > 1) n       puts( No ); n     else if (cnt == 1) { n       puts( Yes ); n       for (int i = 0; i < a.size(); i++) { n         if (a[i] !=  ? ) n           printf( %c , a[i]); n         else n           putchar( z ); n       } n       puts(  ); n     } else { n       bool f = false; n       for (int i = 0; i < n; i++) { n         int cnt = 0; n         for (int k = i, j = 0; j < t.size() && k < a.size(); k++, j++) { n           if (a[k] == t[j] || a[k] ==  ? ) n             cnt++; n           else n             break; n         } n         if (cnt == t.size()) { n           string ts = a; n           for (int k = i, j = 0; j < t.size(); k++, j++) ts[k] = t[j]; n           if (get(ts) > 1) n             continue; n           else { n             f = true; n             puts( Yes ); n             for (int i = 0; i < ts.size(); i++) n               if (ts[i] ==  ? ) ts[i] =  z ; n             cout << ts << endl; n           } n           break; n         } n       } n       if (!f) puts( No ); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   assert(1 <= n && n <= (int)(2e5)); n   string sex; n   cin >> sex; n   assert(sex.size() == n); n   for (int i = 0; i < n; ++i) { n     assert(sex[i] ==  B  || sex[i] ==  G ); n   } n   vector<int> a(n); n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n     assert(1 <= a[i] && a[i] <= (int)(1e7)); n   } n   vector<int> pre(n), nxt(n); n   priority_queue<pair<int, pair<int, int> >, n                  vector<pair<int, pair<int, int> > >, n                  greater<pair<int, pair<int, int> > > > n       pq; n   for (int i = 0; i < n; ++i) { n     pre[i] = i - 1; n     nxt[i] = i + 1; n     if (i > 0 && sex[i - 1] != sex[i]) { n       pq.push(make_pair(abs(a[i - 1] - a[i]), make_pair(i - 1, i))); n     } n   } n   vector<bool> mark(n, true); n   vector<pair<int, int> > result; n   while (!pq.empty()) { n     int i = pq.top().second.first; n     int j = pq.top().second.second; n     pq.pop(); n     if (mark[i] && mark[j]) { n       mark[i] = false; n       mark[j] = false; n       result.push_back(make_pair(i, j)); n       i = pre[i]; n       j = nxt[j]; n       if (i != -1) { n         nxt[i] = j; n       } n       if (j != n) { n         pre[j] = i; n       } n       if (i != -1 && j != n && sex[i] != sex[j] && mark[i] && mark[j]) { n         pq.push(make_pair(abs(a[i] - a[j]), make_pair(i, j))); n       } n     } n   } n   cout << result.size() << endl; n   for (int i = 0; i < result.size(); ++i) { n     cout << result[i].first + 1 <<     << result[i].second + 1 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, q, x, y; n int a[200005]; n long long lazy[200005 * 4], S[200005 * 4]; n void push(int node, int bas, int son) { n   S[node * 2] += lazy[node]; n   S[node * 2 + 1] += lazy[node]; n   lazy[node * 2] += lazy[node]; n   lazy[node * 2 + 1] += lazy[node]; n   lazy[node] = 0; n } n int get(int node, int bas, int son) { n   if (bas == son) return (S[node] == 0 ? bas : -1); n   if (S[node] < 0) return -1; n   push(node, bas, son); n   return max(get(node * 2, bas, ((bas + son) / 2)), n              get(node * 2 + 1, ((bas + son) / 2) + 1, son)); n } n void up(int node, int bas, int son, int x, int y, long long val) { n   if (bas > y || son < x) return; n   if (bas >= x && son <= y) { n     S[node] += val; n     lazy[node] += val; n     return; n   } n   push(node, bas, son); n   up(node * 2, bas, ((bas + son) / 2), x, y, val); n   up(node * 2 + 1, ((bas + son) / 2) + 1, son, x, y, val); n   S[node] = max(S[node * 2], S[node * 2 + 1]); n } n int main() { n   long long sum = 0; n   scanf( %d %d , &n, &q); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     up(1, 1, n, i, i, a[i] - sum); n     sum += a[i]; n   } n   while (q--) { n     scanf( %d %d , &x, &y); n     up(1, 1, n, x, x, y - a[x]); n     up(1, 1, n, x + 1, n, a[x] - y); n     a[x] = y; n     printf( %d n , get(1, 1, n)); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int x, y, z; n   cin >> x >> y >> z; n   int x1, y1, z1; n   cin >> x1 >> y1 >> z1; n   int a[6]; n   for (auto &z : a) cin >> z; n   int sum = 0; n   if (x < 0) n     sum += a[4]; n   else if (x > x1) n     sum += a[5]; n   if (y < 0) n     sum += a[0]; n   else if (y > y1) n     sum += a[1]; n   if (z < 0) n     sum += a[2]; n   else if (z > z1) n     sum += a[3]; n   cout << sum <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1005; n const int M = 1000005; n const long long INF = (long long)(1e15); n long long a[N][N]; n long long retc[M], retr[M]; n priority_queue<pair<long long, int> > sc, sr; n int main() { n   int n, m, k, p; n   scanf( %d%d%d%d , &n, &m, &k, &p); n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       scanf( %I64d , &a[i][j]); n     } n   } n   for (int i = 1; i <= n; i++) { n     long long s = 0; n     for (int j = 1; j <= m; j++) { n       s += a[i][j]; n     } n     sr.push(make_pair(s, i)); n   } n   for (int j = 1; j <= m; j++) { n     long long s = 0; n     for (int i = 1; i <= n; i++) { n       s += a[i][j]; n     } n     sc.push(make_pair(s, j)); n   } n   for (int i = 1; i <= k; i++) { n     long long s = sr.top().first; n     int id = sr.top().second; n     retr[i] = retr[i - 1] + s; n     s -= p * m; n     sr.pop(); n     sr.push(make_pair(s, id)); n   } n   for (int i = 1; i <= k; i++) { n     long long s = sc.top().first; n     int id = sc.top().second; n     retc[i] = retc[i - 1] + s; n     s -= p * n; n     sc.pop(); n     sc.push(make_pair(s, id)); n   } n   long long ans = -INF; n   for (int i = 0; i <= k; i++) { n     ans = max(ans, retr[i] + retc[k - i] - (long long)i * (k - i) * p); n   } n   printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long double PI = 3.14159265359; n long long inf = 1000000000000000007; n long long mod = 1000000007; n long long mod1 = 998244353; n const bool multi = 0; n long long DP[500007]; n long long s[500007]; n vector<long long> G[500007]; n long long ans = inf, n; n void dfs(long long v, long long o) { n   s[v] = 1; n   vector<pair<pair<long long, long long>, long long>> DPs; n   DP[v] = inf; n   if ((long long)(G[v]).size() == 1) DP[v] = 0; n   long long sum = 0; n   for (auto u : G[v]) { n     if (u == o) continue; n     dfs(u, v); n     s[v] += s[u]; n     DPs.push_back({{-2 * s[u], 2 * DP[u] + s[u] * (s[u] + 1)}, u}); n   } n   sort(DPs.begin(), DPs.end()); n   vector<pair<long long, long long>> CHT; n   for (auto x : DPs) { n     long long l = 0, r = (long long)(CHT).size() - 1, sr; n     long long S = n - s[x.second]; n     ans = n         min(ans, 2 * DP[x.second] + (n - s[x.second]) * (n - s[x.second] - 1)); n     while (l <= r) { n       sr = (l + r) / 2; n       long long L = n           2 * DP[x.second] + S * (S - 1) + S * CHT[sr].first + CHT[sr].second; n       ans = min(ans, L); n       if (sr + 1 == (long long)(CHT).size()) break; n       long long R = 2 * DP[x.second] + S * (S - 1) + S * CHT[sr + 1].first + n                     CHT[sr + 1].second; n       ans = min(ans, R); n       if (L <= R) n         r = sr - 1; n       else n         l = sr + 1; n     } n     if ((long long)(CHT).size() == 0 || x.first.first != CHT.back().first) n       CHT.push_back({x.first.first, x.first.second}); n     DP[v] = min(DP[v], DP[x.second] + n                            (s[v] - s[x.second]) * (s[v] - s[x.second] - 1) / 2); n   } n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long tt; n   if (multi) n     cin >> tt; n   else n     tt = 1; n   while (tt--) { n     long long a, b; n     cin >> n; n     for (long long i = 1; i < n; i++) { n       cin >> a >> b; n       G[a].push_back(b); n       G[b].push_back(a); n     } n     dfs(1, -1); n     ans /= 2; n     cout << ans + 2 * (n * (n - 1) / 2 - ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count() * n                ((uint64_t) new char | 1)); n int K, N; n vector<int> A, B, Z; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   cin >> K; n   N = 1 << K; n   Z.resize(N); n   int all_xor = 0; n   for (auto &z : Z) { n     cin >> z; n     all_xor ^= z; n   } n   if (all_xor != 0) { n     cout <<  Fou  <<   n ; n     return 0; n   } n   A.resize(N); n   B.resize(N); n   vector<int> location(N); n   for (int i = 0; i < N; i++) A[i] = B[i] = i; n   shuffle(A.begin(), A.end(), rng); n   shuffle(B.begin(), B.end(), rng); n   for (int i = 0; i < N; i++) location[B[i]] = i; n   for (int i = 0; i < N; i++) { n     int u = i; n     while (Z[u] != (A[u] ^ B[u])) { n       int v = location[A[u] ^ Z[u]]; n       swap(B[u], B[v]); n       swap(location[B[u]], location[B[v]]); n       if (v > i) { n         break; n       } else { n         swap(A[v], A[i + 1]); n         u = v; n       } n     } n   } n   cout <<  Shi  <<   n ; n   for (int i = 0; i < N; i++) cout << A[i] << (i < N - 1 ?     :   n ); n   for (int i = 0; i < N; i++) cout << B[i] << (i < N - 1 ?     :   n ); n } n


#include<bits/stdc++.h> n #define PI acos(-1.0)  n #define eps 1e-8 n #define fi first n #define se second n #define m_p make_pair n //#define int long long n #define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0) n using namespace std; n typedef long long ll; n typedef unsigned int uint;  n const double E_num = 0.57721566490153286060651209; n const int maxn = 2e3 + 10; n const int N = 5e5 + 10; n const int INF = 0x3f3f3f3f; n const int mod = 1e9 + 7; n  n  n int n,m,w; n int mp[maxn][maxn]; n ll dis1[maxn][maxn],dis2[maxn][maxn],ans1 = 1e18,ans2 = 1e18; n queue<pair<int,int>> q; n inline void bfs1(int x,int y) n { n  #define dis dis1 n  memset(dis,INF,sizeof(dis)); n  q.push(m_p(x,y)); n  dis[x][y] = 0; n  while(!q.empty()) n  { n   x = q.front().fi,y = q.front().se; n   q.pop(); n   if(dis[x - 1][y] > dis[x][y] + w && mp[x - 1][y] >= 0) n   { n    q.push(m_p(x - 1,y)); n    dis[x - 1][y] = dis[x][y] + w; n   } n   if(dis[x + 1][y] > dis[x][y] + w && mp[x + 1][y] >= 0) n   { n    q.push(m_p(x + 1,y)); n    dis[x + 1][y] = dis[x][y] + w; n   } n   if(dis[x][y - 1] > dis[x][y] + w && mp[x][y - 1] >= 0) n   { n    q.push(m_p(x,y - 1)); n    dis[x][y - 1] = dis[x][y] + w; n   } n   if(dis[x][y + 1] > dis[x][y] + w && mp[x][y + 1] >= 0) n   { n    q.push(m_p(x,y + 1)); n    dis[x][y + 1] = dis[x][y] + w; n   } n  } n  #undef dis n } n inline void bfs2(int x,int y) n { n  #define dis dis2 n  memset(dis,INF,sizeof(dis)); n  q.push(m_p(x,y)); n  dis[x][y] = 0; n  while(!q.empty()) n  { n   x = q.front().fi,y = q.front().se; n   q.pop(); n   if(dis[x - 1][y] > dis[x][y] + w && mp[x - 1][y] >= 0) n   { n    q.push(m_p(x - 1,y)); n    dis[x - 1][y] = dis[x][y] + w; n   } n   if(dis[x + 1][y] > dis[x][y] + w && mp[x + 1][y] >= 0) n   { n    q.push(m_p(x + 1,y)); n    dis[x + 1][y] = dis[x][y] + w; n   } n   if(dis[x][y - 1] > dis[x][y] + w && mp[x][y - 1] >= 0) n   { n    q.push(m_p(x,y - 1)); n    dis[x][y - 1] = dis[x][y] + w; n   } n   if(dis[x][y + 1] > dis[x][y] + w && mp[x][y + 1] >= 0) n   { n    q.push(m_p(x,y + 1)); n    dis[x][y + 1] = dis[x][y] + w; n   } n  } n  #undef dis n } n int main() n { n  IOS; n  cin >> n >> m >> w; n  for(int i = 0;i <= m + 1; ++i) n  { n   mp[0][i] = mp[n + 1][i] = -1; n  } n  for(int i = 1;i <= n + 1; ++i) n  { n   mp[i][0] = mp[i][m + 1] = -1; n  } n  for(int i = 1;i <= n; ++i) n  { n   for(int j = 1;j <= m; ++j)  n   { n    cin >> mp[i][j]; n   } n  } n  bfs1(1,1); n  bfs2(n,m); n  for(int i = 1;i <= n; ++i) n  { n   for(int j = 1;j <= m; ++j) n   { n    if(mp[i][j] > 0) ans1 = min(ans1,dis1[i][j] + mp[i][j]); n   } n  } n  for(int i = 1;i <= n; ++i) n  { n   for(int j = 1;j <= m; ++j) n   { n    if(mp[i][j] > 0) ans2 = min(ans2,dis2[i][j] + mp[i][j]); n   } n  } n  ans1 = min(dis1[n][m] ,ans1 + ans2); n  if(ans1 < 1e18) cout << ans1 << endl; n  else cout << -1 << endl; n  return 0; n } n


#include<bits/stdc++.h> n using namespace std; n #define lc cout<<  n  n #define f1(a) for(int i=0;i<a;++i) n int main(){ n  ios_base::sync_with_stdio(false); n  cin.tie(NULL); n  cout.tie(NULL); n  int t;cin>>t; n  while(t--){ n   int n;cin>>n; n   int a[n],b[n]; n   std::vector<int> pos_index,pos_dif; n   std::vector<int> neg_index,neg_dif; n   f1(n){cin>>a[i];} n   f1(n){cin>>b[i];} n   int test=0; n   f1(n){test+=a[i]-b[i]; n    if(a[i]-b[i]>0){pos_index.emplace_back(i);pos_dif.emplace_back(a[i]-b[i]);} n    else{ n     if(a[i]-b[i]<0){ n     neg_index.emplace_back(i);neg_dif.emplace_back(b[i]-a[i]); n     } n    }} n   if(test!=0){cout<<-1;lc;} n   else{ n    f1(n){test+=abs(a[i]-b[i]);} n    int res=test/2; n    cout<<res;lc; n    vector<int>fr,se; n    f1(pos_dif.size()){while(pos_dif[i]--){fr.emplace_back(pos_index[i]);}} n    f1(neg_dif.size()){while(neg_dif[i]--){se.emplace_back(neg_index[i]);}} n    f1(fr.size()){cout<<fr[i]+1<<   <<se[i]+1;lc;} n  n     n     n   } n    n  n  } n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline bool read(T &x) { n   int c = getchar(); n   int sgn = 1; n   while (~c && c< 0  | c>  9 ) { n     if (c ==  - ) sgn = -1; n     c = getchar(); n   } n   for (x = 0; ~c &&  0  <= c && c <=  9 ; c = getchar()) x = x * 10 + c -  0 ; n   x *= sgn; n   return ~c; n } n const long long N = 200005; n const long long MOD = 1e9 + 7; n long long cnt[N]; n long long ara[N]; n int main() { n   long long n = ({ n     long long a; n     read(a); n     a; n   }); n   long long mx = 0; n   long long val; n   for (int i = 1; i <= n; i++) { n     ara[i] = ({ n       long long a; n       read(a); n       a; n     }); n     ++cnt[ara[i]]; n     if (cnt[ara[i]] > mx) { n       mx = cnt[ara[i]]; n       val = ara[i]; n     } n   } n   if (mx == n) n     puts( 0 ); n   else { n     cout << n - mx << endl; n     for (int i = 1; i <= n; i++) { n       if (ara[i] == val) { n         for (long long j = i - 1; j >= 1; --j) { n           if (ara[j] != val) { n             if (ara[j] > val) { n               cout << 2 <<     << j <<     << j + 1 << endl; n             } else { n               cout << 1 <<     << j <<     << j + 1 << endl; n             } n             ara[j] = val; n           } else n             break; n         } n         for (long long j = i + 1; j <= n; ++j) { n           if (ara[j] != val) { n             if (ara[j] > val) { n               cout << 2 <<     << j <<     << j - 1 << endl; n             } else { n               cout << 1 <<     << j <<     << j - 1 << endl; n             } n             ara[j] = val; n           } else n             break; n         } n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int a[1000004]; n int main() { n   int n, x; n   long long sum; n   int cnt; n   while (scanf(  %d , &n) == 1) { n     sum = 0LL; n     cnt = 0; n     for (int i = 0; i < n; i++) { n       scanf(  %d , &a[i]); n       if (a[i] % 2 == 1) cnt++; n       sum += a[i]; n     } n     if (sum % 2LL == 0LL) { n       if (cnt % 2 == 0 && cnt > 0) n         cout <<  First n ; n       else n         cout <<  Second n ; n     } else n       cout <<  First n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n double a[220005]; n double b[220005]; n int n; n double check(double x) { n   double q1 = 0.0, q2 = 0.0, q3 = 0.0; n   for (int i = 1; i <= n; i++) b[i] = a[i] - x; n   for (int i = 1; i <= n; i++) q1 = max(b[i], b[i] + q1), q3 = max(q1, q3); n   for (int i = 1; i <= n; i++) b[i] = (-1.0) * b[i]; n   for (int i = 1; i <= n; i++) q2 = max(b[i], b[i] + q2), q3 = max(q3, q2); n   return q3; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %lf , &a[i]); n   double l, r; n   l = -30000.0, r = 30000.0; n   while (r - l > 1e-11) { n     double mid1 = (l + r) / 2.0; n     double mid2 = (mid1 + r) / 2.0; n     if (check(mid1) >= check(mid2)) n       l = mid1; n     else n       r = mid2; n   } n   printf( %.13lf n , check((l + r) / (2.0))); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, Q, l, r; n int a[5000], b[5000][5000], c[5000][5000]; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   for (int i = 1; i <= n; i++) b[i][i] = c[i][i] = a[i]; n   for (int L = 1; L < n; L++) n     for (int i = 1; i + L <= n; i++) { n       int j = i + L; n       b[i][j] = b[i + 1][j] ^ b[i][j - 1]; n       c[i][j] = max(b[i][j], max(c[i + 1][j], c[i][j - 1])); n     } n   scanf( %d , &Q); n   while (Q--) { n     scanf( %d%d , &l, &r); n     printf( %d n , c[l][r]); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxN = 2e5 + 5; n const long long INF = 1e18; n const long long MOD = 1e9 + 7; n int seg[2][8 * maxN], lzy[2][8 * maxN]; n int lt[maxN], rt[maxN], Q[2][2 * maxN], N; n vector<int> vec; n long long two_to_the_power_of(long long x) { n   if (x == 0) return 1; n   long long y = two_to_the_power_of(x / 2); n   y = y * y % MOD; n   if (x & 1) y = y * 2 % MOD; n   return y; n } n void apply(bool b, int id, int s, int e, int x) { n   seg[b][id] = 1ll * (e - s) * x % MOD; n   lzy[b][id] = x; n   return; n } n void shift(bool b, int id, int s, int e) { n   if (lzy[b][id] == -1) return; n   int md = (s + e) / 2; n   apply(b, 2 * id, s, md, lzy[b][id]); n   apply(b, 2 * id + 1, md, e, lzy[b][id]); n   lzy[b][id] = -1; n   return; n } n void update(bool b, int l, int r, int x, int id = 1, int s = 0, int e = N) { n   if (l <= s && e <= r) { n     apply(b, id, s, e, x); n     return; n   } n   if (l >= e || s >= r) return; n   shift(b, id, s, e); n   int md = (s + e) / 2; n   update(b, l, r, x, 2 * id, s, md); n   update(b, l, r, x, 2 * id + 1, md, e); n   seg[b][id] = (seg[b][2 * id] + seg[b][2 * id + 1]) % MOD; n   return; n } n int fetch(bool b, int l, int r, int id = 1, int s = 0, int e = N) { n   if (l <= s && e <= r) return seg[b][id]; n   if (l >= e || s >= r) return 0; n   shift(b, id, s, e); n   int md = (s + e) / 2; n   return (fetch(b, l, r, 2 * id, s, md) + fetch(b, l, r, 2 * id + 1, md, e)) % n          MOD; n } n int main() { n   time_t START = clock(); n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   memset(lzy, -1, sizeof lzy); n   int n, m, k; n   scanf( %d%d%d , &k, &n, &m); n   for (int i = 0; i < n + m; i++) { n     scanf( %d%d , lt + i, rt + i); n     vec.push_back(lt[i]); n     vec.push_back(rt[i]); n   } n   vec.push_back(0); n   vec.push_back(1); n   vec.push_back(k); n   sort(vec.begin(), vec.end()); n   vec.resize(unique(vec.begin(), vec.end()) - vec.begin()); n   N = vec.size(); n   for (int i = 0; i < n + m; i++) { n     lt[i] = lower_bound(vec.begin(), vec.end(), lt[i]) - vec.begin(); n     rt[i] = lower_bound(vec.begin(), vec.end(), rt[i]) - vec.begin(); n     Q[i < n][rt[i]] = max(Q[i < n][rt[i]], lt[i]); n   } n   update(0, 0, 1, 1); n   update(1, 0, 1, 1); n   for (int i = 1; i < N; i++) { n     update(0, 0, Q[1][i], 0); n     update(1, 0, Q[0][i], 0); n     if (i == N - 1) break; n     for (int b = 0; b < 2; b++) { n       int tmp = (1ll * fetch(b, 0, i) * n                  (two_to_the_power_of(vec[i + 1] - vec[i] - 1) - 1) % MOD) % n                 MOD; n       tmp = (tmp + 1ll * fetch(1 - b, 0, i) * n                        two_to_the_power_of(vec[i + 1] - vec[i] - 1) % MOD) % n             MOD; n       update(b, i, i + 1, tmp); n     } n   } n   printf( %d n , (seg[0][1] + seg[1][1]) % MOD); n   time_t FINISH = clock(); n   cerr <<  Execution time:   n        << (long double)(FINISH - START) / CLOCKS_PER_SEC * 1000.0 n        <<   milliseconds. n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long ar[500005 + 5]; n int main() { n   long long i, j, k, n; n   cin >> n; n   if (n <= 2) n     cout << 1 << endl; n   else if (n == 3) n     cout << 2 << endl; n   else { n     long long lo = 1LL, hi = n, ans = 1LL; n     while (lo <= hi) { n       long long md = (lo + hi) / 2LL; n       if (md * 3LL <= n) { n         ans = max(ans, md); n         lo = md + 1LL; n       } else n         hi = md - 1LL; n     } n     if (ans * 3 + 1 <= n) { n       cout << (ans * 2) + 1 << endl; n     } else n       cout << ans * 2 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n const ll inf = 1e18; n int n, m; n int edges[22][2]; n bool used[6][6]; n int arr[7]; n void init() {} n void input() { n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     cin >> edges[i][0] >> edges[i][1]; n     edges[i][0]--; n     edges[i][1]--; n   } n } n int check() { n   int now = 0; n   for (int i = 1; i <= 6; i++) { n     for (int j = 1; j <= 6; j++) { n       used[i][j] = false; n     } n   } n   for (int i = 0; i < m; i++) { n     int v = edges[i][0], u = edges[i][1]; n     if (!used[arr[v]][arr[u]]) { n       now++; n       used[arr[v]][arr[u]] = true; n       used[arr[u]][arr[v]] = true; n     } n   } n   return now; n } n void solve() { n   int ans = 0; n   for (arr[0] = 1; arr[0] < 7; arr[0]++) { n     for (arr[1] = 1; arr[1] < 7; arr[1]++) { n       for (arr[2] = 1; arr[2] < 7; arr[2]++) { n         for (arr[3] = 1; arr[3] < 7; arr[3]++) { n           for (arr[4] = 1; arr[4] < 7; arr[4]++) { n             for (arr[5] = 1; arr[5] < 7; arr[5]++) { n               for (arr[6] = 1; arr[6] < 7; arr[6]++) { n                 ans = max(ans, check()); n               } n             } n           } n         } n       } n     } n   } n   cout << ans << endl; n } n void output() {} n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int number_of_testcases = 1; n   while (number_of_testcases--) { n     init(); n     input(); n     solve(); n     output(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int i, j, t, n; n   double x, y; n   cin >> t; n   while (t--) { n     cin >> n; n     switch (n) { n       case 1: n         j = 0; n         break; n       case 2: n         j = 1; n         break; n       case 3: n         j = 2; n         break; n       default: n         x = double(n); n         y = sqrt(x); n         i = int(y); n         if (n == (i * i)) { n           j = (i - 1) + (i - 1); n         } else { n           n = n - (i * i); n           if ((n % i) == 0) { n             j = (i - 1) + (n / i) + (i - 1); n           } else { n             j = i + (n / i) + (i - 1); n           } n         } n     } n     cout << j << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<int, int> > edge; n int flo[120][120], flo2[120][120], dp[120][120], menda[120][120], com; n int st[120], en[120]; n vector<int> vc[120]; n void sho(int n) { n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       if (i == j) continue; n       flo[i][j] = 1e7; n     } n   } n   for (int i = 0; i < edge.size(); i++) { n     flo[edge[i].first][edge[i].second] = 1; n   } n   for (int k = 1; k <= n; k++) { n     for (int i = 1; i <= n; i++) { n       for (int j = 1; j <= n; j++) { n         flo[i][j] = min(flo[i][k] + flo[k][j], flo[i][j]); n       } n     } n   } n } n void sho2(int n, int stop) { n   memset(flo2, 0, sizeof flo2); n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       if (i == j && i != stop) continue; n       flo2[i][j] = 1e7; n     } n   } n   for (int i = 0; i < edge.size(); i++) { n     if (edge[i].first == stop || edge[i].second == stop) continue; n     flo2[edge[i].first][edge[i].second] = 1; n   } n   for (int k = 1; k <= n; k++) { n     if (k == stop) continue; n     for (int i = 1; i <= n; i++) { n       if (i == stop) continue; n       for (int j = 1; j <= n; j++) { n         if (j == stop) continue; n         flo2[i][j] = min(flo2[i][k] + flo2[k][j], flo2[i][j]); n       } n     } n   } n } n void dp_func(int v, int bus, int x, int y) { n   int f = -1; n   for (int i = 0; i < vc[v].size(); i++) { n     int w = vc[v][i]; n     if (flo[v][y] == flo[w][y] + 1) f = max(f, dp[w][bus]); n   } n   if (f == -1) f = 1e7; n   for (int i = 1; i <= com; i++) { n     if (bus == i) continue; n     int c = st[i], d = en[i]; n     if (flo[c][d] == 1e7) continue; n     if (menda[i][v] == 0) continue; n     f = min(f, dp[v][i] + 1); n   } n   dp[v][bus] = min(f, dp[v][bus]); n } n int main() { n   int i, j, k, l, n, m, a, b; n   scanf( %d%d%d%d , &n, &m, &a, &b); n   for (int i = 1; i <= m; i++) { n     scanf( %d%d , &l, &k); n     edge.push_back(make_pair(l, k)); n     vc[l].push_back(k); n   } n   sho(n); n   if (flo[a][b] == 1e7) { n     cout <<  -1 ; n     return 0; n   } n   scanf( %d , &com); n   for (int i = 1; i <= com; i++) { n     scanf( %d%d , &st[i], &en[i]); n   } n   for (int i = 1; i <= n; i++) { n     sho2(n, i); n     for (int j = 1; j <= com; j++) { n       int x = st[j], y = en[j]; n       if (flo[x][y] < flo2[x][y]) menda[j][i] = 1; n     } n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= com; j++) dp[i][j] = 1e7; n   } n   for (int j = 1; j <= com; j++) { n     if (flo[st[j]][en[j]] == 1e7) continue; n     if (menda[j][b]) dp[b][j] = 0; n   } n   for (int ite = 1; ite <= n; ite++) { n     for (int i = 1; i <= n; i++) { n       for (int j = 1; j <= com; j++) { n         int x = st[j], y = en[j]; n         if (flo[x][y] == 1e7) continue; n         dp_func(i, j, x, y); n       } n     } n   } n   int ans = 1e7; n   for (int j = 1; j <= com; j++) { n     if (!menda[j][a]) continue; n     ans = min(ans, dp[a][j]); n   } n   if (ans == 1e7) n     ans = -1; n   else n     ans++; n   cout << ans << endl; n } n


#include <bits/stdc++.h> n const size_t Size = 1e5 + 5; n const size_t SqrtS = 775 + 5; n int f[Size]; n int sum[Size]; n int cost[Size]; n std::vector<int> d[Size]; n int n, k; n int main(void) { n   std::scanf( %d%d , &n, &k); n   for (int i = 1, x, y; i <= k; ++i) { n     std::scanf( %d%d , &x, &y); n     d[y].push_back(n - x + 1); n   } n   int sqrtK = SqrtS; n   std::memset(f, -0x3f, sizeof f); n   for (int i = 1, lim; i <= n; ++i) { n     std::memset(sum, 0, sizeof sum); n     lim = std::min(n + 1 - i, sqrtK); n     for (int j = d[i].size() - 1; j >= 0; --j) n       if (d[i][j] <= lim) sum[d[i][j]] = 1; n     for (int j = 1; j <= lim + 1; ++j) sum[j] += sum[j - 1]; n     cost[0] = std::max(0, f[0]); n     for (int j = 1; j <= lim; ++j) cost[j] = std::max(cost[j - 1], f[j]); n     f[0] = std::max(cost[0], std::max(f[1], cost[0] - 3) + 3 * sum[1]); n     for (int j = 1; j <= lim; ++j) n       f[j] = std::max(f[j + 1], cost[j] - (j + 2) * (j + 1) / 2 - 2) + n              3 * sum[j + 1]; n   } n   std::printf( %d n , k * 3 - f[0]); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n long long small = 1298173; n int n, m, a = 1, b = 2; n void step() { n   if (a == 1 and b == n - 1) { n     a++; n     b = a + 1; n   } else if (b < n) { n     b++; n   } else { n     a++; n     b = a + 1; n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(); n   cin >> n >> m; n   if (m == 1) { n     cout <<  2 2 n ; n     cout <<  1   << n <<   2 n ; n     return 0; n   } n   cout << 2 <<     << small <<   n ; n   cout << 1 <<     << n <<   2  <<   n ; n   small -= 2; n   int counter = n - 2; n   while (counter > 1) { n     cout << a <<     << b <<     << 10 <<   n ; n     small -= 10; n     step(); n     counter--; n   } n   cout << a <<     << b <<     << small <<   n ; n   step(); n   m -= (n - 1); n   while (m > 0) { n     cout << a <<     << b <<     << 1298183 <<   n ; n     step(); n     m--; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[100000 + 10]; n int b[100000 + 10]; n int main() { n   int n, x, m, y; n   scanf( %d%d%d%d , &n, &m, &x, &y); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n   } n   for (int i = 1; i <= m; i++) { n     scanf( %d , &b[i]); n   } n   printf( %s , a[x] < b[m - y + 1] ?  YES  :  NO ); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   unordered_map<int, int> nums; n   int n; n   cin >> n; n   while (n--) { n     int dummy; n     cin >> dummy; n     nums[dummy]++; n   } n   int res = 0; n   for (auto p : nums) res = max(res, p.second); n   cout << res << endl; n } n


#include <bits/stdc++.h> n using namespace std;                         n   n const int N=2e5+7; n  n int n; n int x=-1,y=-1,z=-1; n      n int a[N], t[N*4]; n vector<int> suff(N), pref(N);  n  n void build(int v=1, int tl=1, int tr=n){ n   if(tl==tr) t[v]=a[tl]; n   else { n     int tm = (tl+tr)/ 2; n     build(v*2, tl, tm); n     build(v*2+1,tm+1,tr); n     t[v]=min(t[v*2], t[v*2+1]); n   } n } n int get(int v, int tl, int tr, int l, int r){ n   if(l<=tl and tr<=r) return t[v]; n   if(r < tl || tr< l) return INT_MAX; n   int tm=(tl + tr)>>1; n   return min(get(v*2, tl, tm, l, r), get(v*2+1, tm+1,tr,l,r)); n } n void calc(){ n   pref[1]=a[1]; n   for(int i=2;i<=n;i++) pref[i]=max(pref[i-1], a[i]); n   suff[n]=a[n]; n   for(int i=n-1;i>=1;i--) suff[i]=max(suff[i+1], a[i]); n } n int main(){ n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int tt; n   cin>>tt; n   while(tt--){       n    cin>>n;                   n    for(int i = 1; i <= n; i++) cin>>a[i];  n    build(); n    calc(); n    bool flag=false; n    int x, y, z; n    for(int i=n;i>=3;i--){ n     int value = suff[i]; n     auto j1 = lower_bound(pref.begin(), pref.begin()+i-1, value);  n     int start = j1 - pref.begin(); n  n     auto j2 = upper_bound(pref.begin(), pref.begin()+i-1, value); n     int end = j2 - pref.begin(); n     //cout<<start<<     <<end<<   ; n     if(start >= end) continue; n      n     int low= start, high=end; n     while(high - low > 1) { n       int mid = (low + high) / 2; n       if (get(1,1,n,mid, i-1)<value) { n         low=mid; n       } else { n         high=mid; n       } n     } n     //cout<<     << high <<   n ; n     if (high < i and get(1,1,n,high,i-1) == value and pref[high-1] == value) { n      x = high-1; n      y = i-high; n      z = n-i+1; n       flag=true; n     } n    } n    if(!flag) n     cout<< NO n ; n    else n     cout<< YES n <<x<<     << y <<     << z <<  n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int x; n   cin >> x; n   if (x % 4 == 0) n     printf( 1 A ); n   else if (x % 4 == 1) n     printf( 0 A ); n   else if (x % 4 == 2) n     printf( 1 B ); n   else if (x % 4 == 3) n     printf( 2 A ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, x, y, a, b, flag = 0, check = 1; n   cin >> n >> a >> x >> b >> y; n   for (int i = a, j = b; check != 0; i++, j--) { n     if (i == n + 1) { n       i = 1; n     } n     if (j == 0) { n       j = n; n     } n     if (i == j) { n       flag = 1; n       break; n     } n     if (i == x || j == y) { n       check = 0; n     } n   } n   if (flag == 1) { n     cout <<  YES ; n   } else { n     cout <<  NO ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long n; n   cin >> n; n   cout << 9 * n <<     << 8 * n <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[1005][10]; n int main() { n   int n, s = 0; n   cin >> n; n   for (int i = 0; i < n; i++) { n     int c = 0; n     for (int j = 0; j < 3; j++) { n       cin >> a[i][j]; n       if (a[i][j]) c++; n     } n     if (c >= 2) s++; n   } n   cout << s; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5e5 + 5; n int n = 5e5, f[N], s[N]; n vector<int> v[N]; n int q, a[N], c[N]; n int main() { n   int i, j, t; n   long long ans; n   for (i = 1; i <= n; i = i + 1) { n     if (i == 1) f[i] = 1; n     for (j = i; j <= n; j = j + i) { n       v[j].push_back(i); n       if (j > i) f[j] -= f[i]; n     } n   } n   scanf( %d%d , &n, &q); n   for (i = 1; i <= n; i = i + 1) scanf( %d , a + i); n   ans = 0; n   while (q--) { n     scanf( %d , &i); n     t = v[a[i]].size(); n     if (c[i]) { n       c[i] = 0; n       i = a[i]; n       for (j = 0; j < t; j = j + 1) ans -= f[v[i][j]] * (--s[v[i][j]]); n     } else { n       c[i] = 1; n       i = a[i]; n       for (j = 0; j < t; j = j + 1) ans += f[v[i][j]] * (s[v[i][j]]++); n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Line { n   double kx; n   double b; n   double x1; n   double x2; n }; n double CONST = pow(10, 12); n long long n, k, ai; n double bi; n vector<double> pre, summ, resumm; n vector<double> data; n double intersec(Line f, Line s) { return (s.b - f.b) / (f.kx - s.kx); } n long long bs(double number, vector<Line> &v) { n   long long l = 0; n   long long r = v.size(); n   while (r - l > 1) { n     long long m = (l + r) / 2; n     if (v[m].x1 <= number) n       l = m; n     else n       r = m; n   } n   return l; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin >> n >> k; n   double s1 = 0, s2 = 0, s3 = 0; n   for (long long i = 0; i < n; i++) { n     cin >> ai; n     bi = ai; n     s1 += bi; n     s2 += s1 / bi; n     s3 += 1 / bi; n     data.push_back(bi); n     pre.push_back(s2); n     summ.push_back(s1); n     resumm.push_back(s3); n   } n   vector<double> answers; n   for (long long i = 0; i < n; i++) { n     answers.push_back(pre[i]); n   } n   for (long long p = 1; p < k; p++) { n     vector<Line> st; n     vector<double> na; n     for (long long i = 0; i < answers.size() - 1; i++) { n       Line ll; n       ll.kx = -summ[i + p - 1]; n       ll.b = summ[i + p - 1] * resumm[i + p - 1] + answers[i] - pre[i + p - 1]; n       while (st.size() > 0) { n         double t = intersec(ll, st.back()); n         if (t <= st.back().x1) { n           st.pop_back(); n         } else { n           st.back().x2 = t; n           break; n         } n       } n       ll.x2 = CONST; n       if (st.size() == 0) n         ll.x1 = 0; n       else n         ll.x1 = st.back().x2; n       st.push_back(ll); n       long long ind = bs(resumm[i + p], st); n       double an = pre[i + p] + st[ind].kx * resumm[i + p] + st[ind].b; n       na.push_back(an); n     } n     answers = na; n   } n   cout.precision(30); n   cout << answers.back() << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int x, y, z; n   cin >> x >> y >> z; n   if (x > 0) { n     cout <<  0 ; n     for (int i = 1; i <= x; i++) cout <<  0 ; n   } n   if (y > 0) { n     if (x == 0) cout <<  0 ; n     cout <<  1 ; n     y--; n   } else if (z > 0 && y == 0 && x == 0) { n     cout <<  1 ; n   } n   for (int i = 1; i <= z; i++) cout <<  1 ; n   int m = 0; n   while (y > 0) { n     cout << m; n     if (m == 0) n       m = 1; n     else n       m = 0; n     y--; n   } n   cout << endl; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int t; n   cin >> t; n   while (t > 0) { n     solve(); n     t -= 1; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 100; n const int oo = maxn; n int n; n int a[maxn], b[maxn], c[maxn], it[maxn * 8], itmin[maxn * 8]; n int findit(int u, int v, int l, int r, int i) { n   if (r < u || v < l) return oo; n   if (u <= l && r <= v) { n     return (itmin[i]); n   } n   int mid = (l + r) >> 1; n   return ( n       min(findit(u, v, l, mid, i * 2), findit(u, v, mid + 1, r, i * 2 + 1))); n } n void updateit(int u, int w, int l, int r, int i) { n   if (r < u || u < l) return; n   if (u == l && u == r) { n     it[i] += w; n     if (it[i] == 0) n       itmin[i] = oo; n     else n       itmin[i] = u; n     return; n   } n   int mid = (l + r) >> 1; n   updateit(u, w, l, mid, i * 2); n   updateit(u, w, mid + 1, r, i * 2 + 1); n   if (it[i * 2] == 0) { n     it[i] = it[i * 2 + 1]; n     itmin[i] = itmin[i * 2 + 1]; n     return; n   } n   if (it[i * 2 + 1] == 0) { n     it[i] = it[i * 2]; n     itmin[i] = itmin[i * 2]; n     return; n   } n   it[i] = it[i * 2] + it[i * 2 + 1]; n   itmin[i] = min(itmin[i * 2], itmin[i * 2 + 1]); n } n void nhap() { n   cin >> n; n   for (int i = 1; i <= n; i++) { n     updateit(i, 0, 1, n, 1); n   } n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     a[i] %= n; n   } n   for (int i = 1; i <= n; i++) { n     cin >> b[i]; n     b[i] %= n; n     updateit(b[i] + 1, 1, 1, n, 1); n   } n } n void xuli() { n   for (int i = 1; i <= n; i++) c[i] = -1; n   for (int i = 1; i <= n; i++) { n     int x = n - a[i]; n     int j = findit(x + 1, n, 1, n, 1); n     if (j != oo) { n       c[i] = (a[i] + j - 1) % n; n       updateit(j, -1, 1, n, 1); n       continue; n     } else { n       j = findit(1, n, 1, n, 1); n       c[i] = (a[i] + j - 1) % n; n       updateit(j, -1, 1, n, 1); n       continue; n     } n   } n   for (int i = 1; i <= n; i++) { n     cout << c[i] <<    ; n   } n } n int main() { n   nhap(); n   xuli(); n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int neg = 1, num = 0; n   char c = getchar(); n   for (; !isdigit(c); c = getchar()) n     if (c ==  - ) neg = -1; n   for (; isdigit(c); c = getchar()) num = (num << 1) + (num << 3) + c -  0 ; n   return neg * num; n } n struct node { n   int x, y; n } p[6000010]; n int n, m, cnt, a[2010], b[2010], c[2010], d[2010], suf[6000010]; n int ans = 2147483647; n bool cmp(node f, node g) { return f.x == g.x ? f.y < g.y : f.x < g.x; } n int main() { n   n = read(); n   m = read(); n   for (int i = 1; i <= n; i++) n     a[i] = read(), b[i] = read(), p[i].x = 0, p[i].y = 0; n   for (int i = 1; i <= m; i++) c[i] = read(), d[i] = read(); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) { n       p[++cnt].x = max(0, c[j] - a[i] + 1); n       p[cnt].y = max(0, d[j] - b[i] + 1); n     } n   sort(p + 1, p + cnt + 1, cmp); n   suf[cnt] = p[cnt].y; n   for (int i = cnt - 1; i >= 1; i--) suf[i] = max(suf[i + 1], p[i].y); n   int pos = 0; n   for (int i = 0; i <= 1e6; i++) { n     while (p[pos + 1].x <= i && pos < cnt) pos++; n     ans = min(ans, i + suf[pos + 1]); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Node { n   int x, y, t; n   Node(int x = -1, int y = -1, int t = 0) : x(x), y(y) {} n }; n Node a[1005]; n string G[1005]; n int row[1005], col[1005]; n int main() { n   int n, m; n   cin >> n >> m; n   for (int i = 1; i <= n; i++) { n     cin >> G[i]; n     G[i] =     + G[i]; n   } n   memset(row, 0, sizeof(row)); n   memset(col, 0, sizeof(col)); n   int cnt = 0; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       if (G[i][j] ==  * ) { n         row[i]++; n         col[i]++; n         int flag = 0; n         for (int k = 0; k < cnt; k++) { n           if (a[k].x == i || a[k].y == j) { n             flag = 1; n             if (a[k].x == i) n               a[k].t = -a[k].x; n             else if (a[k].y == j) n               a[k].t = a[k].y; n             break; n           } n         } n         if (!flag && cnt < 10) a[cnt++] = Node(i, j); n       } n     } n   } n   int cntr = 0, cntc = 0; n   for (int i = 1; i <= n; i++) { n     if (row[i] > 1) cntr++; n   } n   for (int i = 1; i <= m; i++) { n     if (col[i] > 1) cntc++; n   } n   if (cnt > 2 || cntr > 1 || cntc > 1) n     cout <<  NO  << endl; n   else { n     cout <<  YES  << endl; n     if (cnt == 0) n       cout << 1 <<     << 1 << endl; n     else if (cnt == 1) n       cout << a[0].x <<     << a[0].y << endl; n     else { n       if (a[0].t != 0 && a[1].t != 0) { n         int x, y; n         if (a[0].t > 0) n           x = -a[1].t, y = a[0].t; n         else n           x = -a[0].t, y = a[1].t; n         cout << x <<     << y << endl; n       } else if (a[0].t != 0) { n         if (a[0].t > 0) n           cout << a[1].x <<     << a[0].y << endl; n         else n           cout << a[0].x <<     << a[1].y << endl; n       } else if (a[1].t != 0) { n         if (a[1].t < 0) n           cout << a[1].x <<     << a[0].y << endl; n         else n           cout << a[0].x <<     << a[1].y << endl; n       } else { n         cout << a[0].x <<     << a[1].y << endl; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 17; n vector<int> adj[N]; n int n, k; n int cnt[N]; n bool mark[N]; n long long int ans = 0; n int findCentroid(int v, int n, int &ans, int par = -1) { n   int sum = 1; n   int mx = 0; n   for (int u : adj[v]) { n     if (u != par && !mark[u]) { n       int x = findCentroid(u, n, ans, v); n       sum += x; n       mx = max(mx, x); n     } n   } n   mx = max(mx, n - sum); n   if (mx <= n / 2) { n     ans = v; n   } n   return sum; n } n int getSubTreeSize(int v, int par = -1) { n   int res = 1; n   for (int u : adj[v]) { n     if (par != u && !mark[u]) { n       res += getSubTreeSize(u, v); n     } n   } n   return res; n } n void addToCnt(int v, int plus, int depth = 1, int par = -1) { n   if (depth > k) return; n   cnt[depth] += plus; n   for (int u : adj[v]) { n     if (u != par && !mark[u]) { n       addToCnt(u, plus, depth + 1, v); n     } n   } n } n void findMiddleCentroidPaths(int v, int depth = 1, int par = -1) { n   if (k - depth >= 0) ans += cnt[k - depth]; n   for (int u : adj[v]) { n     if (u != par && !mark[u]) { n       findMiddleCentroidPaths(u, depth + 1, v); n     } n   } n } n void findBeginCentroidPaths(int v, int depth = 0, int par = -1) { n   if (depth == k) { n     ans++; n     return; n   } n   for (int u : adj[v]) { n     if (u != par && !mark[u]) { n       findBeginCentroidPaths(u, depth + 1, v); n     } n   } n } n void centroidDecomposition(int root, int siz) { n   int centroid; n   findCentroid(root, siz, centroid); n   mark[centroid] = true; n   findBeginCentroidPaths(centroid); n   for (int v : adj[centroid]) { n     if (mark[v]) continue; n     findMiddleCentroidPaths(v); n     addToCnt(v, +1); n   } n   for (int v : adj[centroid]) { n     if (mark[v]) continue; n     addToCnt(v, -1); n   } n   for (int v : adj[centroid]) { n     if (!mark[v]) { n       centroidDecomposition(v, getSubTreeSize(v)); n     } n   } n   mark[centroid] = false; n } n int main() { n   cin >> n >> k; n   for (int i = 1; i < n; i++) { n     int u, v; n     cin >> u >> v; n     u--; n     v--; n     adj[v].push_back(u); n     adj[u].push_back(v); n   } n   centroidDecomposition(0, n); n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n char a[105][105]; n int main() { n   ios::sync_with_stdio(false); n   int n, m; n   cin >> n >> m; n   for (int i = 1; i <= m; i++) n     for (int j = 1; j <= n; j++) cin >> a[i][j]; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) cout << a[j][i] << a[j][i]; n     cout << endl; n     for (int j = 1; j <= m; j++) cout << a[j][i] << a[j][i]; n     cout << endl; n   } n   return 0; n } n