#include <bits/stdc++.h> n using namespace std; n const int N = 1010; n void solve() { n   int n, m; n   cin >> n >> m; n   vector<vector<long long>> a = n       vector<vector<long long>>(n, vector<long long>(m, 0)); n   for (int i = 0; i < n; ++i) n     for (int j = 0; j < m; ++j) cin >> a[i][j]; n   vector<long long> col(m, 0), row(n, 0); n   for (int i = 0; i < m; ++i) n     for (int j = 0; j < n; ++j) col[i] += a[j][i]; n   for (int i = 0; i < n; ++i) n     for (int j = 0; j < m; ++j) row[i] += a[i][j]; n   vector<long long> x(m + 1), y(n + 1); n   long long mn = LLONG_MAX; n   long long ans = 0; n   int ansx = 0; n   for (int i = 0; i <= m; ++i) { n     long long curx = i * 4; n     for (int j = 0; j < m; ++j) { n       long long curpos = j * 4 + 2; n       long long dx = curx - curpos; n       x[i] += dx * dx * col[j]; n     } n     if (x[i] < mn) { n       mn = x[i]; n       ansx = i; n     } n   } n   ans += mn; n   mn = LLONG_MAX; n   int ansy = 0; n   for (int i = 0; i <= n; ++i) { n     int cury = i * 4; n     for (int j = 0; j < n; ++j) { n       long long curpos = j * 4 + 2; n       long long dy = cury - curpos; n       y[i] += dy * dy * row[j]; n     } n     if (y[i] < mn) { n       mn = y[i]; n       ansy = i; n     } n   } n   ans += mn; n   cout << ans <<   n ; n   cout << ansy <<     << ansx <<   n ; n } n int main(void) { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int tc = 1; n   while (tc--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = 1LL << 60; n const long long md = 1e9 + 7; n const long double eps = 1e-14; n const int mx = 3010; n int t[mx]; n void upd(int p) { n   for (++p; p < mx; p += p & -p) ++t[p]; n } n int que(int p) { n   int res = 0; n   for (; p > 0; p -= p & -p) res += t[p]; n   return res; n } n char zey[mx][mx]; n int le[mx][mx], ri[mx][mx]; n int solve(vector<int> &a, vector<int> &b) { n   int sz = a.size(); n   if (sz == 0) return 0; n   int res = 0; n   fill(t, t + sz + 5, 0); n   vector<pair<int, int> > bb(sz); n   for (int i = (0); i < int(sz); ++i) bb[i] = pair<int, int>(i - b[i] + 1, i); n   sort(bb.begin(), bb.end()); n   int ix = 0; n   for (int i = (0); i < int(sz); ++i) { n     while (ix < sz && bb[ix].first <= i) upd(bb[ix++].second); n     res += que(min(sz, i + a[i])) - i; n   } n   return res; n } n int main() { n   memset(t, 0, sizeof(t)); n   int n, m; n   cin >> n >> m; n   for (int i = (0); i < int(n); ++i) scanf(  %s  , zey[i]); n   for (int i = (0); i < int(n); ++i) { n     le[i][0] = zey[i][0] ==  z ; n     ri[i][m - 1] = zey[i][m - 1] ==  z ; n   } n   for (int i = (0); i < int(n); ++i) n     for (int j = (1); j < int(m); ++j) n       le[i][j] = zey[i][j] ==  z  ? 1 + le[i][j - 1] : 0; n   for (int i = (0); i < int(n); ++i) n     for (int j = (m - 1); j-- > int(0);) n       ri[i][j] = zey[i][j] ==  z  ? 1 + ri[i][j + 1] : 0; n   long long ans = 0; n   for (int s = (0); s < int(m + n - 1); ++s) { n     vector<int> a, b; n     for (int d = (max(0, s - m + 1)); d < int(min(n, s + 1)); ++d) { n       if (zey[d][s - d] ==  z ) { n         a.push_back(le[d][s - d]); n         b.push_back(ri[d][s - d]); n       } else { n         ans += solve(a, b); n         a.clear(); n         b.clear(); n       } n     } n     ans += solve(a, b); n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n unsigned long long val(int i) { return i + 1; } n unsigned long long pot1[100001], pot2[100001], prec1[10][100001], n     prec2[10][100001]; n int v[100000]; n struct Node { n   unsigned long long h1, h2; n   int flag; n   bool valid; n   int sz; n   Node() { n     sz = h1 = h2 = 0; n     flag = -1; n     valid = true; n   } n   Node(unsigned long long a, unsigned long long b, int c) { n     h1 = a; n     h2 = b; n     sz = c; n     flag = -1; n     valid = true; n   } n   Node(unsigned long long a, unsigned long long b, int c, bool d) { n     h1 = a; n     h2 = b; n     sz = c; n     valid = d; n     flag = -1; n   } n   void set(int c) { flag = c; } n   void reset() { flag = -1; } n   void update(int c) { n     if (c == -1) return; n     h1 = prec1[c][sz]; n     h2 = prec2[c][sz]; n   } n   bool operator!=(const Node& n) { return h1 != n.h1 || h2 != n.h2; } n }; n Node st[400000]; n inline int left(int i) { return 2 * i + 1; } n inline int right(int i) { return 2 * i + 2; } n void initialize(int r, int beg, int end) { n   if (beg == end) { n     st[r] = Node(val(v[beg]), val(v[beg]), 1); n   } else { n     int h = (beg + end) / 2; n     initialize(left(r), beg, h); n     initialize(right(r), h + 1, end); n     st[r] = Node( n         (st[left(r)].h1 * pot1[st[right(r)].sz] + st[right(r)].h1) % 1000000009, n         (st[left(r)].h2 * pot2[st[right(r)].sz] + st[right(r)].h2) % 1000000007, n         st[left(r)].sz + st[right(r)].sz); n   } n } n void update(int r, int beg, int end, int qbeg, int qend, char c) { n   if (end < qbeg || beg > qend) { n     if (st[r].flag != -1) { n       st[r].update(st[r].flag); n       if (beg != end) { n         st[2 * r + 1].set(st[r].flag); n         st[2 * r + 2].set(st[r].flag); n       } n       st[r].reset(); n     } n     return; n   } n   if (beg >= qbeg && end <= qend) { n     st[r].update(c); n     if (beg != end) { n       st[left(r)].set(c); n       st[right(r)].set(c); n     } n     st[r].reset(); n   } else { n     int h = (beg + end) / 2; n     if (st[r].flag != -1) { n       st[r].update(st[r].flag); n       st[2 * r + 1].set(st[r].flag); n       st[2 * r + 2].set(st[r].flag); n       st[r].reset(); n     } n     update(left(r), beg, h, qbeg, qend, c); n     update(right(r), h + 1, end, qbeg, qend, c); n     st[r] = Node( n         (st[left(r)].h1 * pot1[st[right(r)].sz] + st[right(r)].h1) % 1000000009, n         (st[left(r)].h2 * pot2[st[right(r)].sz] + st[right(r)].h2) % 1000000007, n         st[right(r)].sz + st[left(r)].sz); n   } n } n Node query(int r, int beg, int end, int qbeg, int qend) { n   if (end < qbeg || beg > qend) return Node(0, 0, false); n   if (beg >= qbeg && end <= qend) { n     if (st[r].flag != -1) { n       st[r].update(st[r].flag); n       if (beg != end) { n         st[left(r)].set(st[r].flag); n         st[right(r)].set(st[r].flag); n       } n       st[r].reset(); n     } n     return st[r]; n   } else { n     if (st[r].flag != -1) { n       st[r].update(st[r].flag); n       st[left(r)].set(st[r].flag); n       st[right(r)].set(st[r].flag); n       st[r].reset(); n     } n     Node le = query(left(r), beg, (beg + end) / 2, qbeg, qend); n     Node ri = query(right(r), (beg + end) / 2 + 1, end, qbeg, qend); n     if (le.valid && ri.valid) n       return Node((le.h1 * pot1[ri.sz] + ri.h1) % 1000000009, n                   (le.h2 * pot2[ri.sz] + ri.h2) % 1000000007, le.sz + ri.sz); n     else if (le.valid) n       return le; n     else n       return ri; n   } n } n bool qquery(int r, int beg, int end, int qbeg, int qend, int d) { n   int sz = qend - qbeg + 1; n   if (sz <= d) return true; n   if (qbeg > qend) return 0; n   int sobra = sz % d; n   if (sobra) { n     if (query(r, beg, end, qbeg, qbeg + sobra - 1) != n         query(r, beg, end, qend - sobra + 1, qend)) n       return false; n   } n   qend -= sobra; n   sz = qend - qbeg + 1; n   int n = sz / d; n   if (sz == d) return true; n   if (n & 1) { n     int b0 = qbeg, b1 = qbeg + n / 2 * d, b2 = qbeg + (n / 2 + 1) * d; n     int e0 = b1 - 1, e1 = b2 - 1, e2 = qend; n     if (query(r, beg, end, b0, e0) != query(r, beg, end, b2, e2) || n         query(r, beg, end, b0, b0 + d - 1) != query(r, beg, end, b1, e1)) n       return false; n     return qquery(r, beg, end, b0, e0, d); n   } else { n     int b0 = qbeg, b1 = qbeg + (n + 1) / 2 * d; n     int e0 = b1 - 1, e1 = qend; n     if (query(r, beg, end, b0, e0) != query(r, beg, end, b1, e1)) return false; n     return qquery(r, beg, end, b0, e0, d); n   } n } n void precalc() { n   pot1[0] = 1; n   pot2[0] = 1; n   for (int i = 1; i <= 100000; i++) { n     pot1[i] = (pot1[i - 1] * 33) % 1000000009; n     pot2[i] = (pot2[i - 1] * 33) % 1000000007; n   } n   for (int i = 0; i < 10; i++) { n     prec1[i][0] = 0; n     prec2[i][0] = 0; n     for (int j = 1; j <= 100000; j++) { n       prec1[i][j] = (prec1[i][j - 1] * 33 + val(i)) % 1000000009; n       prec2[i][j] = (prec2[i][j - 1] * 33 + val(i)) % 1000000007; n     } n   } n } n int main(void) { n   int n, m, k; n   char s[100001]; n   precalc(); n   scanf( %d%d%d , &n, &m, &k); n   scanf( %s , s); n   for (int i = 0; i < n; i++) { n     v[i] = s[i] -  0 ; n   } n   initialize(0, 0, n - 1); n   for (int i = 0; i < m + k; i++) { n     int cmd; n     int l, r, x; n     scanf( %d%d%d%d , &cmd, &l, &r, &x); n     if (cmd == 1) { n       l--; n       r--; n       update(0, 0, n - 1, l, r, x); n     } else { n       l--; n       r--; n       printf( %s n , qquery(0, 0, n - 1, l, r, x) ?  YES  :  NO ); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100005, M = 500005; n vector<int> gr[N], path, odd; n pair<int, int> ed[M]; n bool us[M]; n void GetEulerCircuit(int u) { n   while (true) { n     while (!gr[u].empty() && us[gr[u].back()]) { n       gr[u].pop_back(); n     } n     if (gr[u].empty()) { n       break; n     } n     us[gr[u].back()] = true; n     int v = u ^ ed[gr[u].back()].first ^ ed[gr[u].back()].second; n     gr[u].pop_back(); n     GetEulerCircuit(v); n   } n   path.push_back(u); n } n int main() { n   ios_base::sync_with_stdio(false); n   cout.tie(0); n   int n, m; n   scanf( %d %d  , &n, &m); n   for (int i = 0; i < m; i++) { n     int u, v; n     scanf( %d %d  , &u, &v); n     ed[i] = make_pair(u, v); n     gr[u].push_back(i); n     gr[v].push_back(i); n   } n   for (int u = 1; u <= n; u++) { n     if (gr[u].size() % 2) { n       odd.push_back(u); n     } n   } n   for (int i = 0; i < odd.size(); i += 2) { n     int u = odd[i], v = odd[i + 1]; n     ed[m] = make_pair(u, v); n     gr[u].push_back(m); n     gr[v].push_back(m); n     m++; n   } n   if (m % 2) { n     ed[m] = make_pair(1, 1); n     gr[1].push_back(m); n     gr[1].push_back(m); n     m++; n   } n   GetEulerCircuit(1); n   for (int i = 0; i < m; i++) { n     int u = path[i], v = path[i + 1]; n     if (i % 2) { n       swap(u, v); n     } n     ed[i] = make_pair(u, v); n   } n   printf( %d n , m); n   for (int i = 0; i < m; i++) { n     int u = ed[i].first, v = ed[i].second; n     printf( %d %d n , u, v); n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline void read(T &x) { n   x = 0; n   T f = 1; n   char c = getchar(); n   for (; !isdigit(c); c = getchar()) n     if (c ==  - ) f = -1; n   for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + (c ^ 48); n   x *= f; n } n int main() { n   int n; n   read(n); n   for (int i = 1; i <= n; i++) { n     int x; n     read(x); n     cout << x / 6 + (x % 6 == 0 ? 0 : 1) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e6; n int q[maxn], fin, razbeg, jump, n; n pair<int, int> ans[maxn]; n int sz = 0; n int main() { n   cin >> n >> fin >> razbeg >> jump; n   for (int i = 0; i < n; i++) cin >> q[i]; n   q[n] = 0; n   q[n + 1] = fin; n   n += 2; n   sort(q, q + n); n   int i = 0, now = 0; n   while (i < n - 1) n     if (i == n - 2) { n       if (fin - now > 0) ans[sz] = make_pair(0, fin - now), sz++; n       break; n     } else if (q[i + 1] - 1 - now < razbeg) { n       cout <<  IMPOSSIBLE ; n       return 0; n     } else { n       if (q[i + 1] - 1 - now > 0) n         ans[sz] = make_pair(0, q[i + 1] - 1 - now), sz++; n       now = q[i + 1] - 1; n       while (1) { n         i++; n         if (q[i] + 1 - now > jump) { n           cout <<  IMPOSSIBLE ; n           return 0; n         } n         if (i == n - 2) { n           ans[sz] = make_pair(1, q[i] + 1 - now), sz++, now = q[i] + 1; n           break; n         } else if (q[i + 1] - 1 - q[i] - 1 >= razbeg) { n           ans[sz] = make_pair(1, q[i] + 1 - now), sz++, now = q[i] + 1; n           break; n         } n       } n     } n   for (int i = 0; i < sz; i++) n     if (ans[i].first == 0) n       cout <<  RUN   << ans[i].second <<   n ; n     else n       cout <<  JUMP   << ans[i].second <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, q, c[100005], v[100005], f[100005]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> q; n   for (int i = 1; i <= n; i++) cin >> v[i]; n   for (int i = 1; i <= n; i++) cin >> c[i]; n   while (q--) { n     long long a, b, ans = 0, mx = 0, smx = 0; n     memset(f, -6, sizeof(f)); n     cin >> a >> b; n     for (int i = 1; i <= n; i++) { n       long long cur = max(v[i] * b, f[c[i]] + v[i] * a); n       if (c[i] != mx) n         cur = max(cur, f[mx] + v[i] * b); n       else n         cur = max(cur, f[smx] + v[i] * b); n       if (cur > f[mx]) { n         if (mx != c[i]) smx = mx; n         mx = c[i]; n       } else if (cur > f[smx] && c[i] != mx) n         smx = c[i]; n       f[c[i]] = max(f[c[i]], cur); n       ans = max(ans, cur); n     } n     cout << ans <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int a[4010][4010]; n int n, m; n int u[4010], v[4010], sz[4010]; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= m; i++) { n     scanf( %d%d , &u[i], &v[i]); n     a[u[i]][v[i]] = a[v[i]][u[i]] = 1; n     sz[u[i]]++; n     sz[v[i]]++; n   } n   int ans = 1e9; n   for (int i = 1; i <= m; i++) { n     int x = u[i], y = v[i]; n     for (int j = i + 1; j <= m; j++) { n       int c = u[j], d = v[j]; n       if (x == c && a[y][d] == 1) { n         ans = min(ans, sz[x] + sz[y] + sz[d] - 6); n       } else if (x == d && a[y][c] == 1) { n         ans = min(ans, sz[x] + sz[c] + sz[y] - 6); n       } else if (y == c && a[x][d] == 1) { n         ans = min(ans, sz[y] + sz[x] + sz[d] - 6); n       } else if (y == d && a[x][c] == 1) { n         ans = min(ans, sz[y] + sz[x] + sz[c] - 6); n       } n     } n   } n   if (ans == 1e9) n     cout << -1 << endl; n   else n     cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXK = 26; n const long long MOD = 1e9 + 7; n int last[MAXK], ord[MAXK]; n long long sum, dp[MAXK]; n void add(int c) { n   sum = (sum - dp[c] + MOD) % MOD; n   dp[c] = (dp[c] + sum + 1) % MOD; n   sum = (sum + dp[c]) % MOD; n } n int main() { n   ios_base::sync_with_stdio(false); n   int N, K; n   string S; n   cin >> N >> K >> S; n   memset((last), (-1), sizeof(last)); n   sum = 0; n   for (int _n((int)S.size()), i(0); i < _n; i++) { n     int c = S[i] -  a ; n     last[c] = i; n     add(c); n   } n   iota(ord, ord + K, 0); n   sort(ord, ord + K, [](int a, int b) { return last[a] < last[b]; }); n   for (int _n(N), i(0); i < _n; i++) { n     int c = ord[i % K]; n     add(c); n   } n   cout << (sum + 1) % MOD << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma GCC optimize( Ofast ) n const int INF = 1e9 + 7; n const long long INF2 = 1e15; n void solve(void) {} n int main(void) { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long n, l, r, ql, qr; n   cin >> n >> l >> r >> ql >> qr; n   vector<long long> v(n); n   for (int i = 0; i < n; i++) cin >> v[i]; n   long long total = 0; n   vector<long long> left(n + 1), right(n + 1); n   for (long long i = 0; i < n; i++) { n     total += v[i] * l; n     left[i + 1] = total; n   } n   total = 0; n   for (long long i = n - 1; i >= 0; i--) { n     total += v[i] * r; n     right[i] = total; n   } n   long long ans = INF, current; n   for (long long i = 0; i <= n; i++) { n     long long a = i, b = n - i, change; n     current = INF; n     change = min(a, b); n     a -= change; n     b -= change; n     if (a) { n       a--; n       current = min(current, a * ql); n     } else if (b) { n       current = min(current, b * qr); n     } else { n       current = 0; n     } n     a = i, b = n - i; n     change = min(a, b); n     a -= change; n     b -= change; n     if (b) { n       b--; n       current = min(current, b * qr); n     } else if (a) { n       current = min(current, a * ql); n     } else { n       current = 0; n     } n     long long temp = 0; n     a = i, b = n - i; n     if (b) { n       b--; n       temp += qr * b; n     } n     if (a) { n       a--; n       temp += ql * a; n     } n     current = min(current, temp); n     ans = min(ans, current + left[i] + right[i]); n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n using namespace std; n int main() { n   int n, k, q; n   cin >> n >> k >> q; n   vector<long long> vec(n + 1); n   vector<long long> pq; n   map<long long, long long> ar, ans, rev; n   for (int i = 1; i <= n; i++) cin >> vec[i], ar[i] = vec[i], rev[vec[i]] = i; n   while (q--) { n     int a, b; n     cin >> a >> b; n     if (a == 1) { n       pq.push_back(ar[b]); n       ans[b] = 1; n       if (pq.size() > k) { n         sort(pq.begin(), pq.end()); n         long long dd = rev[pq[0]]; n         ans[dd] = 0; n         pq.erase(pq.begin()); n       } n     } else { n       if (ans[b]) { n         cout <<  YES n ; n       } else n         cout <<  NO n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int lowbit(int x) { return x & -x; } n inline int ABS(int x) { return x < 0 ? -x : x; } n inline long long ABS(long long x) { return x < 0 ? -x : x; } n inline void MAX(int &a, int b) { n   if (b > a) a = b; n } n inline void MAX(long long &a, long long b) { n   if (b > a) a = b; n } n inline void MIN(int &a, int b) { n   if (b < a) a = b; n } n inline void MIN(long long &a, long long b) { n   if (b < a) a = b; n } n inline void SUM(int &a, int b) { a = (a + b) % 1000000007; } n int fast(int x, int v) { n   int ans = 1; n   for (; v; v >>= 1, x = (long long)x * x % 1000000007) n     if (v & 1) ans = (long long)ans * x % 1000000007; n   return ans; n } n long long fast(long long x, long long v) { n   long long ans = 1; n   for (; v; v >>= 1, x = x * x % 1000000007) n     if (v & 1) ans = ans * x % 1000000007; n   return ans; n } n inline long long sqr(int x) { return x * x; } n int n, a[300005], l, r, sum; n long long pre[300005], ans; n map<int, int> s; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) { n     scanf( %d , a + i); n     pre[i] = pre[i - 1] + (a[i] < 0 ? 0 : a[i]); n     if (!s[a[i]]) s[a[i]] = i; n   } n   ans = -0x7f7f7f7f; n   for (int i = n; i >= 1; --i) n     if (s[a[i]] && s[a[i]] != i && pre[i - 1] - pre[s[a[i]]] + a[i] * 2 > ans) { n       ans = pre[i - 1] - pre[s[a[i]]] + a[i] * 2; n       l = s[a[i]]; n       r = i; n     } n   for (int i = l + 1; i <= r - 1; ++i) n     if (a[i] < 0) ++sum; n   printf( %I64d %d n , ans, sum + l - 1 + n - r); n   for (int i = 1; i <= l - 1; ++i) printf( %d  , i); n   for (int i = l + 1; i <= r - 1; ++i) n     if (a[i] < 0) printf( %d  , i); n   for (int i = r + 1; i <= n; ++i) printf( %d  , i); n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 9223372036854775807; n const long long MOD = 1000000007; n const long double PI = acos(-1); n inline long long power(long long a, long long b, long long z = MOD) { n   long long res = 1; n   a = (a % z); n   while (b) { n     if (b & 1) res = (res * a) % z; n     a = (a * a) % z; n     b = b >> 1; n   } n   return res; n } n inline long long inverse(long long a, long long z = MOD) { n   return power(a, z - 2, z); n } n long long extend(long long a, long long b, long long &x, long long &y) { n   if (b == 0) { n     x = 1; n     y = 0; n     return a; n   } n   long long x1, y1; n   long long g = extend(b, a % b, x1, y1); n   x = y1; n   y = x1 - (a / b) * y1; n   return g; n } n inline long long mat_power(long long a, long long b, long long c, long long d, n                            long long n, long long z = MOD) { n   long long a0 = 1, b0 = 0, c0 = 0, d0 = 1; n   while (n) { n     if (n & 1) { n       long long a1 = a0, b1 = b0, c1 = c0, d1 = d0; n       a0 = (a1 * a + b1 * c) % z; n       b0 = (a1 * b + b1 * d) % z; n       c0 = (c1 * a + d1 * c) % z; n       d0 = (c1 * b + d1 * d) % z; n     } n     long long a1 = a, b1 = b, c1 = c, d1 = d; n     a = (a1 * a1 + b1 * c1) % z; n     b = (a1 * b1 + b1 * d1) % z; n     c = (c1 * a1 + d1 * c1) % z; n     d = (c1 * b1 + d1 * d1) % z; n     n = n >> 1; n   } n   return c0; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n, m; n   cin >> n >> m; n   int aray[n][m], arr[n][m]; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) cin >> aray[i][j]; n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) cin >> arr[i][j]; n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (aray[i][j] > arr[i][j]) swap(aray[i][j], arr[i][j]); n     } n   } n   for (int i = 0; i < n; i++) { n     for (int j = 1; j < m; j++) { n       if (aray[i][j] <= aray[i][j - 1] || arr[i][j] <= arr[i][j - 1]) { n         cout <<  Impossible n ; n         return 0; n       } n     } n   } n   for (int j = 0; j < m; j++) { n     for (int i = 1; i < n; i++) { n       if (aray[i][j] <= aray[i - 1][j] || arr[i][j] <= arr[i - 1][j]) { n         cout <<  Impossible n ; n         return 0; n       } n     } n   } n   cout <<  Possible n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   map<char, int> mp; n   mp[ q ] = 9; n   mp[ k ] = 0; n   mp[ r ] = 5; n   mp[ b ] = 3; n   mp[ n ] = 3; n   mp[ p ] = 1; n   int white = 0, black = 0; n   for (int i = 0; i < 8; i++) { n     for (int j = 0; j < 8; j++) { n       char x; n       cin >> x; n       if (x >=  A  && x <=  Z ) white += mp[x + 32]; n       if (x >=  a  && x <=  z ) black += mp[x]; n     } n   } n   if (white > black) n     cout <<  White  << endl; n   else if (white == black) n     cout <<  Draw  << endl; n   else n     cout <<  Black  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 20; n long long int dp[MAXN][MAXN]; n long long int tav[MAXN]; n long long int n; n string s; n long long int dtoc(char x) { return x -  0 ; } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   tav[0] = 1; n   for (int i = 1; i < MAXN; i++) { n     tav[i] = 10 * tav[i - 1]; n   } n   cin >> n; n   cin >> s; n   for (int i = 0; i <= n; i++) { n     for (int j = 0; j <= n; j++) { n       if (i) { n         dp[i][j] = dp[i - 1][j] + dtoc(s[2 * n - i - j]) * tav[i - 1]; n       } n       if (j) { n         dp[i][j] = n             max(dp[i][j], dp[i][j - 1] + dtoc(s[2 * n - i - j]) * tav[j - 1]); n       } n     } n   } n   int i = n; n   int j = n; n   while (i > 0 || j > 0) { n     if (i && dp[i - 1][j] + dtoc(s[2 * n - i - j]) * tav[i - 1] == dp[i][j]) { n       i--; n       cout <<  H ; n     } else { n       j--; n       cout <<  M ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int v[300123], n, ans, g[300123], f[300123], a, b, m, op; n queue<int> q; n int main() { n   ios_base::sync_with_stdio(0); n   cin >> n; n   for (int i = 1; i <= n; ++i) { n     cin >> v[i]; n     if (~v[i]) n       g[v[i]]++; n     else { n       g[0]++; n     } n   } n   ans = 1; n   for (int i = 1; i <= n; ++i) { n     if (g[i] == 0) { n       q.push(i); n       f[i] = 1; n     } n   } n   while (!q.empty()) { n     int u = q.front(); n     q.pop(); n     g[v[u]]--; n     if (g[v[u]] == 0) { n       f[v[u]] = f[u] + 1; n       q.push(v[u]); n       ans = max(ans, f[v[u]]); n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   for (int i = 0; i < t; i++) { n     int x; n     cin >> x; n     int ans = (x % 10 - 1) * 10; n     ans += 1; n     if (x / 10 > 0) ans += 2; n     if (x / 100 > 0) ans += 3; n     if (x / 1000 > 0) ans += 4; n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(NULL); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   while (t--) { n     int a, b, c, m; n     cin >> a >> b >> c >> m; n     int len = a + b + c; n     int maxi = len - 3; n     int valMax = max(a, max(b, c)); n     int mini = max(0, ((2 * valMax) - len - 1)); n     if (m < mini || m > maxi) n       cout <<  NO n ; n     else n       cout <<  YES n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename A, typename B> n ostream &operator<<(ostream &os, const pair<A, B> &p) { n   return os <<  (  << p.first <<  ,   << p.second <<  ) ; n } n template <typename T_container, typename T = typename enable_if< n                                     !is_same<T_container, string>::value, n                                     typename T_container::value_type>::type> n ostream &operator<<(ostream &os, const T_container &v) { n   os <<  { ; n   string sep; n   for (const T &x : v) os << sep << x, sep =  ,  ; n   return os <<  } ; n } n void dbg_out() { cerr << endl; } n template <typename Head, typename... Tail> n void dbg_out(Head H, Tail... T) { n   cerr <<     << H; n   dbg_out(T...); n } n const int INF = 1 << 30; n const int MOD = 1e9 + 7; n const int MAXN = 2e5 + 5; n string asignar(int n) { n   if (n == 0) return  S ; n   if (n == 1) return  M ; n   if (n == 2) return  L ; n   if (n == 3) return  XL ; n   if (n == 4) return  XXL ; n   if (n == 5) return  XXXL ; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   vector<int> stock(6); n   for (long long i = (0); i < (long long)(6); i++) cin >> stock[i]; n   int n; n   cin >> n; n   vector<pair<pair<int, int>, int>> quiero(n); n   for (long long j = (0); j < (long long)(n); j++) { n     string s; n     cin >> s; n     string curr =   ; n     pair<int, int> x = make_pair(-1, -1); n     for (long long i = (0); i < (long long)(s.size()); i++) { n       if (s[i] !=  , ) { n         curr += s[i]; n       } else { n         if (curr ==  S ) x.first = 0; n         if (curr ==  M ) x.first = 1; n         if (curr ==  L ) x.first = 2; n         if (curr ==  XL ) x.first = 3; n         if (curr ==  XXL ) x.first = 4; n         if (curr ==  XXXL ) x.first = 5; n         curr =   ; n       } n     } n     if (curr ==  S ) x.second = 0; n     if (curr ==  M ) x.second = 1; n     if (curr ==  L ) x.second = 2; n     if (curr ==  XL ) x.second = 3; n     if (curr ==  XXL ) x.second = 4; n     if (curr ==  XXXL ) x.second = 5; n     if (x.first == -1) x.first = x.second; n     if (x.first > x.second) swap(x.first, x.second); n     quiero[j] = make_pair(x, j); n   } n   sort((quiero).begin(), (quiero).end()); n   ; n   vector<string> ans(n); n   for (long long i = (0); i < (long long)(n); i++) { n     if (stock[quiero[i].first.first] > 0) { n       ans[quiero[i].second] = asignar(quiero[i].first.first); n       stock[quiero[i].first.first]--; n     } else if (stock[quiero[i].first.second] > 0) { n       ans[quiero[i].second] = asignar(quiero[i].first.second); n       stock[quiero[i].first.second]--; n     } else { n       cout <<  NO n ; n       return 0; n     } n   } n   cout <<  YES n ; n   for (long long i = (0); i < (long long)(n); i++) cout << ans[i] <<   n ; n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int in() { n   int x; n   scanf( %d , &x); n   return x; n } n const int N = 1e6 + 7; n int n, m, a[N], Q; n long long SUM, psr[N], psl[N]; n int en[N], st[N]; n pair<int, long long> dpl[N], dpr[N]; n int solve(long long maax) { n   if (maax >= SUM) return 1; n   long long sum = 0, cur = 0; n   for (int i = 0; i < n; i++) { n     sum += a[i]; n     while (sum > maax) { n       en[cur] = i - 1; n       sum -= a[cur]; n       cur++; n     } n   } n   while (cur < n) en[cur++] = n - 1; n   sum = 0, cur = n - 1; n   for (int i = n - 1; i >= 0; i--) { n     sum += a[i]; n     while (sum > maax) { n       st[cur] = i + 1; n       sum -= a[cur]; n       cur--; n     } n   } n   while (cur >= 0) st[cur--] = 0; n   for (int i = 0; i < n; i++) { n     if (st[i] == 0) n       dpl[i] = {1, psl[i + 1]}; n     else n       dpl[i] = dpl[st[i] - 1], dpl[i].first++; n   } n   for (int i = n - 1; i >= 0; --i) { n     if (en[i] == n - 1) n       dpr[i] = {1, psr[i]}; n     else n       dpr[i] = dpr[en[i] + 1], dpr[i].first++; n   } n   int mn = dpr[0].first; n   for (int i = 1; i < n; i++) n     mn = min(mn, dpr[i].first + dpl[i - 1].first - n                      ((dpr[i].second + dpl[i - 1].second) <= maax)); n   return mn; n } n int32_t main() { n   n = in(), Q = in(); n   for (int i = 0; i < n; i++) { n     a[i] = in(); n     SUM += a[i]; n     psl[i + 1] = a[i] + psl[i]; n   } n   for (int i = n - 1; i >= 0; --i) psr[i] = psr[i + 1] + a[i]; n   while (Q--) { n     long long x; n     cin >> x; n     cout << solve(x) <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int a[1005][1005], used[1005][1005]; n const int dx[4] = {0, 0, 1, -1}; n const int dy[4] = {1, -1, 0, 0}; n void DFS(int i, int j, int N, int M) { n   if (i == 0 || i == N + 1 || j == 0 || j == M + 1 || used[i][j]) return; n   used[i][j] = 1; n   for (int d = 0; d < 4; ++d) n     if (a[i + dx[d]][j + dy[d]] == 1) DFS(i + dx[d], j + dy[d], N, M); n } n int main() { n   int N, M; n   scanf( %d%d , &N, &M); n   int numberBlacks = 0; n   for (int i = 1; i <= N; ++i) { n     char s[1005]; n     scanf( %s , s + 1); n     for (int j = 1; j <= M; ++j) n       if (s[j] ==  # ) { n         a[i][j] = 1; n         ++numberBlacks; n       } n   } n   if (numberBlacks == 0) { n     printf( 0 n ); n     return 0; n   } n   int emptyLines = 0; n   for (int i = 1; i <= N; ++i) { n     int cnt = 0, numberLineSegments = 0; n     for (int j = 1; j <= M; ++j) { n       cnt += a[i][j]; n       if (a[i][j - 1] == 0 && a[i][j] == 1) ++numberLineSegments; n     } n     if (numberLineSegments > 1) { n       printf( -1 n ); n       return 0; n     } n     if (cnt == 0) ++emptyLines; n   } n   int emptyColumns = 0; n   for (int j = 1; j <= M; ++j) { n     int cnt = 0, numberColumnSegments = 0; n     for (int i = 1; i <= N; ++i) { n       cnt += a[i][j]; n       if (a[i - 1][j] == 0 && a[i][j] == 1) ++numberColumnSegments; n     } n     if (numberColumnSegments > 1) { n       printf( -1 n ); n       return 0; n     } n     if (cnt == 0) ++emptyColumns; n   } n   if (emptyLines == 0 && emptyColumns > 0 || n       emptyLines > 0 && emptyColumns == 0) { n     printf( -1 n ); n     return 0; n   } n   int connectedComponents = 0; n   for (int i = 1; i <= N; ++i) n     for (int j = 1; j <= M; ++j) n       if (used[i][j] == 0 && a[i][j] == 1) { n         ++connectedComponents; n         DFS(i, j, N, M); n       } n   printf( %d n , connectedComponents); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n T scani(T &n) { n   n = 0; n   bool negative = false; n   char c = getchar(); n   while (c <  0  || c >  9 ) { n     if (c ==  - ) negative = true; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     n = n * 10 + c - 48; n     c = getchar(); n   } n   if (negative) n = ~(n - 1); n   return n; n } n template <typename T> n void write(T n, int type = true) { n   if (n < 0) { n     putchar( - ); n     n = -n; n   } n   if (!n) { n     putchar( 0 ); n     if (type == 32) n       putchar(   ); n     else if (type) n       putchar(  n ); n     return; n   } n   char buff[22]; n   int len = 0; n   while (n) buff[len++] = n % 10 + 48, n /= 10; n   for (int i = len - 1; i >= 0; i--) putchar(buff[i]); n   if (type == 32) n     putchar(   ); n   else if (type) n     putchar(  n ); n } n int scans(char *a) { n   int i = 0; n   char c = 0; n   while (c < 33) c = getchar(); n   while (c > 33) { n     a[i++] = c; n     c = getchar(); n   } n   a[i] = 0; n   return i; n } n const int N = 100005; n int main() { n   int n, k; n   scani(n), scani(k); n   int ans; n   ans = (2 * n + k - 1) / k + (5 * n + k - 1) / k + (8 * n + k - 1) / k; n   write(ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline bool uin(T& a, T b) { n   return a > b ? (a = b, true) : false; n } n template <class T> n inline bool uax(T& a, T b) { n   return a < b ? (a = b, true) : false; n } n const int nax = 5123; n pair<int, int> edge[nax]; n vector<int> g[nax]; n int col[nax]; n int N, M; n bool cycle_found = false; n int dfs_num[nax]; n void dfs(int v) { n   dfs_num[v] = 1; n   for (int i : g[v]) { n     int ch = edge[i].first ^ edge[i].second ^ v; n     if (dfs_num[ch] == 2) { n       continue; n     } else if (dfs_num[ch] == 1) { n       col[i] = 1; n       cycle_found = true; n       continue; n     } n     dfs(ch); n   } n   dfs_num[v] = 2; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> N >> M; n   for (int u, v, i = 0; i < M; ++i) { n     cin >> u >> v; n     --u, --v; n     edge[i] = make_pair(u, v); n     g[u].push_back(i); n   } n   for (int i = 0; i < N; ++i) n     if (dfs_num[i] != 2) dfs(i); n   cout << (cycle_found ? 2 : 1) <<   n ; n   for (int i = 0; i < M; ++i) cout << col[i] + 1 <<    n [i == M - 1]; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, mid, rt, res, a[200010], f[200010], sz[200010]; n vector<int> e[200010]; n void dfs(int u, int fa) { n   f[u] = sz[u] = 1; n   int m1 = 0, m2 = 0; n   for (auto v : e[u]) { n     if (v == fa) continue; n     dfs(v, u); n     sz[u] += sz[v]; n     if (f[v] == sz[v]) n       f[u] += f[v]; n     else { n       if (f[v] > m1) swap(f[v], m1); n       if (f[v] > m2) swap(f[v], m2); n     } n   } n   f[u] = a[u] < mid ? 0 : f[u] + m1; n   res = max(res, f[u] + m2); n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   for (int i = 1; i < n; i++) { n     int u, v; n     scanf( %d%d , &u, &v); n     e[u].push_back(v); n     e[v].push_back(u); n   } n   rt = min_element(a + 1, a + 1 + n) - a; n   int l = 0, r = 1000010, ans; n   while (l <= r) { n     mid = (l + r) >> 1; n     res = 0; n     dfs(rt, 0); n     if (res >= m) n       l = mid + 1, ans = mid; n     else n       r = mid - 1; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool used[10000005]; n int ans[105]; n bool comp(pair<pair<int, int>, int> a, pair<pair<int, int>, int> b) { n   if (a.first.second == b.first.second) return a.first.first < b.first.first; n   return a.first.second < b.first.second; n } n void solve() { n   int N; n   cin >> N; n   vector<pair<pair<int, int>, int>> x(N); n   for (int i = 0; i < N; i++) { n     cin >> x[i].first.first >> x[i].first.second; n     x[i].second = i; n   } n   sort(x.begin(), x.end(), comp); n   for (int i = 0; i < N; i++) { n     for (int j = x[i].first.first; j < x[i].first.second + 1; j++) { n       if (!used[j]) { n         used[j] = true; n         ans[x[i].second] = j; n         break; n       } n     } n   } n   for (int i = 0; i < N; i++) { n     cout << ans[i] <<    ; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MAXN = 200100, M = 1000000009; n string s; n long long n, ans = 0, a[MAXN], b[MAXN]; n void obra(long long v) { n   long long tc = 0, k = a[v]; n   for (long long i = 0; i < n; i++) { n     b[i] = a[i] + k / n; n     if (i == v) b[i] -= a[i]; n   } n   k %= 14; n   for (long long i = 0; i < k; i++) { n     b[(v + 1 + i) % n]++; n   } n   for (long long i = 0; i < n; i++) { n     if (b[i] % 2 == 0) { n       tc += b[i]; n     } n   } n   if (ans < tc) ans = tc; n } n int main() { n   ios_base::sync_with_stdio(0); n   n = 14; n   for (long long i = 0; i < n; i++) { n     cin >> a[i]; n   } n   for (long long i = 0; i < n; i++) obra(i); n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   long long arr[n]; n   for (int i = 0; i < n; i++) scanf( %lld , &arr[i]); n   map<long long, int> mp; n   long long sum = 0; n   for (int i = n - 1; i >= 0; i--) { n     sum += arr[i]; n     mp[sum] = i; n   } n   long long ans = 0; n   long long cnt = 0; n   for (int i = 0; i < n; i++) { n     cnt += arr[i]; n     if (mp[cnt] > i && mp[cnt] != 0) { n       ans = max(cnt, ans); n     } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ld = long double; n template <typename T> n T &chmin(T &a, const T &b) { n   return a = min(a, b); n } n template <typename T> n T &chmax(T &a, const T &b) { n   return a = max(a, b); n } n template <typename T> n T inf; n template <> n constexpr int inf<int> = 1e9; n template <> n constexpr ll inf<ll> = 1e18; n template <> n constexpr ld inf<ld> = 1e30; n struct yes_no : numpunct<char> { n   string_type do_truename() const { return  Yes ; } n   string_type do_falsename() const { return  No ; } n }; n int N, K; n int A[501000]; n int main() { n   locale loc(locale(), new yes_no); n   cout << boolalpha; n   cout.imbue(loc); n   scanf( %d%d , &K, &N); n   for (int i = 0; i < (int)(N); i++) scanf( %d , &A[i]); n   sort(A, A + N); n   set<pair<ll, ll>> index_value, value_index; n   for (int i = 0; i < (int)(N - 1); i++) { n     const int val = A[i + 1] - A[i]; n     index_value.emplace(i, val); n     value_index.emplace(val, i); n   } n   index_value.emplace(-1, inf<ll>); n   index_value.emplace(N, inf<ll>); n   value_index.emplace(inf<ll>, -1); n   value_index.emplace(inf<ll>, N); n   ll res = 0; n   for (int loop = 0; loop < (int)(K); loop++) { n     int v, l, r; n     ll val, lval, rval; n     tie(val, v) = *begin(value_index); n     auto it = index_value.lower_bound(make_pair(v, val)); n     tie(l, lval) = *prev(it); n     tie(r, rval) = *next(it); n     index_value.erase(make_pair(v, val)); n     index_value.erase(make_pair(l, lval)); n     index_value.erase(make_pair(r, rval)); n     value_index.erase(make_pair(val, v)); n     value_index.erase(make_pair(lval, l)); n     value_index.erase(make_pair(rval, r)); n     index_value.emplace(v, lval + rval - val); n     value_index.emplace(lval + rval - val, v); n     res += val; n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> childs[100000 + 1]; n long long s[100000 + 1]; n int p[100000 + 1]; n long long a[100000 + 1]; n bool flag = false; n void fill(int v, long long prevsum) { n   if (s[v] != -1) { n     a[v] = s[v] - prevsum; n     if (a[v] < 0) flag = true; n     for (int i = 0; i < childs[v].size(); ++i) { n       fill(childs[v][i], s[v]); n     } n   } else { n     long long mi = 1000000001; n     for (int i = 0; i < childs[v].size(); ++i) { n       mi = min(mi, s[childs[v][i]]); n     } n     if (mi == 1000000001) mi = prevsum; n     a[v] = mi - prevsum; n     if (a[v] < 0) flag = true; n     for (int i = 0; i < childs[v].size(); ++i) { n       fill(childs[v][i], mi); n     } n   } n } n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; ++i) { n     childs[i].clear(); n   } n   for (int i = 2; i < n + 1; ++i) { n     cin >> p[i]; n     childs[p[i]].push_back(i); n   } n   for (int i = 1; i < n + 1; ++i) { n     cin >> s[i]; n   } n   fill(1, 0); n   if (flag) n     cout << -1 << endl; n   else { n     long long sum = 0; n     for (int i = 1; i < n + 1; ++i) { n       sum += a[i]; n     } n     cout << sum << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n string a, b; n int i, ax, ay, bx, by; n int main() { n   cin >> a >> b; n   for (i = 0; i < a.length(); i++) n     if (a[i] ==  1 ) n       ax++; n     else n       ay++; n   for (i = 0; i <= b.length(); i++) n     if (b[i] ==  1 ) n       bx++; n     else n       by++; n   if (ax % 2 == 1) ax++; n   if (ax < bx) n     cout <<  NO ; n   else n     cout <<  YES ; n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:200000 ) n const int INF = 1e9 + 7; n const double EPS = 1e-6; n const double pi = acos(-1); n const int MOD = 1e9 + 7; n const int N = 1e5 + 1000; n using namespace std; n double mid; n struct node { n   int pos, val; n   node() { pos = val = 0; } n   node(int a, int b) { n     pos = a; n     val = b; n   } n   bool operator<(node a) const { n     if (val != a.val) return val < a.val; n     if (abs(mid - pos * 1.0) != abs(mid - a.pos * 1.0)) n       return abs(mid - pos * 1.0) < abs(mid - a.pos * 1.0); n     return pos < a.pos; n   } n }; n int n, m, ans[N]; n set<node> st; n int main() { n   cin >> n >> m; n   mid = (m + 1) * 0.5; n   for (int i = 1; i <= m; i++) { n     st.insert(node(i, 0)); n   } n   for (int i = 1; i <= n; i++) { n     auto it = *st.begin(); n     st.erase(it); n     it.val++; n     ans[i] = it.pos; n     st.insert(it); n   } n   for (int i = 1; i <= n; i++) cout << ans[i] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k, op; n string tes; n int main() { n   cin >> n >> k >> ws >> tes; n   op = 0; n   for (int i = 1; i < n; i++) n     if (tes.substr(0, i) == tes.substr(n - i, i)) op = i; n   cout << tes; n   tes = tes.substr(op, tes.length()); n   for (int i = 1; i < k; i++) cout << tes; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char x[100100]; n long x2[100100]; n long mes[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; n long wr = 0; n long res, cur, repsmax = -1; n long reps = 0; n long tmp1, tmp2, tmp3; n string s1; n int compare(const void *a, const void *b) { return (*(long *)a - *(long *)b); } n long check(long z) { n   if ((isdigit(x[z])) && (isdigit(x[z + 1])) && (isdigit(x[z + 3])) && n       (isdigit(x[z + 4])) && (isdigit(x[z + 6])) && (isdigit(x[z + 7])) && n       (isdigit(x[z + 8])) && (isdigit(x[z + 9]))) { n     if ((x[z + 2] == x[z + 5]) && (x[z + 2] ==  - )) { n       tmp1 = n           ((long)(x[z]) - (long)( 0 )) * 10 + ((long)(x[z + 1]) - (long)( 0 )); n       tmp2 = ((long)(x[z + 3]) - (long)( 0 )) * 10 + n              ((long)(x[z + 4]) - (long)( 0 )); n       tmp3 = ((long)(x[z + 6]) - (long)( 0 )) * 1000 + n              ((long)(x[z + 7]) - (long)( 0 )) * 100 + n              (((long)(x[z + 8]) - (long)( 0 )) * 10) + n              ((long)(x[z + 9]) - (long)( 0 )); n       if ((tmp2 <= 12) && (tmp3 <= 2015) && (tmp3 >= 2013)) { n         if ((tmp1 <= mes[tmp2]) && (tmp2 != 0) && (tmp1 != 0)) { n           x2[wr++] = tmp1 + tmp2 * 100 + tmp3 * 10000; n         } n       } n     } n   } n   if ((!isdigit(x[z + 9])) && (x[z + 9] !=  - )) { n     return -2; n   } n } n int main(int argc, char *argv[]) { n   scanf( %s , &x); n   long j = 0; n   while (check(j++) > -2) { n   } n   qsort(x2, wr - 1, sizeof(long), compare); n   cur = x2[0]; n   for (long j = 0; j < wr; j++) { n     if (cur == x2[j]) { n       reps++; n     } else { n       if (reps > repsmax) { n         repsmax = reps; n         res = cur; n       } n       cur = x2[j]; n       reps = 1; n     } n   } n   if (reps > repsmax) { n     repsmax = reps; n     res = cur; n   } n   cur = x2[j]; n   reps = 1; n   ldiv_t divresult1; n   divresult1 = ldiv(res, 100); n   if (divresult1.rem < 10) { n     cout <<  0 ; n   } n   cout << divresult1.rem <<  - ; n   ldiv_t divresult2; n   divresult2 = ldiv(res, 10000); n   if (((divresult2.rem - divresult1.rem) / 100) < 10) { n     cout <<  0 ; n   } n   cout << (divresult2.rem - divresult1.rem) / 100 <<  - ; n   ldiv_t divresult3; n   divresult3 = ldiv(res, 100000000); n   cout << (divresult3.rem - divresult2.rem) / 10000; n   return EXIT_SUCCESS; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100005; n int n, m; n vector<pair<int, int> > e[N]; n vector<int> ans[2]; n bool fla[2], flag; n int E[N], d[N]; n vector<int> cnt[2]; n void DFS(int first, int o, int res) { n   if (flag) return; n   d[first] = o; n   cnt[o].push_back(first); n   for (auto& i : e[first]) { n     if (flag) return; n     int second = (i.second + o) % 2; n     if (d[i.first] == -1) n       DFS(i.first, second != res, res); n     else if (d[i.first] != (second != res)) n       flag = true; n   } n } n void SOLVE(int res) { n   memset(d, -1, sizeof(d)); n   flag = false; n   for (int i(1); i <= n; i++) n     if (d[i] == -1) { n       cnt[0].clear(); n       cnt[1].clear(); n       DFS(i, 0, res); n       if (flag) { n         fla[res] = true; n         return; n       } n       for (bool ___ = 1; ___; ___ = 0) { n         int mx = (cnt[1].size() < cnt[0].size()); n         for (auto& i : cnt[mx]) ans[res].push_back(i); n       } n     } n } n int main() { n   n = ({ n     long long first = 0; n     int n = 0, c = getchar(); n     for (; !isdigit(c); c = getchar()) n = c ==  - ; n     for (; isdigit(c); c = getchar()) first = first * 10 + c -  0 ; n     n ? -first : first; n   }), n   m = ({ n     long long first = 0; n     int n = 0, c = getchar(); n     for (; !isdigit(c); c = getchar()) n = c ==  - ; n     for (; isdigit(c); c = getchar()) first = first * 10 + c -  0 ; n     n ? -first : first; n   }); n   for (int i(1); i <= m; i++) { n     int first = ({ n       long long first = 0; n       int n = 0, c = getchar(); n       for (; !isdigit(c); c = getchar()) n = c ==  - ; n       for (; isdigit(c); c = getchar()) first = first * 10 + c -  0 ; n       n ? -first : first; n     }), n         second = ({ n           long long first = 0; n           int n = 0, c = getchar(); n           for (; !isdigit(c); c = getchar()) n = c ==  - ; n           for (; isdigit(c); c = getchar()) first = first * 10 + c -  0 ; n           n ? -first : first; n         }); n     int c = ({ n               char c = getchar(); n               while (c ==     || c ==   n ) c = getchar(); n               c; n             }) ==  R ; n     e[first].push_back({second, c}); n     e[second].push_back({first, c}); n   } n   SOLVE(0); n   SOLVE(1); n   for (bool ___ = 1; ___; ___ = 0) { n     int res; n     if (fla[0] && fla[1]) n       return cout << -1 <<   n , 0; n     else if (fla[0]) n       res = 1; n     else if (fla[1]) n       res = 0; n     else if (ans[0].size() < ans[1].size()) n       res = 0; n     else n       res = 1; n     cout << ans[res].size() <<   n ; n     for (auto& i : ans[res]) cout << i <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n class BLongtailHedgehog { n  public: n   int N, M; n   vector<int> dist, deg_in, deg; n   vector<vector<int>> G; n   void input(std::istream& in, std::ostream& out) { n     ios_base::sync_with_stdio(false); n     in >> N >> M; n     dist = vector<int>(N, -1); n     deg_in = vector<int>(N, 0); n     deg = vector<int>(N, 0); n     G = vector<vector<int>>(N, vector<int>()); n     for (int(i) = (0); (i) < (M); ++(i)) { n       int u, v; n       in >> u >> v; n       u--, v--; n       if (u > v) swap(u, v); n       G[u].push_back(v); n       deg_in[v]++, deg[u]++, deg[v]++; n     } n   } n   void bfs(int s) { n     queue<int> q; n     dist[s] = 1, q.push(s); n     while (!q.empty()) { n       int u = q.front(); n       q.pop(); n       for (auto v : G[u]) { n         deg_in[v]--; n         dist[v] = max(dist[v], dist[u] + 1); n         if (deg_in[v] == 0) { n           q.push(v); n         } n       } n     } n   } n   void solve(std::istream& in, std::ostream& out) { n     input(in, out); n     for (int(i) = (0); (i) < (N); ++(i)) { n       if (dist[i] == -1) bfs(i); n     } n     long long ans = 0; n     for (int(i) = (0); (i) < (N); ++(i)) ans = max(ans, 1LL * dist[i] * deg[i]); n     out << ans <<   n ; n   } n }; n int main() { n   BLongtailHedgehog solver; n   std::istream& in(std::cin); n   std::ostream& out(std::cout); n   solver.solve(in, out); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void hi() { n   static long long i = 0; n   cout <<  Check Point :   << ++i <<   n ; n   return; n } n signed main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   long long t; n   t = 1; n   while (t--) { n     string str; n     cin >> str; n     long long n = str.length(); n     stack<char> stk; n     for (long long i = 0; i < n; i++) { n       if (!stk.empty() && ((stk.top() ==  +  && str[i] ==  + ) || n                            ((stk.top() ==  -  && str[i] ==  - )))) { n         stk.pop(); n       } else { n         stk.push(str[i]); n       } n     } n     if (stk.empty()) { n       cout <<  Yes  n            <<   n ; n     } else { n       cout <<  No  n            <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 400010; n int gcds[N], a[N], b[N]; n int c[N], mx[N]; n int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i - 1]); n     gcds[i] = gcd(i, n); n   } n   long long ans = 0; n   for (int i = 1; i < n; i++) { n     if (n % i) continue; n     for (int j = 1; j <= n; j++) c[j] = c[j - 1] + (i == gcds[j]); n     for (int j = 0; j < i; j++) mx[j] = a[j]; n     for (int j = i; j < n; j++) mx[j % i] = max(mx[j % i], a[j]); n     for (int j = 0; j < n; j++) b[j] = b[j + n] = mx[j % i] == a[j]; n     for (int j = 2 * n - 2; j >= 0; j--) n       if (b[j]) b[j] += b[j + 1]; n     for (int j = 0; j < n; j++) ans += c[min(n - 1, b[j])]; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[23][23]; n char s[23][23]; n int dp[1 << 23]; n int n, m; n int cost[23][23]; n int st[23][23]; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < n; i++) scanf( %s , s[i]); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) scanf( %d , &a[i][j]); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) { n       int mx = -0x3f3f3f3f; n       int ans = 0; n       for (int k = 0; k < n; k++) { n         if (s[i][j] == s[k][j]) { n           ans += a[k][j]; n           mx = max(mx, a[k][j]); n           st[i][j] |= 1 << k; n         } n       } n       ans -= mx; n       cost[i][j] = ans; n     } n   for (int i = 1; i < (1 << n); i++) dp[i] = 0x3f3f3f3f; n   dp[0] = 0; n   for (int i = 0; i < (1 << n); i++) n     for (int j = 0; j < n; j++) n       if ((i & (1 << j)) == 0) { n         for (int k = 0; k < m; k++) { n           dp[i | (1 << j)] = min(dp[i | (1 << j)], dp[i] + a[j][k]); n           dp[i | st[j][k]] = min(dp[i | st[j][k]], dp[i] + cost[j][k]); n         } n       } n   printf( %d n , dp[(1 << n) - 1]); n   return 0; n } n


#include <bits/stdc++.h> n long long int a[300000]; n int main() { n   long long int n, i, hei, bai; n   while (scanf( %lld , &n) != EOF) { n     hei = bai = 0; n     for (i = 0; i < n; i++) { n       scanf( %lld , &a[i]); n       if (i % 2) { n         hei += (a[i] + 1) / 2; n         bai += a[i] / 2; n       } else { n         bai += (a[i] + 1) / 2; n         hei += a[i] / 2; n       } n     } n     printf( %lld n , bai < hei ? bai : hei); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = INT_MAX; n const long long INFL = LLONG_MAX; n const int output_precision = 15; n const long double pi = acos(-1); n const bool debug = true; n int N; n int A[5]; n int check() { n   vector<int> B(4); n   for (int(i) = 1, j1234 = 4; (i) <= j1234; (i)++) B[i - 1] = A[i]; n   sort(B.begin(), B.end()); n   int x1 = B[0] + B[1] + B[2] + B[3]; n   int x2 = 2 * (B[1] + B[2]); n   int x3 = 4 * (B[3] - B[0]); n   return x1 == x2 && x2 == x3; n } n int main() { n   ios_base::sync_with_stdio(0); n   cout.precision(output_precision); n   cout << fixed; n   cout.tie(0); n   cin >> N; n   for (int(i) = 1, j1234 = N; (i) <= j1234; (i)++) cin >> A[i]; n   if (N == 0) { n     cout <<  YES  <<   n ; n     cout << 1 <<   n ; n     cout << 1 <<   n ; n     cout << 3 <<   n ; n     cout << 3 <<   n ; n     return 0; n   } n   if (N == 1) { n     int a = A[1]; n     cout <<  YES  <<   n ; n     cout << a <<   n ; n     cout << 3 * a <<   n ; n     cout << 3 * a <<   n ; n     return 0; n   } n   if (N == 2) { n     for (int(x) = 1, j1234 = 1500; (x) <= j1234; (x)++) n       for (int(y) = 1, j1234 = 1500; (y) <= j1234; (y)++) { n         A[3] = x; n         A[4] = y; n         if (check()) { n           cout <<  YES  <<   n ; n           cout << x <<   n ; n           cout << y <<   n ; n           return 0; n         } n       } n     cout <<  NO  <<   n ; n     return 0; n   } n   if (N == 3) { n     for (int(i) = 1, j1234 = 1500; (i) <= j1234; (i)++) { n       A[4] = i; n       if (check()) { n         cout <<  YES  <<   n ; n         cout << i <<   n ; n         return 0; n       } n     } n     cout <<  NO  <<   n ; n     return 0; n   } n   if (N == 4) { n     if (check()) { n       cout <<  YES  <<   n ; n     } else { n       cout <<  NO  <<   n ; n     } n     return 0; n   } n } n


#include <bits/stdc++.h> n using namespace std; n void islam() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n } n void get(long long &x) { cin >> x; } n class ghost { n  public: n   ghost() { n     get(n); n     for (int i = 0; i < n; i++) { n       get(arr[i]); n       s += arr[i]; n     } n     s /= n; n   } n   void solve() { n     long long c = 0; n     for (int i = 0; i < n; i++) { n       c += s - arr[i]; n       a += abs(c); n     } n   } n   void print() { cout << a; } n  n  private: n   long long n, s; n   long long arr[50000 + 10], a = 0; n }; n int main() { n   islam(); n   ghost x; n   x.solve(); n   x.print(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool isAscending(const vector<int64_t>& vec) { n   for (int i = 1; i < vec.size(); ++i) { n     if (vec[i - 1] >= vec[i]) { n       return false; n     } n   } n   return true; n } n bool Requal(const vector<int64_t>& av, const vector<int64_t>& bv) { n   for (int i = 0; i < av.size(); ++i) { n     if (av[i] != bv[bv.size() - i - 1]) { n       return false; n     } n   } n   return true; n } n bool Equal(const vector<int64_t>& av, const vector<int64_t>& bv) { n   for (int i = 0; i < av.size(); ++i) { n     if (av[i] != bv[i]) { n       return false; n     } n   } n   return true; n } n int main() { n   std::ios_base::sync_with_stdio(false); n   std::cin.tie(nullptr); n   int len; n   cin >> len; n   vector<int64_t> av(len), bv(len); n   for (auto&& e : av) { n     cin >> e; n   } n   for (auto&& e : bv) { n     cin >> e; n   } n   if (len == 1) { n     if (av[0] == bv[0]) { n       cout <<  SMALL  << endl; n       cout << 0 << endl; n       cout << endl; n     } else { n       cout <<  IMPOSSIBLE  << endl; n     } n     return 0; n   } n   if (len == 2) { n     int64_t total = 0; n     vector<pair<char, int64_t>> buf; n     string res; n     if (av[0] > av[1]) { n       swap(av[0], av[1]); n       res +=  R ; n     } n     while (1) { n       if (bv[0] > bv[1]) { n         swap(bv[0], bv[1]); n         buf.push_back({ R , 1}); n       } n       if (bv[0] == av[0]) { n         if (bv[1] >= av[1] && ((bv[1] - av[1]) % bv[0]) == 0) { n           buf.push_back({ P , (bv[1] - av[1]) / bv[0]}); n           total += (bv[1] - av[1]) / bv[0]; n         } else { n           total = -1; n         } n         break; n       } n       if (bv[0] == bv[1]) { n         total = -1; n         break; n       } n       buf.push_back({ P , bv[1] / bv[0]}); n       bv[1] = bv[1] % bv[0]; n       total += buf.back().second; n       if (bv[1] == 0) { n         bv[1] = bv[0]; n         total--; n         buf.back().second--; n       } n     } n     if (total < 0) { n       cout <<  IMPOSSIBLE  << endl; n       return 0; n     } n     if (total > 200000) { n       cout <<  BIG  << endl; n       cout << total << endl; n     } else { n       cout <<  SMALL  << endl; n       for (auto it = buf.rbegin(); it != buf.rend(); ++it) { n         for (int i = 0; i < it->second; ++i) { n           res += it->first; n         } n       } n       cout << res.size() << endl; n       cout << res << endl; n     } n     return 0; n   } n   int64_t total = 0; n   string res; n   while (1) { n     if (Equal(av, bv)) { n       break; n     } n     if (Requal(av, bv)) { n       res +=  R ; n       break; n     } n     if (bv[0] > bv[1]) { n       reverse(begin(bv), end(bv)); n       res +=  R ; n     } n     if (isAscending(bv) and bv[0] > 0) { n       for (int i = len; i-- > 1;) { n         bv[i] -= bv[i - 1]; n       } n       res +=  P ; n       ++total; n     } else { n       res =   ; n       break; n     } n   } n   if (res ==   ) { n     cout <<  IMPOSSIBLE  << endl; n   } else if (total > 200000) { n     cout <<  BIG  << endl; n     cout << total << endl; n   } else { n     cout <<  SMALL  << endl; n     reverse(begin(res), end(res)); n     cout << res.size() << endl; n     cout << res << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n queue<int> fila; n struct G { n   double d; n   int m, p, r; n } g[250002]; n bool mk[250002]; n int a2[9000000]; n bool cmp2(int a, int b) { return g[a].d < g[b].d; } n int pos = 0; n int ind = 0; n struct T { n   int m; n   int* e; n   void pdt(int a, int* b) { m = a, e = b; } n } mas[2 * 250002]; n bool cmp(T a, T b) { return a.m < b.m; } n int ls[4 * 250002]; n int s[4 * 250002][2]; n int r[4 * 250002]; n int build(int a, int b, int f) { n   int p = ind++; n   ls[p] = pos; n   r[p] = b; n   int i = ls[f]; n   for (int i = ls[f]; a2[i] >= 0; i++) { n     if (g[a2[i]].m >= a && g[a2[i]].m <= b) a2[pos] = a2[i], pos++; n   } n   a2[pos] = -1; n   pos++; n   if (a != b) n     s[p][0] = build(a, (a + b) / 2, p), n     s[p][1] = build(((a + b) / 2) + 1, b, p); n   else n     s[p][0] = s[p][1] = -1; n   return p; n } n void get(int p, int power, int ray) { n   if (r[p] <= power) { n     for (int i = ls[p]; a2[i] >= 0; i++) { n       if (g[a2[i]].d <= ray + 1e-2) { n         if (!mk[a2[i]]) fila.push(a2[i]), mk[a2[i]] = true; n         ls[p] = i + 1; n       } else n         break; n     } n   } else { n     get(s[p][0], power, ray); n     if (power > r[s[p][0]]) get(s[p][1], power, ray); n   } n } n int main() { n   double x0, y0; n   int pp, rr, n; n   scanf( %lf %lf %d %d %d , &x0, &y0, &pp, &rr, &n); n   int tot = 0; n   mas[tot++].pdt(pp, &pp); n   double x, y; n   for (int i = 0; i < n; i++) { n     scanf( %lf %lf %d %d %d , &x, &y, &g[i].m, &g[i].p, &g[i].r); n     x -= x0; n     y -= y0; n     g[i].d = sqrt(x * x + y * y); n     mk[i] = false; n     a2[i] = i; n     mas[tot++].pdt(g[i].m, &g[i].m), mas[tot++].pdt(g[i].p, &g[i].p); n   } n   a2[n] = -1; n   sort(mas, mas + tot, cmp); n   int v = 1; n   int tmp = mas[0].m; n   *mas[0].e = v; n   for (int i = 1; i < tot; i++) { n     if (mas[i].m != tmp) v++; n     tmp = mas[i].m; n     *mas[i].e = v; n   } n   sort(a2, a2 + n, cmp2); n   r[0] = v; n   ls[0] = 0; n   ind = 1; n   pos = n + 1; n   if (n >= 2) s[0][0] = build(1, v / 2, 0), s[0][1] = build((v / 2) + 1, v, 0); n   get(0, pp, rr); n   int resp = 0; n   while (fila.size()) { n     resp++; n     int u = fila.front(); n     fila.pop(); n     get(0, g[u].p, g[u].r); n   } n   printf( %d n , resp); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int run(int n) { n   int i, a[n]; n   for (i = 0; i < n; i++) cin >> a[i]; n   sort(a, a + i); n   int ans = 0; n   int tempCount = 0; n   for (i = 0; i < n; i++) { n     tempCount++; n     if (a[i] == tempCount) { n       ans++; n       tempCount = 0; n     } n   } n   return ans; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t; n   cin >> t; n   int n; n   while (t--) { n     cin >> n; n     cout << run(n) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, s = 1; n   char a; n   string b[10], c; n   cin >> n; n   b[0] =   ; n   b[1] =   ; n   b[2] =  2 ; n   b[3] =  3 ; n   b[4] =  322 ; n   b[5] =  5 ; n   b[6] =  53 ; n   b[7] =  7 ; n   b[8] =  7222 ; n   b[9] =  7332 ; n   int k = 0; n   int d = 0; n   for (int i = 1; i <= n; i++) { n     cin >> a; n     s = a - 48; n     if (s != 1 && s != 0) c = c + b[s]; n   } n   sort(c.begin(), c.end()); n   k = c.size(); n   for (int i = k - 1; i >= 0; i--) cout << c[i]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t, n, k; n   cin >> t; n   while (t--) { n     cin >> n >> k; n     for (int i = 0; i < n; ++i) cout << (char)(i % k +  a ); n     cout << endl; n   } n   return 0; n } n


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


#include <bits/stdc++.h> n int b[1000005], a[1000005]; n int main() { n   int n, i; n   scanf( %d , &n); n   for (i = 0; i < n; i++) scanf( %d , &a[i]); n   b[n - 1] = a[n - 1]; n   for (i = n - 2; i >= 0; i--) b[i] = a[i] + a[i + 1]; n   for (i = 0; i < n; i++) printf( %d  , b[i]); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n namespace io { n template <typename T> n inline void read(T &x) { n   bool flag = false; n   char c = getchar(); n   while ((c <  0  || c >  9 ) && c !=  - ) c = getchar(); n   c ==  -  ? flag = true, x = 0 : x = c -  0 ; n   c = getchar(); n   while (c >=  0  && c <=  9 ) x = (x << 3) + (x << 1) + c -  0 , c = getchar(); n   if (flag) x = -x; n } n template <typename T> n inline void write(T x) { n   char st[10010]; n   int cnt = 0; n   if (x < 0) x = -x, putchar( - ); n   if (x == 0) putchar( 0 ); n   while (x) { n     st[++cnt] = x % 10 + 48; n     x /= 10; n   } n   while (cnt) { n     putchar(st[cnt--]); n   } n } n }  // namespace io n using namespace io; n int main() { n   int a, ans = 0; n   read(a); n   while (a) { n     ans += (a % 8 == 1); n     a /= 8; n   } n   write(ans); n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n const int N = 300010; n const int MOD = 1e9 + 7; n struct xxx { n   int id; n   char c; n } s[N]; n std::string a, b, c; n int gap, n; n int sa[N], lcp[N], rank[N], tmp[N], bucket[N], cnt[N][3], ans[N], dp[19][N], n     fw[N]; n void update(int i, int v) { n   while (i < N) { n     fw[i] = (fw[i] + v) % MOD; n     i += i & -i; n   } n } n int query(int i) { n   int res = 0; n   while (i) { n     res = (res + fw[i]) % MOD; n     i -= i & -i; n   } n   if (res < 0) res += MOD; n   return res; n } n int rmq(int f, int t) { n   int k = 31 - __builtin_clz(t - f + 1); n   int x = dp[k][f]; n   int y = dp[k][t - (1 << k) + 1]; n   if (lcp[x] < lcp[y]) return x; n   return y; n } n void solve(int f, int t, int h) { n   if (t - f + 1 < 3) return; n   int m = rmq(f, t - 1); n   int c0 = cnt[t][0] - cnt[f - 1][0]; n   int c1 = cnt[t][1] - cnt[f - 1][1]; n   int c2 = cnt[t][2] - cnt[f - 1][2]; n   int v = lcp[m]; n   int num = 1ll * c0 * c1 % MOD * c2 % MOD; n   update(v + 1, -num); n   update(h + 1, num); n   solve(f, m, v); n   solve(m + 1, t, v); n } n bool cmp2(int i, int j) { n   i += gap; n   j += gap; n   if (i < n && j < n) return rank[i] < rank[j]; n   return i > j; n } n bool cmp(int i, int j) { n   if (rank[i] != rank[j]) return rank[i] < rank[j]; n   i += gap; n   j += gap; n   if (i < n && j < n) return rank[i] < rank[j]; n   return i > j; n } n int main() { n   std::cin >> a >> b >> c; n   int an = int(a.size()), bn = int(b.size()), cn = int(c.size()); n   for (int i = 0; i < an; ++i) s[i].id = 0, s[i].c = a[i]; n   for (int i = 0; i < bn; ++i) s[i + an + 1].id = 1, s[i + an + 1].c = b[i]; n   for (int i = 0; i < cn; ++i) n     s[i + an + bn + 2].id = 2, s[i + an + bn + 2].c = c[i]; n   s[an].c = 1; n   s[an + bn + 1].c = 2; n   n = an + bn + cn + 2; n   int rn = 255; n   for (int i = 0; i < n; ++i) { n     sa[i] = i; n     rank[i] = s[i].c; n   } n   for (gap = 1;; gap <<= 1) { n     int p = 0; n     for (int i = n - 1; i + gap >= n; --i) tmp[p++] = i; n     std::fill(bucket, bucket + rn, 0); n     for (int i = 0; i + gap < n; ++i) ++bucket[rank[i + gap]]; n     for (int i = 1; i < rn; ++i) bucket[i] += bucket[i - 1]; n     for (int i = 0; i + gap < n; ++i) tmp[--bucket[rank[i + gap]] + p] = i; n     std::fill(bucket, bucket + rn, 0); n     for (int i = 0; i < n; ++i) ++bucket[rank[i]]; n     for (int i = 1; i < rn; ++i) bucket[i] += bucket[i - 1]; n     for (int i = n - 1; i >= 0; --i) sa[--bucket[rank[tmp[i]]]] = tmp[i]; n     tmp[0] = 0; n     for (int i = 1; i < n; ++i) tmp[i] = tmp[i - 1] + cmp(sa[i - 1], sa[i]); n     for (int i = 0; i < n; ++i) rank[sa[i]] = tmp[i]; n     if (tmp[n - 1] == n - 1) break; n     rn = tmp[n - 1] + 1; n   } n   for (int i = 0, k = 0; i < n; ++i) { n     if (rank[i] == n - 1) continue; n     for (int j = sa[rank[i] + 1]; s[i + k].c == s[j + k].c;) ++k; n     lcp[rank[i]] = k; n     if (k) --k; n   } n   for (int i = 0; i < n - 1; ++i) dp[0][i] = i; n   for (int j = 1; 1 << j < n; ++j) n     for (int i = 0; i + (1 << j) - 1 < n - 1; ++i) { n       int x = dp[j - 1][i]; n       int y = dp[j - 1][i + (1 << (j - 1))]; n       dp[j][i] = lcp[x] < lcp[y] ? x : y; n     } n   assert(s[sa[0]].c == 1); n   assert(s[sa[1]].c == 2); n   for (int i = 2; i < n; ++i) { n     cnt[i][0] = cnt[i - 1][0]; n     cnt[i][1] = cnt[i - 1][1]; n     cnt[i][2] = cnt[i - 1][2]; n     ++cnt[i][s[sa[i]].id]; n   } n   int sz = std::min(an, std::min(bn, cn)); n   solve(2, n - 1, 0); n   for (int i = 1; i <= sz; ++i) std::cout << query(i) <<    ; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:1073741824 ) n using namespace std; n const int N = 3e5 + 100; n int col[N], d[N][2]; n vector<int> v[N]; n int main() { n #pragma warning(disable : 4996) n   int n, m; n   scanf( %d%d , &n, &m); n   for (int i = 0; i < m; i++) { n     int a, b; n     scanf( %d%d , &a, &b); n     a--, b--; n     v[a].push_back(b); n     v[b].push_back(a); n   } n   queue<int> q; n   for (int i = 0; i < n; i++) { n     d[i][0] = v[i].size(); n     if (d[i][0] > 1) q.push(i); n   } n   while (!q.empty()) { n     int node = q.front(); n     q.pop(); n     if (d[node][col[node]] > 1) { n       col[node] ^= 1; n       for (int to : v[node]) { n         d[to][col[node]]++; n         d[to][col[node] ^ 1]--; n         if (d[to][col[to]] > 1) q.push(to); n       } n     } n   } n   for (int i = 0; i < n; i++) printf( %d , col[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1000005; n const long long mod = 1e9 + 7; n const int N = 1e6; n vector<int> rec[maxn]; n long long fac[maxn], ans; n int n, m; n int ord[maxn], bit; n bool comp(int a, int b) { return rec[a][bit] < rec[b][bit]; } n void mysort(int l, int r, int j) { n   if (l == r) return; n   bit = j; n   sort(ord + l, ord + r + 1, comp); n   int p = l; n   while (p <= r && rec[ord[p]][j] == -1) ++p; n   int cnt = 1; n   for (int i = l + 1; i < p; i++) n     if (rec[ord[i]][j - 1] == rec[ord[i - 1]][j - 1]) n       ++cnt; n     else { n       (ans *= fac[cnt]) %= mod; n       cnt = 1; n     } n   (ans *= fac[cnt]) %= mod; n   int t = p; n   while (t <= r) { n     p = t; n     while (p <= r && rec[ord[p]][j] == rec[ord[t]][j]) ++p; n     mysort(t, p - 1, j + 1); n     t = p; n   } n } n int main() { n   fac[0] = 1ll; n   for (int i = 1; i <= N; i++) fac[i] = fac[i - 1] * i % mod; n   scanf( %d %d , &n, &m); n   for (int i = 1; i <= m; i++) rec[i].push_back(0); n   for (int g, i = 1; i <= n; i++) { n     scanf( %d , &g); n     for (int v, j = 1; j <= g; j++) scanf( %d , &v), rec[v].push_back(i); n   } n   for (int i = 1; i <= m; i++) { n     sort(rec[i].begin(), rec[i].end()); n     rec[i].push_back(-1); n     ord[i] = i; n   } n   ans = 1ll; n   mysort(1, m, 1); n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int Power(int base, int exp) { n   int ans = 1; n   while (exp > 0) { n     if (exp & 1) ans = (1LL * ans * base) % 1000000007; n     exp = exp >> 1; n     base = (1LL * base * base) % 1000000007; n   } n   return ans; n } n vector<map<long double, int> > p_CntS(2005); n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int n; n   cin >> n; n   long double x[n], y[n]; n   for (int i = 0; i < n; i += 1) cin >> x[i] >> y[i]; n   long double slope; n   for (int p1 = 0; p1 < n; p1 += 1) n     for (int p2 = 0; p2 < n; p2 += 1) { n       if (p1 == p2) continue; n       slope = y[p2] - y[p1]; n       if (x[p2] - x[p1] == 0) n         p_CntS[p1][600.0]++; n       else n         p_CntS[p1][300.0 + slope / (x[p2] - x[p1])]++; n     } n   long long int ans = (1LL * n * (n - 1) * (n - 2)) / 6, ans_ = 0; n   int cnt; n   for (int i = 0; i < n; i += 1) { n     map<long double, int>::iterator it = p_CntS[i].begin(); n     while (it != p_CntS[i].end()) { n       cnt = (*it).second; n       ans_ += 1LL * cnt * (cnt - 1) / 2; n       it++; n     } n   } n   cout << ans - ans_ / 3 <<   n ; n } n


#include <bits/stdc++.h> n int ans = 0; n double H = 100, len = 100000; n int F[110000], T[110000], FF[110000], TT[110000]; n bool vis[110000]; n int judge(bool fuck, double st, double t) { n   int ret = 0; n   int top, flo; n   double point = st; n   memset(vis, 0, sizeof(vis)); n   while (point < len) { n     top = (int)ceil(point); n     flo = (int)floor(point); n     if (fuck) { n       if (T[top] < 0 || T[flo] < 0 || T[top] != T[flo]) { n         return -1; n       } n       if (vis[TT[top]] == 1) return -1; n       ret += T[top]; n       vis[TT[top]] = 1; n       fuck = !fuck; n     } else { n       if (F[top] < 0 || F[flo] < 0 || F[top] != F[flo]) { n         return -1; n       } n       if (vis[FF[top]] == 1) return -1; n       ret += F[top]; n       vis[FF[top]] = 1; n       fuck = !fuck; n     } n     point += t; n   } n   if (ans < ret) ans = ret; n   return ret; n } n int work() { n   int n, i, j, val, x, y; n   char c; n   double A, B, t; n   double hl, hr; n   scanf( %lf%lf%d , &hl, &hr, &n); n   memset(T, -1, sizeof(T)); n   memset(F, -1, sizeof(F)); n   for (i = 1; i <= n; i++) { n     scanf( %d %c%d%d , &val, &c, &x, &y); n     if (c ==  T ) { n       for (j = x; j <= y; j++) T[j] = val, TT[j] = i; n     } else { n       for (j = x; j <= y; j++) F[j] = val, FF[j] = i; n     } n   } n   A = hl / H; n   B = hr / H; n   for (i = 0; i <= 100; i++) { n     if ((i & 1) == 0) { n       t = len / (A + B + i); n       judge(0, A * t, t); n     } n     if ((i & 1) == 1) { n       t = len / ((1 - A) + B + i); n       judge(1, (1 - A) * t, t); n     } n     if ((i & 1) == 1) { n       t = len / (A + (1 - B) + i); n       judge(0, A * t, t); n     } n     if ((i & 1) == 0) { n       t = len / ((1 - A) + (1 - B) + i); n       judge(1, (1 - A) * t, t); n     } n   } n   printf( %d n , ans); n   return 1; n } n int main() { n   work(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct point { n   int x, y; n   point() {} n   point(int x, int y) : x(x), y(y) {} n } p[1010]; n point operator-(const point &a, const point &b) { n   return point(a.x - b.x, a.y - b.y); n } n long long operator*(const point &a, const point &b) { n   return (long long)a.x * b.y - (long long)a.y * b.x; n } n int main() { n   int n, i, x, y, low, high; n   cin >> n; n   for (i = 0; i < n; i++) { n     cin >> x >> y; n     p[i] = point(x, y); n   } n   low = p[0].x; n   high = p[1].x; n   if (low > high) swap(low, high); n   p[n] = p[0]; n   for (i = 0; i < n; i++) { n     while (low <= high && (p[i + 1] - p[i]) * (point(low, p[0].y) - p[i]) > 0) n       low++; n     while (low <= high && (p[i + 1] - p[i]) * (point(high, p[0].y) - p[i]) > 0) n       high--; n   } n   cout << high - low + 1 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 500000; n const long long minx = -0x5fffffffffffffff; n struct nn { n   long long x, y; n   nn() { y = minx; } n }; n int n, u, v; n nn num[N]; n vector<int> node[N]; n long long dfs(int f, int x) { n   for (int i = 0; i < node[x].size(); i++) { n     if (node[x][i] == f) continue; n     long long r = dfs(x, node[x][i]); n     num[x].y += r; n   } n   return num[x].y; n } n long long dfs1(int f, int x, long long &ans) { n   long long m1 = minx; n   long long m2 = minx; n   for (int i = 0; i < node[x].size(); i++) { n     if (node[x][i] == f) continue; n     long long r = dfs1(x, node[x][i], ans); n     if (m1 < r) { n       m2 = m1; n       m1 = r; n     } else if (m2 < r) { n       m2 = r; n     } n   } n   if (m2 != minx) { n     if (ans < m1 + m2) ans = m1 + m2; n   } n   return m1 > num[x].y ? m1 : num[x].y; n } n int main() { n   long long ans; n   while (scanf( %d , &n) != EOF) { n     ans = minx; n     for (int i = 0; i < N; i++) node[i].clear(); n     for (int i = 1; i <= n; i++) { n       scanf( %lld , &num[i].x); n       num[i].y = num[i].x; n     } n     for (int i = 0; i < n - 1; i++) { n       scanf( %d%d , &u, &v); n       node[u].push_back(v); n       node[v].push_back(u); n     } n     dfs(0, 1); n     dfs1(0, 1, ans); n     if (ans != minx) n       printf( %lld n , ans); n     else n       printf( Impossible n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   int n, k; n   cin >> n >> k; n   if (k > (n * n + 1) / 2) { n     cout <<  NO ; n     return 0; n   } n   cout <<  YES n ; n   int tmp = 0; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       if (j % 2 == i % 2 && tmp != k) { n         cout <<  L ; n         tmp++; n       } else n         cout <<  S ; n     } n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n using namespace chrono; n const int infinity = (int)1e9 + 42; n const int64_t llInfinity = (int64_t)1e18 + 256; n const int module = (int)1e9 + 7; n const long double eps = 1e-8; n mt19937_64 randGen(system_clock().now().time_since_epoch().count()); n inline void raiseError(string errorCode) { n   cerr <<  Error :   << errorCode << endl; n   exit(42); n } n inline int readAns(int req) { n   cout << req << endl; n   int q; n   cin >> q; n   if (q == 0) { n     exit(0); n   } n   return q; n } n signed main() { n   ios_base::sync_with_stdio(false); n   int m, n; n   cin >> m >> n; n   vector<int> v(n); n   for (int i = 0; i < n; i++) { n     v[i] = readAns(m) * -1; n   } n   int l = 1, r = m - 1; n   int p = 0; n   while (l != r) { n     int m = (l + r) / 2; n     int ans = readAns(m) * v[p]; n     p++; n     p %= n; n     if (ans < 0) { n       r = m - 1; n     } else { n       l = m + 1; n     } n   } n   readAns(l); n   assert(false); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int n, m; n   string s, t; n   cin >> n >> m >> s >> t; n   bool can = false; n   if (s == t) n     can = true; n   else { n     if (n <= m + 1) { n       for (int i = 0; i < int(n); i++) { n         if (s[i] ==  * ) { n           if (s.substr(0, i) == t.substr(0, i) && n               s.substr(i + 1, string::npos) == n                   t.substr(m - (n - (i + 1)), string::npos)) n             can = true; n         } n       } n     } n   } n   cout << (can ?  YES  :  NO ) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[200001], b[200001], c[200001]; n int main() { n   long long n, ans = 0; n   cin >> n; n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 0; i < n; i++) cin >> b[i]; n   for (int i = 0; i < n; i++) c[i] = a[i] - b[i]; n   sort(c, c + n); n   for (int i = n - 1;; i--) { n     if (c[i] <= 0) break; n     int ans1 = &c[i] - upper_bound(c, c + n, 0 - c[i]); n     ans += ans1; n   } n   cout << ans << endl; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 10; n unsigned long long preLef[N], preRig[N], sum[N], a[N]; n map<unsigned long long, unsigned long long> f, g; n int n, m; n unsigned long long Rand() { n   return (unsigned long long)rand() * rand() * rand() + n          (unsigned long long)rand() * rand() * rand(); n } n int main() { n   srand(time(0)); n   scanf( %d%d , &n, &m); n   for (int i = 0, l, r; i < n; ++i) { n     scanf( %d%d , &l, &r); n     unsigned long long t = Rand(); n     a[l] ^= t; n     a[r + 1] ^= t; n     preLef[l] ^= t; n     preRig[r] ^= t; n   } n   for (int i = 1; i <= m + 1; ++i) { n     a[i] ^= a[i - 1]; n     sum[i] = sum[i - 1] ^ a[i]; n     preLef[i] ^= preLef[i - 1]; n     preRig[i] ^= preRig[i - 1]; n   } n   g[0] = 0; n   f[0] = 1; n   unsigned long long ans = 0; n   for (int i = 1; i <= m; ++i) { n     unsigned long long t = sum[i] ^ preLef[i]; n     ans += f[t] * i - g[t]; n     t = sum[i] ^ preRig[i]; n     g[t] += i; n     ++f[t]; n   } n   for (int i = 1, lf = 0; i <= m; ++i) n     if (!a[i]) n       ans -= (unsigned long long)(i - lf + 1) * (i - lf) / 2; n     else n       lf = i; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e4 + 5; n int n, a[N]; n int main() { n   ios ::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   if (fopen( main.in ,  r )) freopen( main.in ,  r , stdin); n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   int cnt = 0; n   for (int i = 2; i < n; i++) n     if (a[i - 1] == 1 && a[i + 1] == 1 && a[i] == 0) a[i + 1] = 0, cnt++; n   cout << cnt; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char s[10]; n int main() { n   int n, i, len; n   long long wi = 1, he = 1; n   scanf( %d , &n); n   for (i = 1; i <= n; i++) { n     scanf( %s , s); n     len = strlen(s); n     if (len == 4) { n       wi++; n       he++; n     } else if (!strcmp(s,  UL ) || !strcmp(s,  DR )) n       wi++; n     else if (!strcmp(s,  UR ) || !strcmp(s,  DL )) n       he++; n   } n   printf( %I64d n , wi * he); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string rotate(string s, int off, int* a) { n   string new_s(s); n   for (int i = 0; i < 4; i++) new_s[a[(i + off) % 4] - 1] = s[a[i] - 1]; n   return new_s; n } n string rotate1(string s, int off) { n   int a[] = {1, 3, 5, 6}; n   return rotate(s, off, a); n } n string rotate2(string s, int off) { n   int a[] = {2, 6, 4, 3}; n   return rotate(s, off, a); n } n int main() { n   string s; n   cin >> s; n   sort(s.begin(), s.end()); n   map<string, int> m; n   int ans = 0; n   do { n     bool flg = true; n     map<string, int> mm; n     for (int i = 0; i < 4; i++) { n       string b = rotate1(s, i); n       for (int j = 0; j < 4; j++) { n         string bb = rotate2(b, j); n         if (mm.find(bb) == mm.end()) n           if (m.find(bb) != m.end()) flg = false; n         mm[bb] = m[bb] = 1; n       } n     } n     if (flg) ans++; n   } while (next_permutation(s.begin(), s.end())); n   cout << ans; n   return 0; n } n


n // Problem: A. Searching Local Minimum n // Contest: Codeforces - Codeforces Round #700 (Div. 1) n // URL: https://codeforces.com/contest/1479/problem/A n // Memory Limit: 512 MB n // Time Limit: 2000 ms n // Powered by CP Editor (https://github.com/cpeditor/cpeditor) n  n #include <iostream> n #include <iosfwd> n #include <iomanip> n #include <cstdio> n #include <cstring> n #include <cstdlib> n #include <ctime> n #include <cmath> n #include <cassert> n #include <cctype> n #include <climits> n #include <vector> n #include <bitset> n #include <set> n #include <queue> n #include <math.h> n #include <stack> n #include <map> n #include <deque> n #include <string> n #include <list> n #include <iterator> n #include <sstream> n #include <complex> n #include <fstream> n #include <functional> n #include <numeric> n #include <utility> n #include <algorithm> n #include <assert.h> n #include <unordered_map> n #pragma GCC optimize ( -O3 ) n  n using namespace std; n  n #define watch(x) cout << (#x) <<   =   << (x) << endl n #define PI double(2 * acos(0.0)) n #define LL long long n #define MOD 1000000007 n #define all(x) (x).begin(), (x).end() n #define INF 1e15 n int query(int a, int n){ n       if(a == 0 || a == n + 1){ n             return 1e7; n       } n       cout <<  ?   << a << endl; n       int p; n       cin >> p; n       return p; n } n int helper(int a, int n){ n       int left = a - 1; n       int right = a + 1; n       int mid = a; n       int p = query(left, n); n       int q = query(mid, n); n       int r = query(right, n); n       if(p > q && q < r){ n             return 0; n       } n       if(p < q){ n             return 2; n       } n       else{ n             return 1; n       } n } n int main() { n       int n; n       cin >> n; n       int start = 1; n       int end = n; n       int ans = -1; n       while(start <= end){ n             int mid = (start + end) / 2; n             int p = helper(mid, n); n             if(p == 0){ n                   ans = mid; n                   break; n             } n             if(p == 1){ n                   start = mid + 1; n             }else{ n                   end = mid - 1; n             } n       } n       cout <<  !   << ans << endl; n } n  n  n  n  n  n  n  n  n  n  n  n  n  n  n  n  n  n  n  n  n  n  n  n  n  n  n  n  n  n  n  n  n  n  n  n  n  n  n  n


#include <bits/stdc++.h> n using namespace std; n const int INF = INT_MAX; n const long long INFL = LLONG_MAX; n const long double pi = acos(-1); n class SumSegTree { n  private: n   vector<long long> sum, lazy; n   int N; n   void check_index(int i) { n     if (i < 0 || N <= i) { n       throw  index out of bound ; n     } n   } n  n  public: n   SumSegTree(int N) { n     this->N = 1; n     while (this->N < N) this->N <<= 1; n     this->sum.resize(this->N << 1); n     this->lazy.resize(this->N << 1); n   } n   long long get_sum(int l, int r) { n     check_index(l); n     check_index(r); n     return get_sum(l, r, 0, N - 1, 1); n   } n   void add(int l, int r, int val) { n     check_index(l); n     check_index(r); n     add(l, r, val, 0, N - 1, 1); n   } n   void delegate(int a, int b, int n) { n     sum[n] += (b - a + 1) * lazy[n]; n     if (a < b) { n       lazy[n * 2] += lazy[n]; n       lazy[n * 2 + 1] += lazy[n]; n     } n     lazy[n] = 0; n   } n   long long get_sum(int l, int r, int a, int b, int n) { n     if (b < l || r < a) return 0; n     if (lazy[n] != 0) delegate(a, b, n); n     if (l <= a && b <= r) return sum[n]; n     int m = (a + b) / 2; n     return get_sum(l, r, a, m, n * 2) + get_sum(l, r, m + 1, b, n * 2 + 1); n   } n   void add(int l, int r, long long val, int a, int b, int n) { n     if (b < l || r < a) return; n     if (l <= a && b <= r) { n       if (a == b) n         sum[n] += val; n       else n         lazy[n] += val; n     } else { n       int m = (a + b) / 2; n       if (lazy[n] != 0) delegate(a, b, n); n       sum[n] += (min(b, r) - max(a, l) + 1) * val; n       add(l, r, val, a, m, n * 2); n       add(l, r, val, m + 1, b, n * 2 + 1); n     } n   } n }; n int N, M; n vector<int> adj[300100]; n vector<pair<int, long long> > query[300100]; n SumSegTree segt(300005); n long long ans[300100]; n void dfs(int u, int prev, int h) { n   for (int(i) = 0; (i) < (int((query[u]).size())); (i)++) { n     int dist = query[u][i].first; n     long long value = query[u][i].second; n     segt.add(h, min(N, h + dist), value); n   } n   ans[u] = segt.get_sum(h, h); n   for (int(i) = 0; (i) < (int((adj[u]).size())); (i)++) { n     int v = adj[u][i]; n     if (v == prev) continue; n     dfs(v, u, h + 1); n   } n   for (int(i) = 0; (i) < (int((query[u]).size())); (i)++) { n     int dist = query[u][i].first; n     long long value = query[u][i].second; n     segt.add(h, min(N, h + dist), -value); n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cout.precision(15); n   cout << fixed; n   cout.tie(0); n   cin.tie(0); n   cin >> N; n   for (int(i) = 1; (i) <= (N - 1); (i)++) { n     int a, b; n     cin >> a >> b; n     adj[a].push_back(b); n     adj[b].push_back(a); n   } n   cin >> M; n   for (int(i) = 1; (i) <= (M); (i)++) { n     int v, d; n     long long x; n     cin >> v >> d >> x; n     query[v].push_back(make_pair(d, x)); n   } n   dfs(1, 0, 0); n   for (int(i) = 1; (i) <= (N); (i)++) n     cout << ans[i] << (i + 1 <= N ?     :   n ); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   pair<int, int> home, uni; n   cin >> home.first >> home.second >> uni.first >> uni.second; n   int count; n   cin >> count; n   int solution = 0; n   int64_t a, b, c; n   for (int i = 0; i < count; i++) { n     cin >> a >> b >> c; n     bool h_p = a * home.first + b * home.second + c > 0; n     bool u_p = a * uni.first + b * uni.second + c > 0; n     if ((h_p && !u_p) || (!h_p && u_p)) { n       solution++; n     } n   } n   cout << solution << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(void) { n   int n; n   char str[102][102]; n   scanf( %d , &n); n   bool row, col; n   row = col = false; n   for (int i = 0; i < n; i++) { n     scanf( %s , str[i]); n     bool bolong = false; n     for (int j = 0; j < n; j++) { n       if (str[i][j] ==  . ) { n         bolong = true; n       } n     } n     if (!bolong) { n       row = true; n     } n   } n   if (!row) { n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < n; j++) { n         if (str[i][j] ==  . ) { n           printf( %d %d n , i + 1, j + 1); n           break; n         } n       } n     } n     return 0; n   } n   for (int i = 0; i < n; i++) { n     bool bolong = false; n     for (int j = 0; j < n; j++) { n       if (str[j][i] ==  . ) bolong = true; n     } n     if (!bolong) col = true; n   } n   if (!col) { n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < n; j++) { n         if (str[j][i] ==  . ) { n           printf( %d %d n , j + 1, i + 1); n           break; n         } n       } n     } n   } else n     printf( -1 ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937_64 rng( n     (unsigned)chrono::system_clock::now().time_since_epoch().count()); n const long double error = 1e-7; n const long double PI = acosl(-1); n const int nmax = 2e5 + 10; n int master[nmax]; n int sz[nmax]; n void init(int u) { n   master[u] = u; n   sz[u] = 1; n   return; n } n int fnd(int u) { n   if (master[u] == u) return u; n   return master[u] = fnd(master[u]); n } n bool unite(int u, int v) { n   u = fnd(u); n   v = fnd(v); n   if (u == v) return false; n   if (sz[u] < sz[v]) swap(u, v); n   sz[u] += sz[v]; n   master[v] = u; n   return true; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   int n, m; n   cin >> n >> m; n   vector<long long int> A(n + 1), B(m + 1); n   for (int i = 1; i <= n; i++) { n     cin >> A[i]; n   } n   for (int i = 1; i <= m; i++) { n     cin >> B[i]; n   } n   vector<pair<long long int, pair<int, int>>> edges; n   long long int totcost = 0; n   for (int i = 1; i <= n; i++) { n     int k; n     cin >> k; n     for (int j = 0; j < k; j++) { n       int x; n       cin >> x; n       edges.emplace_back(A[i] + B[x], make_pair(i, n + x)); n       totcost += A[i] + B[x]; n     } n   } n   for (int i = 1; i <= n + m; i++) init(i); n   sort(edges.begin(), edges.end()); n   reverse(edges.begin(), edges.end()); n   long long int ans = 0; n   for (auto e : edges) { n     if (unite(e.second.first, e.second.second)) ans += e.first; n   } n   ans = totcost - ans; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int L[100010]; n int P[100010][20]; n vector<int> pp[100010][20]; n int T[100010]; n vector<int> tt[100010]; n int arr[100010]; n vector<int> graph[100010]; n vector<int> fnc(vector<int> &a, vector<int> &b) { n   vector<int> ans; n   int aa = a.size(), bb = b.size(), i, j, shajib = 0; n   for (i = 0, j = 0; i + j < aa + bb && shajib < 10;) { n     if (i == aa) { n       ans.push_back(b[j]); n       j++; n       shajib++; n     } else if (j == bb) { n       ans.push_back(a[i]); n       i++; n       shajib++; n     } else { n       if (a[i] == b[j]) { n         i++; n       } else if (a[i] > b[j]) { n         ans.push_back(b[j]); n         j++; n         shajib++; n       } else { n         ans.push_back(a[i]); n         i++; n         shajib++; n       } n     } n   } n   return ans; n } n void dfs(int src, int par) { n   if (src == -1) n     L[par] = 1; n   else n     L[par] = L[src] + 1; n   T[par] = src; n   int len = graph[par].size(); n   for (int i = 0; i < (int)graph[par].size(); i++) { n     int v = graph[par][i]; n     if (v != src) { n       dfs(par, v); n     } n   } n } n vector<int> query(int n, int p, int q) { n   int log = 1, i, j, temp; n   if (L[p] < L[q]) { n     temp = q; n     q = p; n     p = temp; n   } n   log = log2(n); n   vector<int> os; n   os = fnc(os, tt[p]); n   for (j = log; j >= 0; j--) { n     if (L[p] - (1 << j) >= L[q]) { n       os = fnc(os, pp[p][j]); n       p = P[p][j]; n     } n   } n   if (p == q) return os; n   os = fnc(os, tt[q]); n   for (j = log; j >= 0; j--) { n     if (P[p][j] != -1 && P[p][j] != P[q][j]) { n       os = fnc(os, pp[p][j]); n       os = fnc(os, pp[q][j]); n       p = P[p][j], q = P[q][j]; n     } n   } n   os = fnc(os, pp[p][0]); n   return os; n } n void lca(int n) { n   memset(P, -1, sizeof(P)); n   int i, j; n   for (i = 1; i <= n; i++) { n     P[i][0] = T[i]; n     if (T[i] != -1) pp[i][0] = fnc(pp[i][0], tt[T[i]]); n   } n   for (j = 1; (1 << j) < n; j++) { n     for (i = 1; i <= n; i++) { n       if (P[i][j - 1] != -1) { n         P[i][j] = P[P[i][j - 1]][j - 1]; n         pp[i][j] = fnc(pp[i][j], pp[P[i][j - 1]][j - 1]); n         pp[i][j] = fnc(pp[i][j], pp[i][j - 1]); n       } n     } n   } n } n int main() { n   int i, j, n, m, t, a, b, k, c; n   scanf( %d%d%d , &n, &m, &k); n   for (i = 0; i < n - 1; i++) { n     scanf( %d%d , &a, &b); n     graph[a].push_back(b); n     graph[b].push_back(a); n   } n   for (i = 1; i <= m; i++) { n     scanf( %d , &a); n     tt[a].push_back(i); n   } n   for (i = 1; i <= n; i++) { n     sort(tt[i].begin(), tt[i].end()); n     a = tt[i].size(); n     while (a > 10) { n       tt[i].pop_back(); n       a--; n     } n   } n   dfs(-1, 1); n   lca(n); n   vector<int> os; n   for (i = 1; i <= k; i++) { n     scanf( %d%d%d , &a, &b, &c); n     os = query(n, a, b); n     c = min(c, (int)os.size()); n     a = 0; n     printf( %d  , c); n     for (auto it = os.begin(); a < c && it != os.end(); a++, it++) { n       printf( %d  , (*it)); n     } n     printf(  n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int a[107]; n map<string, int> f; n vector<pair<int, string>> b; n bool cmp(pair<int, string> u, pair<int, string> v) { return u.first > v.first; } n int main() { n   cin >> n >> m; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   for (int i = 1; i <= m; i++) { n     string s; n     cin >> s; n     if (f[s] == 0) n       f[s] = 1; n     else n       f[s]++; n   } n   sort(a + 1, a + n + 1); n   for (map<string, int>::iterator it = f.begin(); it != f.end(); ++it) { n     b.push_back(make_pair(it->second, it->first)); n   } n   sort(b.begin(), b.end(), cmp); n   long long minP = 0, maxP = 0; n   for (int i = 0; i < b.size(); i++) { n     minP += b[i].first * a[i + 1]; n     maxP += b[i].first * a[n - i]; n   } n   cout << minP <<     << maxP << endl; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   long long cos, rlev; n   bool operator<(const node &a) const { return cos < a.cos; } n } f[300005]; n int main() { n   int n, k; n   while (scanf( %d%d , &n, &k) != EOF) { n     for (int i = 1; i <= n; i++) { n       scanf( %I64d , &f[i].cos); n       f[i].rlev = i; n     } n     priority_queue<node> p; n     long long ans = 0; n     for (int i = 1; i <= k; i++) p.push(f[i]); n     for (int i = k + 1; i <= k + n; i++) { n       if (i <= n) p.push(f[i]); n       node now = p.top(); n       p.pop(); n       if (i >= now.rlev) { n         ans += (i - now.rlev) * now.cos; n         f[now.rlev].rlev = i; n       } n     } n     printf( %I64d n , ans); n     for (int i = 1; i < n; i++) { n       printf( %I64d  , f[i].rlev); n     } n     printf( %I64d n , f[n].rlev); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long mul(long long a, long long b, long long p = 1000000007) { n   return ((a % p) * (b % p)) % p; n } n long long add(long long a, long long b, long long p = 1000000007) { n   return (a % p + b % p) % p; n } n void input(long long a[], long long sz) { n   for (long long i = 0; i < sz; i++) cin >> a[i]; n } n void print(long long a[], long long sz) { n   for (long long i = 0; i < sz; i++) { n     if (i == sz - 1) n       cout << a[i] <<   n ; n     else n       cout << a[i] <<    ; n   } n } n long long maxr(long long a[], long long sz) { n   long long ma; n   for (long long i = 0; i < sz; i++) { n     if (i == 0) n       ma = a[i]; n     else if (a[i] > ma) n       ma = a[i]; n   } n   return ma; n } n long long minr(long long a[], long long sz) { n   long long mi; n   for (long long i = 0; i < sz; i++) { n     if (i == 0) n       mi = a[i]; n     else if (a[i] < mi) n       mi = a[i]; n   } n   return mi; n } n long long isprm(long long n) { n   if (n <= 1) return 0; n   if (n <= 3) return 1; n   if (n % 2 == 0 || n % 3 == 0) return 0; n   for (int i = 5; i * i <= n; i = i + 6) n     if (n % i == 0 || n % (i + 2) == 0) return 0; n   return 1; n } n long long power(long long x, long long y, long long p = 1000000007) { n   long long res = 1; n   x = x % p; n   while (y > 0) { n     if (y & 1) res = (res * x) % p; n     y = y >> 1; n     x = (x * x) % p; n   } n   return res; n } n long long modInverse(long long n, long long p = 1000000007) { n   return power(n, p - 2, p); n } n long long ncrMod(long long n, long long r, long long p = 1000000007) { n   if (r == 0) return 1; n   long long fac[n + 1]; n   fac[0] = 1; n   for (long long i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % p; n   return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % n          p; n } n bool comp(const pair<int, int> &a, const pair<int, int> &b) { n   if (a.first == b.first) { n     return (a.second > b.second); n   } n   return (a.first < b.first); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   int t; n   cin >> t; n   while (t--) { n     long long n, k; n     cin >> n >> k; n     long long q = n / k; n     long long r = n % k; n     if ((r % 2 == 1 && k % 2 == 0) || (r % 2 == 1 && q == 1) || q == 0) n       cout <<  NO n ; n     else { n       cout <<  YES n ; n       if (r % 2 == 0) { n         cout << q + r <<    ; n         for (long long i = 0; i < k - 1; i++) cout << q <<    ; n         cout <<   n ; n       } else { n         cout << q + r <<    ; n         for (long long i = 0; i < k - 1; i++) { n           if (i % 2 == 0) n             cout << q - 1 <<    ; n           else n             cout << q + 1 <<    ; n         } n         cout <<   n ; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 5e5 + 20; n struct edge { n   int v, l, r; n   edge() {} n   edge(int a, int b, int c) { n     v = a; n     l = b; n     r = c; n   } n }; n struct nd { n   int no, lim, to; n   nd() {} n   nd(int a, int b, int c) { n     no = a; n     lim = b; n     to = c; n   } n }; n bool operator<(const nd& a, const nd& b) { n   if (a.no != b.no) { n     return a.no > b.no; n   } n   if (a.lim != b.lim) { n     return a.lim > b.lim; n   } n   return a.to < b.to; n } n bool operator<(const edge& a, const edge& b) { n   if (a.l != b.l) { n     return a.l < b.l; n   } n   return a.r < b.r; n } n vector<edge> G[2 * maxn]; n int now[2 * maxn]; n int main() { n   int n, m, u, v, l, r; n   scanf( %d%d , &n, &m); n   for (int i = 0; i < m; i++) { n     scanf( %d%d%d%d , &u, &v, &l, &r); n     if (l & 1) { n       if (r & 1) { n         G[u].push_back(edge(v + n, l + 1, r - 1)); n         G[v].push_back(edge(u + n, l + 1, r - 1)); n         G[u + n].push_back(edge(v, l, r - 2)); n         G[v + n].push_back(edge(u, l, r - 2)); n       } else { n         if (l + 1 < r) { n           G[u].push_back(edge(v + n, l + 1, r - 2)); n           G[v].push_back(edge(u + n, l + 1, r - 2)); n         } n         G[u + n].push_back(edge(v, l, r - 1)); n         G[v + n].push_back(edge(u, l, r - 1)); n       } n     } else { n       if (r & 1) { n         G[u].push_back(edge(v + n, l, r - 1)); n         G[v].push_back(edge(u + n, l, r - 1)); n         if (l + 1 < r) { n           G[u + n].push_back(edge(v, l + 1, r - 2)); n           G[v + n].push_back(edge(u, l + 1, r - 2)); n         } n       } else { n         G[u].push_back(edge(v + n, l, r - 2)); n         G[v].push_back(edge(u + n, l, r - 2)); n         G[u + n].push_back(edge(v, l + 1, r - 1)); n         G[v + n].push_back(edge(u, l + 1, r - 1)); n       } n     } n   } n   for (int i = 1; i <= n + n; i++) { n     sort(G[i].begin(), G[i].end()); n   } n   priority_queue<nd> Q; n   Q.push(nd(0, 0, 1)); n   int ans = 0x3f3f3f3f; n   nd t; n   while (!Q.empty()) { n     t = Q.top(); n     int u = t.to, no = t.no, lim = t.lim; n     Q.pop(); n     if (u == n || u == n * 2) { n       ans = min(ans, no); n     } n     for (int& i = now[u]; i < G[u].size(); i++) { n       if (lim < G[u][i].l) { n         break; n       } n       if (no <= G[u][i].r) { n         Q.push(nd(max(no + 1, G[u][i].l + 1), G[u][i].r + 1, G[u][i].v)); n       } n     } n   } n   if (ans == 0x3f3f3f3f) { n     printf( -1 n ); n   } else { n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1000008; n int n, m, k, d, i, j, e; n int tmp[N], pos[N], p[N], c[N]; n char s[N]; n int main() { n   scanf( %s %d , s, &m); n   n = strlen(s); n   while (m--) { n     scanf( %d%d , &k, &d); n     for (i = 0; i < n; i++) { n       c[i] = (i + 1) % n; n       pos[i] = p[i] = i; n     } n     int t = 0; n     for (i = 0; i < d; i++) n       for (j = i; j < k; t++, j += d) p[t] = j; n     for (i = 0; i < n; ++i) { n       c[i] = p[c[i]]; n     } n     for (e = n - k + 1; e; e >>= 1) { n       for (i = 0; i < n; ++i) tmp[i] = c[i]; n       if (e & 1) n         for (i = 0; i < n; ++i) pos[i] = c[pos[i]]; n       for (i = 0; i < n; ++i) c[i] = tmp[c[i]]; n     } n     for (i = 0; i < n; ++i) tmp[(i + n - k + 1) % n] = s[pos[i]]; n     for (i = 0; i < n; ++i) putchar(s[i] = (char)tmp[i]); n     putchar(  n ); n   } n } n