#include <bits/stdc++.h> n const int INF = 0x3f3f3f3f; n const std::string str =  ATGC ; n int main() { n   int n, m; n   std::vector<std::string> s; n   std::vector<std::string> tmp; n   std::vector<std::string> ans; n   int min_ans = INF, pos; n   std::cin >> n >> m; n   for (int i = 0; i < n; ++i) { n     std::string st; n     std::cin >> st; n     s.push_back(st); n   } n   for (int c1 = 0; c1 < 4; ++c1) n     for (int c2 = c1 + 1; c2 < 4; ++c2) { n       std::string stri; n       std::set<int> st; n       for (int i = 0; i < 4; ++i) st.insert(i); n       st.erase(c1); n       st.erase(c2); n       auto it = st.begin(); n       stri.push_back(str[c1]); n       stri.push_back(str[c2]); n       stri.push_back(str[*(it++)]); n       stri.push_back(str[*it]); n       pos = 0; n       tmp.clear(); n       for (int i = 0; i < n; ++i) { n         tmp.push_back(  ); n         int bit = (i & 1) << 1; n         int cnt1 = 0, cnt2 = 0; n         for (int j = 0; j < m; ++j) { n           if (s[i][j] != stri[bit + (j & 1)]) ++cnt1; n           if (s[i][j] != stri[bit + ((j & 1) ^ 1)]) ++cnt2; n         } n         if (cnt1 < cnt2) n           for (int j = 0; j < m; ++j) tmp[i].push_back(stri[bit + (j & 1)]); n         else n           for (int j = 0; j < m; ++j) n             tmp[i].push_back(stri[bit + ((j & 1) ^ 1)]); n         pos += std::min(cnt1, cnt2); n       } n       if (pos < min_ans) { n         ans = tmp; n         min_ans = pos; n       } n       pos = 0; n       tmp.clear(); n       for (int i = 0; i < n; ++i) tmp.push_back(  ); n       for (int j = 0; j < m; ++j) { n         int bit = (j & 1) << 1; n         int cnt1 = 0, cnt2 = 0; n         for (int i = 0; i < n; ++i) { n           if (s[i][j] != stri[bit + (i & 1)]) ++cnt1; n           if (s[i][j] != stri[bit + ((i & 1) ^ 1)]) ++cnt2; n         } n         if (cnt1 < cnt2) n           for (int i = 0; i < n; ++i) tmp[i].push_back(stri[bit + (i & 1)]); n         else n           for (int i = 0; i < n; ++i) n             tmp[i].push_back(stri[bit + ((i & 1) ^ 1)]); n         pos += std::min(cnt1, cnt2); n       } n       if (pos < min_ans) { n         ans = tmp; n         min_ans = pos; n       } n     } n   for (int i = 0; i < n; ++i) std::cout << ans[i] << std::endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long base = 233; n const long long ff = 4e5 + 1; n const long long mod = 1e9 + 7; n const long long inf = 1e17 + 1; n long long read() { n   long long x; n   scanf( %lld , &x); n   return x; n } n inline long long Read() { n   long long x = 0, f = 1; n   char ch = getchar(); n   while (!isdigit(ch)) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     x = (x << 3) + (x << 1) + ch -  0 ; n     ch = getchar(); n   } n   return x * f; n } n long long a, b, c, d; n long long my_max(long long x, long long y) { return (x > y) ? x : y; } n void init() { n   long long ans = 0; n   a = Read(), b = Read(), c = Read(), d = Read(); n   for (long long i = a; i <= b; ++i) { n     long long st = my_max(min(i + b - c, d), 0LL); n     long long ed = my_max(min(i + c - 1, d) - c + 1, 0LL); n     long long q = max(ed - st + 1, 0LL); n     ans += (st + ed) * q / 2; n     if (i + c - 1 > d) n       ans += my_max(min((i + c - 1) - d, c - b + 1), 0LL) * (d - c + 1); n   } n   cout << ans; n } n signed main() { init(); } n


#include <bits/stdc++.h> n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   if (n % 2 != 0) { n     printf( %d , n); n   } else { n     int a[n][m]; n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < m; j++) { n         scanf( %d , &a[i][j]); n       } n     } n     bool isSymmetric = true; n     while (isSymmetric == true && n % 2 == 0) { n       for (int i = 0; i < n / 2; i++) { n         for (int j = 0; j < m; j++) { n           if (a[i][j] != a[n - 1 - i][j]) { n             isSymmetric = false; n             break; n           } n         } n       } n       if (isSymmetric) { n         n = n / 2; n       } n     } n     printf( %d , n); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   cin >> n >> k; n   vector<int> questions(k); n   vector<int> previsited(n + 1, 0); n   vector<int> posvisited(n + 1, 0); n   vector<int> vis; n   int tot_non = n; n   for (int i = 0; i < k; i++) { n     cin >> questions[i]; n     if (!previsited[questions[i]]) { n       tot_non--; n       vis.push_back(questions[i]); n       previsited[questions[i]] = i + 1; n     } n     posvisited[questions[i]] = i + 1; n   } n   int ans = tot_non * 3; n   if (previsited[1] == 0) ans--; n   if (previsited[n] == 0) ans--; n   for (int i = 0; i < vis.size(); i++) { n     if (vis[i] <= n - 1 && previsited[vis[i]] > posvisited[vis[i] + 1]) { n       ans++; n     } n     if (vis[i] >= 2 && previsited[vis[i]] > posvisited[vis[i] - 1]) { n       ans++; n     } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int MOD = (pow(10, 9) + 7); n void vec_p(vector<long long int> &a) { n   for (long long int i = 0; i < a.size(); i++) { n     cout << a[i] <<    ; n   } n   cout <<   n ; n } n long long int vec_sum(vector<long long int> a) { n   long long int s = 0; n   for (long long int i = 0; i < a.size(); i++) { n     s += a[i]; n   } n   return s; n } n void pfx(vector<long long int> &a, vector<long long int> &prefix_sum) { n   prefix_sum[0] = a[0]; n   for (long long int i = 1; i < a.size(); i++) { n     prefix_sum[i] += (prefix_sum[i - 1] + a[i]); n   } n } n void sorting_p(vector<pair<long long int, long long int> > &a) { n   sort(a.begin(), a.end()); n } n void sorting(vector<long long int> &a) { n   sort(a.begin(), a.end(), greater<long long int>()); n } n void vec_in(vector<long long int> &a, long long int n) { n   for (long long int i = 0; i < n; i++) { n     long long int x; n     cin >> x; n     a.push_back(x); n   } n } n void Graph_in(vector<vector<long long int> > &G, long long int n) { n   long long int x, y; n   for (long long int i = 0; i < n; i++) { n     cin >> x >> y; n     G[x].push_back(y); n     G[y].push_back(x); n   } n } n void lps_table(string &s, vector<long long int> &lps) { n   long long int n = s.size(); n   long long int len = 0; n   for (long long int i = 1; i < n; i++) { n     while (s[i] != s[len] && len > 0) { n       len = lps[len - 1]; n       if (len == 0 && s[len] != s[i]) { n         lps[i] = 0; n         len = 0; n         break; n       } n     } n     if (s[len] == s[i]) { n       lps[i] = len + 1; n       len++; n     } n     if (len == 0 && s[len] != s[i]) { n       lps[i] = 0; n       len = 0; n     } n   } n } n void traversal(long long int &x, long long int &y, n                vector<vector<long long int> > &a, long long int r, n                long long int c) { n   if (x + 2 < r) { n     if (y + 1 < c) { n       if (a[x + 2][y + 1] == -1) { n         long long int i = x + 2; n         long long int j = y + 1; n         a[i][j] = a[x][y] ^ 1; n         traversal(i, j, a, r, c); n       } n     } n     if (y - 1 < c && y - 1 >= 0) { n       if (a[x + 2][y - 1] == -1) { n         long long int i = x + 2; n         long long int j = y - 1; n         a[i][j] = a[x][y] ^ 1; n         traversal(i, j, a, r, c); n       } n     } n   } n   if (x + 1 < r) { n     if (y + 2 < c) { n       if (a[x + 1][y + 2] == -1) { n         long long int i = x + 1; n         long long int j = y + 2; n         a[i][j] = a[x][y] ^ 1; n         traversal(i, j, a, r, c); n       } n     } n     if (y - 2 < c && y - 2 >= 0) { n       if (a[x + 1][y - 2] == -1) { n         long long int i = x + 1; n         long long int j = y - 2; n         a[i][j] = a[x][y] ^ 1; n         traversal(i, j, a, r, c); n       } n     } n   } n   if (x - 2 < r && x - 2 >= 0) { n     if (y + 1 < c) { n       if (a[x - 2][y + 1] == -1) { n         long long int i = x - 2; n         long long int j = y + 1; n         a[i][j] = a[x][y] ^ 1; n         traversal(i, j, a, r, c); n       } n     } n     if (y - 1 < c && y - 1 >= 0) { n       if (a[x - 2][y - 1] == -1) { n         long long int i = x - 2; n         long long int j = y - 1; n         a[i][j] = a[x][y] ^ 1; n         traversal(i, j, a, r, c); n       } n     } n   } n   if (x - 1 < r && x - 1 >= 0) { n     if (y + 2 < c) { n       if (a[x - 1][y + 2] == -1) { n         long long int i = x - 1; n         long long int j = y + 2; n         a[i][j] = a[x][y] ^ 1; n         traversal(i, j, a, r, c); n       } n     } n     if (y - 2 < c && y - 2 >= 0) { n       if (a[x - 1][y - 2] == -1) { n         long long int i = x - 1; n         long long int j = y - 2; n         a[i][j] = a[x][y] ^ 1; n         traversal(i, j, a, r, c); n       } n     } n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int r, c; n   cin >> r >> c; n   if (r < c) { n     swap(r, c); n   } n   vector<vector<long long int> > a(r, vector<long long int>(c, -1)); n   for (long long int i = 0; i < r; i++) { n     for (long long int j = 0; j < c; j++) { n       if (a[i][j] == -1) { n         a[i][j] = 1; n         traversal(i, j, a, r, c); n       } n     } n   } n   long long int one = 0; n   for (long long int i = 0; i < r; i++) { n     for (long long int j = 0; j < c; j++) { n       if (a[i][j] == 1) { n         one++; n       } n     } n   } n   cout << one <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n, m; n   cin >> n >> m; n   int a[n + 1]; n   for (int i = 1; i <= n; ++i) cin >> a[i]; n   int ans = 0; n   for (int i = 0; i < m; ++i) { n     int u, v; n     cin >> u >> v; n     ans += min(a[u], a[v]); n   } n   cout << ans << endl; n } n


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


#include <bits/stdc++.h> n using namespace std; n const int inf = (int)1e9; n const double INF = 1e12, EPS = 1e-9; n int n; n pair<int, int> p[5000]; n int main() { n   cin >> n; n   for (int i = 0; i < (int)n; i++) { n     int a, b; n     cin >> a >> b; n     p[i] = pair<int, int>(a, b); n   } n   sort(p, p + n); n   int mn = 0; n   for (int i = 0; i < (int)n; i++) { n     if (p[i].second >= mn) n       mn = p[i].second; n     else n       mn = p[i].first; n   } n   cout << mn << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string str; n   cin >> str; n   stack<char> s; n   map<char, char> type = {{ ( ,  ) }, { { ,  } }, { < ,  > }, { [ ,  ] }}; n   int ans = 0; n   for (auto it : str) { n     if (it ==  {  || it ==  (  || it ==  <  || it ==  [ ) { n       s.push(it); n     } else { n       if (s.empty()) { n         cout <<  Impossible n ; n         return 0; n       } else { n         if (type[s.top()] != it) { n           ans += 1; n         } n         s.pop(); n       } n     } n   } n   if (s.empty()) { n     cout << ans <<   n ; n     return 0; n   } n   cout <<  Impossible n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = (int)1e5 + 10; n bool isp[maxn]; n int _prime[maxn], lp = 0; n int _minCost[maxn]; n void InitPrime() { n   isp[0] = isp[1] = true; n   for (int i = 4; i < maxn; i += 2) isp[i] = true; n   for (int i = 3; i * i < maxn; i += 2) n     if (!isp[i]) n       for (int j = i * i; j < maxn; j += i) isp[j] = true; n   _prime[lp++] = 2; n   for (int i = 3; i < maxn; i += 2) n     if (!isp[i]) _prime[lp++] = i; n } n bool IsPrime(long long n) { n   if (n < maxn) return !isp[n]; n   for (int i = 0; i < lp && (long long)_prime[i] * _prime[i] <= n; ++i) { n     if (n % _prime[i] == 0) return false; n   } n   return true; n } n int minCost(long long n) { n   if (IsPrime(n)) return 1; n   for (int i = 0; i < lp && _prime[i] + 2 <= n; ++i) { n     if (n - _prime[i] >= 2 && IsPrime(n - _prime[i])) return 2; n   } n   return 3; n } n int main() { n   InitPrime(); n   long long n; n   cin >> n; n   cout << minCost(n) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = INT_MAX; n const long long INFL = LLONG_MAX; n const long double pi = acos(-1); n int N; n int A[300100]; n long long S[300100]; n int shouldCut[300100]; n map<int, vector<int> > mymap; n vector<int> v; n int main() { n   ios_base::sync_with_stdio(0); n   cout.precision(15); n   cout << fixed; n   cout.tie(0); n   cin.tie(0); n   cin >> N; n   for (int(i) = 1, j123 = N; (i) <= j123; (i)++) { n     cin >> A[i]; n     S[i] = max(0, A[i]); n     S[i] += S[i - 1]; n     mymap[A[i]].push_back(i); n     v.push_back(A[i]); n   } n   sort(v.begin(), v.end()); n   v.erase(unique(v.begin(), v.end()), v.end()); n   int mx = -2e9; n   long long bestTot = -2e10; n   int bestFirst = -1; n   int bestLast = -1; n   for (int(i) = 0, j123 = int(v.size()); (i) < j123; (i)++) { n     if (int(mymap[v[i]].size()) > 1) { n       sort(mymap[v[i]].begin(), mymap[v[i]].end()); n       int curfirst = mymap[v[i]].front(); n       int curlast = mymap[v[i]].back(); n       long long score = S[curlast - 1] - S[curfirst]; n       score += A[curfirst]; n       score += A[curlast]; n       if (score > bestTot) { n         bestTot = score; n         bestFirst = curfirst; n         bestLast = curlast; n       } n     } n   } n   for (int i = 1; i < bestFirst; i++) shouldCut[i] = 1; n   for (int i = N; i > bestLast; i--) shouldCut[i] = 1; n   for (int i = bestFirst + 1; i <= bestLast - 1; i++) n     if (A[i] < 0) shouldCut[i] = 1; n   long long toteth = 0; n   vector<int> vec; n   for (int(i) = 1, j123 = N; (i) <= j123; (i)++) { n     if (shouldCut[i]) n       vec.push_back(i); n     else n       toteth += A[i]; n   } n   cout << toteth <<     << int(vec.size()) <<   n ; n   for (int(i) = 0, j123 = int(vec.size()); (i) < j123; (i)++) n     cout << vec[i] << (i + 1 < int(vec.size()) ?     :   n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 1999999999; n const double pi = acos(-1.0); n const double eps = 1e-9; n char gc() { n   char c; n   while (isspace(c = getchar())) n     ; n   return c; n } n int gs(char* s) { n   if (!fgets(s, 9999999, stdin)) return -1; n   int l = strlen(s); n   while (l && isspace(s[l - 1])) s[--l] = 0; n   return l; n } n int main() { n   ios::sync_with_stdio(false); n   cout << fixed << setprecision(15); n   int n; n   cin >> n; n   vector<int> v; n   for (int i = 0, _i = (n); i < (_i); i++) { n     int t; n     cin >> t; n     v.push_back(t); n   } n   sort((v).begin(), (v).end()); n   v.erase(unique((v).begin(), (v).end()), v.end()); n   for (int i = 0, _i = (((int)(v).size()) - 1); i < (_i); i++) { n     if (v[i] * 2 > v[i + 1]) { n       cout <<  YES ; n       return 0; n     } n   } n   cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 24; n int n, m, a[N][N], f[1 << 20]; n char str[N][N]; n void work() { n   int i, j, k, x; n   scanf( %d%d , &n, &m); n   for (i = 0; i < n; ++i) scanf( %s , str[i]); n   for (i = 0; i < n; ++i) n     for (j = 0; j < m; ++j) scanf( %d , &a[i][j]); n   for (i = 1; i < 1 << n; ++i) { n     x = __builtin_ctz(i & -i); n     f[i] = 1 << 30; n     for (j = 0; j < m; ++j) { n       f[i] = min(f[i], f[i ^ (1 << x)] + a[x][j]); n       int sum = 0, mx = 0, mask = 0; n       for (k = 0; k < n; ++k) { n         if (str[k][j] == str[x][j]) { n           sum += a[k][j]; n           mx = max(mx, a[k][j]); n           mask |= 1 << k; n         } n       } n       f[i] = min(f[i], f[i & (i ^ mask)] + sum - mx); n     } n   } n   cout << f[(1 << n) - 1] << endl; n } n int main() { n   work(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int pw(long long int a, long long int b, long long int mod) { n   if (!b) return 1; n   if (b & 1) return a * pw(a * a % mod, b / 2, mod) % mod; n   return pw(a * a % mod, b / 2, mod) % mod; n } n const long long int MAXN = 1e5 + 10; n const long long int MAXK = 1e4 + 10; n const long long int INF = 8e18; n const long long int MOD = 1e9 + 7; n long long int h, n, m, k, dist[MAXK], mark[MAXK], seen[MAXN]; n pair<long long int, long long int> A[MAXN]; n vector<pair<long long int, long long int>> adj[MAXK]; n set<pair<long long int, long long int>> st; n priority_queue<pair<long long int, long long int>, n                vector<pair<long long int, long long int>>, n                greater<pair<long long int, long long int>>> n     pq; n void Dijk() { n   fill(dist, dist + MAXK, INF); n   fill(mark, mark + MAXK, 0); n   dist[0] = 0; n   pq.push({0, 0}); n   while (!pq.empty()) { n     long long int u = pq.top().second; n     pq.pop(); n     if (mark[u]) continue; n     mark[u] = 1; n     for (pair<long long int, long long int>& e : adj[u]) { n       if (dist[e.first] <= dist[u] + e.second) continue; n       dist[e.first] = dist[u] + e.second; n       pq.push({dist[e.first], e.first}); n     } n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   cin >> h >> n >> m >> k; n   for (long long int i = 1; i <= n; i++) { n     cin >> A[i].first >> A[i].second; n     A[i].first--; n     if (A[i].first % k == 0) { n       st.insert({A[i].second, -i}); n       seen[i] = 1; n     } n   } n   Dijk(); n   while (m--) { n     long long int type; n     cin >> type; n     if (type == 1) { n       long long int x; n       cin >> x; n       for (long long int i = 0; i < k; i++) { n         adj[i].push_back({(i + x) % k, x}); n       } n       Dijk(); n       for (long long int i = 1; i <= n; i++) { n         if (!seen[i] && dist[A[i].first % k] <= A[i].first) { n           seen[i] = 1; n           st.insert({A[i].second, -i}); n         } n       } n     } n     if (type == 2) { n       long long int x, val; n       cin >> x >> val; n       if (seen[x]) st.erase({A[x].second, -x}); n       A[x].second -= val; n       if (seen[x]) st.insert({A[x].second, -x}); n     } n     if (type == 3) { n       if (st.empty()) { n         cout << 0 <<   n ; n         continue; n       } n       auto it = st.end(); n       it--; n       cout << (*it).first <<   n ; n       st.erase(it); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void read(T &x) { n   cin >> x; n } n template <typename T, typename T0> n void read(T &x, T0 &y) { n   cin >> x >> y; n } n template <typename T, typename T0, typename T1> n void read(T &x, T0 &y, T1 &z) { n   cin >> x >> y >> z; n } n bool isPowerOfTwo(int x) { return x && (!(x & (x - 1))); } n int digits(int n) { n   int x = floor(log10(n)) + 1; n   return x; n } n long long int fact(long long int n) { n   if (n == 0) return 1; n   return n * fact(n - 1); n } n bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) { n   return (a.second < b.second); n } n vector<bool> sieve(1000000, true); n void sieve_er() { n   sieve[0] = false; n   sieve[1] = false; n   for (long long int i = 2; i * i <= 1000000; i++) { n     if (sieve[i] == true) { n       for (long long int j = i * i; j < 1000000; j += i) sieve[j] = false; n     } n   } n } n vector<long long int> vec; n void divisors(long long int n) { n   for (long long int i = 1; i <= sqrt(n); i++) { n     if (n % i == 0) { n       if (n / i == i) n         vec.push_back(i); n       else { n         vec.push_back(i); n         vec.push_back(n / i); n       } n     } n   } n } n long long int total_divisors(long long int n) { n   long long int c = 0; n   for (long long int i = 1; i <= sqrt(n); i++) { n     if (n % i == 0) { n       if (n / i == i) n         c = c + 1; n       else n         c = c + 2; n     } n   } n   return c; n } n long long int nCr(long long int n, long long int r) { n   long long int ans = 1; n   for (long long int i = 0; i < r; i++) { n     ans *= n - i; n     ans /= i + 1; n   } n   return ans; n } n long long int power_m(long long int a, long long int b, n                       long long int m = 1000000007) { n   a %= m; n   long long int res = 1; n   while (b > 0) { n     if (b & 1) res = res * a % m; n     a = a * a % m; n     b >>= 1; n   } n   return res; n } n long long int power(long long int a, long long int b) { n   long long int res = 1; n   while (b > 0) { n     if (b & 1) res = res * a; n     a = a * a; n     b >>= 1; n   } n   return res; n } n long long int strTo(string s) { n   stringstream tmp(s); n   long long int x = 0; n   tmp >> x; n   return x; n } n string NoTo(long long int n) { n   ostringstream str1; n   str1 << n; n   string ans = str1.str(); n   return ans; n } n void start() {} n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int t; n   cin >> t; n   while (t--) { n     long long int x1, y1, x2, y2; n     cin >> x1 >> y1 >> x2 >> y2; n     int ans = 0; n     if (x1 == x2) n       ans = abs(y1 - y2); n     else if (y1 == y2) n       ans = abs(x1 - x2); n     else { n       ans += abs(x1 - x2); n       ans += 2; n       ans += abs(y1 - y2); n     } n     cout << ans << endl; n   } n   cerr <<  Time :   << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC n        <<  ms n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, p, k; n   long long a, ans = 0; n   map<long long, long long> mp; n   scanf( %d %d %d , &n, &p, &k); n   for (int i = 0; i < n; i++) { n     scanf( %lld , &a); n     ans += mp[((((a * a) % p) * ((a * a) % p)) % p - (k * a) % p + p) % p]++; n   } n   printf( %lld , ans); n } n


#include <bits/stdc++.h> n int abss(int a); n int main() { n   int size; n   scanf( %d , &size); n   int colunm[size + 10][2]; n   int i; n   int left = 0, right = 0; n   for (i = 1; i <= size; i++) { n     scanf( %d %d , &colunm[i][0], &colunm[i][1]); n     left += colunm[i][0]; n     right += colunm[i][1]; n   } n   int abs = abss(left - right); n   int max = 0, temp, flag = 0; n   for (i = 1; i <= size; i++) { n     int tLeft = left; n     int tRight = right; n     tLeft = tLeft - colunm[i][0] + colunm[i][1]; n     tRight = tRight - colunm[i][1] + colunm[i][0]; n     int tAbs = abss(tLeft - tRight); n     temp = tAbs - abs; n     if (temp > max) { n       max = temp; n       flag = i; n     } n   } n   printf( %d , flag); n   return 0; n } n int abss(int a) { n   if (a > 0) n     return a; n   else n     return -a; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k; n int num[500005]; n long long dp[500005]; n int cnt[500005]; n bool ok(int cost) { n   dp[0] = dp[1] = cnt[0] = cnt[1] = 0; n   for (int i = (2); i <= (n); i += (1)) { n     dp[i] = dp[i - 1]; n     cnt[i] = cnt[i - 1]; n     long long v = dp[i - 2] + num[i] - num[i - 1] - cost; n     int c = cnt[i - 2] + 1; n     if (v < dp[i] || v == dp[i] && c > cnt[i]) dp[i] = v, cnt[i] = c; n   } n   return (cnt[n] >= k); n } n int main() { n   scanf( %d %d , &k, &n); n   for (int i = (1); i <= (n); i += (1)) scanf( %d , &num[i]); n   sort(num + 1, num + n + 1); n   int l = 0, r = 1000000000; n   while (l <= r) { n     int m = (l + r) >> 1; n     if (ok(m)) n       r = m - 1; n     else n       l = m + 1; n   } n   ok(l); n   printf( %lld n , dp[n] + 1LL * l * k); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int ans = 0; n   long long p = 1, q = 2, r, n; n   cin >> n; n   while (q <= n) { n     ans++; n     r = q; n     q = p + q; n     p = r; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 8; n const int dx[9] = {-1, -1, -1, 0, 0, 0, 1, 1, 1}; n const int dy[9] = {-1, 0, 1, -1, 0, 1, -1, 0, 1}; n string a[N]; n int v[105][N][N], f[105][N][N]; n int main() { n   for (int i = 0; i < N; ++i) cin >> a[i]; n   memset(v, 0, sizeof(v)); n   for (int i = 0; i < N; ++i) n     for (int j = 0; j < N; ++j) n       if (a[i][j] ==  S ) n         for (int k = i + 1; k <= N; ++k) n           v[k - i][k][j] = v[k - i][k - 1][j] = 1; n   memset(f, 0, sizeof(f)); n   f[0][7][0] = 1; n   for (int t = 1; t <= 100; ++t) n     for (int x = 0; x < N; ++x) n       for (int y = 0; y < N; ++y) n         if (f[t - 1][x][y]) n           for (int dire = 0; dire < 9; ++dire) { n             int xx = x + dx[dire]; n             int yy = y + dy[dire]; n             if (xx < 0 || xx >= N) continue; n             if (yy < 0 || yy >= N) continue; n             if (v[t][xx][yy]) continue; n             f[t][xx][yy] = 1; n           } n   bool flag = 0; n   for (int t = 1; t <= 100; ++t) flag |= f[t][0][7]; n   if (flag) n     cout <<  WIN ; n   else n     cout <<  LOSE ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   vector<int> p(n); n   for (int i = 0; i < n; ++i) { n     cin >> p[i]; n   } n   vector<int> f(n + 1, 0); n   for (int& x : p) { n     f[x] = f[x - 1] + 1; n   } n   cout << n - *max_element(f.begin(), f.end()); 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   cout.tie(NULL); n   int n, h, l = 0, c = 0, a = 0, b = 0, d; n   cin >> n >> h; n   while (n--) { n     cin >> d; n     c += d; n     if (c > a) n       a = c; n     else if (c < b) n       b = c; n   } n   l -= b; n   h -= a; n   d = h - l + 1; n   if (d < 0) d = 0; n   cout << d; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-9; n const double PI = acos(-1.0); n const long long int mod = 1e9 + 9; n const int MAXN = 1e6 + 5; n long long int fpow(long long int x, long long int y) { n   long long int res = 1; n   while (y > 0) { n     if (y & 1) res = (res % mod * x % mod) % mod; n     x = (x % mod * x % mod) % mod; n     y = y / 2; n   } n   return res; n } n void cp() { n   int n, m; n   cin >> n >> m; n   long long int total = (fpow(2, m) - 1 + mod) % mod; n   long long int ans = 1; n   for (int i = 0; i < n; i++) { n     long long int me = me = (total - i + mod) % mod; n     ans = (ans % mod * me % mod) % mod; n   } n   cout << ans; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   int t; n   t = 1; n   while (t--) { n     cp(); n   } n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int n; n double ans = 0; n void calc(vector<int> arr, int d, double p) { n   if (d == 0) { n     int tmp = 0; n     for (int i = 0; i < n; ++i) { n       for (int j = 0; j < i; ++j) { n         if (arr[j] > arr[i]) { n           tmp++; n         } n       } n     } n     ans += double(tmp) * p; n     return; n   } n   for (int i = 0; i < n; ++i) { n     for (int j = i; j < n; ++j) { n       vector<int> xxx = arr; n       reverse(xxx.begin() + min(i, j), xxx.begin() + max(i, j) + 1); n       calc(xxx, d - 1, p / (n * (n + 1) / 2)); n     } n   } n } n int main() { n   int dep; n   cin >> n >> dep; n   vector<int> vec(n); n   for (int i = 0; i < n; ++i) { n     scanf( %d , &vec[i]); n   } n   calc(vec, dep, 1); n   printf( %.10lf n , ans); n   return 0; n } n


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


#include <bits/stdc++.h> n #pragma GCC optimize ( O3 ) n #pragma GCC optimize ( Ofast ) n using namespace std; n  n #define int int64_t n #define SQ(a) (a)*(a) n  n const int INF = 1e15; n  n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n  n   int n, m; n   cin >> n >> m; n  n   vector<vector<pair<int, int>>> g(n); n   for (int i = 0, a, b, c; i < m; i++) { n     cin >> a >> b >> c; n     a--, b--; n     g[a].push_back({b, c}); n     g[b].push_back({a, c}); n   } n  n   vector<int> deven(n, INF); n   vector<vector<int>> dodd(51, vector<int> (n, INF)); n  n   deven[0] = 0; n   priority_queue<array<int, 3>> Q; // {dist, v, (-1 OR w_last)} n   Q.push({0, 0, -1}); n  n   vector<vector<bool>> vis(52, vector<bool> (n)); n   while (!Q.empty()) { n     int d = -Q.top()[0]; n     int v =  Q.top()[1]; n     int lw=  Q.top()[2]; n     Q.pop(); n  n     if (vis[lw + 1][v]) n       continue; n     vis[lw + 1][v] = true; n  n     if (lw == -1) { // even steps n       for (auto [u, w] : g[v]) { n         if (dodd[w][u] > d) { n           dodd[w][u] = d; n           Q.push({-d, u, w}); n         } n       } n     } else { n       for (auto [u, w] : g[v]) { n         if (deven[u] > d + SQ(lw + w)) { n           deven[u] = d + SQ(lw + w); n           Q.push({-deven[u], u, -1}); n         } n       } n     } n   } n  n   for (int i = 0; i < n; i++) n     cout << (deven[i] == INF ? -1 : deven[i]) <<    ; n   cout <<   n ; n }


#include <bits/stdc++.h> n using namespace std; n int main(int argc, const char* argv[]) { n   int n; n   cin >> n; n   int a[n]; n   int b[n]; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   for (int i = 0; i < n; i++) { n     cin >> b[i]; n   } n   long long mx = 0; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       long long an = 0; n       long long bn = 0; n       for (int t = i; t <= j; t++) { n         an = an | a[t]; n         bn = bn | b[t]; n       } n       if (an + bn > mx) { n         mx = an + bn; n       } n     } n   } n   cout << mx; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const int mod = 1e9 + 7; n const int N = 1e5 + 5; n long long a[N], b[N]; n long long n, h; n bool cmp(int a, int b) { return a > b; } n long long judge(int index) { n   for (int i = 0; i <= index; i++) { n     b[i] = a[i]; n   } n   sort(b, b + index + 1, cmp); n   long long sum; n   if (index % 2 == 0) n     sum = b[index], index--; n   else n     sum = 0; n   for (int i = 0; i <= index; i += 2) { n     sum += b[i]; n   } n   if (sum <= h) n     return 1; n   else n     return 0; n } n int main() { n   std::ios::sync_with_stdio(false); n   cin >> n >> h; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   int l = 0, r = n - 1; n   long long res; n   while (l <= r) { n     int mid = (l + r) / 2; n     if (judge(mid)) { n       l = mid + 1; n       res = mid; n     } else { n       r = mid - 1; n     } n   } n   cout << res + 1 <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n signed long long int a[100000 + 5], b[100000 + 5]; n int n, m; n signed long long int func(signed long long int x) { n   signed long long int ans = 0; n   for (int i = 1; i <= n; ++i) { n     if (a[i] < x) ans += x - a[i]; n   } n   for (int i = 1; i <= m; ++i) { n     if (x < b[i]) ans += b[i] - x; n   } n   return ans; n } n signed long long int ter(signed long long int i, signed long long int j) { n   if (i == j) return i; n   if (j - i == 2) { n     signed long long int mi = j - 1; n     if (func(mi) < func(i) && func(mi) < func(j)) return mi; n     if (func(i) < func(j) && func(i) < func(mi)) return i; n     return j; n   } n   signed long long int l, r; n   l = i + (j - i) / 3; n   r = j - (j - i) / 3; n   if (func(l) > func(r)) n     return ter(l, j); n   else n     return ter(i, r); n } n int main() { n   cin >> n >> m; n   for (int i = 1; i <= n; ++i) cin >> a[i]; n   for (int i = 1; i <= m; ++i) cin >> b[i]; n   cout << func(ter(0, 2 * 1000000007 + 1)); n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int a[120000]; n void query(int L, int R) { n   int M = (L + R) / 2; n   printf( ? %d n , M); n   fflush(stdout); n   printf( ? %d n , n / 2 + M); n   fflush(stdout); n   scanf( %d %d , a + M, a + M + n / 2); n   if (a[M] == a[n / 2 + M]) { n     printf( ! %d n , M); n     fflush(stdout); n     return; n   } n   if (((a[M + n / 2] - a[M]) < 0) ^ ((a[L + n / 2] - a[L]) < 0)) n     query(L, M); n   else n     query(M, R); n } n int main() { n   scanf( %d , &n); n   if (n % 4) { n     printf( ! -1 n ); n     fflush(stdout); n     return 0; n   } n   printf( ? 1 n ); n   fflush(stdout); n   printf( ? %d n , n / 2 + 1); n   fflush(stdout); n   scanf( %d %d , a + 1, a + n / 2 + 1); n   if (a[1] == a[n / 2 + 1]) { n     printf( ! %d n , 1); n     fflush(stdout); n     return 0; n   } n   printf( ? %d n , n / 2); n   fflush(stdout); n   printf( ? %d n , n); n   fflush(stdout); n   scanf( %d %d , a + n / 2, a + n); n   if (a[n / 2] == a[n]) { n     printf( ! %d n , n / 2); n     fflush(stdout); n     return 0; n   } n   query(1, n / 2); n } n


#include <bits/stdc++.h> n using namespace std; n long long readi() { n   long long input = 0; n   char c =    ; n   while (c <  - ) { n     c = getchar(); n   } n   bool negative = false; n   if (c ==  - ) { n     negative = true; n     c = getchar(); n   } n   while (c >=  0 ) { n     input = 10 * input + (c -  0 ); n     c = getchar(); n   } n   if (negative) { n     input = -input; n   } n   return input; n } n void printi(long long output) { n   if (output == 0) { n     putchar( 0 ); n     return; n   } n   if (output < 0) { n     putchar( - ); n     output = -output; n   } n   int aout[20]; n   int ilen = 0; n   while (output) { n     aout[ilen] = ((output % 10)); n     output /= 10; n     ilen++; n   } n   for (int i = ilen - 1; i >= 0; i--) { n     putchar(aout[i] +  0 ); n   } n   return; n } n string reads() { n   string input =   ; n   char c =    ; n   while (c <=    ) { n     c = getchar(); n   } n   while (c >    ) { n     input += c; n     c = getchar(); n   } n   return input; n } n void prints(string output) { n   for (int i = 0; i < output.length(); i++) { n     putchar(output[i]); n   } n   return; n } n long long getrand() { n   long long rand1 = (rand() % 536870912); n   long long rand2 = (rand() % 536870912); n   return (rand1 * 536870912ll + rand2); n } n int N; n long long arr[5][1010]; n bool bad[1010]; n int ans; n long long scalar(long long a, long long b, long long c) { n   long long res = 0ll; n   for (long long i = 0; i < 5; i++) { n     res += ((arr[i][b] - arr[i][a]) * (arr[i][c] - arr[i][a])); n   } n   return res; n } n int32_t main() { n   srand(time(NULL)); n   cout << fixed << setprecision(10); n   cerr << fixed << setprecision(10); n   ios_base::sync_with_stdio(false); n   if (fopen( cf850a.in ,  r )) { n     freopen( cf850a.in ,  r , stdin); n   } n   cin >> N; n   for (int i = 0; i < N; i++) { n     for (int j = 0; j < 5; j++) { n       cin >> arr[j][i]; n     } n   } n   if (N >= 100) { n     cout <<  0 n ; n     return 0; n   } n   for (int i = 0; i < N; i++) { n     for (int j = 0; j < N; j++) { n       if (j == i) { n         continue; n       } n       for (int k = j + 1; k < N; k++) { n         if (k == i) { n           continue; n         } n         if (scalar(i, j, k) > 0) { n           bad[i] = true; n         } n       } n     } n   } n   for (int i = 0; i < N; i++) { n     if (!bad[i]) { n       ans++; n     } n   } n   cout << ans <<   n ; n   for (int i = 0; i < N; i++) { n     if (!bad[i]) { n       cout << i + 1 <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n void mergesort(char z[], int left, int right) { n   char temp[1000]; n   if (left >= right) { n     return; n   } n   int mid = (left + right) / 2; n   mergesort(z, left, mid); n   mergesort(z, mid + 1, right); n   int lenl = mid; n   int lenr = right; n   int i = left, j = mid + 1, k = left; n   while (i <= lenl && j <= lenr) { n     if (z[i] >= z[j]) { n       temp[k] = z[i]; n       k++; n       i++; n     } else { n       temp[k] = z[j]; n       k++; n       j++; n     } n   } n   while (i <= lenl) { n     temp[k] = z[i]; n     k++; n     i++; n   } n   while (j <= lenr) { n     temp[k] = z[j]; n     k++; n     j++; n   } n   for (int i = left; i <= right; i++) { n     z[i] = temp[i]; n   } n } n int main() { n   int a, b, c, d, e, f, g, h; n   char z[1000]; n   scanf( %d , &a); n   getchar(); n   for (int i = 0; i < a; i++) { n     scanf( %s , z); n     getchar(); n     b = strlen(z); n     mergesort(z, 0, b - 1); n     if (b == 1) { n       printf( YES n ); n     } else { n       for (int j = 0; j < b - 1; j++) { n         if ((z[j] - z[j + 1]) == 1) { n         } else { n           printf( NO n ); n           break; n         } n         if (j == b - 2) { n           printf( YES n ); n         } n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n string s; n vector<int> q; n int mo[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; n int h, w; n int data(int x, int y) { n   int tmp = 0; n   for (int i = 0; i < x - 1; i++) tmp += mo[i]; n   return tmp + y; n } n int cal(int x, int y, int z) { return x * 60 * 60 + y * 60 + z; } n int getpos(string s) { n   int p = data((s[5] -  0 ) * 10 + s[6] -  0 , (s[8] -  0 ) * 10 + s[9] -  0 ); n   int res = (p - 1) * 24 * 60 * 60; n   res += cal((s[11] -  0 ) * 10 + s[12] -  0 , (s[14] -  0 ) * 10 + s[15] -  0 , n              (s[17] -  0 ) * 10 + s[18] -  0 ); n   return res; n } n int main() { n   cin >> n >> m; n   h = 0; n   getline(cin, s); n   while (getline(cin, s)) { n     int t = getpos(s); n     q.push_back(t); n     while (h < q.size() && t - q[h] >= n) h++; n     if (q.size() - h >= m) { n       for (int i = 0; i <= 18; i++) cout << s[i]; n       cout << endl; n       return 0; n     } n   } n   cout << -1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n template <typename _Tp> n void read(_Tp &x) { n   char ch(getchar()); n   bool f(false); n   while (!isdigit(ch)) f |= ch == 45, ch = getchar(); n   x = ch & 15, ch = getchar(); n   while (isdigit(ch)) x = x * 10 + (ch & 15), ch = getchar(); n   if (f) x = -x; n } n template <typename _Tp, typename... Args> n void read(_Tp &t, Args &...args) { n   read(t); n   read(args...); n } n const int N = 100005, mod = 1000000007; n template <typename _Tp1, typename _Tp2> n inline void add(_Tp1 &a, _Tp2 b) { n   (a += b) >= mod && (a -= mod); n } n template <typename _Tp1, typename _Tp2> n inline void sub(_Tp1 &a, _Tp2 b) { n   (a -= b) < 0 && (a += mod); n } n long long ksm(long long a, long long b = mod - 2) { n   long long res = 1; n   while (b) { n     if (b & 1) res = res * a % mod; n     a = a * a % mod, b >>= 1; n   } n   return res; n } n long long fac[N], inv[N], ifac[N]; n void init() { n   fac[0] = fac[1] = inv[0] = inv[1] = ifac[0] = ifac[1] = 1; n   for (int i = 2; i < N; ++i) n     fac[i] = fac[i - 1] * i % mod, n     inv[i] = inv[mod % i] * (mod - mod / i) % mod, n     ifac[i] = ifac[i - 1] * inv[i] % mod; n } n inline long long C(int n, int m) { n   return n < m || m < 0 ? 0 : fac[n] * ifac[m] % mod * ifac[n - m] % mod; n } n int f[N]; n std::map<int, int> mp; n int cnt[N], pw[N]; n int main() { n   init(); n   int n, k; n   read(n, k); n   for (int i = 0; i <= n; ++i) pw[i] = ksm(k - 1, i); n   f[0] = 0; n   for (int i = 0; i < n; ++i) n     f[i + 1] = (1LL * (k - 1) * i % mod * f[i] % mod + 1) * inv[n - i] % mod; n   for (int i = 1; i <= n; ++i) add(f[i], f[i - 1]); n   int a = 0, s = 0; n   for (int i = 1, x; i <= n; ++i) { n     read(x); n     if (x != -1) n       ++mp[x]; n     else n       ++a; n   } n   for (auto it : mp) ++cnt[it.second]; n   cnt[0] += k - mp.size(); n   for (int i = 0; i <= n; ++i) n     if (cnt[i]) { n       int tmp = 0; n       for (int j = 0; j <= a; ++j) n         add(tmp, 1LL * f[i + j] * C(a, j) % mod * pw[a - j] % mod); n       add(s, 1LL * tmp * cnt[i] % mod); n     } n   s = ksm(k, mod - 1 - a) * s % mod; n   printf( %lld n , 1LL * (f[n] - s + mod) * n % mod); n   return 0; n } n


/* n Author: QAQAutoMaton n Lang: C++ n Code: C.cpp n Mail: lk@qaq-am.com n Blog: https://www.qaq-am.com/ n */ n #include<bits/stdc++.h> n #define debug(...) fprintf(stderr,__VA_ARGS__) n #define DEBUG printf( Passing [%s] in LINE %d n ,__FUNCTION__,__LINE__) n #define Debug debug( Passing [%s] in LINE %d n ,__FUNCTION__,__LINE__) n #define all(x) x.begin(),x.end() n #define x first n #define y second n using namespace std; n typedef unsigned uint; n typedef long long ll; n typedef unsigned long long ull; n typedef complex<double> cp; n typedef pair<int,int> pii; n int inf; n const double eps=1e-8; n const double pi=acos(-1.0); n template<class T,class T2>int chkmin(T &a,T2 b){return a>b?a=b,1:0;} n template<class T,class T2>int chkmax(T &a,T2 b){return a<b?a=b,1:0;} n template<class T>T sqr(T a){return a*a;} n template<class T,class T2>T mmin(T a,T2 b){return a<b?a:b;} n template<class T,class T2>T mmax(T a,T2 b){return a>b?a:b;} n template<class T>T aabs(T a){return a<0?-a:a;} n template<class T>int dcmp(T a,T b){return a>b;} n template<int *a>int cmp_a(int x,int y){return a[x]<a[y];} n template<class T>bool sort2(T &a,T &b){return a>b?swap(a,b),1:0;} n #define min mmin n #define max mmax n #define abs aabs n struct __INIT__{ n  __INIT__(){ n   fill((unsigned char*)&inf,(unsigned char*)&inf+sizeof(inf),0x3f); n  } n }__INIT___; n namespace io { n  const int SIZE = (1 << 21) + 1; n  char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c, qu[55]; int f, qr; n  // getchar n  #define gc() (iS == iT ? (iT = (iS = ibuf) + fread (ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++) n  // print the remaining part n  inline void flush () { n   fwrite (obuf, 1, oS - obuf, stdout); n   oS = obuf; n  } n  // putchar n  inline void putc (char x) { n   *oS ++ = x; n   if (oS == oT) flush (); n  } n  template<typename A> n  inline bool read (A &x) { n   for (f = 1, c = gc(); c <  0  || c >  9 ; c = gc()) if (c ==  - ) f = -1;else if(c==EOF)return 0; n   for (x = 0; c <=  9  && c >=  0 ; c = gc()) x = x * 10 + (c & 15); x *= f; n   return 1; n  } n  inline bool read (char &x) { n   while((x=gc())==   ||x==  n  || x==  r ); n   return x!=EOF; n  } n  inline bool read(char *x){ n   while((*x=gc())==  n  || *x==   ||*x==  r ); n   if(*x==EOF)return 0; n   while(!(*x==  n ||*x==   ||*x==  r ||*x==EOF))*(++x)=gc(); n   *x=0; n   return 1; n  } n  template<typename A,typename ...B> n  inline bool read(A &x,B &...y){ n   return read(x)&&read(y...); n  } n  template<typename A> n  inline bool write (A x) { n   if (!x) putc ( 0 ); if (x < 0) putc ( - ), x = -x; n   while (x) qu[++ qr] = x % 10 +  0 ,  x /= 10; n   while (qr) putc (qu[qr --]); n   return 0; n  } n  inline bool write (char x) { n   putc(x); n   return 0; n  } n  inline bool write(const char *x){ n   while(*x){putc(*x);++x;} n   return 0; n  } n  inline bool write(char *x){ n   while(*x){putc(*x);++x;} n   return 0; n  } n  template<typename A,typename ...B> n  inline bool write(A x,B ...y){ n   return write(x)||write(y...); n  } n  //no need to call flush at the end manually! n  struct Flusher_ {~Flusher_(){flush();}}io_flusher_; n } n using io :: read; n using io :: putc; n using io :: write; n char s[100005]; n int p[1005],q[1005],rp[1005]; n int t1,t2,t3,t4,t5,t6; n int a[1005][1005],b[1005][1005]; n int v[4]; n signed main(){ n #ifdef QAQAutoMaton  n  freopen( C.in , r ,stdin); n  freopen( C.out , w ,stdout); n #endif n  int t; n  read(t); n  for(;t;--t){ n   int n,m; n   read(n,m); n   for(int i=0;i<n;++i)for(int j=0;j<n;++j){ n    read(a[i][j]); n    --a[i][j]; n   } n   read(s+1); n   pii x={1,0},y={2,0},z={3,0}; n   for(int i=1;i<=m;++i){ n    switch(s[i]){ n     case  R :++y.y;break; n     case  L :--y.y;break; n     case  D :++x.y;break; n     case  U :--x.y;break; n     case  I :swap(y,z);break; n     case  C :swap(x,z);break; n    } n   } n   ((x.y%=n)+=n)%=n; n   ((y.y%=n)+=n)%=n; n   ((z.y%=n)+=n)%=n; n   for(int i=0;i<n;++i){ n    for(int j=0;j<n;++j){ n     v[1]=i;v[2]=j;v[3]=a[i][j]; n     b[(v[x.x]+x.y)%n][(v[y.x]+y.y)%n]=(v[z.x]+z.y)%n; n    } n   } n   for(int i=0;i<n;++i)for(int j=0;j<n;++j)write(b[i][j]+1,j==n-1?  n :   ); n   putc(  n ); n  } n  return 0; n } n  n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, bx, x; n   long long X = 0; n   cin >> n >> bx; n   for (int i = 1; i <= n; i++) { n     cin >> x; n     X += x * pow(bx, n - i); n   } n   int m, by, y; n   long long Y = 0; n   cin >> m >> by; n   for (int i = 1; i <= m; i++) { n     cin >> y; n     Y += y * pow(by, m - i); n   } n   if (X == Y) { n     cout <<  = ; n     return 0; n   } n   if (X > Y) { n     cout <<  > ; n     return 0; n   } n   cout <<  < ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long x; n   cin >> x; n   while (x--) { n     string str; n     cin >> str; n     long long n, b, m, v, finl, finl2; n     finl2 = 0; n     v = 0; n     b = 0; n     m = 0; n     v = 0; n     for (n = 0; n < str.size(); n++) { n       finl = (int)str[n] - 48; n       if (finl == 0) n         b++; n       else if (finl % 2 == 0) n         m++; n       finl2 = finl + finl2; n     } n     if (finl2 % 3 == 0) v = 1; n     if (b >= 2 && v == 1 || b >= 1 && m >= 1 && v == 1) n       cout <<  red ; n     else n       cout <<  cyan ; n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, si, mi, t; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> t; n     if (t == 1) { n       si = i; n     } n     if (t == n) { n       mi = i; n     } n   } n   if (si < mi) { n     cout << max(mi, n - 1 - si); n   } else { n     cout << max(si, n - 1 - mi); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 50005; n long long num[maxn]; n int main() { n   long long sum = 0, ans = 0; n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) cin >> num[i], sum += num[i]; n   if (n == 0) { n     cout <<  0  << endl; n     return 0; n   } n   sum = sum / n; n   for (int i = 0; i < n - 1; i++) { n     if (num[i] < sum) { n       ans += sum - num[i]; n       num[i + 1] -= (sum - num[i]); n     } else if (num[i] > sum) { n       ans += num[i] - sum; n       num[i + 1] += num[i] - sum; n     } n     num[i] = sum; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void mohammed() { n   ios::sync_with_stdio(0); n   ios_base::sync_with_stdio(0); n   cin.tie(0), cout.tie(0); n } n int main() { n   mohammed(); n   int n, x, y; n   map<int, int> mp; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> x >> y; n     mp[x]++; n     mp[y + 1]--; n   } n   x = 0; n   for (map<int, int>::iterator it = mp.begin(); it != mp.end(); it++) { n     it->second += x; n     if (it->second > 2) return cout <<  NO n , 0; n     x = it->second; n   } n   cout <<  YES n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_N = (int)2e5 + 7; n long long dp[MAX_N]; n long long num[MAX_N][2]; n long long dist[MAX_N][2]; n vector<int> a[MAX_N]; n long long ans = 0; n void go(int v, int pr = -1) { n   ++num[v][0]; n   for (int to : a[v]) { n     if (to == pr) continue; n     go(to, v); n     for (int i = 0; i < 2; ++i) n       for (int j = 0; j < 2; ++j) { n         ans += dist[v][i] * num[to][j] + dist[to][j] * num[v][i]; n         if (!i && !j) { n           ans += num[to][j] * num[v][i]; n         } n       } n     num[v][0] += num[to][1]; n     dist[v][0] += dist[to][1]; n     num[v][1] += num[to][0]; n     dist[v][1] += dist[to][0] + num[to][0]; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   int n; n   cin >> n; n   for (int i = 0; i < n - 1; ++i) { n     int x, y; n     cin >> x >> y; n     --x; n     --y; n     a[x].push_back(y); n     a[y].push_back(x); n   } n   go(0); n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxlongint = 2147483647; n const int inf = 1000000000; n int main() { n   int a, b, c, d, e, f, ans = 0, k = -1, n1, n2; n   cin >> a >> b >> c >> d >> e >> f; n   ans = a * b + c * d + e * f; n   for (n1 = 1; n1 <= 1000; n1++) n     if (n1 * n1 == ans) { n       k = n1; n       break; n     } n   if (k == -1) { n     cout << -1 << endl; n     return 0; n   } n   if (b == k) swap(a, b); n   if (d == k) swap(c, d); n   if (f == k) swap(e, f); n   if (a == k && c == k && e == k) { n     cout << k << endl; n     for (n1 = 1; n1 <= b; n1++) { n       for (n2 = 1; n2 <= k; n2++) putchar( A ); n       printf(  n ); n     } n     for (n1 = 1; n1 <= d; n1++) { n       for (n2 = 1; n2 <= k; n2++) putchar( B ); n       printf(  n ); n     } n     for (n1 = 1; n1 <= f; n1++) { n       for (n2 = 1; n2 <= k; n2++) putchar( C ); n       printf(  n ); n     } n     return 0; n   } n   int sd = 0; n   if (c == k) { n     sd = 1; n     swap(a, c); n     swap(b, d); n   } else if (e == k) { n     sd = 2; n     swap(a, e); n     swap(b, f); n   } n   if (a != k) { n     cout << -1 << endl; n     return 0; n   } n   if (d == k - b) swap(c, d); n   if (f == k - b) swap(e, f); n   if (c != k - b || e != k - b) { n     cout << -1 << endl; n     return 0; n   } n   { n     cout << k << endl; n     for (n1 = 1; n1 <= b; n1++) { n       for (n2 = 1; n2 <= k; n2++) putchar( A  + sd); n       printf(  n ); n     } n     for (n1 = 1; n1 <= k - b; n1++) { n       if (sd != 1) n         for (n2 = 1; n2 <= d; n2++) putchar( B ); n       else n         for (n2 = 1; n2 <= d; n2++) putchar( A ); n       if (sd != 2) n         for (n2 = 1; n2 <= f; n2++) putchar( C ); n       else n         for (n2 = 1; n2 <= f; n2++) putchar( A ); n       printf(  n ); n     } n     return 0; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   vector<int> v(n); n   multiset<int> list; n   for (int i = 0; i < n; i++) { n     cin >> v[i]; n   } n   for (int i = 0; i < n; i++) { n     int x; n     cin >> x; n     list.insert(x); n   } n   for (int i = 0; i < n; ++i) { n     auto current = list.lower_bound(n - v[i]); n     if (current == list.end()) { n       current = list.begin(); n     } n     cout << (v[i] + *current) % n <<    ; n     list.erase(current); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 10; n struct edge { n   int u, v; n   int adj(int x) { return u + v - x; } n }; n int n, c[N]; n vector<int> adj[N]; n vector<edge> e; n int lvl[N], pre[N], root; n vector<int> cycle; n void dfs1(int u) { n   for (auto ind : adj[u]) n     if (ind != pre[u]) { n       auto ei = e[ind]; n       int v = ei.adj(u); n       if (lvl[v]) { n         if ((lvl[u] + lvl[v]) % 2 == 0 && cycle.empty()) { n           for (int w = u; w != v; w = e[pre[w]].adj(w)) cycle.push_back(pre[w]); n           reverse((cycle).begin(), (cycle).end()); n           cycle.push_back(ind); n           root = v; n         } n       } else { n         lvl[v] = lvl[u] + 1; n         pre[v] = ind; n         dfs1(v); n       } n     } n } n long long int ans[N]; n bool vis[N]; n long long int dfs2(int u, int pre) { n   if (vis[u]) return 0; n   vis[u] = 1; n   long long int sum = 0; n   for (auto ind : adj[u]) n     if (ind != pre) { n       int v = e[ind].adj(u); n       sum += dfs2(v, ind); n     } n   if (pre != -1) ans[pre] = c[u] - sum; n   return c[u] - sum; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int m; n   cin >> n >> m; n   for (int i = 0; i < (int)(n); i++) cin >> c[i]; n   e.resize(m); n   for (int i = 0; i < (int)(m); i++) { n     cin >> e[i].u >> e[i].v; n     e[i].u--; n     e[i].v--; n     adj[e[i].u].push_back(i); n     adj[e[i].v].push_back(i); n   } n   bool ok = 1; n   for (int u = 0; u < (int)(n); u++) n     if (!lvl[u]) { n       cycle.clear(); n       lvl[u] = 1; n       pre[u] = -1; n       dfs1(u); n       if (cycle.empty()) { n         ok &= dfs2(u, -1) == 0; n       } else { n         auto delta = dfs2(root, -1) / 2; n         for (auto ind : cycle) { n           ans[ind] += delta; n           delta = -delta; n         } n       } n     } n   if (ok) { n     cout <<  YES n ; n     for (int i = 0; i < (int)(m); i++) cout << ans[i] <<   n ; n   } else n     cout <<  NO n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   cout << setprecision(10); n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     int x; n     cin >> x; n     if (x == 1) { n       cout <<  HARD n ; n       return 0; n     } n   } n   cout <<  EASY n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 41, K = 11; n bitset<40> a[N]; n vector<int> steps[K]; n int n, k; n bool ok = true; n void go(int pos, bitset<40> is_one, bitset<40> is_question) { n   if (pos == k) { n     if (is_question != 0) { n       ok = false; n       return; n     } n     int ones = is_one.count(); n     if (is_one != a[ones]) { n       ok = false; n       return; n     } n     return; n   } n   int ones = 0, zeros = 0, questions = 0; n   for (int i : steps[pos]) { n     if (is_question[i]) { n       questions++; n     } else if (is_one[i]) { n       ones++; n     } else { n       zeros++; n     } n     is_question[i] = 0; n     is_one[i] = 1; n   } n   for (int i = 0; i < zeros; i++) { n     is_one[steps[pos][i]] = 0; n   } n   go(pos + 1, is_one, is_question); n   for (int i = zeros; i < questions + zeros; i++) { n     is_one[steps[pos][i]] = 0; n     go(pos + 1, is_one, is_question); n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int _ = 1; n   while (_--) { n     cin >> n >> k; n     for (int i = 0; i <= n; i++) { n       a[i] = 0; n       for (int j = 0; j < i; j++) { n         a[i][n - j - 1] = 1; n       } n     } n     for (int i = 0; i < k; i++) { n       int q; n       cin >> q; n       while (q--) { n         int x; n         cin >> x; n         steps[i].push_back(--x); n       } n     } n     bitset<40> is_one = 0; n     bitset<40> is_question = (1ll << n) - 1; n     go(0, is_one, is_question); n     cout << (n == 1 || ok ?  ACCEPTED  :  REJECTED ) << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long xmax, ymax, sx, sy, i, j; n   cin >> xmax >> ymax >> sx >> sy; n   cout << sx <<     << sy << endl; n   for (i = sx - 1; i >= 1; i--) cout << i <<     << sy << endl; n   i++; n   for (j = sy + 1; j <= ymax; j++) cout << i <<     << j << endl; n   j--; n   for (j = sy - 1; j >= 1; j--) cout << i <<     << j << endl; n   j++; n   for (i = 2; i <= xmax; i++) { n     if (i % 2 == 0) { n       for (j = 1; j <= ymax; j++) { n         if (i <= sx && j == sy) n           continue; n         else n           cout << i <<     << j << endl; n       } n       j--; n     } else { n       for (j = ymax; j >= 1; j--) { n         if (i <= sx && j == sy) n           continue; n         else n           cout << i <<     << j << endl; n       } n       j++; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long i, m, n, x, y; n set<long long> a, b; n int main() { n   cin >> n >> m; n   for (i = 0; i < m; i++) { n     cin >> x >> y; n     a.insert(x); n     b.insert(y); n     cout << (n - a.size()) * (n - b.size()) <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1000000000; n const int MAX = 1000007; n int n; n long long a, b; n vector<int> primes(int x) { n   vector<int> p; n   for (int i = 2; i * i <= x; ++i) n     if (x % i == 0) { n       p.push_back(i); n       while (x % i == 0) { n         x /= i; n       } n     } n   if (x != 1) p.push_back(x); n   return p; n } n long long dp[MAX][3]; n long long solve(vector<int> c) { n   vector<int> p = primes(c[0]); n   vector<int> p1 = primes(c[0] - 1); n   for (int i = (0); i < (p1.size()); i++) p.push_back(p1[i]); n   p1 = primes(c[0] + 1); n   for (int i = (0); i < (p1.size()); i++) p.push_back(p1[i]); n   p1 = primes(c.back()); n   for (int i = (0); i < (p1.size()); i++) p.push_back(p1[i]); n   p1 = primes(c.back() + 1); n   for (int i = (0); i < (p1.size()); i++) p.push_back(p1[i]); n   p1 = primes(c.back() - 1); n   for (int i = (0); i < (p1.size()); i++) p.push_back(p1[i]); n   sort(p.begin(), p.end()); n   p.resize(unique(p.begin(), p.end()) - p.begin()); n   long long res = 1LL * INF * INF; n   for (int t = (0); t < (p.size()); t++) { n     int pr = p[t]; n     dp[0][0] = dp[0][1] = dp[0][2] = 0; n     for (int i = (0); i < (n); i++) { n       int val = min(c[i] % pr, pr - c[i] % pr); n       dp[i + 1][0] = dp[i + 1][1] = dp[i + 1][2] = 1LL * INF * INF; n       if (val <= 1) { n         dp[i + 1][2] = min(dp[i][2] + a * val, n                            min(dp[i][1] + a * val, dp[i][0] + a * val)); n       } n       dp[i + 1][1] = min(dp[i][0], dp[i][1]) + b; n       if (val <= 1) { n         dp[i + 1][0] = dp[i][0] + a * val; n       } n     } n     long long r = min(dp[n][2], min(dp[n][1], dp[n][0])); n     res = min(res, r); n   } n   return res; n } n int main() { n   cin >> n >> a >> b; n   swap(a, b); n   vector<int> c(n); n   for (int i = (0); i < (n); i++) { n     scanf( %d , &c[i]); n   } n   long long res = solve(c); n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[55][55]; n int query(int xa, int ya, int xb, int yb) { n   if (xa > xb || ya > yb) swap(xa, xb), swap(ya, yb); n   cout <<  ?   << xa + 1 <<     << ya + 1 <<     << xb + 1 <<     << yb + 1 n        << endl; n   int p; n   cin >> p; n   return p; n } n int h[2]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n; n   cin >> n; n   a[0][0] = 1, a[n - 1][n - 1] = 0; n   for (int i = 0; i < n; i++) { n     for (int j = i + 2; j < n; j += 2) { n       int p = query(i, j - 2, i, j); n       p ^= 1; n       a[i][j] = p ^ a[i][j - 2]; n       p = query(j - 2, i, j, i); n       p ^= 1; n       a[j][i] = p ^ a[j - 2][i]; n     } n     if (i < n - 1) { n       int p = query(i, i, i + 1, i + 1); n       p ^= 1; n       a[i + 1][i + 1] = p ^ a[i][i]; n     } n   } n   a[0][1] = 0; n   for (int i = 0; i < n; i++) { n     if (i & 1) { n       int p = query(i - 1, n - 2, i, n - 1); n       p ^= 1; n       a[i][n - 1] = a[i - 1][n - 2] ^ p; n       for (int j = n - 3; j >= 0; j -= 2) { n         int p = query(i, j, i, j + 2); n         p ^= 1; n         a[i][j] = a[i][j + 2] ^ p; n       } n     } else { n       if (i) { n         int p = query(i - 2, 1, i, 1); n         p ^= 1; n         a[i][1] = a[i - 2][1] ^ p; n       } n       for (int j = 3; j < n; j += 2) { n         int p = query(i, j - 2, i, j); n         p ^= 1; n         a[i][j] = a[i][j - 2] ^ p; n       } n     } n   } n   int ind; n   for (int i = 0; i < n - 2; i++) { n     if (i % 2 == 0 && a[i + 2][i + 2] == 0 && a[i][i] == 1) { n       ind = i; n       break; n     } n   } n   if (!(a[ind][ind] == a[ind][ind + 2]) ^ n       (a[ind][ind + 1] == a[ind + 1][ind + 2])) { n     int p = query(ind, ind, ind + 1, ind + 2); n     if (p) { n       h[a[ind + 1][ind + 2]] = 1; n       h[1 - a[ind + 1][ind + 2]] = 0; n     } else { n       h[a[ind + 1][ind + 2]] = 0; n       h[1 - a[ind + 1][ind + 2]] = 1; n     } n   } else { n     int p = query(ind, ind + 1, ind + 2, ind + 2); n     if (p) { n       h[a[ind][ind + 1]] = 0; n       h[1 - a[ind][ind + 1]] = 1; n     } else { n       h[a[ind][ind + 1]] = 1; n       h[1 - a[ind][ind + 1]] = 0; n     } n   } n   cout <<  !  << endl; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       if ((i + j) % 2) { n         cout << h[a[i][j]]; n       } else { n         cout << a[i][j]; n       } n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char a[100][40]; n char b[100][40]; n int at[1445], use[100], ok[100], idx[100], pos[100], sum[100], cnt; n int dp[43205], ans_dd, ans_hh, ans_mm; n int pt[43205][100]; n void run_to(int t) { n   while (1) { n     int next_dd = ans_dd; n     int next_hh = ans_hh; n     int next_mm = ans_mm; n     if (++next_mm == 60) { n       next_mm = 0; n       if (++next_hh == 24) { n         next_hh = 0; n         next_dd++; n       } n     } n     if ((next_dd - 1) * cnt + at[next_hh * 60 + next_mm] >= t) break; n     ans_dd = next_dd; n     ans_hh = next_hh; n     ans_mm = next_mm; n   } n } n void output(int t, int i, int cnt) { n   if (~i) { n     int x = pt[t][i]; n     if (~x) { n       output(t - use[idx[x]], i - 1, cnt + 1); n       run_to(t - use[idx[x]] + 1); n       printf( %d %d %02d:%02d , x + 1, ans_dd, ans_hh, ans_mm); n       run_to(t); n       printf(  %d %02d:%02d n , ans_dd, ans_hh, ans_mm); n     } else { n       output(t, i - 1, cnt); n     } n   } else { n     printf( %d n , cnt); n   } n } n int main() { n   int m, n, all, hh, mm; n   scanf( %d%d%d , &m, &n, &all); n   for (int i = 0; i < m; i++) scanf( %s , a[i]); n   for (int i = 0; i < m; i++) scanf( %d , use + i); n   for (int i = 0; i < 4; i++) { n     scanf( %d:%d- , &hh, &mm); n     int st = hh * 60 + mm; n     scanf( %d:%d , &hh, &mm); n     int ed = hh * 60 + mm; n     for (int x = st; x <= ed; x++) at[x] = -1; n   } n   for (int x = 0; x < 1440; x++) n     if (~at[x]) at[x] = cnt++; n   all *= at[1440] = cnt; n   for (int x = 1439; ~x; x--) n     if (at[x] < 0) at[x] = at[x + 1]; n   for (int i = 0; i < n; i++) { n     scanf( %s , b[i]); n     for (idx[i] = 0; idx[i] < m; idx[i]++) n       if (!strcmp(b[i], a[idx[i]])) break; n     if (idx[i] != m) ok[i] = 1; n     scanf( %d%d:%d%d , pos + i, &hh, &mm, sum + i); n     pos[i] = (pos[i] - 1) * cnt + at[hh * 60 + mm]; n   } n   for (int i = 0; i < n; i++) { n     int k = -1; n     for (int j = 0; j < n; j++) n       if (ok[j] && (k < 0 || pos[j] < pos[k])) k = j; n     for (int x = all; ~x; x--) pt[x][i] = -1; n     for (int x = all; ~x; x--) { n       int t = x + use[idx[k]]; n       if (t <= pos[k] && sum[k] + dp[x] > dp[t]) { n         dp[t] = sum[k] + dp[x]; n         pt[t][i] = k; n       } n     } n     ok[k] = 0; n   } n   int x = max_element(dp, dp + all + 1) - dp; n   printf( %d n , dp[x]); n   output(x, n - 1, 0); n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f; n const long long INF = 2e18; n const int N = 11234; n int n, m; n set<string> st; n char s[N]; n int dp[N][4]; n void dfs(int pos, string pre, int len) { n   if (pos - 1 < 5) return; n   if (dp[pos][len]) return; n   dp[pos][len] = 1; n   string str =   ; n   for (int i = 1; i >= 0; --i) str += s[pos - i]; n   if (str != pre) { n     st.insert(str); n     dfs(pos - 2, str, 2); n   } n   if (pos - 2 >= 5) { n     str =   ; n     for (int i = 2; i >= 0; --i) str += s[pos - i]; n     if (str != pre) { n       st.insert(str); n       dfs(pos - 3, str, 3); n     } n   } n } n int main() { n   while (scanf( %s , s) != EOF) { n     int i, j; n     memset(dp, 0, sizeof(dp)); n     st.clear(); n     n = strlen(s); n     if (n < 5) n       puts( 0 ); n     else { n       dfs(n - 1,   , 0); n       printf( %d n , st.size()); n       set<string>::iterator it = st.begin(); n       for (; it != st.end(); ++it) { n         cout << *it << endl; n       } n     } n   } n } n


#include <bits/stdc++.h> n template <class C, class E> n inline bool contains(const C &container, const E &element) { n   return container.find(element) != container.end(); n } n template <class T> n inline void checkmin(T &a, T b) { n   if (b < a) a = b; n } n template <class T> n inline void checkmax(T &a, T b) { n   if (b > a) a = b; n } n using namespace std; n class UnionFind { n  public: n   UnionFind(int elements) : _subsets(elements), _setCount(elements) { n     for (int i = 0; i < elements; ++i) { n       _subsets[i].parent = i; n       _subsets[i].rank = 0; n     } n   } n   int Find(int i) { n     if (_subsets[i].parent != i) _subsets[i].parent = Find(_subsets[i].parent); n     return _subsets[i].parent; n   } n   void Union(int x, int y) { n     int xroot = Find(x); n     int yroot = Find(y); n     if (xroot == yroot) return; n     --_setCount; n     if (_subsets[xroot].rank < _subsets[yroot].rank) n       _subsets[xroot].parent = yroot; n     else if (_subsets[xroot].rank > _subsets[yroot].rank) n       _subsets[yroot].parent = xroot; n     else { n       _subsets[yroot].parent = xroot; n       _subsets[xroot].rank++; n     } n   } n   struct subset { n     int parent; n     int rank; n   }; n   void Trace() { n     for (int i = (0), _b = ((_subsets.size()) - 1); i <= _b; i++) n       cout << i <<  :  << Find(i) <<   | ; n     cout << endl; n   } n   vector<subset> _subsets; n   int _setCount; n }; n vector<int> res; n struct Node { n   Node(int v) { n     l = r = NULL; n     val = v; n   } n   Node(Node *_l, Node *_r) { n     l = _l; n     r = _r; n   } n   Node *l, *r; n   int val; n   void Solve() { n     if (l == NULL) { n       cout << val <<    ; n     } else { n       l->Solve(); n       r->Solve(); n     } n   } n }; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cout << std::setprecision(15); n   cout << std::fixed; n   int n; n   cin >> n; n   res.reserve(n); n   UnionFind uf(n + 1); n   vector<Node *> d(n + 1); n   for (int i = (1), _b = (n); i <= _b; i++) { n     d[i] = new Node(i); n   } n   for (int i = (0), _b = ((n - 1) - 1); i <= _b; i++) { n     int a, b; n     cin >> a >> b; n     int ra = uf.Find(a); n     int rb = uf.Find(b); n     Node *na = d[ra]; n     Node *nb = d[rb]; n     assert(na != NULL); n     assert(nb != NULL); n     d[ra] = NULL; n     d[rb] = NULL; n     uf.Union(ra, rb); n     int nr = uf.Find(ra); n     Node *nn = new Node(na, nb); n     d[nr] = nn; n   } n   int rootR = uf.Find(1); n   d[rootR]->Solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long tc, n, m, a, b, k; n string str; n char c; n set<long long> s; n map<long long, long long> mp; n int main() { n   int k2, k3, k5, k6, ans = 0; n   cin >> k2 >> k3 >> k5 >> k6; n   int x = min(k2, k5); n   x = min(x, k6); n   ans += 256 * x; n   k2 -= x; n   x = min(k2, k3); n   ans += 32 * x; n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n string inp[100005]; n int num[100005]; n int tl; n int ch[1200005][27]; n int fail[1200005], len[1200005], elen[1200005]; n long long cost[1200005]; n int rt = 1, tot = 1, last = 1; n void add(int x, int c, int _elen) { n   int p = last; n   int np = ++tot; n   len[np] = len[p] + 1; n   cost[np] = c; n   elen[np] = _elen; n   last = np; n   while (p && !ch[p][x]) { n     ch[p][x] = np; n     p = fail[p]; n   } n   if (!p) { n     fail[np] = rt; n   } else { n     int q = ch[p][x]; n     if (len[q] == len[p] + 1) { n       fail[np] = q; n     } else { n       int nq = ++tot; n       memcpy(ch[nq], ch[q], sizeof(ch[q])); n       len[nq] = len[p] + 1; n       fail[nq] = fail[q]; n       fail[np] = fail[q] = nq; n       while (p && ch[p][x] == q) { n         ch[p][x] = nq; n         p = fail[p]; n       } n     } n   } n } n int freq[600005], tpsort[1200005]; n void TopoSort() { n   for (int i = (1); i <= (tot); i += (1)) freq[len[i]]++; n   for (int i = (1); i <= (tl); i += (1)) freq[i] += freq[i - 1]; n   for (int i = (tot); i >= (1); i -= (1)) tpsort[--freq[len[i]]] = i; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n; n   cin >> n; n   for (int i = (1); i <= (n); i += (1)) cin >> inp[i]; n   for (int i = (1); i <= (n); i += (1)) cin >> num[i]; n   for (int i = (0); i <= (1200005 - 1); i += (1)) elen[i] = 1000000000; n   tl = 0; n   for (int i = (1); i <= (n); i += (1)) { n     int t = inp[i].length(); n     for (int j = (0); j <= (t - 1); j += (1)) { n       add(inp[i][j] -  a , num[i], j + 1); n     } n     add(26, 0, 0); n     tl += t + 1; n   } n   TopoSort(); n   long long mx = 0; n   for (int i = (tot); i >= (1); i -= (1)) { n     int u = tpsort[i]; n     cost[fail[u]] += cost[u]; n     elen[fail[u]] = min(elen[fail[u]], elen[u]); n     if (elen[u] > len[fail[u]]) { n       mx = max(mx, cost[u] * min(elen[u], len[u])); n     } n   } n   printf( %lld n , mx); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int dx[] = {-1, 0, 1, 0}; n const int dy[] = {0, -1, 0, 1}; n inline int read() { n   int ans = 0; n   bool f = 1; n   char ch = getchar(); n   while (!isdigit(ch)) f ^= ch ==  - , ch = getchar(); n   while (isdigit(ch)) ans = ((ans << 2) + ans << 1) + (ch ^ 48), ch = getchar(); n   return f ? ans : -ans; n } n long long n, m; n bool CanMove(long long x, long long y) { n   return (x >= 0 && x < n && y >= 0 && y < m); n } n long long DFS1(long long x, long long y, long long sx, long long sy) { n   x--, y--; n   sx--, sy--; n   long long val[n][m]; n   queue<pair<long long, long long> > q; n   for (long long i = 0; i < n; i++) n     for (long long j = 0; j < m; j++) val[i][j] = 0; n   q.push(make_pair(x, y)); n   val[x][y] = 1; n   while (!q.empty()) { n     pair<long long, long long> p = q.front(); n     q.pop(); n     long long X = p.first, Y = p.second; n     if (CanMove(X + 1, Y + 2) && val[X + 1][Y + 2] == 0) { n       q.push(make_pair(X + 1, Y + 2)); n       val[X + 1][Y + 2] = val[X][Y] + 1; n     } n     if (CanMove(X + 1, Y - 2) && val[X + 1][Y - 2] == 0) { n       q.push(make_pair(X + 1, Y - 2)); n       val[X + 1][Y - 2] = val[X][Y] + 1; n     } n     if (CanMove(X - 1, Y + 2) && val[X - 1][Y + 2] == 0) { n       q.push(make_pair(X - 1, Y + 2)); n       val[X - 1][Y + 2] = val[X][Y] + 1; n     } n     if (CanMove(X - 1, Y - 2) && val[X - 1][Y - 2] == 0) { n       q.push(make_pair(X - 1, Y - 2)); n       val[X - 1][Y - 2] = val[X][Y] + 1; n     } n     if (CanMove(X + 2, Y + 1) && val[X + 2][Y + 1] == 0) { n       q.push(make_pair(X + 2, Y + 1)); n       val[X + 2][Y + 1] = val[X][Y] + 1; n     } n     if (CanMove(X + 2, Y - 1) && val[X + 2][Y - 1] == 0) { n       q.push(make_pair(X + 2, Y - 1)); n       val[X + 2][Y - 1] = val[X][Y] + 1; n     } n     if (CanMove(X - 2, Y + 1) && val[X - 2][Y + 1] == 0) { n       q.push(make_pair(X - 2, Y + 1)); n       val[X - 2][Y + 1] = val[X][Y] + 1; n     } n     if (CanMove(X - 2, Y - 1) && val[X - 2][Y - 1] == 0) { n       q.push(make_pair(X - 2, Y - 1)); n       val[X - 2][Y - 1] = val[X][Y] + 1; n     } n   } n   return val[sx][sy]; n } n vector<pair<long long, long long> > DFS2(long long x, long long y, long long sx, n                                          long long sy) { n   x--, y--; n   sx--, sy--; n   long long val[n][m]; n   pair<long long, long long> pre[n][m]; n   for (long long i = 0; i < n; i++) n     for (long long j = 0; j < m; j++) val[i][j] = 0; n   vector<pair<long long, long long> > v; n   v.clear(); n   queue<pair<long long, long long> > q; n   q.push(make_pair(x, y)); n   val[x][y] = 1; n   while (!q.empty()) { n     pair<long long, long long> pp = q.front(); n     q.pop(); n     long long X = pp.first, Y = pp.second; n     if (CanMove(X + 1, Y + 2) && val[X + 1][Y + 2] == 0) { n       q.push(make_pair(X + 1, Y + 2)); n       val[X + 1][Y + 2] = val[X][Y] + 1; n       pre[X + 1][Y + 2] = make_pair(X, Y); n     } n     if (CanMove(X + 1, Y - 2) && val[X + 1][Y - 2] == 0) { n       q.push(make_pair(X + 1, Y - 2)); n       val[X + 1][Y - 2] = val[X][Y] + 1; n       pre[X + 1][Y - 2] = make_pair(X, Y); n     } n     if (CanMove(X - 1, Y + 2) && val[X - 1][Y + 2] == 0) { n       q.push(make_pair(X - 1, Y + 2)); n       val[X - 1][Y + 2] = val[X][Y] + 1; n       pre[X - 1][Y + 2] = make_pair(X, Y); n     } n     if (CanMove(X - 1, Y - 2) && val[X - 1][Y - 2] == 0) { n       q.push(make_pair(X - 1, Y - 2)); n       val[X - 1][Y - 2] = val[X][Y] + 1; n       pre[X - 1][Y - 2] = make_pair(X, Y); n     } n     if (CanMove(X + 2, Y + 1) && val[X + 2][Y + 1] == 0) { n       q.push(make_pair(X + 2, Y + 1)); n       val[X + 2][Y + 1] = val[X][Y] + 1; n       pre[X + 2][Y + 1] = make_pair(X, Y); n     } n     if (CanMove(X + 2, Y - 1) && val[X + 2][Y - 1] == 0) { n       q.push(make_pair(X + 2, Y - 1)); n       val[X + 2][Y - 1] = val[X][Y] + 1; n       pre[X + 2][Y - 1] = make_pair(X, Y); n     } n     if (CanMove(X - 2, Y + 1) && val[X - 2][Y + 1] == 0) { n       q.push(make_pair(X - 2, Y + 1)); n       val[X - 2][Y + 1] = val[X][Y] + 1; n       pre[X - 2][Y + 1] = make_pair(X, Y); n     } n     if (CanMove(X - 2, Y - 1) && val[X - 2][Y - 1] == 0) { n       q.push(make_pair(X - 2, Y - 1)); n       val[X - 2][Y - 1] = val[X][Y] + 1; n       pre[X - 2][Y - 1] = make_pair(X, Y); n     } n   } n   while (sx != x || sy != y) { n     v.push_back(make_pair(sx + 1, sy + 1)); n     pair<long long, long long> pp = pre[sx][sy]; n     sx = pp.first, sy = pp.second; n   } n   return v; n } n vector<pair<long long, long long> > v; n long long x, y, sx, sy; n long long fl1, fl2; n int main(int argc, char const *argv[]) { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n >> m >> x >> y >> sx >> sy; n   fl1 = ((x + y) % 2 != (sx + sy) % 2); n   long long aa = DFS1(x, y, n / 2, m / 2); n   long long bb = DFS1(sx, sy, n / 2, m / 2); n   long long cc = DFS1(x, y, n / 2 + 1, m / 2); n   long long dd = DFS1(sx, sy, n / 2 + 1, m / 2); n   if (fl1 == 0) n     fl2 = ((dd < aa) || (bb <= aa)); n   else n     fl2 = (!((aa <= dd) || ((cc - 1) <= dd))); n   cout << (fl2 ?  BLACK  :  WHITE ) << endl; n   if (fl2) { n     if (fl1) { n       v = DFS2(sx, sy, n / 2 + 1, m / 2); n       for (long long i = (long long)v.size() - 1; i >= 0; i--) { n         long long a, b; n         cin >> a >> b; n         cout << v[i].first <<     << v[i].second << endl; n       } n     } else { n       if (dd < aa) { n         v = DFS2(sx, sy, n / 2 + 1, m / 2); n         for (long long i = (long long)v.size() - 1; i >= 0; i--) { n           long long a, b; n           cin >> a >> b; n           cout << v[i].first <<     << v[i].second << endl; n           if ((a == v[i].first) && (b == v[i].second)) return 0; n         } n       } else { n         v = DFS2(sx, sy, n / 2, m / 2); n         for (long long i = (long long)v.size() - 1; i >= 0; i--) { n           long long a, b; n           cin >> a >> b; n           long long c, d; n           c = abs(sx - a); n           d = abs(sy - b); n           if ((c == 1 && d == 2) || (c == 2 && d == 1)) { n             cout << a <<     << b << endl; n             return 0; n           } else { n             cout << v[i].first <<     << v[i].second << endl; n             sx = v[i].first, sy = v[i].second; n           } n         } n         long long a, b; n         cin >> a >> b; n         long long c, d; n         c = abs(sx - a); n         d = abs(sy - b); n         if ((c == 1 && d == 2) || (c == 2 && d == 1)) { n           cout << a <<     << b << endl; n           return 0; n         } else { n           sx++, sy += 2; n           cout << sx <<     << sy << endl; n           cin >> a >> b; n           c = abs(sx - a); n           d = abs(sy - b); n           if ((c == 1 && d == 2) || (c == 2 && d == 1)) { n             cout << a <<     << b << endl; n             return 0; n           } else { n             sx -= 2; n             sy--; n             cout << sx <<     << sy << endl; n             sx += 2; n             sy--; n             cin >> a >> b; n             cout << sx <<     << sy << endl; n           } n         } n       } n     } n   } else { n     if (!fl1) { n       v = DFS2(x, y, n / 2, m / 2); n       cout << v[v.size() - 1].first <<     << v[v.size() - 1].second << endl; n       for (long long i = (long long)v.size() - 2; i >= 0; i--) { n         long long a, b; n         cin >> a >> b; n         cout << v[i].first <<     << v[i].second << endl; n       } n     } else { n       if (aa <= dd) { n         v = DFS2(x, y, n / 2, m / 2); n         cout << v[v.size() - 1].first <<     << v[v.size() - 1].second << endl; n         if (v[v.size() - 1].first == x && v[v.size() - 1].second == y) return 0; n         for (long long i = (long long)v.size() - 2; i >= 0; i--) { n           long long a, b; n           cin >> a >> b; n           cout << v[i].first <<     << v[i].second << endl; n           if (v[i].first == x && v[i].second == y) return 0; n         } n       } else { n         v = DFS2(x, y, n / 2 + 1, m / 2); n         long long P = sx, Q = sy; n         long long X = x, Y = y; n         for (long long i = (long long)v.size() - 1; i >= 0; i--) { n           long long c, d; n           c = abs(X - P); n           d = abs(Y - Q); n           if ((c == 1 && d == 2) || (c == 2 && d == 1)) { n             cout << P <<     << Q << endl; n             return 0; n           } else { n             cout << v[i].first <<     << v[i].second << endl; n             cin >> P >> Q; n             X = v[i].first; n             Y = v[i].second; n           } n         } n         long long c, d; n         c = abs(X - P); n         d = abs(Y - Q); n         if ((c == 1 && d == 2) || (c == 2 && d == 1)) { n           cout << P <<     << Q << endl; n           return 0; n         } else { n           X -= 2, Y++; n           cout << X <<     << Y << endl; n           cin >> P >> Q; n           c = abs(X - P); n           d = abs(Y - Q); n           if ((c == 1 && d == 2) || (c == 2 && d == 1)) { n             cout << P <<     << Q << endl; n             return 0; n           } else { n             X += 2, Y++; n             cout << X <<     << Y << endl; n             cin >> P >> Q; n             X--, Y -= 2; n             cout << X <<     << Y << endl; n             return 0; n           } n         } n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 2e5 + 10; n int col[N], par[N], to[N]; n int num = 0; n void dfs(int u, int from) { n   if (col[u] == 2) return; n   if (col[u] == 1) { n     ++num; n     return; n   } n   col[u] = 1; n   par[u] = from; n   dfs(to[u], u); n   col[u] = 2; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   int n, cnt = 0; n   cin >> n; n   for (int i = 1; i <= n; ++i) cin >> to[i]; n   int b[N]; n   for (int i = 0; i < n; ++i) { n     int x; n     cin >> x; n     if (x) ++cnt; n   } n   for (int i = 1; i <= n; ++i) n     if (!col[i]) dfs(i, -1); n   if (num == 1) num = 0; n   cout << num + ((cnt % 2) ? 0 : 1); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, k, q; n   long long int i, j; n   cin >> n >> k >> q; n   long long int arr[n][2]; n   for (i = 0; i < n; i++) { n     cin >> arr[i][0] >> arr[i][1]; n   } n   long long int temp[200010]; n   for (i = 0; i < 200001; i++) { n     temp[i] = 0; n   } n   for (i = 0; i < n; i++) { n     temp[arr[i][0]] = temp[arr[i][0]] + 1; n     temp[arr[i][1] + 1] = temp[arr[i][1] + 1] - 1; n   } n   for (i = 1; i < 200001; i++) { n     temp[i] = temp[i - 1] + temp[i]; n   } n   long long int ans[200010]; n   ans[0] = 0; n   for (i = 1; i < 200001; i++) { n     if (temp[i] >= k) n       ans[i] = ans[i - 1] + 1; n     else n       ans[i] = ans[i - 1]; n   } n   int s, e; n   while (q--) { n     cin >> s >> e; n     cout << ans[e] - ans[s - 1] << endl; n   } n   return (0); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int i, j; n   int arr1[n], arr2[n], p[n]; n   for (i = 1; i <= n; i++) cin >> arr1[i]; n   for (i = 1; i <= n; i++) cin >> arr2[i]; n   for (i = 1; i <= n; i++) p[arr1[i]] = arr2[i]; n   for (i = 1; i <= n; i++) cout << p[i] <<    ; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, k; n   cin >> n >> k; n   long long x = __builtin_popcount(n); n   if (k > n || k < x) { n     cout <<  NO  << endl; n   } else { n     cout <<  YES n ; n     k = k - x; n     long long count[30] = {0}; n     int i = 0; n     int a = n; n     n = 0; n     while (a > 0) { n       count[i] = a % 2; n       n++; n       a = a / 2; n       i++; n     } n     long long cur = 29; n     while (k--) { n       if (count[cur]) { n         count[cur - 1] += 2; n         count[cur]--; n       } else if (count[cur] == 0) { n         cur = cur - 1; n         k++; n       } n     } n     cur = 1; n     for (int i = 0; i < 30; i++) { n       for (int j = 0; j < count[i]; j++) { n         cout << cur <<    ; n       } n       cur *= 2; n     } n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long n; n   cin >> n; n   vector<vector<long long>> adj(n + 1, vector<long long>(n + 1, 0)); n   for (long long i = 1; i <= n; i++) { n     for (long long j = 1; j <= n; j++) { n       cin >> adj[i][j]; n     } n   } n   vector<long long> del(n + 1), v, ans; n   for (long long i = 1; i <= n; i++) cin >> del[i]; n   for (long long k = n; k >= 1; k--) { n     v.push_back(del[k]); n     for (long long i = 1; i <= n; i++) { n       for (long long j = 1; j <= n; j++) { n         adj[i][j] = min(adj[i][j], adj[i][del[k]] + adj[del[k]][j]); n       } n     } n     long long sum = 0; n     for (long long i = 0; i < v.size(); i++) { n       for (long long j = 0; j < v.size(); j++) { n         sum += adj[v[i]][v[j]]; n       } n     } n     ans.push_back(sum); n   } n   for (long long i = ans.size() - 1; i >= 0; i--) { n     cout << ans[i] <<    ; n   } n   cout << endl; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long qu = 1; n   for (long long cas = 1; cas <= qu; cas++) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long a, b; n   cin >> a >> b; n   if (b - a >= 5l) n     printf( 0 ); n   else { n     long long p = 1l, t = b; n     while (t > a && t > 0) { n       p *= t; n       p %= 10; n       t--; n     } n     printf( %I64d , p); n   } n } n


#include <bits/stdc++.h> n int n; n int p[512]; n int c[512][512]; n int a[512]; n int main() { n   int i, j, k; n   scanf( %d , &n); n   for (i = 1; i <= n; i++) { n     for (j = 1; j <= n; j++) { n       scanf( %d , &c[i][j]); n     } n   } n   for (i = 1; i <= n; i++) { n     scanf( %d , &p[i]); n   } n   for (i = 1; i <= n; i++) { n     for (j = 1; j <= n; j++) { n       if (c[i][j] == i) continue; n       for (k = 1; k <= n; k++) { n         if (p[k] == c[i][j]) break; n         if ((p[k] < c[i][j]) && (p[k] != i)) break; n       } n       if (p[k] == c[i][j]) { n         a[i] = c[i][j]; n         break; n       } n     } n   } n   for (i = 1; i < n; i++) printf( %d  , a[i]); n   printf( %d n , a[n]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long int m = 1000000007; n int prsb(long long int n) { n   int pos = 0; n   long long int m = 1; n   while (!(n & m)) { n     m = m << 1; n     pos++; n   } n   return pos; n } n int main() { n   int t; n   cin >> t; n   while (t--) { n     long long int a, b; n     cin >> a >> b; n     long long int x, y, z; n     int n, c = 0; n     x = min(a, b); n     y = max(a, b); n     z = y / x; n     if (y % x != 0) { n       c = -1; n     } else if (z == 1) { n       c = 0; n     } else if (z && (!(z & (z - 1)))) { n       n = prsb(z); n       c += n / 3; n       n = n % 3; n       c += n / 2; n       n = n % 2; n       c += n / 1; n     } else { n       c = -1; n     } n     cout << c << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long i, a, b, c, d, mn, mx; n   cin >> a >> b >> c >> d; n   mn = min(b, d); n   b -= mn; n   d -= mn; n   if (d > b) { n     for (i = 0; i < 1000; i++) { n       if (d % a == 0) { n         cout << d + mn; n         return 0; n       } n       d += c; n     } n   } else if (d < b) { n     for (i = 0; i < 1000; i++) { n       if (b % c == 0) { n         cout << b + mn; n         return 0; n       } n       b += a; n     } n   } else if (d == b) { n     cout << d + mn; n     return 0; n   } n   cout <<  -1 ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct T { n   double x, y; n }; n T ball; n double g1, g2, r, h; n double ll, rr, eps = 1e-8; n int main() { n   scanf( %lf%lf%lf%lf%lf%lf , &g1, &g2, &h, &ball.x, &ball.y, &r); n   ll = 0; n   rr = ball.x; n   while (rr - ll > eps) { n     double m = (ll + rr) / 2; n     double xl = m - (ball.x - m); n     double a, b, c; n     a = ball.y - (h - r); n     b = m - xl; n     c = -a * m - b * (h - r); n     double yl = -c / b; n     if (yl < g1) n       rr = m; n     else if (yl > g2) n       ll = m; n     else { n       if ((b * g2 + c) / sqrt(a * a + b * b) < r) n         ll = m; n       else if (yl - r < g1) n         rr = m; n       else { n         printf( %.10lf , m); n         return 0; n       } n     } n   } n   printf( -1 ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int NMax = 301000, MMax = 200; n int N, A[NMax]; n map<int, int> S; n long long sum[NMax]; n vector<int> R; n int main() { n   scanf( %d , &N); n   for (int i = 1; i <= N; i++) { n     scanf( %d , A + i); n     S[A[i]] = max(S[A[i]], i); n     if (A[i] >= 0) n       sum[i] = sum[i - 1] + A[i]; n     else n       sum[i] = sum[i - 1]; n   } n   long long maxx = -(~0uLL >> 1); n   pair<int, int> ans; n   for (int i = 1; i <= N; i++) { n     int t = S[A[i]]; n     if (t > i) { n       long long first = sum[t] - sum[i - 1]; n       if (A[i] < 0) first += (long long)A[i] * 2; n       if (first > maxx) { n         maxx = first; n         ans = make_pair(i, t); n       } n     } n   } n   printf( %I64d  , maxx); n   for (int i = 1; i < ans.first; i++) R.push_back(i); n   for (int i = ans.first + 1; i < ans.second; i++) n     if (A[i] < 0) R.push_back(i); n   for (int i = ans.second + 1; i <= N; i++) R.push_back(i); n   printf( %d n , (int)R.size()); n   for (int i = 0; i < R.size(); i++) printf( %d  , R[i]); n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long toInt(string s) { n   long v; n   istringstream sin(s); n   sin >> v; n   return v; n } n template <class T> n inline string toString(T x) { n   ostringstream sout; n   sout << x; n   return sout.str(); n } n template <class T> n inline T sqr(T x) { n   return x * x; n } n long long pw(long long x, long long y) { n   long long ans = 1; n   for (long long i = 0; i < y; i++) { n     ans = ans * x; n   } n   return ans; n } n long long factorial(long long x) { n   long long ans = 1; n   for (int i = 1; i < x + 1; i++) { n     ans = ans * i; n   } n   return ans; n } n long long combination(long long x, long long y) { n   return factorial(x) / factorial(y) / factorial(x - y); n } n long long permutation(long long x, long long y) { n   return factorial(x) / factorial(x - y); n } n string flip(string s) { n   string ss; n   for (int i = 0; i < s.size(); i++) { n     ss = toString(s[i]) + ss; n   } n   return ss; n } n long long mod(long long a, long long b) { n   if (a % b == 0) { n     return 0; n   } else { n     return a % b; n   } n } n int main() { n   long long n; n   cin >> n; n   vector<long long> v(n); n   long long a, b; n   for (long long i = 0; i < n - 1; i++) { n     cin >> a >> b; n     v[a - 1]++; n     v[b - 1]++; n   } n   long long ans = 0; n   for (long long i = 0; i < n; i++) { n     if (v[i] >= 2) { n       ans += combination(v[i], 2); n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int deg[200040], erased[200500], depth[200040]; n void dfs(int x, int d, vector<vector<int>>& adj, int pa) { n   for (auto u : adj[x]) n     if (u != pa) dfs(u, d + 1, adj, x); n   depth[x] = d; n } n int main() { n   int n; n   cin >> n; n   vector<vector<int>> adj(n + 1); n   int root; n   for (int i = 1; i <= n; i++) { n     int p; n     cin >> p; n     if (p == 0) { n       root = i; n       continue; n     } n     adj[p].push_back(i); n     adj[i].push_back(p); n     deg[i]++; n     deg[p]++; n   } n   dfs(root, 0, adj, 0); n   set<pair<int, int>> S; n   for (int i = 1; i <= n; i++) { n     if (deg[i] % 2 == 0) S.insert({-depth[i], i}); n   } n   vector<int> res; n   while (!S.empty()) { n     int x = (*S.begin()).second; n     S.erase({-depth[x], x}); n     erased[x] = 1; n     for (auto u : adj[x]) { n       if (erased[u]) continue; n       deg[u]--; n       if (deg[u] % 2 == 0) { n         S.insert({-depth[u], u}); n       } else n         S.erase({-depth[u], u}); n     } n     res.push_back(x); n   } n   if (res.size() != n) { n     cout <<  NO ; n     return 0; n   } n   cout <<  YES n ; n   for (auto u : res) { n     cout << u << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[1005]; n int main() { n   int i, t, mini = 1000000, s, val, tfin; n   cin >> n; n   for (i = 1; i <= n; i++) cin >> a[i]; n   for (t = 1; t <= 105; t++) { n     s = 0; n     for (i = 1; i <= n; i++) { n       val = abs(t + 1 - a[i]); n       val = min(val, abs(t - a[i])); n       val = min(val, abs(t - 1 - a[i])); n       s += val; n     } n     if (mini > s) { n       mini = s; n       tfin = t; n     } n   } n   cout << tfin <<     << mini; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long md = 1000000007; n long long bexp(long long a, long long b) { n   if (b == 0) return 1; n   if (b == 1) return a; n   long long r = bexp(a, b / 2); n   r = r * r; n   if (b % 2 == 0) n     return r; n   else n     return r * a; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int test_case = 1; n   cin >> test_case; n   while (test_case--) { n     long long n, i, j, m, k; n     cin >> n; n     long long a[n]; n     for (i = 0; i < n; i++) { n       cin >> a[i]; n     } n     long long pr = -1; n     long long ans = 0; n     for (i = 0; i < n; i++) { n       if (a[i] != 0) { n         if (pr == -1) { n           pr = i; n         } else { n           ans += i - pr - 1; n           pr = i; n         } n       } n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n const int P = 998244353; n int n, m, a[N], pre[N], rec[N], f[N]; n inline void add(int &x, int y) { n   x += y; n   if (x >= P) x -= P; n } n inline void sub(int &x, int y) { n   x -= y; n   if (x < 0) x += P; n } n struct BL { n   int bel[N], tag[450], w[N], size, cnt, sum[450][N], ans[450]; n   void init() { n     sum[1][0] = ans[1] = 1; n     size = sqrt(n); n     for (int i = 1; i <= n; ++i) bel[i] = (i - 1) / size + 1; n     cnt = bel[n]; n     for (int i = 1; i <= cnt; ++i) tag[i] = m; n   } n   void update(int pos, int x) { n     int px = bel[pos]; n     sub(sum[px][w[pos]], f[pos - 1]); n     if (w[pos] <= tag[px]) sub(ans[px], f[pos - 1]); n     w[pos] = x; n     add(sum[px][x], f[pos - 1]); n     if (x <= tag[px]) add(ans[px], f[pos - 1]); n   } n   void ins(int x, int y, int d) { n     if (x > y) return; n     if (bel[x] + 1 >= bel[y]) { n       for (int i = x; i <= y; ++i) update(i, w[i] + d); n       return; n     } n     for (int i = x; i <= bel[x] * size; ++i) update(i, w[i] + d); n     for (int i = (bel[y] - 1) * size + 1; i <= y; ++i) update(i, w[i] + d); n     for (int i = bel[x] + 1; i <= bel[y] - 1; ++i) { n       if (d > 0) sub(ans[i], sum[i][tag[i]]); n       tag[i] -= d; n       if (d < 0) add(ans[i], sum[i][tag[i]]); n     } n   } n   int qry(int x) { n     int res = 0; n     for (int i = x; bel[i] == bel[x]; --i) n       if (w[i] <= tag[bel[x]]) add(res, f[i - 1]); n     for (int i = bel[x] - 1; i >= 1; --i) add(res, ans[i]); n     return res; n   } n } bl; n int main() { n   scanf( %d%d , &n, &m); n   bl.init(); n   f[0] = 1; n   for (int i = 1; i <= n; ++i) { n     scanf( %d , &a[i]); n     pre[i] = rec[a[i]], rec[a[i]] = i; n     bl.ins(pre[pre[i]] + 1, pre[i], -1), bl.ins(pre[i] + 1, i, 1); n     f[i] = bl.qry(i); n     add(bl.sum[bl.bel[i + 1]][0], f[i]); n     add(bl.ans[bl.bel[i + 1]], f[i]); n   } n   printf( %d n , f[n]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n T gcd(T a, T b) { n   return b <= 0 ? a : gcd(b, a % b); n } n template <class T> n T lcm(T a, T b) { n   return (a * b) / gcd(a, b); n } n template <class T> n T large(T a, T b) { n   return a > b ? a : b; n } n template <class T> n T small(T a, T b) { n   return a < b ? a : b; n } n template <class T> n T dif(T a, T b) { n   return a - b < 0 ? b - a : a - b; n } n template <class T> n string toStr(const T &x) { n   stringstream s; n   s << x; n   return s.str(); n } n template <class T> n int toInt(const T &x) { n   stringstream s; n   s << x; n   int r; n   s >> r; n   return r; n } n void in() {} n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   map<int, vector<pair<int, string> > > m1; n   map<int, string> m2; n   int n, m, a, b, i; n   string s; n   cin >> n >> m; n   for (i = 0; i < n; i++) { n     cin >> s >> a >> b; n     m1[a].push_back(make_pair(b, s)); n   } n   for (i = 0; i < m1.size(); i++) { n     sort(m1[i].rbegin(), m1[i].rend()); n   } n   for (auto it : m1) { n     for (i = 0; i < it.second.size(); i++) { n       if (it.second[1].first == it.second[2].first) { n         cout <<  ?  << endl; n         break; n       } else { n         if (i < 1) n           cout << it.second[i].second <<     << it.second[i + 1].second << endl; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize(2) n using namespace std; n const int maxn = 1e6; n const int INF = 0x7fffffff; n const int mod = 1e9 + 7; n const double eps = 1e-7; n const double Pi = acos(-1.0); n inline int read_int() { n   char c; n   int ret = 0, sgn = 1; n   do { n     c = getchar(); n   } while ((c <  0  || c >  9 ) && c !=  - ); n   if (c ==  - ) n     sgn = -1; n   else n     ret = c -  0 ; n   while ((c = getchar()) >=  0  && c <=  9 ) ret = ret * 10 + (c -  0 ); n   return sgn * ret; n } n inline long long read_ll() { n   char c; n   long long ret = 0, sgn = 1; n   do { n     c = getchar(); n   } while ((c <  0  || c >  9 ) && c !=  - ); n   if (c ==  - ) n     sgn = -1; n   else n     ret = c -  0 ; n   while ((c = getchar()) >=  0  && c <=  9 ) ret = ret * 10 + (c -  0 ); n   return sgn * ret; n } n int n, res = 1; n string s; n int main() { n   cin >> n >> s; n   for (int i = 1; i < n; i++) { n     res += (s[i] != s[i - 1]); n   } n   cout << ((res + 2) < (n) ? (res + 2) : (n)) << endl; n   return 0; n } n


#include<bits/stdc++.h> n using namespace std; n int main(){ n  int t; n  cin >> t; n  while(t--){ n   int n; n   cin >> n; n   vector <int> a(n); n   int k = INT_MAX,z = -1; n   for(int i=0;i<n;i++){ n    cin >> a[i]; n    k = min(k,a[i]); n    if(k==a[i]){ n     z = i; n    } n   } n   cout << n-1 << endl; n   for(int i=0;i<n;i++){ n    if(i!=z){ n     cout << i+1 <<     << z+1 <<     << max(1,k+abs(z-i)) <<     << k << endl; n    } n   } n   //cout << endl; n  } n }