#include <bits/stdc++.h> n using namespace std; n const int size = 50 + 10; n int n, k, aim[size], num[size], ans = -1; n int main() { n   cin >> n >> k; n   if (k > n) { n     cout << -1 << endl; n     return 0; n   } n   for (int i = 1; i <= n; i++) cin >> aim[i]; n   sort(aim + 1, aim + n + 1); n   num[1] = 0; n   for (int i = 2; i <= n; i++) n     if (aim[i] > aim[i - 1]) n       num[i] = i - 1; n     else n       num[i] = num[i - 1]; n   for (int i = 1; i <= n; i++) n     if (aim[i] != aim[i + 1] && n - num[i] == k) ans = aim[i]; n   if (ans < 0) { n     cout << ans << endl; n     return 0; n   } n   cout << ans <<     << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int32_t main() { n   ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   long long n, m; n   long long mn, mx; n   cin >> n >> m; n   mn = n; n   mn = max(0ll, n - 2 * m); n   cout << mn <<    ; n   long long cnt = 0; n   while (m > 0) { n     m -= cnt; n     cnt++; n   } n   cout << max(0ll, n - cnt) <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ld = long double; n void solve() { n   string s; n   cin >> s; n   vector<vector<ll>> cnt(2, vector<ll>(2, 0)); n   ll e = 0, o = 0; n   for (int i = 0; i < s.size(); i++) { n     o++; n     if (i % 2 == 0) { n       o += cnt[s[i] -  a ][0]; n       e += cnt[s[i] -  a ][1]; n       cnt[s[i] -  a ][0]++; n     } else { n       e += cnt[s[i] -  a ][0]; n       o += cnt[s[i] -  a ][1]; n       cnt[s[i] -  a ][1]++; n     } n   } n   cout << e <<     << o <<   n ; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   int t = 1; n   for (int i = 1; i <= t; i++) { n     solve(); n   } n   return EXIT_SUCCESS; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   for (int tt = 0; tt < int(t); tt++) { n     int a, b, c, r; n     cin >> a >> b >> c >> r; n     int L = max(min(a, b), c - r); n     int R = min(max(a, b), c + r); n     cout << max(a, b) - min(a, b) - max(0, R - L) << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e3 + 5; n int a[maxn][maxn]; n int col[maxn]; n int deg[maxn]; n bool vis[maxn]; n int n; n void recurse() { n   int piv = -1; n   for (int i = 1; i <= n; i++) { n     if (vis[i]) continue; n     if (deg[i] % 2 == 1) { n       piv = i; n       break; n     } n   } n   if (piv == -1) { n     for (int i = 1; i <= n; i++) { n       if (vis[i]) continue; n       col[i] = 0; n     } n     return; n   } n   vis[piv] = true; n   vector<int> adj; n   for (int i = 1; i <= n; i++) { n     if (vis[i]) continue; n     if (a[piv][i] == 1) { n       a[piv][i] = a[i][piv] = 0; n       adj.push_back(i); n       deg[i] -= 1; n     } n   } n   for (auto i : adj) { n     for (auto j : adj) { n       if (i == j) continue; n       if (a[i][j] == 1) { n         a[i][j] = 0; n         deg[i] -= 1; n       } else { n         a[i][j] = 1; n         deg[i] += 1; n       } n     } n   } n   recurse(); n   int cnt[] = {0, 0}; n   for (int i : adj) { n     cnt[col[i]] += 1; n   } n   col[piv] = cnt[0] % 2 == 0 ? 0 : 1; n } n void solve() { n   int m; n   cin >> n >> m; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) a[i][j] = 0; n   memset(deg, 0, sizeof deg); n   memset(vis, false, sizeof vis); n   for (int i = 1; i <= m; i++) { n     int p, q; n     cin >> p >> q; n     a[p][q] = a[q][p] = 1; n     deg[p] += 1; n     deg[q] += 1; n   } n   recurse(); n   cout << *max_element(col + 1, col + n + 1) + 1 << endl; n   for (int i = 1; i <= n; i++) cout << 1 + col[i] <<    ; n   cout << endl; n } n int main() { n   int t; n   cin >> t; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int q; n   cin >> q; n   while (q--) { n     int n, h; n     cin >> h >> n; n     int a[n]; n     for (int i = 0; i < n; ++i) cin >> a[i]; n     int cnt = 1; n     vector<int> v; n     for (int i = 1; i < n; ++i) { n       if (a[i] == a[i - 1] - 1) { n         ++cnt; n       } else { n         v.push_back(cnt); n         cnt = 1; n       } n     } n     v.push_back(cnt); n     int sm = 0; n     if (v.size() == 1) { n       if (a[n - 1] < 2) n         sm = 0; n       else { n         if (v[0] % 2 == 0) n           sm = 1; n         else n           sm = 0; n       } n     } else { n       for (int i = 0; i < v.size(); ++i) { n         if (i == 0) { n           if (v[i] % 2 == 0) ++sm; n         } else if (i == v.size() - 1) { n           ++v[i]; n           if (a[n - 1] >= 2 && v[i] % 2 == 0) ++sm; n         } else { n           ++v[i]; n           if (v[i] % 2 == 0) ++sm; n         } n       } n     } n     cout << sm << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n string a, ans; n int b; n string c; n int main() { n   while (cin >> a >> b) { n     for (int i = 0; i < a.size(); i++) n       if (a[i] >=  A  && a[i] <=  Z ) a[i] = a[i] -  A  +  a ; n     for (int i = 0; i < a.size(); i++) { n       if ((int)a[i] < b + 97) { n         a[i] = a[i] -  a  +  A ; n       } n     } n     cout << a << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long double eps = 1e-9; n const int inf = (1 << 30) - 1; n const long long inf64 = ((long long)1 << 62) - 1; n const long double pi = 3.1415926535897932384626433832795; n template <class T> n T sqr(T x) { n   return x * x; n } n template <class T> n T abs(T x) { n   return x < 0 ? -x : x; n } n const int MAXN = 310000; n int a[MAXN], ind[MAXN], ind_cut[MAXN]; n long long sum[MAXN]; n bool comp(int ind1, int ind2) { n   return ((a[ind1] < a[ind2]) || (a[ind1] == a[ind2] && ind1 < ind2)); n } n int main() { n   int n; n   cin >> n; n   sum[0] = 0; n   for (int i = 0; i < n; ++i) { n     scanf( %d , &a[i]); n     sum[i + 1] = (a[i] > 0 ? sum[i] + a[i] : sum[i]); n     ind[i] = i; n   } n   sort(ind, ind + n, comp); n   int i = 0; n   int ind_l, ind_r; n   long long max_sum = -2 * inf; n   ind[n] = n; n   a[n] = inf; n   while (i < n) { n     int j = i; n     while (a[ind[j + 1]] == a[ind[i]]) j++; n     long long temp = sum[ind[j]] - sum[ind[i] + 1] + 2 * a[ind[i]]; n     if (j > i && temp > max_sum) { n       max_sum = temp; n       ind_l = ind[i]; n       ind_r = ind[j]; n     } n     i = j + 1; n   } n   int num = 0; n   for (int i = 0; i < ind_l; ++i) { n     ind_cut[num] = i + 1; n     num++; n   } n   for (int i = ind_l + 1; i < ind_r; ++i) { n     if (a[i] < 0) { n       ind_cut[num] = i + 1; n       num++; n     } n   } n   for (int i = ind_r + 1; i < n; ++i) { n     ind_cut[num] = i + 1; n     num++; n   } n   cout << max_sum <<     << num << endl; n   for (int i = 0; i < num; ++i) { n     cout << ind_cut[i] <<    ; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[1005]; n long long int p[1005], f[1005], d; n long long int power(long long int a, long long int b) { n   if (b == 0) return 1; n   if (b == 1) return a % 998244353; n   long long int c = power(a, b / 2); n   if (b & 1) n     return (((c * c) % 998244353) * a) % 998244353; n   else n     return (c * c) % 998244353; n } n long long int ret(int a, int b) { n   long long int c = f[a]; n   c *= power(f[b], 998244353 - 2); n   c %= 998244353; n   c *= power(f[a - b], 998244353 - 2); n   return c % 998244353; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   f[0] = 1; n   for (int i = 1; i < 1005; i++) { n     f[i] = i * f[i - 1]; n     f[i] %= 998244353; n   } n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   p[n + 1] = 1; n   for (int i = n; i > 0; i--) { n     p[i] = p[i + 1]; n     for (int j = i + a[i]; j <= n && a[i] > 0; j++) { n       d = ret(j - i - 1, a[i] - 1); n       p[i] += d * p[j + 1]; n       p[i] %= 998244353; n     } n   } n   cout << ((p[1] + 998244353 - 1) % 998244353); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int c, hr, hb, wr, wb; n   cin >> c >> hr >> hb >> wr >> wb; n   long long int ret = 0, ret1 = 0; n   int cnt = 0; n   for (long long int i = 0; i <= (c / wr); i++) { n     long long int r = i * wr; n     long long int l = c - r; n     long long int b = l / wb; n     ret1 = i * hr + b * hb; n     ret = max(ret, ret1); n     cnt++; n     if (cnt > 1e7) break; n   } n   cnt = 0; n   for (long long int i = 0; i <= (c / wb); i++) { n     long long int r = i * wb; n     long long int l = c - r; n     long long int b = l / wr; n     ret1 = i * hb + b * hr; n     ret = max(ret, ret1); n     cnt++; n     if (cnt > 1e7) break; n   } n   cout << ret << endl; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n; n   while (scanf( %d , &n) != EOF) { n     printf( %d n , n * 3 / 2); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n set<int> st; n int n; n long long w[4] = {1, 5, 10, 50}; n bool vis[1280][105000]; n void doit(int i, int s) { n   if (i == n) { n     st.insert(s); n     return; n   } n   if (vis[i][s]) return; n   vis[i][s] = 1; n   for (int j = 0; j < 4; j++) doit(i + 1, w[j] + s); n } n long long ans[12] = {0, 4, 10, 20, 35, 56, 83, 116, 155, 198, 244, 292}; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   if (n <= 11) n     cout << ans[n] << endl; n   else n     cout << ans[11] + (n - 11) * 49LL << endl; n   ; n } n


#include <bits/stdc++.h> n using namespace std; n long long mi, ma, n, d, k, l, r, mid, a[500007], b[500007], cnt[500007]; n inline long long read() { n   long long s = 0, w = 1; n   char c = getchar(); n   for (; !isdigit(c); c = getchar()) n     if (c ==  - ) w = -1; n   for (; isdigit(c); c = getchar()) s = (s << 3) + (s << 1) + (c ^ 48); n   return (s * w); n } n inline bool check() { n   long long num = 0, sum = 0; n   for (int i = 1; i <= n; i++) { n     long long delta = mid - cnt[i] - b[i] - sum; n     if (delta <= 0) { n       sum = sum + b[i]; n       continue; n     } n     num = num + delta; n     if (num > k) { n       for (int j = min(i + 2 * d, n); j; j--) b[j] = 0; n       return false; n     } n     b[i] = b[i] + delta; n     if (i + 2 * d < n) b[i + 2 * d + 1] = b[i + 2 * d + 1] - delta; n     sum = sum + b[i]; n   } n   for (int i = 1; i <= n; i++) b[i] = 0; n   return true; n } n int main() { n   n = read(), d = read(), k = read(); n   mi = LLONG_MAX; n   for (int i = 1; i <= n; i++) { n     long long x = read(); n     a[max(1LL, i - d)] = a[max(1LL, i - d)] + x; n     if (i + d < n) a[i + d + 1] = a[i + d + 1] - x; n   } n   for (int i = 1; i <= n; i++) n     cnt[i] = cnt[i - 1] + a[i], ma = max(ma, cnt[i]), mi = min(mi, cnt[i]); n   l = mi, r = ma + k; n   while (l < r) { n     mid = (l + r + 1) >> 1; n     if (check()) n       l = mid; n     else n       r = mid - 1; n   } n   printf( %lld n , l); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[100005]; n long long dp[100005]; n int main() { n   int n; n   memset(a, 0, sizeof(a)); n   memset(dp, 0, sizeof(dp)); n   cin >> n; n   int x; n   for (int i = 0; i < n; i++) { n     cin >> x; n     a[x]++; n   } n   dp[1] = a[1]; n   dp[0] = 0LL; n   for (int i = 2; i <= 100002; i++) { n     dp[i] = max(dp[i - 1], dp[i - 2] + i * a[i]); n   } n   cout << dp[100002] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int solve(bool can_be_equal, const string &sa, vector<int> num_digits) { n   int received = 0; n   int given = 0; n   int offset = can_be_equal ? 0 : 1; n   for (char c : sa) { n     int d = c -  0 ; n     bool found = false; n     for (int search = d + offset; search < 10; ++search) { n       if (num_digits[search] != 0) { n         num_digits[search]--; n         found = true; n         given++; n         break; n       } n     } n     if (!found) { n       received++; n       for (int search = 0; search < d + offset; ++search) { n         if (num_digits[search] != 0) { n           num_digits[search]--; n           found = true; n           break; n         } n       } n     } n   } n   return can_be_equal ? received : given; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   int n; n   cin >> n; n   string sa, sb; n   cin >> sa; n   cin >> sb; n   vector<int> num_digits(10); n   for (char c : sb) { n     num_digits[c -  0 ]++; n   } n   cout << solve(true, sa, num_digits) << endl; n   cout << solve(false, sa, num_digits) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long bigmod(long long a, int p, long long int m) { n   if (p == 0) return 1; n   if (p & 1) { n     return ((a % m) * (bigmod(a, p - 1, m))) % m; n   } else { n     long long tmp = bigmod(a, p / 2, m); n     return (tmp * tmp) % m; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int chu; n   cin >> chu; n   while (chu--) { n     long long int n; n     cin >> n; n     long long int x; n     cin >> x; n     long long int a[n]; n     for (long long int i = 0; i < n; i++) cin >> a[i]; n     sort(a, a + n); n     reverse(a, a + n); n     long long int ans = 0; n     long long int i = 0; n     long long int y = 1; n     for (i = 0; i < n; i++) { n       if (a[i] * y >= x) n         ans++, y = 1; n       else n         y++; n     } n     cout << ans; n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n using namespace std; n int main() { n   int k; n   string s, st; n   cin >> k >> s; n   map<char, int> m; n   set<char> s1; n   if (s.length() % k) { n     cout << -1; n     return 0; n   } n   for (auto i = 0; i < s.length(); i++) { n     if (m.find(s[i]) == m.end()) n       m.insert({s[i], 1}); n     else n       m[s[i]]++; n     s1.insert(s[i]); n   } n   for (auto i = m.begin(); i != m.end(); ++i) { n     if (i->second % k) { n       cout << -1; n       return 0; n     } n   } n   for (auto i = s1.begin(); i != s1.end(); ++i) { n     while (m[*i] != 0) { n       st += *i; n       m[*i] -= k; n     } n   } n   for (auto i = 0; i < k; i++) cout << st; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename _T> n inline void read(_T &f) { n   f = 0; n   _T fu = 1; n   char c = getchar(); n   while (c <  0  || c >  9 ) { n     if (c ==  - ) { n       fu = -1; n     } n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     f = (f << 3) + (f << 1) + (c & 15); n     c = getchar(); n   } n   f *= fu; n } n template <typename T> n void print(T x) { n   if (x < 0) putchar( - ), x = -x; n   if (x < 10) n     putchar(x + 48); n   else n     print(x / 10), putchar(x % 10 + 48); n } n template <typename T> n void print(T x, char t) { n   print(x); n   putchar(t); n } n const int N = 105, md = 1e9 + 7; n inline int add(int x, int y) { n   x += y; n   if (x >= md) x -= md; n   return x; n } n inline void addx(int &x, int y) { n   x += y; n   if (x >= md) x -= md; n } n inline int sub(int x, int y) { n   x -= y; n   if (x < 0) x += md; n   return x; n } n inline void subx(int &x, int y) { n   x -= y; n   if (x < 0) x += md; n } n inline int mul(int x, int y) { return 1ll * x * y % md; } n inline int fpow(int x, int y) { n   int ans = 1; n   while (y) { n     if (y & 1) ans = mul(ans, x); n     y >>= 1; n     x = mul(x, x); n   } n   return ans; n } n vector<int> adj[N]; n int f[N][N][N], ans[N], siz[N], c[N][N], tmp[N][N]; n int n; n void conv(int a[N][N], int b[N][N], int &lena, int lenb) { n   for (int i = 0; i <= lena + lenb; i++) { n     for (int j = 0; j <= lena + lenb; j++) tmp[i][j] = 0; n   } n   for (int i = 0; i <= lena; i++) { n     for (int j = 0; j <= lena; j++) { n       if (a[i][j] == 0) continue; n       for (int x = 0; x <= lenb; x++) { n         for (int y = 0; y <= lenb; y++) { n           if (b[x][y] == 0) continue; n           addx(tmp[i + x + 1][j], mul(mul(a[i][j], b[x][y]), y)); n           addx(tmp[i + x][j + y], mul(a[i][j], b[x][y])); n         } n       } n     } n   } n   lena += lenb; n   for (int i = 0; i <= lena; i++) { n     for (int j = 0; j <= lena; j++) a[i][j] = tmp[i][j]; n   } n } n void dfs1(int u, int fa) { n   siz[u] = 1; n   f[u][0][1] = 1; n   for (auto v : adj[u]) { n     if (v == fa) continue; n     dfs1(v, u); n     conv(f[u], f[v], siz[u], siz[v]); n   } n } n int main() { n   read(n); n   c[0][0] = 1; n   for (int i = 1; i <= n; i++) { n     c[i][0] = 1; n     for (int j = 1; j <= i; j++) c[i][j] = add(c[i - 1][j - 1], c[i - 1][j]); n   } n   for (int i = 1; i < n; i++) { n     int u, v; n     read(u); n     read(v); n     adj[u].push_back(v); n     adj[v].push_back(u); n   } n   dfs1(1, 0); n   for (int i = 0; i <= n; i++) { n     for (int j = 0; j <= n; j++) { n       if (f[1][i][j] == 0) continue; n       addx(ans[n - i - 1], mul(f[1][i][j], j)); n     } n   } n   for (int i = n - 1; i >= 0; i--) { n     if (i != n - 1) n       ans[i] = mul(ans[i], fpow(n, n - i - 2)); n     else n       ans[i] = 1; n     for (int j = i + 1; j <= n - 1; j++) subx(ans[i], mul(ans[j], c[j][i])); n   } n   for (int i = 0; i <= n - 1; i++) print(ans[i],    ); n   putchar(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve(int test_number); n int main() { n   cout.setf(ios::fixed); n   cout.precision(9); n   cerr.setf(ios::fixed); n   cerr.precision(3); n   int n = 1; n   for (int i = 0; i < n; i++) { n     solve(i + 1); n   } n   return 0; n } n const int MAXN = 200100; n const int BUBEN = 800; n int n; n int a[MAXN]; n int d[MAXN]; n int nd; n bool used[BUBEN][MAXN]; n int sum[BUBEN]; n vector<int> q[MAXN]; n int st[MAXN]; n inline int gcd(int a, int b) { n   if (b == 0) n     return a; n   else n     return gcd(b, a % b); n } n void solve(int test_number) { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cin >> n; n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 1; i * i <= n; i++) { n     if (n % i == 0) { n       d[nd++] = i; n       int j = n / i; n       if (i != j && j != n) d[nd++] = j; n     } n   } n   sort(d, d + nd); n   for (int j = 0; j < nd; j++) { n     int step = d[j]; n     for (int from = 0; from < step; from++) { n       int mx = a[from]; n       for (int i = from; i < n; i += step) { n         mx = max(mx, a[i]); n       } n       for (int i = from; i < n; i += step) { n         if (mx == a[i]) { n           used[j][i] = true; n         } n       } n     } n     int last = -1; n     int pref = 0; n     for (int i = 0; i < n; i++) n       if (used[j][i]) { n         if (last == -1) last = i; n       } else if (last != -1) { n         if (last == 0) { n           pref = i - last; n         } else { n           q[j].push_back(i - last); n           sum[j] += i - last; n         } n         last = -1; n       } n     if (last != -1) { n       pref += n - last; n     } n     q[j].push_back(pref); n     sum[j] += pref; n     sort(q[j].begin(), q[j].end()); n   } n   long long res = 0; n   for (int len = 1; len < n; len++) { n     int step = gcd(len, n); n     long long j = lower_bound(d, d + nd, step) - d; n     while ((int)q[j].size() != st[j] && q[j][st[j]] < len) { n       sum[j] -= q[j][st[j]]; n       st[j]++; n     } n     if (sum[j] == n) { n       res += (long long)n; n     } else { n       res += (long long)sum[j] - n              (long long)(len - 1) * (long long)(q[j].size() - st[j]); n     } n   } n   cout << res << endl; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rng32(chrono::steady_clock::now().time_since_epoch().count()); n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int t; n   cin >> t; n   while (t--) { n     int a, b, c; n     cin >> a >> b >> c; n     int ans = -1; n     for (int x = 0; x < a + 1; x++) { n       for (int y = 0; y <= c; y += 2) { n         if (y / 2 + 2 * x <= b) { n           ans = max(ans, 3 * x + y + y / 2); n         } n       } n     } n     cout << ans <<   n ; n   } n } n


#include <bits/stdc++.h> n using ll = long long; n using ld = long double; n using namespace std; n void debugi(vector<int>& vec) { n   int n = (int)vec.size(); n   for (int i = 0; i < n; i++) { n     cout << vec[i] <<    ; n   } n   cout << endl; n } n void debugd(vector<long double>& vec) { n   int n = (int)vec.size(); n   for (int i = 0; i < n; i++) { n     cout << vec[i] <<    ; n   } n   cout << endl; n } n void debugl(vector<ll>& vec) { n   int n = (int)vec.size(); n   for (int i = 0; i < n; i++) { n     cout << vec[i] <<    ; n   } n   cout << endl; n } n void debugmap(map<int, int>& mp) { n   for (auto x : mp) { n     cout << x.first <<     << x.second <<   ,  ; n   } n   cout << endl; n } n void debugset(set<int>& st) { n   for (auto x : st) { n     cout << x <<    ; n   } n   cout << endl; n } n void debugparri(vector<pair<int, int>>& arr) { n   for (int i = 0; i < (int)arr.size(); i++) { n     cout <<  [  << arr[i].first <<   :   << arr[i].second <<  ]  n          <<    ; n   } n   cout << endl; n } n void debugpi(pair<int, int>& pi) { n   cout << pi.first <<     << pi.second << endl; n } n void debugq(queue<int> q) { n   while (!q.empty()) { n     cout << q.front() <<    ; n     q.pop(); n   } n   cout << endl; n } n bool check(int n, int i, int j) { n   if ((0 <= i & i < n) && (0 <= j & j < n)) { n     return true; n   } n   return false; n } n ll MOD = 1e9 + 7; n ll powab(ll a, ll b, ll mod) { n   if (b == 0) { n     return 1; n   } n   ll z = powab(a, b / 2, mod); n   ll res = (z * z * 1LL) % mod; n   if (b & 1) { n     res = (res * 1LL * a) % mod; n   } n   return res; n } n ll modinv(ll a, ll mod) { n   ll ans = powab(a, mod - 2, mod); n   return ans; n } n ll gcd(ll a, ll b) { n   if (b == 0) { n     return a; n   } n   return gcd(b, a % b); n } n void upd(int k, int x, vector<ll>& tree, int n) { n   if (k == 0) { n     return; n   } n   while (k <= n) { n     tree[k] += x; n     k += (k & -k); n   } n } n ll sum(int k, vector<ll>& tree, int n) { n   ll s = 0; n   while (k >= 1) { n     s += tree[k]; n     k -= (k & -k); n   } n   return s; n } n void solve(int tt, int t) { n   int n, m, k, rn; n   cin >> n >> m >> k; n   rn = n; n   int lim = 5050; n   vector<int> dp(lim), lst(lim, -1); n   vector<int> arr; n   if (m == 1) { n     if (k != 0) { n       cout <<  NO  << endl; n       return; n     } n     if (n % 2 != 0) { n       cout <<  NO   << endl; n       return; n     } n     vector<vector<char>> grid(n, vector<char>(m)); n     int ct = 1; n     for (int i = 0; i < n; i++) { n       if (i % 2 == 0 && ct) { n         grid[i][0] =  a ; n         grid[i + 1][0] =  a ; n         ct ^= 1; n       } else if (i % 2 == 0 && !ct) { n         grid[i][0] =  b ; n         grid[i + 1][0] =  b ; n         ct ^= 1; n       } n     } n     cout <<  YES  << endl; n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < m; j++) { n         cout << grid[i][j]; n       } n       cout << endl; n     } n     return; n   } n   vector<vector<int>> grid(n, vector<int>(m, -1)); n   dp[0] = 1; n   if (n % 2 != 0) { n     if (k < m / 2) { n       cout <<  NO  << endl; n       return; n     } n     k -= m / 2; n     int ct = 1; n     for (int i = 0; i < m; i += 2) { n       if (ct) { n         grid[n - 1][i] = 1; n         grid[n - 1][i + 1] = 1; n         ct ^= 1; n       } else { n         grid[n - 1][i] = 0; n         grid[n - 1][i + 1] = 0; n         ct ^= 1; n       } n     } n     n -= 1; n   } n   if (n % 2 == 0) { n     for (int i = 2; i <= n; i += 2) { n       arr.push_back(i); n     } n   } else { n     for (int i = 1; i <= n; i += 2) { n       arr.push_back(i); n     } n   } n   reverse(arr.begin(), arr.end()); n   queue<pair<int, int>> q; n   q.push({0, 0}); n   vector<int> vis(lim); n   vis[0] = 1; n   while (!q.empty()) { n     int cur = q.front().first, dis = q.front().second; n     q.pop(); n     for (auto x : arr) { n       if (x + cur < lim) { n         if (vis[x + cur] == 0) { n           vis[x + cur] = 1; n           q.push({x + cur, dis + 1}); n           lst[x + cur] = cur; n           dp[x + cur] = dis + 1; n         } n       } n     } n   } n   n = rn; n   if (vis[k] == 1 && dp[k] <= m / 2) { n     vector<int> nums; n     int cur = k; n     while (cur != 0) { n       int prev = lst[cur]; n       nums.push_back(cur - prev); n       cur = prev; n     } n     int z = nums.size(), idx = 0; n     vector<int> curcol(m); n     int cl = 2; n     for (int i = 0; i < m; i++) { n       if (i % 2 == 0) { n         curcol[i] = 2; n       } else n         curcol[i] = 3; n     } n     for (int i = 0; i < m; i += 2) { n       int use = 0; n       if (idx < z) { n         use = nums[idx]; n         idx += 1; n       } n       int col = i, col1 = i + 1; n       int color = 0, st = n - 1, curcol = 2; n       if (grid[n - 1][i] != -1) { n         color = grid[n - 1][0] ^ 1; n         st = n - 2; n       } n       if (i != 0 && st >= 0) { n         if (grid[st][i - 1] == 0) n           color = 1; n         else n           color = 0; n       } n       while (use > 0 && st >= 0) { n         grid[st][col] = color; n         grid[st][col1] = color; n         use -= 1; n         color ^= 1; n         st -= 1; n       } n     } n     for (int i = 0; i < m; i++) { n       int now = curcol[i]; n       for (int j = 0; j < n; j += 2) { n         if (grid[j][i] != -1) break; n         grid[j][i] = now; n         grid[j + 1][i] = now; n         if (now == 2) n           now = 3; n         else n           now = 2; n       } n     } n     cout <<  YES  << endl; n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < m; j++) { n         cout << char(97 + grid[i][j]); n       } n       cout << endl; n     } n     return; n   } n   cout <<  NO  << endl; n   return; n } n int main(int argc, const char* argv[]) { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t; n   cin >> t; n   for (int tt = 0; tt < t; tt++) { n     solve(tt, t); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N, K; n int A[300005]; n int DP[300005]; n bool chk(int d) { n   int mxp = 0; n   for (int i = K; i <= N; i++) { n     if (A[i] - A[DP[i - K] + 1] <= d) mxp = i; n     DP[i] = mxp; n   } n   return mxp == N; n } n int main() { n   cin >> N >> K; n   for (int i = 1; i <= N; i++) cin >> A[i]; n   sort(A + 1, A + 1 + N); n   int l = -1, r = 1e9; n   while (l + 1 < r) { n     int md = (l + r) / 2; n     if (chk(md)) n       r = md; n     else n       l = md; n   } n   cout << r <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 119; n int n, F[N], T[N]; n long double D[N]; n vector<pair<int, int> > Adj[N]; n void DFS(int v, int p) { n   long double d = 2.0 / Adj[v].size(), t = 1.0; n   for (auto X : Adj[v]) n     if (X.first == p) t += D[X.second], t -= (t > 2.0) ? 2.0 : 0; n   for (auto X : Adj[v]) n     if (X.first != p) { n       F[X.second] = v; n       T[X.second] = X.first; n       t += d; n       t -= (t > 2.0) ? 2 : 0; n       D[X.second] = t; n       DFS(X.first, v); n     } n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i < n; i++) n     scanf( %d%d , &F[i], &T[i]), Adj[F[i]].push_back({T[i], i}), n         Adj[T[i]].push_back({F[i], i}); n   DFS(1, 0); n   printf( %d n , n - 1); n   for (int i = 1; i < n; i++) { n     if (D[i] > 1) swap(F[i], T[i]), D[i] -= 1; n     printf( 1 %d %d %d %.12f n , i, F[i], T[i], (double)D[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int a[200013]; n int type[200013], len[200013]; n int high[200013]; n int res[200013]; n multiset<int> has; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   for (int i = 1; i <= m; i++) scanf( %d%d , &type[i], &len[i]); n   for (int i = m; i > 0; i--) high[i] = max(high[i + 1], len[i]); n   for (int i = high[1] + 1; i <= n; i++) res[i] = a[i]; n   for (int i = 1; i <= high[1]; i++) has.insert(a[i]); n   int dir = 1; n   int last = high[1]; n   for (int i = 1; i <= m + 1; i++) { n     if (len[i] == high[i]) { n       while (last > len[i]) { n         if (dir == 1) { n           res[last--] = *has.rbegin(); n           has.erase(--has.end()); n         } else { n           res[last--] = *has.begin(); n           has.erase(has.begin()); n         } n       } n       dir = type[i]; n     } n   } n   for (int i = 1; i <= n; i++) printf( %d  , res[i]); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n int const maxl = 22; n int const maxm = 2520; n long long f[maxl][maxm + 4][50][2]; n int a[maxl]; n std::vector<int> alcm; n long long plcm[maxm + 4][10]; n long long kth[maxm + 4]; n long long l, r; n int gcd(int x, int y) { return !y ? x : gcd(y, x % y); } n int lcm(int x, int y) { n   if (!x) return y; n   if (!y) return x; n   return x * y / gcd(x, y); n } n void init() { n   for (int i2 = 0, t2 = 1; i2 < 4; i2++, t2 *= 2) n     for (int i3 = 0, t3 = 1; i3 < 3; i3++, t3 *= 3) n       for (int i5 = 0, t5 = 1; i5 < 2; i5++, t5 *= 5) n         for (int i7 = 0, t7 = 1; i7 < 2; i7++, t7 *= 7) n           alcm.push_back(t2 * t3 * t5 * t7); n   std::sort(alcm.begin(), alcm.end()); n   for (int i = 0; i <= maxm; i++) n     for (int j = 0; j < 10; j++) plcm[i][j] = lcm(i, j); n   for (int i = 0; i < (int)alcm.size(); i++) kth[alcm[i]] = i; n } n int get_len(long long x) { n   int ret = 0; n   for (; x; x /= 10) a[ret++] = x % 10; n   return ret; n } n long long calc(long long x) { n   int n = get_len(x); n   for (int i = 0; i <= n; i++) n     for (int j = 0; j < maxm; j++) n       for (int k = 0; k < (int)alcm.size(); k++) n         for (int l = 0; l < 2; l++) f[i][j][k][l] = 0; n   f[0][0][0][0] = 1; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < maxm; j++) n       for (int k = 0; k < (int)alcm.size(); k++) n         for (int l = 0; l < 2; l++) { n           if (!f[i][j][k][l]) continue; n           int up = (l ? 9 : a[n - i - 1]); n           for (int ti = 0; ti <= up; ti++) { n             int last = l || (ti < a[n - i - 1]); n             f[i + 1][(j * 10 + ti) % maxm][kth[plcm[alcm[k]][ti]]][last] += n                 f[i][j][k][l]; n           } n         } n   long long ans = 0; n   for (int i = 0; i < maxm; i++) n     for (int k = 0; k < (int)alcm.size(); k++) n       for (int l = 0; l < 2; l++) n         if (!(i % alcm[k])) ans += f[n][i][k][l]; n   return ans; n } n int main() { n   std::ios::sync_with_stdio(false); n   init(); n   int T; n   std::cin >> T; n   while (T--) { n     std::cin >> l >> r; n     std::cout << calc(r) - calc(l - 1) <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = n     3.14159265358979323846264338327950288419716939937510582097494459230; n const long long MOD = 1e9 + 7; n const long long INF = 1e14; n long long mpow(long long a, long long b, long long p = MOD) { n   a = a % p; n   long long res = 1; n   while (b > 0) { n     if (b & 1) res = (res * a) % p; n     a = (a * a) % p; n     b = b >> 1; n   } n   return res % p; n } n const long long N = 4 * 100000; n long long n, m, b[N], a[N], g[N], h[N]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n >> m; n   for (int i = 1; i <= (int)(n); ++i) n     cin >> a[i], a[i] += a[i - 1], g[i] = 1e14; n   for (int i = 1; i <= (int)(m); ++i) n     cin >> b[i], b[i] += b[i - 1], h[i] = 1e14; n   for (int i = 1; i <= (int)(n); ++i) n     for (long long j = i; j <= n; j++) n       g[j - i + 1] = min(g[j - i + 1], a[j] - a[i - 1]); n   for (int i = 1; i <= (int)(m); ++i) n     for (long long j = i; j <= m; j++) n       h[j - i + 1] = min(h[j - i + 1], b[j] - b[i - 1]); n   long long x; n   cin >> x; n   long long ans = 0; n   for (int i = 1; i <= (int)(n); ++i) n     for (int j = 1; j <= (int)(m); ++j) { n       if (g[i] * h[j] <= x) { n         ans = max(ans, (long long)(i * j)); n       } n     } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int gcd(int a, int b) { n   while (b > 0) { n     int t = a; n     a = b; n     b = t % b; n   } n   return a; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   int n; n   cin >> n; n   vector<int> a(n); n   for (int i = 0; i < n; ++i) cin >> a[i]; n   { n     int i; n     for (i = 1; i < n; ++i) n       if (a[i] != a[i - 1]) break; n     if (i == n) { n       cout << (int64_t)n * (n - 1) << endl; n       return 0; n     } n   } n   int64_t ans = 0; n   for (int divisor = 1; divisor < n; ++divisor) { n     if (n % divisor > 0) continue; n     vector<int> max_of_rem(a.begin(), a.begin() + divisor); n     for (int i = divisor; i < n; ++i) n       if (a[i] > max_of_rem[i % divisor]) max_of_rem[i % divisor] = a[i]; n     int lim = n; n     for (int left = 0; left < lim;) { n       int offs; n       for (offs = 0; offs < n - 1 && a[(left + offs) % n] == n                                          max_of_rem[(left + offs) % divisor]; n            ++offs) n         ; n       if (offs == 0) { n         ++left; n         continue; n       } n       int goffs = offs; n       if (left == 0) { n         while (left + offs < lim && n                a[lim - 1] == max_of_rem[(lim - 1) % divisor]) n           --lim; n         offs += n - lim; n       } n       for (int i = divisor; i <= offs; i += divisor) { n         if (gcd(n, i) == divisor) { n           ans += offs == n ? n : offs - i + 1; n         } n       } n       left += goffs; n     } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int ansa, ansb; n int mid[110], cnt; n int yy; n int cmp(int a, int b) { return a > b; } n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) { n     int s; n     scanf( %d , &s); n     int m = s / 2; n     int x; n     if (s & 1) { n       for (int i = 1; i <= m; ++i) { n         scanf( %d , &x); n         ansa += x; n       } n       scanf( %d , &x); n       mid[cnt++] = x; n       for (int i = 1; i <= m; ++i) { n         scanf( %d , &x); n         ansb += x; n       } n     } else { n       for (int i = 1; i <= m; ++i) { n         scanf( %d , &x); n         ansa += x; n       } n       for (int i = 1; i <= m; ++i) { n         scanf( %d , &x); n         ansb += x; n       } n     } n   } n   sort(mid, mid + cnt, cmp); n   for (int i = 0; i < cnt; ++i) { n     if (i & 1) n       ansb += mid[i]; n     else n       ansa += mid[i]; n   } n   printf( %d %d , ansa, ansb); n } n


#include <bits/stdc++.h> n using namespace std; n const int N_MAX = 1005, M_MAX = 100005; n struct union_find { n   int rank[N_MAX], parent[N_MAX]; n   union_find() { n     for (int i = 0; i < N_MAX; i++) { n       rank[i] = 0; n       parent[i] = i; n     } n   } n   int find(int x) { return x == parent[x] ? x : parent[x] = find(parent[x]); } n   bool join(int x, int y) { n     x = find(x); n     y = find(y); n     if (x == y) return false; n     if (rank[x] >= rank[y]) { n       parent[y] = x; n       if (rank[x] == rank[y]) rank[x]++; n     } else n       parent[x] = y; n     return true; n   } n }; n struct edge { n   int a, b; n   char who; n }; n int N, M, H; n edge edges[M_MAX]; n union_find S_UF, M_UF; n int impossible() { n   puts( -1 ); n   return 0; n } n int main() { n   scanf( %d %d , &N, &M); n   if (N % 2 == 0) return impossible(); n   H = (N - 1) / 2; n   for (int i = 0; i < M; i++) { n     scanf( %d %d %c , &edges[i].a, &edges[i].b, &edges[i].who); n     edges[i].a--; n     edges[i].b--; n   } n   int S = 0; n   for (int i = 0; i < M; i++) n     if (edges[i].who ==  S ) n       if (S_UF.join(edges[i].a, edges[i].b)) S++; n   if (S < H) return impossible(); n   int components = N - S; n   vector<int> answer; n   for (int i = 0; i < M && (int)answer.size() < H - (components - 1); i++) n     if (edges[i].who ==  M ) n       if (S_UF.find(edges[i].a) == S_UF.find(edges[i].b)) n         if (M_UF.join(edges[i].a, edges[i].b)) answer.push_back(i); n   assert((int)answer.size() <= H - (components - 1)); n   if ((int)answer.size() != H - (components - 1)) return impossible(); n   for (int i = 0; i < M; i++) n     if (edges[i].who ==  M ) n       if (S_UF.join(edges[i].a, edges[i].b)) { n         assert(M_UF.join(edges[i].a, edges[i].b)); n         answer.push_back(i); n       } n   assert((int)answer.size() <= H); n   if ((int)answer.size() != H) return impossible(); n   for (int i = 0; i < M; i++) n     if (edges[i].who ==  S ) n       if (M_UF.join(edges[i].a, edges[i].b)) answer.push_back(i); n   assert((int)answer.size() == N - 1); n   printf( %d n , (int)answer.size()); n   for (int i = 0; i < (int)answer.size(); i++) n     printf( %d%c , answer[i] + 1, i < (int)answer.size() - 1 ?     :   n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool reach(int from[3], int to[3], vector<vector<string>> &works) { n   if (works[from[0]][from[1]][from[2]] ==  0 ) return false; n   if (from[0] == to[0] && from[1] == to[1] && from[2] == to[2]) return true; n   for (int d = 0; d < 3; ++d) n     if (from[d] < to[d]) { n       ++from[d]; n       bool res = reach(from, to, works); n       --from[d]; n       if (res) return true; n     } n   return false; n } n int main() { n   int lim[3]; n   for (int i = 0; i < 3; ++i) scanf( %d , &lim[i]); n   vector<vector<string>> works(lim[0], n                                vector<string>(lim[1], string(lim[2],    ))); n   for (int i = 0; i < lim[0]; ++i) n     for (int j = 0; j < lim[1]; ++j) scanf( %s , &works[i][j][0]); n   int result = 0; n   for (int i = 0; i < lim[0]; ++i) n     for (int j = 0; j < lim[1]; ++j) n       for (int k = 0; k < lim[2]; ++k) n         if (works[i][j][k] ==  1 ) { n           int at[3] = {i, j, k}; n           bool critical = false; n           for (int fromd = 0; fromd < 3; ++fromd) n             if (at[fromd] >= 1) { n               int from[3] = {i, j, k}; n               --from[fromd]; n               for (int tod = 0; tod < 3; ++tod) n                 if (at[tod] + 1 < lim[tod]) { n                   int to[3] = {i, j, k}; n                   ++to[tod]; n                   bool before = reach(from, to, works); n                   works[i][j][k] =  0 ; n                   bool after = reach(from, to, works); n                   works[i][j][k] =  1 ; n                   if (before && !after) critical = true; n                 } n             } n           if (critical) ++result; n         } n   printf( %d n , result); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long suml[100100], sumr[100100], l[100100], r[100100], A[100100], n     B[100100], total, ans[100100]; n int main() { n   long long N; n   cin >> N; n   for (long long i = 1; i <= N; i++) { n     scanf( %lld , A + i); n   } n   for (long long i = N; i > 0; i--) { n     scanf( %lld , B + i); n   } n   for (long long i = 1; i <= N; i++) { n     l[i] = i - 1; n     r[i] = i + 1; n   } n   for (long long i = 1; i <= N; i++) { n     long long x = B[i]; n     long long sum = suml[r[x]] + sumr[l[x]] + A[x]; n     sumr[l[x]] = suml[r[x]] = sum; n     l[r[x]] = l[x]; n     r[l[x]] = r[x]; n     total = max(total, sum); n     ans[i] = total; n   } n   for (long long i = N - 1; i >= 0; i--) { n     printf( %lld n , ans[i]); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n; n char s[200010], t[200010]; n double s1[200010][26], s2[200010][26]; n double ans = 0.0; n int main() { n   scanf( %d n , &n); n   memset(s1, 0, sizeof(s1)); n   memset(s2, 0, sizeof(s2)); n   for (int i = 1; i <= n; i++) { n     scanf( %c , &s[i]); n     for (int c = 0; c < 26; c++) s1[i][c] = s1[i - 1][c]; n     s1[i][s[i] -  A ] += i; n   } n   scanf(  n ); n   for (int i = 1; i <= n; i++) { n     scanf( %c , &t[i]); n     for (int c = 0; c < 26; c++) s2[i][c] = s2[i - 1][c]; n     s2[i][t[i] -  A ] += i; n   } n   scanf(  n ); n   double total = 0.0; n   for (int i = 1; i <= n; i++) { n     total += 1.0 * i * i; n     ans += 1.0 * (s2[i - 1][s[i] -  A ]) * (n - i + 1); n     ans += 1.0 * (s1[i - 1][t[i] -  A ]) * (n - i + 1); n     if (t[i] == s[i]) ans += 1.0 * i * (n - i + 1); n   } n   double ret = ans / total; n   printf( %.12lf n , ret); n   return 0; n } n


#include <bits/stdc++.h> n const int MAXN = 1e5 + 10; n int n, m, k, p; n int h[MAXN], a[MAXN]; n namespace solver1 { n long long fin[MAXN]; n bool check(long long x) { n   long long need = 0; n   static long long t[MAXN]; n   for (int i = 1; i <= n; i++) { n     need += (t[i] = std::max(0ll, (fin[i] - x + p - 1) / p)); n   } n   if (need > k * m) return 0; n   static int cnt[MAXN]; n   memset(cnt, 0, sizeof cnt); n   for (int i = 1; i <= n; i++) { n     for (int j = t[i] - 1; j >= 0; j--) { n       long long delta = fin[i] - x - (long long)j * p; n       if (delta <= h[i]) { n         cnt[1]++; n       } else if (h[i] + (long long)a[i] * (m - 1) < delta) { n         return 0; n       } else { n         cnt[(delta - h[i] + a[i] - 1) / a[i] + 1]++; n       } n     } n   } n   int tot = 0; n   for (int i = 1; i <= m; i++) { n     tot += cnt[i]; n     tot = std::max(0, tot - k); n   } n   return tot == 0; n } n void main() { n   for (int i = 1; i <= n; i++) { n     fin[i] = (long long)a[i] * m + h[i]; n   } n   long long l = 1, r = 1e15, ans = 1e15; n   while (l <= r) { n     long long mid = (l + r) >> 1; n     if (check(mid)) { n       ans = mid; n       r = mid - 1; n     } else { n       l = mid + 1; n     } n   } n   printf( %lld n , ans); n } n }  // namespace solver1 n int main() { n   scanf( %d%d%d%d , &n, &m, &k, &p); n   for (int i = 1; i <= n; i++) { n     scanf( %d%d , h + i, a + i); n   } n   solver1::main(); n } n


#include <bits/stdc++.h> n using namespace std; n struct Point { n   int x; n   int y; n   int z; n   int id; n }; n bool compY(Point a, Point b) { n   if (a.y != b.y) { n     return a.y < b.y; n   } n   return a.z < b.z; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   int n; n   cin >> n; n   map<int, vector<Point>> m; n   for (int i = 0; i < n; i++) { n     int x, y, z; n     cin >> x >> y >> z; n     m[x].push_back({x, y, z, i}); n   } n   vector<pair<int, int>> ans; n   vector<bool> deleted(n, false); n   for (auto &e : m) { n     if (e.second.size() < 2) { n       continue; n     } n     vector<Point> a = e.second; n     int k = a.size(); n     if (k < 2) { n       continue; n     } n     sort(a.begin(), a.end(), compY); n     for (int i = 0; i < k - 1; i++) { n       if (a[i].y == a[i + 1].y) { n         ans.emplace_back(a[i].id, a[i + 1].id); n         deleted[a[i].id] = true; n         deleted[a[i + 1].id] = true; n         i++; n       } n     } n     int l = 0; n     int r = 0; n     while (l < k) { n       if (deleted[a[l].id]) { n         l++; n         continue; n       } n       r = max(r, l + 1); n       while (r < k && deleted[a[r].id]) { n         r++; n       } n       if (r == k) { n         break; n       } n       ans.emplace_back(a[l].id, a[r].id); n       deleted[a[l].id] = true; n       deleted[a[r].id] = true; n       l++; n     } n   } n   bool havePrev = false; n   Point prev; n   for (auto &e : m) { n     bool here = false; n     Point cur; n     for (auto &x : e.second) { n       if (!deleted[x.id]) { n         here = true; n         cur = x; n         break; n       } n     } n     if (!here) { n       continue; n     } n     if (havePrev) { n       ans.emplace_back(prev.id, cur.id); n       havePrev = false; n     } else { n       havePrev = true; n       prev = cur; n     } n   } n   for (auto &e : ans) { n     cout << e.first + 1 <<     << e.second + 1 <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1000; n int a[MAXN]; n int n; n int main() { n   scanf( %d , &n); n   n *= 2; n   for (int i = 1; i <= n; ++i) scanf( %d , &a[i]); n   sort(a + 1, a + n + 1); n   int ans = 1e9; n   for (int j = 1; j <= n; ++j) n     for (int k = j + 1; k <= n; ++k) { n       int t = 0; n       for (int i = 1; i < n;) { n         while (i == j || i == k) ++i; n         int p = i + 1; n         while (p == j || p == k) ++p; n         t += a[p] - a[i]; n         i = p + 1; n       } n       ans = min(t, ans); n     } n   printf( %d , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long dp[10][10000]; n long long cnt[10]; n long long w; n int32_t main() { n   ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   cin >> w; n   for (long long i = 0; i < 8; i++) { n     cin >> cnt[i]; n   } n   for (long long i = 0; i < 10; i++) n     for (long long j = 0; j < 10000; j++) dp[i][j] = -1; n   dp[0][0] = 0; n   for (long long i = 0; i < 8; i++) { n     for (long long j = 0; j < 10000; j++) { n       if (dp[i][j] == -1) continue; n       long long b = 840 / (i + 1); n       b = min(b, cnt[i]); n       for (long long k = 0; k <= b; k++) n         if (j + k * (i + 1) < 10000) { n           dp[i + 1][j + k * (i + 1)] = n               max(dp[i + 1][j + k * (i + 1)], n                   dp[i][j] + (cnt[i] - k) / (840 / (i + 1))); n         } n     } n   } n   long long ans = 0; n   for (long long j = 0; j < 10000; j++) { n     if (dp[8][j] != -1 && j <= w) { n       ans = max(ans, j + 840ll * (min(dp[8][j], (w - j) / 840))); n     } n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   while (scanf( %d , &n) != EOF) { n     if (n % 2 != 0) n       printf( black n ); n     else { n       printf( white n1 2 n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int w, m; n   cin >> w >> m; n   if (m == 1 || w == 2) { n     cout <<  YES  << endl; n     return 0; n   } n   while (m) { n     if (m % w == 1) { n       --m; n     } else if (m % w == w - 1) { n       ++m; n     } n     if (m % w == 0) { n       m /= w; n     } else { n       cout <<  NO  << endl; n       return 0; n     } n   } n   cout <<  YES  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1.1e6; n int ans; n int n, m; n vector<int> F; n int S[N][7][7]; n int s(int i, int a, int b) { n   if (i == m) return (F[i] - a) / 3; n   if (i + 1 == m) return (F[i] - a) / 3 + (F[i + 1] - b) / 3; n   int loc = S[i + 1][b][0]; n   for (int j = 0; j < 3; j++) n     if (a + j <= min(6, F[i]) && b + j <= min(6, F[i + 1]) && n         0 + j <= min(6, F[i + 2])) n       loc = max(loc, j + ((F[i] - (a + j)) / 3) + S[i + 1][b + j][j]); n   return loc; n } n int main() { n   scanf(  %d%d , &n, &m); n   F = vector<int>(m + 1); n   for (int i = 0; i < n; i++) { n     int a; n     scanf(  %d , &a); n     F[a]++; n   } n   for (int i = m; i + 1; i--) n     for (int a = 0; a < 7; a++) n       for (int b = 0; b < 7; b++) S[i][a][b] = s(i, a, b); n   printf( %d n , ans + S[0][0][0]); n } n


#include <bits/stdc++.h> n using namespace std; n int T, n, k, l, a[300010], lst; n bool flag = 0; n int main() { n   scanf( %d , &T); n   while (T--) { n     flag = 0; n     lst = 0; n     scanf( %d%d%d , &n, &k, &l); n     for (int i = 1; i <= n; i++) { n       scanf( %d , &a[i]); n       if (a[i] > l) n         flag = 1; n       else if (a[i] + k <= l) n         lst = 0; n       else if (lst < k) n         lst = max(lst + 1, k + a[i] - l); n       else { n         if (lst + 1 + a[i] <= l + k) n           lst++; n         else n           flag = 1; n       } n     } n     if (flag) n       printf( No n ); n     else n       printf( Yes n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s, t; n   cin >> s >> t; n   int flag = 0; n   vector<char> arr(26,  . ); n   for (int i = 0; i < s.length(); i++) { n     int a = int(s[i]); n     int b = int(t[i]); n     if (arr[a - 97] == t[i] || arr[a - 97] ==  . ) { n       arr[a - 97] = t[i]; n       if (arr[b - 97] == s[i] || arr[b - 97] ==  . ) n         arr[b - 97] = char(a); n       else { n         cout << -1; n         flag = -1; n         break; n       } n     } else { n       cout << -1; n       flag = -1; n       break; n     } n   } n   if (flag != -1) { n     for (int i = 0; i < 26; i++) { n       if (arr[i] !=  . ) { n         int x = (int)arr[i]; n         if (arr[x - 97] == char(i + 97) || arr[x - 97] ==  . ) { n           arr[x - 97] =  . ; n           continue; n         } else { n           cout << -1; n           flag = -1; n           break; n         } n       } n     } n   } n   if (flag != -1) { n     int sum = 0; n     for (int i = 0; i < 26; i++) { n       if (arr[i] !=  . ) sum++; n     } n     cout << sum << endl; n     if (sum != 0) { n       for (int i = 0; i < 26; i++) { n         if (arr[i] !=  . ) cout << arr[i] <<     << char(i + 97) << endl; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1005; n const int MOD = 1000000007; n int t, k, dp[N][N][2]; n char l[N], r[N], s[N]; n void add(int &a, int b) { n   a += b; n   if (a >= MOD) a -= MOD; n } n int dfs(int l, int pos, int f, int limit) { n   if (l == -1) return f; n   if (!limit && dp[l][pos][f] != -1) return dp[l][pos][f]; n   int up = limit ? (s[l] -  0 ) : 9, ret = 0; n   for (int i = 0; i <= up; i++) { n     if (i == 4 || i == 7) { n       int ff = f; n       if (pos != 1001) ff |= ((pos - l) <= k); n       add(ret, dfs(l - 1, l, ff, limit && (i == up))); n     } else { n       add(ret, dfs(l - 1, pos, f, limit && (i == up))); n     } n   } n   if (!limit) dp[l][pos][f] = ret; n   return ret; n } n int gao(char *a) { n   strcpy(s, a); n   reverse(s, s + strlen(s)); n   return dfs(strlen(a) - 1, 1001, 0, 1); n } n void sub(char *s) { n   int len = strlen(s), l = strlen(s) - 1; n   while (s[l] ==  0 ) s[l] =  9 , l--; n   s[l]--; n   if (s[0] ==  0  && len > 1) { n     for (int i = 1; i <= len; i++) s[i - 1] = s[i]; n   } n } n int main() { n   memset(dp, -1, sizeof(dp)); n   cin >> t >> k; n   while (t--) { n     cin >> l >> r; n     sub(l); n     cout << ((gao(r) - gao(l)) % MOD + MOD) % MOD << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[101][101]; n int main() { n   int n; n   cin >> n; n   int sum = 0; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       cin >> a[i][j]; n       if ((i == j || i == (n - j + 1)) || n           (i == (n + 1) / 2 || j == (n + 1) / 2)) { n         sum += a[i][j]; n       } n     } n   } n   cout << sum; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 1e9 + 7; n const int N = 2e5 + 1; n const int oo = 1e9; n const double pi = acos(-1); n int main() { n   int t; n   cin >> t; n   while (t--) { n     int a, b, n; n     cin >> a >> b >> n; n     if (b < a) swap(a, b); n     int ans = 0; n     while (a <= n && b <= n) { n       ++ans; n       if (ans % 2) n         a += b; n       else n         b += a; n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 2147483647; n const int N = 300010; n int n, m, ans, ansx, ansy, s1, s2, max1; n struct aa { n   int x, y, w; n } a[N], b[N << 1]; n multiset<int> q1, q2; n void Init(int &x) { n   char y; n   while ((y = getchar()) <  0  || y >  9 ) n     ; n   x = y -  0 ; n   while ((y = getchar()) >=  0  && y <=  9 ) x = (x << 3) + (x << 1) + y -  0 ; n } n bool cmp(aa x, aa y) { return x.x < y.x || x.x == y.x && x.y < y.y; } n int main() { n   scanf( %d%d , &n, &m); n   memset(a, 0, sizeof(a)); n   for (int i = 1; i <= n; ++i) scanf( %d%d , &a[i].x, &a[i].y); n   for (int i = 1; i <= n; ++i) n     b[(i << 1) - 1].x = a[i].x, b[(i << 1) - 1].y = 0, b[i << 1].x = a[i].y, n                  b[i << 1].y = 1, b[i << 1].w = i; n   n <<= 1; n   sort(b + 1, b + n + 1, cmp); n   ans = 0; n   ansx = 0; n   ansy = 0; n   s1 = 0; n   s2 = 0; n   q1.clear(); n   q2.clear(); n   for (int i = 1; i <= n; ++i) n     if (b[i].y) { n       if (s1 == m && b[i].x - max1 + 1 > ans) { n         ansx = max1; n         ansy = b[i].x; n         ans = ansy - ansx + 1; n       } n       if (a[b[i].w].x > max1) n         q2.erase(q2.find(a[b[i].w].x)), --s2; n       else { n         q1.erase(q1.find(a[b[i].w].x)); n         --s1; n         if (s2) n           q1.insert(max1 = *q2.begin()), q2.erase(q2.begin()), ++s1, --s2; n         else if (s1) n           max1 = *q1.rbegin(); n       } n     } else { n       if (s1 < m) n         q1.insert(b[i].x), ++s1, max1 = b[i].x; n       else n         q2.insert(b[i].x), ++s2; n     } n   printf( %d n , ans); n   bool flag = 0; n   if (ans == 0) { n     for (int i = 1; i < m; ++i) printf( %d  , i); n     printf( %d n , m); n     return 0; n   } n   for (int i = 1; i <= n && m; ++i) n     if (a[i].x <= ansx && a[i].y >= ansy) { n       if (flag) n         putchar(   ); n       else n         flag = 1; n       printf( %d , i); n       --m; n     } n   putchar(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n = 0; n   int nTemp = 0; n   cin >> n; n   nTemp = n; n   int sum = 0; n   int temp = 0; n   while (nTemp--) { n     cin >> temp; n     sum = sum + temp; n   } n   int ways = 0; n   for (int i = 1; i <= 5; i++) { n     if ((sum + i) % (n + 1) != 1) { n       ways++; n     } n   } n   cout << ways << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = (long long)5e18; n const long long N = 120000; n long long n, a[N], m, b[N]; n long long pre[N], nxt[N]; n long long q[N], top = 0; n long long _abs(long long x) { return x < 0 ? -x : x; } n void solve() { n   scanf( %lld%lld , &n, &m); n   for (long long i = 1; i <= n; i++) { n     scanf( %lld , &a[i]); n     if (i > 1) b[i - 1] = _abs(a[i] - a[i - 1]); n   } n   b[0] = b[n] = inf; n   q[++top] = 0; n   for (long long i = 1; i <= n; i++) { n     while (b[q[top]] < b[i]) nxt[q[top]] = i, top--; n     pre[i] = q[top]; n     q[++top] = i; n   } n   while (m--) { n     long long ans = 0; n     long long l, r; n     scanf( %lld%lld , &l, &r); n     r--; n     for (long long i = l; i <= r; i++) { n       long long L = max(l, pre[i] + 1); n       long long R = min(r, nxt[i] - 1); n       ans += (R - i + 1) * (i - L + 1) * b[i]; n     } n     printf( %lld n , ans); n   } n } n signed main() { n   long long TTT = 1; n   while (TTT--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1000000007; n const int MAXN = 1000006; n char s[MAXN]; n int qp(long long a, int b) { n   long long ans = 1; n   while (b) { n     if (b % 2) ans = ans * a % mod; n     b /= 2; n     a = a * a % mod; n   } n   return ans; n } n void solve() { n   int n, k; n   scanf( %d%d , &n, &k); n   scanf( %s , s); n   string P(s); n   P =  #  + P; n   int pi[MAXN], m = P.size() - 1, q = 0; n   pi[0] = pi[1] = 0; n   for (int i = 2; i <= m; ++i) { n     while (q && P[i] != P[q + 1]) q = pi[q]; n     if (P[i] == P[q + 1]) ++q; n     pi[i] = q; n   } n   int ile = 0; n   int prev = -m + 1; n   bool ok = true; n   set<int> S; n   int i = m; n   while (i) { n     S.insert(pi[i]); n     i = pi[i]; n   } n   for (int i = 0; i < (k); ++i) { n     int a; n     scanf( %d , &a); n     ile += max(0, a - (prev + m)); n     if (a - prev < m && prev > 0 && S.find(m - a + prev) == S.end()) ok = false; n     prev = a; n   } n   ile += max(0, n - (prev + m) + 1); n   if (!ok) n     printf( 0 n ); n   else n     printf( %d n , qp(26, ile)); n } n int main() { n   int t = 1; n   for (int i = 0; i < (t); ++i) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long bigmod(long long n, long long pow, long long m) { n   long long ret = 1; n   while (pow) { n     if (pow % 2) { n       ret *= n; n       ret %= m; n     } n     n *= n; n     n %= m; n     pow /= 2; n   } n   return ret; n } n long long arr[3 * 100009]; n int main() { n   long long n, a, sum = 0, sum1 = 0; n   scanf( %I64d , &n); n   for (long long i = 1; i <= n; i++) scanf( %I64d , &arr[i]); n   sort(arr + 1, arr + n + 1); n   for (long long i = 1; i <= n; i++) { n     sum += (arr[i] * (bigmod((long long)2, i - 1, 1000000000 + 7))); n     sum -= (arr[i] * (bigmod((long long)2, n - i, 1000000000 + 7))); n     sum %= 1000000000 + 7; n   } n   printf( %I64d n , sum); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1050; n long double pr[N][N]; n int n, m, P[N]; n inline long double gp(int i, int j) { n   if (i < j) n     return pr[i][j]; n   else n     return 1 - pr[j][i]; n } n int main() { n   cin >> n >> m; n   for (int i = 1; i <= n; ++i) cin >> P[i]; n   for (int i = 1; i <= n; ++i) n     for (int j = i + 1; j <= n; ++j) pr[i][j] = P[i] > P[j]; n   for (int i = 1, a, b; i <= m; ++i) { n     cin >> a >> b; n     if (a > b) swap(a, b); n     for (int i = 1; i <= n; ++i) n       if (i != a && i != b) { n         pr[i][b] = pr[i][a] = (gp(i, a) + gp(i, b)) / 2; n         pr[b][i] = pr[a][i] = 1 - pr[i][a]; n       } n     pr[a][b] = pr[b][a] = 0.5; n   } n   long double res = 0; n   for (int i = 1; i <= n; ++i) n     for (int j = i + 1; j <= n; ++j) res += pr[i][j]; n   cout << fixed << setprecision(10) << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int m; n vector<vector<char> > grid; n vector<vector<int> > mindist; n vector<int> first; n vector<int> last; n bool notpossible = false; n int main() { n   scanf( %d %d n , &n, &m); n   grid.assign(n, vector<char>(m)); n   first.assign(n, -1); n   last.assign(n, -1); n   mindist.assign(n, vector<int>(m)); n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       scanf( %c , &grid[i][j]); n       if (grid[i][j] ==  1 ) { n         if (first[i] == -1) { n           first[i] = j; n         } n         last[i] = j; n       } n     } n     if (last[i] == -1) { n       notpossible = true; n     } n     scanf(  n ); n   } n   if (notpossible) { n     printf( -1 n ); n     return 0; n   } n   for (int i = 0; i < n; i++) { n     int current = m - last[i]; n     for (int j = 0; j < m; j++) { n       if (grid[i][j] ==  1 ) { n         current = 0; n       } n       mindist[i][j] = current; n       current++; n     } n     current = first[i] + 1; n     for (int j = m - 1; j >= 0; j--) { n       if (grid[i][j] ==  1 ) { n         current = 0; n       } n       mindist[i][j] = min(mindist[i][j], current); n       current++; n     } n   } n   int mincolumn = 0; n   for (int i = 0; i < n; i++) { n     mincolumn += mindist[i][0]; n   } n   for (int i = 1; i < m; i++) { n     int currentcolumn = 0; n     for (int j = 0; j < n; j++) { n       currentcolumn += mindist[j][i]; n     } n     mincolumn = min(mincolumn, currentcolumn); n   } n   printf( %d n , mincolumn); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-8; n struct point { n   double x, y; n   point(double _x = 0, double _y = 0) : x(_x), y(_y) {} n   void read() { scanf( %lf%lf , &x, &y); } n }; n point polygon[30]; n int sign(double x) { n   if (fabs(x) < eps) return 0; n   return (x > 0) ? 1 : -1; n } n double cross(const point &a, const point &b, const point &c, const point &d) { n   return (b.x - a.x) * (d.y - c.y) - (b.y - a.y) * (d.x - c.x); n } n double dist(const point &a, const point &b) { n   return hypot(a.x - b.x, a.y - b.y); n } n pair<point, bool> getIntersection(const point &a, const point &b, n                                   const point &c, const point &d) { n   if (sign(cross(a, b, c, d)) == 0) return make_pair(point(0, 0), false); n   double a1 = b.x - a.x, b1 = c.x - d.x, c1 = c.x - a.x; n   double a2 = b.y - a.y, b2 = c.y - d.y, c2 = c.y - a.y; n   double tmp = a1 * b2 - a2 * b1; n   double x = (c1 * b2 - c2 * b1) / tmp; n   double y = (c2 * a1 - c1 * a2) / tmp; n   if (sign(x) >= 0 && sign(x - 1) < 0 && sign(y) >= 0 && sign(y - 1) < 0) { n     return make_pair(point(a.x + x * (b.x - a.x), a.y + x * (b.y - a.y)), true); n   } else { n     return make_pair(point(0, 0), false); n   } n } n int main() { n   point start, end; n   start.read(); n   end.read(); n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) polygon[i].read(); n   int flag = 0; n   point a, b; n   int la, lb; n   for (int i = 0; i < n; i++) { n     pair<point, bool> tmp = n         getIntersection(start, end, polygon[i], polygon[(i + 1) % n]); n     if (tmp.second) { n       if (flag == 0) { n         a = tmp.first; n         la = i; n       } else { n         b = tmp.first; n         lb = i; n       } n       flag++; n     } n   } n   if (flag < 2) { n     printf( %.10lf n , dist(start, end)); n     return 0; n   } n   if (sign(dist(start, a) - dist(start, b)) > 0) { n     swap(a, b); n     swap(la, lb); n   } n   double ans = dist(start, a) + dist(a, b) * 2 + dist(b, end); n   double s1 = dist(start, a) + dist(a, polygon[(la + 1) % n]); n   for (int i = (la + 1) % n; i != lb; i = (i + 1) % n) n     s1 += dist(polygon[i], polygon[(i + 1) % n]); n   s1 += dist(polygon[lb], b) + dist(b, end); n   ans = min(ans, s1); n   double s2 = dist(start, a) + dist(a, polygon[la]); n   for (int i = la; i != (lb + 1) % n; i = (i + n - 1) % n) n     s2 += dist(polygon[i], polygon[(i + n - 1) % n]); n   s2 += dist(polygon[(lb + 1) % n], b) + dist(b, end); n   ans = min(ans, s2); n   printf( %.10lf n , ans); n   return 0; n } n


#include <bits/stdc++.h> n int const MAXN = 300100; n char a[MAXN]; n int n, x, y; n long long solve() { n   if (n == 1) return a[0] ==  0  ? y : 0; n   long long cc = 1; n   for (int i = 1; i < n; ++i) { n     if (a[i - 1] != a[i]) ++cc; n   } n   if (a[0] ==  1 ) --cc; n   if (a[n - 1] ==  1 ) --cc; n   if (cc <= 0) return 0; n   long long price = ((cc + 1) / 2) * y; n   long long rot = cc / 2; n   long long ans = price; n   while (rot > 0) { n     price -= y; n     price += x; n     ans = std::min(ans, price); n     --rot; n   } n   return ans; n } n int main() { n   scanf( %d %d %d , &n, &x, &y); n   scanf( %s , a); n   long long ans = solve(); n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int a[n]; n   int hired = 0, untreated, counter = 0, counter2 = 0; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     if (a[i] != -1) { n       hired += a[i]; n     } else if (a[i] == -1 && hired != 0) { n       counter++; n       hired--; n     } n     if (a[i] == -1) { n       counter2++; n     } n   } n   untreated = counter2 - counter; n   cout << untreated; n } n


#include <bits/stdc++.h> n using namespace std; n pair<int, int> sorted_pair(int x, int y) { n   if (x > y) { n     return pair<int, int>(y, x); n   } else { n     return pair<int, int>(x, y); n   } n } n int main() { n   int n; n   cin >> n; n   vector<vector<int>> graph(n + 1, vector<int>()); n   set<pair<int, int>> pairs; n   for (int i = 0; i < n - 1; i++) { n     int x, y; n     cin >> x >> y; n     graph[x].push_back(y); n     graph[y].push_back(x); n   } n   for (int i = 1; i <= n; i++) { n     queue<pair<int, int>> q; n     q.push(pair<int, int>(i, 0)); n     vector<bool> visited(n + 1, false); n     while (!q.empty()) { n       int v = q.front().first; n       int lvl = q.front().second; n       visited[v] = true; n       if (lvl < 2) { n         for (int k = 0; k < graph[v].size(); k++) { n           if (!visited[graph[v][k]]) { n             q.push(pair<int, int>(graph[v][k], lvl + 1)); n           } n         } n       } else if (lvl == 2) { n         pairs.insert(sorted_pair(i, v)); n       } n       q.pop(); n     } n   } n   cout << pairs.size() << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct quad { n   pair<int, int> points[4]; n   void read() { n     for (int i = 0; i < 4; i++) { n       cin >> points[i].first >> points[i].second; n     } n   } n   int cross(int a, int b, int c, int d) { return a * d - b * c; } n   int sgn(int x) { n     if (x < 0) return -1; n     if (x > 0) return 1; n     return 0; n   } n   bool contains(int x, int y) { n     int side = 0; n     for (int i = 0; i < 4; i++) { n       int j = (i + 1) % 4; n       int a = points[j].first - points[i].first; n       int b = points[j].second - points[i].second; n       int c = x - points[i].first; n       int d = y - points[i].second; n       int v = sgn(cross(a, b, c, d)); n       if (v == 0) { n         continue; n       } n       if (side == 0) { n         side = v; n       } else if (side != v) { n         return false; n       } n     } n     return true; n   } n }; n int main() { n   quad A, B; n   A.read(); n   B.read(); n   for (int x = -100; x <= 100; x++) { n     for (int y = -100; y <= 100; y++) { n       if (A.contains(x, y) && B.contains(x, y)) { n         cout <<  YES n ; n         return 0; n       } n     } n   } n   cout <<  NO n ; n   return 0; n } n


#include <bits/stdc++.h> n const int N = 2000010; n using namespace std; n char s[N], t[N]; n int main() { n   int n; n   scanf( %d , &n); n   scanf( %s%s , s, t); n   int a[5] = {}; n   for (int i = 0; i < 2 * n; i++) { n     a[((s[i] ==  1 ) << 1) | (t[i] ==  1 )]++; n   } n   int s0 = 0, s1 = 0; n   int t = 0; n   while (a[3]) { n     if (t == 0) n       s0++; n     else n       s1++; n     a[3]--; n     t ^= 1; n   } n   while (a[2]) { n     if (t == 0) n       s0++, a[2]--; n     else n       a[2]--; n     t ^= 1; n   } n   while (a[1]) { n     if (t == 1) n       s1++, a[1]--; n     else n       a[1]--; n     t ^= 1; n   } n   if (s0 > s1) n     puts( First ); n   else if (s0 == s1) n     puts( Draw ); n   else n     puts( Second ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long m, n; n   cin >> m >> n; n   double sum; n   sum = m; n   for (double i = m - 1; i > 0; i--) { n     sum -= pow(i / m, n); n   } n   cout << fixed; n   cout << setprecision(12) << sum <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100007, dx[8] = {1, 1, 1, 0, 0, -1, -1, -1}, n           dy[8] = {1, 0, -1, 1, -1, 1, 0, -1}; n int xs, ys, xt, yt, n, r[maxn], a[maxn], b[maxn], d[maxn] = {}, cnt = 0; n bool vis[maxn] = {}; n vector<int> row, col[maxn]; n vector<pair<int, int> > seg[maxn], cells[maxn]; n int dist(int x, int y) { n   x = lower_bound(row.begin(), row.end(), x) - row.begin() + 1; n   y = lower_bound(cells[x].begin(), cells[x].end(), make_pair(y, 0)) - n       cells[x].begin(); n   return d[cells[x][y].second]; n } n void set_dist(int x, int y, int val) { n   x = lower_bound(row.begin(), row.end(), x) - row.begin() + 1; n   y = lower_bound(cells[x].begin(), cells[x].end(), make_pair(y, 0)) - n       cells[x].begin(); n   d[cells[x][y].second] = val; n } n bool valid(int x, int y) { n   if (!binary_search(row.begin(), row.end(), x)) return 0; n   x = lower_bound(row.begin(), row.end(), x) - row.begin() + 1; n   if (!binary_search(col[x].begin(), col[x].end(), y)) return 0; n   return 1; n } n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   cin >> xs >> ys >> xt >> yt >> n; n   for (int i = 1; i <= n; ++i) { n     cin >> r[i] >> a[i] >> b[i]; n     row.push_back(r[i]); n   } n   sort(row.begin(), row.end()); n   for (int i = 1; i <= n; ++i) n     r[i] = lower_bound(row.begin(), row.end(), r[i]) - row.begin() + 1; n   for (int i = 1; i <= n; ++i) seg[r[i]].push_back({a[i], b[i]}); n   for (int i = 1; i <= n; ++i) { n     if (vis[r[i]]) continue; n     sort(seg[r[i]].begin(), seg[r[i]].end()); n     int cur = 1; n     for (auto it : seg[r[i]]) { n       cur = max(it.first, cur); n       for (int j = cur; j <= it.second; ++j) n         cells[r[i]].push_back({j, ++cnt}), col[r[i]].push_back(j); n     } n     vis[r[i]] = 1; n   } n   queue<pair<int, int> > q; n   q.push({xs, ys}); n   set_dist(xs, ys, 1); n   while (q.size()) { n     int x = q.front().first, y = q.front().second; n     q.pop(); n     for (int i = 0; i < 8; ++i) { n       int xx = dx[i] + x, yy = dy[i] + y; n       if (valid(xx, yy) && dist(xx, yy) == 0) n         set_dist(xx, yy, dist(x, y) + 1), q.push({xx, yy}); n     } n   } n   cout << dist(xt, yt) - 1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t, i, j, c = 0, d = 0, a, b, k = 0, sum = 0, sum1 = 0; n   cin >> t; n   string s; n   stack<char> st; n   cin >> s; n   for (i = 0; i < s.size(); i++) { n     if (s[i] ==  ( ) { n       sum++; n     } else { n       sum1++; n     } n     if (s[i] ==  ( ) { n       st.push(s[i]); n     } else if (s[i] ==  ) ) { n       if (st.size() > 0 && st.top() ==  ( ) { n         st.pop(); n       } else { n         c++; n       } n     } n   } n   if (sum != sum1) { n     cout <<  NO  << endl; n   } else if (st.size() <= 1 && c <= 1) { n     cout <<  YES  << endl; n   } else { n     cout <<  NO  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int nxt() { n   int x; n   scanf( %d , &x); n   return x; n } n int main() { n   mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n   int n = nxt(); n   vector<int> a(n); n   for (int i = 0; i < n; ++i) { n     a[i] = nxt(); n   } n   int s = accumulate((a).begin(), (a).end(), 0); n   int k = *max_element((a).begin(), (a).end()); n   int t = k * n; n   int need = 2 * s + 1; n   if (need > t) { n     k += (need - t + n - 1) / n; n   } n   printf( %d n , k); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   size_t found; n   vector<string> str; n   string a, b, c, in, out; n   long long i, j, k, m, n, l, t; n   cin >> t; n   while (t--) { n     cin >> in; n     for (i = 0, j = 0, k = 0; i < in.size(); i++) { n       if (in[i] ==  :  && i == 0) { n         i++; n         j = 1; n       } n       if (in[i] ==  : ) { n         b = in.substr(j, i - j); n         if (b ==   ) k++; n         str.push_back(b); n         j = i + 1; n       } n     } n     if (in[in.size() - 1] !=  : ) { n       b = in.substr(j, in.size() - j); n       str.push_back(b); n     } n     for (i = 0; i < str.size(); i++) { n       if (i > 0) cout <<  : ; n       if (str[i] ==   ) { n         for (j = 0; j < 8 - str.size() + k; j++) { n           if (j > 0) cout <<  : ; n           cout <<  0000 ; n         } n       } else { n         if (str[i].size() < 4) { n           for (l = str[i].size(); l < 4; l++) cout <<  0 ; n         } n         cout << str[i]; n       } n     } n     cout << endl; n     str.clear(); n     in.clear(); n     k = 0; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MaxN = 100000 + 5; n int N; n map<int, int> Numbers; n int main(void) { n   scanf( %d , &N); n   if (N % 2) { n     printf( NO n ); n     return 0; n   } n   for (int i = 0; i < N; i++) { n     int Number; n     scanf( %d , &Number); n     Numbers[Number]++; n   } n   while (!Numbers.empty()) { n     while (!Numbers.begin()->second) Numbers.erase(Numbers.begin()); n     int Number = Numbers.begin()->first; n     if (N == 2) { n       if (Numbers[Number + 1] == 1) n         printf( YES n ); n       else n         printf( NO n ); n       return 0; n     } n     if (Numbers[Number + 1] < 2) { n       printf( NO n ); n       return 0; n     } n     Numbers[Number]--; n     Numbers[Number + 1]--; n     N -= 2; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, i, c = 1; n   cin >> a >> b; n   if (b < a) a = b; n   for (i = 2; i <= a; i++) c *= i; n   cout << c << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, a, b, c, d, l = 1, v[1001]; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a >> b >> c >> d; n     v[i] = a + b + c + d; n     if (v[0] < v[i]) l++; n   } n   cout << l; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100 * 1000 + 5; n int n, m, cnt; n short int mark[N]; n bool visited[N]; n vector<int> in[N], out[N], comp[N]; n void input() { n   cin.tie(0); n   cin >> n >> m; n   int v, u; n   for (int i = 0; i < m; i++) { n     cin >> v >> u; n     v--, u--; n     out[v].push_back(u); n     in[u].push_back(v); n   } n   return; n } n void getComp(int v) { n   visited[v] = true; n   for (int u : in[v]) n     if (!visited[u]) getComp(u); n   for (int u : out[v]) n     if (!visited[u]) getComp(u); n   comp[cnt].push_back(v); n   return; n } n bool isDAG(int v) { n   bool res = true; n   mark[v]++; n   for (int u : out[v]) n     if (!mark[u]) n       res &= isDAG(u); n     else if (mark[u] == 1) n       return false; n   mark[v]++; n   return res; n } n void solve() { n   for (int i = 0; i < n; i++) n     if (!visited[i]) { n       getComp(i); n       cnt++; n     } n   for (int i = 0; i < cnt; i++) { n     bool check = true; n     for (int v : comp[i]) n       if (!mark[v]) check &= isDAG(v); n     if (check) n--; n   } n } n int main() { n   ios::sync_with_stdio(false); n   input(); n   solve(); n   cout << n << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using namespace std; n long long n, m, f, t, d, val; n vector<long long> a, c; n set<pair<long long, long long> > s; n int main() { n   cin.tie(0); n   cout.tie(0); n   ios::sync_with_stdio(0); n   cin >> n >> m; n   a.resize(n); n   c.resize(n); n   for (auto& i : a) cin >> i; n   for (auto& i : c) cin >> i; n   for (int i = 0; i < n; i++) s.insert(make_pair(c[i], i)); n   while (m--) { n     cin >> t >> d; n     t--; n     long long bill = 0; n     val = min(a[t], d); n     bill += val * c[t]; n     a[t] -= val; n     d -= val; n     while (d) { n       if (s.empty()) break; n       val = min(a[s.begin()->second], d); n       bill += val * (s.begin()->first); n       a[s.begin()->second] -= val; n       d -= val; n       if (a[s.begin()->second] == 0) s.erase(s.begin()); n     } n     if (d) n       cout <<  0 n ; n     else n       cout << bill <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n inline long long read() { n   register long long w = 1, x = 0, ch = getchar(); n   for (; ch <  0  || ch >  9 ; ch = getchar()) n     if (ch ==  - ) w = -1; n   for (; ch >=  0  && ch <=  9 ; ch = getchar()) x = x * 10 + ch -  0 ; n   return x * w; n } n const long long MAXN = (long long)7e2 + 10; n const long long MOD = (long long)1e9 + 7; n const long long INF = 0x3f3f3f3f3f3f3f3f; n char s[MAXN]; n long long n, ans, digit[MAXN], Dec[MAXN], divv[MAXN][10], f[MAXN][MAXN][10][2]; n inline void Init() { n   cin >> s + 1; n   n = strlen(s + 1); n   for (register long long i = 1; i <= n; i++) { n     digit[i] = s[i] -  0 ; n   } n } n inline void DP() { n   Dec[0] = 1; n   for (register long long i = 1; i <= n; i++) { n     Dec[i] = Dec[i - 1] * 10 % MOD; n   } n   for (register long long i = 0; i <= 9; i++) { n     f[0][0][i][1] = 1; n   } n   for (register long long i = 0; i < n; i++) { n     for (register long long j = 0; j <= i; j++) { n       for (register long long x = 1; x <= 9; x++) { n         for (register long long l = 0; l <= 1; l++) { n           for (register long long k = 0; k <= (l ? digit[i + 1] : 9); k++) { n             f[i + 1][j + (k >= x)][x][(k == digit[i + 1]) && l] += n                 f[i][j][x][l]; n             f[i + 1][j + (k >= x)][x][(k == digit[i + 1]) && l] %= MOD; n           } n         } n       } n     } n   } n   for (register long long x = 1; x <= 9; x++) { n     for (register long long j = 1; j <= n; j++) { n       for (register long long i = j; i <= n; i++) n         divv[j][x] = (divv[j][x] + f[n][i][x][1] + f[n][i][x][0]) % MOD; n     } n   } n   for (register long long i = 1; i <= n; i++) { n     for (register long long x = 1; x <= 9; x++) { n       ans = (ans + (divv[i][x] - divv[i][x + 1] + MOD) % MOD * Dec[i - 1] % n                        MOD * x % MOD) % n             MOD; n     } n   } n   printf( %lld n , ans); n } n signed main() { n   Init(); n   DP(); n   return 0; n } n


#include <bits/stdc++.h> n #pragma gcc optimize( Ofast ) n using namespace std; n inline void input(int &_x) { n   _x = 0; n   int _tmp = 1; n   char _tc = getchar(); n   while ((_tc <  0  || _tc >  9 ) && _tc !=  - ) _tc = getchar(); n   if (_tc ==  - ) _tc = getchar(), _tmp = -1; n   while (_tc >=  0  && _tc <=  9 ) _x = _x * 10 + (_tc - 48), _tc = getchar(); n   _x *= _tmp; n } n int root[400009], tag[400009]; n int aa[100009]; n inline void build(int id, int l, int r) { n   if (l == r) { n     root[id] = aa[l]; n     return; n   } n   int mid = (l + r) >> 1; n   build((id << 1), l, mid); n   build((id << 1) + 1, mid + 1, r); n   root[id] = max(root[(id << 1)], root[(id << 1) + 1]); n } n inline void push(int id) { n   root[id] += tag[id]; n   tag[(id << 1)] += tag[id]; n   tag[(id << 1) + 1] += tag[id]; n   tag[id] = 0; n } n inline void modify(int id, int l, int r, int ql, int qr, int v) { n   if (tag[id]) push(id); n   if (ql <= l && r <= qr) { n     tag[id] += v; n     push(id); n     return; n   } n   if (qr < l || r < ql) return; n   int mid = (l + r) >> 1; n   modify((id << 1), l, mid, ql, qr, v); n   modify((id << 1) + 1, mid + 1, r, ql, qr, v); n   root[id] = max(root[(id << 1)], root[(id << 1) + 1]); n } n int n, m; n vector<int> seg[100009]; n bitset<100009> vis; n inline bool cmp(int a, int b) { return aa[a] < aa[b]; } n signed main() { n   int ma = -1; n   input(n); n   input(m); n   for (int i = 1; i < n + 1; ++i) input(aa[i]), ma = max(ma, aa[i]); n   vector<pair<int, int> > v(m), w; n   for (auto &i : v) input(i.first), input(i.second); n   for (int i = 1; i < n + 1; ++i) w.emplace_back(aa[i], i); n   sort((w).begin(), (w).end()); n   int ans = -1, p = 0; n   build(1, 1, n); n   for (int i = 1; i < n + 1; ++i) n     for (int j = 0; j < m; ++j) n       if (v[j].first <= i && i <= v[j].second) seg[i].emplace_back(j); n   for (int _ = 0; _ < n; ++_) { n     int i = w[_].second; n     if (ans > ma - (aa[i] - (int)(seg[i]).size())) continue; n     int cnt = 0; n     for (auto &j : seg[i]) modify(1, 1, n, v[j].first, v[j].second, -1), ++cnt; n     if (root[1] - (aa[i] - cnt) > ans) { n       ans = root[1] - (aa[i] - cnt); n       p = i; n     } n     for (auto &j : seg[i]) modify(1, 1, n, v[j].first, v[j].second, 1); n   } n   cout << ans <<   n  << (int)(seg[p]).size() <<   n ; n   for (auto &i : seg[p]) cout << i + 1 <<    ; n } n


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


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   int ar1[100], ar2[100]; n   memset(ar1, 0, sizeof(ar1)); n   memset(ar2, 0, sizeof(ar2)); n   string s1, s2; n   cin >> s1 >> s2; n   int t; n   cin >> t; n   vector<pair<pair<int, char>, pair<int, char> > > v(t); n   for (int i = 0; i < t; i++) n     cin >> v[i].first.first >> v[i].first.second >> v[i].second.first >> n         v[i].second.second; n   sort(v.begin(), v.end()); n   for (int i = 0; i < t; i++) { n     if (v[i].first.second ==  a ) { n       if (ar2[v[i].second.first] < 2) { n         ar2[v[i].second.first] += (v[i].second.second ==  y ) ? 1 : 2; n         if (ar2[v[i].second.first] >= 2) n           cout << s2 <<     << v[i].second.first <<     << v[i].first.first n                << endl; n       } n     } else if (v[i].first.second ==  h ) { n       if (ar1[v[i].second.first] < 2) { n         ar1[v[i].second.first] += (v[i].second.second ==  y ) ? 1 : 2; n         if (ar1[v[i].second.first] >= 2) n           cout << s1 <<     n                <<     << v[i].second.first <<     << v[i].first.first << endl; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int arrmax = 200005; n char str[arrmax]; n int reversetimes[arrmax / 2]; n int main() { n   int m, a, sum, len; n   while (~scanf( %s %d , str, &m)) { n     len = strlen(str); n     memset(reversetimes, 0, 2 * len); n     while (m--) { n       scanf( %d , &a); n       reversetimes[a - 1]++; n     } n     sum = 0; n     for (int i = 0; i < len / 2; i++) { n       sum += reversetimes[i]; n       if (sum & 1) swap(str[i], str[len - i - 1]); n     } n     printf( %s n , str); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n string res[101]; n int dx[] = {0, 0, -1, 1}; n int dy[] = {-1, 1, 0, 0}; n inline bool in(int x, int y) { n   if (x < 0 || y < 0 || x >= n || y >= m) return 0; n   return 1; n } n int main(int argc, char **argv) { n   cin >> n >> m; n   for (int i = 0; i <= n - 1; i++) { n     cin >> res[i]; n   } n   int c = 1; n   for (int i = 0; i <= n - 1; i++) { n     c = i % 2; n     for (int j = 0; j <= m - 1; j++) { n       if (res[i][j] ==  . ) { n         if (c) n           res[i][j] =  B ; n         else n           res[i][j] =  W ; n       } n       if (c == 1) n         c = 0; n       else n         c = 1; n     } n   } n   for (int i = 0; i <= n - 1; i++) cout << res[i] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int x, y, a, b, c, ans; n int main() { n   cin >> x >> y; n   if (x > y) swap(x, y); n   a = b = c = x; n   while (true) { n     a = b + c - 1; n     ans++; n     if (a >= y) break; n     b = a + c - 1; n     ans++; n     if (b >= y) break; n     c = a + b - 1; n     ans++; n     if (c >= y) break; n   } n   cout << ans + 2; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, a[500011], L[500011], R[500011], aL[500011], aR[500011], n     ans[500011]; n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> n; n   for (long long i = 1; i <= n; i++) cin >> a[i]; n   stack<pair<long long, long long> > st; n   for (long long i = 1; i <= n; i++) { n     while (!st.empty() && st.top().first >= a[i]) st.pop(); n     if (st.empty()) n       L[i] = 0; n     else { n       L[i] = st.top().second; n     } n     st.push(pair<long long, long long>(a[i], i)); n   } n   while (!st.empty()) st.pop(); n   for (long long i = n; i >= 1; i--) { n     while (!st.empty() && st.top().first >= a[i]) st.pop(); n     if (st.empty()) n       R[i] = n + 1; n     else n       R[i] = st.top().second; n     st.push(pair<long long, long long>(a[i], i)); n   } n   for (long long i = 1; i <= n; i++) { n     aL[i] = aL[L[i]] + a[i] * (i - L[i]); n   } n   for (long long i = n; i >= 1; i--) { n     aR[i] = aR[R[i]] + a[i] * (R[i] - i); n   } n   long long A = 0, pos = 0; n   for (long long i = 1; i <= n; i++) { n     if (aL[i] + aR[i] - a[i] > A) { n       A = aL[i] + aR[i] - a[i]; n       pos = i; n     } n   } n   ans[pos] = a[pos]; n   long long cur_min = ans[pos]; n   for (long long i = pos - 1; i >= 1; i--) { n     ans[i] = min(a[i], cur_min); n     cur_min = min(a[i], cur_min); n   } n   cur_min = ans[pos]; n   for (long long i = pos + 1; i <= n; i++) { n     ans[i] = min(a[i], cur_min); n     cur_min = min(a[i], cur_min); n   } n   for (long long i = 1; i <= n; i++) cout << ans[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n; n   string s; n   cin >> n; n   cin >> s; n   map<char, long long int> M; n   long long int i; n   for (i = 0; i < n; i++) { n     M[s[i]]++; n   } n   long long int z = n / 3; n   if (M[ 0 ] < z) { n     for (i = 0; i < n; i++) { n       if (M[s[i]] > z && s[i] !=  0  && M[ 0 ] < z) { n         M[s[i]]--; n         s[i] =  0 ; n         M[ 0 ]++; n       } n     } n   } n   if (M[ 2 ] < z) { n     for (i = n - 1; i >= 0; i--) { n       if (M[s[i]] > z && s[i] !=  2  && M[ 2 ] < z) { n         M[s[i]]--; n         s[i] =  2 ; n         M[ 2 ]++; n       } n     } n   } n   if (M[ 1 ] < z) { n     for (i = n - 1; i >= 0; i--) { n       if (M[s[i]] > z && s[i] ==  0  && M[ 1 ] < z) { n         M[s[i]]--; n         s[i] =  1 ; n         M[ 1 ]++; n       } n     } n     for (i = 0; i < n; i++) { n       if (M[s[i]] > z && s[i] ==  2  && M[ 1 ] < z) { n         M[s[i]]--; n         s[i] =  1 ; n         M[ 1 ]++; n       } n     } n   } n   cout << s << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n char buf[100], name[100], op[100], tmp[100]; n struct widget { n   long long x, y; n   bool finish; n   int type, border, spacing; n   vector<int> w; n   widget(long long xx = 0, long long yy = 0, int t = 0) { n     x = xx; n     y = yy; n     type = t; n     w = vector<int>(); n     finish = false; n     border = spacing = 0; n   } n   void gao(){}; n } wid[110]; n map<string, int> mp; n void gao(int id) { n   if (wid[id].finish || wid[id].type == 0) return; n   long long &x = wid[id].x; n   long long &y = wid[id].y; n   x = y = 0; n   for (int i = 0; i < ((int)wid[id].w.size()); ++i) { n     int z = wid[id].w[i]; n     gao(z); n     if (wid[id].type == 1) { n       x += wid[z].x + wid[id].spacing; n       y = max(y, wid[z].y); n     } else { n       y += wid[z].y + wid[id].spacing; n       x = max(x, wid[z].x); n     } n   } n   wid[id].finish = true; n   if (((int)wid[id].w.size()) == 0) return; n   if (wid[id].type == 1) { n     x += wid[id].border * 2 - wid[id].spacing; n     y += wid[id].border * 2; n   } else { n     y += wid[id].border * 2 - wid[id].spacing; n     x += wid[id].border * 2; n   } n } n void solve() { n   mp.clear(); n   int x, y, tot = 0; n   for (int i = 0; i < n; ++i) { n     scanf( %s , buf); n     if (string(buf) ==  Widget ) { n       scanf( %s , buf); n       sscanf(buf,  %[^(](%d,%d) , name, &x, &y); n       mp[name] = tot; n       wid[tot++] = widget(x, y, 0); n     } else if (string(buf) ==  HBox ) { n       scanf( %s , buf); n       mp[buf] = tot; n       wid[tot++] = widget(-1, -1, 1); n     } else if (string(buf) ==  VBox ) { n       scanf( %s , buf); n       mp[buf] = tot; n       wid[tot++] = widget(-1, -1, 2); n     } else { n       sscanf(buf,  %[^.].%[^(](%[^)] , name, op, tmp); n       int id = mp[name]; n       if (string(op) ==  set_border ) { n         sscanf(tmp,  %d , &x); n         wid[id].border = x; n       } else if (string(op) ==  set_spacing ) { n         sscanf(tmp,  %d , &x); n         wid[id].spacing = x; n       } else { n         wid[id].w.push_back(mp[tmp]); n       } n     } n   } n   map<string, int>::iterator it = mp.begin(); n   for (; it != mp.end(); ++it) { n     int id = it->second; n     gao(id); n     printf( n          %s   n          %I64d  n             n          %I64d  n           n , n         it->first.c_str(), wid[id].x, wid[id].y); n   } n   puts(  ); n } n int main() { n   while (scanf( %d , &n) != EOF) { n     solve(); n   } n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n bool b[150000] = {0}; n bool dfs(int n, vector<set<int>>& v) { n   b[n] = 1; n   for (auto itr = v[n].begin(); itr != v[n].end(); itr++) { n     if (!b[*itr]) n       if (v[n] != v[*itr]) return 0; n     if (!b[*itr]) dfs(*itr, v); n   } n   return 1; n } n int main() { n   int n, m; n   cin >> n >> m; n   vector<set<int>> v(n); n   for (int i = 0; i < n; i++) v[i].insert(i); n   for (int i = 0; i < m; i++) { n     int a, b; n     cin >> a >> b; n     a--; n     b--; n     v[a].insert(b); n     v[b].insert(a); n   } n   bool r = 1; n   for (int i = 0; i < n && r; i++) { n     if (!b[i]) { n       if (!dfs(i, v)) r = 0; n     } n   } n   cout << (r ?  YES  :  NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 3 * 1e5 + 10, maxm = (1 << 17), mod = 1e9 + 7; n const double PI = 3.14159265359; n vector<int> g[maxn]; n int d[maxn]; n bool r[maxn]; n set<int> s; n int main() { n   ios::sync_with_stdio(0); n   int n, a, b, m; n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     cin >> a >> b; n     a--, b--; n     g[a].push_back(b), g[b].push_back(a); n   } n   for (int i = 0; i < n; i++) { n     d[i] = g[i].size(); n     if (g[i].size() > 1) s.insert(i); n   } n   while (s.size()) { n     a = *s.begin(); n     s.erase(s.begin()); n     r[a] = !r[a], d[a] = g[a].size() - d[a]; n     for (int i = 0; i < g[a].size(); i++) { n       b = g[a][i]; n       d[b] += (r[a] == r[b] ? 1 : -1); n       if (d[b] > 1) n         s.insert(b); n       else n         s.erase(b); n     } n   } n   for (int i = 0; i < n; i++) cout << r[i]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   int t, d, p, id; n } s[110]; n int dp[2010]; n vector<int> v[2010]; n int cmp(node a, node b) { return a.d < b.d; } n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d%d%d , &s[i].t, &s[i].d, &s[i].p), s[i].id = i; n   } n   sort(s + 1, s + n + 1, cmp); n   for (int i = 1; i <= n; i++) { n     if (s[i].d - s[i].t <= 0) continue; n     for (int j = s[i].d - 1; j - s[i].t >= 0; j--) { n       if (dp[j] < dp[j - s[i].t] + s[i].p) { n         dp[j] = dp[j - s[i].t] + s[i].p; n         v[j] = v[j - s[i].t]; n         v[j].push_back(s[i].id); n       } n     } n   } n   int best = 0, bestI = 0; n   for (int i = 0; i <= 2000; i++) { n     if (dp[i] > best) { n       best = dp[i]; n       bestI = i; n     } n   } n   printf( %d n , best); n   printf( %d n , v[bestI].size()); n   for (int i = 0; i < v[bestI].size(); i++) printf( %d  , v[bestI][i]); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   vector<int> a; n   int cnt = 0; n   int sum = 0; n   for (int i = 0; i < 2 * n - 1; ++i) { n     int x; n     cin >> x; n     a.push_back(abs(x)); n     if (x < 0) cnt++; n     sum += abs(x); n   } n   cnt %= n; n   queue<int> q; n   vector<bool> used(n + 1, false); n   used[cnt] = 1; n   q.push(cnt); n   int c = cnt; n   while (!q.empty()) { n     int u = q.front(); n     q.pop(); n     if (!used[n - u]) { n       used[n - u] = 1; n       q.push(n - u); n       c = min(c, n - u); n     } n     for (int i = 1; i <= u; ++i) { n       int to = u - i + n - i; n       to %= n; n       if (to >= 0 && !used[to]) { n         used[to] = 1; n         q.push(to); n         c = min(c, to); n       } n     } n   } n   sort(a.begin(), a.end()); n   if (c == 0) { n     cout << sum; n   } else { n     for (int i = 0; i < c; ++i) { n       sum -= 2 * a[i]; n     } n     cout << sum; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(int argc, char **argv) { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int m, n; n   cin >> n >> m; n   vector<string> s(n); n   vector<pair<int, int>> t(n, {-1, -1}); n   for (int i = 0; i < n; i++) cin >> s[i]; n   bool ok = true; n   for (int i = 0; i < n; i++) { n     int k = 0; n     for (int j = 0; j < m; j++) { n       if (s[i][j] ==  X ) { n         if (!j || s[i][j - 1] ==  . ) k++; n         if (t[i].first < 0) t[i].first = j; n         t[i].second = j; n       } n     } n     if (k > 1) ok = false; n   } n   int k = 0; n   pair<int, int> u({-1, -1}); n   for (int i = 0; i < n; i++) { n     if (t[i].first < 0) continue; n     if (!i || t[i - 1].first < 0) k++; n     if (u.first < 0) u = t[i]; n     if (u != t[i]) ok = false; n   } n   if (k > 1) ok = false; n   cout << (ok ?  YES  :  NO ) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ii = pair<int, int>; n using vi = vector<int>; n using vvi = vector<vi>; n using vii = vector<ii>; n using vb = vector<bool>; n using ll = long long; n template <typename A, typename B> n istream &operator>>(istream &in, pair<A, B> &p) { n   in >> p.first >> p.second; n   return in; n } n template <typename T> n istream &operator>>(ifstream &in, vector<T> &v) { n   for (int i = 0; i < (int)v.size(); ++i) { n     in >> v[i]; n   } n   return in; n } n int main() { n   int n, k; n   cin >> n >> k; n   vector<set<int>> times(n); n   for (int i = 0; i < k; ++i) { n     int x; n     cin >> x; n     x--; n     times[x].insert(i); n   } n   int res = 0; n   for (int x = 0; x < n; ++x) { n     if (times[x].empty()) { n       res += 3; n       if (x == 0) res--; n       if (x == n - 1) res--; n     } else { n       int t = *times[x].begin(); n       if (x != n - 1 && times[x + 1].lower_bound(t) == times[x + 1].end()) n         res++; n       if (x != 0 && times[x - 1].lower_bound(t) == times[x - 1].end()) res++; n     } n   } n   cout << res << endl; n } n


#include <bits/stdc++.h> n using namespace std; n bool fun(int x, int y) { return x > y; } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   int n, s; n   cin >> n >> s; n   if (s <= n) { n     cout << 1 <<   n ; n   } n   if (s > n && s % n == 0) { n     cout << s / n <<   n ; n   } n   if (s > n && s % n != 0) { n     cout << s / n + 1 <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n ostream& operator<<(ostream& os, vector<T> V) { n   os <<  [  ; n   for (auto v : V) os << v <<    ; n   return os <<  ] ; n } n template <class T> n ostream& operator<<(ostream& os, set<T> S) { n   os <<  {  ; n   for (auto s : S) os << s <<    ; n   return os <<  } ; n } n template <class L, class R> n ostream& operator<<(ostream& os, pair<L, R> P) { n   return os <<  (  << P.first <<  ,  << P.second <<  ) ; n } n template <class L, class R> n ostream& operator<<(ostream& os, map<L, R> M) { n   os <<  {  ; n   for (auto m : M) os <<  (  << m.F <<  :  << m.S <<  ) ; n   return os <<  } ; n } n template <typename Arg1> n void __f(const char* name, Arg1&& arg1) { n   cout << name <<   :   << arg1 << std::endl; n } n template <typename Arg1, typename... Args> n void __f(const char* names, Arg1&& arg1, Args&&... args) { n   const char* comma = strchr(names + 1,  , ); n   cout.write(names, comma - names) <<   :   << arg1 <<   |  ; n   __f(comma + 1, args...); n } n long long const M = 1e9; n bool check(long long v, long long n, long long k) { n   long long s = n; n   long long d = k; n   while (n / k >= 1) { n     s = s + n / k; n     k = k * d; n   } n   return (s >= v); n } n int main() { n   long long n, k; n   cin >> n >> k; n   long long l = 0; n   long long r = M; n   while (r - l > 1) { n     long long mid = (l + r) >> 1; n     if (check(n, mid, k)) n       r = mid; n     else n       l = mid; n   } n   cout << r <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long double ans; n long long one, two; n map<pair<long long, long long>, pair<long long, long long> > o, t; n map<pair<long long, long long>, pair<long long, long long> >::iterator ii; n void add(long long a, long long b, long long c, long long ind) { n   pair<long long, long long> p = make_pair(a, b); n   if (c > o[p].first && ind != t[p].second) { n     t[p] = o[p]; n     o[p] = {c, ind}; n   } n   if (c > t[p].first && ind != o[p].second) t[p] = {c, ind}; n } n int main() { n   long long n, i, j; n   cin >> n; n   for (i = 1; i <= n; i++) { n     long long a, b, c; n     cin >> a >> b >> c; n     long long t = min(a, min(b, c)); n     long double cur = 1.0 * t / 2; n     if (cur > ans) { n       ans = cur; n       one = i; n     } n     add(a, b, c, i); n     add(a, c, b, i); n     add(b, a, c, i); n     add(b, c, a, i); n     add(c, a, b, i); n     add(c, b, a, i); n   } n   for (ii = t.begin(); ii != t.end(); ii++) { n     long long cur = ii->second.first + o[ii->first].first; n     long long Min = min(cur, min(ii->first.first, ii->first.second)); n     long double curans = 1.0 * Min / 2; n     if (curans > ans) { n       ans = curans; n       one = ii->second.second; n       two = o[ii->first].second; n     } n   } n   if (one != 0 && two != 0) n     cout <<  2 n  << one <<     << two; n   else n     cout <<  1 n  << one; 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   int t; n   cin >> t; n   for (int i = 0; i < t; ++i) { n     int n, a[150000]{}; n     cin >> n; n     for (int j = 0; j < n; ++j) { n       cin >> a[j]; n     } n     int min = a[n - 1], cnt = 0; n     for (int j = n - 2; j >= 0; --j) { n       if (a[j] < min) { n         min = a[j]; n       } else if (a[j] > min) { n         ++cnt; n       } n     } n     cout << cnt <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n long long k; n long long a[21][21]; n map<long long, int> mp[21][21]; n int Color[21][21]; n queue<pair<int, int>> q; n void BFS(int x, int y, int depthMax, vector<pair<int, int>> d) { n   Color[x][y] = 1; n   mp[x][y][a[x][y]] = 1; n   q.push(make_pair(x, y)); n   while (!q.empty()) { n     tie(x, y) = q.front(); n     q.pop(); n     if (Color[x][y] == depthMax) { n       continue; n     } n     for (auto i : d) { n       int u = x + i.first, v = y + i.second; n       if (u > 0 && u <= n && v > 0 && v <= m) { n         for (auto it : mp[x][y]) { n           mp[u][v][a[u][v] ^ it.first] += it.second; n         } n         if (Color[u][v] == 0) { n           Color[u][v] = Color[x][y] + 1; n           q.push(make_pair(u, v)); n         } n       } n     } n   } n } n long long GetMap(map<long long, int> &mp, long long x) { n   auto it = mp.find(x); n   return (it == mp.end() ? 0 : it->second); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m >> k; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       cin >> a[i][j]; n     } n   } n   if (n == 1 && m == 1) { n     cout << (a[1][1] == k ? 1 : 0); n     return 0; n   } n   int numStep = (n + m - 1) / 2; n   BFS(1, 1, numStep, n       vector<pair<int, int>>({make_pair(1, 0), make_pair(0, 1)})); n   BFS(n, m, (n + m - 1) - numStep, n       vector<pair<int, int>>({make_pair(-1, 0), make_pair(0, -1)})); n   long long Ans = 0; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       if (i + j - 1 == numStep && Color[i][j] == numStep) { n         for (auto x : mp[i][j]) { n           long long tmp = 0; n           if (i + 1 <= n) { n             tmp += GetMap(mp[i + 1][j], x.first ^ k); n           } n           if (j + 1 <= m) { n             tmp += GetMap(mp[i][j + 1], x.first ^ k); n           } n           Ans += 1LL * tmp * x.second; n         } n         break; n       } n     } n   } n   cout << Ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, a, b[101]; n void read() { n   int c, d, k = 0; n   cin >> n >> m; n   for (int i = 1; i <= n; i++) cin >> a, b[i] = a + b[i - 1]; n   for (int i = 0; i < m; i++) { n     cin >> c >> d; n     k += (b[d] - b[c - 1] > 0 ? b[d] - b[c - 1] : 0); n   } n   cout << k; n } n int main() { read(); } n