#include <bits/stdc++.h> n using namespace std; n const long long MOD = (long long)1e9 + 7; n const long long MOD1 = 2286661337; n const long long MOD2 = 998244353; n const int INF = (int)1e9 + 7; n const double EPS = 1e-7; n const int N = (int)2e5; n int n; n long long a[N], b[N]; n bool is_same_() { n   for (int i = 0; i < n; ++i) { n     if (a[i] != b[i]) { n       return false; n     } n   } n   return true; n } n bool is_up() { n   for (int i = 1; i < n; ++i) { n     if (b[i] <= b[i - 1]) { n       return false; n     } n   } n   return true; n } n bool is_down() { n   for (int i = 1; i < n; ++i) { n     if (b[i] >= b[i - 1]) { n       return false; n     } n   } n   return true; n } n bool is_bad() { n   for (int i = 0; i < n; ++i) { n     if (b[i] <= 0) { n       return true; n     } n   } n   return false; n } n bool is_rev_same() { n   for (int i = 0; i < n; ++i) { n     if (a[i] != b[n - 1 - i]) { n       return false; n     } n   } n   return true; n } n bool is_uncomp() { n   for (int i = 0; i < n; ++i) { n     if (a[i] > b[i]) { n       return true; n     } n   } n   return false; n } n void solve() { n   int cnt = 0; n   string ans; n   int prev_ = -1; n   while (!is_same_()) { n     if (is_rev_same()) { n       ans.push_back( R ); n       break; n     } n     if (is_bad()) { n       cnt = -1; n       break; n     } n     if (is_up()) { n       for (int i = n - 1; i >= 0; --i) { n         b[i] = (i ? b[i] - b[i - 1] : b[i]); n       } n       if (prev_ != 0) { n         if (prev_ != -1) { n           ans.push_back( R ); n         } n         prev_ = 0; n       } n       ans.push_back( P ); n     } else if (is_down()) { n       for (int i = 0; i < n; ++i) { n         b[i] = (i == n - 1 ? b[i] : b[i] - b[i + 1]); n       } n       if (prev_ != 1) { n         ans.push_back( R ); n         prev_ = 1; n       } n       ans.push_back( P ); n     } else { n       cnt = -1; n       break; n     } n     ++cnt; n   } n   if (prev_ == 1) { n     ans.push_back( R ); n   } n   reverse(ans.begin(), ans.end()); n   if (cnt == -1) { n     printf( IMPOSSIBLE ); n   } else if (cnt > N) { n     printf( BIG n%d , cnt); n   } else { n     printf( SMALL n%d n%s , ans.size(), ans.c_str()); n   } n } n signed main() { n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) { n     scanf( %lld , &a[i]); n   } n   for (int i = 0; i < n; ++i) { n     scanf( %lld , &b[i]); n   } n   if (n == 1) { n     if (a[0] == b[0]) { n       printf( SMALL n0 ); n     } else { n       printf( IMPOSSIBLE ); n     } n   } else if (n == 2) { n     long long cnt = 0; n     int prev_ = -1; n     string ans =   ; n     while (!is_same_()) { n       if (is_rev_same()) { n         ans.push_back( R ); n         break; n       } n       if (is_uncomp()) { n         ans.push_back( R ); n         reverse(b, b + n); n         if (is_uncomp()) { n           cnt = -1; n           break; n         } n       } n       long long x = b[0], y = b[1]; n       if (x <= 0 || y <= 0) { n         cnt = -1; n         break; n       } n       if (x < y) { n         if (prev_ != 0) { n           if (prev_ != -1) { n             ans.push_back( R ); n           } n           prev_ = 0; n         } n         long long cur_cnt = y / x; n         if (x == a[0]) { n           cur_cnt = min(cur_cnt, (y - a[1] + x - 1) / x); n         } n         if (ans.size() + cur_cnt <= N) { n           for (int i = 0; i < cur_cnt; ++i) { n             ans.push_back( P ); n           } n         } n         cnt += cur_cnt; n         y -= x * cur_cnt; n       } else if (x > y) { n         if (prev_ != 1) { n           ans.push_back( R ); n           prev_ = 1; n         } n         long long cur_cnt = x / y; n         if (y == a[1]) { n           cur_cnt = min(cur_cnt, (x - a[0] + y - 1) / y); n         } n         if (ans.size() + cur_cnt <= N) { n           for (int i = 0; i < cur_cnt; ++i) { n             ans.push_back( P ); n           } n         } n         cnt += cur_cnt; n         x -= y * cur_cnt; n       } else { n         cnt = -1; n         break; n       } n       b[0] = x, b[1] = y; n     } n     if (prev_ == 1) { n       ans.push_back( R ); n     } n     reverse(ans.begin(), ans.end()); n     if (cnt == -1) { n       printf( IMPOSSIBLE ); n     } else if (cnt > N) { n       printf( BIG n%lld , cnt); n     } else { n       printf( SMALL n%d n%s , ans.size(), ans.c_str()); n     } n   } else { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:268435456,268435456 ) n using namespace std; n int above[41][41], dp[41][41][41][41]; n char s[42]; n int main() { n   int n, m, q, s1, s2, s3, s4; n   scanf( %d %d %d , &n, &m, &q); n   for (int i = 1; i <= n; i++) { n     scanf( %s , s + 1); n     for (int j = 1; j <= m; j++) n       if (s[j] ==  0 ) above[i][j] += above[i - 1][j] + 1; n   } n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) n       for (int k = i; k <= n; k++) n         for (int l = j; l <= m; l++) { n           dp[i][j][k][l] = n               dp[i][j][k - 1][l] + dp[i][j][k][l - 1] - dp[i][j][k - 1][l - 1]; n           int temp = min(above[k][l], k - i + 1); n           dp[i][j][k][l] += temp; n           for (int z = l - 1; z >= j; z--) { n             temp = min(temp, above[k][z]); n             if (temp == 0) n               break; n             else n               dp[i][j][k][l] += temp; n           } n         } n   for (int i = 0; i < q; i++) n     scanf( %d %d %d %d , &s1, &s2, &s3, &s4), n         printf( %d n , dp[s1][s2][s3][s4]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n vector<int> vec[300000]; n int mn(int x); n int mx(int x) { n   if (!vec[x].size()) return 0; n   int ret = n; n   for (int i = (0); i < (vec[x].size()); i++) ret = min(ret, mn(vec[x][i])); n   return ret; n } n int mn(int x) { n   if (!vec[x].size()) return 0; n   int ret = 0; n   for (int i = (0); i < (vec[x].size()); i++) ret += mx(vec[x][i]) + 1; n   return ret - 1; n } n int main() { n   scanf( %d , &n); n   for (int i = (0); i < (n - 1); i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     x--; n     y--; n     vec[x].push_back(y); n   } n   int leaf = 0; n   for (int i = (0); i < (n); i++) n     if (!vec[i].size()) leaf++; n   printf( %d %d n , leaf - mx(0), 1 + mn(0)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int T, n, m, sa[40010], sb[40010], ls, du[201], ans; n bool g[201][201]; n int getint() { n   int w = 0; n   bool q = 0; n   char c = getchar(); n   while ((c >  9  || c <  0 ) && c !=  - ) c = getchar(); n   if (c ==  - ) c = getchar(), q = 1; n   while (c >=  0  && c <=  9 ) w = w * 10 + c -  0 , c = getchar(); n   return q ? -w : w; n } n void dfs(int u) { n   while (du[u]) { n     for (int i = 1; i <= n; i++) n       if (g[u][i]) { n         ls++; n         sa[ls] = u; n         sb[ls] = i; n         du[u]--, du[i]--; n         g[u][i] = g[i][u] = 0; n         u = i; n         break; n       } n   } n } n int main() { n   T = getint(); n   while (T--) { n     ans = n = getint(); n     m = getint(); n     for (int i = 1, x, y; i <= m; i++) { n       x = getint(); n       y = getint(); n       g[x][y] = g[y][x] = 1; n       du[x]++; n       du[y]++; n     } n     for (int i = 1; i <= n; i++) ans -= du[i] & 1; n     printf( %d n , ans); n     for (int i = 1; i <= n; i++) n       if (du[i] & 1) n         while (du[i]) dfs(i); n     for (int i = 1; i <= n; i++) n       while (du[i]) dfs(i); n     for (int i = 1; i <= ls; i++) printf( %d %d n , sa[i], sb[i]); n     ls = 0; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long cross(pair<long long, long long> a, pair<long long, long long> b, n                 pair<long long, long long> c) { n   return abs((b.first - a.first) * (c.second - a.second) - n              (b.second - a.second) * (c.first - a.first)); n } n vector<pair<long long, long long> > P; n int n; n long long s; n int main() { n   cin >> n >> s; n   for (int i = 1; i <= n; i++) { n     pair<long long, long long> p; n     cin >> p.first >> p.second; n     P.push_back(p); n   } n   sort(P.begin(), P.end()); n   pair<long long, long long> a, b, c; n   a = P[0], b = P[1], c = P[2]; n   long long ans = cross(a, b, c); n   int flag = 1; n   while (flag) { n     flag = 0; n     for (int i = 0; i < P.size(); i++) { n       long long tmp = cross(P[i], b, c); n       if (tmp > ans) { n         ans = tmp; n         a = P[i]; n         flag = 1; n       } n     } n     for (int i = 0; i < P.size(); i++) { n       long long tmp = cross(a, P[i], c); n       if (tmp > ans) { n         ans = tmp; n         b = P[i]; n         flag = 1; n       } n     } n     for (int i = 0; i < P.size(); i++) { n       long long tmp = cross(a, b, P[i]); n       if (tmp > ans) { n         ans = tmp; n         c = P[i]; n         flag = 1; n       } n     } n   } n   cout << a.first + b.first - c.first <<     << a.second + b.second - c.second n        << endl; n   cout << a.first + c.first - b.first <<     << a.second + c.second - b.second n        << endl; n   cout << b.first + c.first - a.first <<     << b.second + c.second - a.second n        << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   s +=  0 ; n   int c = 1; n   char sc = s[0]; n   bool xd = false; n   string res =   ; n   for (int i = 1; i < s.size(); i++) { n     if (sc != s[i]) { n       if (c == 1) { n         res += sc; n         xd = false; n       } else if (c >= 2 && xd) { n         xd = false; n         res += sc; n       } else { n         xd = true; n         res += sc; n         res += sc; n       } n       c = 1; n       sc = s[i]; n     } else n       c++; n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int64_t mod = 1e9 + 7; n int64_t mod1 = 1e9 + 6; n int64_t power(int64_t a, int64_t b) { n   if (b == 0) n     return 1; n   else if (b % 2 == 0) n     return power((((a % mod) * (a % mod)) % mod), b / 2) % mod; n   else n     return ((a % mod) * (power((((a % mod) * (a % mod)) % mod), b / 2) % mod)) % n            mod; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int64_t n; n   cin >> n; n   int64_t a[n + 1], b[n + 1], c[n + 1], d[n + 1], e[n + 1]; n   for (int64_t i = 1; i <= n; ++i) cin >> a[i] >> b[i] >> c[i] >> d[i] >> e[i]; n   if (n <= 2) { n     cout << n <<   n ; n     for (int64_t i = 1; i <= n; ++i) cout << i <<   n ; n     return 0; n   } n   bool vis[n + 1]; n   memset(vis, false, sizeof(vis)); n   for (int64_t i = 1; i <= n; ++i) { n     if (vis[i]) continue; n     int64_t ch = 0; n     for (int64_t j = 1; j <= n; ++j) { n       if (i == j) continue; n       for (int64_t k = j + 1; k <= n; ++k) { n         if (i == k or k == j) continue; n         int64_t s = n             (a[j] - a[i]) * (a[k] - a[i]) + (b[j] - b[i]) * (b[k] - b[i]) + n             (c[j] - c[i]) * (c[k] - c[i]) + (d[j] - d[i]) * (d[k] - d[i]) + n             (e[j] - e[i]) * (e[k] - e[i]); n         if (s > 0) { n           ch = 1; n           break; n         } n       } n       if (ch) break; n     } n     if (ch == 0) { n       cout << 1 <<   n ; n       cout << i; n       return 0; n     } n   } n   cout << 0; n } n


#define rep(i,n) for(int i=0;i<(int)(n);i++) n #define ALL(v) v.begin(),v.end() n typedef long long ll; n   n #include<bits/stdc++.h> n using namespace std; n   n int main(){ n   ios::sync_with_stdio(false); n   std::cin.tie(nullptr); n    n   int t; n   cin>>t; n   while(t--){ n     int xa,ya,xb,yb,xf,yf; n     cin>>xa>>ya>>xb>>yb>>xf>>yf; n      n     if(xa==xb && xf==xa && (yf-ya)*(yf-yb)<0) cout<<abs(ya-yb)+2<<endl; n     else if(ya==yb && yf==ya && (xf-xa)*(xf-xb)<0) cout<<abs(xa-xb)+2<<endl; n     else cout<<abs(xa-xb)+abs(ya-yb)<<endl; n      n      n      n      n   } n    n    n      n   return 0; n }


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 400; n int n, m, dp[MAXN + 5][MAXN + 5]; n char s[MAXN + 5], t[MAXN + 5]; n inline void ckmax(int &x, int y) { x = x > y ? x : y; } n int main() { n   int T; n   scanf( %d , &T); n   while (T--) { n     scanf( %s%s , s + 1, t + 1); n     n = strlen(s + 1); n     m = strlen(t + 1); n     bool ok = 0; n     for (int i = 1; i <= m; ++i) { n       memset(dp, -1, sizeof(dp)); n       dp[0][0] = 0; n       for (int j = 1; j <= n; ++j) { n         for (int k = 0; k <= i; ++k) { n           if (dp[j - 1][k] != -1) { n             ckmax(dp[j][k], dp[j - 1][k] + (i + dp[j - 1][k] + 1 <= m && n                                             s[j] == t[i + dp[j - 1][k] + 1])); n           } n           if (k >= 1 && s[j] == t[k]) { n             ckmax(dp[j][k], dp[j - 1][k - 1]); n           } n         } n       } n       if (dp[n][i] == m - i) { n         ok = 1; n         break; n       } n     } n     if (ok) n       puts( YES ); n     else n       puts( NO ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n   string s1, s2; n   cin >> s1 >> s2; n   if (s1.size() > s2.size()) n     cout << s1.size(); n   else if (s2.size() > s1.size()) n     cout << s2.size(); n   else if (s1 == s2) n     cout <<  -1 ; n   else n     cout << s1.size(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5e5 + 5; n int n, k, par[N]; n int64_t f[N], g[N]; n vector<pair<int, int> > ke[N]; n vector<int64_t> c[N]; n void dfs(int u) { n   int64_t sum = 0; n   for (auto e : ke[u]) n     if (e.second != par[u]) { n       int v = e.second, w = e.first; n       par[v] = u; n       dfs(v); n       sum += f[v]; n       c[u].push_back(g[v] + w - f[v]); n     } n   sort(c[u].begin(), c[u].end(), greater<int64_t>()); n   for (int i = 0; i < k - 1 && i < (int)c[u].size() && c[u][i] > 0; ++i) n     sum += c[u][i]; n   f[u] = g[u] = sum; n   if (k <= (int)c[u].size() && c[u][k - 1] > 0) f[u] += c[u][k - 1]; n } n int main() { n   int T; n   scanf( %d , &T); n   while (T--) { n     scanf( %d%d , &n, &k); n     for (int i = 1; i < n; ++i) { n       int u, v, w; n       scanf( %d%d%d , &u, &v, &w); n       ke[u].push_back(make_pair(w, v)); n       ke[v].push_back(make_pair(w, u)); n     } n     dfs(1); n     printf( %I64d n , f[1]); n     for (int i = 1; i <= n; ++i) ke[i].clear(), c[i].clear(), par[i] = 0; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 5; n namespace SBT { n const int _N = N << 2 | 1; n int lc[_N], rc[_N], siz[_N], val[_N], rp[_N], rsiz[_N]; n int tp, dl[_N], tot, q[_N], qc[_N]; n const double alpha = 0.55; n inline int _new(int v, int ct) { n   int u = tp ? dl[tp--] : ++tot; n   lc[u] = rc[u] = 0, val[u] = v, siz[u] = rp[u] = ct, rsiz[u] = 1; n   return u; n } n inline void upt(int x) { n   siz[x] = siz[lc[x]] + siz[rc[x]] + rp[x]; n   rsiz[x] = rsiz[lc[x]] + rsiz[rc[x]] + 1; n } n inline void zig(int &u) { n   int v = lc[u]; n   lc[u] = rc[v]; n   rc[v] = u; n   upt(u); n   upt(v); n   u = v; n } n inline void zag(int &u) { n   int v = rc[u]; n   rc[u] = lc[v]; n   lc[v] = u; n   upt(u); n   upt(v); n   u = v; n } n inline void ins(int &u, int v, int ct = 1) { n   if (!u) { n     u = _new(v, ct); n     return; n   } n   siz[u] += ct; n   if (val[u] == v) n     rp[u] += ct; n   else if (v < val[u]) { n     ins(lc[u], v, ct); n     if (rsiz[lc[u]] > rsiz[u] * alpha) zig(u); n   } else { n     ins(rc[u], v, ct); n     if (rsiz[rc[u]] > rsiz[u] * alpha) zag(u); n   } n   upt(u); n } n inline void del(int &u, int v) { n   if (!u) return; n   if (val[u] == v) { n     if (rp[u] > 1) n       --rp[u], --siz[u]; n     else if (!lc[u] || !rc[u]) n       dl[++tp] = u, u = lc[u] | rc[u]; n     else if (rsiz[lc[u]] > rsiz[rc[u]]) n       zig(u), del(u, v); n     else n       zag(u), del(u, v); n     if (u) upt(u); n     return; n   } n   if (v < val[u]) n     del(lc[u], v); n   else n     del(rc[u], v); n   upt(u); n } n int rank(int u, int v) { n   if (!u) return 0; n   if (val[u] == v) return siz[lc[u]] + rp[u]; n   if (v < val[u]) return rank(lc[u], v); n   return rank(rc[u], v) + siz[lc[u]] + rp[u]; n } n inline int range(int u, int l, int r) { return rank(u, r) - rank(u, l - 1); } n void us_dfs(int u) { n   if (lc[u]) us_dfs(lc[u]); n   q[++q[0]] = val[u], qc[q[0]] = rp[u]; n   dl[++tp] = u; n   if (rc[u]) us_dfs(rc[u]); n } n inline void mge(int &u, int &v) { n   if (rsiz[u] < rsiz[v]) swap(u, v); n   q[0] = 0; n   us_dfs(v); n   v = 0; n   for (int i = 1; i <= q[0]; ++i) ins(u, q[i], qc[i]); n } n }  // namespace SBT n using SBT::del; n using SBT::ins; n using SBT::mge; n using SBT::range; n int n, root, tot; n int fi[N], ne[N], to[N]; n int fa[N], siz[N], ans[N]; n inline int read() { n   int s = 0, w = 1; n   char ch = getchar(); n   for (; !isdigit(ch); ch = getchar()) n     if (ch ==  - ) w = -1; n   for (; isdigit(ch); ch = getchar()) s = (s << 1) + (s << 3) + (ch ^ 48); n   return s * w; n } n void print(int x) { n   if (x < 0) { n     putchar( - ); n     x = -x; n   } n   if (x > 9) print(x / 10); n   putchar(x % 10 +  0 ); n } n inline void add(int x, int y) { n   ne[++tot] = fi[x], fi[x] = tot, to[tot] = y; n   ne[++tot] = fi[y], fi[y] = tot, to[tot] = x; n } n void pre_dfs(int x) { n   siz[x] = 1; n   for (int i = fi[x], v = to[i]; i; v = to[i = ne[i]]) n     if (v != fa[x]) { n       pre_dfs(v); n       siz[x] += siz[v]; n     } n } n int Rt, rt_path, rt[N]; n inline bool check(int u, int mn, int mx, int maxson, int lim) { n   int l = mx - lim, r = lim - mn; n   if (l > r) return false; n   if (maxson != fa[u]) return range(rt[maxson], l, r) > 0; n   int s1 = range(Rt, l, r); n   int s2 = range(rt[u], l, r); n   int s3 = range(rt_path, l + siz[u], r + siz[u]); n   return s1 + s3 > s2; n } n inline void solve_dfs(int x) { n   del(Rt, siz[x]); n   if (x != root) ins(rt_path, siz[x]); n   int mn = 1e9, mx = 0, submx = 0, mxson = 0; n   for (int i = fi[x], v = to[i]; i; v = to[i = ne[i]]) { n     if (v != fa[x]) solve_dfs(v); n     int sz = v == fa[x] ? n - siz[x] : siz[v]; n     mn = min(mn, sz); n     if (sz > mx) { n       submx = mx, mx = sz, mxson = v; n     } else if (sz > submx) n       submx = sz; n   } n   ins(Rt, siz[x]); n   ins(rt[x], siz[x]); n   if (x != root) del(rt_path, siz[x]); n   if (mxson == fa[x]) n     for (int i = fi[x], v = to[i]; i; v = to[i = ne[i]]) n       if (v != fa[x]) mge(rt[x], rt[v]); n   int l = submx == 0 ? mx : submx, r = mx, mid = 0; n   while (l < r) n     check(x, mn, mx, mxson, mid = l + r >> 1) ? r = mid : l = mid + 1; n   ans[x] = l; n   if (mxson != fa[x]) n     for (int i = fi[x], v = to[i]; i; v = to[i = ne[i]]) n       if (v != fa[x]) mge(rt[x], rt[v]); n } n int main() { n   n = read(); n   for (int i = 1; i <= n; ++i) { n     int x = read(), y = read(); n     if (!x) n       root = y; n     else n       fa[y] = x, add(x, y); n   } n   pre_dfs(root); n   for (int i = 1; i <= n; ++i) ins(Rt, siz[i]); n   solve_dfs(root); n   for (int i = 1; i <= n; ++i) print(ans[i]), puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, k, M, D, N, sol, x, mid, f, l, T; n long long up(long long a, long long b) { n   long long tmp = a / b; n   if (a % b != 0) tmp = tmp + 1ll; n   return tmp; n } n long long rep(long long a) { n   long long tmp = n / a; n   tmp = up(tmp, k); n   return tmp; n } n int main() { n   cin >> n >> k >> M >> D; n   sol = rep(M) * M; n   for (long long p = 2ll; p <= D; p++) { n     f = 1ll, l = M; n     mid = (f + l + 1ll) / 2; n     T = -1; n     while (l > f) { n       mid = (f + l + 1ll) / 2; n       if (rep(mid) == p && rep(mid + 1ll) != p) { n         T = mid; n         break; n       } n       if (rep(mid) >= p) { n         f = mid; n       } else if (rep(mid) < p) { n         l = mid - 1ll; n       } n       if (l - f <= 1) { n         if (rep(f) == p && rep(l + 1ll) > p) { n           T = f; n           break; n         } n         if (rep(l) == p && rep(l + 1ll) > p) { n           T = l; n           break; n         } n       } n     } n     if (T != -1) { n       x = T; n       T = rep(x) * x; n       sol = max(sol, T); n     } n   } n   cout << sol; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int max(int a, int b) { return a > b ? a : b; } n inline int min(int a, int b) { return a < b ? a : b; } n map<int, int> mp; n int main() { n   int n, x; n   cin >> n; n   mp.clear(); n   for (int i(0); i < (n); ++i) { n     cin >> x; n     ++mp[x]; n   } n   long long ans(0); n   for (map<int, int>::iterator it = mp.begin(); it != mp.end() && it->first < 0; n        ++it) n     ans += (long long)it->second * (long long)mp[-it->first]; n   ans += (long long)mp[0] * ((long long)mp[0] - 1ll) / 2ll; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n long long a[305]; n int prt[305], num[305] = {0}, ha[305] = {0}; n long long f[305][605] = {0}; n long long c[305][305], jc[305]; n int n; n bool pd(int i, int j) { n   long long ha = sqrt(a[i] * a[j]); n   if (ha * ha == a[i] * a[j]) return true; n   return false; n } n int getfa(int x) { n   if (x == prt[x]) return x; n   return prt[x] = getfa(prt[x]); n } n void init() { n   c[0][0] = 1; n   for (int i = 1; i <= 300; i++) { n     c[i][0] = 1; n     for (int j = 1; j <= i; j++) n       c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % mod; n   } n   jc[0] = 1; n   for (int i = 1; i <= 300; i++) jc[i] = jc[i - 1] * i % mod; n } n void solve() { n   int i, j, k, h; n   int sum = num[1]; n   f[1][num[1] - 1] = 1; n   for (i = 2; i <= num[0]; i++) { n     for (j = 0; j <= sum - 1; j++) n       for (k = 1; k <= num[i]; k++) n         for (h = 0; h <= j && h <= k; h++) { n           f[i][j - h + num[i] - k] += f[i - 1][j] * c[j][h] % mod * n                                       c[num[i] - 1][k - 1] % mod * n                                       c[sum + 1 - j][k - h] % mod; n           f[i][j - h + num[i] - k] %= mod; n         } n     sum += num[i]; n   } n   long long ans = f[num[0]][0]; n   for (int i = 1; i <= num[0]; i++) ans = ans * jc[num[i]] % mod; n   printf( %lld n , ans); n } n int main() { n   init(); n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %lld , &a[i]); n   for (int i = 1; i <= n; i++) prt[i] = i; n   for (int i = 1; i <= n; i++) n     for (int j = i + 1; j <= n; j++) n       if (pd(i, j)) { n         int f1, f2; n         f1 = getfa(i); n         f2 = getfa(j); n         if (f1 != f2) prt[f1] = f2; n       } n   for (int i = 1; i <= n; i++) prt[i] = getfa(i); n   for (int i = 1; i <= n; i++) { n     if (ha[prt[i]] == 0) { n       num[0]++; n       ha[prt[i]] = num[0]; n       num[num[0]]++; n     } else n       num[ha[prt[i]]]++; n   } n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int dx[] = {0, 0, 1, -1, 1, 1, -1, -1}; n const int dy[] = {1, -1, 0, 0, 1, -1, 1, -1}; n const int Mod = 1000000000 + 0; n template <typename T> n inline T sq(T x) { n   return x * x; n } n template <typename T> n inline T In() { n   T x; n   cin >> x; n   return x; n } n template <typename T> n inline void Out(T& x) { n   cout << x; n } n template <typename T> n inline void sort(T& v) { n   sort(begin(v), end(v)); n } n template <typename T> n inline void revs(T& v) { n   reverse(begin(v), end(v)); n } n template <typename T> n inline void uniq(T& v) { n   sort(v); n   v.erase(unique(begin(v), end(v)), end(v)); n } n template <typename T> n inline int ubnd(T& v, typename T::value_type& x) { n   return upper_bound(begin(v), end(v), x) - begin(v); n } n template <typename T> n inline int lbnd(T& v, typename T::value_type& x) { n   return lower_bound(begin(v), end(v), x) - begin(v); n } n template <typename T, typename F> n inline void inpt(T& v, int n, F f) { n   for (v.reserve(n); n--; v.emplace_back(f())) n     ; n } n template <typename T, typename F> n inline void show(T& v, F f, string d =    , string e =   n ) { n   int i = 0; n   for (auto& x : v) i++ && (cout << d), f(x); n   cout << e; n } n template <typename T> n inline typename T::iterator minel(T& v) { n   return min_element(begin(v), end(v)); n } n template <typename T> n inline typename T::iterator maxel(T& v) { n   return max_element(begin(v), end(v)); n } n inline void fast_io() { n   ios::sync_with_stdio(0); n   cin.tie(0); n } n inline int in() { n   int x; n   scanf( %d , &x); n   return x; n } n inline long long pow_mod(long long a, long long k, long long m) { n   long long r = 1; n   for (; k > 0; a = a * a % m, k >>= 1) n     if (k & 1) r = r * a % m; n   return r; n } n inline long long mod_inv(long long a, long long p) { n   return pow_mod(a, p - 2, p); n } n int main() { n   vector<long long> xs, ys; n   int n = in(); n   for (int i = 0; i < n; i++) { n     long long x, y; n     cin >> x >> y; n     xs.emplace_back(x); n     ys.emplace_back(y); n   } n   sort(xs); n   sort(ys); n   long long xx = abs(xs[0] - xs[n - 1]); n   long long yy = abs(ys[0] - ys[n - 1]); n   cout << sq(max(xx, yy)) << endl; n   return 0; n } n


#include <bits/stdc++.h> n const long long mod = (long long)1e9 + 7; n const int maxn = (int)4e5 + 2; n long long inf = 1LL << 60; n const double eps = 1e-9; n using namespace std; n template <class N> n inline void read(N &n) { n   N x = 0, f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = 10 * x + ch -  0 ; n     ch = getchar(); n   } n   n = x * f; n } n long long a[2001], b[2001]; n int main() { n   ios_base::sync_with_stdio(0); n   int n, m; n   cin >> n; n   long long s1 = 0, s2 = 0; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     s1 += a[i]; n   } n   cin >> m; n   for (int i = 0; i < m; i++) { n     cin >> b[i]; n     s2 += b[i]; n   } n   long long diff = abs(s1 - s2); n   pair<int, int> ans1 = {-1, -1}; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       long long bs = s2 - b[j] + a[i]; n       long long as = s1 - a[i] + b[j]; n       if (diff > abs(as - bs)) { n         diff = abs(as - bs); n         ans1 = {i, j}; n       } n     } n   } n   map<long long, pair<int, int> > mpa, mpb; n   for (int i = 0; i < n; i++) { n     for (int j = i + 1; j < n; j++) { n       mpa[a[i] + a[j]] = {i, j}; n     } n   } n   long long diff1 = inf; n   pair<int, int> ans[2]; n   for (int i = 0; i < m; i++) { n     for (int j = i + 1; j < m; j++) { n       long long d = b[i] + b[j]; n       auto it1 = mpa.lower_bound((s1 - s2 + 2 * d) / 2); n       if (it1 != mpa.begin()) it1--; n       int jj = 0; n       while (it1 != mpa.end() && jj <= 2) { n         long long diff2 = abs((s1 - it1->first + d) - (s2 - d + it1->first)); n         if (diff1 > diff2) { n           diff1 = diff2; n           ans[1] = {i, j}; n           ans[0] = it1->second; n         } n         jj++; n         it1++; n       } n     } n   } n   if (diff1 < diff) { n     cout << diff1 << endl; n     cout << 2 << endl; n     cout << ans[0].first + 1 <<     << ans[1].first + 1 << endl; n     cout << ans[0].second + 1 <<     << ans[1].second + 1 << endl; n   } else { n     cout << diff << endl; n     if (ans1.first == -1) n       cout << 0; n     else { n       cout << 1 << endl; n       cout << ans1.first + 1 <<     << ans1.second + 1; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n const int iinf = 1 << 29; n const long long llinf = 1ll << 60; n const double PI = 3.14159265; n mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); n long long mrand(long long B) { return (unsigned long long)rng() % B; } n long long cross(pair<long long, long long> p1, pair<long long, long long> p2) { n   return p1.first * p2.second - p2.first * p1.second; n } n bool online(pair<long long, long long> a, pair<long long, long long> b, n             pair<long long, long long> c) { n   pair<long long, long long> p1{b.first - a.first, b.second - a.second}; n   pair<long long, long long> p2{c.first - a.first, c.second - a.second}; n   return cross(p1, p2) == 0; n } n void z_func(vector<int> &z, vector<int> &s, int n) { n   int L = 0, R = 0; n   for (int i = 1; i < n; i++) { n     if (i > R) { n       L = R = i; n       while (R < n && s[R - L] == s[R]) R++; n       z[i] = R - L; n       R--; n     } else { n       int k = i - L; n       if (z[k] < R - i + 1) n         z[i] = z[k]; n       else { n         L = i; n         while (R < n && s[R - L] == s[R]) R++; n         z[i] = R - L; n         R--; n       } n     } n   } n } n const int MXN = 1e5 + 1; n int n, m; n bool roots[MXN]; n vector<int> g[MXN]; n int listt[MXN]; n vector<int> ans; n void fail() { n   cout <<  -1 n ; n   exit(0); n } n int dfs(int node) { n   int request = -1; n   for (auto &v : g[node]) { n     int req = dfs(v); n     if (req != -1 && request != -1 && req != request) fail(); n     if (req != -1) request = req; n   } n   if (request != -1 && listt[node] != request) fail(); n   if (request == -1) { n     if (listt[node] == node) { n       ans.push_back(node); n       return -1; n     } else { n       return listt[node]; n     } n   } else { n     if (node == listt[node]) { n       ans.push_back(node); n       return -1; n     } n     return listt[node]; n   } n } n void work() { n   cin >> n >> m; n   for (int i = 0; i < m; ++i) { n     int u, v; n     cin >> u >> v; n     roots[v] = true; n     g[u].push_back(v); n   } n   for (int i = 1; i <= n; ++i) cin >> listt[i]; n   for (int i = 1; i <= n; ++i) n     if (!roots[i]) dfs(i); n   cout << ((int)(ans).size()) <<   n ; n   for (auto &e : ans) cout << e <<   n ; n } n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   work(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k; n struct Poi { n   int x, y, ff; n   bool operator<(const Poi &A) const { n     return x < A.x || (x == A.x && y < A.y); n   } n } pp[(200100)]; n int row[(200100)], cntr; n int cnt[(200100)], pre[(200100)]; n long long ans[(200100)]; n inline void solve() { n   sort(pp + 1, pp + 1 + n * 2); n   for (int i = 1; i <= n * 2; ++i) { n     int y = lower_bound(row + 1, row + 1 + cntr, pp[i].y) - row; n     for (int j = y; row[j] < row[y] + k; ++j) { n       if (cnt[j] > 0) n         ans[cnt[j]] += (long long)(row[j + 1] - row[j]) * (pp[i].x - pre[j]); n       pre[j] = pp[i].x; n       cnt[j] += pp[i].ff; n     } n   } n   for (int i = 1; i <= n; ++i) cout << ans[i] <<    ; n } n inline void In() { n   scanf( %d %d , &n, &k); n   for (int i = 1; i <= n; ++i) { n     int i1 = i * 2 - 1, i2 = i * 2; n     scanf( %d %d , &pp[i1].x, &pp[i1].y); n     pp[i1].ff = 1; n     row[i1] = pp[i1].y; n     row[i2] = row[i1] + k; n     pp[i2].y = pp[i1].y; n     pp[i2].ff = -1; n     pp[i2].x = pp[i1].x + k; n   } n   sort(row + 1, row + 1 + n * 2); n   cntr = 1; n   for (int i = 2; i <= n * 2; ++i) n     if (row[i] != row[cntr]) row[++cntr] = row[i]; n } n int main() { n   In(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int gcd(long long int x, long long int y) { n   if (y == 0) return x; n   return gcd(y, x % y); n } n long long exp(long long a, long long b) { n   if (b == 0) return 1; n   long long res = exp(a, b / 2); n   if (b % 2) n     return res * res * a; n   else n     return res * res; n } n int prime(long long x) { n   if (x % 2 == 0) n     return 0; n   else { n     for (int i = 3; i * i <= x; i += 2) { n       if (x % i == 0) return 0; n     } n   } n   return 1; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   int n, m, k; n   map<int, int> ma; n   cin >> n; n   int arr[n]; n   for (int i = 0; i < n; i++) cin >> arr[i]; n   cin >> m; n   int brr[m]; n   for (int i = 0; i < m; i++) cin >> brr[i]; n   for (int i = 0; i < n; i++) { n     for (int j = m - 1; j >= 0; j--) { n       if (brr[j] % arr[i] == 0) { n         k = brr[j] / arr[i]; n         if (ma.find(k) == ma.end()) n           ma.insert({k, 1}); n         else n           ma[k]++; n       } n     } n   } n   map<int, int>::iterator it; n   it = ma.end(); n   it--; n   cout << it->second <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, a[105], sum = 0, i, j, x, k; n   scanf( %d , &n); n   for (i = 0; i < n; i++) { n     scanf( %d , &a[i]); n     sum += a[i]; n   } n   x = sum / (n / 2); n   for (j = 0; j < n; j++) { n     for (k = j + 1; k < n; k++) { n       if (a[j] + a[k] == x) { n         printf( %d %d n , j + 1, k + 1); n         a[j] = 0; n         a[k] = 0; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f, N = 1e3 + 5, MOD = 1e9 + 7; n int T, cas = 0; n int n, m; n bool vis[N]; n vector<int> primes; n void Init_Primes() { n   memset(vis, 0, sizeof(vis)); n   int m = sqrt(N + 0.5); n   for (int i = (2); i <= (m); i++) n     if (!vis[i]) n       for (int j = i * i; j <= N; j += i) vis[j] = 1; n   for (int i = (2); i < (N); i++) n     if (!vis[i]) primes.push_back(i); n } n int main() { n   Init_Primes(); n   while (scanf( %d , &n) != EOF) { n     int a[N], k = 0; n     for (int i = 2; i <= n; i++) { n       if (!vis[i]) { n         int t = 1; n         while (t * i <= n) { n           t *= i; n           a[k++] = t; n         } n       } n     } n     printf( %d n , k); n     for (int i = 0; i < (k - 1); i++) printf( %d  , a[i]); n     if (k > 0) printf( %d n , a[k - 1]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int d1[5], c1[5], d2[5], c2[5]; n int min2[5], min1[5]; n int main() { n   int n; n   cin >> n; n   for (int i = 1; i <= 4; i++) { n     cin >> c1[i] >> d1[i] >> c2[i] >> d2[i]; n     min1[i] = min(c1[i], d1[i]); n     min2[i] = min(c2[i], d2[i]); n   } n   int tmp = 1e9; n   int it; n   for (int i = 1; i <= 4; i++) n     if (min1[i] + min2[i] < tmp) { n       tmp = min1[i] + min2[i]; n       it = i; n     } n   if (tmp > n) n     cout << -1; n   else n     cout << it <<     << min1[it] <<     << n - min1[it]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a, x, y, xx, yy; n int main() { n   cin >> a >> x >> y >> xx >> yy; n   if (abs(x - xx) == a) n     cout << a + min(2 * a - y - yy, y + yy); n   else if (abs(y - yy) == a) n     cout << a + min(2 * a - x - xx, x + xx); n   else n     cout << abs(xx - x) + abs(yy - y); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long a, b, c; n   cin >> a >> b >> c; n   long long d = b * b - 4 * a * c; n   if (a != 0) { n     double x = a, y = b; n     if (d < 0) cout << 0; n     if (d == 0) { n       cout << 1 << endl; n       printf( %lf , -y / (2 * x)); n     } n     if (d > 0) { n       cout << 2 << endl; n       if (x > 0) n         printf( %lf n%lf , (-y - sqrt(d)) / (2 * x), (-y + sqrt(d)) / (2 * x)); n       else n         printf( %lf n%lf , (-y + sqrt(d)) / (2 * x), (-y - sqrt(d)) / (2 * x)); n     } n   } else { n     if (b == 0) { n       if (c == 0) n         cout << -1; n       else n         cout << 0; n     } else { n       double s = c, t = b; n       cout << 1 << endl; n       printf( %lf , -s / t); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200005; n bitset<N> vis; n int n, m, d[N], bound; n vector<int> g[N]; n set<int> h[N]; n unsigned long long a, b, c, ans, sum[N]; n inline unsigned long long get(int u, int v) { n   if (u > v) swap(u, v); n   unsigned long long x = a * u * 1LLU * (u - 1) / 2 + u * (b * u + c * v); n   unsigned long long y = b * (v * 1LLU * (v - 1) - u * 1LLU * (u + 1)) / 2 + n                          (v - u - 1) * (a * u + c * v); n   unsigned long long z = c * (n * 1LLU * (n - 1) - v * 1LLU * (v + 1)) / 2 + n                          (n - v - 1) * (a * u + b * v); n   return x + y + z; n } n inline unsigned long long val(int u, int v, int w) { n   int x[3] = {u, v, w}; n   sort(x, x + 3); n   return a * x[0] + b * x[1] + c * x[2]; n } n vector<pair<int, int> > edges; n bitset<N> isHeavy; n vector<int> heavy; n inline bool isEdge(int u, int v) { return h[u].find(v) != h[u].end(); } n void triangles() { n   for (int i = 0; i < n; ++i) { n     if (d[i] > bound) { n       heavy.push_back(i); n       isHeavy[i] = 1; n     } n   } n   for (int u : heavy) { n     for (pair<int, int> e : edges) { n       int v = e.first, w = e.second; n       if (isHeavy[v] and v < u) continue; n       if (isHeavy[w] and w < u) continue; n       if (isEdge(u, v) and isEdge(u, w)) { n         ans -= val(u, v, w); n       } n     } n   } n   for (pair<int, int> e : edges) { n     int u = e.first, v = e.second; n     if (u > v) swap(u, v); n     if (isHeavy[u] or isHeavy[v]) continue; n     for (int i = 0, j = 0; i < g[u].size() and j < g[v].size();) { n       if (g[u][i] == g[v][j]) { n         int w = g[u][i]; n         if (!isHeavy[w] and w > v) { n           ans -= val(u, v, w); n         } n         ++i; n       } else if (g[u][i] < g[v][j]) { n         ++i; n       } else { n         ++j; n       } n     } n   } n } n int main() { n   scanf( %d %d %llu %llu %llu , &n, &m, &a, &b, &c); n   bound = 700; n   for (unsigned long long k = 0; k < n; ++k) { n     unsigned long long x = k * (k - 1) / 2; n     unsigned long long y = k * (a + 2 * b + 3 * c) - (2 * a + b); n     if (x % 3 == 0) n       x /= 3; n     else n       y /= 3; n     ans += x * y; n   } n   while (m--) { n     int u, v; n     scanf( %d %d , &u, &v); n     ++d[u], ++d[v]; n     edges.emplace_back(u, v); n     ans -= get(u, v); n     g[u].push_back(v); n     g[v].push_back(u); n     h[u].insert(v); n     h[v].insert(u); n   } n   for (int i = 0; i < n; ++i) { n     sort(g[i].begin(), g[i].end()); n     int sz = g[i].size(); n     for (int j = 0; j < sz; ++j) { n       sum[j + 1] = sum[j] + g[i][j]; n     } n     int at = 0; n     while (at < sz and g[i][at] < i) { n       ++at; n     } n     for (int j = 0; j < sz; ++j) { n       unsigned long long cur; n       int u = g[i][j]; n       if (u > i) { n         cur = (sz - 1 - j) * (a * i + b * u) + c * (sum[sz] - sum[j + 1]); n         ans += cur; n       } n       if (u < i) { n         cur = (sz - at) * (a * u + b * i) + c * (sum[sz] - sum[at]); n         ans += cur; n         cur = j * (b * u + c * i) + a * sum[j]; n         ans += cur; n       } n     } n   } n   triangles(); n   printf( %llu n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int arr[200][200]; n   int n, m, ok = 1000000000, ans = 0; n   scanf( %d %d , &n, &m); n   for (int i = 0; i < n; i++) { n     ok = 1000000000; n     for (int j = 0; j < m; j++) { n       scanf( %d , &arr[i][j]); n       if (ok > arr[i][j]) ok = arr[i][j]; n     } n     ans = max(ans, ok); n   } n   printf( %d , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int a; n   cin >> a; n   if (a <= 1) { n     cout << a; n   } else if (a == 2) { n     cout << 2; n   } else if (a == 3) { n     cout << 6; n   } else if (a == 4) { n     cout << 20; n   } else if (a == 5) { n     cout << 70; n   } else if (a == 6) { n     cout << 252; n   } else if (a == 7) { n     cout << 924; n   } else if (a == 9) { n     cout << 12870; n   } else if (a == 10) { n     cout << 48620; n   } else if (a == 8) { n     cout << 3432; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long a[200001], b[200001]; n int main() { n   int t; n   int i, j; n   cin >> t; n   while (t--) { n     long long n, m; n     cin >> n >> m; n     for (i = 0; i < n; i++) cin >> a[i] >> b[i]; n     long long A = 0, B = 0, Ap = 0, Bp = 0; n     long long tot = 0, sum = 0; n     for (i = 0; i < n; i++) { n       A += a[i] - (m - min(m, b[i])); n       Ap += m - min(m, b[i]); n       B += b[i] - (m - min(m, a[i])); n       Bp += m - min(m, a[i]); n       sum += m; n       tot += a[i] + b[i] - m; n     } n     if (A + A < tot) { n       cout << tot - (A + A) << endl; n       for (i = 0; i < n; i++) { n         cout << m - min(m, b[i]) <<     << min(m, b[i]) << endl; n       } n     } else if (B + B < tot) { n       cout << tot - (B + B) << endl; n       for (i = 0; i < n; i++) { n         cout << min(m, a[i]) <<     << m - min(m, a[i]) << endl; n       } n     } else { n       long long togo = A - tot / 2; n       cout << (tot & 1) << endl; n       for (i = 0; i < n; i++) { n         long long x = min(togo, min(m, b[i])); n         x = min(x, a[i] - (m - min(m, b[i]))); n         togo -= x; n         cout << m - min(m, b[i]) + x <<     << min(m, b[i]) - x << endl; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   long long mx = 0, x = 0; n   cin >> n; n   for (int i = 0; i < n; i++) { n     long long y; n     cin >> y; n     y += mx; n     mx += max(0LL, x - y), x = max(y, x); n   } n   cout << mx << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct fastIO { n   inline fastIO operator>>(int& num) { n     num = 0; n     char c = getchar(); n     while (c !=  -  && (c <  0  || c >  9 )) c = getchar(); n     bool foo = 0; n     if (c ==  - ) { n       foo = 1; n       c = getchar(); n     } n     while (c >=  0  && c <=  9 ) { n       num = (num << 3) + (num << 1) + c -  0 ; n       c = getchar(); n     } n     if (foo) num = -num; n     return *this; n   } n   inline fastIO operator>>(long long& num) { n     num = 0; n     char c = getchar(); n     while (c !=  -  && (c <  0  || c >  9 )) c = getchar(); n     bool foo = 0; n     if (c ==  - ) { n       foo = 1; n       c = getchar(); n     } n     while (c >=  0  && c <=  9 ) { n       num = (num << 3) + (num << 1) + c -  0 ; n       c = getchar(); n     } n     if (foo) num = -num; n     return *this; n   } n   inline fastIO operator>>(float& num) { n     scanf( %f , &num); n     return *this; n   } n   inline fastIO operator>>(double& num) { n     scanf( %lf , &num); n     return *this; n   } n   inline fastIO operator>>(long double& num) { n     scanf( %Lf , &num); n     return *this; n   } n   inline fastIO operator>>(char& num) { n     num = getchar(); n     while (num ==     || num ==   n ) num = getchar(); n     return *this; n   } n   inline fastIO operator>>(char* num) { n     int cnt = 0; n     char c = getchar(); n     while (c ==     || c ==   n ) c = getchar(); n     while (c !=     && c !=   n ) { n       num[cnt++] = c; n       c = getchar(); n     } n     num[cnt] = 0; n     return *this; n   } n   inline fastIO operator>>(string& num) { n     cin >> num; n     return *this; n   } n   inline void printInt(const int& num) { n     if (num < 10) { n       putchar(num +  0 ); n       return; n     } n     printInt(num / 10); n     putchar((num % 10) +  0 ); n   } n   inline void printINT(const long long& num) { n     if (num < 10) { n       putchar(num +  0 ); n       return; n     } n     printINT(num / 10); n     putchar((num % 10) +  0 ); n   } n   inline fastIO operator<<(const int& num) { n     if (num < 0) n       putchar( - ), printInt(-num); n     else n       printInt(num); n     return *this; n   } n   inline fastIO operator<<(const long long& num) { n     if (num < 0) n       putchar( - ), printINT(-num); n     else n       printINT(num); n     return *this; n   } n   inline fastIO operator<<(const float& num) { n     printf( %.10f , num); n     return *this; n   } n   inline fastIO operator<<(const double& num) { n     printf( %.16lf , num); n     return *this; n   } n   inline fastIO operator<<(const long double& num) { n     printf( %.20Lf , num); n     return *this; n   } n   inline fastIO operator<<(const char& num) { n     putchar(num); n     return *this; n   } n   inline fastIO operator<<(const char* num) { n     for (int i = 0; num[i]; i++) putchar(num[i]); n     return *this; n   } n   inline fastIO operator<<(const string& num) { n     cout << num; n     return *this; n   } n } fio; n int n, m, k; n int fuck[17][17]; n int shit[17][17][17]; n int dy[17]; n int ans = 0; n void dfs(int x, int y, int mx) { n   if (x == n) { n     for (int i = 1; i <= mx; i++) n       for (int j = i + 1; j <= mx; j++) n         if (dy[i] && dy[j] && dy[i] == dy[j]) return; n     int fri = 0, bb = k; n     for (int i = 1; i <= mx; i++) { n       if (!dy[i]) n         fri++; n       else n         bb--; n     } n     int fk = 1; n     for (int i = 0; i < fri; i++) { n       fk *= bb; n       bb--; n     } n     ans += fk; n     if (ans > 1000000007) ans -= 1000000007; n     return; n   } n   for (int i = 1; i <= mx + 1; i++) { n     if (shit[x][y][i]) continue; n     for (int ii = x; ii < n; ii++) n       for (int jj = y; jj < m; jj++) shit[ii][jj][i]++; n     bool fau = 1; n     if (fuck[x][y] && dy[i] && fuck[x][y] != dy[i]) fau = 0; n     if (fau) { n       bool dyed = 0; n       if (!dy[i]) { n         dy[i] = fuck[x][y]; n         dyed = 1; n       } n       if (y < m - 1) n         dfs(x, y + 1, min(max(mx, i), k - 1)); n       else n         dfs(x + 1, 0, min(max(mx, i), k)); n       if (dyed) dy[i] = 0; n     } n     for (int ii = x; ii < n; ii++) n       for (int jj = y; jj < m; jj++) shit[ii][jj][i]--; n   } n } n int main() { n   fio >> n >> m >> k; n   if (n + m - 1 > k) { n     fio <<  0 n ; n     return 0; n   } n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) fio >> fuck[i][j]; n   dfs(0, 0, 0); n   fio << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC target( avx,avx2,fma ) n using namespace std; n const long long MOD = 1e9 + 7; n const long long mxN = 1e5 + 3; n vector<long long> g[mxN]; n long long good[mxN]; n long long total[mxN], people[mxN]; n long long h[mxN]; n bool ok[mxN]; n void dfs1(long long u, long long par) { n   long long sum = 0; n   for (long long i : g[u]) { n     if (i != par) { n       dfs1(i, u); n       sum += total[i]; n     } n   } n   total[u] = sum + people[u]; n } n bool dfs2(long long u, long long par) { n   bool ret = true; n   if (ok[u] == false) return false; n   long long cnt = 0; n   for (long long i : g[u]) { n     if (i != par) { n       cnt++; n       ret = (ret && dfs2(i, u)); n     } n   } n   if (ret == true) { n     long long su = 0; n     for (long long ii : g[u]) { n       if (ii != par) { n         su += good[ii]; n       } n     } n     if (su > good[u]) n       return false; n     else n       return true; n   } n   return false; n } n void solve() { n   long long n; n   cin >> n; n   for (long long i = 0; i <= n; ++i) { n     g[i].clear(); n     good[i] = 0; n     total[i] = 0; n     people[i] = 0; n     h[i] = 0; n     ok[i] = true; n   } n   long long m; n   cin >> m; n   for (long long i = 1; i <= n; ++i) { n     cin >> people[i]; n   } n   for (long long i = 1; i <= n; ++i) { n     cin >> h[i]; n   } n   long long u, v; n   for (long long i = 1; i < n; ++i) { n     cin >> u >> v; n     g[u].push_back(v); n     g[v].push_back(u); n   } n   dfs1(1, -1); n   for (long long i = 1; i <= n; ++i) { n     long long g2 = (total[i] + h[i]); n     long long b2 = (total[i] - h[i]); n     if (g2 & 1) { n       ok[i] = false; n       continue; n     } n     if (b2 & 1) { n       ok[i] = false; n       continue; n     } n     long long go = g2 / 2; n     long long b = b2 / 2; n     if (go > total[i] or b > total[i] or go < 0 or b < 0) { n       ok[i] = false; n       continue; n     } n     good[i] = go; n   } n   bool ans = dfs2(1, -1); n   if (ans) n     cout <<  YES  n          <<   n ; n   else n     cout <<  NO  n          <<   n ; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long nT = 1; n   cin >> nT; n   for (long long i = 1; i <= nT; ++i) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100; n int l[N], r[N], t[N], c[N]; n int main() { n   int n, m; n   scanf( %d %d , &n, &m); n   for (int i = 0; i < m; ++i) { n     scanf( %d %d %d %d , l + i, r + i, t + i, c + i); n   } n   int ans = 0; n   for (int i = 1; i <= n; ++i) { n     int win = 0, min = -1; n     for (int j = 0; j < m; ++j) { n       if (l[j] <= i && i <= r[j]) { n         if (min == -1 || min > t[j]) { n           min = t[j]; n           win = c[j]; n         } n       } n     } n     ans += win; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1000000007; n int n, m; n int clauses[123123][2]; n vector<int> uses[123123]; n bool vis[123123]; n vector<int> chain[123123]; n int counter; n vector<pair<long long, long long>> dips; n long long ans; n int rightc(int cnr) { n   int xnr = abs(clauses[cnr][1]); n   if (uses[xnr].size() == 2) { n     return uses[xnr][0] == cnr ? uses[xnr][1] : uses[xnr][0]; n   } else { n     return 0; n   } n } n void load() { n   cin >> n >> m; n   int k; n   for (int i = 1; i <= n; ++i) { n     cin >> k; n     for (int j = 0; j < k; ++j) { n       cin >> clauses[i][j]; n       uses[abs(clauses[i][j])].push_back(i); n     } n   } n } n void dfs(int cnr, int from) { n   if (vis[cnr]) { n     return; n   } n   vis[cnr] = 1; n   if (abs(clauses[cnr][1]) == from) { n     swap(clauses[cnr][0], clauses[cnr][1]); n   } n   chain[counter].push_back(cnr); n   dfs(rightc(cnr), abs(clauses[cnr][1])); n } n void dfs2(int cnr, int from, int f) { n   if (vis[cnr]) { n     return; n   } n   vis[cnr] = 1; n   if (!clauses[cnr][1]) { n     if (!f) { n       swap(clauses[cnr][0], clauses[cnr][1]); n     } n   } else { n     if (abs(clauses[cnr][1]) == from) { n       swap(clauses[cnr][0], clauses[cnr][1]); n     } n   } n   chain[counter].push_back(cnr); n   dfs2(rightc(cnr), abs(clauses[cnr][1]), 1); n } n void buildChains() { n   vis[0] = 1; n   for (int i = 1; i <= m; ++i) n     if (uses[i].size() == 1 && !vis[uses[i][0]]) { n       dfs(uses[i][0], i); n       ++counter; n     } n   for (int i = 1; i <= m; ++i) n     if (uses[i].size() == 2) { n       if (!vis[uses[i][0]] && !clauses[uses[i][0]][1]) { n         dfs2(uses[i][0], i, 0); n         ++counter; n       } n       if (!vis[uses[i][1]] && !clauses[uses[i][1]][1]) { n         dfs2(uses[i][1], i, 0); n         ++counter; n       } n     } n   for (int i = 1; i <= n; ++i) n     if (!vis[i]) { n       dfs(i, abs(clauses[i][0])); n       ++counter; n     } n } n long long dp[123123][2][2][2]; n int val(int i, int j, int nr) { n   int v1 = i; n   if (clauses[nr][0] < 0) { n     v1 = 1 - v1; n   } n   if (clauses[nr][0] == 0) { n     v1 = 0; n   } n   int v2 = j; n   if (clauses[nr][1] < 0) { n     v2 = 1 - v2; n   } n   if (clauses[nr][1] == 0) { n     v2 = 0; n   } n   return (v1 | v2); n } n void cleaar(int si) { n   for (int i = 0; i < si; ++i) n     for (int j = 0; j < 2; ++j) n       for (int l = 0; l < 2; ++l) n         for (int k = 0; k < 2; ++k) dp[i][j][l][k] = 0; n } n void consider(int nr) { n   if (chain[nr].size() == 1 && n       abs(clauses[chain[nr][0]][0]) == abs(clauses[chain[nr][0]][1])) { n     if (clauses[chain[nr][0]][0] == clauses[chain[nr][0]][1]) { n       dips.push_back(make_pair(1, 1)); n     } else { n       dips.push_back(make_pair(0, 2)); n     } n     return; n   } n   cleaar(chain[nr].size()); n   int fi = chain[nr][0]; n   for (int i = 0; i < 2; ++i) n     for (int j = 0; j < 2; ++j) dp[0][i][j][val(i, j, fi)] = 1; n   for (int i = 0; i < chain[nr].size() - 1; ++i) n     for (int j = 0; j < 2; ++j) n       for (int l = 0; l < 2; ++l) n         for (int k = 0; k < 2; ++k) { n           dp[i + 1][j][0][val(l, 0, chain[nr][i + 1]) ^ k] += dp[i][j][l][k]; n           dp[i + 1][j][0][val(l, 0, chain[nr][i + 1]) ^ k] %= mod; n           dp[i + 1][j][1][val(l, 1, chain[nr][i + 1]) ^ k] += dp[i][j][l][k]; n           dp[i + 1][j][1][val(l, 1, chain[nr][i + 1]) ^ k] %= mod; n         } n   int si = chain[nr].size(); n   if (!clauses[chain[nr][0]][0] && !clauses[chain[nr][si - 1]][1]) { n     dips.push_back(make_pair(dp[si - 1][0][0][0], dp[si - 1][0][0][1])); n     return; n   } n   if (!clauses[chain[nr][si - 1]][1]) { n     long long r0 = (dp[si - 1][0][0][0] + dp[si - 1][1][0][0]) % mod; n     long long r1 = (dp[si - 1][0][0][1] + dp[si - 1][1][0][1]) % mod; n     dips.push_back(make_pair(r0, r1)); n     return; n   } n   if (abs(clauses[chain[nr][0]][0]) != abs(clauses[chain[nr][si - 1]][1])) { n     long long r0 = (dp[si - 1][0][0][0] + dp[si - 1][1][1][0] + n                     dp[si - 1][0][1][0] + dp[si - 1][1][0][0]) % n                    mod; n     long long r1 = (dp[si - 1][0][0][1] + dp[si - 1][1][1][1] + n                     dp[si - 1][0][1][1] + dp[si - 1][1][0][1]) % n                    mod; n     dips.push_back(make_pair(r0, r1)); n     return; n   } n   long long r0 = (dp[si - 1][0][0][0] + dp[si - 1][1][1][0]) % mod; n   long long r1 = (dp[si - 1][0][0][1] + dp[si - 1][1][1][1]) % mod; n   dips.push_back(make_pair(r0, r1)); n } n void makeAns() { n   long long a = dips[0].first; n   long long b = dips[0].second; n   for (int i = 1; i < counter; ++i) { n     long long c = (a * dips[i].first + b * dips[i].second) % mod; n     long long d = (a * dips[i].second + b * dips[i].first) % mod; n     a = c; n     b = d; n   } n   ans = b; n   for (int i = 1; i <= m; ++i) n     if (uses[i].empty()) { n       ans *= 2; n       ans %= mod; n     } n } n void debug() { n   return; n   for (int i = 0; i < counter; ++i) { n     cerr << i <<   :  ; n     for (int ind : chain[i]) { n       cerr <<  (  << clauses[ind][0] <<  ,   << clauses[ind][1] <<  )  ; n     } n     cerr << endl; n   } n   for (auto tmp : dips) { n     cerr << tmp.first <<     << tmp.second << endl; n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   load(); n   buildChains(); n   for (int i = 0; i < counter; ++i) { n     consider(i); n   } n   debug(); n   makeAns(); n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int MX = 3005; n int n, a[MX], mem[MX][MX], ind[MX]; n vector<int> pos[MX]; n int dp(int i, int j) { n   if (i > j) return 0; n   int &res = mem[i][j]; n   if (res != -1) return res; n   res = dp(i, j - 1); n   for (int k = ind[j] - 1, x; k >= 0 && (x = pos[a[j]][k]) >= i; k--) n     res = max(res, dp(i, x) + dp(x + 1, j - 1) + 1); n   return res; n } n void main_() { n   cin >> n; n   for (int i = 0; i < int(n + 1); i++) pos[i].clear(); n   for (int i = 0; i < int(n); i++) { n     cin >> a[i]; n     ind[i] = pos[a[i]].size(); n     pos[a[i]].push_back(i); n   } n   for (int i = 0; i < int(n); i++) n     for (int j = 0; j < int(n); j++) mem[i][j] = -1; n   cout << n - 1 - dp(0, n - 1) <<   n ; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t = 1; n   cin >> t; n   while (t--) main_(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long solve() { n   long long k, a, b; n   cin >> k >> a >> b; n   if (a > b) swap(a, b); n   if (a == 0) { n     if (b % k == 0) return b / k; n     return -1; n   } n   long long ans = a / k + b / k; n   if (b % k != 0 && a < k) return -1; n   if (a % k != 0 && b < k) return -1; n   if (ans == 0) return -1; n   return ans; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout << solve() << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void read_to_vector(size_t N, std::vector<T> &v) { n   for (size_t i = 0; i < N; i++) { n     T tmp; n     std::cin >> tmp; n     v.push_back(tmp); n   } n } n template <typename T> n void print_vector(const std::vector<T> &v) { n   for (auto it = v.begin(); it < v.end(); it++) { n     if (it != v.begin()) std::cout <<    ; n     std::cout << (*it); n   } n } n string getpre(string s0, string s1) { n   string res; n   for (int i = 0; i < s0.size() && i < s1.size(); i++) { n     if (s0[i] == s1[i]) { n       res += s0[i]; n     } else { n       break; n     } n   } n   return res; n } n string getpre(vector<string> &ss) { n   if (ss.size() == 0) { n     return   ; n   } n   string smin = ss[0]; n   string smax = ss[0]; n   for (int i = 0; i < ss.size(); i++) { n     smin = min(ss[i], smin); n     smax = max(ss[i], smax); n   } n   return getpre(smin, smax); n } n void solve() { n   long long n; n   cin >> n; n   vector<string> w0, w1; n   read_to_vector(n, w0); n   read_to_vector(n, w1); n   vector<int> ch0(n, -1), ch1(n, -1); n   string chans0; n   string chans1; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < w0[i].size(); j++) { n       if (w0[i][j] != w1[i][j]) { n         if (ch0[i] == -1) { n           ch0[i] = j; n         } n         ch1[i] = j; n       } n     } n     ch1[i]++; n     if (ch0[i] != -1) { n       chans0 = w0[i].substr(ch0[i], ch1[i] - ch0[i]); n       chans1 = w1[i].substr(ch0[i], ch1[i] - ch0[i]); n     } n   } n   vector<string> prefs; n   vector<string> posts; n   for (int i = 0; i < n; i++) { n     if (ch0[i] == -1) { n       continue; n     } n     string pre = w0[i].substr(0, ch0[i]); n     reverse(pre.begin(), pre.end()); n     string post = w0[i].substr(ch1[i], w0[i].size() - ch1[i]); n     prefs.push_back(pre); n     posts.push_back(post); n   } n   string pre = getpre(prefs); n   reverse(pre.begin(), pre.end()); n   string post = getpre(posts); n   string rep0 = pre + chans0 + post; n   string rep1 = pre + chans1 + post; n   bool test = true; n   for (int i = 0; i < n; i++) { n     string w = w0[i]; n     string wend = w; n     int ind = w.find(rep0); n     if (ind != string::npos) { n       wend = w.substr(0, ind) + rep1 + n              w.substr(ind + rep1.size(), w.size() - (ind + rep1.size())); n     } n     test &= (wend == w1[i]); n   } n   if (test) { n     cout <<  YES n  << rep0 <<   n  << rep1 <<   n ; n   } else { n     cout <<  NO n ; n   } n } n void pre_compute() {} n int main() { n   std::cout << std::setprecision(15); n   std::ios::sync_with_stdio(false); n   std::cin.tie(0); n   pre_compute(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, v = 0, p = 0; n   string s, result =   ; n   cin >> n >> s; n   v = (n - 11) / 2; n   p = v; n   for (int i = 0; i < n; i++) { n     if (s[i] ==  8 ) { n       if (v > 0) n         v--; n       else n         result += s[i]; n     } else { n       if (p > 0) n         p--; n       else n         result += s[i]; n     } n   } n   if (result[0] ==  8 ) n     cout <<  YES  << endl; n   else n     cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int arr[510]; n int dp[510][510]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0; i <= n - 1; i++) scanf( %d , &arr[i]); n   for (int l = 1; l <= n; l++) { n     for (int i = 0; i <= n - 1; i++) { n       int j = i + l - 1; n       if (j > n - 1) continue; n       if (i == j) { n         dp[i][j] = 1; n         continue; n       } n       dp[i][j] = dp[i + 1][j] + 1; n       for (int k = i + 2; k <= j; k++) n         if (arr[k] == arr[i]) n           dp[i][j] = min(dp[i][j], dp[i + 1][k - 1] + dp[k + 1][j]); n       if (i < n - 1 && arr[i + 1] == arr[i]) n         dp[i][j] = min(dp[i][j], dp[i + 2][j] + 1); n     } n   } n   cout << dp[0][n - 1] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void mego() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n } n bool isp(long long x) { n   for (long long i = 2; i * i <= x; i++) n     if (!(x % i)) return false; n   return true; n } n long long coun = 0; n int gcd(long long a, long long b) { n   if (b == 0) return a; n   coun += (a / b); n   return gcd(b, a % b); n } n int main() { n   mego(); n   long long n, a, z = 0, b = 0, d = 0, c = 0; n   vector<long long> v, v1; n   long long t = 0; n   cin >> t; n   map<int, pair<int, int> > ma; n   for (int i = 1; i <= t; i++) { n     cin >> a; n     if (!ma[a].first) { n       ma[a].first = i; n     } else if (!ma[a].second) n       ma[a].second = i; n     else { n       if ((i - ma[a].second) != (ma[a].second - ma[a].first)) { n         ma[a].first = -1; n         ma[a].second = -1; n       } else { n         ma[a].first = ma[a].second; n         ma[a].second = i; n       } n     } n   } n   vector<pair<int, int> > vp; n   pair<int, int> rej = {-1, -1}; n   for (auto it : ma) { n     if (it.second != rej) { n       vp.push_back(make_pair(it.first, it.second.second - it.second.first)); n     } n   } n   cout << vp.size() <<   n ; n   for (int i = 0; i < vp.size(); i++) { n     cout << vp[i].first <<     << (vp[i].second > 0 ? vp[i].second : 0) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long n, k; n   cin >> n >> k; n   string second; n   cin >> second; n   vector<long long> cnt(k, 0); n   for (long long i = 0; i < n; i++) { n     cnt[second[i] -  A ]++; n   } n   long long ans = cnt[0]; n   for (long long i = 0; i < k; i++) { n     ans = min(ans, cnt[i]); n   } n   cout << ans * k << endl; n } n signed main() { n   long long n = 1; n   while (n--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 21; n int cnt, u; n int sz[1 << MAXN], g[1 << MAXN], p[1 << MAXN]; n long long int f[1 << MAXN], h[1 << MAXN]; n inline long long int read() { n   register long long int num = 0, neg = 1; n   register char ch = getchar(); n   while (!isdigit(ch) && ch !=  - ) { n     ch = getchar(); n   } n   if (ch ==  - ) { n     neg = -1; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     num = (num << 3) + (num << 1) + (ch -  0 ); n     ch = getchar(); n   } n   return num * neg; n } n int main() { n   cnt = read(); n   for (register int i = 0; i < cnt; i++) { n     if (!(f[i] = read())) { n       i--, cnt--; n     } n   } n   for (register int i = 1; i < (1 << cnt); i++) { n     sz[i] = sz[i >> 1] + (i & 1); n     for (register int j = 0; j < cnt; j++) { n       if ((i >> j) & 1) { n         h[i] += f[j]; n       } n     } n     if ((h[i] + sz[i] & 1)) { n       for (register int j = i; (j = ((j - 1) & i)) && !p[i];) { n         if (h[i] - sz[i] + 1 <= 2 * h[j] && 2 * h[j] <= h[i] + sz[i] - 1) { n           p[i] = 1; n         } n       } n     } n   } n   for (register int i = 1; i < (1 << cnt); i++) { n     if (p[i] && !g[i]) { n       g[i] = 1, u = (1 << cnt) - i - 1; n       for (register int j = u; j; j = (j - 1) & u) { n         g[i | j] = max(g[i | j], g[j] + 1); n       } n     } n   } n   printf( %d n , cnt - g[(1 << cnt) - 1]); n } n


#include <bits/stdc++.h> n using namespace std; n const long long NMAX = 300000 + 5; n const long long inf = 922337203685477580LL; n vector<vector<pair<long long, long long> > > g(NMAX); n vector<vector<int> > id(NMAX); n long long dis[NMAX]; n long long last[NMAX]; n long long a[NMAX]; n int main() { n   int n, m; n   cin >> n >> m; n   long long ans = 0; n   for (int i = 0; i < m; i++) { n     int a, b, c; n     cin >> a >> b >> c; n     g[a].push_back({b, c}); n     g[b].push_back({a, c}); n     id[a].push_back(i); n     id[b].push_back(i); n   } n   int s; n   cin >> s; n   for (int i = 1; i <= n; i++) dis[i] = inf; n   dis[s] = 0; n   for (int i = 1; i <= n; i++) last[i] = inf; n   last[s] = 0; n   set<pair<int, int> > q; n   q.insert({0, s}); n   while (!q.empty()) { n     int w = q.begin()->second; n     q.erase(q.begin()); n     for (int i = 0; i < g[w].size(); i++) { n       int to = g[w][i].first; n       int len = g[w][i].second; n       if (dis[w] + len < dis[to] || n           (dis[w] + len <= dis[to] && len < last[to])) { n         q.erase(make_pair(dis[to], to)); n         dis[to] = dis[w] + len; n         last[to] = len; n         a[to] = id[w][i] + 1; n         q.insert(make_pair(dis[to], to)); n       } n     } n   } n   for (int i = 1; i <= n; i++) ans += last[i]; n   cout << ans << endl; n   for (int i = 1; i <= n; i++) { n     if (i != s) { n       cout << a[i] <<    ; n     } n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 5; n long long mod = 1e18; n long long fast_pow(long long b, long long p) { n   if (p == 0) return 1; n   long long m = fast_pow(b, p / 2); n   m = ((m % mod) * (m % mod)) % mod; n   if (p % 2 == 1) m = ((m % mod) * (b % mod)) % mod; n   return m; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   pair<int, int> arr[N]; n   for (int i = 0; i < n; i++) { n     cin >> arr[i].first; n   } n   for (int i = 0; i < n; i++) { n     cin >> arr[i].second; n   } n   sort(arr, arr + n); n   long long cu = 1; n   priority_queue<long long> q; n   long long sum = 0; n   long long ans = 0; n   for (long long i = 0; i < n; i++) { n     if (q.empty()) cu = arr[i].first; n     if (arr[i].first == cu) { n       q.push(arr[i].second); n       sum += arr[i].second; n     } else { n       sum -= q.top(); n       q.pop(); n       ans += sum; n       while (q.size() && (++cu) < arr[i].first) { n         sum -= q.top(); n         q.pop(); n         ans += sum; n       } n       i--; n     } n   } n   while (q.size()) { n     sum -= q.top(); n     q.pop(); n     ans += sum; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k, p, x, y; n   int a[1010], b[1010]; n   int i, j; n   while (scanf( %d%d%d%d%d , &n, &k, &p, &x, &y) != EOF) { n     int sum = 0; n     for (i = 0; i < k; i++) { n       scanf( %d , &a[i]); n       sum += a[i]; n     } n     if (sum > x) { n       puts( -1 ); n       continue; n     } n     for (i = k; i < n; i++) { n       a[i] = 1; n       sum += 1; n     } n     int t = k; n     while (t < n && sum + y - 1 <= x) { n       a[t++] = y; n       sum += y - 1; n     } n     for (i = 0; i < n; i++) b[i] = a[i]; n     sort(b, b + n); n     int m = b[n / 2]; n     if (sum <= x && m >= y) { n       for (i = k; i < n - 1; i++) printf( %d  , a[i]); n       printf( %d n , a[n - 1]); n     } else n       printf( -1 n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<int, int> mm; n map<int, int>::iterator it; n int x[100005]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n; n   cin >> n; n   vector<int> v; n   for (int i = 1; i <= n; i++) cin >> x[i]; n   for (int i = n; i >= 1; i--) { n     if (mm[x[i]] == 0) { n       mm[x[i]]++; n       v.push_back(x[i]); n     } n   } n   cout << endl; n   cout << v.size() <<   n ; n   for (int i = v.size() - 1; i >= 0; i--) { n     cout << v[i] <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n pair<long double, long double> st[4 * N]; n int n, q; n template <typename T> n pair<T, T> operator+(const pair<T, T> &a, const pair<T, T> &b) { n   pair<T, T> res; n   res.first = a.first + b.first * a.second; n   res.second = a.second * b.second; n   return res; n } n void update(int id, int l, int r, int pos, long double val) { n   if (l > pos || r < pos) return; n   if (l == r) { n     st[id] = make_pair(val, val); n     return; n   } n   int mid = (l + r) >> 1; n   update(id << 1, l, mid, pos, val); n   update(id << 1 | 1, mid + 1, r, pos, val); n   st[id] = st[id << 1] + st[id << 1 | 1]; n } n pair<long double, long double> getsum(int id, int l, int r, int u, int v) { n   if (l > v || r < u) return make_pair(0, 1); n   if (l >= u && r <= v) return st[id]; n   int mid = (l + r) >> 1; n   pair<long double, long double> lnode = getsum(id << 1, l, mid, u, v); n   pair<long double, long double> rnode = getsum(id << 1 | 1, mid + 1, r, u, v); n   return lnode + rnode; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> q; n   for (int i = 1; i <= n; i++) { n     int a, b; n     cin >> a >> b; n     long double p = (long double)a / b; n     update(1, 1, n, i, (1.0L - p) / p); n   } n   while (q--) { n     int op; n     cin >> op; n     if (op == 1) { n       int pos, a, b; n       cin >> pos >> a >> b; n       long double p = (long double)a / b; n       update(1, 1, n, pos, (1.0L - p) / p); n     } else { n       int l, r; n       cin >> l >> r; n       long double res = 1 + getsum(1, 1, n, l, r).first; n       long double ans = (1.0L / res); n       if (ans != ans) ans = 0; n       cout << fixed << setprecision(9) << ans <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 53; n const int MAXM = 53; n char mp_cur[MAXN][MAXM]; n char mp_end[MAXN][MAXM]; n vector<pair<int, int>> ans; n int n, m; n void podn(int i, int j); n void opus(int i, int j) { n   if (mp_cur[i][j + 1] ==  L ) { n     podn(i, j + 1); n   } n   ans.push_back({i + 1, j + 1}); n   mp_cur[i][j] =  L ; n   mp_cur[i][j + 1] =  R ; n   mp_cur[i + 1][j] =  L ; n   mp_cur[i + 1][j + 1] =  R ; n } n void podn(int i, int j) { n   if (mp_cur[i + 1][j] ==  U ) { n     opus(i + 1, j); n   } else if (mp_cur[i + 1][j] ==  R ) { n     podn(i + 1, j - 1); n     opus(i + 1, j); n   } n   ans.push_back({i + 1, j + 1}); n   mp_cur[i][j] =  U ; n   mp_cur[i][j + 1] =  U ; n   mp_cur[i + 1][j] =  D ; n   mp_cur[i + 1][j + 1] =  D ; n } n bool uh(int i, int j) { n   if (0 <= i && i < n && 0 <= j && j < m) { n     return false; n   } else { n     return true; n   } n } n bool correct() { n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < m; ++j) { n       if (mp_cur[i][j] ==  U ) { n         if (uh(i + 1, j)) { n           return false; n         } else { n           if (!(mp_cur[i + 1][j] ==  D )) { n             return false; n           } n         } n       } else if (mp_cur[i][j] ==  D ) { n         if (uh(i - 1, j)) { n           return false; n         } else { n           if (!(mp_cur[i - 1][j] ==  U )) return false; n         } n       } else if (mp_cur[i][j] ==  L ) { n         if (uh(i, j + 1)) { n           return false; n         } else { n           if (!(mp_cur[i][j + 1] ==  R )) return false; n         } n       } else if (mp_cur[i][j] ==  R ) { n         if (uh(i, j - 1)) { n           return false; n         } else { n           if (!(mp_cur[i][j - 1] ==  L )) return false; n         } n       } n     } n   } n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < m; ++j) { n       if (mp_end[i][j] ==  U ) { n         if (uh(i + 1, j)) { n           return false; n         } else { n           if (!(mp_end[i + 1][j] ==  D )) return false; n         } n       } else if (mp_end[i][j] ==  D ) { n         if (uh(i - 1, j)) { n           return false; n         } else { n           if (!(mp_end[i - 1][j] ==  U )) return false; n         } n       } else if (mp_end[i][j] ==  L ) { n         if (uh(i, j + 1)) { n           return false; n         } else { n           if (!(mp_end[i][j + 1] ==  R )) return false; n         } n       } else if (mp_end[i][j] ==  R ) { n         if (uh(i, j - 1)) { n           return false; n         } else { n           if (!(mp_end[i][j - 1] ==  L )) return false; n         } n       } n     } n   } n   return true; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> m; n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < m; ++j) { n       cin >> mp_cur[i][j]; n     } n   } n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < m; ++j) { n       cin >> mp_end[i][j]; n     } n   } n   if (!correct()) { n     cout << -1 << endl; n     return 0; n   } n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < m; ++j) { n       if (mp_cur[i][j] != mp_end[i][j]) { n         if (mp_cur[i][j] ==  U ) { n           opus(i, j); n         } else if (mp_cur[i][j] ==  L ) { n           podn(i, j); n         } n       } n     } n   } n   cout << ans.size() << endl; n   for (int i = 0; i < ans.size(); ++i) { n     cout << ans[i].first <<     << ans[i].second << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n namespace solution { n class ISolution { n  public: n   virtual void init(){}; n   virtual bool input() { return false; }; n   virtual void output(){}; n   virtual int run() = 0; n }; n }  // namespace solution n namespace solution {} n namespace solution { n using namespace std; n const int SIZE = 1011; n const int MAX_TIME = 1000011; n int n; n int T[SIZE]; n int C[SIZE]; n long long X[MAX_TIME]; n class Solution : public ISolution { n  public: n   bool input() { n     if (!(cin >> n)) return false; n     for (int i = 0; i < n; ++i) cin >> T[i] >> C[i]; n     return true; n   } n   void solve() { n     fill(X, X + MAX_TIME, 0); n     for (int i = 0; i < n; ++i) X[T[i]] += C[i]; n     long long t = 0; n     long long res = 0; n     long long sum = 0; n     for (long long i = 0; i < MAX_TIME; ++i) { n       if (sum > 0) { n         t = max(t, i); n         sum--; n       } n       sum += X[i]; n       res = max(res, sum); n     } n     if (sum) { n       t += sum; n     } n     cout << t <<     << res << endl; n   } n   int run() { n     while (init(), input()) { n       solve(); n       output(); n     } n     return 0; n   } n }; n }  // namespace solution n int main() { return solution::Solution().run(); } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   scanf( %d , &n); n   printf( %d n , (n + 1) / 2); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   ; n   int bit; n   cin >> bit; n   ; n   int sum = 0; n   bool test = false; n   for (int i = 1; i < n; i++) { n     int h; n     cin >> h; n     ; n     bit -= (h + 1); n     if (bit < 0) { n       cout <<  NO ; n       return 0; n     } n   } n   int h; n   cin >> h; n   ; n   bit -= h; n   if (bit != 0) n     cout <<  NO ; n   else n     cout <<  YES ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n T POW(T b, T p) { n   T r = 1; n   while (p) { n     if (p & 1) r = (r * b); n     b = (b * b); n     p >>= 1; n   } n   return r; n } n template <typename T> n T BigMod(T b, T p, T m) { n   T r = 1; n   while (p) { n     if (p & 1) r = (r * b) % m; n     b = (b * b) % m; n     p >>= 1; n   } n   return r; n } n int main() { n   long long n, a, b, c, cnt; n   while (cin >> n) { n     a = 1; n     b = 1; n     cnt = 0; n     while (1) { n       c = a + b; n       a = b; n       b = c; n       if (c <= n) n         cnt++; n       else n         break; n     } n     cout << cnt << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int arr[n]; n   int count25 = 0; n   int count50 = 0; n   int ans = 0; n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n     if (arr[i] == 25) { n       count25++; n     } else if (arr[i] == 50) { n       if (count25 > 0) { n         count25--; n         count50++; n       } else { n         ans = -1; n       } n     } else if (arr[i] == 100) { n       if ((count25 > 0 && count50 > 0) || (count25 >= 3 && count50 == 0)) { n         if (count50 == 0) { n           count25--; n           count25--; n           count25--; n         } else { n           count25--; n           count50--; n         } n       } else { n         ans = -1; n       } n     } n   } n   if (ans == -1) { n     cout <<  NO ; n   } else { n     cout <<  YES ; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 600005; n const long long mod = 1000000000000000003; n char str[N]; n long long f[N]; n unordered_set<long long> st; n int main() { n   f[0] = 1; n   int n, m; n   for (int i = 1; i < 600000; i++) f[i] = f[i - 1] * 3 % mod; n   cin >> n >> m; n   while (n--) { n     scanf( %s , str); n     long long res = 0; n     int len = strlen(str); n     for (int i = 0; i < len; i++) n       res = (res + f[len - i - 1] * (str[i] -  a )) % mod; n     st.insert(res); n   } n   while (m--) { n     scanf( %s , str); n     long long sum = 0; n     int flag = 0; n     int len = strlen(str); n     for (int i = 0; i < len; i++) n       sum = (sum + f[len - i - 1] * (str[i] -  a )) % mod; n     for (int i = 0; i < len; i++) { n       long long sum2 = (sum - f[len - i - 1] * (str[i] -  a )) % mod; n       if (sum2 < 0) sum2 += mod; n       for (int j = 0; j < 3; j++) { n         if (str[i] -  a  != j) n           if (st.count((sum2 + f[len - i - 1] * j) % mod)) { n             flag = 1; n             break; n           } n       } n       if (flag) break; n     } n     if (flag) n       puts( YES ); n     else n       puts( NO ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 2e6 + 5; n long long mod = 1e9 + 7, bas = 31; n long long h[2][N], pw[N], cnt[N]; n char c[N]; n long long n, q; n long long query(long long l, long long r, long long op) { n   return ((h[op][r] - h[op][l - 1] * pw[cnt[r] - cnt[l - 1]]) % mod + mod) % n          mod; n } n signed main() { n   ios::sync_with_stdio(false); n   cin >> n >> c + 1; n   for (long long i = 1; i <= n; i++) { n     h[0][i] = h[0][i - 1]; n     h[1][i] = h[1][i - 1]; n     cnt[i] = cnt[i - 1]; n     if (c[i] ==  0 ) { n       h[0][i] = (h[0][i] * bas + i % 2 + 1) % mod; n       h[1][i] = (h[1][i] * bas + (i + 1) % 2 + 1) % mod; n       ++cnt[i]; n     } n   } n   pw[0] = 1; n   for (long long i = 1; i <= n; i++) pw[i] = pw[i - 1] * bas % mod; n   cin >> q; n   for (long long i = 1; i <= q; i++) { n     long long t1, t2, len; n     cin >> t1 >> t2 >> len; n     if (query(t1, t1 + len - 1, t1 & 1) == query(t2, t2 + len - 1, t2 & 1)) n       puts( Yes ); n     else n       puts( No ); n   } n } n


#include<bits/stdc++.h> n using namespace std; n #define endl   n  n int main()  n { n    ios_base::sync_with_stdio(false); n    cin.tie(NULL); n    int t; n    cin>>t; n    while(t--) n    { n        long long int n,i,j,k,l; n        cin>>n>>k; n        if(k==1) n        { n            cout<< 1 <<endl; n            continue; n        } n        if(n&1) n        { n            if(k*2>n-2) n            { n                j=(n-2)/2+1; n                l=(k-1)/j; n                k=k+l; n            } n        } n        if(k%n==0) n        cout<<n<<endl; n        else n        cout<<(k%n)<<endl; n    } n    return 0; n }


#include <bits/stdc++.h> n using namespace std; n void doRoutine() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n } n bool isInside(pair<long long int, long long int> p, n               pair<long long int, long long int> a[]) { n   return (p.first >= a[0].first) && (p.second >= a[0].second) && n          (p.first <= a[3].first) && (p.second <= a[3].second); n } n signed main() { n   doRoutine(); n   pair<long long int, long long int> a[4], b[4]; n   for (long long int i = 0; i < 4; i++) cin >> a[i].first >> a[i].second; n   for (long long int i = 0; i < 4; i++) cin >> b[i].first >> b[i].second; n   sort(a, a + 4); n   sort(b, b + 4); n   for (long long int i = b[0].first; i <= b[3].first; i++) { n     long long int f = n         b[0].second - (i <= b[1].first ? (i - b[0].first) : (b[3].first - i)); n     long long int s = (2 * b[0].second) - f; n     for (long long int j = f; j <= s; j++) { n       if (isInside({i, j}, a)) return cout <<  Yes  <<   n , 0; n     } n   } n   cout <<  No  <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, k, x; n int bot[100001]; n int top[100001]; n int mir[101]; n int hr, hl; n int uuh, bbh, ubh, buh; n int L = 100000; n double eps = 0.00001; n int mxr; n int sco(int s, bool up) { n   int res = 0; n   int dh = ((s % 2) ? (up ? uuh : bbh) : (up ? ubh : buh)); n   double f = ((double)(up ? (100 - hl) : hl)) * L / (100 * (s - 1) + dh); n   double T = ((double)L * 100) / (100 * (s - 1) + dh); n   int l1 = 0, l2 = 0, l3 = 0; n   int* u = (up ? top : bot); n   for (int i = 0; i < s; ++i) { n     if ((f - floor(f)) > eps) { n       l3 = u[(int)floor(f)]; n     } else { n       l3 = max(u[(int)floor(f) - 1], u[(int)floor(f)]); n     } n     if ((l3 == l1) || (l3 == 0)) { n       return 0; n     } n     l1 = l2; n     l2 = l3; n     res += mir[l3]; n     u = (u == top ? bot : top); n     f += T; n   } n   return res; n } n int main(int argc, char** argv) { n   cin >> hl >> hr >> n; n   uuh = 200 - hl - hr; n   bbh = hr + hl; n   ubh = 100 + hr - hl; n   buh = 100 + hl - hr; n   int v, a, b; n   char c; n   for (int i = 1; i <= n; ++i) { n     cin >> v >> c >> a >> b; n     mir[i] = v; n     if (c ==  T ) { n       for (int j = a; j < b; ++j) { n         top[j] = i; n       } n     } else { n       for (int j = a; j < b; ++j) { n         bot[j] = i; n       } n     } n   } n   double T; n   int mxr = 0; n   for (k = 1; k <= n; ++k) { n     int res = sco(k, true); n     if (res > mxr) { n       mxr = res; n     } n     res = sco(k, false); n     if (res > mxr) { n       mxr = res; n     } n   } n   cout << mxr; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const int N = 200000; n int a[N], n, m, k; n int check(int x) { n   int cnt = 1, space = k; n   for (int i = x; i < n; i++) { n     if (a[i] <= space) n       space -= a[i]; n     else { n       if (cnt == m) return 0; n       ++cnt, space = k - a[i]; n     } n   } n   return 1; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> m >> k; n   for (int i = 0; i < n; i++) cin >> a[i]; n   int l = 0, r = n - 1, mid; n   while (l != r) { n     mid = (l + r) / 2; n     check(mid) ? r = mid : l = mid + 1; n   } n   cout << n - l <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n #define IOS ios::sync_with_stdio(false), cin.tie(0) n #define debug(a) cout << #a  :   << a << endl n using namespace std; n typedef long long ll; n const int N = 1010; n int a[N][N], b[N][N], mov[3], loc[3]; n int main(void){ n     IOS; n     int T; cin >> T; n     while (T--){ n         int n, m; cin >> n >> m; n         for (int i = 0; i < n; ++i){ n             for (int j = 0; j < n; ++j){ n                 cin >> a[i][j]; n                 a[i][j]--; n             } n         } n         for (int i = 0; i < 3; ++i){ n             mov[i] = 0; n             loc[i] = i; n         } n         string op; cin >> op; n         for (int i = 0; i < m; ++i){ n             if (op[i] ==  D ){ n                 mov[0] = (mov[0] + 1) % n; n             }else if (op[i] ==  U ){ n                 mov[0] = (mov[0] - 1 + n) % n; n             }else if (op[i] ==  L ){ n                 mov[1] = (mov[1] - 1 + n) % n; n             }else if (op[i] ==  R ){ n                 mov[1] = (mov[1] + 1) % n; n             }else if (op[i] ==  I ){ n                 swap(loc[1], loc[2]), swap(mov[1], mov[2]); n             }else{ n                 swap(loc[0], loc[2]), swap(mov[0], mov[2]); n             } n         } n         for (int i = 0; i < n; ++i){ n             for (int j = 0; j < n; ++j){ n                 int tmp[3] = {i, j, a[i][j]}, tp[3]; n                 for (int k = 0; k < 3; ++k){ n                     tp[k] = (tmp[loc[k]] + mov[k]) % n; n                 } n                 b[tp[0]][tp[1]] = tp[2]; n                 //b[tmp[loc[0]]][tmp[loc[1]]] = tmp[loc[2]]; n             } n         } n         for (int i = 0; i < n; ++i){ n             for (int j = 0; j < n; ++j){ n                 cout << b[i][j] + 1 <<    ; n             } n             cout << endl; n         } n         cout << endl; n     } n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n const long long int N = 200005; n const long long int mod = 1e9 + 7; n long long int n, i, j, m, k; n long long int a[N]; n long long int sz[N], c[N]; n long long int vis[N]; n vector<long long int> g[N]; n long long int ans = 0; n void dfs(long long int v, long long int p) { n   vis[v] = c[v]; n   for (auto x : g[v]) { n     if (x != p) { n       dfs(x, v); n       vis[v] += vis[x]; n     } n   } n   ans += min(vis[v], 2 * k - vis[v]); n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> k; n   for (i = 1; i <= 2 * k; i++) { n     long long int x; n     cin >> x; n     c[x] = 1; n   } n   for (i = 1; i < n; i++) { n     long long int x, y; n     cin >> x >> y; n     g[x].push_back(y); n     g[y].push_back(x); n   } n   dfs(1, 0); n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     set<int> s; n     for (int i = 0; i < n; i++) { n       int a; n       cin >> a; n       s.insert(a); n     } n     cout << s.size() <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long Seg[3][1 << 21 + 2], X, Cnt = 1; n pair<long long, int> P[1000001]; n int l_q, r_q, Len; n long long RSQ(int l, int r, int root) { n   if (l > r_q || l_q > r || l > r) return 0; n   if (l >= l_q && r <= r_q) return Seg[Len][root]; n   int mid = (r + l) / 2; n   long long C1 = RSQ(l, mid, root * 2); n   long long C2 = RSQ(mid + 1, r, root * 2 + 1); n   return C1 + C2; n } n void Update(int l, int r, int root) { n   if (l > r_q || l_q > r || l > r) return; n   if (l >= l_q && r <= r_q) { n     Seg[Len][root] += X; n     return; n   } n   int mid = (r + l) / 2; n   Update(l, mid, root * 2); n   Update(mid + 1, r, root * 2 + 1); n   Seg[Len][root] = Seg[Len][root * 2] + Seg[Len][root * 2 + 1]; n } n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) cin >> P[i].first, P[i].second = i; n   sort(P, P + n); n   for (int i = 0; i < n; i++) { n     P[P[i].second].first = Cnt++; n   } n   reverse(P, P + n); n   for (int i = 0; i < n; i++) { n     for (int L = 0; L < 3; L++) { n       l_q = 1; n       r_q = P[i].first - 1; n       Len = L - 1; n       long long val; n       if (L == 0) n         val = 1; n       else n         val = RSQ(1, n, 1); n       l_q = r_q = P[i].first; n       Len = L; n       X = val; n       Update(1, n, 1); n     } n   } n   cout << Seg[2][1] << endl; n   return 0; n } n


#include <bits/stdc++.h> n int main(void) { n   int n; n   while (scanf( %d , &n) != EOF) { n     int k = 0, flag = 0, su; n     for (int i = 9; i >= 0; i--) { n       if (i == n && i != 1) continue; n       if (n % i == 0) { n         k = n / i; n         su = i; n         break; n       } n     } n     if (flag == 0) { n       printf( %d n , k); n       for (int i = 0; i < k; i++) { n         printf( %d , su); n         if (i != k - 1) printf(   ); n       } n       printf(  n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast,unroll-loops ) n #pragma GCC target( avx,avx2,fma ) n using namespace std; n template <typename T> n using vc = vector<T>; n template <typename T> n using uset = unordered_set<T>; n template <typename A, typename B> n using umap = unordered_map<A, B>; n template <typename T, typename Comp = less<T>> n using pq = priority_queue<T, vc<T>, Comp>; n template <typename T> n using minpq = pq<T, greater<T>>; n using ld = long double; n using ll = long long; n using ull = unsigned long long; n using pii = pair<int, int>; n using ppi = pair<pii, int>; n using pip = pair<int, pii>; n using vi = vc<int>; n using vii = vc<pii>; n constexpr char el =   n ; n constexpr char sp =    ; n constexpr int INF = 0x3f3f3f3f; n constexpr ll LLINF = 0x3f3f3f3f3f3f3f3fLL; n int k; n string s; n vi a{-1}; n ll evan(ll x) { return x * (x + 1) / 2; } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout << fixed; n   cin >> k >> s; n   for (int i = 0; i < s.size(); ++i) n     if (s[i] ==  1 ) a.push_back(i); n   a.push_back(s.size()); n   ll ans = 0; n   if (k == 0) { n     for (int i = 1; i < a.size(); ++i) ans += evan(a[i] - a[i - 1] - 1); n     cout << ans; n     exit(0); n   } n   int l = 1, r = k; n   while (r + 1 < a.size()) { n     ll A = a[l] - a[l - 1]; n     ll B = a[r + 1] - a[r]; n     ans += A * B; n     ++l, ++r; n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e9 + 5; n int n, m; n vector<vector<pair<int, int>>> adj; n vector<int> dist, color; n void bfs(int v) { n   queue<int> q; n   q.push(v); n   while (!q.empty()) { n     int u = q.front(); n     q.pop(); n     for (auto it : adj[u]) { n       int to = it.first; n       if (color[to] == -1) { n         color[to] = (it.second == 1) ? 0 : 1; n       } else { n         if (dist[to] == INF && color[to] == it.second) { n           dist[to] = dist[u] + 1; n           q.push(to); n         } n       } n     } n   } n } n int main() { n   cin >> n >> m; n   dist.assign(n, INF); n   adj.resize(n); n   color.assign(n, -1); n   dist[n - 1] = 0; n   for (int i = 0; i < m; ++i) { n     int u, v, t; n     cin >> u >> v >> t; n     u--; n     v--; n     adj[v].push_back({u, t}); n   } n   bfs(n - 1); n   if (dist[0] == INF) { n     cout << -1 <<   n ; n   } else { n     cout << dist[0] <<   n ; n   } n   for (int i = 0; i < n; ++i) { n     if (color[i] == -1) n       cout << 0; n     else n       cout << color[i]; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> a; n vector<long long> dp(2001); n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL), cout.tie(NULL); n   int t = 1; n   long long n, k, x, temp; n   2000000001LL; n   while (t--) { n     cin >> n >> k; n     for (int i = 0; i < n; i++) { n       cin >> x, a.push_back(x); n     } n     long long left = 0, right = 2000000000LL; n     long long required = 2000000001LL; n     while (left < right) { n       x = (left + right) / 2LL; n       required = 2000000001LL; n       for (int i = 0; i < n; i++) { n         dp[i] = i; n         for (int j = 0; j < i; j++) { n           temp = (abs(a[i] - a[j]) + i - j - 1) / (i - j); n           if (temp <= x) dp[i] = min(dp[i], dp[j] + i - j - 1); n         } n         required = min(required, dp[i] + n - i - 1); n       } n       if (required > k) n         left = x + 1; n       else n         right = x; n     } n     cout << left << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string r, s, t, u; n int mini; n int main() { n   int i, j, k; n   cin >> r >> u; n   int n, m = u.size(); n   mini = INT_MAX; n   s =   ; n   for (i = 0; i < m; i++) s.append( 0 ); n   s.append(r); n   for (i = 0; i < m; i++) s.append( 0 ); n   n = s.size(); n   for (i = 0; i < n - m + 1; i++) { n     t = s.substr(i, m); n     int cont = 0; n     for (k = 0; k < m; k++) n       if (t[k] != u[k]) cont++; n     if (mini > cont) mini = cont; n   } n   cout << mini << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   int p = 0, q = 0; n   cin >> n; n   int ar[n]; n   for (int i = 0; i < n; i++) { n     cin >> ar[i]; n   } n   for (int i = 0; i < n; i++) { n     if (ar[i] == 5) n       p++; n     else n       q++; n   } n   int r = p / 9; n   if (q == 0) n     cout <<  -1 ; n   else if (p < 9 && q != 0) n     cout <<  0 ; n   else { n     for (int i = 1; i <= r; i++) { n       cout <<  555555555 ; n     } n     for (int i = 1; i <= q; i++) { n       cout <<  0 ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 100100; n long long mabs(long long x) { return x < 0 ? -x : x; } n int n, m; n long long a[M], h[M]; n long long q[M], c[M], qf, qs, sum; n void read() { n   cin >> n >> m; n   for (int i = 0; i < n; ++i) { n     cin >> h[i]; n   } n   for (int i = 0; i + 1 < n; ++i) { n     a[i] = mabs(h[i + 1] - h[i]); n   } n } n void ini() { n   qf = qs = 0; n   sum = 0; n } n void pop() { n   --qf; n   sum -= q[qf] * c[qf]; n } n void push(long long x, long long cnt) { n   assert(qs == qf || q[qf - 1] > x); n   sum += x * cnt; n   q[qf] = x; n   c[qf] = cnt; n   ++qf; n } n void add(long long x) { n   long long cur = 1; n   while (qs < qf && q[qf - 1] <= x) { n     pop(); n     cur += c[qf]; n   } n   push(x, cur); n } n long long get(long long l, long long r) { n   long long ans = 0; n   ini(); n   for (int i = l; i < r; ++i) { n     add(a[i]); n     ans += sum; n   } n   return ans; n } n void kill() { n   for (int i = 0; i < m; ++i) { n     int l, r; n     cin >> l >> r; n     --l; n     --r; n     cout << get(l, r) <<   n ; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cout.precision(13); n   cout << fixed; n   read(); n   kill(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using namespace rel_ops; n const double PI = acos((double)-1); n int ts, ts2, ts3, ts4; n int n, m; n long long gcd(long long x, long long y) { n   long long t; n   for (; y != 0;) { n     t = x % y; n     x = y; n     y = t; n   } n   return x; n } n class point { n  public: n   double x, y; n   point() { x = y = 0; } n   point(double a, double b) { x = a, y = b; } n   inline friend point operator+(const point &A, const point &B) { n     return point(A.x + B.x, A.y + B.y); n   } n   inline friend point operator-(const point &A, const point &B) { n     return point(A.x - B.x, A.y - B.y); n   } n   inline friend point operator*(const point &A, double p) { n     return point(A.x * p, A.y * p); n   } n   inline friend point operator/(const point &A, double p) { n     return point(A.x / p, A.y / p); n   } n   inline friend void operator+=(point &A, const point &B) { A = A + B; } n   inline friend void operator-=(point &A, const point &B) { A = A - B; } n   inline friend void operator*=(point &A, double B) { A = A * B; } n   inline friend void operator/=(point &A, double B) { A = A / B; } n   inline friend bool operator<(const point &A, const point &B) { n     return A.x < B.x - 1E-10 || fabs(A.x - B.x) < 1E-10 && A.y + 1E-10 < B.y; n   } n   inline friend bool operator==(const point &A, const point &B) { n     return !(A < B || B < A); n   } n   inline friend bool operator>(const point &A, const point &B) { return B < A; } n }; n class XL { n  public: n   point a, b; n   XL() {} n   XL(point x, point y) { n     a = x; n     b = y; n   } n   XL(double t1, double t2, double t3, double t4) { n     a.x = t1; n     a.y = t2; n     b.x = t3; n     b.y = t4; n   } n }; n inline double gag(double x) { n   if (x < -PI - 1E-10) return gag(x + 2 * PI); n   if (x > PI - 1E-10) return gag(x - 2 * PI); n   return x; n } n inline double gag2(double x) { n   if (x < -1E-10) return gag2(x + 2 * PI); n   if (x > 2 * PI - 1E-10) return gag2(x - 2 * PI); n   return x; n } n inline double sqr(double x) { return x * x; } n inline double chaji(const point &A, const point &B) { n   return A.x * B.y - B.x * A.y; n } n inline double dianji(const point &A, const point &B) { n   return A.x * B.x + A.y * B.y; n } n inline double dis(const point &A, const point &B) { n   return sqrt(sqr(A.x - B.x) + sqr(A.y - B.y)); n } n inline double dis(const XL &A) { return dis(A.a, A.b); } n inline double dis(const point &A) { return sqrt(sqr(A.x) + sqr(A.y)); } n inline double atan2(const point &A) { return gag(atan2(A.y, A.x)); } n inline point shengcheng(double JD, double CD = 1.0) { n   return point(cos(JD) * CD, sin(JD) * CD); n } n inline short sgn(double x) { n   if (fabs(x) < 1E-10) n     return 0; n   else if (x < 0) n     return -1; n   else n     return 1; n } n point ref(point a, point b) { return point(b.x * 2 - a.x, b.y * 2 - a.y); } n point s[5]; n double sqr(point a) { return sqr(a.x) + sqr(a.y); } n int getcan; n point getwjy(point a, point b, point c) { n   double d = 2 * (a.x * (b.y - c.y) + b.x * (c.y - a.y) + c.x * (a.y - b.y)); n   getcan = 1; n   if (d == 0) { n     getcan = 0; n     return point(0, 0); n   } n   return point( n       (sqr(a) * (b.y - c.y) + sqr(c) * (a.y - b.y) + sqr(b) * (c.y - a.y)) / d, n       (sqr(a) * (c.x - b.x) + sqr(b) * (a.x - c.x) + sqr(c) * (b.x - a.x)) / d); n } n bool jud(point a, point b, point c) { n   double u1, u2, u3, u4, u5, u6, u7, u8, u9; n   s[2] = getwjy(a, ref(c, b), b); n   if (getcan == 0) return 0; n   s[3] = getwjy(ref(a, b), b, c); n   if (getcan == 0) return 0; n   s[1] = ref(s[2], a); n   s[4] = ref(s[3], c); n   int i; n   for (i = 1; i <= 4; i++) n     if ((u1 = chaji(s[i % 4 + 1] - s[i], s[(i + 1) % 4 + 1] - s[i % 4 + 1])) * n             (u2 = chaji(s[(i + 1) % 4 + 1] - s[(i % 4 + 1)], n                         s[((i + 1) + 1) % 4 + 1] - s[(i + 1) % 4 + 1])) <= n         0) n       break; n   if (i <= 4) return 0; n   cout <<  YES n ; n   for (int i = 1; i <= 4; i++) n     printf( %.9lf %.9lf%c , s[i].x, s[i].y, i == 4 ?   n  :    ); n   return 1; n } n int main() { n   long long i, j, k, l, t1, t2, t3, t4, t5, t6, t7, t8, t9, t, nw; n   int tt1, tt2, tt3, tt4; n   double u1, u2, u3, u4, u5, u6, u7, u8, u9; n   char c1, c2, c3; n   srand((unsigned)time(0)); n   int _; n   point a, b, c; n   for (cin >> _; _--;) { n     cin >> a.x >> a.y >> b.x >> b.y >> c.x >> c.y; n     if (jud(c, a, b)) continue; n     if (jud(a, b, c)) continue; n     if (jud(b, c, a)) continue; n     cout <<  NO n n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct edge { n   int src, dst; n }; n int const MOD = 1000000007; n long long _MOD = 1000000009; n double EPS = 1e-9; n long long INF = LLONG_MAX / 2; n int main() { n   int N, w; n   cin >> N >> w; n   vector<int> a(N); n   for (int i = 0; i < N; i++) cin >> a[i]; n   vector<int> b(N); n   int sum = 0; n   for (int i = 0; i < N; i++) { n     b[i] = (a[i] + 1) / 2; n     sum += b[i]; n   } n   if (sum > w) { n     cout << -1 << endl; n     return 0; n   } n   vector<pair<int, int> > ai(N); n   for (int i = 0; i < N; i++) ai[i] = pair<int, int>(a[i], i); n   sort(ai.rbegin(), ai.rend()); n   for (int t = 0; t < N; t++) { n     int i = ai[t].second; n     int d = min(a[i] - b[i], w - sum); n     b[i] += d; n     sum += d; n   } n   for (int i = 0; i < N; i++) cout << b[i] <<    ; n   cout << endl; n } n