#include <bits/stdc++.h> n using namespace std; n int n, x; n void solve() { n   cin >> n >> x; n   int left = x - 1; n   int right = n - x; n   if (left >= right) n     cout << (x - 1 == 0 ? 1 : x - 1) << endl; n   else n     cout << (x + 1 > n ? n : x + 1) << endl; n } n int main() { n   ios_base ::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); n   ; n   int tt; n   tt = 1; n   for (int i = 1; i <= tt; ++i) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<int, int> > res; n int n; n int tx[1000000], add[1000000]; n void up(int i, int l, int r, int a, int b, int x) { n   if (b < l || a > r) return; n   if (a <= l && b >= r) { n     tx[i] += x; n     add[i] += x; n     return; n   } n   int m = (l + r) / 2; n   up(2 * i, l, m, a, b, x); n   up(2 * i + 1, m + 1, r, a, b, x); n   tx[i] = min(tx[2 * i], tx[2 * i + 1]) + add[i]; n } n int getmin(int i, int l, int r, int a, int b) { n   if (b < l || a > r) return 1000000000; n   if (a <= l && b >= r) return tx[i]; n   int m = (l + r) / 2; n   return min(getmin(2 * i, l, m, a, b), getmin(2 * i + 1, m + 1, r, a, b)) + n          add[i]; n } n void com(int l, int r) { n   if (l > r) return; n   if (l == r) { n     for (int i = 0; i < getmin(1, 1, n, l, l); i++) n       res.push_back(make_pair(l, r)); n     return; n   } n   int m = getmin(1, 1, n, l, r); n   if (m == 0) { n     int mid = (l + r) / 2; n     int left = l; n     int right = r; n     while (1) { n       mid = (left + right) / 2; n       if (getmin(1, 1, n, l, mid) == 0) { n         if (getmin(1, 1, n, mid, mid) == 0) { n           com(l, mid - 1); n           com(mid + 1, r); n           return; n         } else n           right = mid; n       } else n         left = mid + 1; n     } n   } else if (m > 0) { n     up(1, 1, n, l, r, -m); n     for (int i = 0; i < m; i++) res.push_back(make_pair(l, r)); n     com(l, r); n   } n } n int main() { n   int k; n   scanf( %d , &n); n   ; n   for (int i = 0; i < n; i++) { n     scanf( %d , &k); n     ; n     up(1, 1, n, i + 1, i + 1, k); n   } n   com(1, n); n   cout << res.size() << endl; n   for (int i = 0; i < res.size(); i++) n     printf( %d %d n , res[i].first, res[i].second); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, ans = 1, res = 1, x, mn = 0, mx = 1e9; n set<int> S; n set<int>::iterator it; n char s[10]; n int main() { n   scanf( %d , &n); n   S.insert(1e9), S.insert(0); n   for (int i = 1; i <= n; ++i) { n     scanf( %s%d , s, &x); n     if (s[1] ==  D ) n       S.insert(x), res += (x >= mn && x <= mx) ? 1 : 0; n     else { n       if (x < mn || x > mx) ans = 0; n       if (x != mn && x != mx) ans = 2 * ans % 1000000007; n       S.erase(x); n       res = 1; n       it = S.lower_bound(x); n       mx = *it; n       mn = *(--it); n     } n   } n   printf( %d n , 1ll * ans * res % 1000000007); n } n


#include <bits/stdc++.h> n using namespace std; n int n; n vector<int> g[100005]; n int dep[100005], f[20][100005], mrk[100005], ans[100005]; n map<pair<int, int>, int> mp; n void dfs(int x, int p, int d) { n   for (int i = 0; i < g[x].size(); i++) n     if (g[x][i] != p) dfs(g[x][i], f[0][g[x][i]] = x, dep[g[x][i]] = d + 1); n } n int dfs2(int x, int p) { n   int cnt = 0; n   for (int i = 0; i < g[x].size(); i++) n     if (g[x][i] != p) { n       int num = dfs2(g[x][i], x); n       ans[mp[make_pair(x, g[x][i])]] = num; n       cnt += num; n     } n   cnt += mrk[x]; n   return cnt; n } n int lca(int x, int y) { n   if (dep[x] < dep[y]) swap(x, y); n   int sub = dep[x] - dep[y]; n   for (int i = 0; i < 20; i++) n     if (sub & (1 << i)) x = f[i][x]; n   if (x == y) return x; n   for (int i = 19; i >= 0; i--) n     if (f[i][x] != f[i][y]) { n       x = f[i][x]; n       y = f[i][y]; n     } n   x = f[0][x]; n   y = f[0][y]; n   return x; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i < n; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     g[x].push_back(y); n     g[y].push_back(x); n     mp[make_pair(x, y)] = i; n     mp[make_pair(y, x)] = i; n   } n   dfs(1, f[0][1] = 0, dep[1] = 1); n   for (int i = 1; i < 20; i++) n     for (int j = 1; j <= n; j++) f[i][j] = f[i - 1][f[i - 1][j]]; n   int q; n   scanf( %d , &q); n   while (q--) { n     int x, y; n     scanf( %d%d , &x, &y); n     mrk[x]++; n     mrk[y]++; n     mrk[lca(x, y)] -= 2; n   } n   dfs2(1, 0); n   for (int i = 1; i < n; i++) printf( %d  , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = acos(-1); n const long long INF = 1e18; n const int MAXN = 1e5 + 1; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int b, g, n; n   cin >> b >> g >> n; n   cout << min(b, min(g, n)) + 1 - max(0, n - max(b, g)); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n using namespace std; n int n, l[100100], r[100100]; n int rs[100100], dp[2][100100]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> l[i]; n   for (int i = 1; i <= n; i++) cin >> r[i]; n   int val = n, cnt = n; n   for (int i = 1; i <= n; i++) { n     if (l[i] == r[i] && !r[i]) rs[i] = val, cnt--; n   } n   int cn = 0; n   while (cnt && cn <= 10000) { n     val--; n     for (int i = 1; i <= n; i++) dp[0][i] = dp[0][i - 1] + (rs[i] > 0); n     for (int i = n; i; i--) dp[1][i] = dp[1][i + 1] + (rs[i] > 0); n     for (int i = 1; i <= n; i++) { n       if (!rs[i] && dp[0][i] == l[i] && dp[1][i] == r[i]) rs[i] = val, cnt--; n     } n     cn++; n   } n   if (cn >= 10000) return cout <<  NO , 0; n   for (int i = 1; i <= n; i++) n     if (rs[i] <= 0) return cout <<  NO , 0; n   cout <<  YES n ; n   for (int i = 1; i <= n; i++) cout << rs[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double dist(int x, int y, int a, int b) { n   return sqrt((x - a) * (x - a) + (y - b) * (y - b)); n } n int main() { n   ios_base::sync_with_stdio(false); n   cout.tie(0); n   cout << fixed; n   cout.precision(10); n   int n, m; n   cin >> n; n   int c, r; n   vector<pair<pair<int, int>, int> > arr; n   for (int i = 0; i < n; i++) { n     cin >> c >> r; n     arr.emplace_back(pair<int, int>(c, r), i); n   } n   sort(arr.begin(), arr.end()); n   int centre[n]; n   for (int i = 0; i < n; i++) { n     centre[i] = arr[i].first.first; n   } n   int count[n]; n   for (int i = 0; i < n; i++) { n     count[i] = INT_MAX; n   } n   cin >> m; n   for (int i = 1; i <= m; i++) { n     int x, y; n     cin >> x >> y; n     if (x <= centre[0]) { n       if (dist(x, y, arr[0].first.first, 0) <= arr[0].first.second) { n         count[0] = min(count[0], i); n       } n     } else if (x >= arr[n - 1].first.first) { n       if (dist(x, y, arr[n - 1].first.first, 0) <= arr[n - 1].first.second) { n         count[n - 1] = min(count[n - 1], i); n       } n     } else { n       int pos = upper_bound(centre, centre + n, x) - centre; n       if (dist(x, y, arr[pos].first.first, 0) <= arr[pos].first.second) { n         count[pos] = min(count[pos], i); n       } n       if (dist(x, y, arr[pos - 1].first.first, 0) <= n           arr[pos - 1].first.second) { n         count[pos - 1] = min(count[pos - 1], i); n       } n     } n   } n   int sol[n]; n   int res = 0; n   for (int i = 0; i < n; i++) { n     sol[arr[i].second] = count[i]; n     if (count[i] != INT_MAX) { n       res++; n     } n   } n   cout << res << (  n ); n   for (int i = 0; i < n; i++) { n     if (sol[i] != INT_MAX) { n       cout << sol[i] <<    ; n     } else { n       cout << -1 <<    ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int p[2001]; n int a[2001]; n int find(int v) { n   if (p[v] != v) p[v] = find(p[v]); n   return p[v]; n } n void merge(int u, int v) { n   int pu = find(u); n   int pv = find(v); n   p[pu] = pv; n } n int main() { n   int n, m, k, i, l, r; n   cin >> n >> m >> k; n   for (i = 0; i < n; i++) p[i] = i; n   for (i = 0; i < n; i++) { n     l = i; n     r = i + k - 1; n     if (r >= n) break; n     while (l < r) { n       merge(l, r); n       l++; n       r--; n     } n   } n   for (i = 0; i < n; i++) { n     p[i] = find(i); n     a[p[i]]++; n   } n   long long re = 1; n   for (i = 0; i < n; i++) n     if (a[i] > 0) { n       re *= m; n       re %= 1000000007; n     } n   cout << re; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n long long int max(long long int a, long long int b) { n   if (a > b) n     return a; n   else n     return b; n } n long long int min(long long int a, long long int b) { n   if (a < b) n     return a; n   else n     return b; n } n const int dx[4] = {-1, 1, 0, 0}; n const int dy[4] = {0, 0, -1, 1}; n int XX[] = {-1, -1, -1, 0, 0, 1, 1, 1}; n int YY[] = {-1, 0, 1, -1, 1, -1, 0, 1}; n int main() { n   ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); n   long long int k, i, j, t, a, b, l, r, x, n; n   cin >> n >> k; n   long long int arr[n]; n   for (i = 0; i < n; i++) { n     cin >> arr[i]; n   } n   sort(arr, arr + n); n   long long int maxi = k, count = 0; n   for (i = 0; i < n; i++) { n     long long int z = ceil(arr[i] * 1.0 / 2); n     if (maxi >= z) { n       maxi = max(maxi, arr[i]); n       continue; n     } n     while (maxi < z) { n       maxi = 2 * maxi; n       count++; n     } n     maxi = max(maxi, arr[i]); n   } n   cout << count; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline void read(T &x) { n   char c; n   bool nega = 0; n   while ((!isdigit(c = getchar())) && (c !=  - )) n     ; n   if (c ==  - ) { n     nega = 1; n     c = getchar(); n   } n   x = c - 48; n   while (isdigit(c = getchar())) x = x * 10 + c - 48; n   if (nega) x = -x; n } n template <typename T> n inline void writep(T x) { n   if (x > 9) writep(x / 10); n   putchar(x % 10 + 48); n } n template <typename T> n inline void write(T x) { n   if (x < 0) { n     putchar( - ); n     x = -x; n   } n   writep(x); n } n template <typename T> n inline void writeln(T x) { n   write(x); n   putchar(  n ); n } n int n, h[300003], dp[300003]; n set<int> st[300003]; n vector<pair<int, int> > ve; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int i, j; n   cin >> n; n   for (i = 0; i < n; i++) cin >> h[i]; n   pair<int, int> t; n   ve.push_back({0, h[0]}); n   for (i = 1; i < n; i++) { n     t = ve.back(); n     while (h[i] <= t.second) { n       st[t.first].insert(i); n       ve.pop_back(); n       if (ve.empty()) break; n       t = ve.back(); n     } n     ve.push_back({i, h[i]}); n   } n   ve.clear(); n   ve.push_back({0, h[0]}); n   for (i = 1; i < n; i++) { n     t = ve.back(); n     while (h[i] >= t.second) { n       st[t.first].insert(i); n       ve.pop_back(); n       if (ve.empty()) break; n       t = ve.back(); n     } n     ve.push_back({i, h[i]}); n   } n   ve.clear(); n   ve.push_back({n - 1, h[n - 1]}); n   for (i = n - 2; i >= 0; i--) { n     t = ve.back(); n     while (h[i] >= t.second) { n       st[i].insert(t.first); n       ve.pop_back(); n       if (ve.empty()) break; n       t = ve.back(); n     } n     ve.push_back({i, h[i]}); n   } n   ve.clear(); n   ve.push_back({n - 1, h[n - 1]}); n   for (i = n - 2; i >= 0; i--) { n     t = ve.back(); n     while (h[i] <= t.second) { n       st[i].insert(t.first); n       ve.pop_back(); n       if (ve.empty()) break; n       t = ve.back(); n     } n     ve.push_back({i, h[i]}); n   } n   dp[n - 1] = 0; n   set<int>::iterator it; n   for (i = n - 2; i >= 0; i--) { n     dp[i] = 0x3f3f3f3f; n     for (it = st[i].begin(); it != st[i].end(); it++) { n       dp[i] = ((dp[i]) < (dp[*it] + 1) ? (dp[i]) : (dp[*it] + 1)); n     } n   } n   cout << dp[0] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, s = 0, i, x, vs = 0; n   vector<int> a; n   cin >> n; n   for (i = 0; i < n; i++) { n     cin >> x; n     a.push_back(x); n     vs += x; n   } n   sort(a.begin(), a.end(), std::greater<int>()); n   if (n == 1) n     cout << a[0] << endl; n   else if (n == 2) n     cout << 2 * vs << endl; n   else { n     while (a.size() > 1) { n       s += vs; n       s += a.back(); n       vs -= a.back(); n       a.pop_back(); n     } n     s += a[0]; n     cout << s << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n struct pp { n   int val; n   int same; n }; n vector<pp> with[100005]; n vector<int> v[100005]; n int color[100005]; n int currcolor[100005]; n bool ending = false; n void dfs(int parent, int curr) { n   if (ending == true) return; n   for (int i = 1; i <= with[curr][0].val; i++) { n     if (ending == true) return; n     if (with[curr][i].val == parent) continue; n     int vertex = with[curr][i].val; n     if ((currcolor[vertex] != -1) && n         ((currcolor[vertex] + currcolor[curr] + with[curr][i].same) % 2 == 0)) { n       cout <<  NO n ; n       ending = true; n       return; n     } n     if (currcolor[vertex] != -1) continue; n     if (with[curr][i].same == 1) n       currcolor[vertex] = currcolor[curr]; n     else n       currcolor[vertex] = 1 - currcolor[curr]; n     dfs(curr, vertex); n   } n } n int main() { n   int n, m; n   cin >> n >> m; n   pp nullpp; n   nullpp.val = 0, nullpp.same = 0; n   for (int i = 1; i <= m; i++) with[i].push_back(nullpp); n   for (int i = 1; i <= n; i++) cin >> color[i]; n   for (int i = 1; i <= m; i++) { n     int k, val; n     cin >> k; n     for (int j = 1; j <= k; j++) { n       cin >> val; n       v[val].push_back(i); n     } n   } n   for (int i = 1; i <= n; i++) { n     with[v[i][0]][0].val++; n     pp ed; n     ed.val = v[i][1]; n     ed.same = color[i]; n     with[v[i][0]].push_back(ed); n     with[v[i][1]][0].val++; n     ed.val = v[i][0]; n     with[v[i][1]].push_back(ed); n   } n   for (int i = 1; i <= m; i++) currcolor[i] = -1; n   for (int i = 1; i <= m; i++) n     if (currcolor[i] == -1) { n       currcolor[i] = 1; n       dfs(-1, i); n     } n   if (ending == false) cout <<  YES n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 5; n int m[N]; n int main(void) { n   int n, a, b, k; n   scanf( %d %d %d %d , &n, &a, &b, &k); n   int ans = 0; n   for (int i = 1; i <= n; i++) { n     scanf( %d , &m[i]); n     m[i] = m[i] % (a + b); n     if (m[i] == 0) { n       m[i] = a + b; n     } n   } n   sort(m + 1, m + 1 + n); n   for (int i = 1; i <= n; i++) { n     int need = m[i] / a; n     if (m[i] % a) need++; n     if (need > 1) { n       k -= need - 1; n     } n     if (k >= 0) { n       ans++; n     } else n       break; n   } n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int v[55]; n double dp[55][55][55]; n int main() { n   int n, p, sum = 0; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &v[i]); n     sum += v[i]; n   } n   scanf( %d , &p); n   if (sum <= p) { n     printf( %d n , n); n     return 0; n   } n   double ans = 0; n   for (int i = 1; i <= n; i++) { n     memset(dp, 0, sizeof dp); n     dp[0][0][0] = 1; n     swap(v[i], v[n]); n     for (int j = 1; j < n; j++) { n       for (int cnt = 0; cnt < j; cnt++) { n         for (int k = 0; k <= p; k++) { n           if (k + v[j] <= p) n             dp[j][cnt + 1][k + v[j]] += dp[j - 1][cnt][k] * (cnt + 1) / j; n           dp[j][cnt][k] += dp[j - 1][cnt][k] * (j - cnt) / j; n         } n       } n     } n     for (int cnt = 0; cnt < n; cnt++) { n       for (int k = 0; k + v[n] <= p; k++) { n         ans += dp[n - 1][cnt][k] / n; n       } n     } n     swap(v[i], v[n]); n   } n   printf( %.15lf n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> p; n bool first[5000] = {0}; n void gen() { n   p.push_back(0); n   for (int i = 2; i <= 500; i++) { n     if (!first[i]) { n       p.push_back(i); n       for (int j = 2 * i; j <= 500; j += i) { n         first[j] = 1; n       } n     } n   } n } n bool isprime(int x) { n   int sz = sqrt(x) + 1; n   for (int i = 2; i <= sz; i++) { n     if (x % i == 0) return 0; n   } n   return 1; n } n int main() { n   gen(); n   int n; n   cin >> n; n   if (n <= 500) { n     vector<int> ans; n     for (int i = 0; i < p.size(); i++) { n       if (p[i] > n) break; n       for (int j = 0; j <= i; j++) { n         for (int k = 0; k <= j; k++) { n           if (p[i] + p[j] + p[k] == n) { n             if (p[i]) ans.push_back(p[i]); n             if (j) ans.push_back(p[j]); n             if (k) ans.push_back(p[k]); n             cout << ans.size() <<   n ; n             for (auto x : ans) { n               cout << x <<    ; n             } n             return 0; n           } n         } n       } n     } n   } else { n     vector<int> ans; n     for (int i = n; i >= n - 500; i--) { n       if (isprime(i)) { n         ans.push_back(i); n         n -= i; n         break; n       } n     } n     for (int i = 0; i < p.size(); i++) { n       if (p[i] > n) break; n       for (int j = 0; j <= i; j++) { n         if (p[i] + p[j] == n) { n           if (p[i]) ans.push_back(p[i]); n           if (p[j]) ans.push_back(p[j]); n           cout << ans.size() <<   n ; n           for (auto x : ans) { n             cout << x <<    ; n           } n           return 0; n         } n       } n     } n   } n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int n, a, b; n char seats[200001]; n int main() { n   scanf( %d%d%d , &n, &a, &b); n   scanf( %s , seats); n   int ans = 0; n   char prev =  . ; n   for (int i = 0; i < n; ++i) { n     if (seats[i] ==  * ) { n       prev =  . ; n       continue; n     } n     if (prev ==  A  && b > 0) { n       prev =  B ; n       ++ans; n       --b; n     } else if (prev ==  B  && a > 0) { n       prev =  A ; n       ++ans; n       --a; n     } else if (prev ==  .  && a > b) { n       prev =  A ; n       ++ans; n       --a; n     } else if (prev ==  .  && b > 0) { n       prev =  B ; n       ++ans; n       --b; n     } else n       prev =  . ; n   } n   printf( %d n , ans); n   return 0; n } n


#include <iostream> n #include <vector> n #include <algorithm> n #include <map> n  n using namespace std; n  n const int64_t INF = 1e15; n  n struct Dinic { n  struct Edge { n   int v; n   int64_t f, c; n   Edge(int _v = 0, int64_t _f = 0, int64_t _c = 0): v(_v), f(_f), c(_c) {}; n  }; n  n  int n; n  vector<int> idx, dep, qu; n  vector<vector<int>> adj; n  vector<Edge> edges; n  n  Dinic(int _n): n(_n), idx(_n), dep(_n), adj(_n), edges(0) {} n  n  void addEdge(int u, int v, int64_t c) { n   adj[u].push_back((int) edges.size()); n   edges.emplace_back(v, 0, c); n   adj[v].push_back((int) edges.size()); n   edges.emplace_back(u, 0, 0); n  } n  n  bool levelGraph(int s, int t) { n   qu.clear(); n   fill(dep.begin(), dep.end(), n + 1); n   fill(idx.begin(), idx.end(), 0); n  n   qu.push_back(s); n   dep[s] = 0; n   for (int qi = 0; qi < (int) qu.size() && qu.back() != t; ++qi) { n    int u = qu[qi]; n    for (int i : adj[u]) { n     auto &e = edges[i]; n     if (e.f < e.c && dep[u] + 1 < dep[e.v]) { n      dep[e.v] = dep[u] + 1; n      qu.push_back(e.v); n     } n    } n   } n   return dep[t] <= n; n  } n  n  int64_t sendFlow(int u, int t, int64_t f) { n   if (u == t) return f; n   for (int &i = idx[u]; i < (int) adj[u].size(); ++i) { n    auto &e = edges[adj[u][i]]; n    if (e.f < e.c && dep[u] + 1 == dep[e.v]) { n     int64_t g = sendFlow(e.v, t, min(f, e.c - e.f)); n     if (g > 0) { n      edges[adj[u][i]].f += g; n      edges[adj[u][i] ^ 1].f -= g; n      return g;  n     } n    } n   } n   return 0; n  } n  n  int64_t maxFlow(int s, int t) { n   while (levelGraph(s, t)) { n    while (sendFlow(s, t, INF)); n   } n  n   int64_t flow = 0; n   for (int i : adj[s]) { n    flow += edges[i].f; n   } n   return flow; n  } n }; n  n int main() { n  ios_base::sync_with_stdio(0); n  cin.tie(0); n  n  int n; n  map<pair<int, int>, int> a; n  auto get = [&](int x, int y) { n   auto it = a.find(make_pair(x, y)); n   return (it == a.end() ? -1 : it->second); n  }; n  n  cin >> n; n  Dinic dinic(n * 2 + 2); n  int s = n * 2, t = n * 2 + 1; n  int64_t total = 0; n  for (int i = 0; i < n; ++i) { n   int x, y, w; n   cin >> x >> y >> w; n   dinic.addEdge(2 * i, 2 * i + 1, w); n   total += w; n  n   a[make_pair(x, y)] = i; n  } n  n  for (auto rit = a.begin(); rit != a.end(); ++rit) { n   int x = rit->first.first, y = rit->first.second, i = rit->second, j; n  n   if (x % 2 == 0 && y % 2 == 0) { n    for (int y0 = y - 1; y0 <= y + 1; y0 += 2) { n     if (~(j = get(x, y0))) dinic.addEdge(2 * i + 1, 2 * j, INF); n    } n    for (int x0 = x - 1; x0 <= x + 1; x0 += 2) { n     if (~(j = get(x0, y))) dinic.addEdge(2 * j + 1, 2 * i, INF); n    } n   } else if (x % 2 == 0) { n    for (int x0 = x - 1; x0 <= x + 1; x0 += 2) { n     if (~(j = get(x0, y))) dinic.addEdge(2 * i + 1, 2 * j, INF); n    } n   } else if (y % 2 == 0) { n    dinic.addEdge(s, 2 * i, INF); n   } else { n    dinic.addEdge(2 * i + 1, t, INF); n   } n  } n  n  cout << total - dinic.maxFlow(s, t) << endl; n }


#include <bits/stdc++.h> n using namespace std; n const int N = 3000020, M = 120; n int L, R, p, ans, tot, cnt, g[N], f[N], P[M]; n bool buc[N], vis[M]; n void dfs(int k, int res) { n   g[++cnt] = res; n   for (int i = k; i <= tot; i++) { n     if (1ll * P[i] * res <= 1ll * R) dfs(i, res * P[i]); n   } n } n signed main() { n   memset(f, 0x3f, sizeof(f)); n   cin >> L >> R >> p; n   for (int i = 2; i <= p; i++) { n     if (!vis[i]) P[++tot] = i; n     for (int j = 1; j <= tot && i * P[j] <= p; j++) { n       vis[i * P[j]] = 1; n       if (i % P[j] == 0) break; n     } n   } n   dfs(1, 1); n   sort(g + 1, g + cnt + 1); n   f[1] = 0; n   for (int i = 2; i <= p; i++) { n     int j = 1; n     for (int k = 1; k <= cnt; k++) { n       while (j <= cnt && 1ll * g[j] != 1ll * g[k] * i) j++; n       if (j == cnt + 1) break; n       f[j] = min(f[j], f[k] + 1); n       if (f[j] + i <= p && g[j] >= L && !buc[j]) buc[j] = 1, ans++; n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     long long int n; n     cin >> n; n     cout << n << endl; n   } n } n


#include <bits/stdc++.h> n const long long oo = 1e9; n const long long mod = 95542721; n const long long MAX = 1e9; n const long long ARR = 1e5; n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   long long n, m; n   set<long long> x, y; n   cin >> n >> m; n   for (long long i = (0); i < (long long)(m); ++i) { n     int a, b; n     cin >> a >> b; n     x.insert(a), y.insert(b); n     cout << (n - x.size()) * (n - y.size()) <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int a, b, x, y; n     cin >> a >> b >> x >> y; n     if (a == x || b == y) { n       cout << abs(x - a) + abs(b - y) <<   n ; n       goto v; n     } else n       cout << abs(a - x) + abs(b - y) + 2 <<   n ; n   v:; n   } n } n


#include <bits/stdc++.h> n using namespace std; n char s[92], sl[10][10], str[10]; n bool cmp(char* a, char* b) { n   for (int i = 0; i < 10; i++) n     if (a[i] != b[i]) return false; n   return true; n } n int main() { n   scanf( %s , &s); n   for (int i = 0; i < 10; i++) scanf( %s , sl[i]); n   for (int i = 0; i < 80; i += 10) { n     for (int k = 0; k < 10; k++) str[k] = s[i + k]; n     for (int i = 0; i < 10; i++) n       if (cmp(str, sl[i])) { n         printf( %d , i); n         break; n       } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool F = true; n long long sum = 0; n void DFS(int v, int par, vector<vector<int>> &g, vector<long long> &s, n          vector<long long> &a) { n   if (s[v] == -1) { n     if (g[v].size() == 0) return; n     long long m = 2e9; n     for (int u : g[v]) m = min(s[u], m); n     a[v] = m - s[par]; n     if (a[v] < 0) { n       F = false; n     } n     for (int u : g[v]) a[u] = s[u] - m; n   } n   sum += a[v]; n   for (int u : g[v]) DFS(u, v, g, s, a); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   int n; n   cin >> n; n   vector<vector<int>> g(n); n   vector<long long> s(n), a(n); n   for (int i = 1; i < n; ++i) { n     int p; n     cin >> p; n     g[p - 1].push_back(i); n   } n   for (int i = 0; i < n; ++i) { n     cin >> s[i]; n   } n   a[0] = s[0]; n   DFS(0, 0, g, s, a); n   if (!F) { n     cout << -1 <<   n ; n   } else { n     cout << sum <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n struct Data { n   int64_t value; n   int64_t supports; n   int64_t supportedBy; n }; n bool isTop(map<pair<int64_t, int64_t>, Data> const& cubes, n            pair<int64_t, int64_t> const& pos) { n   bool top = true; n   for (int64_t i = -1; i <= 1; ++i) { n     map<pair<int64_t, int64_t>, Data>::const_iterator f = n         cubes.find(pair<int64_t, int64_t>{pos.first + i, pos.second + 1}); n     if (f != cubes.end()) { n       if (f->second.supportedBy <= 1) { n         top = false; n         break; n       } n     } n   } n   return top; n } n int main(int, char**) { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int64_t n; n   cin >> n; n   map<pair<int64_t, int64_t>, Data> cubes; n   for (int64_t i = 0; i < n; ++i) { n     int64_t x, y; n     cin >> x >> y; n     cubes.insert(make_pair(pair<int64_t, int64_t>{x, y}, Data{i, 0, 0})); n   } n   for (auto& p : cubes) { n     Data& data = p.second; n     pair<int64_t, int64_t> const& pos = p.first; n     if (cubes.find(pair<int64_t, int64_t>{pos.first - 1, pos.second - 1}) != n         cubes.end()) n       data.supportedBy += 1; n     if (cubes.find(pair<int64_t, int64_t>{pos.first, pos.second - 1}) != n         cubes.end()) n       data.supportedBy += 1; n     if (cubes.find(pair<int64_t, int64_t>{pos.first + 1, pos.second - 1}) != n         cubes.end()) n       data.supportedBy += 1; n     if (cubes.find(pair<int64_t, int64_t>{pos.first - 1, pos.second + 1}) != n         cubes.end()) n       data.supports += 1; n     if (cubes.find(pair<int64_t, int64_t>{pos.first, pos.second + 1}) != n         cubes.end()) n       data.supports += 1; n     if (cubes.find(pair<int64_t, int64_t>{pos.first + 1, pos.second + 1}) != n         cubes.end()) n       data.supports += 1; n   } n   set<pair<int64_t, pair<int64_t, int64_t> > > tops; n   for (auto const& p : cubes) { n     int64_t value = p.second.value; n     pair<int64_t, int64_t> const& pos = p.first; n     if (isTop(cubes, pos)) n       tops.insert(pair<int64_t, pair<int64_t, int64_t> >{value, pos}); n   } n   int64_t result = 0; n   bool smallest = false; n   while (!tops.empty()) { n     set<pair<int64_t, pair<int64_t, int64_t> > >::iterator used; n     if (smallest) n       used = tops.begin(); n     else n       used = --tops.end(); n     smallest = !smallest; n     result *= n; n     result += used->first; n     result %= 1000000009l; n     pair<int64_t, int64_t> pos = used->second; n     cubes.erase(pos); n     tops.erase(used); n     set<pair<int64_t, pair<int64_t, int64_t> > > update; n     for (int64_t i = -1; i <= 1; ++i) { n       { n         pair<int64_t, int64_t> pm1{pos.first + i, pos.second - 1}; n         map<pair<int64_t, int64_t>, Data>::iterator fm1 = cubes.find(pm1); n         if (fm1 != cubes.end()) { n           fm1->second.supports -= 1; n           update.insert( n               pair<int64_t, pair<int64_t, int64_t> >{fm1->second.value, pm1}); n         } n       } n       { n         pair<int64_t, int64_t> pp1{pos.first + i, pos.second + 1}; n         map<pair<int64_t, int64_t>, Data>::iterator fp1 = cubes.find(pp1); n         if (fp1 != cubes.end()) { n           fp1->second.supportedBy -= 1; n           update.insert( n               pair<int64_t, pair<int64_t, int64_t> >{fp1->second.value, pp1}); n           if (fp1->second.supportedBy <= 1) { n             for (int64_t j = -1; j <= 1; ++j) { n               pair<int64_t, int64_t> sibP{pp1.first + j, pos.second}; n               map<pair<int64_t, int64_t>, Data>::iterator sibI = n                   cubes.find(sibP); n               if (sibI != cubes.end()) { n                 tops.erase(pair<int64_t, pair<int64_t, int64_t> >{ n                     sibI->second.value, sibP}); n               } n             } n           } n         } n       } n     } n     for (auto const& updatePos : update) { n       if (isTop(cubes, updatePos.second)) tops.insert(updatePos); n     } n   } n   cout << result << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline void remax(T& A, T B) { n   if (A < B) A = B; n } n template <class T> n inline void remin(T& A, T B) { n   if (A > B) A = B; n } n string ToString(long long num) { n   string ret; n   do { n     ret += ((num % 10) +  0 ); n     num /= 10; n   } while (num); n   reverse(ret.begin(), ret.end()); n   return ret; n } n long long ToNumber(string s) { n   long long r = 0, p = 1; n   for (int i = s.size() - 1; i >= 0; --i) r += (s[i] -  0 ) * p, p *= 10; n   return r; n } n long long Gcd(long long a, long long b) { n   while (a %= b ^= a ^= b ^= a) n     ; n   return b; n } n long long Power(long long base, long long power) { n   long long ret = 1; n   while (power) { n     if (power & 1) ret *= base; n     power >>= 1; n     base *= base; n   } n   return ret; n } n long long PowerMod(long long base, long long power, long long mod) { n   if (!power) return 1; n   if (power & 1) return (base * PowerMod(base, power - 1, mod)) % mod; n   return PowerMod((base * base) % mod, power >> 1, mod); n } n int Log(long long num, long long base) { n   int ret = 0; n   while (num) { n     ++ret; n     num /= base; n   } n   return ret; n } n int Count(long long mask) { n   int ret = 0; n   while (mask) { n     if (mask & 1) ++ret; n     mask >>= 1; n   } n   return ret; n } n inline void run() { n   int n, k, ans, sum; n   scanf( %d%d , &n, &k); n   ans = n, sum = (n << 1); n   while (true) { n     if (sum >= k) break; n     if (!n) break; n     --ans, ++sum, --n; n   } n   printf( %d n , ans); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr), cout.tie(nullptr); n   run(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 1005; n map<pair<int, int>, int> mapa; n int main() { n   int n, h, m; n   scanf( %d , &n); n   int c = 0; n   for (int i = 0; i < n; i++) { n     scanf( %d%d , &h, &m); n     int x = ++mapa[make_pair(h, m)]; n     if (x > c) c = x; n   } n   printf( %d n , c); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1001000; n int n; n void add(long long* a, int x, long long y) { n   for (; x <= n; x += x & -x) a[x] += y; n } n long long gsm(long long* a, int x) { n   long long ret = 0; n   for (; x; x &= x - 1) ret += a[x]; n   return ret; n } n long long sum0[N], sum1[N]; n void mdf(int x, int y) { n   add(sum0, x, y); n   add(sum1, x, (long long)(1 - x) * y); n } n void modify(int l, int r, int x) { n   mdf(l, x); n   if (r < n) mdf(r + 1, -x); n } n long long query(int x) { return gsm(sum0, x) * x + gsm(sum1, x); } n vector<int> vec[N]; n int q[N], l[N], r[N], a[N], m; n long long ans[N]; n void solve() { n   for (int i = 1; i <= m; ++i) vec[l[i]].push_back(i); n   q[0] = n + 1; n   for (int i = n, j = 0; i; --i) { n     for (; j && a[q[j]] < a[i]; --j) n       ; n     modify(i, q[j] - 1, 1); n     q[++j] = i; n     for (int t : vec[i]) ans[t] += query(r[t]); n   } n   for (int i = 1; i <= n; ++i) vec[i].clear(), sum0[i] = sum1[i] = 0; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m; n   for (int i = 1; i <= n; ++i) cin >> a[i]; n   for (int i = 1; i <= m; ++i) cin >> l[i]; n   for (int i = 1; i <= m; ++i) cin >> r[i]; n   solve(); n   for (int i = 1; i <= m; ++i) n     l[i] = n + 1 - l[i], r[i] = n + 1 - r[i], swap(l[i], r[i]); n   reverse(a + 1, a + n + 1); n   solve(); n   for (int i = 1; i <= m; ++i) n     ans[i] -= r[i] - l[i] + 1, cout << ans[i] <<    n [i == m]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int const MAXN = 5e5 + 10; n int n, a[MAXN], ans; 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++) { n     if (a[i] != a[i + 1]) { n       int pt = i + 1, tam = 1; n       while (a[pt] != a[pt + 1] && pt != n) pt++, tam++; n       if (a[i] == a[pt]) n         for (int j = i; j <= pt; j++) a[j] = a[i]; n       else { n         tam--; n         for (int j = i; j <= i + tam / 2; j++) a[j] = a[i]; n         for (int j = i + tam / 2 + 1; j <= pt; j++) a[j] = a[pt]; n       } n       ans = max(ans, tam / 2); n     } n   } n   printf( %d n , ans); n   for (int i = 1; i <= n; i++) printf( %d  , a[i]); n   printf(  n ); n } n


#include <bits/stdc++.h> n using namespace std; n int32_t main() { n   cin.sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   long long n, m; n   cin >> n >> m; n   vector<long long> v(n); n   for (long long i = 0; i < n; i++) cin >> v[i]; n   vector<long long> dp(n); n   dp[0] = v[0]; n   for (long long i = 1; i < dp.size(); i++) { n     dp[i] = v[i] + dp[i - 1]; n   } n   for (long long i = 0; i < n; i++) { n     vector<long long> temp; n     long long ans = 0; n     for (long long j = 0; j < i; j++) { n       temp.push_back(v[j]); n     } n     if (temp.size() > 0) { n       sort(temp.rbegin(), temp.rend()); n     } n     long long timereq = v[i]; n     long long timeleft = m; n     long long in = 0; n     if (i > 0) { n       timeleft = m - dp[i - 1]; n     } n     while (true) { n       if (timeleft >= timereq) break; n       timeleft = timeleft + temp[in]; n       in++; n       ans++; n     } n     cout << ans <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, i, j, k, use[200010], culoare[200010], v[200010], x, y, a; n vector<int> v2[200010]; n vector<pair<int, int> > G[200010]; n void DF(int nod) { n   use[nod] = 1; n   for (auto it : G[nod]) { n     if (!use[it.first]) { n       if (it.second == 1) { n         culoare[it.first] = culoare[nod]; n       } else { n         culoare[it.first] = 1 - culoare[nod]; n       } n       DF(it.first); n     } else { n       if (it.second == 1) { n         if (culoare[it.first] != culoare[nod]) { n           cout <<  NO ; n           exit(0); n         } n       } else { n         if (culoare[it.first] != 1 - culoare[nod]) { n           cout <<  NO ; n           exit(0); n         } n       } n     } n   } n } n int main() { n   cin >> n >> m; n   for (i = 1; i <= n; i++) cin >> v[i]; n   for (i = 1; i <= m; i++) { n     cin >> x; n     for (j = 1; j <= x; j++) { n       cin >> a; n       v2[a].push_back(i); n     } n   } n   for (i = 1; i <= n; i++) { n     G[v2[i][0]].push_back({v2[i][1], v[i]}); n     G[v2[i][1]].push_back({v2[i][0], v[i]}); n   } n   for (i = 1; i <= m; i++) { n     if (!use[i]) { n       culoare[i] = 1; n       DF(i); n     } n   } n   cout <<  YES ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Tans { n   long a; n   long b; n }; n struct mstr { n   long num; n   long val; n }; n bool operator<(mstr a, mstr b) { return a.val < b.val; } n bool operator>(mstr a, mstr b) { return a.val > b.val; } n bool operator==(mstr a, mstr b) { return a.val == b.val; } n int main() { n   long n, s; n   cin >> n >> s; n   vector<mstr> cards(n); n   vector<Tans> ress; n   priority_queue<mstr> q; n   ress.reserve(n); n   for (long i = 0; i < n; i++) { n     cin >> cards[i].val; n     cards[i].num = i + 1; n     if (cards[i].val) q.push(cards[i]); n   } n   bool f = true; n   vector<mstr> temp; n   while (q.size() != 0 && f) { n     temp.resize(0); n     temp.reserve(q.size()); n     mstr now = q.top(); n     q.pop(); n     if (q.size() < now.val) n       f = false; n     else { n       mstr tt; n       Tans ttt; n       ttt.a = now.num; n       while (now.val != 0) { n         tt = q.top(); n         q.pop(); n         now.val--; n         tt.val--; n         ttt.b = tt.num; n         temp.push_back(tt); n         ress.push_back(ttt); n       } n       for (long j = 0; j < temp.size(); j++) n         if (temp[j].val > 0) q.push(temp[j]); n     } n   } n   if (f == false) n     cout <<  No ; n   else { n     cout <<  Yes n  << ress.size() <<   n ; n     for (long i = 0; i < ress.size(); i++) n       cout << ress[i].a <<     << ress[i].b <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int n, i, sum = 0; n   cin >> n; n   long long int a[n]; n   for (i = 0; i < n; i++) { n     cin >> a[i]; n     sum = sum + a[i]; n   } n   sort(a, a + n); n   i = 0; n   while (1) { n     if (sum % 2 == 0) { n       cout << sum; n       break; n     } else { n       if (a[i] % 2 == 1) { n         sum = sum - a[i]; n       } else { n         i++; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, *animals, i, j, temp; n   bool turn = 0; n   cin >> n; n   animals = new int[n]; n   for (i = 0; i < n; i++) cin >> animals[i]; n   vector<pair<int, int> > segmant; n   pair<int, int> seg; n   for (i = 0; i < (n - 1); i++) { n     if (animals[i] > animals[i + 1]) { n       turn = 1; n       seg.first = (i + 1); n       temp = animals[i]; n       animals[i] = animals[i + 1]; n       animals[i + 1] = temp; n       i += 2; n       for (j = i; j < n - 1; j++) { n         if (animals[j] > animals[j + 1]) { n           temp = animals[j]; n           animals[j] = animals[j + 1]; n           animals[j + 1] = temp; n           j++; n           continue; n         } else { n           break; n         } n       } n       seg.second = j; n     } n     if (turn == true) { n       segmant.push_back(seg); n       i = -1; n       turn = 0; n     } n   } n   for (i = 0; i < segmant.size(); i++) { n     cout << segmant[i].first <<     << segmant[i].second << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long LLINF = 1e18 + 7; n const long long INF = 1e9 + 7; n const long long EPS = 1e-9 - 7; n const long long MAX = 1e2 + 7; n const long long MOD = 1e9 + 7; n int n, d, dp[MAX]; n string s; n bool can[MAX], flag = false; n int main() { n   memset(dp, MAX, sizeof dp); n   cin >> n >> d >> s; n   for (int i = 0; i < n; i++) { n     if (s[i] ==  1 ) { n       can[i + 1] = true; n     } n   } n   for (int i = n - 1; i >= 1; i--) { n     if (n - i <= d && can[i]) { n       flag = true; n       dp[i] = 1; n     } n   } n   if (!flag) { n     cout << -1 << endl; n     return 0; n   } n   for (int i = n - 1; i >= 1; i--) { n     for (int j = i - 1; j >= 1; j--) { n       if (i - j <= d && can[j] && can[i]) { n         dp[j] = min(dp[j], dp[i] + 1); n       } n     } n   } n   if (dp[1] >= n) { n     cout << -1 << endl; n     return 0; n   } n   cout << dp[1] << endl; n   return 0; n } n


/* n     Mbak Sana Dong n     Mbak Chaeyoung Dong n     Mbak Eunha Dong n     Mbak Seulgi Dong n     Mbak Wendy Dong n     Stream https://www.youtube.com/watch?v=LmBYPXGqtss n */ n #pragma GCC optimize( Ofast ) n #pragma GCC target( avx,avx2,fma ) n #pragma GCC optimization ( unroll-loops,O3 ) n #include<bits/stdc++.h> n #define ll long long n using namespace std; n int main() n { n     int n,m,i,j,k,x,y,ans,curr,temp,q; n     int tc; n     cin >> tc; n     while(tc--) { n         string s; n         cin >> s; n         n = s.length(); n         s =     + s; n         vector<int> cnt(30, 0), vis(30, 0); n         for(i=1;i<=n;i++) { n             cnt[s[i] -  a ]++; n         } n  n         stack<char> st; n  n         for(i=1;i<=n;i++) { n             curr = s[i] -  a ; n             if(vis[curr]) { n                 cnt[curr]--; n                 continue; n             } n  n             while(!st.empty() && cnt[st.top() -  a ] > 0 && st.top() < s[i]) { n                 vis[st.top() -  a ] = 0; n                 st.pop(); n             } n  n             st.push(s[i]); n             cnt[curr]--; n             vis[curr] = 1; n         } n  n         string eunha =   ; n         while(!st.empty()) { n             eunha += st.top(); n             st.pop(); n         } n  n         reverse(eunha.begin(), eunha.end()); n         cout << eunha << endl; n     } n     return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int min(int a, int b) { return a > b ? b : a; } n int main() { n   int n, m; n   cin >> n >> m; n   cout << min(n, m) + 1 << endl; n   for (int i = 0; i <= min(n, m); i++) n     cout << i <<     << min(n, m) - i << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize(2) n using namespace std; n const int inf = 0x3f3f3f3f; n const long long INF = 0x3f3f3f3f3f3f3f3f; n char buf[1 << 21], *p1 = buf, *p2 = buf; n inline int gc() { n   return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) n              ? EOF n              : *p1++; n } n inline int read() { n   int ret = 0, f = 0; n   char c = gc(); n   while (!isdigit(c)) { n     if (c ==  - ) f = 1; n     c = gc(); n   } n   while (isdigit(c)) { n     ret = ret * 10 + c - 48; n     c = gc(); n   } n   if (f) return -ret; n   return ret; n } n int sign(double x) { n   if (x > 1e8) n     return 1; n   else if (x < -1e8) n     return -1; n   else n     return 0; n } n const int maxn = 1e6 + 10; n int a[maxn]; n pair<double, int> st[maxn]; n double pre[maxn]; n int precnt[maxn]; n int top; n int erfen(int l, int r, double val) { n   while (l + 1 < r) { n     int mid = l + r >> 1; n     if ((pre[top] - pre[mid - 1] + val) * 1.0 / n             (precnt[top] - precnt[mid - 1] + 1) <= n         st[mid].first) n       r = mid; n     else n       l = mid; n   } n   return r; n } n int main() { n   int n; n   scanf( %d , &n); n   for (int i = (1); i <= (n); i++) { n     scanf( %d , &a[i]); n   } n   st[++top] = make_pair(a[1] * 1.0, 1); n   pre[top] = a[1]; n   precnt[top] = 1; n   for (int i = (2); i <= (n); i++) { n     if (a[i] * 1.0 == st[top].first) { n       st[top].second++; n       pre[top] += a[i]; n       precnt[top]++; n     } else if (a[i] * 1.0 > st[top].first) { n       st[++top] = make_pair(a[i] * 1.0, 1); n       pre[top] = pre[top - 1] + a[i]; n       precnt[top] = precnt[top - 1] + 1; n     } else { n       int id = erfen(0, top + 1, a[i]); n       int cnt = (precnt[top] - precnt[id - 1] + 1); n       double tmp = pre[top] - pre[id - 1] + a[i]; n       double evenly = (pre[top] - pre[id - 1] + a[i]) * 1.0 / n                       (precnt[top] - precnt[id - 1] + 1); n       top = id; n       st[top].first = evenly; n       st[top].second = cnt; n       pre[top] = pre[top - 1] + tmp; n       precnt[top] = precnt[top - 1] + cnt; n     } n   } n   for (int i = (1); i <= (top); i++) { n     for (int j = (1); j <= (st[i].second); j++) { n       printf( %.9f n , st[i].first); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, a, b; n int x[205], y[205], c[205], siz[75], ans[75]; n int info[75], Prev[205 << 1], to[205 << 1], cst[205 << 1], cnt_e; n void Node(int u, int v, int ct) { n   Prev[++cnt_e] = info[u], info[u] = cnt_e, to[cnt_e] = v, cst[cnt_e] = ct; n } n int F[75], bl[75], cnt; n int Find(int now) { n   return (!F[now] || F[now] == now) ? now : F[now] = Find(F[now]); n } n int dp[1 << 18][75]; n int main() { n   scanf( %d%d%d%d , &n, &m, &a, &b); n   for (int i = 1; i <= n; i++) siz[i] = 1; n   for (int i = 1; i <= m; i++) { n     scanf( %d%d%d , &x[i], &y[i], &c[i]); n     if (c[i] == a) n       if (Find(x[i]) != Find(y[i])) { n         siz[Find(y[i])] += siz[Find(x[i])]; n         F[Find(x[i])] = Find(y[i]); n       } n     Node(x[i], y[i], c[i]), Node(y[i], x[i], c[i]); n   } n   for (int i = 1; i <= n; i++) n     if (siz[i] > 3 && Find(i) == i) n       bl[i] = cnt++; n     else if (Find(i) == i) n       bl[i] = -1; n   for (int i = 1; i <= n; i++) n     if (Find(i) != i) bl[i] = bl[Find(i)]; n   memset(dp, 0x3f, sizeof dp); n   memset(ans, 0x3f, sizeof ans); n   dp[bl[1] >= 0 ? 1 << bl[1] : 0][1] = 0; n   queue<int> q; n   for (int i = 0; i < (1 << cnt); i++) { n     for (int j = 1; j <= n; j++) n       if (dp[i][j] < 0x3f3f3f3f) q.push(j); n     for (int now; !q.empty();) { n       now = q.front(), q.pop(); n       for (int j = info[now]; j; j = Prev[j]) n         if (cst[j] == a || n             (cst[j] == b && bl[to[j]] == -1 && Find(now) != Find(to[j]))) { n           if (dp[i][to[j]] > dp[i][now] + cst[j]) { n             dp[i][to[j]] = dp[i][now] + cst[j]; n             q.push(to[j]); n           } n         } else if (Find(now) != Find(to[j]) && !(i >> bl[to[j]] & 1)) n           dp[i | (1 << bl[to[j]])][to[j]] = n               min(dp[i | (1 << bl[to[j]])][to[j]], dp[i][now] + cst[j]); n     } n     for (int j = 1; j <= n; j++) n       if (dp[i][j] < 0x3f3f3f3f) ans[j] = min(ans[j], dp[i][j]); n   } n   for (int i = 1; i <= n; i++) printf( %d%c , ans[i], i == 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   int n; n   cin >> n; n   vector<pair<int, int>> v(n); n   for (auto &x : v) cin >> x.first >> x.second; n   if (is_sorted(v.rbegin(), v.rend())) { n     bool ok = true; n     for (auto x : v) n       if (x.first != x.second) ok = false; n     if (ok) { n       cout <<  maybe n ; n       return 0; n     } n   } n   for (auto x : v) n     if (x.first != x.second) { n       cout <<  rated n ; n       return 0; n     } n   cout <<  unrated n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int inf = (1 << 29) - 1; n int n, m, S, T, ans; n int a[20000], b[20]; n struct edge { n   int b, len; n   edge *nxt, *op; n } e[100000], *le(e); n edge *head[20000]; n void add(int u, int v, int w) { n   le->b = v, le->len = w, le->nxt = head[u], le->op = le + 1, head[u] = le++; n   le->b = u, le->len = 0, le->nxt = head[v], le->op = le - 1, head[v] = le++; n } n edge *cur[20000], *pre[20000]; n int low[20000], dis[20000], gap[20000]; n void init() { n   int g, v, w, k, p, q; n   scanf( %d%d%d , &n, &m, &g); n   S = 0, T = n + m + 1; n   for (int i = 1; i <= (n); ++i) scanf( %d , &a[i]); n   for (int i = 1; i <= (n); ++i) { n     scanf( %d , &v); n     if (a[i]) n       add(i, T, v); n     else n       add(S, i, v); n   } n   for (int i = 1; i <= (m); ++i) { n     scanf( %d%d%d , &p, &w, &k); n     for (int j = 1; j <= (k); ++j) scanf( %d , &b[j]); n     scanf( %d , &q); n     ans += w; n     if (q) w += g; n     if (p) { n       for (int j = 1; j <= (k); ++j) add(b[j], n + i, inf); n       add(n + i, T, w); n     } else { n       add(S, n + i, w); n       for (int j = 1; j <= (k); ++j) add(n + i, b[j], inf); n     } n   } n } n int SAP() { n   int flow(0); n   low[S] = inf, gap[0] = T + 1; n   for (int i = (S); i <= (T); ++i) dis[i] = 0, cur[i] = head[i]; n   edge *i; n   int u(S), v; n   while (dis[S] < T + 5) { n     for (i = cur[u]; i; i = i->nxt) n       if (i->len && dis[v = i->b] + 1 == dis[u]) break; n     if (i) { n       cur[u] = i; n       pre[v] = i; n       low[v] = min(low[u], i->len); n       if ((u = v) == T) { n         for (; u != S; u = pre[u]->op->b) { n           pre[u]->len -= low[T]; n           pre[u]->op->len += low[T]; n         } n         flow += low[T]; n       } n     } else { n       cur[u] = head[u]; n       if (--gap[dis[u]] == 0) break; n       dis[u] = T + 5; n       for (i = cur[u]; i; i = i->nxt) n         if (i->len && dis[i->b] + 1 < dis[u]) dis[u] = dis[i->b] + 1; n       ++gap[dis[u]]; n       if (u != S) u = pre[u]->op->b; n     } n   } n   return flow; n } n int main() { n   init(); n   ans -= SAP(); n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dist[2005][2005]; n char f[2005][2005]; n int n, m, r, c, x, y; n int main() { n   scanf( %d%d%d%d%d%d , &n, &m, &r, &c, &x, &y); n   x = min(x, 10000000); n   y = min(y, 10000000); n   r--; n   c--; n   for (int i = 0; i < n; i++) scanf( %s , &f[i]); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) dist[i][j] = 1000000000; n   dist[r][c] = 0; n   deque<pair<int, pair<int, int> > > que; n   que.push_back(make_pair(0, make_pair(r, c))); n   while (!que.empty()) { n     pair<int, pair<int, int> > p = que.front(); n     que.pop_front(); n     if (p.first != dist[p.second.first][p.second.second]) continue; n     int X = p.second.first, Y = p.second.second; n     int dx[4] = {0, 1, 0, -1}; n     int dy[4] = {1, 0, -1, 0}; n     for (int k = 0; k < 4; k++) { n       int nx = X + dx[k], ny = Y + dy[k]; n       if (!(0 <= nx && nx < n && 0 <= ny && ny < m)) continue; n       if (f[nx][ny] ==  * ) continue; n       int add = (k == 2); n       if (dist[nx][ny] > p.first + add) { n         dist[nx][ny] = p.first + add; n         if (add == 0) n           que.push_front(make_pair(p.first + add, make_pair(nx, ny))); n         else n           que.push_back(make_pair(p.first + add, make_pair(nx, ny))); n       } n     } n   } n   int ans = 0; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) { n       int mnx = dist[i][j]; n       int mny = j + dist[i][j] - c; n       if (mnx <= x && mny <= y) ans++; n     } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline bool chkmin(T &a, const T &b) { n   return a > b ? a = b, 1 : 0; n } n template <typename T> n inline bool chkmax(T &a, const T &b) { n   return a < b ? a = b, 1 : 0; n } n const int oo = 0x3f3f3f3f; n const int maxn = 500100; n int n; n vector<int> chd[maxn + 5]; n int fa[maxn + 5]; n int dep[maxn + 5]; n void dfs(int first) { n   for (auto second : chd[first]) dep[second] = dep[first] + 1, dfs(second); n } n struct node { n   node *c[2], *p; n   int label; n   long long sum; n   int val; n   int sz; n   node() : p(NULL), label(0), sum(0), val(0), sz(1) { memset(c, 0, sizeof c); } n   void update() { n     sum = val; n     sz = 1; n     for (int i = (0), i_end_ = (2); i < i_end_; ++i) n       if (c[i]) sum += c[i]->sum, sz += c[i]->sz; n   } n   void flag_label(int _label) { n     label += _label; n     val += _label; n     sum += (long long)sz * _label; n   } n   void push_down() { n     if (label) { n       for (int i = (0), i_end_ = (2); i < i_end_; ++i) n         if (c[i]) c[i]->flag_label(label); n       label = 0; n     } n   } n   int get_pos() { n     if (!this || !p) return 2; n     for (int i = (0), i_end_ = (2); i < i_end_; ++i) n       if (p->c[i] == this) return i; n     return 2; n   } n   bool is_root() { return get_pos() >= 2; } n   void setc(node *u, const int &v) { n     if (this && v < 2) c[v] = u; n     if (u) u->p = this; n   } n   void rotate() { n     node *_p = this->p; n     bool mark = get_pos(); n     _p->p->setc(this, _p->get_pos()); n     _p->setc(c[mark ^ 1], mark); n     setc(_p, mark ^ 1); n     _p->update(); n   } n   void relax() { n     static node *tmp[maxn + 5]; n     int top = 0; n     node *u = this; n     while (!u->is_root()) tmp[top++] = u, u = u->p; n     u->push_down(); n     while (top) tmp[--top]->push_down(); n   } n   void splay() { n     relax(); n     for (; !is_root(); rotate()) n       if (!p->is_root()) n         ((p->p->c[0] == p) == (p->c[0] == this) ? p : this)->rotate(); n     update(); n   } n   node *access() { n     node *u = this, *v = NULL; n     for (; u; v = u, u = u->p) u->splay(), u->setc(v, 1), u->update(); n     splay(); n     return v; n   } n }; n node nd[maxn + 5]; n vector<int> all[maxn + 5]; n long long ans[maxn + 5]; n int main() { n   scanf( %d , &n); n   for (int i = (0), i_end_ = (n); i < i_end_; ++i) scanf( %d , fa + i), --fa[i]; n   int rt = -1; n   for (int i = (0), i_end_ = (n); i < i_end_; ++i) n     if (!~fa[i]) n       rt = i; n     else n       chd[fa[i]].push_back(i); n   dep[rt] = 0; n   dfs(rt); n   for (int i = (0), i_end_ = (n); i < i_end_; ++i) { n     nd[i].val = 0; n     if (~fa[i]) n       nd[i].p = nd + fa[i]; n     else n       nd[i].p = NULL; n   } n   for (int i = (0), i_end_ = (n); i < i_end_; ++i) all[dep[i]].push_back(i); n   for (int i = (0), i_end_ = (n); i < i_end_; ++i) { n     for (auto first : all[i]) { n       node *u = nd + first; n       u->access(); n       u->flag_label(1); n     } n     for (auto first : all[i]) { n       node *u = nd + first; n       u->access(); n       ans[first] = u->sum - dep[first] - 1; n     } n   } n   for (int i = (0), i_end_ = (n); i < i_end_; ++i) n     printf( n          %  n          ll  n          d  , n         ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long judge(long long mid) { n   long long b; n   long long cnt = 0; n   for (b = 2;; b++) { n     cnt += mid / b / b / b; n     if (mid / b / b / b == 0) break; n   } n   return cnt; n } n int main() { n   long long m; n   cin >> m; n   long long r = 1e16, l = 0; n   while (l < r - 1) { n     long long mid = (l + r) / 2; n     if (judge(mid) >= m) n       r = mid; n     else n       l = mid; n   } n   if (judge(l) == m) n     cout << l << endl; n   else if (judge(r) == m) n     cout << r << endl; n   else n     cout <<  -1  << endl; n   return 0; n } n


#include <bits/stdc++.h> n struct task { n   task() : pc(-1), indegree(0), outgoing() {} n   int pc; n   int indegree; n   std::vector<int> outgoing; n }; n int solve(int pc, std::vector<task> tasks) { n   std::queue<int> todo[3]; n   for (__decltype((tasks).begin()) t = (tasks).begin(), t_end = (tasks).end(); n        t != t_end; ++t) n     if (t->indegree == 0) todo[t->pc].push(t - tasks.begin()); n   int time = 0; n   for (std::size_t solved = 0; solved < tasks.size(); pc = (pc + 1) % 3) { n     while (!todo[pc].empty()) { n       int t = todo[pc].front(); n       for (__decltype((tasks[t].outgoing).begin()) n                n = (tasks[t].outgoing).begin(), n                n_end = (tasks[t].outgoing).end(); n            n != n_end; ++n) { n         if (--tasks[*n].indegree == 0) todo[tasks[*n].pc].push(*n); n       } n       todo[pc].pop(); n       ++time; n       ++solved; n     } n     ++time; n   } n   return time - 1; n } n int main() { n   int n; n   scanf( %d , &n); n   std::vector<task> tasks(n); n   for (int i = 0; i < n; ++i) { n     scanf( %d , &tasks[i].pc); n     --tasks[i].pc; n   } n   for (int i = 0; i < n; ++i) { n     int k; n     scanf( %d , &k); n     while (k--) { n       int a; n       scanf( %d , &a); n       --a; n       tasks[a].outgoing.push_back(i); n       ++tasks[i].indegree; n     } n   } n   printf( %d n , n          std::min(std::min(solve(0, tasks), solve(1, tasks)), solve(2, tasks))); n } n


#include <bits/stdc++.h> n using namespace std; n int N, K; n int h[100005]; n bool isok[100005]; n int check(int L) { n   deque<int> A, B; n   int ret = 0; n   for (int i = 1; i <= N; ++i) { n     while (A.size() && A.front() + L <= i) A.pop_front(); n     while (B.size() && B.front() + L <= i) B.pop_front(); n     while (A.size() && h[A.back()] >= h[i]) A.pop_back(); n     while (B.size() && h[B.back()] <= h[i]) B.pop_back(); n     A.push_back(i); n     B.push_back(i); n     if (i >= L) ret += (isok[i - L + 1] = (h[B.front()] - h[A.front()] <= K)); n   } n   return ret; n } n int main() { n   while (scanf( %d %d , &N, &K) == 2) { n     for (int i = 1; i <= N; ++i) scanf( %d , h + i); n     int lo = 1, hi = N + 1, mid; n     while (lo + 1 < hi) { n       mid = lo + hi >> 1; n       if (check(mid)) n         lo = mid; n       else n         hi = mid; n     } n     int s = check(lo); n     printf( %d %d n , lo, s); n     for (int i = 1; i + lo - 1 <= N; ++i) n       if (isok[i]) printf( %d %d n , i, i + lo - 1); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int size = 110000, mod = 1000000007, inf = 0x3f3f3f3f; n const double pi = acos(-1.0); n int month[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; n int nex[2][4] = {0, 1, 0, -1, 1, 0, -1, 0}; n long long cal(long long a, int n) { n   long long s = 1 % mod; n   while (n) { n     if (n & 1) s = (s * a) % mod; n     a = (a * a) % mod; n     n /= 2; n   } n   return s; n } n int n, ar[size]; n long long ans; n vector<int> fac, fac2; n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) cin >> ar[i]; n   sort(ar, ar + n); n   for (int i = ar[n - 1]; i >= 1; i--) { n     long long t = 1; n     fac.clear(); n     for (int j = 1; j * j <= i; j++) n       if (i % j == 0) { n         fac.push_back(j); n         if (j * j != i) fac.push_back(i / j); n       } n     sort(fac.begin(), fac.end()); n     int pre = 0; n     for (int j = 1; j < fac.size(); j++) { n       int num = lower_bound(ar, ar + n, fac[j]) - ar; n       t = t * cal(j, num - pre) % mod; n       pre = num; n     } n     t = t * (cal(fac.size(), n - pre) - cal(fac.size() - 1, n - pre)) % mod; n     ans = (ans + t) % mod; n   } n   cout << (ans + mod) % mod << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   int n, x; n   cin >> n >> x; n   int temp, cnt = 0; n   for (int i = 0; i < n; i++) { n     cin >> temp; n     cnt += temp; n   } n   if ((cnt + n - 1) == x) n     cout <<  YES  << endl; n   else n     cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int X; n vector<int> v[100010]; n int all, all2; n bool used[800 + 10]; n int dp[800 + 10], dp2[800 + 10]; n void calc(int x) { n   int i; n   for ((i) = 0; (i) < (int)(800); (i)++) used[i] = false; n   for ((i) = 0; (i) < (int)(v[x].size()); (i)++) used[v[x][i]] = true; n   for ((i) = 0; (i) < (int)(800); (i)++) dp2[i] = (1 << 29); n   int cost = 0; n   for (i = 800 - 1; i >= 0; i--) { n     if (used[i]) cost += 3; n     int tmp = cost + dp[max(i - 1, 0)]; n     if (tmp < dp2[i]) dp2[i] = tmp; n     if (i > 0) tmp += i * (i + 1) / 2 + 2; n     if (tmp < dp2[0]) dp2[0] = tmp; n   } n   if (v[x].empty()) all2 = all; n   for ((i) = 0; (i) < (int)(800); (i)++) dp[i] = dp2[i]; n   for ((i) = 0; (i) < (int)(800); (i)++) dp[i + 1] = min(dp[i + 1], dp[i]); n } n int main(void) { n   int K, i, j, ans = 0; n   cin >> X >> K; n   for ((i) = 0; (i) < (int)(K); (i)++) { n     int r, c; n     scanf( %d%d , &r, &c); n     int x = c - 1; n     int y = X - r; n     if (y < 800) n       v[x].push_back(y); n     else n       ans += 3; n   } n   for (i = X - 1; i >= 0; i--) calc(i); n   cout << dp[0] + ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline void cmin(T &a, T b) { n   ((a > b) && (a = b)); n } n int cmax(int &a, int b) { return a < b ? a = b, 1 : 0; } n char IO; n template <class T = int> n T rd() { n   T s = 0; n   int f = 0; n   while (!isdigit(IO = getchar())) f |= IO ==  - ; n   do s = (s << 1) + (s << 3) + (IO ^  0 ); n   while (isdigit(IO = getchar())); n   return f ? -s : s; n } n const int N = 1e6 + 10, INF = 1e9 + 10; n int n, m, k, T; n int s[N], t[N]; n int F[1 << 20], G[1 << 20]; n int p[N], a[N], b[N]; n int main() { n   n = rd(), m = rd(), k = rd(); n   for (int i = 0, iend = k - 1; i <= iend; ++i) scanf( %1d , s + i); n   for (int i = 0, iend = k - 1; i <= iend; ++i) scanf( %1d , t + i); n   for (int i = 0, iend = k - 1; i <= iend; ++i) p[i] = i; n   memset(F, 63, sizeof F), memset(G, -63, sizeof G); n   int S = 0; n   for (int i = 0, iend = k - 1; i <= iend; ++i) S |= s[i] << i; n   F[S] = 0; n   for (int i = 1, iend = n; i <= iend; ++i) { n     int x = rd() - 1, y = rd() - 1; n     for (int j = 0, jend = k - 1; j <= jend; ++j) n       if (p[j] == x) { n         x = j; n         break; n       } n     for (int j = 0, jend = k - 1; j <= jend; ++j) n       if (p[j] == y) { n         y = j; n         break; n       } n     swap(p[x], p[y]); n     int S = 0, T = 0; n     for (int j = 0, jend = k - 1; j <= jend; ++j) { n       S |= s[p[j]] << j; n       T |= t[p[j]] << j; n     } n     cmin(F[S], i), G[T] = i; n   } n   int ans = 0, l = 1, r = n, c1 = 0, c2 = 0; n   for (int i = 0, iend = k - 1; i <= iend; ++i) c1 += s[i], c2 += t[i]; n   for (int S = (1 << k) - 1, Send = 0; S >= Send; --S) { n     if (G[S] - F[S] >= m) n       if (cmax(ans, k - (c1 + c2 - 2 * __builtin_popcount(S)))) n         l = F[S] + 1, r = G[S]; n     for (int i = 0, iend = k - 1; i <= iend; ++i) n       if (S & (1 << i)) { n         cmin(F[S ^ (1 << i)], F[S]); n         cmax(G[S ^ (1 << i)], G[S]); n       } n   } n   printf( %d n%d %d n , ans, l, r); n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> graph[100005]; n vector<pair<long long, long long> > graph_p[100005]; n vector<pair<long long, long long> > op; n vector<long long> vis(100005, 0); n long long power(long long x, long long y) { n   long long res = 1; n   x = x % 1000000009; n   if (x == 0) return 0; n   while (y > 0) { n     if (y & 1) res = ((res % 1000000009) * (x % 1000000009)) % 1000000009; n     y = y >> 1; n     x = ((x % 1000000009) * (x % 1000000009)) % 1000000009; n   } n   return res; n } n void init_graph(long long n, long long m) { n   cin >> n >> m; n   for (long long i = 1; i <= m; i++) { n     long long a, b; n     cin >> a >> b; n     graph[a].push_back(b); n     graph[b].push_back(a); n   } n } n void sieve(long long n) { n   bool prime[n + 1]; n   memset(prime, true, sizeof(prime)); n   for (long long p = 2; p * p <= n; p++) { n     if (prime[p] == true) { n       for (long long i = p * p; i <= n; i += p) prime[i] = false; n     } n   } n } n long long n, h, l[2000005], r[2000005], ans; n long long sum[2000005], de[2000005]; n void we_have_choices() { n   cin >> n >> h; n   for (long long i = 1; i <= n; i++) { n     cin >> l[i] >> r[i]; n     sum[i] = sum[i - 1] + r[i] - l[i]; n     if (i == 1) n       de[i] = 0; n     else n       de[i] = de[i - 1] + l[i] - r[i - 1]; n   } n   for (long long i = 1; i <= n; i++) { n     long long pos = lower_bound(de + i, de + n + 1, de[i] + h) - de; n     ans = max(ans, sum[pos - 1] - sum[i - 1] + h); n   } n   cout << ans <<   n ; n } n int32_t main() { n   ios_base::sync_with_stdio(false), cin.tie(nullptr); n   ; n   long long t = 1; n   while (t--) we_have_choices(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 5; n int n, k, a[maxn]; n vector<int> ans; n long long sum; n int main() { n   ios::sync_with_stdio(false); n   cin >> n >> k; n   for (int i = 1; i <= n; ++i) { n     cin >> a[i]; n     sum += a[i]; n   } n   int partsum = sum / k; n   int tmp = 0, lastpos = 0; n   bool flag = false; n   for (int i = 1; i <= n; ++i) { n     tmp += a[i]; n     if (tmp == partsum) { n       ans.push_back(i - lastpos); n       lastpos = i; n       tmp = 0; n     } n     if (tmp >= partsum) { n       flag = true; n       break; n     } n   } n   if (flag) n     cout <<  No  << endl; n   else if (ans.size() * partsum != sum) n     cout <<  No  << endl; n   else { n     cout <<  Yes  << endl; n     for (auto s : ans) cout << s <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 10; n const int maxm = 300005; n struct segtree { n   int tree[30 * maxm], L[30 * maxm], R[30 * maxm]; n   int sz = 0; n   int root[maxm]; n   int update(int pre, int l, int r, int pos, int val) { n     if (l > r || pos < l || pos > r) return pre; n     int no = ++sz; n     tree[no] = tree[pre]; n     L[no] = L[pre]; n     R[no] = R[pre]; n     if (l == r && l == pos) { n       tree[no] += val; n       R[no] = L[no] = 0; n       return no; n     } n     int mid = (l + r) >> 1; n     L[no] = update(L[pre], l, mid, pos, val); n     R[no] = update(R[pre], mid + 1, r, pos, val); n     tree[no] = tree[L[no]] + tree[R[no]]; n     return no; n   } n   int sum(int no, int l, int r, int pos) { n     if (l == r) return 0; n     int mid = (l + r) >> 1; n     if (pos <= mid) return sum(L[no], l, mid, pos); n     return tree[L[no]] + sum(R[no], mid + 1, r, pos); n   } n   int get(int no, int l, int r, int pos) { n     if (l == r) return tree[no]; n     int mid = (l + r) >> 1; n     if (pos <= mid) return get(L[no], l, mid, pos); n     return get(R[no], mid + 1, r, pos); n   } n } a, b; n map<string, int> mapa; n const int inf = 1e9 + 10; n int main() { n   int q; n   scanf( %d , &q); n   int idx = 1; n   for (int i = 1; i <= q; i++) { n     char op[15]; n     scanf( %s , op); n     a.root[i] = a.root[i - 1]; n     b.root[i] = b.root[i - 1]; n     if (op[0] ==  s ) { n       char s[20]; n       int val; n       scanf( %s %d , s, &val); n       string t = s; n       if (mapa.count(t)) { n         int x = a.get(a.root[i], 1, maxn, mapa[t]); n         a.root[i] = a.update(a.root[i], 1, maxn, mapa[t], val - x); n         if (x != 0) { n           b.root[i] = b.update(b.root[i], 1, inf, x, -1); n         } n         b.root[i] = b.update(b.root[i], 1, inf, val, 1); n       } else { n         mapa[t] = idx++; n         a.root[i] = a.update(a.root[i], 1, maxn, mapa[t], val); n         b.root[i] = b.update(b.root[i], 1, inf, val, 1); n       } n     } else if (op[0] ==  r ) { n       char s[20]; n       scanf( %s , s); n       string t = s; n       if (mapa.count(t)) { n         int x = a.get(a.root[i], 1, maxn, mapa[t]); n         a.root[i] = a.update(a.root[i], 1, maxn, mapa[t], -x); n         if (x != 0) { n           b.root[i] = b.update(b.root[i], 1, inf, x, -1); n         } n       } n     } else if (op[0] ==  q ) { n       char s[20]; n       scanf( %s , s); n       string t = s; n       if (mapa.count(t)) { n         int x = a.get(a.root[i], 1, maxn, mapa[t]); n         if (x != 0) { n           printf( %d n , b.sum(b.root[i], 1, inf, x)); n         } else n           puts( -1 ); n       } else n         puts( -1 ); n       fflush(stdout); n     } else { n       int x; n       scanf( %d , &x); n       a.root[i] = a.root[i - x - 1]; n       b.root[i] = b.root[i - x - 1]; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void doRoutine() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n } n long long int gcd(long long int a, long long int b) { n   return b ? gcd(b, a % b) : a; n } n signed main() { n   doRoutine(); n   vector<long long int> a(6); n   long long int s = 0; n   for (long long int i = 0; i < 6; i++) { n     cin >> a[i]; n     s += a[i]; n   } n   for (long long int i = 0; i < 6; i++) { n     for (long long int j = 0; j < 6; j++) { n       for (long long int l = 0; l < 6; l++) { n         if (i != j && j != l && l != i) { n           if (a[i] + a[j] + a[l] == s - a[i] - a[j] - a[l]) n             return cout <<  YES  <<   n , 0; n         } n       } n     } n   } n   cout <<  NO  <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int dd = (int)2e6 + 7; n int A[dd], cnt[dd]; n long long tmp[dd]; n pair<int, int> msk[20]; n int count2[20], w; n long long pw[20]; n long long ans = 0; n void get(int i, long long k) { n   if (i == w) { n     ans += tmp[k]; n     return; n   } n   if (count2[i] == 1) { n     get(i + 1, k + pw[i] * msk[i].first); n     return; n   } n   get(i + 1, k + pw[i] * msk[i].first); n   get(i + 1, k + pw[i] * msk[i].second); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int n, m; n   cin >> w >> n >> m; n   for (int i = 0; i < n; ++i) { n     cin >> A[i]; n     cnt[A[i]]++; n   } n   pw[0] = 1; n   for (int i = 1; i <= w; ++i) { n     pw[i] = pw[i - 1] * 3; n   } n   for (int i = 0; i < (1 << w); ++i) n     if (cnt[i]) { n       for (int j = 0; j < (1 << w); ++j) n         if (cnt[j]) { n           int k = 0; n           for (int l = 0; l < w; ++l) { n             int ii = (i >> l) & 1, jj = (j >> l) & 1; n             if (ii == jj && ii == 1) { n               k += 2 * pw[l]; n             } n             if (ii != jj) { n               k += pw[l]; n             } n           } n           tmp[k] += cnt[i] * cnt[j]; n         } n     } n   while (m--) { n     string s; n     cin >> s; n     reverse(s.begin(), s.end()); n     for (int i = 0; i < w; ++i) { n       if (s[i] ==  A ) msk[i] = {0, 1}, count2[i] = 2; n       if (s[i] ==  O ) msk[i] = {0, 0}, count2[i] = 1; n       if (s[i] ==  X ) msk[i] = {0, 2}, count2[i] = 2; n       if (s[i] ==  a ) msk[i] = {2, 2}, count2[i] = 1; n       if (s[i] ==  o ) msk[i] = {1, 2}, count2[i] = 2; n       if (s[i] ==  x ) msk[i] = {1, 1}, count2[i] = 1; n     } n     ans = 0; n     get(0, 0); n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n ostream& operator<<(ostream& o, const vector<T>& v) { n   o <<  [ ; n   int b = 0; n   for (const auto& a : v) o << (b++ ?  ,   :   ) << a; n   o <<  ] ; n   return o; n } n template <typename T, typename U> n ostream& operator<<(ostream& o, const pair<T, U>& p) { n   o <<  (  << p.first <<  ,   << p.second <<  ) ; n   return o; n } n template <typename T> n ostream& operator<<(ostream& o, const set<T>& s) { n   o <<  { ; n   int b = 0; n   for (auto& e : s) o << (b++ ?  ,   :   ) << e; n   o <<  } ; n   return o; n } n template <typename T, typename U> n ostream& operator<<(ostream& o, const map<T, U>& m) { n   o <<  { ; n   int b = 0; n   for (auto& e : m) o << (b++ ?  ,   :   ) << e; n   o <<  } ; n   return o; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n; n   cin >> n; n   bool flag = 0; n   vector<int> A(n); n   for (auto& a : A) { n     cin >> a; n     if (a % 2 and flag) { n       if (a < 0) { n         a = a / 2; n       } else { n         a = (a + 1) / 2; n       } n       flag ^= 1; n     } else if (a % 2) { n       if (a < 0) { n         a = (a - 1) / 2; n       } else { n         a = a / 2; n       } n       flag ^= 1; n     } else { n       a = a / 2; n     } n   } n   for (auto& a : A) cout << a << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long pwr(long long base, long long p, long long mod = (1000000007LL)) { n   long long ans = 1; n   while (p) { n     if (p & 1) ans = (ans * base) % mod; n     base = (base * base) % mod; n     p /= 2; n   } n   return ans; n } n long long gcd(long long a, long long b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n int n, col[100005]; n vector<int> adj[100005], adjacent; n set<int> distinct_; n void dfs(int v, int par) { n   for (auto vv : adj[v]) n     if (vv != par) { n       if (col[v] != col[vv]) { n         adjacent.push_back(v); n         adjacent.push_back(vv); n       } n       dfs(vv, v); n     } n } n void verify(int v, int par) { n   distinct_.insert(col[v]); n   for (auto vv : adj[v]) n     if (vv != par) verify(vv, v); n } n bool check(int v) { n   for (auto vv : adj[v]) { n     distinct_.clear(); n     verify(vv, v); n     if ((int)distinct_.size() > 1) return false; n   } n   return true; n } n int main() { n   scanf( %d , &n); n   int e = n - 1; n   while (e--) { n     int a, b; n     scanf( %d%d , &a, &b); n     adj[a].push_back(b); n     adj[b].push_back(a); n   } n   for (int i = 1; i <= n; i++) { n     scanf( %d , &col[i]); n     distinct_.insert(col[i]); n   } n   if ((int)distinct_.size() == 1) { n     printf( YES n ); n     printf( 1 n ); n     return 0; n   } n   if (check(1)) { n     printf( YES n ); n     printf( 1 n ); n     return 0; n   } n   dfs(1, -1); n   if (adjacent.empty()) { n     printf( YES n ); n     printf( 1 n ); n     return 0; n   } n   if (check(adjacent[0])) { n     printf( YES n ); n     printf( %d n , adjacent[0]); n     return 0; n   } n   if (check(adjacent[1])) { n     printf( YES n ); n     printf( %d n , adjacent[1]); n     return 0; n   } n   printf( NO n ); n   return 0; n } n


#include <bits/stdc++.h> n const long long MOD = 1e9 + 7; n using pii = std::pair<long long, long long>; n using namespace std; n const long long maxn = 5e5 + 5; n long long n, a[maxn], origvals[maxn], bit[2][maxn]; n long long sum(long long dir, long long idx) { n   long long curans = 0; n   for (++idx; idx > 0; idx -= idx & -idx) curans += bit[dir][idx]; n   return curans; n } n void add(long long dir, long long idx, long long val) { n   for (++idx; idx < maxn; idx += idx & -idx) bit[dir][idx] += val; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cin >> n; n   vector<pii> compress; n   for (long long i = 0; i < n; i++) { n     cin >> a[i]; n     compress.push_back({a[i], i}); n   } n   sort(compress.begin(), compress.end()); n   for (long long i = 0; i < compress.size(); i++) { n     origvals[i] = compress[i].first; n     a[compress[i].second] = i; n   } n   long long ans = 0; n   for (long long i = 0; i < n; i++) { n     add(0, a[i], i + 1); n     long long cnt = (sum(0, a[i]) * (n - i)) % MOD; n     ans += (origvals[a[i]] * cnt) % MOD; n     ans %= MOD; n   } n   for (long long i = n - 1; i >= 0; i--) { n     add(1, a[i], n - i); n     long long cnt = ((sum(1, a[i]) * (i + 1))) % MOD; n     ans += (origvals[a[i]] * cnt) % MOD; n     ans %= MOD; n   } n   for (long long i = 0; i < n; i++) { n     long long cnt = ((i + 1) * (n - i)) % MOD; n     ans -= (origvals[a[i]] * cnt) % MOD; n     ans += MOD; n     ans %= MOD; n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a[200], n, k; n   for (int i = 0; i <= 200; i++) a[i] = 0; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> k; n     a[k]++; n   } n   int kol = 0; n   for (int i = 1; i <= 198; i++) kol += a[i] / 2; n   cout << kol / 2; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 4000 + 5, mod = 1000000009; n int n, x, y; n long long P[N], d[N][N]; n int main() { n   scanf( %d , &n); n   scanf( %d , &x); n   scanf( %d , &y); n   for (int i = 0; i <= max(x, y); i++) d[i][0] = 1; n   for (int i = 1; i <= max(x, y); i++) n     for (int j = 1; j <= min(n, i); j++) n       d[i][j] = (d[i - 1][j] + d[i - 1][j - 1]) % mod; n   P[0] = 1; n   for (int i = 1; i <= max(x, y); i++) P[i] = (P[i - 1] * i) % mod; n   long long ans = 0; n   for (int l = 2; l <= n - 1; l++) n     for (int r = l; r <= n - 1; r++) n       ans = (ans + (d[y - 1][r - l] * d[x - 1][n - (r - l + 2)]) % mod) % mod; n   ans = ans * ((P[x] * P[y]) % mod) % mod; n   printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long double eps = 1e-9; n const long long INF = 1ll << 61, Mo = 1e9 + 7; n int n, m, t, tu, tp, td; n int h[400][400]; n int s[400][400], x[400][400], z[400][400], y[400][400]; n set<pair<int, int> >::iterator ii; n inline int time(int h1, int h2) { n   if (h1 == h2) return tp; n   if (h1 < h2) return tu; n   return td; n } n void init() { n   scanf( %d%d%d , &n, &m, &t); n   scanf( %d%d%d , &tp, &tu, &td); n   int i, j; n   for (i = 1; i <= n; i++) n     for (j = 1; j <= m; j++) scanf( %d , &h[i][j]); n   for (j = 1; j <= m; j++) n     for (i = 2; i <= n; i++) { n       x[i][j] = x[i - 1][j] + time(h[i - 1][j], h[i][j]); n       s[i][j] = s[i - 1][j] + time(h[i][j], h[i - 1][j]); n     } n   for (i = 1; i <= n; i++) n     for (j = 2; j <= m; j++) { n       y[i][j] = y[i][j - 1] + time(h[i][j - 1], h[i][j]); n       z[i][j] = z[i][j - 1] + time(h[i][j], h[i][j - 1]); n     } n } n int main() { n   int i, j, k, w, ux, uy, dx, dy; n   init(); n   int ans = 2e9; n   for (i = 1; i <= n; i++) n     for (j = i + 2; j <= n; j++) { n       vector<pair<int, int> > V; n       set<pair<int, int> > U; n       for (k = 3; k <= m; k++) { n         V.push_back(pair<int, int>(y[i][k] + z[j][k] + x[j][k] - x[i][k], k)); n         U.insert(V.back()); n       } n       for (k = 0; k < V.size(); k++) { n         int tmp = t - (s[j][k + 1] - s[i][k + 1]) + (y[i][k + 1] + z[j][k + 1]); n         ii = U.lower_bound(pair<int, int>(tmp, 0)); n         if (ii != U.end() && ii->first - tmp < ans) { n           ans = ii->first - tmp; n           ux = i; n           uy = k + 1; n           dx = j; n           dy = ii->second; n         } n         if (ii != U.begin()) { n           ii--; n           if (tmp - ii->first < ans) { n             ans = tmp - ii->first; n             ux = i; n             uy = k + 1; n             dx = j; n             dy = ii->second; n           } n         } n         U.erase(V[k]); n       } n     } n   printf( %d %d %d %d n , ux, uy, dx, dy); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char BUFFER[100000 + 5]; n bool readn(int &n) { return scanf( %d , &n) == 1; } n bool readl(long long &n) { return scanf( %I64d , &n) == 1; } n bool readd(double &n) { return scanf( %lf , &n) == 1; } n bool reads(string &s) { n   s =   ; n   int n = scanf( %s , BUFFER); n   if (n == 1) s = BUFFER; n   return n == 1; n } n bool readln(string &s) { n   char *valid = gets(BUFFER); n   if (valid) s = BUFFER; n   return ((bool)valid); n } n void Print(long long &n, bool printNewLine = true) { n   printNewLine ? printf( %I64d n , n) : printf( %I64d , n); n } n const int maxn = 1e5 + 10; n int carry[maxn], f[maxn], d[maxn]; n int anscnt = 0, ansfuel = 0; n int N, D, F; n vector<int> a, b; n void calc() { n   int i, j, sum = 0; n   for (i = 0; i < N; ++i) n     if (d[i] >= D) n       if (carry[i] > 0) n         a.push_back(f[i]); n       else n         b.push_back(f[i]); n   sort(a.begin(), a.end()); n   sort(b.begin(), b.end()); n   for (i = 0; i < ((int)b.size()); ++i) { n     sum += b[i]; n     if (sum > F) break; n     anscnt = i + 1; n     ansfuel = sum; n   } n   if (0 == ((int)a.size()) || a[0] > F) return; n   int C = 0; n   for (i = 0; i < N; ++i) { n     C += carry[i]; n     C = min(C, N); n   } n   sum = a[0]; n   i = 1; n   j = 0; n   while (1) { n     if (sum > F) break; n     int cnt = min(i + j + C, N); n     if (cnt > anscnt || (cnt == anscnt && sum < ansfuel)) { n       anscnt = cnt; n       ansfuel = sum; n     } n     if (i == a.size() && j == b.size()) break; n     if (i < a.size() && (j == b.size() || a[i] < b[j])) { n       sum += a[i]; n       i++; n     } else { n       sum += b[j]; n       j++; n     } n   } n } n int main() { n   int i; n   cin >> N >> D >> F; n   for (i = 0; i < N; ++i) cin >> carry[i] >> f[i] >> d[i]; n   calc(); n   cout << anscnt <<     << ansfuel << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long a, b, w, x, c; n   cin >> a >> b >> w >> x >> c; n   long long delta = c - a; n   if (delta <= 0) { n     cout << 0 << endl; n   } else { n     double k = (1.0 * delta * x - b) / (w - x); n     long long t = ceil(delta + k); n     cout << t << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline const T MAX(T a, T b) { n   return ((a) > (b) ? (a) : (b)); n } n template <typename T> n inline const T MIN(T a, T b) { n   return ((a) > (b) ? (b) : (a)); n } n template <typename T> n inline const T ABS(T a) { n   return (((a) > 0) ? (a) : (-(a))); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int a, b, c; n   cin >> a >> b >> c; n   cout << (a * b) + (b * c) + (c * a) - (a + b + c) + 1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int size_n, size_m; n set<int> result; n vector<char> marks(0); n vector<string> mrk(0); n int main() { n   int ii, jj; n   string str; n   cin >> size_n >> size_m; n   marks.resize(size_m); n   mrk.resize(size_n); n   cin.ignore(); n   for (ii = 0; ii < size_m; ii++) marks[ii] =  n ; n   for (ii = 0; ii < size_n; ii++) { n     getline(cin, str); n     mrk[ii] = str; n     for (jj = 0; jj < size_m; jj++) { n       if ((marks[jj] ==  n ) || (str[jj] >= marks[jj])) marks[jj] = str[jj]; n     } n   } n   for (ii = 0; ii < size_m; ii++) { n     for (jj = 0; jj < size_n; jj++) { n       if (mrk[jj][ii] == marks[ii]) result.insert(jj); n     } n   } n   cout << result.size() << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Node { n   int key, priority; n   int add = 0; n   Node *left_child = nullptr, *right_child = nullptr; n   Node(const int key) : key(key), priority(rand()) {} n   void propagate() { n     key += add; n     if (left_child) left_child->add += add; n     if (right_child) right_child->add += add; n     add = 0; n   } n }; n int size(Node* const root) { n   return root ? 1 + size(root->left_child) + size(root->right_child) : 0; n } n pair<Node*, Node*> split(Node* const root, const int key) { n   if (!root) n     return {nullptr, nullptr}; n   else n     root->propagate(); n   Node *left, *right; n   if (key <= root->key) { n     right = root; n     tie(left, right->left_child) = split(root->left_child, key); n   } else { n     left = root; n     tie(left->right_child, right) = split(root->right_child, key); n   } n   return {left, right}; n } n Node* merge(Node* const left, Node* const right) { n   if (!left || !right) n     return left ? left : right; n   else n     left->propagate(), right->propagate(); n   if (left->priority < right->priority) { n     left->right_child = merge(left->right_child, right); n     return left; n   } else { n     right->left_child = merge(left, right->left_child); n     return right; n   } n } n void erase_min(Node*& root) { n   if (!root) n     return; n   else n     root->propagate(); n   if (!root->left_child) n     root = root->right_child; n   else n     erase_min(root->left_child); n } n int main() { n   ios::sync_with_stdio(false); n   Node* root = nullptr; n   int n; n   cin >> n; n   while (n--) { n     int l, r; n     cin >> l >> r; n     Node *t1, *t2, *t3; n     tie(t1, t2) = split(root, l); n     tie(t2, t3) = split(t2, r); n     if (t2) t2->add++; n     erase_min(t3); n     root = merge(merge(t1, new Node(l)), merge(t2, t3)); n   } n   cout << size(root) << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int mxN = 1e6 + 5; n vector<int> adj[mxN]; n int n, m, sz[mxN], dp[mxN], dpb[mxN], mn[mxN]; n bool bad[mxN]; n int ans = 1e9 + 7, V; n void dfs1(int v, int par = -1) { n   sz[v] += bad[v]; n   for (auto to : adj[v]) { n     if (par != to) { n       dfs1(to, v); n       sz[v] += sz[to]; n       if (sz[to]) { n         dpb[v] += dpb[to] + 2; n         mn[v] = min(mn[v], (dp[to] + 1) - (dpb[to] + 2)); n       } n     } n   } n   dp[v] = dpb[v] + mn[v]; n } n void dfs2(int v, int par = -1) { n   if (dp[v] < ans) n     ans = dp[v], V = v; n   else if (dp[v] == ans) n     V = min(V, v); n   multiset<pair<int, int> > mt; n   for (auto to : adj[v]) { n     if (sz[to]) mt.insert({min(0, (dp[to] + 1) - (dpb[to] + 2)), to}); n   } n   for (auto to : adj[v]) { n     if (to != par) { n       int dpB = dp[v], dpbB = dpb[v]; n       int mnB = mn[to]; n       int subdpB = dp[to], subdpbB = dpb[to]; n       dp[v] -= mt.begin()->first; n       if (sz[to]) mt.erase({min(0, (dp[to] + 1) - (dpb[to] + 2)), to}); n       if (!mt.empty()) dp[v] += mt.begin()->first; n       if (sz[to]) { n         dp[v] -= dpb[to] + 2; n         dpb[v] -= dpb[to] + 2; n       } n       sz[v] -= sz[to]; n       sz[to] += sz[v]; n       if (sz[v]) { n         dpb[to] += dpb[v] + 2; n         dp[to] += dpb[v] + 2; n         if (mn[to] > (dp[v] + 1) - (dpb[v] + 2)) { n           dp[to] -= mn[to]; n           mn[to] = (dp[v] + 1) - (dpb[v] + 2); n           dp[to] += mn[to]; n         } n       } n       dfs2(to, v); n       sz[to] -= sz[v]; n       sz[v] += sz[to]; n       dp[v] = dpB, dpb[v] = dpbB; n       mn[to] = mnB; n       dp[to] = subdpB, dpb[to] = subdpbB; n       if (sz[to]) mt.insert({min(0, (dp[to] + 1) - (dpb[to] + 2)), to}); n     } n   } n } n int main() { n   ios ::sync_with_stdio(false), cin.tie(nullptr); n   cin >> n >> m; n   for (int i = 1; i < n; ++i) { n     int u, v; n     cin >> u >> v; n     adj[u].push_back(v); n     adj[v].push_back(u); n   } n   for (int i = 1; i <= m; ++i) { n     int v; n     cin >> v; n     bad[v] = 1; n   } n   dfs1(1); n   dfs2(1); n   cout << V <<   n  << ans; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n using namespace std; n const int inf = 1000000010; n const int MAXN = 300010; n struct edge { n   int u, v, c, t, res; n } E[MAXN]; n int n, m, k, u, v, x, y, t, a, b, N; n int comp[MAXN * 2]; n bool mark[MAXN * 2]; n int del[MAXN * 2]; n int del2[MAXN * 2]; n unordered_map<int, int> deg[MAXN]; n unordered_map<int, vector<int>> GRAPH[MAXN]; n vector<int> GRAPH2[MAXN]; n vector<int> G[2 * MAXN]; n vector<int> GR[2 * MAXN]; n vector<int> topol; n void add_edge(int x, int y) { n   G[x].push_back(y); n   GR[y].push_back(x); n } n void add(int x, int y) { n   add_edge(x ^ 1, y); n   add_edge(y ^ 1, x); n } n void add2(int x, int y) { n   add_edge(x ^ 1, y); n   add_edge(y ^ 1, x); n   del[x ^ 1]++; n   del[y ^ 1]++; n   del2[y]++; n   del2[x]++; n } n void dfs1(int node) { n   mark[node] = 1; n   for (int v : G[node]) n     if (!mark[v]) dfs1(v); n   topol.push_back(node); n } n void dfs2(int node, int id) { n   comp[node] = id; n   for (int v : GR[node]) n     if (!comp[v]) dfs2(v, id); n } n bool check(int T) { n   for (int i = 0; i < 2 * MAXN; i++) { n     while (del[i]) { n       G[i].pop_back(); n       del[i]--; n     } n     while (del2[i]) { n       GR[i].pop_back(); n       del2[i]--; n     } n   } n   for (int i = 1; i <= m; i++) n     if (E[i].t > T) add2(2 * i ^ 1, 2 * i ^ 1); n   memset(comp, 0, sizeof(comp)); n   memset(mark, 0, sizeof(mark)); n   topol.clear(); n   for (int i = 2; i < 2 * MAXN; i++) n     if (!mark[i]) dfs1(i); n   reverse(topol.begin(), topol.end()); n   int id = 1; n   for (int v : topol) n     if (!comp[v]) dfs2(v, id++); n   for (int i = 2; i < 2 * N + 2; i++) n     if (comp[i] == comp[i ^ 1]) return 0; n   for (int i = 1; i <= m; i++) E[i].res = (comp[2 * i] > comp[2 * i + 1]); n   return 1; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m; n   for (int i = 1; i <= m; i++) { n     cin >> u >> v >> x >> y; n     E[i] = {u, v, x, y}; n     deg[u][x]++; n     deg[v][x]++; n     GRAPH[u][x].push_back(i); n     GRAPH[v][x].push_back(i); n     GRAPH2[u].push_back(i); n     GRAPH2[v].push_back(i); n   } n   for (int i = 1; i <= n; i++) n     for (pair<int, int> p : deg[i]) n       if (p.second > 2) return cout <<  No  <<   n , 0; n   for (int i = 1; i <= n; i++) n     for (auto it : GRAPH[i]) n       if (it.second.size() == 2) add(it.second[0] * 2, it.second[1] * 2); n   N = m; n   for (int i = 1; i <= n; i++) n     if (GRAPH2[i].size() >= 2) { n       k = GRAPH2[i].size(); n       for (int j = 0; j < k; j++) { n         int a = GRAPH2[i][j]; n         add(2 * a ^ 1, 2 * (++N)); n         if (j) add(2 * (N - 1) ^ 1, 2 * N); n         if (j) add(2 * a ^ 1, 2 * (N - 1) ^ 1); n       } n     } n   int dwn = -1, up = inf; n   while (up - dwn > 1) { n     int mid = (dwn + up) / 2; n     if (check(mid)) n       up = mid; n     else n       dwn = mid; n   } n   if (!check(up)) return cout <<  No  <<   n , 0; n   vector<int> out; n   for (int i = 1; i <= m; i++) n     if (E[i].res) out.push_back(i); n   cout <<  Yes n  << up <<     << out.size() <<   n ; n   for (int i : out) cout << i <<    ; n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 100005; n bool isprime[MAX]; n int a[MAX]; n int ans[100005]; n vector<int> factor[MAX]; n void get_prime() { n   memset(isprime, 1, sizeof(isprime)); n   for (int i = 2; i < MAX; i++) { n     if (isprime[i]) { n       factor[i].push_back(i); n       for (int j = 2 * i; j < MAX; j += i) { n         factor[j].push_back(i); n         isprime[j] = 0; n       } n     } n   } n } n int main() { n   get_prime(); n   int n; n   while (cin >> n) { n     memset(ans, 0, sizeof(ans)); n     for (int i = 0; i < n; i++) cin >> a[i]; n     for (int i = 0; i < n; i++) { n       int m = 0; n       for (int j = 0; j < factor[a[i]].size(); j++) n         m = max(ans[factor[a[i]][j]] + 1, m); n       for (int j = 0; j < factor[a[i]].size(); j++) ans[factor[a[i]][j]] = m; n     } n     int M = 1; n     for (int i = 0; i < 10000; i++) M = max(M, ans[i]); n     cout << M << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e6 + 10; n const int MOD = 1e9 + 7; n bool mark[N]; n vector<int> ps, corporator[N]; n int primeMul[N], primeCnt[N], nthPower[N], b[N], n, k; n int _sum(int, int); n int _mul(int, int); n int _pow(int, int); n void _push(int&, int); n int main() { n   cin >> n >> k; n   fill(primeMul, primeMul + k + 1, 1); n   for (int i = 1; i <= k; i++) { n     nthPower[i] = _pow(i, n); n     if (i > 1 && !mark[i]) { n       for (int j = i; j <= k; j += i) { n         mark[j] = true; n         primeMul[j] *= i; n         primeCnt[j]++; n       } n     } n     if (primeMul[i] == i) ps.push_back(i); n   } n   for (int i : ps) n     for (int j = i; j <= k; j += i) corporator[j].push_back(i); n   b[1] = nthPower[1]; n   int ans = b[1] ^ 1; n   for (int i = 2; i <= k; i++) { n     b[i] = b[i - 1]; n     for (int c : corporator[i]) { n       int v = _sum(MOD - nthPower[(i - 1) / c], nthPower[i / c]); n       _push(b[i], primeCnt[c] & 1 ? MOD - v : v); n     } n     _push(ans, b[i] ^ i); n   } n   cout << ans << endl; n } n int _sum(int a, int b) { return a + b - (a + b >= MOD ? MOD : 0); } n int _mul(int a, int b) { return 1LL * a * b % MOD; } n int _pow(int a, int b) { n   if (!b) return 1; n   int res = _pow(a, b >> 1); n   res = _mul(res, res); n   if (b & 1) res = _mul(res, a); n   return res; n } n void _push(int& a, int b) { a = _sum(a, b); } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f, maxn = 100007, mod = 1e9 + 7; n const long long linf = 0x3f3f3f3f3f3f3f3fLL; n const long long P = 19260817; n string str; n string s; n bool flag; n int main() { n   cin >> str; n   for (int i = 1; i <= 5; i++) { n     cin >> s; n     if (s[0] == str[0] || s[1] == str[1]) flag = true; n   } n   if (flag) { n     printf( YES ); n   } else { n     printf( NO ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1005; n int a[maxn]; n int ans[maxn << 1]; n int vis1[maxn], vis[maxn]; n int main() { n   int T; n   scanf( %d , &T); n   while (T--) { n     int n; n     scanf( %d , &n); n     int res = n; n     int p = 0; n     int mex; n     for (int i = 1; i <= n; i++) { n       scanf( %d , &a[i]); n       vis1[i] = 0; n     } n     while (res > 0) { n       for (int i = 0; i <= n; i++) vis[i] = 0; n       for (int i = 1; i <= n; i++) vis[a[i]] = 1; n       for (int i = 0;; i++) { n         if (!vis[i]) { n           mex = i; n           break; n         } n       } n       if (mex == 0) { n         for (int i = 1; i <= n; i++) { n           if (!vis1[i]) { n             a[i] = mex; n             ans[++p] = i; n             break; n           } n         } n       } else { n         ans[++p] = mex; n         a[mex] = mex; n         vis1[mex] = 1; n         res--; n       } n     } n     printf( %d n , p); n     for (int i = 1; i <= p; i++) printf( %d  , ans[i]); n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const int maxn = 1e3 + 10, maxm = 1e6 + 5, maxc = 26; n long long sum = 0, mi = 0, ma = 0; n int main() { n   int n, w, num; n   scanf( %d%d , &n, &w); n   for (int i = 0; i < n; i++) { n     scanf( %d , &num); n     sum += num; n     mi = min(mi, sum); n     ma = max(ma, sum); n   } n   printf( %I64d n , w - ma + mi + 1 <= 0 ? 0 : w - ma + mi + 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma comment(linker,  /STACK:36777216 ) n template <class T> n void chmax(T& l, const T r) { n   l = max(l, r); n } n template <class T> n void chmin(T& l, const T r) { n   l = min(l, r); n } n void reader(int& x) { n   int k, m = 0; n   x = 0; n   for (;;) { n     (k) = getchar_unlocked(); n     if (k ==  - ) { n       m = 1; n       break; n     } n     if ( 0  <= k && k <=  9 ) { n       x = k -  0 ; n       break; n     } n   } n   for (;;) { n     (k) = getchar_unlocked(); n     if (k <  0  || k >  9 ) break; n     x = x * 10 + k -  0 ; n   } n   if (m) x = -x; n } n void reader(long long& x) { n   int k, m = 0; n   x = 0; n   for (;;) { n     (k) = getchar_unlocked(); n     if (k ==  - ) { n       m = 1; n       break; n     } n     if ( 0  <= k && k <=  9 ) { n       x = k -  0 ; n       break; n     } n   } n   for (;;) { n     (k) = getchar_unlocked(); n     if (k <  0  || k >  9 ) break; n     x = x * 10 + k -  0 ; n   } n   if (m) x = -x; n } n int reader(char c[]) { n   int i, s = 0; n   for (;;) { n     (i) = getchar_unlocked(); n     if (i !=     && i !=   n  && i !=   r  && i !=   t  && i != EOF) break; n   } n   c[s++] = i; n   for (;;) { n     (i) = getchar_unlocked(); n     if (i ==     || i ==   n  || i ==   r  || i ==   t  || i == EOF) break; n     c[s++] = i; n   } n   c[s] =   0 ; n   return s; n } n template <class T, class S> n void reader(T& x, S& y) { n   reader(x); n   reader(y); n } n template <class T, class S, class U> n void reader(T& x, S& y, U& z) { n   reader(x); n   reader(y); n   reader(z); n } n template <class T, class S, class U, class V> n void reader(T& x, S& y, U& z, V& w) { n   reader(x); n   reader(y); n   reader(z); n   reader(w); n } n void writer(int x, char c) { n   int s = 0, m = 0; n   char f[10]; n   if (x < 0) m = 1, x = -x; n   while (x) f[s++] = x % 10, x /= 10; n   if (!s) f[s++] = 0; n   if (m) putchar_unlocked( - ); n   while (s--) putchar_unlocked(f[s] +  0 ); n   putchar_unlocked(c); n } n void writer(long long x, char c) { n   int s = 0, m = 0; n   char f[20]; n   if (x < 0) m = 1, x = -x; n   while (x) f[s++] = x % 10, x /= 10; n   if (!s) f[s++] = 0; n   if (m) putchar_unlocked( - ); n   while (s--) putchar_unlocked(f[s] +  0 ); n   putchar_unlocked(c); n } n void writer(const char c[]) { n   int i; n   for (i = 0; c[i] !=   0 ; i++) putchar_unlocked(c[i]); n } n void writer(const char x[], char c) { n   int i; n   for (i = 0; x[i] !=   0 ; i++) putchar_unlocked(x[i]); n   putchar_unlocked(c); n } n template <class T> n void writerLn(T x) { n   writer(x,   n ); n } n template <class T, class S> n void writerLn(T x, S y) { n   writer(x,    ); n   writer(y,   n ); n } n template <class T, class S, class U> n void writerLn(T x, S y, U z) { n   writer(x,    ); n   writer(y,    ); n   writer(z,   n ); n } n template <class T> n void writerArr(T x[], int n) { n   if (!n) { n     putchar_unlocked(  n ); n     return; n   } n   for (int i = 0; i < (n - 1); i++) writer(x[i],    ); n   writer(x[n - 1],   n ); n } n template <class T> n T gcd(T a, T b) { n   return b ? gcd(b, a % b) : a; n } n template <class T> n T lcm(T a, T b) { n   return a / gcd(a, b) * b; n } n template <class T> n T extgcd(T a, T b, T& x, T& y) { n   for (T u = y = 1, v = x = 0; a;) { n     T q = b / a; n     swap(x -= q * u, u); n     swap(y -= q * v, v); n     swap(b -= q * a, a); n   } n   return b; n } n template <class T> n T mod_inv(T a, T m) { n   T x, y; n   extgcd(a, m, x, y); n   return (m + x % m) % m; n } n template <class T> n T CRT(T r1, T m1, T r2, T m2) { n   T a1, a2; n   extgcd(m1, m2, a1, a2); n   T ret = (m1 * a1 * r2 + m2 * a2 * r1) % (m1 * m2); n   return ret < 0 ? ret + m1 * m2 : ret; n } n int memo[100][100]; n int _k; n int dfs(int a, int b, int k) { n   if (memo[a][b] != -1) return memo[a][b]; n   if (k == 0) { n     if (_k % 2 == 0) n       return b % 2 == 1; n     else n       return b % 2 == 0; n   } n   int val = 0; n   if (a != 0) val = max(val, dfs(a - 1, b, k - 1) ^ 1); n   if (b != 0) val = max(val, dfs(a, b - 1, k - 1) ^ 1); n   return memo[a][b] = val; n } n int solve_dfs(int e, int o, int m) { n   memset(memo, -1, sizeof(memo)); n   _k = m; n   int ans = dfs(e, o, m); n   return ans; n } n int solve(int e, int o, int m) { n   if (m == 0) return o % 2; n   if (o == 0) return 0; n   if (e + o + 1 == m) return 0; n   if (m > o * 2) return 0; n   if (m < e * 2) n     return m % 2; n   else n     return (m + o + e) % 2; n } n void test() { n   for (int e = 0; e < (10); e++) { n     for (int o = 0; o < (10); o++) { n       printf( %d %d :  , e, o); n       for (int k = 0; k < (e + o + 1); k++) { n         int x = solve_dfs(e, o, k); n         printf( %d  , x); n         int y = solve(e, o, k); n         if (x != y) { n           cout <<  ? ; n         } n       } n       puts(  ); n     } n   } n } n int main() { n   int n, m; n   cin >> n >> m; n   int e = 0, o = 0; n   for (int i = 0; i < (n); i++) { n     int x; n     reader(x); n     if (x % 2 == 0) n       e++; n     else n       o++; n   } n   m = n - m; n   int ans = solve(e, o, m); n   if (ans) n     puts( Stannis ); n   else n     puts( Daenerys ); n   return 0; n } n


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


#include <bits/stdc++.h> n using std::cin; n using std::cout; n const int N = 1e5 + 1; n int r, a, b, m, las[N]; n int main() { n   cin >> a >> b >> m >> r; n   memset(las, -1, sizeof las); n   las[r] = 0; n   for (int i = 1;; ++i) { n     r = (a * r + b) % m; n     if (~las[r]) { n       cout << i - las[r]; n       return 0; n     } n     las[r] = i; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n string s[80000]; n unordered_map<string, int> substrings; n int main(void) { n   cin >> n; n   for (int i = 0; i < n; ++i) { n     cin >> s[i]; n     unordered_map<string, bool> used; n     for (int j = 1; j <= s[i].size(); ++j) { n       for (int k = 0; k + j <= s[i].size(); ++k) { n         string substring = s[i].substr(k, j); n         if (used.find(substring) == used.end()) { n           used[substring] = true; n           if (substrings.find(substring) == substrings.end()) { n             substrings[substring] = 1; n           } else { n             substrings[substring] += 1; n           } n         } n       } n     } n   } n   for (int i = 0; i < n; ++i) { n     for (int j = 1; j <= s[i].size(); ++j) { n       for (int k = 0; k + j <= s[i].size(); ++k) { n         if (substrings[s[i].substr(k, j)] == 1) { n           cout << s[i].substr(k, j) << endl; n           goto van; n         } n       } n     } n   van:; n   } n   return 0; n } n