#include <bits/stdc++.h> n using namespace std; n int a[514]; n long long int dp[514][514]; n long long int mod = 1000000007ll; n long long int mdp(int s, int e) { n   if (s == e) return 1; n   if (dp[s][e] != -1) return dp[s][e]; n   long long int& ans = dp[s][e]; n   if (s + 1 == e) return ans = 1; n   ans = mdp(s + 1, e); n   for (int i = s + 1; i < e; i++) { n     if (a[s] < a[i]) { n       ans += mdp(s + 1, i) * mdp(i, e) % mod; n     } n   } n   return ans = ans % mod; n } n int sol() { n   memset(dp, -1, sizeof(dp)); n   int n; n   if (scanf( %d , &n) == EOF) return 0; n   for (int i = 0; i < n; i++) { n     scanf( %d , a + i); n   } n   printf( %I64d n , n == 1 ? 1 : mdp(1, n)); n   return 1; n } n int main() { n   while (sol()) n     ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N, a, b, Q; n long long W[(500000)], S[(500000)], O[(500000)]; n map<pair<int, int>, vector<pair<int, int> > > M; n void pro(int t, int b, long long* g, int& l) { n   *g = 0, l = 1; n   while (b < N) { n     long long S = W[b] + g[l - 1]; n     g[l++] = S, b += t; n   } n } n int main() { n   scanf( %d , &N); n   for (int i(0); i < N; i++) scanf( %lld , W + i); n   scanf( %d , &Q); n   for (int i(0); i < Q; i++) n     scanf( %d%d , &a, &b), --a, M[{b, a % b}].push_back({a, i}); n   for (auto& h : M) { n     pro(h.first.first, h.first.second, S, a); n     for (auto& w : h.second) n       O[w.second] = S[a - 1] - S[w.first / h.first.first]; n   } n   for (int i(0); i < Q; i++) printf( %lld n , O[i]); n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n int chkmax(T& a, T b) { n   if (b > a) { n     a = b; n     return 1; n   } n   return 0; n } n template <class T> n int chkmin(T& a, T b) { n   if (b < a) { n     a = b; n     return 1; n   } n   return 0; n } n template <class iterator> n void output(iterator begin, iterator end, ostream& out = cerr) { n   while (begin != end) { n     out << (*begin) <<    ; n     begin++; n   } n   out <<   n ; n } n template <class T> n void output(T x, ostream& out = cerr) { n   output(x.begin(), x.end(), out); n } n const int MOD = 1e9 + 7; n const int INF = 1e9; n int n, flag, ans = 1; n set<int> buy, sell, middle; n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int max_buy = -INF, min_sell = INF; n   cin >> n; n   for (int i = 0; i < n; ++i) { n     int x; n     string s; n     cin >> s >> x; n     if (flag) continue; n     if (s ==  ADD ) { n       if (x <= max_buy) buy.insert(x); n       if (x >= min_sell) sell.insert(x); n       if (x > max_buy && x < min_sell) middle.insert(x); n     } else { n       if (x < max_buy || x > min_sell) { n         cout << 0 <<   n ; n         flag = 1; n         continue; n       } n       if (x > max_buy && x < min_sell) ans = (ans + ans) % MOD; n       for (auto val : middle) { n         if (val < x) n           buy.insert(val); n         else n           sell.insert(val); n       } n       middle.clear(); n       if (buy.find(x) != buy.end()) n         buy.erase(x); n       else n         sell.erase(x); n       max_buy = -INF; n       min_sell = INF; n       if (!buy.empty()) max_buy = (*prev(buy.end())); n       if (!sell.empty()) min_sell = (*sell.begin()); n     } n   } n   if (flag) return 0; n   ans = ((long long)ans * (long long)(middle.size() + 1)) % (long long)MOD; n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, d; n   cin >> d >> n; n   long long int ilosc = 0; n   for (int i = 0; i < n - 1; i++) { n     int p; n     cin >> p; n     ilosc += (d - p); n   } n   cin >> d; n   cout << ilosc <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long fastpow(long long base, long long power, long long M) { n   if (power == 1) return base; n   long long val = fastpow(base, power / 2, M); n   return (power % 2 == 0) ? (val * val) % M : (((val * val) % M) * base) % M; n } n inline void debugMode() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n } n const int N = 1e5 + 1; n const int MOD = 1e9 + 7; n int main() { n   debugMode(); n   int s, b; n   cin >> s >> b; n   int arr[s]; n   for (int i = 0; i < s; i++) cin >> arr[i]; n   vector<pair<int, int>> base(b); n   for (int i = 0; i < b; i++) cin >> base[i].first >> base[i].second; n   sort((base).begin(), (base).end()); n   for (int i = 1; i < b; i++) base[i].second += base[i - 1].second; n   for (int i = 0; i < s; i++) { n     int left = -1, right = b; n     while (left + 1 < right) { n       int mid = (left + right) / 2; n       if (base[mid].first <= arr[i]) n         left = mid; n       else n         right = mid; n     } n     if (left == -1) n       cout << 0 <<    ; n     else n       cout << base[left].second <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void cmax(T& a, T b) { n   a = max(a, b); n } n template <typename T> n void cmin(T& a, T b) { n   a = min(a, b); n } n void _BG(const char* s) {} n template <typename T, typename... TT> n void _BG(const char* s, T a, TT... b) { n   for (int c = 0; *s && (c || *s !=  , ); ++s) { n     cerr << *s; n     switch (*s) { n       case  ( : n       case  [ : n       case  { : n         ++c; n         break; n       case  ) : n       case  ] : n       case  } : n         --c; n         break; n     } n   } n   cerr <<   =   << a; n   if (*s) { n     cerr <<  ,  ; n     _BG(++s, b...); n   } else n     cerr << endl; n } n bool RD(void) { return true; } n bool RD(char& a) { return scanf(  %c , &a) == 1; } n bool RD(char* a) { return scanf( %s , a) == 1; } n bool RD(double& a) { return scanf( %lf , &a) == 1; } n bool RD(int& a) { return scanf( %d , &a) == 1; } n bool RD(long long& a) { return scanf( %lld , &a) == 1; } n template <typename T, typename... TT> n bool RD(T& a, TT&... b) { n   return RD(a) && RD(b...); n } n void PT(const char a) { putchar(a); } n void PT(const char* a) { fputs(a, stdout); } n void PT(char* a) { fputs(a, stdout); } n void PT(const double a) { printf( %.16f , a); } n void PT(const int a) { printf( %d , a); } n void PT(const long long a) { printf( %lld , a); } n template <char sep =    , char end =   n > n void PL(void) { n   if (end) PT(end); n } n template <char sep =    , char end =   n , typename T, typename... TT> n void PL(const T a, const TT... b) { n   PT(a); n   if (sizeof...(b) && sep) PT(sep); n   PL<sep, end>(b...); n } n const long long inf = 4e10; n const int N = 106; n int n, r, a[N]; n pair<int, int> p[N]; n long long d[N]; n bool used[N]; n long long dis(int i, int j) { n   return r * (abs(p[i].first - p[j].first) + abs(p[i].second - p[j].second)) - n          a[i]; n } n int main() { n   RD(n, r); n   for (int i(2), _B((n - 1) + 1); i < _B; ++i) RD(a[i]); n   for (int i(1), _B((n) + 1); i < _B; ++i) RD(p[i].first, p[i].second); n   fill_n(d, n + 1, inf); n   d[1] = 0; n   for (int u = 1, v; u != n; u = v) { n     used[u] = 1; n     v = 0; n     for (int i(1), _B((n) + 1); i < _B; ++i) n       if (!used[i]) { n         cmin(d[i], d[u] + dis(u, i)); n         if (d[i] < d[v]) v = i; n       } n   } n   PL(d[n]); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1003; n int n, k; n int pos[5][maxn]; n int dp[maxn]; n bool vis[maxn]; n vector<int> v[maxn]; n int dfs(int cur) { n   if (vis[cur]) return dp[cur]; n   vis[cur] = true; n   for (int i = 0; i < v[cur].size(); i++) { n     int nxt = v[cur][i]; n     dp[cur] = max(dp[cur], 1 + dfs(nxt)); n   } n   return dp[cur]; n } n int main() { n   cin >> n >> k; n   for (__typeof(k) i = 0; i < (k); i++) { n     for (__typeof(n) j = 0; j < (n); j++) { n       int x; n       cin >> x; n       pos[i][--x] = j; n     } n   } n   for (__typeof(n) i = 0; i < (n); i++) { n     for (__typeof(n) j = 0; j < (n); j++) { n       bool flag = 1; n       for (__typeof(k) a = 0; a < (k); a++) { n         if (pos[a][i] <= pos[a][j]) { n           flag = 0; n           break; n         } n       } n       if (flag) v[i].push_back(j); n     } n   } n   for (__typeof(n) i = 0; i < (n); i++) dfs(i); n   cout << *max_element(dp, dp + n) + 1; n } n


#include <bits/stdc++.h> n using namespace std; n string imp =  Impossible n ; n const int N = 2e5 + 5; n int bfstim[N]; n long long MOD = 1e9 + 7; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int tests; n   tests = 1; n   cin >> tests; n   while (tests--) { n     int n; n     cin >> n; n     if (n > 2) { n       cout << n - 2 <<   n ; n     } else n       cout <<  0 n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1000000007; n const int UNDEF = -1; n const long long INF = 1e18; n template <typename T> n inline bool chkmax(T &aa, T bb) { n   return aa < bb ? aa = bb, true : false; n } n template <typename T> n inline bool chkmin(T &aa, T bb) { n   return aa > bb ? aa = bb, true : false; n } n int rint(); n char rch(); n long long rlong(); n void makeunique(vector<int> &vx) { n   sort(vx.begin(), vx.end()); n   auto it = unique(vx.begin(), vx.end()); n   vx.resize(std::distance(vx.begin(), it)); n } n int bins(vector<int> &vx, int x) { n   int imin = 0, imax = vx.size(); n   while (imin < imax) { n     int imid = (imin + imax) >> 1; n     if (vx[imid] < x) n       imin = imid + 1; n     else n       imax = imid; n   } n   return imin; n } n const int mn = 2e7 + 2; n int f[mn]; n int longest_strictly_increasing_subsequence(vector<int> &p, int maxval, n                                             int repeats) { n   memset(f, 0, sizeof(int) * (maxval + 1)); n   for (int r = 0; r < repeats; r++) { n     for (auto &x : p) { n       int best = f[x - 1] + 1; n       for (int y = x; y <= maxval; y++) { n         if (f[y] < best) n           f[y] = best; n         else n           break; n       } n     } n   } n   return f[maxval]; n } n int compress(vector<int> &vinp) { n   vector<int> bintable = vinp; n   makeunique(bintable); n   for (auto &w : vinp) { n     w = bins(bintable, w) + 1; n   } n   return (int)bintable.size(); n } n int n, t; n vector<int> b; n void solve() { n   for (int i = 0; i < n; i++) b[i] = rint(); n   int maxval = compress(b); n   int ans = longest_strictly_increasing_subsequence(b, maxval, min(t, maxval)); n   printf( %d n , ans); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int numtests, maxb; n   numtests = rint(), n = rint(), maxb = rint(), t = rint(); n   b.resize(n); n   for (int test = 0; test < numtests; test++) solve(); n } n static char stdinBuffer[1024]; n static char *stdinDataEnd = stdinBuffer + sizeof(stdinBuffer); n static const char *stdinPos = stdinDataEnd; n void readAhead(size_t amount) { n   size_t remaining = stdinDataEnd - stdinPos; n   if (remaining < amount) { n     memmove(stdinBuffer, stdinPos, remaining); n     size_t sz = fread(stdinBuffer + remaining, 1, n                       sizeof(stdinBuffer) - remaining, stdin); n     stdinPos = stdinBuffer; n     stdinDataEnd = stdinBuffer + remaining + sz; n     if (stdinDataEnd != stdinBuffer + sizeof(stdinBuffer)) *stdinDataEnd = 0; n   } n } n int rint() { n   readAhead(16); n   int x = 0; n   bool neg = false; n   while (*stdinPos ==     || *stdinPos ==   n ) ++stdinPos; n   if (*stdinPos ==  - ) { n     ++stdinPos; n     neg = true; n   } n   while (*stdinPos >=  0  && *stdinPos <=  9 ) { n     x *= 10; n     x += *stdinPos -  0 ; n     ++stdinPos; n   } n   return neg ? -x : x; n } n char rch() { n   readAhead(16); n   while (*stdinPos ==     || *stdinPos ==   n ) ++stdinPos; n   char ans = *stdinPos; n   ++stdinPos; n   return ans; n } n long long rlong() { n   readAhead(32); n   long long x = 0; n   bool neg = false; n   while (*stdinPos ==     || *stdinPos ==   n ) ++stdinPos; n   if (*stdinPos ==  - ) { n     ++stdinPos; n     neg = true; n   } n   while (*stdinPos >=  0  && *stdinPos <=  9 ) { n     x *= 10; n     x += *stdinPos -  0 ; n     ++stdinPos; n   } n   return neg ? -x : x; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = int; n vector<pair<ll, string>> con[101010]; n int main() { n   ios_base::sync_with_stdio(false); n   cout.tie(NULL); n   int n, m; n   cin >> n >> m; n   int reg, sc; n   string name; n   while (n--) { n     cin >> name; n     cin >> reg; n     cin >> sc; n     con[reg].push_back(make_pair(sc, name)); n   } n   for (int i = 1; i <= m; i++) { n     sort(con[i].begin(), con[i].end()); n     reverse(con[i].begin(), con[i].end()); n     if ((int)con[i].size() > 2 && con[i][1].first == con[i][2].first) { n       cout <<  ? n ; n     } else { n       cout << con[i][0].second <<     << con[i][1].second << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, x, y, a, l, w, z, ss, t, k, e, c, total, nnn, yes, n     mx = 531, mn = -1, r, xx, yy, last, cas; n map<string, int> wow; n int main() { n   string s; n   cin >> s; n   wow[s] = 1; n   x = 1; n   l = s.size(); n   for (int i = 0; i < l; i++) { n     char te = s[l - 1]; n     for (int j = l - 1; j > 0; j--) { n       s[j] = s[j - 1]; n     } n     s[0] = te; n     if (wow[s] == 0) { n       x++; n       wow[s]++; n     } n   } n   cout << x; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100005; n int n, p1l, p1r, p2l, p2r; n string s, t, ans; n int main() { n   ios_base::sync_with_stdio(false); n   cin >> s >> t; n   sort(s.begin(), s.end()); n   sort(t.begin(), t.end()); n   ans.resize(s.length(), 0); n   p1l = 0; n   p2r = (int)s.length() - 1; n   p1r = ((int)s.length() + 1) / 2 - 1; n   p2l = p2r - (int)s.length() / 2 + 1; n   n = (int)s.length() - 1; n   for (int i = 0, nm = 0; i <= n; ++i, ++nm) { n     if (t[p2r] <= s[p1l]) { n       if (nm % 2 == 0) n         ans[n] = s[p1r--]; n       else n         ans[n] = t[p2l++]; n       n -= 1; n       i -= 1; n     } else { n       if (nm % 2 == 0) n         ans[i] = s[p1l++]; n       else n         ans[i] = t[p2r--]; n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> finger; n int seq[100]; n int main() { n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < n; i++) cin >> seq[i]; n   for (int i = 0; i < m; i++) { n     int x; n     cin >> x; n     finger.push_back(x); n   } n   for (int i = 0; i < n; i++) { n     for (vector<int>::iterator itr = finger.begin(); itr != finger.end(); n          itr++) { n       if (*itr == seq[i]) { n         cout << *itr <<    ; n         break; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int u, v, w; n   cin >> u >> v; n   if (u > v || (u + v) % 2 != 0) n     cout << -1 << endl; n   else if (u == v && u == 0) n     cout << 0 << endl; n   else if (u == v) n     cout << 1 << endl << u << endl; n   else { n     w = (v - u) / 2; n     if ((u & w) == 0) n       cout << 2 << endl << u + w <<     << w << endl; n     else n       cout << 3 << endl << u <<     << w <<     << w << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 2e5 + 10; n long long ans[M << 2], tag[M << 2]; n int a[M], p[M]; n void push_up(int p) { ans[p] = min(ans[p << 1], ans[p << 1 | 1]); } n void ps(int p, int l, int r, long long x) { n   ans[p] += x; n   tag[p] += x; n } n void push_down(int p, int l, int r) { n   if (!tag[p]) return; n   int mid = l + r >> 1; n   ps(p << 1, l, mid, tag[p]); n   ps(p << 1 | 1, mid + 1, r, tag[p]); n   tag[p] = 0; n } n void add(int p, int p_l, int p_r, int l, int r, int x) { n   if (r < l) return; n   if (p_l >= l && p_r <= r) { n     ans[p] += x; n     tag[p] += x; n     return; n   } n   push_down(p, p_l, p_r); n   int mid = p_l + p_r >> 1; n   if (l <= mid) add(p << 1, p_l, mid, l, r, x); n   if (r > mid) add(p << 1 | 1, mid + 1, p_r, l, r, x); n   push_up(p); n } n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &p[i]); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     add(1, 1, n, p[i] + 1, n, a[i]); n   } n   long long sum = min(a[1], a[n]); n   for (int i = 1; i < n; i++) { n     add(1, 1, n, p[i] + 1, n, -a[i]); n     add(1, 1, n, 1, p[i] - 1, a[i]); n     sum = min(ans[1], sum); n   } n   printf( %lld n , sum); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 9; n const double eps = 1e-11; n const int inf = (int)2e9; n const int mod = (int)1e9 + 7; n const double pi = 3.1415926535897932384626433832795; n int a[N]; n int main() { n   int n, k; n   scanf( %d %d , &n, &k); n   int ex = 0; n   for (int i = 1; i <= n; i++) { n     a[i] = i; n     ex += i * (n - i + 1); n   } n   int cnt = 0; n   do { n     int cur = 0; n     for (int i = 1; i <= n; i++) { n       int mn = inf; n       for (int j = i; j <= n; j++) { n         mn = min(mn, a[j]); n         cur += mn; n       } n     } n     if (cur == ex) { n       k--; n       if (!k) { n         for (int i = 1; i <= n; i++) { n           printf( %d%c , a[i],    n [i == n]); n         } n       } n     } n   } while (next_permutation(a + 1, a + n + 1)); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int n, m; n int dir[405][405], dic[405][405]; n int main() { n   memset(dir, 63, sizeof(dir)); n   memset(dic, 63, sizeof(dic)); n   scanf( %d %d , &n, &m); n   for (int i = 1, x, y; i <= m; i++) { n     scanf( %d %d , &x, &y); n     dir[x][y] = dir[y][x] = 1; n   } n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) n       if (dir[i][j] == 1061109567) dic[i][j] = 1; n   for (int k = 1; k <= n; k++) { n     for (int i = 1; i <= n; i++) { n       for (int j = 1; j <= n; j++) n         dir[i][j] = min(dir[i][k] + dir[k][j], dir[i][j]); n     } n   } n   for (int k = 1; k <= n; k++) { n     for (int i = 1; i <= n; i++) { n       for (int j = 1; j <= n; j++) { n         int res = dic[i][k] + dic[k][j]; n         if (res == dir[i][j]) continue; n         dic[i][j] = min(dic[i][j], res); n       } n     } n   } n   int ans = max(dic[1][n], dir[1][n]); n   if (ans == 1061109567) ans = -1; n   printf( %d n , ans); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int n; n char promocode[1100][10]; n int strdif(char a[], char b[]) { n   int cnt = 0; n   for (int ii = 0; ii < 6; ii++) { n     if (a[ii] != b[ii]) cnt++; n   } n   return cnt; n } n int main() { n   scanf( %d n , &n); n   for (int ii = 1; ii <= n; ii++) { n     scanf( %s , promocode[ii]); n   } n   int ans = 6; n   for (int ii = 1; ii <= n - 1; ii++) { n     for (int jj = ii + 1; jj <= n; jj++) { n       ans = min(ans, (strdif(promocode[ii], promocode[jj]) - 1) / 2); n     } n   } n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   while (cin >> n) { n     int y = 1; n     for (; y <= n / 2 + 1; y++) { n       for (int x = 1; x <= n; x++) { n         if (x <= n / 2 + 1 - y || x >= n / 2 + 1 + y) n           cout <<  * ; n         else n           cout <<  D ; n       } n       cout <<   n ; n     } n     for (y = n / 2; y > 0; y--) { n       for (int x = 1; x <= n; x++) { n         if (x <= n / 2 + 1 - y || x >= n / 2 + 1 + y) n           cout <<  * ; n         else n           cout <<  D ; n       } n       cout <<   n ; n     } n     cout << endl; n   } n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1605; n int n, m, tot; n int a[MAXN][MAXN]; n int sz[MAXN * MAXN]; n int ans[MAXN * MAXN]; n int cnt[MAXN * MAXN]; n int fa1[MAXN * MAXN]; n int fa2[MAXN * MAXN]; n double v[MAXN][MAXN]; n double t[MAXN][MAXN]; n int getroot(int *fa, int u) { n   return u == fa[u] ? u : fa[u] = getroot(fa, fa[u]); n } n void run() { n   memcpy(t, v, sizeof(t)); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) n       if (a[i][j]) { n         double sum = 0, cnt = 0; n         for (int k = i - 4; k <= i + 4; k++) n           for (int l = j - 4; l <= j + 4; l++) n             if (k >= 1 && k <= n && l >= 1 && l <= m && n                 (i - k) * (i - k) + (j - l) * (j - l) <= 25) n               sum += t[k][l], cnt++; n         v[i][j] = sum / cnt; n       } n } n void merge(int *fa, int xa, int ya, int xb, int yb) { n   if (!a[xa][ya] || !a[xb][yb]) return; n   fa[getroot(fa, (xa - 1) * m + ya)] = getroot(fa, (xb - 1) * m + yb); n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) n       fa1[(i - 1) * m + j] = fa2[(i - 1) * m + j] = (i - 1) * m + j; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) scanf( %d , &a[i][j]); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) n       if (a[i][j]) { n         v[i][j] = 1; n         if (i > 1) merge(fa1, i, j, i - 1, j); n         if (i < n) merge(fa1, i, j, i + 1, j); n         if (j > 1) merge(fa1, i, j, i, j - 1); n         if (j < m) merge(fa1, i, j, i, j + 1); n       } n   run(); n   run(); n   run(); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) n       if (a[i][j] && v[i][j] < 0.2) { n         if (i > 1 && v[i - 1][j] < 0.2) merge(fa2, i, j, i - 1, j); n         if (i < n && v[i + 1][j] < 0.2) merge(fa2, i, j, i + 1, j); n         if (j > 1 && v[i][j - 1] < 0.2) merge(fa2, i, j, i, j - 1); n         if (j < m && v[i][j + 1] < 0.2) merge(fa2, i, j, i, j + 1); n       } n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) n       if (a[i][j] && v[i][j] < 0.2) sz[getroot(fa2, (i - 1) * m + j)]++; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) n       if (a[i][j] && v[i][j] < 0.2 && n           getroot(fa2, (i - 1) * m + j) == (i - 1) * m + j && n           sz[(i - 1) * m + j] >= 5) n         cnt[getroot(fa1, (i - 1) * m + j)]++; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) n       if (a[i][j] && getroot(fa1, (i - 1) * m + j) == (i - 1) * m + j) n         ans[++tot] = cnt[(i - 1) * m + j]; n   sort(ans + 1, ans + tot + 1); n   printf( %d n , tot); n   for (int i = 1; i <= tot; i++) printf( %d  , ans[i]); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f; n int main() { n   int n, k; n   scanf( %d %d , &n, &k); n   vector<int> A(n), acc(n); n   for (int i = (0); i < (n); ++i) cin >> A[i]; n   acc[0] = A[0]; n   for (int i = (1); i < (n); ++i) acc[i] = A[i] + acc[i - 1]; n   int rsp = -inf; n   for (int i = (0); i < (n); ++i) { n     for (int j = (i); j < (n); ++j) { n       int sum = acc[j] - (i ? acc[i - 1] : 0); n       rsp = max(rsp, sum); n       vector<int> v1, v2; n       for (int kk = (0); kk < (n); ++kk) { n         if (kk < i or kk > j) { n           v1.push_back(A[kk]); n         } else { n           v2.push_back(A[kk]); n         } n       } n       sort(v1.rbegin(), v1.rend()); n       sort(v2.begin(), v2.end()); n       int lim = min(v1.size(), v2.size()); n       for (int kk = (0); kk < (min(k, lim)); ++kk) { n         sum -= v2[kk]; n         sum += v1[kk]; n         rsp = max(rsp, sum); n       } n     } n   } n   printf( %d n , rsp); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 5e5 + 10; n struct node { n   int v, w, next; n } edges[maxn << 2]; n int head[maxn], cnt; n int n, k; n void add_edge(int u, int v, int w) { n   edges[++cnt] = {v, w, head[u]}; n   head[u] = cnt; n } n long long dp[maxn][2]; n void dfs(int rt, int fa) { n   vector<long long> vv; n   long long ans = 0; n   for (int i = head[rt]; i; i = edges[i].next) { n     if (edges[i].v == fa) continue; n     dfs(edges[i].v, rt); n     vv.push_back(dp[edges[i].v][0] + edges[i].w - dp[edges[i].v][1]); n     ans += dp[edges[i].v][1]; n   } n   sort(vv.begin(), vv.end(), greater<long long>()); n   if (k == 1) dp[rt][0] = ans; n   for (int i = 0; i < vv.size(); ++i) { n     if (i < k && vv[i] > 0) ans += vv[i]; n     if (i == k - 1 - 1) dp[rt][0] = ans; n     if (i == k - 1) dp[rt][1] = ans; n   } n   if (dp[rt][0] == -1) dp[rt][0] = ans; n   if (dp[rt][1] == -1) dp[rt][1] = ans; n } n int main() { n   int q; n   scanf( %d , &q); n   while (q--) { n     scanf( %d%d , &n, &k); n     memset(head, 0, sizeof(int) * (n + 1)); n     memset(dp, -1, sizeof(long long) * (n + 1) * 2); n     cnt = 0; n     for (int i = 0, u, v, w; i < n - 1; ++i) { n       scanf( %d%d%d , &u, &v, &w); n       add_edge(u, v, w); n       add_edge(v, u, w); n     } n     dfs(1, 0); n     printf( %lld n , max(dp[1][0], dp[1][1])); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline bool chkmin(T &a, const T &b) { n   return a > b ? a = b, 1 : 0; n } n template <typename T> n inline bool chkmax(T &a, const T &b) { n   return a < b ? a = b, 1 : 0; n } n inline int read() { n   int res = 0, fl = 1; n   char r = getchar(); n   for (; !isdigit(r); r = getchar()) n     if (r ==  - ) fl = -1; n   for (; isdigit(r); r = getchar()) res = (res << 3) + (res << 1) + r - 48; n   return res * fl; n } n const int Maxn = 2e5 + 10; n vector<pair<int, int> > g[Maxn]; n long double val[Maxn]; n int siz[Maxn]; n void dfs(int now, int pa) { n   siz[now] = 1; n   for (int i = g[now].size() - 1; i >= 0; --i) { n     int nxt = g[now][i].first; n     if (pa == nxt) continue; n     val[nxt] += g[now][i].second; n     dfs(nxt, now); n     val[now] += val[nxt]; n     siz[now] += siz[nxt]; n   } n } n long long Time, t[Maxn]; n bool cmp(pair<int, int> A, pair<int, int> B) { n   return val[A.first] > val[B.first]; n } n void Dfs(int now, int pa) { n   for (int i = g[now].size() - 1; i >= 0; --i) { n     int nxt = g[now][i].first; n     if (nxt == pa) continue; n     val[nxt] = 10000.00 * val[nxt] / siz[nxt]; n   } n   sort(g[now].begin(), g[now].end(), cmp); n   t[now] = Time; n   for (int i = g[now].size() - 1; i >= 0; --i) { n     int nxt = g[now][i].first; n     if (pa == nxt) continue; n     Time += g[now][i].second; n     Dfs(nxt, now); n     Time += g[now][i].second; n   } n } n int main() { n   int n = read(); n   for (int i = 1; i < n; ++i) { n     int x = read(), y = read(), z = read(); n     g[x].push_back(make_pair(y, z)); n     g[y].push_back(make_pair(x, z)); n   } n   dfs(1, 0); n   Dfs(1, 0); n   long double ans = 0; n   for (int i = 1; i <= n; ++i) ans += t[i]; n   printf( %.6Lf n , 1.00 * ans / (n - 1)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   node *bit[2]; n   int cnt = 0; n   int index = -1; n }; n node *root; n void insert(string &s, int id) { n   node *cur = root; n   for (char x : s) { n     int ch = x -  0 ; n     if (!cur->bit[ch]) cur->bit[ch] = new node(); n     cur = cur->bit[ch]; n     cur->cnt++; n   } n   cur->index = id; n } n int already(string &s) { n   node *cur = root; n   for (char x : s) { n     int ch = x -  0 ; n     if (!cur->bit[ch]) n       return -1; n     else if (cur->bit[ch]->cnt < 1) n       return -1; n     cur = cur->bit[ch]; n   } n   return cur->index; n } n void remove(string &s) { n   node *cur = root; n   for (char x : s) { n     int ch = x -  0 ; n     cur = cur->bit[ch]; n     cur->cnt--; n   } n   cur->index = -1; n } n void test_case() { n   root = new node(); n   int n; n   cin >> n; n   int a[n]; n   vector<bool> can(n, true); n   int both_zero = 0, both_one = 0; n   for (int i = 0; i < n; i++) { n     string s; n     cin >> s; n     a[i] = (s[0] -  0 ) * 2 + (s.back() -  0 ); n     if (s[0] != s.back()) { n       int index = already(s); n       if (index != -1) { n         can[index] = false; n         can[i] = false; n         remove(s); n       } else { n         reverse(s.begin(), s.end()); n         insert(s, i); n       } n     } else { n       can[i] = false; n     } n   } n   vector<int> possible[2]; n   vector<int> type(4, 0); n   for (int i = 0; i < n; i++) { n     if (can[i] == true) { n       possible[a[i] - 1].push_back(i + 1); n     } else { n       type[a[i]]++; n     } n   } n   vector<int> ans[2]; n   vector<int> valid(2, true); n   for (int t = 0; t < 2; t++) { n     vector<int> cnt = type; n     vector<int> p[2]; n     p[0] = possible[0]; n     p[1] = possible[1]; n     int last = t; n     for (int i = 0; i < n; i++) { n       bool found = false; n       for (int j = 0; j < 4; j++) { n         if (last == j / 2 && cnt[j] > 0) { n           cnt[j]--; n           found = true; n           last = j % 2; n           break; n         } n       } n       if (found) continue; n       if (!p[last].empty()) { n         p[last].pop_back(); n         last = 1 - last; n         continue; n       } n       if (p[1 - last].empty()) { n         valid[t] = false; n         break; n       } n       ans[t].push_back(p[1 - last].back()); n       p[1 - last].pop_back(); n       last = 1 - last; n     } n   } n   if (valid[0] == false && valid[1] == false) { n     cout <<  -1 n ; n     return; n   } n   int id = 0; n   if (valid[0] == true && valid[1] == false) { n     id = 0; n   } else if (valid[0] == false && valid[1] == true) { n     id = 1; n   } else { n     if (ans[0].size() < ans[1].size()) n       id = 0; n     else n       id = 1; n   } n   cout << (int)ans[id].size() <<   n ; n   for (int i : ans[id]) cout << i <<    ; n   cout <<   n ; n } n signed main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int t = 1; n   cin >> t; n   while (t--) test_case(); n } n


#include <bits/stdc++.h> n using namespace std; n int a[200020], b[200020], c[200020]; n int main(void) { n   int n, k; n   int m; n   int i; n   int o; n   scanf( %d %d , &n, &k); n   for (i = 1; i <= n; i++) scanf( %d , &a[i]); n   for (i = 1; i <= n; i++) scanf( %d , &b[i]); n   for (i = 1, m = o = 0; i <= n; i++) n     if (a[i] < b[i]) { n       o += a[i]; n       --k; n     } else { n       o += b[i]; n       c[m++] = a[i] - b[i]; n     } n   if (k > 0) { n     sort(c, c + m); n     for (i = 0; i < k; i++) o += c[i]; n   } n   printf( %d n , o); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char ans[100010 * 100]; n char s[100010 * 10]; n int f[100010 * 100]; n int getf(int k) { return k == f[k] ? k : f[k] = getf(f[k]); } n int main() { n   int n, k, LEN = 0, id; n   cin >> n; n   for (int i = 0; i < 100010 * 100; i++) f[i] = i; n   for (int i = 0; i < n; i++) { n     scanf( %s %d , s, &k); n     int len = strlen(s); n     while (k--) { n       scanf( %d , &id); n       for (int i = getf(id); i < id + len; i = f[i] = getf(i + 1)) { n         ans[i] = s[i - id]; n       } n       LEN = max(LEN, id + len); n     } n   } n   for (int i = 1; i < LEN; i++) { n     if (ans[i] ==   0 ) ans[i] =  a ; n   } n   puts(ans + 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void foobar() { n   long long int n, i; n   cin >> n; n   vector<long long int> arr(n); n   long long int mm = INT_MAX; n   for (i = 0; i < n; ++i) { n     cin >> arr[i]; n     mm = min(mm, arr[i]); n   } n   for (i = 0; i < n; ++i) { n     arr[i] -= mm; n     if (arr[i] % 2) { n       cout <<  NO  << endl; n       return; n     } n   } n   cout <<  YES  << endl; n } n int main() { n   long long int test; n   cin >> test; n   while (test--) foobar(); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ofstream out( output.txt ); n   auto start = chrono::high_resolution_clock::now(); n   long long t = 1; n   for (long long test = 1; test <= t; test++) { n     long long n; n     cin >> n; n     vector<long long> a(n); n     pair<long long, long long> b[n]; n     for (long long i = 0; i <= n - 1; i++) n       cin >> a[i], b[i].first = a[i], b[i].second = i; n     sort(b, b + n); n     long long ord[n]; n     long long shts = 0, I = 0; n     for (long long i = n - 1; i >= 0; i--) { n       shts += (b[i].first * I + 1); n       ord[I] = b[i].second + 1; n       I++; n     } n     cout << shts <<   n ; n     for (long long i = 0; i <= n - 1; i++) cout << ord[i] <<    ; n     cout <<   n ; n   } n   auto stop = chrono::high_resolution_clock::now(); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 500043; n const int K = 750; n int a[N]; n int sum[K][K]; n int main() { n   int q; n   scanf( %d , &q); n   for (int i = 0; i < q; i++) { n     int t, x, y; n     scanf( %d %d %d , &t, &x, &y); n     if (t == 1) { n       a[x] += y; n       for (int i = 1; i < K; i++) sum[i][x % i] += y; n     } else { n       if (x >= K) { n         int ans = 0; n         for (int i = y; i <= 500000; i += x) ans += a[i]; n         printf( %d n , ans); n       } else n         printf( %d n , sum[x][y]); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline bool cmin(T& a, const T& b) { n   return a > b ? a = b, 1 : 0; n } n template <typename T> n inline bool cmax(T& a, const T& b) { n   return a < b ? a = b, 1 : 0; n } n int read() { n   int x = 0, f = 1; n   char ch; n   for (ch = getchar(); !isdigit(ch); ch = getchar()) n     if (ch ==  - ) f = -1; n   for (; isdigit(ch); x = x * 10 + ch -  0 , ch = getchar()) n     ; n   return x * f; n } n const int MaxN = 48, MaxM = 1 << (MaxN / 2); n int n, m, F[MaxN]; n int find(int x) { return F[x] == x ? x : F[x] = find(F[x]); } n char c[MaxN][MaxN]; n int sz[MaxN], b[MaxM], h[MaxM], g[MaxM], f[MaxM]; n int main() { n   int i, j, k; n   n = read(); n   for (i = 0; i < n; i++) F[i] = i; n   for (i = 0; i < n; i++) { n     scanf( %s , c[i]); n     for (j = 0; j < i; j++) n       if (c[i][j] ==  A ) F[find(i)] = find(j); n   } n   for (i = 0; i < n; i++) sz[find(i)]++; n   for (i = 0; i < n; i++) n     if (find(i) == i && sz[i] > 1) h[i] = 1 << m, m++; n   for (i = 0; i < n; i++) n     for (j = 0; j < i; j++) n       if (c[i][j] ==  X ) { n         int x = find(i), y = find(j); n         if (x == y) return puts( -1 ), 0; n         if (h[x] && h[y]) f[h[x] | h[y]] = 1; n       } n   if (!m) return printf( %d n , n - 1), 0; n   for (i = 0; i < (1 << m); i++) n     for (j = 0; j < m; j++) n       if (i >> j & 1) f[i] |= f[i ^ (1 << j)]; n   for (i = 0; i < (1 << m); i++) { n     b[i] = b[i >> 1] + (i & 1); n     f[i] = !f[i]; n     g[i] = 1; n   } n   for (j = 0; j < m; j++) n     for (i = 0; i < (1 << m); i++) n       if (i >> j & 1) f[i] += f[i ^ (1 << j)]; n   for (i = 1; i <= m; i++) { n     int t = 0; n     for (j = 0; j < (1 << m); j++) { n       g[j] = (long long)g[j] * f[j]; n       if (m - b[j] & 1) n         t -= g[j]; n       else n         t += g[j]; n     } n     if (t) return printf( %d n , n + i - 1), 0; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s, t; n   cin >> s >> t; n   int n = s.length(); n   int x = t.length(); n   vector<int> lft(x); n   vector<int> rgt(x); n   int l = 0; n   for (int i = 0; i < n && l < x; ++i) { n     if (s[i] == t[l]) { n       lft[l++] = i; n     } n   } n   l = x - 1; n   for (int i = n - 1; i >= 0 && l >= 0; --i) { n     if (s[i] == t[l]) { n       rgt[l--] = i; n     } n   } n   int mx = 0; n   mx = max(mx, lft[0]); n   mx = max(mx, rgt[0]); n   mx = max(mx, n - 1 - lft[x - 1]); n   mx = max(mx, n - 1 - rgt[x - 1]); n   for (int i = 1; i < x; ++i) mx = max(mx, rgt[i] - lft[i - 1] - 1); n   cout << mx; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, i; n   cin >> a >> b; n   int arr[a + 1]; n   for (i = 1; i <= a; i++) cin >> arr[i]; n   for (i = 1; i <= a; i++) { n     b -= (86400 - arr[i]); n     if (b <= 0) break; n   } n   cout << i << endl; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long gcd(long long a, long long b) { n   if (b == 0) n     return a; n   else n     return gcd(b, a % b); n } n inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } n signed main() { n   std::ios::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   string s; n   int n, a, b; n   cin >> n >> a >> b >> s; n   for (int i = 0; i <= n / a; i++) { n     if ((n - i * a) % b == 0) { n       cout << i + (n - i * a) / b << endl; n       for (int j = 0; j < i * a; j++) { n         cout << s[j]; n         if ((j + 1) % a == 0) cout << endl; n       } n       for (int j = i * a, k = 0; j < n; j++, k++) { n         cout << s[j]; n         if ((k + 1) % b == 0) cout << endl; n       } n       return 0; n     } n   } n   cout <<  -1 n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> primes; n int dp[101][1 << 18], n, a[111], man, ansd[111], loc[100]; n vector<int> fact[100]; n int rec(int i, int mask) { n   if (i == n) return 0; n   if (dp[i][mask] != 1000000000) return dp[i][mask]; n   int dif = abs(a[i] - 1); n   int ans = rec(i + 1, mask) + dif; n   for (int j = 2; j <= 60; j++) { n     int k; n     for (k = 0; k < fact[j].size(); k++) n       if (mask & (1 << fact[j][k])) break; n     if (k != (int)fact[j].size()) continue; n     int d = abs(a[i] - j); n     if (d <= dif) { n       int msk = mask; n       for (k = 0; k < fact[j].size(); k++) msk = msk | (1 << fact[j][k]); n       ans = min(ans, rec(i + 1, msk) + d); n     } n   } n   return dp[i][mask] = ans; n } n void reccons(int i, int mask, int m) { n   if (i == n) return; n   int dif = abs(a[i] - 1); n   if (rec(i + 1, mask) == m - dif) { n     ansd[i] = 1; n     reccons(i + 1, mask, m - dif); n     return; n   } n   for (int j = 2; j <= 60; j++) { n     int k; n     for (k = 0; k < fact[j].size(); k++) n       if (mask & (1 << fact[j][k])) break; n     if (k != (int)fact[j].size()) continue; n     int d = abs(a[i] - j); n     if (d > dif) continue; n     int msk = mask; n     for (k = 0; k < fact[j].size(); k++) msk = msk | (1 << fact[j][k]); n     if (rec(i + 1, msk) == m - d) { n       ansd[i] = j; n       reccons(i + 1, msk, m - d); n       return; n     } n   } n } n int main() { n   int i, j, t; n   for (i = 2; i <= 60; i++) { n     for (j = 2; j < i; j++) n       if (i % j == 0) break; n     if (j == i) primes.push_back(i); n   } n   for (i = 0; i < 17; i++) loc[primes[i]] = i; n   for (i = 2; i <= 60; i++) { n     for (j = 0; j < 17; j++) n       if (i % primes[j] == 0) fact[i].push_back(j); n   } n   scanf( %d , &n); n   for (i = 0; i < n; i++) scanf( %d , &a[i]); n   for (i = 0; i <= n; i++) n     for (j = 0; j < (1 << 17); j++) dp[i][j] = 1000000000; n   man = rec(0, 0); n   reccons(0, 0, man); n   for (i = 0; i < n; i++) printf( %d  , ansd[i]); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100 + 10; n string a[maxn], ans[maxn]; n int main() { n   int n; n   while (cin >> n) { n     int cnt[2][26]; n     memset(cnt, 0, sizeof cnt); n     for (int i = 0; i < n; ++i) cin >> a[i], ++cnt[0][a[i][0] -  A ]; n     multiset<string> Q; n     for (int i = 0; i < n; ++i) { n       string x; n       cin >> x; n       ++cnt[1][x[0] -  A ]; n       Q.insert(x); n     } n     sort(a, a + n); n     int cur = 0; n     for (int i = 0; i < n; ++i) { n       string& t = a[i]; n       int id = t[0] -  A ; n       for (multiset<string>::iterator it = Q.begin(); it != Q.end(); ++it) { n         const string& now = *it; n         int idx = now[0] -  A ; n         if (now[0] == t[0]) { n           ans[cur++] = t +     + now; n           --cnt[0][id]; n           --cnt[1][idx]; n           Q.erase(it); n           break; n         } else if (cnt[0][idx] < cnt[1][idx] && cnt[0][id] > cnt[1][id]) { n           ans[cur++] = t +     + now; n           --cnt[0][id]; n           --cnt[1][idx]; n           Q.erase(it); n           break; n         } n       } n     } n     sort(ans, ans + cur); n     cout << ans[0]; n     for (int i = 1; i < cur; ++i) { n       cout <<  ,   << ans[i]; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool check(int left, int right, int m) { n   left ^= right; n   if (left > m) n     return false; n   else n     return true; n } n void solve() { n   int n, m; n   cin >> n >> m; n   if (n > m) { n     cout << 0 <<   n ; n     return; n   } n   int ans = 0; n   for (int i = 0; i < 31; ++i) { n     if ((1 << i) & n) n       continue; n     else { n       ans ^= (1 << i); n       if ((ans ^ n) > m) break; n     } n   } n   for (int i = 30; i >= 0; --i) { n     if ((1 << i) & ans) { n       int num = ans ^ (1 << i); n       if ((num ^ n) > m) { n         ans ^= (1 << i); n       } n     } n   } n   cout << ans <<   n ; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int test; n   cin >> test; n   while (test--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_N = 100 * 1000 + 10; n const int inf = (int)1e9; n const double eps = 1e-6; n int main() { n   int r, g, b; n   cin >> r >> g >> b; n   int rr = (((r + 1) / 2) - 1) * 3 + 30; n   int gg = (((g + 1) / 2) - 1) * 3 + 31; n   int bb = (((b + 1) / 2) - 1) * 3 + 32; n   cout << max(rr, max(bb, gg)) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int SOURCE = 0, SINK = 1; n struct edge { n   int to, idx, cap; n }; n vector<vector<edge>> adj; n vector<int> lvl, ptr; n int totalflow; n int dfs(int n, int f) { n   if (n == SINK) { n     totalflow += f; n     return f; n   } n   if (lvl[n] == lvl[SINK]) return 0; n   while (ptr[n] < (int)adj[n].size()) { n     edge& e = adj[n][ptr[n]]; n     ptr[n]++; n     if (lvl[e.to] == lvl[n] + 1 && e.cap > 0) { n       int nf = dfs(e.to, min(f, e.cap)); n       if (nf) { n         e.cap -= nf; n         adj[e.to][e.idx].cap += nf; n         return nf; n       } n     } n   } n   return 0; n } n bool runMaxFlow() { n   lvl.assign(adj.size(), -1); n   ptr.assign(adj.size(), 0); n   lvl[SOURCE] = 0; n   queue<int> bfs; n   bfs.push(SOURCE); n   while (!bfs.empty()) { n     int t = bfs.front(); n     bfs.pop(); n     for (edge& e : adj[t]) { n       if (lvl[e.to] != -1 || e.cap <= 0) continue; n       lvl[e.to] = lvl[t] + 1; n       bfs.push(e.to); n     } n   } n   if (lvl[SINK] == -1) return false; n   while (dfs(SOURCE, 1 << 30)) { n   } n   return true; n } n void initMaxFlow(int nodes) { n   totalflow = 0; n   adj.clear(); n   adj.resize(nodes); n } n void addEdge(int a, int b, int w) { n   adj[a].push_back(edge{b, (int)adj[b].size(), w}); n   adj[b].push_back(edge{a, (int)adj[a].size() - 1, 0}); n } n const int dr[] = {0, 0, -1, 1}, dc[] = {-1, 1, 0, 0}; n int R, C, M, F, S, fw[22][22][22][22]; n char gd[23][23]; n struct scayger { n   int r, c, t, g; n } sg[22 * 22 * 2 + 1]; n bool try1(long long t, bool sgm) { n   initMaxFlow(M + F + R * C * 2 + 3); n   for (int r = 0; r < int(R); r++) n     for (int c = 0; c < int(C); c++) n       addEdge(M + F + 3 + (r * C + c) * 2, M + F + 3 + (r * C + c) * 2 + 1, 1); n   for (int i = 0; i < int(M); i++) addEdge(0, 3 + i, 1); n   for (int i = 0; i < int(F); i++) addEdge(M + 3 + i, 1, 1); n   if (sgm) n     addEdge(0, 2, 1); n   else n     addEdge(2, 1, 1); n   for (int i = 0; i < int(S); i++) { n     scayger& s = sg[i]; n     int g = s.g; n     if (g == 0) g = (sgm) ? 1 : 2; n     for (int r = 0; r < int(R); r++) n       for (int c = 0; c < int(C); c++) { n         long long d = fw[r][c][s.r][s.c]; n         if (d == -1) continue; n         if (t >= d * s.t) { n           if (g == 1) n             addEdge(2 + i, M + F + 3 + (r * C + c) * 2, 1); n           else n             addEdge(M + F + 3 + (r * C + c) * 2 + 1, 2 + i, 1); n         } n       } n   } n   while (runMaxFlow()) { n   } n   return totalflow == S / 2; n } n bool tryit(long long t) { return try1(t, true) || try1(t, false); } n int main() { n   scanf( %d%d%d%d , &R, &C, &M, &F); n   for (int r = 0; r < int(R); r++) scanf( %s , gd[r]); n   S = M + F + 1; n   for (int i = 0; i < int(S); i++) { n     scayger& s = sg[i]; n     scanf( %d%d%d , &s.r, &s.c, &s.t); n     s.r--; n     s.c--; n     if (i == 0) n       s.g = 0; n     else if (i <= M) n       s.g = 1; n     else n       s.g = 2; n   } n   memset(fw, -1, sizeof fw); n   for (int r = 0; r < int(R); r++) n     for (int c = 0; c < int(C); c++) n       if (gd[r][c] ==  . ) { n         for (int i = 0; i < int(4); i++) { n           int r2 = r + dr[i], c2 = c + dc[i]; n           if (r2 < 0 || r2 >= R || c2 < 0 || c2 >= C || gd[r2][c2] ==  # ) n             continue; n           fw[r][c][r2][c2] = fw[r2][c2][r][c] = 1; n         } n         fw[r][c][r][c] = 0; n       } n   for (int kr = 0; kr < int(R); kr++) n     for (int kc = 0; kc < int(C); kc++) n       if (gd[kr][kc] ==  . ) n         for (int ir = 0; ir < int(R); ir++) n           for (int ic = 0; ic < int(C); ic++) n             if (gd[ir][ic] ==  . ) n               for (int jr = 0; jr < int(R); jr++) n                 for (int jc = 0; jc < int(C); jc++) n                   if (gd[jr][jc] ==  . ) { n                     int &d1 = fw[ir][ic][kr][kc], &d2 = fw[kr][kc][jr][jc], n                         &d = fw[ir][ic][jr][jc]; n                     if (d1 == -1 || d2 == -1) continue; n                     if (d == -1 || d1 + d2 < d) d = d1 + d2; n                   } n   if (S % 2 != 0 || !tryit(LLONG_MAX)) { n     puts( -1 ); n     return 0; n   } n   long long t0 = -1, t1 = 1000000000LL * R * C; n   while (t1 - t0 > 1) { n     long long t = (t0 + t1) / 2; n     if (tryit(t)) n       t1 = t; n     else n       t0 = t; n   } n   printf( %lld n , t1); n } n


#include <bits/stdc++.h> n using namespace std; n class TwoSAT { n  private: n   const static int maxm = 100010 * 2; n   int S[maxm], c; n   vector<int> G[maxm]; n   bool DFS(int u) { n     if (vis[u ^ 1]) return false; n     if (vis[u]) return true; n     vis[u] = 1; n     S[c++] = u; n     for (auto &v : G[u]) n       if (!DFS(v)) return false; n     return true; n   } n  n  public: n   int N; n   bool vis[maxm]; n   void Clear() { n     for (int i = 2; i < (N + 1) * 2; ++i) G[i].clear(); n     memset(vis, 0, sizeof(bool) * (N + 1) * 2); n   } n   void AddClause(int x, int xv, int y, int yv) { n     x = x * 2 + xv; n     y = y * 2 + yv; n     G[x].push_back(y); n     G[y].push_back(x); n   } n   bool Solve() { n     for (int i = 2; i < (N + 1) * 2; i += 2) n       if (!vis[i] && !vis[i + 1]) { n         c = 0; n         if (!DFS(i)) { n           while (c > 0) vis[S[--c]] = 0; n           if (!DFS(i + 1)) return false; n         } n       } n     return true; n   } n }; n int n, m; n bool r[100010]; n int f[100010]; n TwoSAT ts; n int main() { n   ios::sync_with_stdio(false); n   int i, j, x, y; n   cin >> n >> m; n   ts.N = m + 1; n   for (i = 1; i <= n; ++i) cin >> r[i]; n   for (i = 1; i <= m; ++i) { n     cin >> x; n     for (j = 0; j < x; ++j) { n       cin >> y; n       if (f[y]) { n         if (r[y]) { n           ts.AddClause(f[y], 1, i, 1); n           ts.AddClause(f[y], 0, i, 0); n         } else { n           ts.AddClause(f[y], 1, i, 0); n           ts.AddClause(f[y], 0, i, 1); n         } n       } else n         f[y] = i; n     } n   } n   if (ts.Solve()) n     puts( YES ); n   else n     puts( NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double d, h, v, e; n int main() { n   ios_base::sync_with_stdio(false); n   cin >> d >> h >> v >> e; n   v = v / (acos(-1) * d * d / 4); n   double t = h / (v - e); n   if (t < 0 || fabs(v - e) < 1e-8) { n     printf( NO n ); n     return 0; n   } n   printf( YES n%.12lf n , t); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using namespace std; n inline int read() { n   char ch; n   bool sign = 0; n   int res = 0; n   while (!isdigit(ch = getchar())) n     if (ch ==  - ) sign = 1; n   while (isdigit(ch)) res = res * 10 + (ch ^ 48), ch = getchar(); n   if (sign) n     return -res; n   else n     return res; n } n mt19937 Rnd(chrono::system_clock::now().time_since_epoch().count()); n const int maxn = 200000 + 10; n const int inf = 1e9; n const int MAXM = 3; n const long long MOD[] = {469762049, 998244353, 1004535809, 1000000007}; n const int mod = 1811939329; n const int apsz = 26; n int a[maxn], n, cnt[maxn], Sqrt; n int mx; n inline int brute(int x) { n   vector<int> pos(2 * n + 2, -1); n   int now = maxn; n   int ans = 0; n   pos[n] = 0; n   for (auto i = (1); i <= (n); ++i) { n     if (now == maxn && a[i] != x && a[i] != mx) continue; n     if (now == maxn) now = 0; n     if (a[i] == x) { n       now++; n     } else if (a[i] == mx) n       now--; n     if (pos[now + n] >= 0) ans = max(ans, i - pos[now + n]); n     if (pos[now + n] == -1) pos[now + n] = i; n   } n   return ans; n } n inline int gao(int c) { n   memset(cnt, 0, sizeof(cnt)); n   int has = 0; n   int l, r, ans; n   for (ans = 0, r = l = 1; r <= n; ++r) { n     ++cnt[a[r]]; n     if (cnt[a[r]] == c) n       has++; n     else if (cnt[a[r]] == c + 1) n       has--; n     while (cnt[a[r]] > c) { n       --cnt[a[l]]; n       if (cnt[a[l]] == c) has++; n       if (cnt[a[l]] == c - 1) has--; n       l++; n     } n     assert(has >= 0 && r >= l); n     if (has > 1) ans = max(ans, r - l + 1); n   } n   return ans; n } n inline int solve() { n   int mxcnt = 0; n   for (auto i = (1); i <= (n); ++i) { n     if (++cnt[a[i]] > mxcnt) mxcnt = cnt[a[i]], mx = a[i]; n   } n   for (auto i = (1); i <= (n); ++i) { n     if (mx != a[i] && cnt[a[i]] == mxcnt) return n; n   } n   int ans(0); n   Sqrt = sqrt(n + 3); n   for (auto i = (1); i <= (n); ++i) { n     if (cnt[a[i]] >= Sqrt && a[i] != mx) { n       ans = max(brute(a[i]), ans); n       cnt[a[i]] = 0; n     } n   } n   for (auto c = (1); c <= (Sqrt); ++c) { n     ans = max(ans, gao(c)); n   } n   return ans; n } n int main(void) { n   scanf( %d , &(n)); n   for (auto i = (1); i <= (n); ++i) scanf( %d , &(a[i])); n   printf( %d n , solve()); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<long long, long long> zipx; n long long unzipx[2000]; n long long inf = 1LL << 50; n long long compressx(vector<long long>& x) { n   sort(x.begin(), x.end()); n   x.erase(unique(x.begin(), x.end()), x.end()); n   for (long long i = 0; i < x.size(); i++) { n     zipx[x[i]] = i; n     unzipx[i] = x[i]; n   } n   return x.size(); n } n map<long long, long long> zipy; n long long unzipy[2000]; n long long compressy(vector<long long>& x) { n   sort(x.begin(), x.end()); n   x.erase(unique(x.begin(), x.end()), x.end()); n   for (long long i = 0; i < x.size(); i++) { n     zipy[x[i]] = i; n     unzipy[i] = x[i]; n   } n   return x.size(); n } n template <typename T> n class Dinic { n  private: n   struct edge { n     long long to; n     T cap; n     long long rev; n   }; n   long long V; n   vector<vector<edge> > G; n   vector<long long> level, iter; n   void bfs(long long s) { n     fill(level.begin(), level.end(), -1); n     queue<long long> que; n     level[s] = 0; n     que.push(s); n     while (!que.empty()) { n       long long v = que.front(); n       que.pop(); n       for (auto& e : G[v]) { n         if (e.cap > 0 && level[e.to] < 0) { n           level[e.to] = level[v] + 1; n           que.push(e.to); n         } n       } n     } n   } n   T dfs(long long v, long long t, T f) { n     if (v == t) { n       return f; n     } n     for (long long& i = iter[v]; i < (long long)G[v].size(); i++) { n       edge& e = G[v][i]; n       if (e.cap > 0 && level[v] < level[e.to]) { n         T d = dfs(e.to, t, min(f, e.cap)); n         if (d > 0) { n           e.cap -= d; n           G[e.to][e.rev].cap += d; n           return d; n         } n       } n     } n     return 0; n   } n  n  public: n   Dinic(long long node_size) : V(node_size), G(V), level(V), iter(V) {} n   void add_edge(long long from, long long to, T cap) { n     G[from].push_back((edge){to, cap, (long long)G[to].size()}); n     G[to].push_back((edge){from, (T)0, (long long)G[from].size() - 1}); n   } n   T solve(long long s, long long t) { n     T flow = 0; n     for (;;) { n       bfs(s); n       if (level[t] < 0) return flow; n       fill(iter.begin(), iter.end(), 0); n       T f; n       while ((f = dfs(s, t, numeric_limits<T>::max())) > 0) { n         flow += f; n       } n     } n   } n }; n signed main() { n   long long n, m; n   cin >> n >> m; n   vector<pair<long long, long long> > a; n   vector<pair<long long, long long> > b; n   vector<long long> x; n   vector<long long> y; n   for (long long i = 0; i < (long long)(m); ++i) { n     long long p, q, r, s; n     cin >> p >> q >> r >> s; n     a.push_back(make_pair(p, q)); n     b.push_back(make_pair(r + 1, s + 1)); n     x.push_back(p); n     y.push_back(q); n     x.push_back(r + 1); n     y.push_back(s + 1); n   } n   long long s = compressx(x); n   long long t = compressy(y); n   vector<vector<bool> > f(s + 10, vector<bool>(t + 10)); n   for (long long i = 0; i < (long long)(m); ++i) { n     for (long long j = zipx[a[i].first]; j < zipx[b[i].first]; j++) { n       for (long long k = zipy[a[i].second]; k < zipy[b[i].second]; k++) { n         f[j][k] = 1; n       } n     } n   } n   Dinic<long long> dn(s + t + 2); n   long long S = s + t; n   long long T = s + t + 1; n   for (long long i = 0; i < (long long)(s - 1); ++i) { n     dn.add_edge(S, i, unzipx[i + 1] - unzipx[i]); n   } n   for (long long j = 0; j < (long long)(t - 1); ++j) { n     dn.add_edge(s + j, T, unzipy[j + 1] - unzipy[j]); n   } n   for (long long i = 0; i < (long long)(s); ++i) { n     for (long long j = 0; j < (long long)(t); ++j) { n       if (f[i][j]) { n         dn.add_edge(i, s + j, inf); n       } n     } n   } n   cout << dn.solve(S, T) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n unordered_map<int, int> mapa; n vector<int> tab; n int main() { n   ios_base::sync_with_stdio(false); n   int n, k, a; n   cin >> n >> k; n   int aktIlosc = n; n   for (int i = 0; i < n; i++) { n     cin >> a; n     if (mapa[a] == 0) tab.push_back(a); n     mapa[a]++; n   } n   sort(tab.begin(), tab.end()); n   for (int i = 0; i + 1 < tab.size(); i++) { n     if (tab[i] < tab[i + 1] && tab[i] + k >= tab[i + 1]) n       aktIlosc -= mapa[tab[i]]; n   } n   cout << aktIlosc; n } n


#include <bits/stdc++.h> n using namespace std; n using namespace chrono; n void _print(long long t) { cerr << t; } n void _print(int t) { cerr << t; } n void _print(string t) { cerr << t; } n void _print(char t) { cerr << t; } n void _print(long double t) { cerr << t; } n void _print(double t) { cerr << t; } n void _print(unsigned long long t) { cerr << t; } n template <class T, class V> n void _print(pair<T, V> p); n template <class T> n void _print(vector<T> v); n template <class T> n void _print(set<T> v); n template <class T, class V> n void _print(map<T, V> v); n template <class T> n void _print(multiset<T> v); n template <class T, class V> n void _print(pair<T, V> p) { n   cerr <<  { ; n   _print(p.first); n   cerr <<  , ; n   _print(p.second); n   cerr <<  } ; n } n template <class T> n void _print(vector<T> v) { n   cerr <<  [  ; n   for (T i : v) { n     _print(i); n     cerr <<    ; n   } n   cerr <<  ] ; n } n template <class T> n void _print(set<T> v) { n   cerr <<  [  ; n   for (T i : v) { n     _print(i); n     cerr <<    ; n   } n   cerr <<  ] ; n } n template <class T> n void _print(multiset<T> v) { n   cerr <<  [  ; n   for (T i : v) { n     _print(i); n     cerr <<    ; n   } n   cerr <<  ] ; n } n template <class T, class V> n void _print(map<T, V> v) { n   cerr <<  [  ; n   for (auto i : v) { n     _print(i); n     cerr <<    ; n   } n   cerr <<  ] ; n } n long long gcd(long long a, long long b) { n   if (b > a) { n     return gcd(b, a); n   } n   if (b == 0) { n     return a; n   } n   return gcd(b, a % b); n } n long long expo(long long a, long long b, long long mod) { n   long long res = 1; n   while (b > 0) { n     if (b & 1) res = (res * a) % mod; n     a = (a * a) % mod; n     b = b >> 1; n   } n   return res; n } n void extendgcd(long long a, long long b, long long *v) { n   if (b == 0) { n     v[0] = 1; n     v[1] = 0; n     v[2] = a; n     return; n   } n   extendgcd(b, a % b, v); n   long long x = v[1]; n   v[1] = v[0] - v[1] * (a / b); n   v[0] = x; n   return; n } n long long mminv(long long a, long long b) { n   long long arr[3]; n   extendgcd(a, b, arr); n   return arr[0]; n } n long long mminvprime(long long a, long long b) { return expo(a, b - 2, b); } n bool revsort(long long a, long long b) { return a > b; } n void swap(int &x, int &y) { n   int temp = x; n   x = y; n   y = temp; n } n long long combination(long long n, long long r, long long m, long long *fact, n                       long long *ifact) { n   long long val1 = fact[n]; n   long long val2 = ifact[n - r]; n   long long val3 = ifact[r]; n   return (((val1 * val2) % m) * val3) % m; n } n void google(int t) { cout <<  Case #  << t <<  :  ; } n vector<long long> sieve(int n) { n   int *arr = new int[n + 1](); n   vector<long long> vect; n   for (int i = 2; i <= n; i++) n     if (arr[i] == 0) { n       vect.push_back(i); n       for (int j = 2 * i; j <= n; j += i) arr[j] = 1; n     } n   return vect; n } n long long mod_add(long long a, long long b, long long m) { n   a = a % m; n   b = b % m; n   return (((a + b) % m) + m) % m; n } n long long mod_mul(long long a, long long b, long long m) { n   a = a % m; n   b = b % m; n   return (((a * b) % m) + m) % m; n } n long long mod_sub(long long a, long long b, long long m) { n   a = a % m; n   b = b % m; n   return (((a - b) % m) + m) % m; n } n long long mod_div(long long a, long long b, long long m) { n   a = a % m; n   b = b % m; n   return (mod_mul(a, mminvprime(b, m), m) + m) % m; n } n long long phin(long long n) { n   long long number = n; n   if (n % 2 == 0) { n     number /= 2; n     while (n % 2 == 0) n /= 2; n   } n   for (long long i = 3; i <= sqrt(n); i += 2) { n     if (n % i == 0) { n       while (n % i == 0) n /= i; n       number = (number / i * (i - 1)); n     } n   } n   if (n > 1) number = (number / n * (n - 1)); n   return number; n } n int r, c, k; n string arr[100]; n vector<char> ans; n vector<vector<char>> ans1; n void put_values(pair<int, int> &p, int x, int i) { n   int count = 0; n   while (count < x) { n     ans1[p.first][p.second] = ans[i]; n     if (arr[p.first][p.second] ==  R ) { n       count++; n     } n     if (p.first % 2 == 0) { n       if (p.second == c - 1) { n         p.first++; n       } else { n         p.second++; n       } n     } else { n       if (p.second == 0) { n         p.first++; n       } else { n         p.second--; n       } n     } n   } n } n void solve() { n   int t; n   cin >> t; n   for (int i = 0; i < 26; i++) { n     ans.push_back( a  + i); n     ans.push_back( A  + i); n   } n   for (int i = 0; i < 10; i++) { n     ans.push_back( 0  + i); n   }; n   while (t--) { n     cin >> r >> c >> k; n     vector<vector<char>> ans2(r, vector<char>(c,  ? )); n     ans1 = ans2; n     for (int i = 0; i < r; i++) cin >> arr[i]; n     int count = 0; n     for (int i = 0; i < r; i++) { n       for (int j = 0; j < c; j++) { n         count += arr[i][j] ==  R ; n       } n     } n     int value = count / k; n     int x = count % k; n     pair<int, int> coordinate = {0, 0}; n     for (int i = 0; i < k; i++) { n       if (i < x) { n         put_values(coordinate, value + 1, i); n       } else { n         put_values(coordinate, value, i); n       } n     } n     for (int i = 0; i < r; i++) { n       for (int j = 0; j < c; j++) { n         if (ans1[i][j] ==  ? ) { n           cout << ans[k - 1]; n         } else { n           cout << ans1[i][j]; n         } n       } n       cout <<   n ; n     } n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   auto start1 = high_resolution_clock::now(); n   solve(); n   auto stop1 = high_resolution_clock::now(); n   auto duration = duration_cast<microseconds>(stop1 - start1); n } n


#include <bits/stdc++.h> n using namespace std; n char dif(char c1) { n   if (c1 ==  a ) return  b ; n   return  a ; n } n char dif2(char c1, char c2) { n   if (c1 !=  a  && c2 !=  a ) return  a ; n   if (c1 !=  b  && c2 !=  b ) return  b ; n   return  c ; n } n int main() { n   int n, t; n   cin >> n >> t; n   string s1, s2; n   cin >> s1 >> s2; n   vector<int> com; n   vector<int> nocom; n   for (long long i = 0; i < (n); i++) n     if (s1[i] == s2[i]) n       com.push_back(i); n     else n       nocom.push_back(i); n   int comsz = com.size(); n   int nocomsz = nocom.size(); n   if (!(2 * t >= nocomsz)) { n     cout << -1 << endl; n     return 0; n   } n   string s3 = s1; n   if (nocomsz & 1) { n     int ind = nocom[nocomsz - 1]; n     s3[ind] = dif2(s1[ind], s2[ind]); n     t--; n   } n   for (int i = 0; (i + 1 < nocomsz) && t > 0; i += 2) { n     int ind1 = nocom[i]; n     s3[ind1] = s2[ind1]; n     int ind2 = nocom[i + 1]; n     s3[ind2] = s1[ind2]; n     t--; n   } n   for (int i = 0; i < comsz && t > 0; i++) { n     int ind = com[i]; n     s3[ind] = dif(s1[ind]); n     t--; n   } n   for (int i = 0; (i + 1 < nocomsz) && t > 0; i += 2) { n     int ind1 = nocom[i]; n     int ind2 = nocom[i + 1]; n     s3[ind1] = dif2(s1[ind1], s2[ind1]); n     s3[ind2] = dif2(s1[ind2], s2[ind2]); n     t--; n   } n   cout << s3 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e4 + 10; n const long long mod = 998244353; n int n, m, k, x; n bool a[55][maxn]; n int main() { n   scanf( %d%d , &m, &n); n   for (int i = 1; i <= (m); i++) { n     scanf( %d , &k); n     for (int j = 1; j <= (k); j++) { n       scanf( %d , &x); n       a[i][x] = 1; n     } n   } n   for (int i = 1; i <= (m); i++) n     for (int j = 1; j <= (m); j++) { n       bool flag = 0; n       for (int l = 1; l <= n && !flag; l++) n         if (a[i][l] && a[j][l]) flag = 1; n       if (!flag) { n         printf( impossible n ); n         return 0; n       } n     } n   printf( possible n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int sum(long int c) { n   int somme = 0; n   while (c) { n     somme = somme + c % 10; n     c = c / 10; n   } n   return (somme); n } n int main() { n   long int k, c = 19; n   cin >> k; n   while (k) { n     if (sum(c) == 10) k--; n     c += 9; n   } n   cout << c - 9; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const int M = 100005; n const int MOD = 1e9 + 7; n long long c[100005], x, k, tot = 0; n vector<long long> son[100005]; n void dfs(int n, int m) { n   if (tot >= 100000) return; n   if (n == 0 || m == 0) { n     printf( %I64d  , c[n]); n     ++tot; n     return; n   } n   for (int v : son[n]) dfs(v, m - 1); n } n int main() { n   cin >> x >> k; n   if (k == 0) { n     printf( %I64d n , x); n     return 0; n   } n   int top = 0; n   for (int i = 1; i * 1ll * i < x; ++i) { n     if (x % i == 0) { n       c[top++] = i; n       c[top++] = x / i; n     } n   } n   if (sqrt(x) * sqrt(x) == x) c[top++] = sqrt(x); n   sort(c, c + top); n   for (int i = 0; i < top; ++i) n     for (int j = 0; j <= i; ++j) n       if (c[i] % c[j] == 0) son[i].push_back(j); n   dfs(top - 1, k); n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:16777216 ) n using namespace std; n const double PI = acos(-1.0); n long long gcd(long long a, long long b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n pair<long long, int> all[2000111]; n long long cntA, cntB; n int cnt; n int main() { n   long long a, b; n   cin >> a >> b; n   long long x = a * b / gcd(a, b); n   for (int i = (1), _b = (x / a - 1); i <= _b; i++) n     all[cnt++] = make_pair(a * i, 0); n   for (int i = (1), _b = (x / b - 1); i <= _b; i++) n     all[cnt++] = make_pair(b * i, 1); n   sort(all, all + cnt); n   long long last = 0; n   for (int i = 0, _a = (cnt); i < _a; i++) { n     if (all[i].second == 0) n       cntA += all[i].first - last; n     else n       cntB += all[i].first - last; n     last = all[i].first; n   } n   if (a > b) n     cntA += x - last; n   else n     cntB += x - last; n   if (cntA > cntB) n     puts( Dasha ); n   else if (cntA < cntB) n     puts( Masha ); n   else n     puts( Equal ); n   return 0; n } n


#include <bits/stdc++.h> n long long R = 7 + 1e9, NUMTESTCASE; n const int NN = 10 + 1e5; n const double pi = acos(-1.0); n int di[4] = {1, 0, -1, 0}, dj[4] = {0, 1, 0, -1}, n     DI[8] = {1, 1, 0, -1, -1, -1, 0, 1}, DJ[8] = {0, 1, 1, 1, 0, -1, -1, -1}; n using namespace std; n int main() { n   int x, t, n, mx, k; n   cin >> n >> mx >> k; n   vector<int> a[n]; n   t = 0; n   for (int i = (0); i <= (n - 1); ++i) { n     scanf( %d , &x); n     if (x <= mx) n       a[t].push_back(1); n     else n       t++; n   } n   int ans = 0; n   for (int i = (0); i <= (n - 1); ++i) ans += max(0, (int)a[i].size() - k + 1); n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int stop, n, k, x, i, ind, j, y[1000001], mini[2001][2001]; n string s[2001]; n struct name { n   int a[26]; n   int cnt; n } trie[1000001]; n vector<int> d[1000001]; n int m; n void add(int v, int pos) { n   if (pos > s[i].length()) return; n   if (trie[v].a[s[i][pos] -  a ]) n     add(trie[v].a[s[i][pos] -  a ], pos + 1); n   else { n     m++; n     trie[v].a[s[i][pos] -  a ] = m; n     add(m, pos + 1); n   }; n   trie[v].cnt++; n } n void dp(int v) { n   for (int j = 0; j < 26; j++) n     if (trie[v].a[j]) dp(trie[v].a[j]); n   d[v].resize(trie[v].cnt + 1); n   for (int j = 0; j < 26; j++) { n     int p = trie[v].a[j]; n     if (p == 0) continue; n     int s = d[p].size() - 1; n     for (int k1 = d[v].size() - s - 1; k1 >= 0; k1--) n       for (int i = 1; i <= s; i++) { n         d[v][k1 + i] = max(d[v][k1 + i], d[v][k1] + d[p][i] + i * (i - 1) / 2); n       } n   } n } n int main() { n   cin >> n >> k; n   for (i = 1; i <= n; i++) { n     cin >> s[i]; n     add(0, 0); n   }; n   dp(0); n   cout << d[0][k]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   vector<int> vet, mat; n   int n, x; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> x; n     if (x != 0) vet.push_back(x); n   } n   for (int i = 0; i < n; i++) { n     cin >> x; n     if (x != 0) mat.push_back(x); n   } n   int aux = 0; n   if (n <= 3) { n     cout <<  YES ; n   } else { n     int r; n     for (int i = 0; i < n - 1; i++) { n       if (mat[i] == vet[0]) { n         r = i; n         break; n       } n     } n     for (int i = 0; i < n - 1; i++) { n       int l = r + i; n       if (l >= n - 1) l -= (n - 1); n       if (vet[i] != mat[l]) { n         aux = 1; n         break; n       } n     } n     if (aux) { n       cout <<  NO ; n     } else n       cout <<  YES ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool comp(int a, int b) { return a < b; } n int main() { n   stack<int> s; n   int N, t; n   cin >> N; n   while (N--) { n     cin >> t; n     if (s.empty()) { n       s.push(t); n     } else { n       if (((s.top()) & 1) == (t & 1)) n         s.pop(); n       else n         s.push(t); n     } n   } n   if (s.size() < 2) n     cout <<  YES  << endl; n   else n     cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using vi = vector<int>; n using vvi = vector<vi>; n using vll = vector<ll>; n using vvll = vector<vll>; n using vb = vector<bool>; n using vd = vector<double>; n using vs = vector<string>; n using vpii = vector<pair<int, int>>; n using vpll = vector<pair<ll, ll>>; n using pii = pair<int, int>; n using pll = pair<ll, ll>; n int dx[8] = {0, 1, 1, 1, 0, -1, -1, -1}; n int dy[8] = {1, 1, 0, -1, -1, -1, 0, 1}; n void solve() { n   vi a(6); n   for (ll i = 0; i < 6; i++) cin >> a[i]; n   ; n   ll ans = ((a[0] + a[1] + a[2]) * (a[0] + a[1] + a[2]) - a[0] * a[0] - n             a[2] * a[2] - a[4] * a[4]); n   cout << ans <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ll t = 1; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n int n, h; n char name[10]; n long double p[70], cont[70]; n int main() { n   scanf( %s%d%d , name, &n, &h); n   if (name[0] ==  B ) { n     printf( %d n , n); n     return 0; n   } n   long double ans = n; n   p[0] = cont[0] = 1.0; n   for (int i = 1; i <= h; i++) { n     p[i] = p[i - 1] / 2.0; n     cont[i] = cont[i - 1] * 2.0; n   } n   for (int i = 1; i <= h; i++) n     for (int j = 2; j <= n; j++) n       ans += p[i] * p[i] * pow(1.0 - p[i], j - 2) * n              (cont[i] - cont[i - 1] * ((j - 2) / (cont[i] - 1.0) + 1.0)) * n              (n - j + 1); n   printf( %.10f n , (double)ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, c1, c2, v, q; n long long xs, xe, ye, ys; n long long pos1[100005]; n long long pos2[100005]; n long long solve(int pos, int v) { n   long long ans = abs(pos - xe) + abs(pos - xs); n   long long y = abs(ye - ys); n   ans += (y - 1) / v + 1; n   return ans; n } n int main() { n   cin >> n >> m >> c1 >> c2 >> v; n   for (int i = 0; i < c1; i++) cin >> pos1[i]; n   for (int i = 0; i < c2; i++) cin >> pos2[i]; n   cin >> q; n   while (q--) { n     int id; n     cin >> ys >> xs >> ye >> xe; n     long long ans = 999999999999; n     if (ys == ye) n       ans = abs(xe - xs); n     else { n       if (c1) { n         id = upper_bound(pos1, pos1 + c1, xs) - pos1; n         if (id != c1) ans = min(ans, solve(pos1[id], 1)); n         if (id - 1 >= 0) ans = min(ans, solve(pos1[id - 1], 1)); n         id = upper_bound(pos1, pos1 + c1, xe) - pos1; n         if (id != c1) ans = min(ans, solve(pos1[id], 1)); n         if (id - 1 >= 0) ans = min(ans, solve(pos1[id - 1], 1)); n       } n       if (c2) { n         id = upper_bound(pos2, pos2 + c2, xs) - pos2; n         if (id != c2) ans = min(ans, solve(pos2[id], v)); n         if (id - 1 >= 0) ans = min(ans, solve(pos2[id - 1], v)); n         id = upper_bound(pos2, pos2 + c2, xe) - pos2; n         if (id != c2) ans = min(ans, solve(pos2[id], v)); n         if (id - 1 >= 0) ans = min(ans, solve(pos2[id - 1], v)); n       } n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 1; n int n, m, q, p[N], dis[N], far, cur, sz[N]; n vector<int> g[N]; n unordered_map<int, double> dp[N]; n vector<pair<int, long long>> v[N]; n int get(int u) { return p[u] == u ? u : p[u] = get(p[u]); } n void dfs(int u, int p, int depth) { n   dis[u] = max(dis[u], depth); n   if (dis[u] > cur) { n     cur = dis[u]; n     far = u; n   } n   for (auto i : g[u]) n     if (i != p) dfs(i, u, depth + 1); n } n double calc(int a, int b) { n   if (dp[a].find(b) != dp[a].end()) return dp[a][b]; n   double ans = 0; n   int mx = max(v[a].back().first, v[b].back().first); n   for (int i = v[a].size() - 1, j = 0; i + 1; i--) { n     while (j < v[b].size() && v[a][i].first + v[b][j].first + 1 <= mx) j++; n     ans += mx * 1LL * j; n     if (j < v[b].size()) { n       ans += v[b].back().second + v[b].size() - j + n              v[a][i].first * 1LL * (v[b].size() - j); n       if (j) ans -= v[b][j - 1].second; n     } n   } n   ans /= v[a].size() * 1LL * v[b].size(); n   return dp[a][b] = dp[b][a] = ans; n } n int main() { n   scanf( %d%d%d , &n, &m, &q); n   int z = sqrt(n); n   for (int i = 0; i < n; i++) p[i] = i, sz[i] = 1; n   for (int i = 0, a, b; i < m; i++) { n     scanf( %d%d , &a, &b); n     --a; n     --b; n     sz[get(b)] += sz[get(a)]; n     p[get(a)] = get(b); n     g[a].push_back(b); n     g[b].push_back(a); n   } n   for (int i = 0; i < n; i++) n     if (p[i] == i) { n       cur = -1e9; n       dfs(i, -1, 0); n       cur = -1e9; n       dfs(far, -1, 0); n       cur = -1e9; n       dfs(far, -1, 0); n     } n   for (int i = 0; i < n; i++) v[get(i)].push_back({dis[i], dis[i]}); n   for (int i = 0; i < n; i++) sort(v[i].begin(), v[i].end()); n   for (int i = 0; i < n; i++) n     if (i == p[i]) n       for (int j = 1; j < v[i].size(); j++) n         v[i][j].second += v[i][j - 1].second; n   for (int i = 0, a, b; i < q; i++) { n     scanf( %d%d , &a, &b); n     --a; n     --b; n     a = get(a); n     b = get(b); n     if (a == b) { n       puts( -1 ); n       continue; n     } n     if (sz[a] > sz[b]) swap(a, b); n     printf( %.10lf n , calc(a, b)); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, k, arr[105][105]; n int check(int c[105], int ctC, int secRow) { n   int ans = INT_MAX; n   if (ctC > 10) return ans; n   for (int i = 0; i < (1 << ctC); i++) { n     int ct = 0; n     for (int r = 2; r <= n; r++) { n       if (r != secRow) { n         int same = 0; n         for (int j = 1; j <= m; j++) { n           int row1El = n               c[j] >= 0 && (i & (1 << c[j])) ? 1 - arr[1][j] : arr[1][j]; n           if (row1El == arr[r][j]) same++; n         } n         ct += min(same, m - same); n       } n     } n     ans = min(ctC + ct, ans); n   } n   return ans; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n >> m >> k; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       cin >> arr[i][j]; n     } n   } n   if (n == 1 || m == 1) { n     cout << 0 << endl; n     return 0; n   } n   int ans = INT_MAX; n   for (int secRow = 2; secRow <= min(15, n); secRow++) { n     int ctA = 0, ctB = 0; n     int a[105], b[105]; n     memset(a, -1, sizeof(a)); n     memset(b, -1, sizeof(b)); n     for (int j = 1; j <= m; j++) { n       if ((arr[1][j] ^ arr[secRow][j]) == 1) { n         a[j] = ctA; n         ctA++; n       } else { n         b[j] = ctB; n         ctB++; n       } n     } n     ans = min(check(a, ctA, secRow), check(b, ctB, secRow)); n   } n   cout << (ans > k ? -1 : ans) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #define ll long long n #define f(i,n) for(int i=0;i<n;i++) n #define endl (  n ) n #define pb push_back n #define mp make_pair n #define pl pair<ll, ll> n #define vl vector<ll> n #define fastIO ios_base::sync_with_stdio(false);cin.tie(NULL); n  n #define M 1000000007 n  const int N=2e5+2; n  ll gcd(ll a, ll b) {if (b > a) {return gcd(b, a);} if (b == 0) {return a;} return gcd(b, a % b);} n ll expo(ll a, ll b, ll mod) {ll res = 1; while (b > 0) {if (b & 1)res = (res * a) % mod; a = (a * a) % mod; b = b >> 1;} return res;} n ll mminvprime(ll a, ll b) {return expo(a, b - 2, b);} n vector<ll> sieve(int n) {int*arr = new int[n + 1](); vector<ll> vect; for (int i = 2; i <= n; i++)if (arr[i] == 0) {vect.push_back(i); for (int j = 2 * i; j <= n; j += i)arr[j] = 1;} return vect;} n ll mod_add(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a + b) % m) + m) % m;} n ll mod_mul(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a * b) % m) + m) % m;} n ll mod_sub(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a - b) % m) + m) % m;} n ll mod_div(ll a, ll b, ll m) {a = a % m; b = b % m; return (mod_mul(a, mminvprime(b, m), m) + m) % m;}  //only for prime m n  n int fact[N]; n  n         void precalc(){ n             fact[0]=1; n             for(int i=1;i<N;i++){ n                 fact[i]=mod_mul(fact[i-1],i,M); n             } n         } n  long long mod(long long x){ n             return ((x%M + M)%M); n         } n         ll inv(ll x){ n             return expo(x,M-2,M); n         } n  n         ll divide(ll a, ll b){ n             return mod_mul(a,inv(b),M); n         } n  n         ll nCr(ll n, ll r){ n             return divide(fact[n],mod_mul(fact[r],fact[n-r],M)); n         } n  n   n int main() n { n     fastIO n   ll t; n   cin>>t; n   while(t--){ n       ll a,b; n       cin>>a>>b; n       ll d=abs(a-b); n       ll e=max(a,b); n       if(a==b){ n           cout<<0<<   <<0<<endl; n           continue; n       } n       n       ll k=e%d; n       ll v; n       if(d-k>=0 and d-k<k) v=d-k; n       else  v=k; n  n       cout<<d<<   <<v<<endl; n   } n  n  n  n  n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5e5 + 7; n const int M = 60; n const int inf = 1e9 + 7; n const long long base = 1e18; n const double pi = acos(-1); n const double ep = 1e-18; n int n; n int a[N]; n void solve() { n   cin >> n; n   for (int i = 0; i < n; i++) scanf( %d , a + i); n   int last = 0; n   while (last + 1 < n && a[last + 1] == a[0]) last++; n   int i = last + 1; n   int ans = 0; n   while (i < n) { n     int j = i; n     while (j < n && a[j] == a[i]) j++; n     if (i != n - 1 && j - i == 1) { n       i = j; n       continue; n     } else { n       ans = max(ans, (i - last) / 2); n       for (int u = last + 1, v = i - 1; u <= v; u++, v--) { n         a[u] = a[last]; n         a[v] = a[i]; n       } n       i = j; n       last = i - 1; n     } n   } n   cout << ans << endl; n   for (int i = 0; i < n; i++) printf( %d  , a[i]); n } n int main() { solve(); } n


#include <bits/stdc++.h> n int main() { n   long long a, b, c; n   scanf( %lld %lld , &a, &b); n   c = (b > (a + 1) / 2) ? 2 * (b - (a + 1) / 2) : 2 * b - 1; n   printf( %lld , c); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long x; n int n, d; n struct cmp { n   bool operator()(pair<int, int> x, pair<int, int> y) { n     return x.first > y.first; n   } n }; n set<pair<int, int>, cmp> que; n set<pair<int, int>, cmp>::iterator it; n vector<int> num; n int a[100005], c[100005]; n bool b[100005]; n long long next() { n   x = (x * 37 + 10007) % 1000000007; n   return x; n } n void initAB() { n   for (int i = 0; i < n; i++) a[i] = i + 1; n   for (int i = 0; i < n; i++) swap(a[i], a[next() % (i + 1)]); n   for (int i = 0; i < n; i++) { n     if (i < d) n       b[i] = 1; n     else n       b[i] = 0; n   } n   for (int i = 0; i < n; i++) swap(b[i], b[next() % (i + 1)]); n } n int main() { n   scanf( %d%d%I64d , &n, &d, &x); n   initAB(); n   if (d < 1000) { n     for (int i = 0; i < n; i++) { n       if (b[i]) num.push_back(i); n       for (int j = 0; j < num.size(); j++) c[i] = max(c[i], a[i - num[j]]); n     } n   } else { n     for (int i = 0; i < n; i++) { n       que.insert(make_pair(a[i], i)); n       for (it = que.begin(); it != que.end(); it++) { n         if (b[i - it->second]) { n           c[i] = it->first; n           break; n         } n       } n     } n   } n   for (int i = 0; i < n; i++) cout << c[i] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string yes =  YES , no =  NO ; n void solve() { n   long long n, k; n   scanf( %lli%lli , &n, &k); n   long long qwe = n - k + 1; n   long long rty = n - 2 * k + 2; n   if (qwe > 0 and qwe % 2 == 1) { n     cout << yes << endl; n     for (int i = 0; i < k - 1; i++) { n       printf( 1  ); n     } n     printf( %lli n , qwe); n     return; n   } n   if (rty > 0 && rty % 2 == 0) { n     cout << yes << endl; n     for (int i = 0; i < k - 1; i++) { n       printf( 2  ); n     } n     printf( %lli n , rty); n     return; n   } n   cout << no << endl; n } n int main() { n   int t; n   scanf( %d , &t); n   while (t--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 400; n int n, m, wynik, A[N], B[N]; n map<double, bitset<120> > mapa; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> m; n   for (int i = 1; i <= n; i++) cin >> A[i]; n   for (int i = 1; i <= m; i++) cin >> B[i]; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       double pom = (A[i] + B[j]) / 2.0; n       mapa[pom][i - 1] = mapa[pom][n + j - 1] = true; n     } n   } n   for (auto u : mapa) n     for (auto v : mapa) wynik = max(wynik, (int)(u.second | v.second).count()); n   cout << wynik << endl; n   getchar(); n   getchar(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1000000007; n const long double pi = acos(-1); n const long long maxn = 200001; n template <class T> n ostream &operator<<(ostream &out, vector<T> &A) { n   for (auto x : A) out << x <<    ; n   return out; n } n template <class T1, class T2> n T1 powr(T1 a, T2 b) { n   T1 res = 1; n   for (long long i = 1; i < b + 1; i++) res = res * a; n   return res; n } n int32_t main() { n   long long Q; n   cin >> Q; n   while (Q--) { n     long long n, pos; n     cin >> n; n     vector<long long> A(n); n     for (long long i = 0; i < n; i++) { n       cin >> A[i]; n       if (A[i] == 1) pos = i; n     } n     bool okl = true, okr = true; n     for (long long j = 1; j < n; j++) { n       okl &= (A[(pos - j + n) % n] == j + 1); n       okr &= (A[(pos + j + n) % n] == j + 1); n     } n     puts(okl or okr ?  YES  :  NO ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int mp[2123][2123]; n int prehb[2123][2123]; n int prehs[2123][2123]; n int prelb[2123][2123]; n int prels[2123][2123]; n int main() { n   ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   int n, m; n   cin >> n >> m; n   for (int i = 1; i <= n; i++) { n     map<int, int> lis; n     for (int j = 1; j <= m; j++) { n       cin >> mp[i][j]; n       lis[mp[i][j]] = 1; n     } n     int cnt = 0; n     for (auto &j : lis) { n       j.second = ++cnt; n     } n     int len = lis.size(); n     for (int j = 1; j <= m; j++) { n       prehb[i][j] = len - lis[mp[i][j]]; n       prehs[i][j] = lis[mp[i][j]] - 1; n     } n   } n   for (int i = 1; i <= m; i++) { n     map<int, int> lis; n     for (int j = 1; j <= n; j++) { n       lis[mp[j][i]] = 1; n     } n     int cnt = 0; n     for (auto &j : lis) { n       j.second = ++cnt; n     } n     int len = lis.size(); n     for (int j = 1; j <= n; j++) { n       prelb[j][i] = len - lis[mp[j][i]]; n       prels[j][i] = lis[mp[j][i]] - 1; n     } n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       int base = max(prehs[i][j], prels[i][j]); n       int big = max(prehb[i][j], prelb[i][j]) + base + 1; n       cout << big <<    ; n     } n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a, b; n int primect = 0; n int primes[40000]; n int arr[1000000]; n set<int> pset; n long long costl[1000000]; n long long costr[1000000]; n long long minCosts[30]; n long long inf = numeric_limits<long long>::max(); n void fillInPrimes() { n   primes[0] = 2; n   primect = 1; n   for (int i = 3; i <= sqrt(1000000000); i += 2) { n     bool isprime = true; n     for (int j = 0; j < primect; j++) { n       if (i % primes[j] == 0) { n         isprime = false; n         break; n       } n     } n     if (isprime) { n       primes[primect] = i; n       primect++; n     } n   } n } n long long calcCost(int l, int r) { n   long long cost = a * (long long)(r - l + 1); n   if (l > 0) { n     if (costl[l - 1] == inf) { n       return inf; n     } else { n       cost += costl[l - 1]; n     } n   } n   if (r < n - 1) { n     if (costr[r + 1] == inf) { n       return inf; n     } else { n       cost += costr[r + 1]; n     } n   } n   return cost; n } n int main() { n   cin >> n >> a >> b; n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n   } n   fillInPrimes(); n   for (int i = 0; i < n; i = n - 1) { n     for (int j = -1; j <= 1; j++) { n       bool factored = false; n       int fac = arr[i] + j; n       int k = 0; n       if (fac < 2) { n         continue; n       } n       while (k < primect && primes[k] <= fac) { n         if (fac % primes[k] == 0) { n           pset.insert(primes[k]); n           fac /= primes[k]; n           factored = true; n         } else { n           k++; n         } n       } n       if (fac > 2) { n         pset.insert(fac); n       } n     } n     if (i == n - 1) { n       break; n     } n   } n   int pi = 0; n   for (auto it = pset.begin(); it != pset.end(); it++) { n     int p = *it; n     for (int i = 0; i < n; i++) { n       int mod = arr[i] % p; n       if (mod == 0) { n         costl[i] = 0; n         costr[i] = 0; n       } else if (mod == 1 || mod == p - 1) { n         costl[i] = b; n         costr[i] = b; n       } else { n         costl[i] = inf; n         costr[i] = inf; n       } n     } n     for (int i = 1; i < n; i++) { n       if (costl[i - 1] == inf) { n         costl[i] = inf; n       } else if (costl[i] != inf) { n         costl[i] += costl[i - 1]; n       } n       if (costr[n - i] == inf) { n         costr[n - i - 1] = inf; n       } else if (costr[n - i - 1] != inf) { n         costr[n - i - 1] += costr[n - i]; n       } n     } n     int firstInf = n; n     int lastInf = 0; n     for (int i = 0; i < n; i++) { n       if (costl[i] == inf) { n         firstInf = i; n         break; n       } n     } n     for (int i = n - 1; i >= 0; i--) { n       if (costr[i] == inf) { n         lastInf = i; n         break; n       } n     } n     long long minEndingHere = inf; n     long long minLeft = 0; n     long long minSoFar = costr[0]; n     for (int i = 0; i < n; i++) { n       int l = min(i, firstInf); n       int r = max(i, lastInf); n       long long cost; n       if (calcCost(minLeft, r) <= calcCost(l, r)) { n         cost = calcCost(minLeft, r); n       } else { n         minLeft = l; n         cost = calcCost(l, r); n       } n       if (cost < minSoFar) { n         minSoFar = cost; n       } n     } n     minCosts[pi] = minSoFar; n     pi++; n   } n   long long minn = inf; n   for (int i = 0; i < pset.size(); i++) { n     minn = min(minn, minCosts[i]); n   } n   cout << minn << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 998244353; n int type[200010]; n int weight[200010]; n map<int, long long int> M; n long long int power(long long int a, int b) { n   a %= mod; n   long long int res = 1; n   while (b) { n     if (b & 1) res = res * a % mod; n     a = a * a % mod; n     b >>= 1; n   } n   return res; n } n long long int inv(int a) { n   if (a <= 0) return 0; n   if (M.find(a) != M.end()) return M[a]; n   return M[a] = power(a, mod - 2); n } n long long int dp[3001][3001]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n, m, w1 = 0, w2 = 0; n   cin >> n >> m; n   for (int i = 1; i <= n; i++) cin >> type[i]; n   for (int i = 1; i <= n; i++) { n     cin >> weight[i]; n     if (!type[i]) n       w1 += weight[i]; n     else n       w2 += weight[i]; n   } n   dp[0][0] = 1; n   for (int i = 1; i <= m; i++) { n     for (int j = 0; j < i; j++) n       dp[i][j] = dp[i - 1][j] * (w1 - (i - 1 - j)) % mod * n                  inv(w1 - (i - 1 - j) + w2 + j) % mod; n     for (int j = 1; j <= i; j++) n       dp[i][j] = (dp[i][j] + dp[i - 1][j - 1] * (w2 + j - 1) % mod * n                                  inv(w1 - (i - 1 - (j - 1)) + w2 + (j - 1))) % n                  mod; n   } n   long long int c1 = 0, c2 = 0; n   for (int i = 0; i <= m; i++) c2 = (c2 + dp[m][i] * (w2 + i)) % mod; n   c2 = c2 * inv(w2) % mod; n   for (int i = 0; i <= m; i++) c1 = (c1 + dp[m][i] * (w1 - (m - i))) % mod; n   c1 = c1 * inv(w1) % mod; n   for (int i = 1; i <= n; i++) n     if (type[i]) n       cout << weight[i] * c2 % mod <<   n ; n     else n       cout << weight[i] * c1 % mod <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline int size(T x) { n   return x.size(); n } n long long k[3], t[3]; n int num_done[100005] = {}; n queue<int> waiters[3]; n set<pair<long long, int> > Q; n void try_putinto(int pid, int qnr, long long tijd) { n   if (!k[qnr]) { n     waiters[qnr].push(pid); n   } else { n     num_done[pid]++; n     k[qnr]--; n     Q.insert(make_pair(tijd + t[qnr], pid)); n   } n } n void onefree(int qnr, long long tijd) { n   k[qnr]++; n   if (!waiters[qnr].empty()) { n     int pid_next = waiters[qnr].front(); n     waiters[qnr].pop(); n     try_putinto(pid_next, qnr, tijd); n   } n } n int main() { n   cin >> k[0] >> k[1] >> k[2]; n   cin >> t[0] >> t[1] >> t[2]; n   int n; n   cin >> n; n   vector<long long> arrival; n   for (int i = 0; i < n; i++) { n     int tmp; n     cin >> tmp; n     arrival.push_back(tmp); n   } n   for (int i = 0; i < n; i++) { n     Q.insert(make_pair(arrival[i], i)); n   } n   long long t_res = 0; n   while (!Q.empty()) { n     pair<long long, int> pp = *Q.begin(); n     long long tijd = pp.first; n     int pid = pp.second; n     Q.erase(Q.begin()); n     if (num_done[pid] > 0) { n       onefree(num_done[pid] - 1, tijd); n     } n     if (num_done[pid] == 3) { n       t_res = max(t_res, tijd - arrival[pid]); n     } else { n       try_putinto(pid, num_done[pid], tijd); n     } n   } n   cout << t_res << endl; n } n


#include <bits/stdc++.h> n using namespace std; n struct point { n   double x; n   double y; n }; n int compare(const void *a, const void *b) { n   point *aa, *bb; n   aa = (point *)a; n   bb = (point *)b; n   if (aa->x > bb->x) n     return 1; n   else if (aa->x < bb->x) n     return -1; n   else n     return aa->y > bb->y; n } n int main(void) { n   double y1, y2, y3, x1, x2, x3; n   set<int> X, Y; n   int status(0); n   point points[8]; n   for (int i = 0; i < 8; i++) { n     std::cin >> points[i].x >> points[i].y; n     if (points[i].x < 0 || points[i].y < 0) status = -1; n     X.insert(points[i].x); n     Y.insert(points[i].y); n   } n   if (status == -1 || X.size() != 3 || Y.size() != 3) { n     std::cout <<  ugly ; n     return 0; n   } n   set<int>::iterator itx = X.begin(); n   set<int>::iterator ity = Y.begin(); n   y1 = *ity; n   ity++; n   y2 = *ity; n   ity++; n   y3 = *ity; n   x1 = *itx; n   itx++; n   x2 = *itx; n   itx++; n   x3 = *itx; n   if (!(y1 < y2 && y2 < y3 && x1 < x2 && x2 < x3)) { n     std::cout <<  ugly ; n     return 0; n   } n   qsort(points, 8, sizeof(point), compare); n   if (x1 == points[0].x && x1 == points[1].x && x1 == points[2].x && n       x2 == points[3].x && x2 == points[4].x && x3 == points[5].x && n       x3 == points[6].x && x3 == points[7].x && y1 == points[0].y && n       y1 == points[3].y && y1 == points[5].y && y2 == points[1].y && n       y2 == points[6].y && y3 == points[2].y && y3 == points[4].y && n       y3 == points[7].y) n     std::cout <<  respectable ; n   else n     std::cout <<  ugly ; n } n


#include <bits/stdc++.h> n using namespace std; n bool check = false; n string s; n int maxi, n, i, j, a; n int main() { n   cin >> n; n   vector<int> v; n   for (i = 0; i < n; i++) { n     maxi = 0; n     for (j = 0; j < n; j++) { n       cin >> a; n       maxi = max(maxi, a); n     } n     if (check == false && maxi == n - 1) { n       maxi = n; n       check = true; n     } n     v.push_back(maxi); n   } n   for (i = 0; i < v.size(); i++) { n     cout << v[i] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, p[200005]; n int ans[200005]; n queue<int> q; n int main() { n   int Q; n   cin >> Q; n   while (Q--) { n     memset(ans, 0, sizeof(ans)); n     cin >> n; n     for (int i = 1; i <= n; i++) cin >> p[i]; n     for (int i = 1; i <= n; i++) { n       if (ans[i]) continue; n       int step = 1; n       q.push(i); n       for (int j = p[i]; j != i; j = p[j]) { n         q.push(j); n         step++; n       } n       while (!q.empty()) { n         ans[q.front()] = step; n         q.pop(); n       } n     } n     for (int i = 1; i <= n; i++) cout << ans[i] <<    ; n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rnd(51); n signed main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   long long n; n   cin >> n; n   vector<long long> a(n); n   for (long long i = 0; i < n; i++) { n     cin >> a[i]; n   } n   deque<pair<long long, long long>> q; n   for (long long i = 0; i < n; i++) { n     if (q.size() == 0) { n       q.push_back({a[i], 1}); n     } else { n       if (q.back().first == a[i]) { n         q.back().second++; n         if (q.size() > 1 && q[q.size() - 2].first >= q.back().first) { n           long long now = q.back().second; n           q.pop_back(); n           q.back().second += now; n         } n       } else if (q.back().first > a[i]) { n         q.push_back({a[i], 1}); n       } else { n         if (q.back().second % 2 == 1) { n           cout <<  NO n ; n           return 0; n         } else { n           q.push_back({a[i], 1}); n         } n       } n     } n   } n   while (q.size() > 1 && ((q[q.size() - 2].second % 2 == 0 && n                            q[q.size() - 2].first < q.back().first) || n                           (q.back().second % 2 == 0 && n                            q.back().first < q[q.size() - 2].first))) { n     long long now = q.back().second; n     long long mx = max(q.back().first, q[q.size() - 2].first); n     q.pop_back(); n     q.back().first = mx; n     q.back().second += now; n   } n   if (q.size() == 1) { n     cout <<  YES n ; n   } else { n     cout <<  NO n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long n, k; n   cin >> n >> k; n   string s; n   cin >> s; n   vector<long long> l_r, r_l; n   long long count = k + 1; n   for (long long i = 0; i < n; i++) { n     if (s[i] ==  1 ) { n       count = 0; n       l_r.push_back(0); n     } else if (s[i] ==  0 ) { n       count++; n       if (count > k) n         l_r.push_back(1); n       else n         l_r.push_back(0); n     } n   } n   count = k + 1; n   for (long long i = n - 1; i >= 0; i--) { n     if (s[i] ==  1 ) { n       count = 0; n       r_l.push_back(0); n     } else if (s[i] ==  0 ) { n       count++; n       if (count > k) n         r_l.push_back(1); n       else n         r_l.push_back(0); n     } n   } n   reverse(r_l.begin(), r_l.end()); n   long long i = 0, ans = 0; n   while (i < n) { n     if (l_r[i] && r_l[i]) { n       ans++; n       i += k; n     } n     i++; n   } n   cout << ans <<   n ; n   return; n } n int main() { n   long long t; n   cin >> t; n   for (long long i = 0; i < t; i++) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, i, a, b, aux1, aux2, cs, cd, mij, rsp; n char s[200005]; n int u[200005], r[200005], l[200005], d[200005]; n int main() { n   ios::sync_with_stdio(false); n   cin >> n; n   cin >> s; n   cin >> a >> b; n   if (abs(a) + abs(b) > n) { n     cout << -1; n     return 0; n   } n   if ((abs(a) + abs(b)) % 2 != n % 2) { n     cout << -1; n     return 0; n   } n   for (i = 0; i < n; i++) { n     if (s[i] ==  U ) u[i + 1]++; n     if (s[i] ==  D ) d[i + 1]++; n     if (s[i] ==  L ) l[i + 1]++; n     if (s[i] ==  R ) r[i + 1]++; n   } n   for (i = 1; i <= n; i++) { n     u[i] += u[i - 1]; n     r[i] += r[i - 1]; n     d[i] += d[i - 1]; n     l[i] += l[i - 1]; n   } n   if (u[n] - d[n] == b && r[n] - l[n] == a) { n     cout << 0; n     return 0; n   } n   rsp = n; n   for (i = 1; i <= n; i++) { n     cs = i; n     cd = n; n     while (cs <= cd) { n       mij = (cs + cd) >> 1; n       aux1 = r[i - 1]; n       aux1 -= l[i - 1]; n       aux1 += r[n] - r[mij]; n       aux1 -= l[n] - l[mij]; n       aux2 = u[i - 1]; n       aux2 -= d[i - 1]; n       aux2 += u[n] - u[mij]; n       aux2 -= d[n] - d[mij]; n       if (abs(aux1 - a) + abs(aux2 - b) <= mij - i + 1) { n         rsp = min(rsp, mij - i + 1); n         cd = mij - 1; n       } else n         cs = mij + 1; n     } n   } n   cout << rsp; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     string s; n     cin >> s; n     string ans; n     ans = s; n     sort(ans.begin(), ans.end()); n     int cnt = 0; n     for (int i = 0; i < n; i++) { n       if (ans[i] != s[i]) cnt++; n     } n     cout << cnt << endl; n   } n } n