#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, x; n   scanf( %d , &n); n   vector<int> cnt(20, 0); n   for (int i = 0; i < n; i++) { n     scanf( %d , &x); n     for (int b = 0; b < 20; b++) { n       if (1 & (x >> b)) cnt[b]++; n     } n   } n   long long ans = 0; n   for (int i = 0; i < n; i++) { n     long long val = 0; n     for (int b = 0; b < 20; b++) { n       if (cnt[b] > 0) { n         cnt[b]--; n         val |= 1 << b; n       } n     } n     ans += val * val; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 3e5 + 10; n long long N, L, R, X, arr[maxn], ans; n signed main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> N >> L >> R >> X; n   for (register long long i = 1; i <= N; ++i) { n     cin >> arr[i]; n   } n   for (register long long i = 0; i < (1 << N); ++i) { n     long long cnt = __builtin_popcount(i); n     if (i < 2) continue; n     vector<long long> v; n     for (register long long j = 0; j < N; ++j) { n       if (i & 1 << j) { n         v.push_back(arr[j + 1]); n       } n     } n     sort(v.begin(), v.end()); n     if (v.back() - v.front() < X) { n       continue; n     } n     long long sum = accumulate(v.begin(), v.end(), 0); n     if (sum < L || sum > R) { n       continue; n     } n     ++ans; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const char al[100] = { A ,  B ,  C ,  D ,  E ,  F ,  G ,  H ,  I , n                        J ,  K ,  L ,  M ,  N ,  O ,  P ,  Q ,  R , n                        S ,  T ,  U ,  V ,  W ,  X ,  Y ,  Z }; n int main() { n   long long int n, m; n   cin >> n >> m; n   vector<long long int> nums(n + 1); n   vector<int> op(10000000 + 1, false); n   vector<int> prost; n   for (int i = 2; i <= 10000000; ++i) { n     if (op[i] == 0) { n       op[i] = i; n       prost.push_back(i); n     } n     for (int j = 0; n          j < (int)prost.size() && prost[j] <= op[i] && i * prost[j] <= 10000000; n          ++j) n       op[i * prost[j]] = prost[j]; n   } n   map<long long int, long long int> my; n   long long int x; n   long long int j; n   long long int count = 0; n   for (long long int i = 0; i < n; i++) { n     cin >> x; n     nums[i] = x; n     j = 0; n     if (op[x] == x) { n       count++; n       my[x]++; n     } else { n       while (x > 1) { n         if (op[x] != x) { n           while (x % prost[j] != 0) j++; n           x /= prost[j]; n           my[prost[j]]++; n           count++; n         } else { n           my[x]++; n           count++; n           x = 0; n         } n       } n     } n   } n   vector<long long int> second; n   for (long long int i = 0; i < m; i++) { n     cin >> x; n     j = 0; n     if (op[x] == x) { n       if (my.find(x) != my.end() && my[x] > 0) { n         my[x]--; n         count--; n       } else { n         if (second.size() != 0 && second[second.size() - 1] * x <= 10000000) n           second[second.size() - 1] *= x; n         else n           second.push_back(x); n       } n     } else { n       while (x > 1) { n         if (op[x] != x) { n           while (x % prost[j] != 0) j++; n           x /= prost[j]; n           if (my.find(prost[j]) != my.end() && my[prost[j]] > 0) { n             my[prost[j]]--; n             count--; n           } else { n             if (second.size() != 0 && n                 second[second.size() - 1] * prost[j] <= 10000000) n               second[second.size() - 1] *= prost[j]; n             else n               second.push_back(prost[j]); n           } n         } else { n           if (my.find(x) != my.end() && my[x] > 0) { n             my[x]--; n             count--; n           } else { n             if (second.size() != 0 && second[second.size() - 1] * x <= 10000000) n               second[second.size() - 1] *= x; n             else n               second.push_back(x); n           } n           x = 0; n         } n       } n     } n   } n   long long int prev = 1; n   long long int o; n   long long int value; n   vector<long long int> ans; n   vector<pair<int, int> > ssort; n   map<long long int, long long int> now; n   int pre = 1; n   for (int i = 0; i < n; i++) { n     pre = 1; n     x = nums[i]; n     j = 0; n     while (x > 1) { n       if (op[x] == x) { n         now[x]++; n         if (now[x] <= my[x]) pre *= x; n         x = 1; n       } else { n         while (x % prost[j] != 0) j++; n         x /= prost[j]; n         now[prost[j]]++; n         if (now[prost[j]] <= my[prost[j]]) pre *= prost[j]; n       } n     } n     ans.push_back(pre); n   } n   if (second.size() == 0) second.push_back(1); n   cout << ans.size() <<     << second.size() << endl; n   for (long long int i = 0; i < ans.size(); i++) cout << ans[i] <<    ; n   cout << endl; n   for (long long int i = 0; i < second.size(); i++) cout << second[i] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   cin.tie(0); n   ios::sync_with_stdio(0); n   int n, r1 = 0, r2 = 0; n   cin >> n; n   vector<int> r(n); n   for (int &i : r) cin >> i; n   for (int i = 0; i < n; i++) { n     int a; n     cin >> a; n     if (a != r[i]) { n       if (a) n         r1++; n       else n         r2++; n     } n   } n   if (!r2) n     cout <<  -1 n ; n   else if (r2 > r1) n     cout <<  1 n ; n   else n     cout << (int)ceil((r1 + 1.0) / r2) <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100005; n char s[45][20] = { n      Washington ,  Adams ,      Jefferson ,  Madison ,     Monroe , n      Adams ,       Jackson ,    Van Buren ,  Harrison ,    Tyler , n      Polk ,        Taylor ,     Fillmore ,   Pierce ,      Buchanan , n      Lincoln ,     Johnson ,    Grant ,      Hayes ,       Garfield , n      Arthur ,      Cleveland ,  Harrison ,   Cleveland ,   McKinley , n      Roosevelt ,   Taft ,       Wilson ,     Harding ,     Coolidge , n      Hoover ,      Roosevelt ,  Truman ,     Eisenhower ,  Kennedy , n      Johnson ,     Nixon ,      Ford ,       Carter ,      Reagan }; n int n; n void solve() { n   cin >> n; n   cout << s[n - 1]; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n bool setmax(T &_a, T _b) { n   if (_b > _a) { n     _a = _b; n     return true; n   } n   return false; n } n template <class T> n bool setmin(T &_a, T _b) { n   if (_b < _a) { n     _a = _b; n     return true; n   } n   return false; n } n template <class T> n T gcd(T _a, T _b) { n   return _b == 0 ? _a : gcd(_b, _a % _b); n } n int a[110][4], n, m; n int main() { n   cin >> n >> m; n   int cur = 1; n   memset(a, 0, sizeof(a)); n   for (int i = int(1); i <= int(n); ++i) { n     if (cur > m) break; n     a[i][0] = cur++; n     if (cur > m) break; n     a[i][3] = cur++; n   } n   for (int i = int(1); i <= int(n); ++i) { n     if (cur > m) break; n     a[i][1] = cur++; n     if (cur > m) break; n     a[i][2] = cur++; n   } n   vector<int> ans; n   for (int i = int(1); i <= int(n); ++i) { n     ans.push_back(a[i][1]); n     ans.push_back(a[i][0]); n     ans.push_back(a[i][2]); n     ans.push_back(a[i][3]); n   } n   for (int x : ans) n     if (x > 0) cout << x <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int OO = 2e7; n const double EPS = (1e-7); n int dcmp(double x, double y) { return fabs(x - y) <= EPS ? 0 : x < y ? -1 : 1; } n bool sort_p(pair<int, int> a, pair<int, int> b) { return a.first < b.first; } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(); n   cout.tie(); n   int n; n   cin >> n; n   vector<int> a(n); n   vector<int> b(n, 0); n   vector<bool> s(n + 1); n   vector<int> t(n + 1); n   bool k = false, q = false; n   for (int i = 0; i < (int)(n); ++i) { n     cin >> a[i]; n     if (a[i] == 0) k = true; n     if (a[i] != 0 && k) q = true; n     s[n - a[i]] = true; n     t[a[i]]++; n   } n   if (k) { n     for (int i = 0; i < (int)(n); ++i) n       if (a[i] != 0) { n         cout <<  Impossible ; n         return 0; n       } n   } n   if (k) { n     cout <<  Possible n ; n     for (int i = 0; i < (int)(n); ++i) cout << 1 <<    ; n     return 0; n   } n   long long int sum = 0; n   for (int i = 0; i < (int)(n); ++i) { n     sum = t[i]; n     if (n - i != 0 && t[i] != 0 && sum % (n - i)) { n       cout <<  Impossible ; n       return 0; n     } n   } n   vector<pair<int, int>> p; n   for (int i = 0; i < (int)(n); ++i) { n     pair<int, int> r = make_pair(n - a[i], i); n     p.push_back(r); n   } n   sort(((p).begin()), ((p).end()), sort_p); n   int l = 1; n   int j = 0; n   for (int i = 0; i < ((int)((p).size())); ++i) { n     for (j = 0; j < p[i].first; j++) { n       b[p[i + j].second] = l; n     } n     i = i + j - 1; n     l++; n   } n   if (sum > n) { n     cout <<  Impossible ; n     return 0; n   } n   cout <<  Possible n ; n   for (int i = 0; i < (int)(n); ++i) cout << b[i] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e6 + 5; n int n, m; n bool used[MAXN]; n set<int> graph[MAXN]; n stack<int> path; n vector<int> cycle; n int fake; n vector<pair<int, int> > edges; n vector<pair<int, int> > answer; n void EulerCycle(int x) { n   cycle.clear(); n   edges.clear(); n   path.push(x); n   while (path.empty() == false) { n     used[x] = true; n     if (graph[x].empty() == false) { n       path.push(*graph[x].begin()); n       int y = *graph[x].begin(); n       graph[x].erase(y); n       graph[y].erase(x); n       x = y; n     } else { n       cycle.push_back(x); n       path.pop(); n       if (path.empty() == false) x = path.top(); n     } n   } n   for (int i = 0; i < cycle.size() - 1; i++) { n     edges.push_back({cycle[i], cycle[i + 1]}); n   } n } n bool isFake(pair<int, int> x) { n   if (x.first == fake || x.second == fake) return true; n   return false; n } n void solve(int x) { n   EulerCycle(x); n   for (int i = 0; i < edges.size(); i++) { n     bool toDelete; n     if (isFake(edges[i]) == true) { n       continue; n     } n     if (i % 2 != 0) n       toDelete = true; n     else n       toDelete = false; n     if (i != 0 && isFake(edges[i - 1]) == true) toDelete = false; n     if (i == 0 && isFake(edges[edges.size() - 1]) == true) toDelete = false; n     if (i != edges.size() - 1 && isFake(edges[i + 1]) == true) toDelete = false; n     if (i == edges.size() - 1 && isFake(edges[0]) == true) toDelete = false; n     if (toDelete == false) { n       answer.push_back(edges[i]); n     } n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     int u, v; n     cin >> u >> v; n     graph[u].insert(v); n     graph[v].insert(u); n   } n   fake = -1; n   for (int i = 1; i <= n; i++) { n     if (graph[i].size() % 2 != 0) { n       fake = n + 1; n       graph[i].insert(fake); n       graph[fake].insert(i); n     } n   } n   for (int x = 1; x <= n + 1; x++) { n     if (used[x] == false) { n       solve(x); n     } n   } n   cout << answer.size() <<   n ; n   for (int i = 0; i < answer.size(); i++) { n     cout << answer[i].first <<     << answer[i].second <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int a, b, maxa; n void use_battery(int s) { n   if (s == 1) a = min(a + 1, maxa); n   --b; n } n void use_accum() { --a; } n int main() { n   int ans = 0; n   int n; n   cin >> n >> b >> a; n   maxa = a; n   vector<int> s(n); n   for (int i = 0; i < n; i++) cin >> s[i]; n   for (int i = 0; i < n; i++) { n     if (a == 0 && b == 0) n       break; n     else if (a == 0) n       use_battery(s[i]); n     else if (b == 0) n       use_accum(); n     else if (s[i] == 1 && a < maxa) n       use_battery(s[i]); n     else n       use_accum(); n     ans++; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int k; n   cin >> k; n   int a[k]; n   for (auto &it : a) cin >> it; n   sort(a, a + k, greater<int>()); n   int declined = max(a[0], 25) - 25; n   cout << declined; n } n


#include <bits/stdc++.h> n using namespace std; n int d[1000001] = {0}; n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   for (int i = 0, a, b; i < m; ++i) scanf( %d%d , &a, &b), d[a]++, d[b]++; n   long long ans = 0; n   for (int i = 1; i <= n; ++i) { n     long long de = d[i], ade = n - 1 - de; n     ans += de * (de - 1) + ade * (ade - 1) - de * ade; n   } n   cout << ans / 6 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n pair<int, int> a[200]; n int n, x; n vector<pair<int, int> > res; n int main() { n   int sum = 0; n   cin >> n; n   cin >> a[0].first; n   a[0].second = 0; n   sum += a[0].first; n   for (int i = 1; i < n; i++) { n     cin >> a[i].first; n     a[i].second = i; n     sum += a[i].first; n   } n   if (a[0].first == 0 || sum < n - 1) { n     cout << -1; n     return 0; n   } n   sort(a + 1, a + n); n   reverse(a + 1, a + n); n   int x = 0; n   int y = 1; n   cout << n - 1 << endl; n   while (y < n) { n     cout << a[x].second + 1 <<     << a[y].second + 1 << endl; n     y++; n     a[x].first--; n     if (a[x].first == 0) x++; n   } 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     long long int m, x, y, a, b; n     cin >> x >> y >> a >> b; n     m = abs(x - a) + abs(y - b); n     if (x != a && y != b) m = m + 2; n     cout << m <<   n ; n   } n } n


#include <bits/stdc++.h> n #pragma GCC optimize( 03 ) n using namespace std; n template <typename Arg1> n void __f(const char* name, Arg1&& arg1) { n   cerr << name <<   :   << arg1 << std::endl; n } n template <typename Arg1, typename... Args> n void __f(const char* names, Arg1&& arg1, Args&&... args) { n   const char* comma = strchr(names + 1,  , ); n   cerr.write(names, comma - names) <<   :   << arg1 <<   |  ; n   __f(comma + 1, args...); n } n template <class T, class T2> n T _f_pow(T a, T2 b) { n   T res = 1LL; n   while (b > 0) { n     if (b & 1) res = (res * a); n     a = (a * a); n     b >>= 1; n   } n   return (res); n } n template <class T> n T gcd(T a, T b) { n   while (a && b) n     if (a > b) n       (a %= b); n     else n       (b %= a); n   return a + b; n } n template <class T> n T lcm(T a, T b) { n   return (a * b) / gcd(a, b); n } n int n, m; n long long par[(int)2e6 + 5], sz[(int)2e6 + 5], cmp, mxComp; n void init() { n   iota(par, par + n + 1, 0); n   fill(sz, sz + n + 1, 1); n   cmp = n; n   mxComp = 1; n } n int Find(int u) { return u == par[u] ? u : par[u] = Find(par[u]); } n int join(int u, int v) { n   u = Find(u); n   v = Find(v); n   if (u == v) return 0; n   if (sz[u] > sz[v]) swap(u, v); n   par[u] = v; n   sz[v] += sz[u]; n   mxComp = max(mxComp, sz[v]); n   cmp--; n   return 1; n } n int from[(int)2e6 + 5], to[(int)2e6 + 5], cst[(int)2e6 + 5], n     sorted[(int)2e6 + 5]; n int f, s; n int krus() { n   init(); n   sort(sorted, sorted + m, [](int a, int b) { return cst[a] < cst[b]; }); n   int ret = 0; n   for (int k = 0; k < m; k++) { n     int e = sorted[k]; n     if (join(from[e], to[e])) { n       if (Find(f) == Find(s)) { n         cout << cst[e] << endl; n         return 0; n       } n       ret += cst[e]; n     } n   } n   return 1; n } n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   ; n   cin >> n >> m; n   int tm; n   m--; n   cin >> f >> s >> tm; n   for (int i = 0, _n = (m); i < _n; i++) { n     cin >> from[i] >> to[i] >> cst[i]; n     sorted[i] = i; n   } n   if (krus()) cout << ((int)1e9) << endl; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } n const int MAXN = 1000000; n const int OPAND = 2, OPOR = 3, OPXOR = 4, OPNOT = 5; n int n; n int op[MAXN]; n vector<int> ch[MAXN]; n int res[MAXN]; n int ans[MAXN]; n int dfsres(int at) { n   if (op[at] == 0 || op[at] == 1) { n     res[at] = op[at]; n   } else if (op[at] == OPNOT) { n     res[at] = 1 - dfsres(ch[at][0]); n   } else if (op[at] == OPAND) { n     int a = dfsres(ch[at][0]), b = dfsres(ch[at][1]); n     res[at] = a == 1 && b == 1 ? 1 : 0; n   } else if (op[at] == OPOR) { n     int a = dfsres(ch[at][0]), b = dfsres(ch[at][1]); n     res[at] = a == 1 || b == 1 ? 1 : 0; n   } else if (op[at] == OPXOR) { n     int a = dfsres(ch[at][0]), b = dfsres(ch[at][1]); n     res[at] = (a == 1) ^ (b == 1) ? 1 : 0; n   } else { n     assert(false); n   } n   return res[at]; n } n void dfsans(int at, int fix, bool neg) { n   if (op[at] == 0 || op[at] == 1) { n     ans[at] = fix != -1 ? fix : 1 - op[at]; n     if (fix == -1 && neg) ans[at] = 1 - ans[at]; n   } else if (op[at] == OPNOT) { n     dfsans(ch[at][0], fix, !neg); n   } else if (op[at] == OPAND) { n     for (int i = (0); i < (2); ++i) n       dfsans(ch[at][i], n              fix != -1                 ? fix n              : res[ch[at][1 - i]] == 0 ? (neg ? 1 : 0) n                                        : -1, n              neg); n   } else if (op[at] == OPOR) { n     for (int i = (0); i < (2); ++i) n       dfsans(ch[at][i], n              fix != -1                 ? fix n              : res[ch[at][1 - i]] == 1 ? (neg ? 0 : 1) n                                        : -1, n              neg); n   } else if (op[at] == OPXOR) { n     for (int i = (0); i < (2); ++i) n       dfsans(ch[at][i], fix != -1 ? fix : -1, n              res[ch[at][1 - i]] == 1 ? !neg : neg); n   } else { n     assert(false); n   } n } n void run() { n   scanf( %d , &n); n   for (int i = (0); i < (n); ++i) { n     char buff[10]; n     scanf( %s , buff); n     if (strcmp(buff,  AND ) == 0) { n       int a, b; n       scanf( %d%d , &a, &b); n       --a, --b; n       op[i] = OPAND; n       ch[i].push_back(a); n       ch[i].push_back(b); n     } else if (strcmp(buff,  OR ) == 0) { n       int a, b; n       scanf( %d%d , &a, &b); n       --a, --b; n       op[i] = OPOR; n       ch[i].push_back(a); n       ch[i].push_back(b); n     } else if (strcmp(buff,  XOR ) == 0) { n       int a, b; n       scanf( %d%d , &a, &b); n       --a, --b; n       op[i] = OPXOR; n       ch[i].push_back(a); n       ch[i].push_back(b); n     } else if (strcmp(buff,  NOT ) == 0) { n       int a; n       scanf( %d , &a); n       --a; n       op[i] = OPNOT; n       ch[i].push_back(a); n     } else if (strcmp(buff,  IN ) == 0) { n       int val; n       scanf( %d , &val); n       op[i] = val; n     } else { n       printf( err: %s n , buff); n       assert(false); n     } n   } n   dfsres(0); n   dfsans(0, -1, false); n   for (int i = (0); i < (n); ++i) n     if (op[i] == 0 || op[i] == 1) printf( %d , ans[i]); n   puts(  ); n } n int main() { n   run(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long tab[12], pr[12]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int q; n   cin >> q; n   long long n, m, w, x; n   for (int z = 0; z < q; z++) { n     cin >> n >> m; n     x = 1; n     for (long long i = 1; i <= 11; i++) { n       tab[i] = (tab[i - 1] + (m % 10)) % 10; n       pr[i] = pr[i - 1] + tab[i]; n       if (tab[i] == tab[1] && i > 1) { n         x = i - 1; n         break; n       } n     } n     w = pr[x] * (n / (x * m)); n     n %= x * m; n     w += pr[n / m]; n     cout << w <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int ar[30005]; n int diff[30005]; n int main() { n   int t; n   cin >> t; n   while (t--) { n     string ans =  YES ; n     int n; n     cin >> n; n     for (int i = 1; i <= n; i++) { n       cin >> ar[i]; n       diff[i] = 0; n     } n     for (int i = 2; i <= n; i++) { n       if (ar[i] < ar[i - 1]) { n         diff[1] += ar[i - 1] - ar[i]; n         diff[i] -= ar[i - 1] - ar[i]; n       } n     } n     for (int i = 1; i <= n; i++) { n       diff[i] += diff[i - 1]; n       ar[i] -= diff[i]; n       if (ar[i] < 0 || (i > 1 && ar[i] < ar[i - 1])) { n         ans =  NO ; n       } n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long read() { n   long long x = 0, f = 1; n   char c = getchar(); n   while (c <  0  || c >  9 ) { n     if (c ==  - ) f = 0; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) n     x = (x << 3) + (x << 1) + (c ^ 48), c = getchar(); n   return f ? x : -x; n } n long long n, m, ans, s = 1, num; n bool exia[20][20], exib[20][20], s1[20][20], s2[20][20]; n signed main() { n   n = read(), m = read(); n   for (long long i = 1; i <= n; i++) { n     long long x = read(), y = read(); n     exia[x][y] = exia[y][x] = 1; n   } n   for (long long i = 1; i <= m; i++) { n     long long x = read(), y = read(); n     exib[x][y] = exib[y][x] = 1; n   } n   for (long long i = 1; i <= 9; i++) { n     for (long long j = 1; j <= 9; j++) { n       if (i == j) continue; n       for (long long k = 1; k <= 9; k++) { n         if (i == k || j == k) continue; n         if (exia[i][j] && exib[i][k]) { n           s1[i][j] = s2[i][k] = 1; n           ans = i; n           if (num && num != i) s = 0; n           num = i; n         } n       } n     } n   } n   if (s) n     printf( %lld , ans); n   else { n     ans = 0; n     for (long long i = 1; i <= 9; i++) { n       for (long long j = 1; j <= 9; j++) { n         if (i == j) continue; n         if (s1[i][j] && s1[j][i]) { n           ans = -1; n           break; n         } n         if (s2[i][j] && s2[j][i]) { n           ans = -1; n           break; n         } n       } n     } n     printf( %lld , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long modex(long long a, long long p, long long m) { n   if (!p) return 1; n   if (p & 1) return a * modex(a, p - 1, m) % m; n   long long v = modex(a, p >> 1, m); n   return v * v % m; n } n namespace fft { n struct cpx { n   double r, i; n   cpx(double r = 0, double i = 0) : r(r), i(i) {} n   cpx operator+(cpx b) { return {r + b.r, i + b.i}; } n   cpx operator-(cpx b) { return {r - b.r, i - b.i}; } n   cpx operator*(cpx b) { return {r * b.r - i * b.i, r * b.i + i * b.r}; } n   cpx operator/(double b) { return {r / b, i / b}; } n   inline cpx conj() { return {r, -i}; } n }; n const double PI = acos(-1); n vector<int> rev(1, 0); n vector<cpx> roots; n int base = 0; n void precompute(int nbase) { n   if (nbase <= base) return; n   rev.resize(1 << nbase); n   for (int i = 0; i < (1 << nbase); ++i) { n     rev[i] = rev[i >> 1] >> 1 | ((i & 1) << nbase - 1); n   } n   roots.resize(1 << nbase); n   for (; base < nbase; ++base) { n     int len = 1 << base; n     double angle = 2 * PI / (1 << (base + 1)); n     for (int i = 0; i < len; ++i) { n       double cur = angle * i; n       roots[len + i] = cpx(cos(cur), sin(cur)); n     } n   } n } n void fft(vector<cpx>& a, int n = -1) { n   if (n == -1) n = a.size(); n   assert((n & (n - 1)) == 0); n   int zeros = __builtin_ctz(n); n   precompute(zeros); n   int shift = base - zeros; n   for (int i = 0; i < n; ++i) { n     if (i < (rev[i] >> shift)) swap(a[i], a[rev[i] >> shift]); n   } n   for (int len = 1; len < n; len <<= 1) n     for (int i = 0; i < n; i += 2 * len) n       for (int j = 0; j < len; ++j) { n         cpx u = a[i + j], v = a[i + j + len] * roots[len + j]; n         a[i + j] = u + v; n         a[i + j + len] = u - v; n       } n } n vector<cpx> fa, fb; n vector<long long> multiply(vector<int> a, vector<int> b) { n   if (a.empty() || b.empty()) return {}; n   int sz = a.size() + b.size() - 1; n   int n = sz == 1 ? 1 : 1 << (32 - __builtin_clz(sz - 1)); n   if (n > fa.size()) fa.resize(n); n   for (int i = 0; i < n; ++i) { n     int x = i < a.size() ? a[i] : 0; n     int y = i < b.size() ? b[i] : 0; n     fa[i] = cpx(x, y); n   } n   fft(fa, n); n   cpx r(0, -0.5 / n); n   for (int i = 0; i <= (n >> 1); ++i) { n     int j = (n - i) & (n - 1); n     cpx x = (fa[i] + fa[j].conj()) / 2; n     cpx y = (fa[i] - fa[j].conj()) * r; n     fa[i] = x * y; n     if (i != j) fa[j] = fa[i].conj(); n   } n   fft(fa, n); n   reverse(fa.begin() + 1, fa.end()); n   vector<long long> res(sz); n   for (int i = 0; i < sz; ++i) { n     res[i] = llround(fa[i].r); n   } n   return res; n } n vector<int> multiply(const vector<int>& a, const vector<int>& b, int m) { n   if (a.empty() || b.empty()) return {}; n   int eq = (a == b); n   int sz = a.size() + b.size() - 1; n   int n = sz == 1 ? 1 : 1 << (32 - __builtin_clz(sz - 1)); n   if (n > fa.size()) fa.resize(n); n   for (int i = 0; i < a.size(); ++i) { n     fa[i] = cpx(a[i] & ((1 << 15) - 1), a[i] >> 15); n   } n   fill(fa.begin() + a.size(), fa.begin() + n, cpx(0, 0)); n   fft(fa, n); n   if (n > fb.size()) fb.resize(n); n   if (eq) { n     copy(fa.begin(), fa.begin() + n, fb.begin()); n   } else { n     for (int i = 0; i < b.size(); ++i) { n       fb[i] = cpx(b[i] & ((1 << 15) - 1), b[i] >> 15); n     } n     fill(fb.begin() + b.size(), fb.begin() + n, cpx(0, 0)); n   } n   fft(fb, n); n   double ratio = 0.5 / n; n   cpx r2(0, -0.5); n   cpx r3(ratio, 0); n   cpx r4(0, -ratio); n   cpx r5(0, 1); n   for (int i = 0; i <= (n >> 1); ++i) { n     int j = (n - i) & (n - 1); n     cpx a1 = (fa[i] + fa[j].conj()) / 2; n     cpx a2 = (fa[i] - fa[j].conj()) * r2; n     cpx b1 = (fb[i] + fb[j].conj()) * r3; n     cpx b2 = (fb[i] - fb[j].conj()) * r4; n     cpx x = a1 * b1; n     cpx y = a2 * b2; n     fa[i] = x + y * r5; n     fb[i] = a1 * b2 + a2 * b1; n     if (i != j) { n       fa[j] = x.conj() + y.conj() * r5; n       fb[j] = fb[i].conj(); n     } n   } n   fft(fa, n); n   fft(fb, n); n   reverse(fa.begin() + 1, fa.end()); n   reverse(fb.begin() + 1, fb.end()); n   vector<int> res(sz); n   for (int i = 0; i < sz; ++i) { n     long long aa = llround(fa[i].r); n     long long bb = llround(fb[i].r); n     long long cc = llround(fa[i].i); n     assert(llround(fb[i].i) == 0); n     res[i] = (aa + ((bb % m) << 15) + ((cc % m) << 30)) % m; n   } n   return res; n } n };  // namespace fft n namespace polynomial { n int MOD = 998244353; n template <typename T> n vector<T> subtract(vector<T> a, const vector<T>& b) { n   if (a.size() < b.size()) { n     a.resize(b.size()); n   } n   for (int i = 0; i < b.size(); ++i) { n     a[i] -= b[i]; n     if (a[i] < 0) a[i] += MOD; n   } n   return a; n } n template <typename T> n vector<T> inverse(const vector<T>& a) { n   assert(!a.empty() && a[0] != 0); n   int n = a.size(); n   vector<T> b = {modex(a[0], MOD - 2, MOD)}; n   for (int m = 2; b.size() < n; m <<= 1) { n     vector<T> aCut(a.begin(), a.begin() + min(a.size(), m)); n     auto x = fft::multiply(aCut, fft::multiply(b, b, MOD), MOD); n     b.resize(m); n     for (int i = 0; i < m; ++i) { n       b[i] += b[i] - x[i]; n       if (b[i] >= MOD) b[i] -= MOD; n       if (b[i] < 0) b[i] += MOD; n     } n   } n   b.resize(n); n   return b; n } n template <typename T> n vector<T> divide(vector<T> a, vector<T> b) { n   assert(!b.empty() && b.back() != 0); n   int n = a.size(); n   int m = b.size(); n   if (n < m) return {}; n   reverse(a.begin(), a.end()); n   reverse(b.begin(), b.end()); n   b.resize(n - m + 1); n   a = multiply(a, inverse(b), MOD); n   a.erase(a.begin() + n - m + 1, a.end()); n   reverse(a.begin(), a.end()); n   return a; n } n template <typename T> n vector<T> remainder(const vector<T>& a, const vector<T>& b) { n   return subtract(a, fft::multiply(b, divide(a, b), MOD)); n } n template <typename T> n vector<T> integral(const vector<T>& a) { n   vector<T> ret(a.size() + 1); n   for (int i = 1; i < ret.size(); ++i) { n     ret[i] = a[i] * modex(i, MOD - 2, MOD) % MOD; n   } n   return ret; n } n template <typename T> n vector<T> derivative(const vector<T>& a) { n   if (a.empty()) return a; n   vector<T> ret(a.size() - 1); n   for (int i = 0; i + 1 < a.size(); ++i) n     ret[i] = (long long)a[i + 1] * (i + 1) % MOD; n   return ret; n } n template <typename T> n vector<T> logarithm(const vector<T>& a) { n   assert(!a.empty() && a[0] == 1); n   auto ret = integral(fft::multiply(derivative(a), inverse(a), MOD)); n   ret.resize(a.size()); n   return ret; n } n template <typename T> n vector<T> exponent(const vector<T>& a) { n   assert(!a.empty() && a[0] == 0); n   int n = a.size(); n   vector<T> b = {1}; n   for (int m = 2; b.size() < n; m <<= 1) { n     vector<T> x(a.begin(), a.begin() + min(a.size(), m)); n     ++x[0]; n     if (x[0] >= MOD) x[0] -= MOD; n     auto oldB = b; n     b.resize(m); n     b = fft::multiply(oldB, subtract(x, logarithm(b)), MOD); n     b.resize(m); n   } n   b.resize(n); n   return b; n } n template <typename T> n vector<T> multipoint(const vector<T>& a, vector<T> xs) { n   int n = xs.size(); n   vector<vector<T>> prods(2 * n - 1); n   function<void(int, int, int)> getProds = [&](int sn, int s, int e) { n     if (s == e) { n       prods[sn] = {(MOD - xs[s]) % MOD, 1}; n       return; n     } n     int m = (s + e) >> 1, lsn = sn + 1, rsn = sn + ((m - s + 1) << 1); n     getProds(lsn, s, m); n     getProds(rsn, m + 1, e); n     prods[sn] = fft::multiply(prods[lsn], prods[rsn]); n   }; n   getProds(0, 0, n - 1); n   function<void(int, int, int, vector<T>)> calc = [&](int sn, int s, int e, n                                                       vector<T> f) { n     if (s == e) { n       xs[s] = f[0]; n       return; n     } n     int m = (s + e) >> 1, lsn = sn + 1, rsn = sn + ((m - s + 1) << 1); n     calc(lsn, s, m, remainder(f, prods[lsn])); n     calc(rsn, m + 1, e, remainder(f, prods[rsn])); n   }; n   calc(0, 0, n - 1, a); n   return xs; n } n template <typename T> n vector<T> chirpz(vector<T> a, T g, int n) { n   int d = a.size(); n   int phi = MOD - 1; n   vector<long long> pw(phi + 1, 1); n   for (int i = 1; i <= phi; ++i) pw[i] = pw[i - 1] * g % MOD; n   auto chirpzEven = [&](const vector<T>& b, int m) { n     vector<T> u(d), v(m + d - 1); n     for (int i = 0; i < d; ++i) { n       u[i] = b[i] * pw[(long long)i * i % phi] % MOD; n     } n     for (int i = -(d - 1); i < m; ++i) { n       v[d - 1 + i] = pw[phi - (long long)i * i % phi]; n     } n     auto z = fft::multiply(u, v, MOD); n     vector<T> ret(m); n     for (int i = 0; i < m; ++i) { n       ret[i] = pw[(long long)i * i % phi] * z[d - 1 + i] % MOD; n     } n     return ret; n   }; n   auto even = chirpzEven(a, (n + 1) >> 1); n   for (int i = 0; i < d; ++i) a[i] = a[i] * pw[i] % MOD; n   auto odd = chirpzEven(a, n >> 1); n   vector<T> ret(n); n   for (int i = 0; i < n; ++i) { n     ret[i] = (i & 1) ? odd[i >> 1] : even[i >> 1]; n   } n   return ret; n } n };  // namespace polynomial n using namespace polynomial; n int primitiveRoot(int n) { n   vector<int> fact; n   int phi = n - 1, x = phi; n   for (int i = 2; i * i <= x; ++i) n     if (x % i == 0) { n       fact.push_back(i); n       while (x % i == 0) x /= i; n     } n   if (x > 1) fact.push_back(x); n   for (int res = 1; res <= n; ++res) { n     bool ok = true; n     for (int i = 0; i < fact.size() && ok; ++i) n       ok &= modex(res, phi / fact[i], n) != 1; n     if (ok) return res; n   } n   return -1; n } n char str[100005]; n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   vector<int> poly(n); n   scanf( %s , str); n   for (int i = 0; i < n; ++i) { n     poly[i] += str[i] -  a ; n   } n   scanf( %s , str); n   for (int i = 0; i < n; ++i) { n     poly[i] -= str[i] -  a ; n   } n   const int MAXN = 300005; n   bitset<MAXN> notp; n   notp[1] = notp[0] = 1; n   for (int i = 2; i < MAXN; ++i) n     if (!notp[i]) n       for (int j = i + i; j < MAXN; j += i) notp[j] = 1; n   mt19937 rng(42); n   for (;;) { n     int p = m + n + rng() % 100000; n     if (notp[p]) continue; n     MOD = p; n     int g = primitiveRoot(p); n     auto z = poly; n     for (int i = 0; i < z.size(); ++i) z[i] = (z[i] % MOD + MOD) % MOD; n     auto res = chirpz(z, g, p - 1); n     long long pw = 1; n     for (int i = 0; i < p - 1; ++i) { n       if (1 < pw && pw < p - 1 && res[i] == 0) { n         long long z = 0; n         long long r = pw; n         for (int i = n - 1; i >= 0; --i) { n           z = (z * r + poly[i]) % MOD; n         } n         cerr <<  z  n              <<  =  << z << endl; n         printf( %d %lld n , MOD, r); n         return 0; n       } n       pw = pw * g % MOD; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long int power(long long int a, long long int b) { n   if (b == 0) return 1; n   if (b == 1) return a; n   long long int r = power(a, b / 2) % 1000000007; n   if (b % 2 == 0) n     return (r * r) % 1000000007; n   else n     return (((r * r) % 1000000007) * a) % 1000000007; n } n int prime[1000009]; n int sumprime[1000009]; n void sieve(int n) { n   prime[0] = prime[1] = 1; n   for (int i = 2; i <= sqrt(n); i++) { n     if (!prime[i]) { n       for (int j = 2 * i; j <= n; j = j + i) { n         prime[j]++; n       } n     } n   } n   for (int i = 1; i <= n; i++) { n     if (!prime[i]) { n       sumprime[i] = sumprime[i - 1] + 1; n     } else { n       sumprime[i] = sumprime[i - 1]; n     } n   } n } n bool comp(const pair<long double, long long int> &a, n           const pair<long double, long long int> &b) { n   if (a.first == b.first) return a.second < b.second; n   return (a.first < b.first); n } n const int N = 5 * 1e5 + 9; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int m, n; n   cin >> n >> m; n   vector<pair<long double, long long int>> v; n   for (int i = 1; i <= m; i++) { n     long double d = abs((m + 1) / 2.0 - i); n     v.push_back({d, i}); n   } n   sort(v.begin(), v.end(), comp); n   for (int i = 0; i < n; i++) { n     int val = i % m; n     cout << v[val].second <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 10; n int n, m; n struct point { n   int x, y; n   point() {} n   point(int x, int y) : x(x), y(y) {} n   point operator-(point a) { return point(x - a.x, y - a.y); } n   bool operator<(const point& a) const { return x == a.x ? y < a.y : x < a.x; } n } a[N], b[N]; n long long dot(point a, point b) { n   return (long long)a.x * b.x + (long long)a.y * b.y; n } n long long cross(point a, point b) { n   return (long long)a.x * b.y - (long long)a.y * b.x; n } n long long dist2(point a, point b) { return dot(a - b, a - b); } n vector<point> convex_hull(point* p, int n) { n   sort(p + 1, p + 1 + n); n   vector<point> convex; n   for (int i = 1; i <= n; ++i) { n     for (int j; (j = convex.size()) > 1 && n                 cross(convex[j - 1] - convex[j - 2], p[i] - convex[j - 2]) <= 0; n          convex.pop_back()) n       ; n     convex.push_back(p[i]); n   } n   int old = convex.size(); n   for (int i = n - 1; i; --i) { n     for (int j; (j = convex.size()) > old && n                 cross(convex[j - 1] - convex[j - 2], p[i] - convex[j - 2]) <= 0; n          convex.pop_back()) n       ; n     convex.push_back(p[i]); n   } n   convex.pop_back(); n   return convex; n } n struct p3 { n   long long x, y, z; n   p3() {} n   p3(long long x, long long y, long long z) : x(x), y(y), z(z) {} n   bool operator<(const p3& a) const { n     return x == a.x ? y == a.y ? z < a.z : y < a.y : x < a.x; n   } n }; n long long f(point a, point b) { return dot(a, b) + cross(a, b); } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; ++i) { n     scanf( %d%d , &a[i].x, &a[i].y); n   } n   for (int i = 1; i <= m; ++i) { n     scanf( %d%d , &b[i].x, &b[i].y); n   } n   vector<point> convex_a = convex_hull(a, n); n   vector<point> convex_b = convex_hull(b, m); n   if (convex_a.size() != convex_b.size()) { n     return puts( NO ), 0; n   } else { n     map<p3, int> appeared; n     for (int i = 0; i < convex_a.size(); ++i) { n       int j = i + 1 == convex_a.size() ? 0 : i + 1; n       int k = i == 0 ? convex_a.size() - 1 : i - 1; n       ++appeared[p3(dist2(convex_a[k], convex_a[i]), n                     f(convex_a[k] - convex_a[i], convex_a[j] - convex_a[i]), n                     dist2(convex_a[j], convex_a[i]))]; n     } n     for (int i = 0; i < convex_b.size(); ++i) { n       int j = i + 1 == convex_b.size() ? 0 : i + 1; n       int k = i == 0 ? convex_b.size() - 1 : i - 1; n       --appeared[p3(dist2(convex_b[k], convex_b[i]), n                     f(convex_b[k] - convex_b[i], convex_b[j] - convex_b[i]), n                     dist2(convex_b[j], convex_b[i]))]; n     } n     bool same = true; n     for (map<p3, int>::iterator it = appeared.begin(); it != appeared.end(); n          ++it) { n       if (it->second != 0) { n         same = false; n       } n     } n     return puts(same ?  YES  :  NO ), 0; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 35, L = 10005; n int T, k, i, j, n, m; n int a[2][N], b[N * 2], bb[N * 2]; n char cc[5], ans[L], *ass, out[26][L], *pp[26]; n inline void getseq() { n   int xb = 0; n   for (j = 0; j < n; ++j) n     if (a[0][j]) b[xb++] = a[0][j]; n   for (j = n - 1; j >= 0; --j) n     if (a[1][j]) b[xb++] = a[1][j]; n } n inline void push(char c1, char c2) { *pp[c1 -  A ]++ = c2; } n inline void opt(char c) { *ass++ = c; } n inline void get_rot2(int x, char c) { n   int i; n   x -= k; n   if (x < 0) x += m; n   for (i = 0; i < x; ++i) push(c,  R ); n   for (i = 0; i < k; ++i) push(c,  l ); n   push(c,  u ); n   for (i = 0; i < k; ++i) push(c,  r ); n   push(c,  d ); n   for (i = x; i < m; ++i) push(c,  R ); n } n inline void rot2(int x) { n   int i; n   x -= k; n   if (x < 0) x += m; n   for (i = 0; i < x; ++i) opt( R ); n   for (i = 0; i < k; ++i) opt( l ); n   opt( u ); n   for (i = 0; i < k; ++i) opt( r ); n   opt( d ); n   for (i = x; i < m; ++i) opt( R ); n } n inline void rot3(int x) { n   swap(b[x], b[x + 1]); n   swap(b[x + 1], b[x + 2]); n   if (k == 1) return rot2(x); n   int i; n   if (x >= 8) opt(x / 8 - 1 +  A ); n   if (x % 8) opt(x % 8 - 1 +  I ); n   opt( U ); n   x = m - x; n   if (x >= 8) opt(x / 8 - 1 +  A ); n   if (x % 8) opt(x % 8 - 1 +  I ); n } n int calcI(int* b) { n   int ass = 0; n   for (i = 0; i < m; ++i) n     for (j = i + 1; j < m; ++j) ass += b[i] > b[j]; n   return ass; n } n int main() { n   for (scanf( %d , &T); T--;) { n     scanf( %d , &k); n     n = k * 2 + 1; n     m = n * 2 - 1; n     ass = ans; n     for (i = 0; i < 2; ++i) n       for (j = 0; j < n; ++j) n         scanf( %s , cc), *cc ==  E  ? a[i][j] = 0 : sscanf(cc,  %d , a[i] + j); n     for (j = 0; j + 1 < n; ++j) n       for (i = 0; i < 2; ++i) n         if (a[i][j] == 0) opt( r ), swap(a[i][j], a[i][j + 1]); n     if (a[0][n - 1] == 0) opt( d ), swap(a[0][n - 1], a[1][n - 1]); n     ; n     int i1, i2; n     getseq(); n     i1 = calcI(b); n     for (i = 0; i < m; ++i) bb[i] = i + 1; n     reverse(bb + n, bb + m); n     i2 = calcI(bb); n     if ((i2 - i1) & 1) { n       puts( SURGERY FAILED ); n       continue; n     } n     for (i = 0; i < 26; ++i) pp[i] = out[i]; n     for (i = n - 1; i; --i) push( R ,  l ); n     push( R ,  u ); n     for (i = 0; i < n - 1; ++i) push( R ,  r ); n     push( R ,  d ); n     for (i = 1; i <= 8; ++i) push( A ,  R ); n     for (i = 2; i <= 8; ++i) n       push( A  + i - 1,  A  + i - 2), push( A  + i - 1,  A ); n     push( I ,  R ); n     for (i = 1; i < 8; ++i) push( I  + i,  I  + i - 1), push( I  + i,  R ); n     get_rot2(0,  S ); n     get_rot2(1,  T ); n     push( U ,  T ); n     push( U ,  T ); n     for (i = 1; i <= 2 * k; i += 2) push( U ,  T ), push( U ,  S ); n     for (i = 0; i + 2 < m; ++i) { n       for (j = i; b[j] != bb[i]; ++j) n         ; n       for (; j > i + 1; rot3(j - 2), --j) n         ; n       if (j > i) rot3(j - 1); n     } n     puts( SURGERY COMPLETE ); n     *ass = 0; n     puts(ans); n     for (i = 0; i < 26; ++i) n       if (pp[i] != out[i]) *pp[i] = 0, printf( %c %s n , i +  A , out[i]); n     puts( DONE ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ii = pair<int, int>; n using pll = pair<ll, ll>; n const int N = 100010; n int m, n, q[N], a[N]; n int main() { n   scanf( %d , &m); n   for (int i = 0; i < m; i++) scanf( %d , &q[i]); n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %d , &a[i]); n   int mn = *min_element(q, q + m) + 2; n   int ans = 0; n   for (int i = 0; i < n; i++) ans += a[i]; n   sort(a, a + n); n   reverse(a, a + n); n   for (int i = mn - 2; i < n; i += mn) { n     ans -= a[i]; n     if (i + 1 < n) ans -= a[i + 1]; n   } n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, tc; n   cin >> tc; n   while (tc-- != 0) { n     cin >> n; n     if (n & 1) { n       cout <<  7 ; n       for (int i = 1; i < n / 2; i++) { n         cout <<  1 ; n       } n     } else { n       for (int i = 1; i <= n / 2; i++) { n         cout <<  1 ; n       } n     } n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 2e5 + 5, inf = 1e9 + 7; n long long n, k, i, j; n long long A[N], l[N], r[N], len[N]; n set<pair<long long, long long> > st; n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   long long i, j, a, b, ans = 0, cur; n   cin >> n; n   for (i = 1; i < n + 1; i++) cin >> A[i]; n   i = 1; n   while (i <= n) { n     j = i; n     while (j <= n && A[j] == A[i]) j++; n     r[i] = j; n     l[j] = i; n     len[i] = j - i; n     st.insert(pair<long long, long long>(i - j, i)); n     i = j; n   } n   while (!st.empty()) { n     cur = st.begin()->second; n     st.erase(st.begin()); n     ans++; n     len[cur] = 0; n     a = l[cur]; n     b = r[cur]; n     if (st.empty()) break; n     if (A[a] == A[b] && a > 0 && b <= n) { n       st.erase(pair<long long, long long>(-len[a], a)); n       st.erase(pair<long long, long long>(-len[b], b)); n       r[a] = r[b]; n       l[r[a]] = a; n       len[a] += len[b]; n       len[b] = 0; n       st.insert(pair<long long, long long>(-len[a], a)); n     } else { n       r[a] = b; n       l[b] = a; n     } n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n struct M { n   int t, x[4]; n   M(int s, int a, int b, int c, int d = 0) { n     t = s; n     x[0] = a; n     x[1] = b; n     x[2] = c; n     x[3] = d; n   } n   void p() { n     printf( %d , t); n     for (int i = 0; i < (int)(t); ++i) printf(  %d , x[i]); n     printf(  n ); n   } n }; n vector<M> an; n int main() { n   int n; n   scanf( %d , &n); n   while (n > 4) { n     int a = n, b = n - 1; n     an.push_back(M(3, 1, a, b)); n     an.push_back(M(3, 1, a, b)); n     for (int i = 3; i < (int)(b); ++i) an.push_back(M(4, i - 1, a, i, b)); n     an.push_back(M(4, b - 1, a, 2, b)); n     n -= 2; n   } n   if (n == 4) { n     an.push_back(M(4, 1, 2, 3, 4)); n     an.push_back(M(4, 1, 3, 4, 2)); n     an.push_back(M(4, 1, 4, 2, 3)); n   } else { n     an.push_back(M(3, 1, 2, 3)); n     an.push_back(M(3, 1, 2, 3)); n   } n   printf( %d n , (int)an.size()); n   for (int i = 0; i < (int)(an.size()); ++i) an[i].p(); n } n


#include <bits/stdc++.h> n using namespace std; n int main(int argc, char const *argv[]) { n   ios_base ::sync_with_stdio(false), cin.tie(NULL); n   int test; n   cin >> test; n   while (test--) { n     int n, k; n     string s; n     cin >> s; n     int a = 1, poss = true; n     while (poss) { n       poss = false; n       string t; n       int len = s.size(); n       for (int i = 0; i < len - 1; ++i) { n         if ((s[i] ==  1  && s[i + 1] ==  0 ) || n             (s[i] ==  0  && s[i + 1] ==  1 )) { n           i += 2; n           poss = true; n           a = (a == 1) ? 0 : 1; n           while (i < len) { n             t += s[i]; n             i++; n           } n         } else n           t += s[i]; n       } n       s = t; n     } n     if (a == 0) n       cout <<  DA n ; n     else n       cout <<  NET n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, k; n int mid = 0; n int num = 0; n int e[21][21]; n int flag[21]; n void dfs(int x, int sum) { n   if (x > n) { n     num = num + 1; n     return; n   } n   dfs(x + 1, sum); n   for (int y = 1; y <= n; y++) { n     if (num > k) { n       break; n     } n     if (flag[y] == 0 && e[x][y] != 0 && sum + e[x][y] <= mid) { n       flag[y] = 1; n       dfs(x + 1, sum + e[x][y]); n       flag[y] = 0; n     } n   } n   return; n } n int main() { n   int u, v, w; n   cin >> n >> m >> k; n   int l = 0; n   int h = 1000 * n; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       e[i][j] = 0; n     } n   } n   for (int i = 0; i < m; i++) { n     cin >> u >> v >> w; n     e[u][v] = w; n   } n   while (l < h) { n     mid = (l + h) / 2; n     num = 0; n     dfs(1, 0); n     if (num < k) { n       l = mid + 1; n     } else { n       h = mid; n     } n   } n   cout << l << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   double a1, a2; n   double b1, b2; n   double c1, c2; n   cin >> a1 >> b1 >> c1; n   cin >> a2 >> b2 >> c2; n   if (fabs(a1) < 1e-14 && fabs(b1) < 1e-14 && fabs(c1) > 1e-14 || n       fabs(a2) < 1e-14 && fabs(b2) < 1e-14 && fabs(c2) > 1e-14) { n     cout << 0; n     return 0; n   } n   if (fabs(a1) < 1e-14 && fabs(b1) < 1e-14 && fabs(c1) < 1e-14 || n       fabs(a2) < 1e-14 && fabs(b2) < 1e-14 && fabs(c2) < 1e-14) { n     cout << -1; n     return 0; n   } n   if ((fabs(b1) < 1e-14 && fabs(b2) > 1e-14) || n       (fabs(b2) < 1e-14 && fabs(b1) > 1e-14)) n     cout << 1; n   else if (fabs(b1) < 1e-14 && fabs(b2) < 1e-14) { n     a1 /= c1; n     a2 /= c2; n     if (fabs(a1 - a2) < 1e-14 || (fabs(a1) == INFINITY && fabs(a2) == INFINITY)) n       cout << -1; n     else n       cout << 0; n   } else { n     a1 /= b1; n     a2 /= b2; n     c1 /= b1; n     c2 /= b2; n     if (fabs(a1 - a2) < 1e-14) { n       if (fabs(c1 - c2) < 1e-14) n         cout << -1; n       else n         cout << 0; n     } else n       cout << 1; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[12], b[20], c[5], u[15], vs, hh; n string v[2033333]; n void add(int o, int s, int k) { n   if (s > k) return; n   if (o == 4) { n     if (k - s > 2) return; n     if (k - s && b[s] == 0) return; n     if (k - s == 2 && b[s] * 100 + b[s + 1] * 10 + b[k] >= 256) return; n     string st =   ; n     for (int i = 1; i <= c[1]; i++) st += char(b[i] + 48); n     st +=  . ; n     for (int i = c[1] + 1; i <= c[1] + c[2]; i++) st += char(b[i] + 48); n     st +=  . ; n     for (int i = c[1] + c[2] + 1; i < s; i++) st += char(b[i] + 48); n     st +=  . ; n     for (int i = s; i <= k; i++) st += char(b[i] + 48); n     v[++vs] = st; n     return; n   } n   c[o] = 1; n   add(o + 1, s + 1, k); n   if (!b[s]) return; n   c[o] = 2; n   add(o + 1, s + 2, k); n   if (b[s] * 100 + b[s + 1] * 10 + b[s + 2] >= 256) return; n   c[o] = 3; n   add(o + 1, s + 3, k); n } n void cty(int k) { n   for (int i = 1; i <= n; i++) { n     b[k] = a[i]; n     if (!u[i]) hh++; n     u[i]++; n     if (k >= 2) { n       do { n         if (hh != n) break; n         for (int j = 1; j <= k; j++) b[(k << 1) - j + 1] = b[j]; n         add(1, 1, k << 1); n         if (k != 2) { n           for (int j = 1; j < k; j++) b[(k << 1) - j] = b[j]; n           add(1, 1, (k << 1) - 1); n         } n       } while (0); n     } n     if (k < 6) cty(k + 1); n     u[i]--; n     if (!u[i]) hh--; n   } n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , a + i); n   cty(1); n   sort(v + 1, v + vs + 1); n   printf( %d n , vs); n   for (int i = 1; i <= vs; i++) { n     int l = v[i].size(); n     for (int j = 0; j < l; j++) printf( %c , v[i][j]); n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool win(long long A, long long B) { n   if (A == 0 || B == 0) return false; n   if (A > B) swap(A, B); n   if (!win(A, B % A)) return true; n   long long N = B / A; n   long long MOD = A + 1; n   return ((N % MOD) % 2 == 0); n } n int main(void) { n   int T, t; n   long long A, B; n   cin >> T; n   for ((t) = 0; (t) < (int)(T); (t)++) { n     cin >> A >> B; n     bool ans = win(A, B); n     cout << (ans ?  First  :  Second ) << 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 *b = new int[n]; n   int max_val = 0; n   for (int i = 0; i < n; i++) { n     cin >> b[i]; n     if (i == 0) { n     } else if (i == 1) { n       b[i] = b[0] + b[1]; n       max_val = (b[0] > b[1]) ? b[0] : b[1]; n     } else { n       b[i] = max_val + b[i]; n       if (max_val < b[i]) max_val = b[i]; n     } n     cout << b[i] <<    ; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, cnt = 1; n   cin >> n; n   int x[n]; n   for (int i = 0; i < n; i++) { n     cin >> x[i]; n   } n   for (int i = 0; i < n - 1; i++) { n     if (x[i] != x[i + 1]) { n       cnt++; n     } n   } n   cout << cnt << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize(3) n using namespace std; n const long long mod = 1e9 + 7; n const long long maxn = 1e5 + 10; n long long n, x, y; n long long money[maxn]; n pair<long long, long long> show[maxn]; n multiset<pair<long long, long long> > s; n inline long long read() { n   long long x = 0, f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   return x * f; n } n int main() { n   long long ans = 0; n   n = read(), x = read(), y = read(); n   for (long long i = 1; i <= n; i++) { n     show[i].first = read(), show[i].second = read(); n     s.insert({show[i].second, show[i].first}); n   } n   sort(show + 1, show + 1 + n); n   for (long long i = 1; i <= n; i++) { n     money[i] = (x + y * (show[i].second - show[i].first)); n     if (!s.size() || (*s.begin()).first >= show[i].first) continue; n     multiset<pair<long long, long long> >::iterator iter = n         s.lower_bound({show[i].first, 0}); n     long long tmp = (*(--iter)).first; n     if (y * (show[i].second - tmp) >= money[i]) continue; n     money[i] = y * (show[i].second - tmp); n     s.erase(iter); n   } n   for (long long i = 1; i <= n; i++) { n     ans += money[i]; n     ans %= mod; n   } n   ans %= mod; n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 9; n long long Pow(long long a, long long x) { n   long long res = 1; n   while (x) { n     if (x & 1) res = res * a % mod; n     a = a * a % mod; n     x >>= 1; n   } n   return res; n } n long long inv(long long a) { return Pow(a, mod - 2); } n long long n, a, b, k; n string s; n int main() { n   std::ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> a >> b >> k; n   cin >> s; n   long long ans = 0; n   for (int i = 0; i < k; ++i) { n     int f = s[i] ==  -  ? -1 : 1; n     ans = (ans + (f * Pow(a, n - i) * Pow(b, i) % mod + mod) % mod) % mod; n   } n   long long q = Pow(inv(a) * b % mod, k); n   if (q == 1) { n     long long res = (n + 1) / k; n     ans = ans * res % mod; n   } else { n     long long res = Pow(q, (n + 1) / k); n     res--; n     if (res < 0) res += mod; n     res = res * inv(q - 1) % mod; n     ans = res * ans % mod; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n T gcd(T a, T b) { n   if (a == 0) return b; n   return gcd(b % a, a); n } n template <typename T> n T pow(T a, T b, long long m) { n   T ans = 1; n   while (b > 0) { n     if (b % 2 == 1) ans = (ans * a) % m; n     b /= 2; n     a = (a * a) % m; n   } n   return ans % m; n } n bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) { n   return (a.second < b.second); n } n bool sortinrev(const pair<int, int> &a, const pair<int, int> &b) { n   return (a.first > b.first); n } n int main() { n   long long i, t, a; n   cin >> t; n   while (t--) { n     cin >> a; n     double z = abs(a - 180); n     z = (double)360 / z; n     if (floor(z) == ceil(z)) { n       cout <<  YES ; n     } else { n       cout <<  NO ; n     } n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n multiset<long long> mset; n void add(long long k) { mset.insert(k); } n long long getmin() { return *mset.begin(); } n long long getmax() { return *prev(mset.end()); } n long long lower(long long k) { n   if (mset.size() == 0) return -1; n   if (k < getmin()) return -1; n   if (mset.count(k)) return k; n   auto it = mset.lower_bound(k); n   return *prev(it); n } n long long uper(long long k) { n   if (mset.size() == 0) return -1; n   auto it = mset.lower_bound(k); n   if (it == mset.end()) return -1; n   return *it; n } n void erase(long long k) { mset.erase(mset.find(k)); } n long long t, n; n const long long inf = 0x3f3f3f3f; n long long dp[200005][2], a[200005]; n signed main() { n   ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); n   long long t; n   cin >> t; n   while (t--) { n     cin >> n; n     for (long long i = 0; i < n; i++) { n       cin >> a[i]; n       dp[i][1] = inf; n       dp[i][0] = inf; n     } n     dp[0][0] = a[0]; n     dp[1][0] = dp[0][0] + a[1]; n     dp[1][1] = dp[0][0]; n     for (long long i = 2; i < n; i++) { n       dp[i][0] = min(dp[i - 1][1] + a[i], dp[i - 2][1] + a[i - 1] + a[i]); n       dp[i][1] = min(dp[i - 1][0], dp[i - 2][0]); n     } n     cout << min(dp[n - 1][0], dp[n - 1][1]) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 1e5, maxl = 20; n bool mark[maxn]; n long long sum = 0, cmp, d[maxn]; n pair<long long, long long> par[maxn][maxl]; n vector<pair<long long, long long> > adj[maxn]; n struct Type_Dsu { n   long long par[maxn], h[maxn]; n   void make_set(long long x) { n     par[x] = x; n     h[x] = 0; n   } n   long long findpar(long long x) { n     return par[x] = (x == par[x] ? x : findpar(par[x])); n   } n   bool merge(long long x, long long y) { n     x = findpar(x); n     y = findpar(y); n     if (x == y) return false; n     if (d[x] > d[y]) swap(x, y); n     par[x] = y; n     h[y] += (h[x] == h[y]); n     return true; n   } n   bool same(long long x, long long y) { return (findpar(x) == findpar(y)); } n } dsu; n struct Edge { n   long long fi, se, w; n }; n vector<Edge> edges; n void input() { n   long long n, m; n   cin >> n >> m; n   cmp = n; n   for (long long i = 0; i < n; i++) dsu.make_set(i); n   for (long long i = 0; i < m; i++) { n     long long fi, se, w; n     cin >> fi >> se >> w; n     fi--; n     se--; n     Edge edg; n     edg.fi = fi; n     edg.se = se; n     edg.w = w; n     edges.push_back(edg); n   } n } n bool comp(Edge a, Edge b) { return (a.w < b.w); } n void mst() { n   sort(edges.begin(), edges.end(), comp); n   for (long long i = 0; i < edges.size(); i++) { n     long long fi = edges[i].fi, se = edges[i].se, w = edges[i].w; n     if (!dsu.same(fi, se)) { n       cmp--; n       sum += w; n       dsu.merge(fi, se); n       adj[fi].push_back(make_pair(w, se)); n       adj[se].push_back(make_pair(w, fi)); n     } n   } n } n void dfs(long long x) { n   mark[x] = true; n   for (long long i = 0; i < adj[x].size(); i++) { n     long long child = adj[x][i].second, w = adj[x][i].first; n     if (mark[child]) continue; n     par[child][0] = make_pair(x, w); n     d[child] = d[x] + 1; n     for (long long j = 1; j < maxl; j++) { n       long long p = par[par[child][j - 1].first][j - 1].first; n       long long maxe = max(par[child][j - 1].second, n                            par[par[child][j - 1].first][j - 1].second); n       par[child][j] = make_pair(p, maxe); n     } n     dfs(child); n   } n } n pair<long long, long long> findpar(long long x, long long d) { n   long long p = x, maxe = 0; n   for (long long i = 0; i < maxl; i++) n     if ((d >> i) & 1) { n       maxe = max(maxe, par[p][i].second); n       p = par[p][i].first; n     } n   return make_pair(p, maxe); n } n long long lca(long long x, long long y) { n   long long maxe = 0; n   if (d[x] > d[y]) swap(x, y); n   maxe = findpar(y, d[y] - d[x]).second; n   y = findpar(y, d[y] - d[x]).first; n   if (x == y) return maxe; n   for (long long i = maxl - 1; i >= 0; i--) n     if (par[x][i].first != par[y][i].first) { n       maxe = max(maxe, par[x][i].second); n       maxe = max(maxe, par[y][i].second); n       x = par[x][i].first; n       y = par[y][i].first; n     } n   maxe = max(maxe, par[x][0].second); n   maxe = max(maxe, par[y][0].second); n   return maxe; n } n void output() { n   long long q; n   cin >> q; n   for (long long i = 0; i < q; i++) { n     long long A, B; n     cin >> A >> B; n     A--; n     B--; n     cout << endl; n     if (cmp > 2) cout << -1 << endl; n     if (cmp == 2) n       if (dsu.same(A, B)) n         cout << -1 << endl; n       else n         cout << sum << endl; n     if (cmp == 1) cout << sum - lca(A, B) << endl; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   input(); n   mst(); n   dfs(0); n   output(); n   return 0; n } n


#include <bits/stdc++.h> n size_t solve(const std::vector<std::string>& c) { n   const size_t n = c.size(); n   size_t count = 0; n   for (size_t i = 0; i < n; ++i) { n     for (size_t j = 0; j < n; ++j) { n       for (size_t k = j + 1; k < n; ++k) { n         if (c[i][j] ==  C  && c[i][k] ==  C ) ++count; n         if (c[j][i] ==  C  && c[k][i] ==  C ) ++count; n       } n     } n   } n   return count; n } n int main() { n   size_t n; n   std::cin >> n; n   std::vector<std::string> c(n); n   for (size_t i = 0; i < n; ++i) std::cin >> c[i]; n   std::cout << solve(c) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5; n int main() { n   vector<int> goh_nakhor[maxn]; n   int n, k, ans = 0; n   cin >> n >> k; n   for (int i = 0; i < k; i++) { n     int num, tmp; n     cin >> num >> tmp; n     if (tmp != 1) ans += num - 1; n     for (int i = 0; i < num - 1; i++) { n       int val; n       cin >> val; n       goh_nakhor[tmp].push_back(val); n     } n   } n   int cnt = 0; n   for (int i = 0; i < goh_nakhor[1].size(); i++) { n     if (goh_nakhor[1][i] == i + 2) cnt++; n   } n   cout << ans + (goh_nakhor[1].size() - cnt - 1) + n - cnt <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n char color[16]; n int r[16], b[16]; n int redCount[1 << 16], blueCount[1 << 16]; n int dp[1 << 16][2][257]; n int calc(int mask, bool noRed, int other) { n   other = min(other, n * n); n   if (dp[mask][noRed][other] != -1) return dp[mask][noRed][other]; n   if (mask == (1 << n) - 1) return dp[mask][noRed][other] = 0; n   dp[mask][noRed][other] = 1000000000; n   for (int i = 0; i < n; ++i) { n     if (mask & (1 << i)) continue; n     int nextMask = mask | (1 << i); n     int payRed = max(0, r[i] - redCount[mask]), n         payBlue = max(0, b[i] - blueCount[mask]); n     int haveRed = 0, haveBlue = 0; n     if (noRed) n       haveBlue = other; n     else n       haveRed = other; n     int add = max(max(payRed - haveRed, payBlue - haveBlue), 0); n     haveRed += add, haveBlue += add; n     haveRed -= payRed, haveBlue -= payBlue; n     if (haveRed == 0) n       dp[mask][noRed][other] = n           min(dp[mask][noRed][other], add + calc(nextMask, 1, haveBlue)); n     else if (haveBlue == 0) n       dp[mask][noRed][other] = n           min(dp[mask][noRed][other], add + calc(nextMask, 0, haveRed)); n   } n   return dp[mask][noRed][other]; n } n int main() { n   cin >> n; n   for (int i = 0; i < n; ++i) cin >> color[i] >> r[i] >> b[i]; n   for (int i = 0; i < 1 << n; ++i) { n     for (int j = 0; j < n; ++j) { n       if ((i & (1 << j)) == 0) continue; n       if (color[j] ==  R ) n         redCount[i]++; n       else n         blueCount[i]++; n     } n     for (int j = 0; j < 2; ++j) n       for (int k = 0; k <= 256; ++k) dp[i][j][k] = -1; n   } n   int needRed = 0, needBlue = 0; n   for (int i = 0; i < n; ++i) { n     needRed += max(0, r[i] - n); n     needBlue += max(0, b[i] - n); n     r[i] = min(r[i], n); n     b[i] = min(b[i], n); n   } n   if (needRed >= needBlue) n     cout << needRed + n + calc(0, 1, needRed - needBlue); n   else n     cout << needBlue + n + calc(0, 0, needBlue - needRed); n   cout <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e9 + 5; n struct segment_change { n   int to_add; n   segment_change(int _to_add = 0) : to_add(_to_add) {} n   void reset() { to_add = 0; } n   bool has_change() const { return to_add != 0; } n   segment_change combine(const segment_change &other) const { n     return segment_change(to_add + other.to_add); n   } n }; n struct segment { n   int maximum; n   segment(int _maximum = -INF) : maximum(_maximum) {} n   void apply(int, const segment_change &change) { maximum += change.to_add; } n   void join(const segment &other) { maximum = max(maximum, other.maximum); } n   void join(const segment &a, const segment &b) { n     *this = a; n     join(b); n   } n }; n struct add_max_seg_tree { n   int tree_n = 0; n   vector<segment> tree; n   vector<segment_change> changes; n   add_max_seg_tree(int n = 0) { n     if (n > 0) init(n); n   } n   void init(int n) { n     tree_n = 1; n     while (tree_n < n) tree_n *= 2; n     tree.assign(2 * tree_n, segment()); n     changes.assign(tree_n, segment_change()); n   } n   void build(const vector<segment> &initial) { n     int n = initial.size(); n     assert(n <= tree_n); n     for (int i = 0; i < n; i++) tree[tree_n + i] = initial[i]; n     for (int position = tree_n - 1; position > 0; position--) n       tree[position].join(tree[2 * position], tree[2 * position + 1]); n   } n   void push_down(int position, int length) { n     if (!changes[position].has_change()) return; n     if (2 * position < tree_n) { n       changes[2 * position] = changes[2 * position].combine(changes[position]); n       changes[2 * position + 1] = n           changes[2 * position + 1].combine(changes[position]); n     } n     tree[2 * position].apply(length / 2, changes[position]); n     tree[2 * position + 1].apply(length / 2, changes[position]); n     changes[position].reset(); n   } n   void push_all(int a, int b) { n     assert(0 <= a && a < b && b <= tree_n); n     a += tree_n; n     b += tree_n - 1; n     for (int up = 31 - __builtin_clz(tree_n); up > 0; up--) { n       int x = a >> up, y = b >> up; n       push_down(x, 1 << up); n       if (x != y) push_down(y, 1 << up); n     } n   } n   void join_and_apply(int position, int length) { n     tree[position].join(tree[2 * position], tree[2 * position + 1]); n     tree[position].apply(length, changes[position]); n   } n   void join_all(int a, int b) { n     assert(0 <= a && a < b && b <= tree_n); n     a += tree_n; n     b += tree_n - 1; n     int length = 1; n     while (a > 1) { n       a /= 2; n       b /= 2; n       length *= 2; n       join_and_apply(a, length); n       if (a != b) join_and_apply(b, length); n     } n   } n   pair<int, int> right_half[32]; n   template <typename T_range_op> n   void process_range(int a, int b, bool needs_join, T_range_op &&range_op) { n     if (a == b) return; n     push_all(a, b); n     int original_a = a, original_b = b; n     int length = 1, r_size = 0; n     for (a += tree_n, b += tree_n; a < b; a /= 2, b /= 2, length *= 2) { n       if (a & 1) range_op(a++, length); n       if (b & 1) right_half[r_size++] = {--b, length}; n     } n     for (int i = r_size - 1; i >= 0; i--) n       range_op(right_half[i].first, right_half[i].second); n     if (needs_join) join_all(original_a, original_b); n   } n   segment query(int a, int b) { n     assert(0 <= a && a <= b && b <= tree_n); n     segment answer; n     process_range(a, b, false, n                   [&](int position, int) { answer.join(tree[position]); }); n     return answer; n   } n   void update(int a, int b, const segment_change &change) { n     assert(0 <= a && a <= b && b <= tree_n); n     process_range(a, b, true, [&](int position, int length) { n       tree[position].apply(length, change); n       if (position < tree_n) n         changes[position] = changes[position].combine(change); n     }); n   } n   void set_value(int index, int value) { n     assert(0 <= index && index < tree_n); n     int current = query(index, index + 1).maximum; n     update(index, index + 1, segment_change(value - current)); n   } n }; n int N; n vector<int> parent; n vector<vector<int>> children; n vector<int> tour_start, tour_end; n int tour; n void dfs(int node) { n   tour_start[node] = tour++; n   for (int child : children[node]) dfs(child); n   tour_end[node] = tour; n } n template <typename T> n ostream &operator<<(ostream &os, const vector<T> &v) { n   os <<  { ; n   for (typename vector<T>::const_iterator vi = v.begin(); vi != v.end(); ++vi) { n     if (vi != v.begin()) os <<  ,  ; n     os << *vi; n   } n   os <<  } ; n   return os; n } n template <> n ostream &operator<<(ostream &os, const vector<string> &v) { n   os <<  { ; n   for (vector<string>::const_iterator vi = v.begin(); vi != v.end(); ++vi) { n     if (vi != v.begin()) os <<  ,  ; n     os <<     << *vi <<    ; n   } n   os <<  } ; n   return os; n } n vector<int> solve(vector<int> perm) { n   assert((int)perm.size() == N); n   assert(perm.front() == 1); n   for (int &p : perm) p--; n   parent.assign(N, -1); n   vector<pair<int, int>> events; n   vector<int> stack = {-1}; n   for (int x : perm) { n     int erased = -1; n     while (stack.back() > x) { n       erased = stack.back(); n       stack.pop_back(); n     } n     events.emplace_back(x, stack.back()); n     events.emplace_back(erased, x); n     parent[x] = stack.back(); n     if (erased >= 0) parent[erased] = x; n     stack.push_back(x); n   } n   children.assign(N, {}); n   tour_start.resize(N); n   tour_end.resize(N); n   for (int i = 0; i < N; i++) n     if (parent[i] >= 0) children[parent[i]].push_back(i); n   tour = 0; n   dfs(0); n   vector<int> answers(N, INF); n   add_max_seg_tree tree(N); n   for (int i = 0; i < N; i++) { n     int a = events[2 * i].first; n     int b = events[2 * i].second; n     assert(a >= 0); n     tree.set_value( n         tour_start[a], n         b < 0 ? 1 : tree.query(tour_start[b], tour_start[b] + 1).maximum + 1); n     a = events[2 * i + 1].first; n     b = events[2 * i + 1].second; n     if (a >= 0) { n       tree.update(tour_start[a], tour_end[a], segment_change(+1)); n     } n     answers[i] = tree.query(0, N).maximum; n   } n   return answers; n } n vector<int> A; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   cin >> N; n   A.resize(N); n   for (int &a : A) cin >> a; n   int one = find(A.begin(), A.end(), 1) - A.begin(); n   vector<int> start_one, end_one; n   for (int i = one; (int)start_one.size() < N; i = (i + 1) % N) n     start_one.push_back(A[i]); n   for (int i = one; (int)end_one.size() < N; i = (i + N - 1) % N) n     end_one.push_back(A[i]); n   vector<int> left = solve(end_one), right = solve(start_one); n   int best_left = 0, best = INF; n   for (int i = 0; i < N; i++) { n     if (max(left[i], right[N - 1 - i]) < best) { n       best = max(left[i], right[N - 1 - i]); n       best_left = i; n     } n   } n   cout << best <<     << (one - best_left + N) % N <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int c1, c2, c3, c4, n, m; n int rs1, rs2, rs3, rs4, rs; n int a, b; n int main() { n   ios_base::sync_with_stdio(0); n   ; n   cin.tie(0); n   ; n   cin >> c1 >> c2 >> c3 >> c4; n   cin >> n >> m; n   for (int i = 1; i <= n; i++) { n     cin >> a; n     rs1 += min(a * c1, c2); n   } n   rs2 = min(rs1, c3); n   for (int i = 1; i <= m; i++) { n     cin >> b; n     rs3 += min(b * c1, c2); n   } n   rs4 = min(rs3, c3); n   cout << min(rs2 + rs4, c4); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1000005; n const int MOD = 1000000007; n const int INF = 0x3f3f3f3f; n void add(int &x, int y) { n   x += y; n   if (x >= MOD) x -= MOD; n } n int n, h[N]; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &h[i]); n     h[i]--; n   } n   h[n + 1] = INF; n   int ans = h[1], sum = min(h[1], h[2]); n   for (int i = 2; i <= n; i++) { n     add(ans, h[i]); n     add(ans, 1LL * sum * min(h[i - 1], h[i]) % MOD); n     sum = 1LL * sum * min(min(h[i - 1], h[i]), h[i + 1]) % MOD; n     add(sum, min(h[i], h[i + 1])); n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n inline long long int min(long long int a, long long int b) { n   return (a < b) ? a : b; n } n long long int dp[303][303], cost[303][303]; n int main() { n   int n, m, k, o, j, i; n   scanf( %d %d %d , &n, &m, &o); n   for (i = 0; i <= n; i++) n     for (j = 0; j <= n; j++) dp[i][j] = cost[i][j] = 10000000000000LL; n   for (i = 0; i < m; i++) { n     int l, r, c; n     scanf( %d %d %d , &l, &r, &c); n     cost[l][r] = min(cost[l][r], c); n   } n   for (i = 1; i <= n; i++) n     for (j = 1; j <= i; j++) n       for (k = i; k <= n; k++) { n         cost[j][i] = min(cost[j][i], cost[j][k]); n         cost[i][k] = min(cost[i][k], cost[j][k]); n       } n   for (i = 0; i <= n; i++) { n     dp[i][0] = 0; n     for (j = 1; j <= i; j++) { n       dp[i][j] = min(cost[i - j + 1][i], dp[i - 1][j]); n       for (k = 1; k < j; k++) n         dp[i][j] = min(dp[i][j], cost[i - k + 1][i] + dp[i - k][j - k]); n     } n   } n   if (dp[n][o] != 10000000000000LL) n     printf( %I64d n , dp[n][o]); n   else n     printf( -1 n ); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ,  unroll-loops ) n using namespace std; n const long long inf = 1ll << 60; n const long long iinf = 2147483647; n const long long mod = 998244353; n const long long maxn = 1e3 + 5; n long long pw(long long x, long long p) { n   long long ret = 1; n   while (p > 0) { n     if (p & 1) { n       ret *= x; n       ret %= mod; n     } n     x *= x; n     x %= mod; n     p >>= 1; n   } n   return ret; n } n long long inv(long long x) { return pw(x, mod - 2); } n long long dp[5005][5005]; n signed main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   ; n   long long n; n   cin >> n; n   vector<long long> vc(n); n   for (long long i = (0); i < (n); i++) { n     cin >> vc[i]; n   } n   for (long long i = (1); i < (n + 1); i++) { n     for (long long j = (1); j < (n - i + 1 + 1); j++) { n       if (i == 1) { n         dp[i][j] = vc[j - 1]; n         continue; n       } n       dp[i][j] = dp[i - 1][j] ^ dp[i - 1][j + 1]; n     } n   } n   for (long long i = (1); i < (n + 1); i++) { n     for (long long j = (1); j < (n - i + 1 + 1); j++) { n       dp[i][j] = max({dp[i][j], dp[i - 1][j], dp[i - 1][j + 1]}); n     } n   } n   long long q; n   cin >> q; n   for (long long i = (0); i < (q); i++) { n     long long l, r; n     cin >> l >> r; n     cout << dp[r - l + 1][l] <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, k; n   cin >> n >> m >> k; n   deque<int> items; n   int tmp; n   for (int i = 0; i < k; i++) { n     cin >> tmp; n     items.push_back(tmp); n   } n   int ans = 0; n   for (int i = 0; i < n * m; i++) { n     cin >> tmp; n     for (int j = 0; j < items.size(); j++) { n       if (items[j] == tmp) { n         ans += j + 1; n         items.erase(items.begin() + j); n         items.push_front(tmp); n         break; n       } n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const unsigned long long x = 31; n unsigned long long X[60000]; n int Ql[20], Qr[20]; n char str[60000], s[60000]; n int N; n map<unsigned long long, int> T[20]; n map<unsigned long long, bool> A; n void add(int index, unsigned long long H) { T[index][H] = T[index][H] + 1; } n void CalcHashes(char s[], int index) { n   unsigned long long H; n   int i, j, L = strlen(s); n   for (i = 1; i <= L; i++) { n     H = 0; n     for (j = 0; j < i; j++) H += X[i - j - 1] * (s[j] -  a  + 1); n     add(index, H); n     for (j = i; j < L; j++) { n       H = (H - X[i - 1] * (s[j - i] -  a  + 1)) * x + (s[j] -  a  + 1); n       add(index, H); n     } n   } n } n int answer; n void check(unsigned long long H) { n   if (A[H]) return; n   A[H] = true; n   int i, t; n   bool v = true; n   for (i = 0; i < N && v; i++) { n     t = T[i][H]; n     if (t < Ql[i] || Qr[i] < t) v = false; n   } n   if (v) answer++; n } n int main() { n   unsigned long long H; n   int i, j, L; n   for (X[0] = 1, i = 1; i <= 60000; i++) X[i] = X[i - 1] * x; n   scanf( %s , str); n   scanf( %d , &N); n   for (i = 0; i < N; i++) { n     scanf( %s%d%d , &s, &Ql[i], &Qr[i]); n     CalcHashes(s, i); n   } n   L = strlen(str); n   for (i = 1; i <= L; i++) { n     H = 0; n     for (j = 0; j < i; j++) H += X[i - j - 1] * (str[j] -  a  + 1); n     check(H); n     for (j = i; j < L; j++) { n       H = (H - X[i - 1] * (str[j - i] -  a  + 1)) * x + (str[j] -  a  + 1); n       check(H); n     } n   } n   printf( %d , answer); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, i; n   scanf( %d , &n); n   for (i = 1; i <= (n / 2); i++) { n     if (i & 1) n       cout <<  ab ; n     else n       cout <<  ba ; n   } n   int k = n / 2; n   if (n & 1) { n     if ((k) % 2 == 0) n       printf( a ); n     else n       printf( b ); n   } n } n


#include<bits/stdc++.h> n using namespace std; n  n int main(){ n  int t,n,x; n  set<int> notes; n  cin>>t; n  while(t--){ n   cin>>n; n   notes.clear(); n   for(int i=0;i<n;i++){ n    cin>>x; n    if(notes.find(x)==notes.end()){ n     notes.insert(x); n    }else{ n     notes.insert(x+1); n    } n   } n   cout<<notes.size()<<endl; n  } n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1e6 + 7; n struct node { n   long long l, r, i; n }; n bool cmp(node &a, node &b) { return a.l < b.l; } n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long t; n   t = 1; n   cin >> t; n   while (t--) { n     long long n; n     cin >> n; n     long long x, y; n     bool flag = 0; n     vector<long long> ans(n); n     vector<node> a; n     for (long long i = 0; i < n; i++) { n       cin >> x >> y; n       a.push_back({x, y, i}); n     } n     sort((a).begin(), (a).end(), cmp); n     long long p = -1; n     x = a[0].r; n     for (long long i = 1; i < n; i++) { n       if (x < a[i].l) { n         p = i; n         break; n       } n       x = max(x, a[i].r); n     } n     if (p == -1) n       cout << -1 <<   n ; n     else { n       for (long long i = 0; i < p; i++) ans[a[i].i] = 1; n       for (long long i = p; i < n; i++) ans[a[i].i] = 2; n       for (auto i : ans) cout << i <<    ; n       cout <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 10; n const double eps = 1e-9; n const int inf = 1e9; n int deg[N]; n int main() { n   int t; n   scanf( %d , &t); n   while (t--) { n     int n, x; n     scanf( %d%d , &n, &x); n     for (int i = 0; i < n + 10; i++) deg[i] = 0; n     for (int i = 1; i < n; i++) { n       int u, v; n       scanf( %d%d , &u, &v); n       deg[u]++; n       deg[v]++; n     } n     if (deg[x] < 2) n       printf( Ayush n ); n     else { n       if (n & 1) n         printf( Ashish n ); n       else n         printf( Ayush n ); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   cout <<  YES n ; n   for (int i = 1; i <= n; i++) { n     int x1, x2, y1, y2; n     cin >> x1 >> y1 >> x2 >> y2; n     cout << (2 * ((x1 % 2 + 2) % 2) + (y1 % 2 + 2) % 2) + 1 <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int get() { n   char ch; n   while (ch = getchar(), (ch <  0  || ch >  9 ) && ch !=  - ) n     ; n   if (ch ==  - ) { n     int s = 0; n     while (ch = getchar(), ch >=  0  && ch <=  9 ) s = s * 10 + ch -  0 ; n     return -s; n   } n   int s = ch -  0 ; n   while (ch = getchar(), ch >=  0  && ch <=  9 ) s = s * 10 + ch -  0 ; n   return s; n } n const int N = 1e3 + 5; n int n; n struct point { n   int x, y, col, id; n   void read() { n     x = get(); n     y = get(); n     col = get(); n   } n } a[N]; n int cross(point a, point b, point c) { n   return (b.x - a.x) * (c.y - a.y) - (c.x - a.x) * (b.y - a.y); n } n int operator*(point a, point b) { return a.x * b.y - a.y * b.x; } n point operator-(point a, point b) { n   a.x -= b.x, a.y -= b.y; n   return a; n } n point operator+(point a, point b) { n   a.x += b.x, a.y += b.y; n   return a; n } n int be[N]; n point sta[N]; n int top; n bool cmp(point a, point b) { return a.x == b.x ? a.y < b.y : a.x < b.x; } n struct edge { n   int x, y; n   edge(const int x_ = 0, const int y_ = 0) { n     x = x_; n     y = y_; n   } n } ed[N]; n int tot; n point que[N]; n int k; n bool inside(point a, point b, point c, point q) { n   return ((cross(a, b, q) >= 0) != (cross(a, c, q) >= 0)) && n          ((cross(b, a, q) >= 0) != cross(b, c, q) >= 0) && n          ((cross(c, a, q) >= 0) != (cross(c, b, q) >= 0)); n } n void solve(point A, point B, point C) { n   for (int i = 1; i <= n; i++) n     if (a[i].col == C.col && inside(A, B, C, a[i])) { n       ed[++tot] = edge(C.id, a[i].id); n       solve(A, B, a[i]); n       solve(a[i], C, A); n       solve(a[i], C, B); n       return; n     } n   for (int i = 1; i <= n; i++) n     if (inside(A, B, C, a[i])) ed[++tot] = edge(A.id, a[i].id); n } n int main() { n   n = get(); n   int all = 0; n   for (int i = 1; i <= n; i++) { n     a[i].read(); n     a[i].id = i; n     all |= 1 << a[i].col; n   } n   if (all != 3) { n     printf( %d n , n - 1); n     for (int i = 1; i <= n - 1; i++) printf( %d %d n , 0, i); n     return 0; n   } n   sort(a + 1, a + 1 + n, cmp); n   top = 0; n   for (int i = 1; i <= n; i++) { n     if (top && sta[top].x == a[i].x) continue; n     while (top > 1 && cross(sta[top - 1], a[i], sta[top]) >= 0) top--; n     sta[++top] = a[i]; n   } n   for (int i = 1; i <= top; i++) be[sta[i].id] = 1; n   top = 0; n   for (int i = n; i >= 1; i--) { n     if (top && sta[top].x == a[i].x) continue; n     while (top > 1 && cross(sta[top - 1], a[i], sta[top]) >= 0) top--; n     sta[++top] = a[i]; n   } n   for (int i = 1; i <= top; i++) n     if (!be[sta[i].id]) be[sta[i].id] = 2; n   k = 0; n   for (int i = 1; i <= n; i++) n     if (be[a[i].id] == 1) que[++k] = a[i]; n   for (int i = n; i >= 1; i--) n     if (be[a[i].id] == 2) que[++k] = a[i]; n   int cnt = 0, keyw = 0; n   for (int i = 1; i <= k; i++) n     if (que[i].col != que[(i + k - 2) % k + 1].col) { n       cnt++; n       keyw = i; n     } n   if (cnt > 2) return printf( Impossible n ), 0; n   if (cnt == 0) { n     for (int i = 1; i <= k - 1; i++) ed[++tot] = edge(que[i].id, que[i + 1].id); n     point other; n     for (int i = 1; i <= n; i++) n       if (a[i].col != que[1].col) { n         other = a[i]; n         break; n       } n     for (int i = 1; i <= k - 1; i++) solve(que[i], que[i + 1], other); n     solve(que[k], que[1], other); n   } else { n     int x = keyw; n     for (; que[x].col == que[keyw].col; x = x % k + 1) { n       if (que[x].col == que[x % k + 1].col) { n         solve(que[x], que[x % k + 1], que[(keyw + k - 2) % k + 1]); n         ed[++tot] = edge(que[x].id, que[x % k + 1].id); n       } n     } n     int lst = (x + k - 2) % k + 1; n     for (; x != keyw; x = x % k + 1) { n       if (que[x].col == que[x % k + 1].col) { n         solve(que[x], que[x % k + 1], que[lst]); n         ed[++tot] = edge(que[x].id, que[x % k + 1].id); n       } n     } n   } n   printf( %d n , tot); n   for (int i = 1; i <= tot; i++) printf( %d %d n , ed[i].x - 1, ed[i].y - 1); n   return 0; n } n


#include <bits/stdc++.h> n int sm(int a, int b) { n   if (a % b == 0) n     return b; n   else n     return a % b; n } n int main() { n   int n, m, s; n   scanf( %d%d%d , &n, &m, &s); n   printf( %I64d , (long long)sm(n, s) * sm(m, s) * ((n - 1) / s + 1) * n                       ((m - 1) / s + 1)); n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1e17 + 9; n long long n, m; n vector<int> num; n long long dp[100009][2]; n void solve(int tp) { n   multiset<int> s; n   multiset<int>::iterator it; n   if (tp == 0) { n     for (long long i = 1; i < (m + 1); ++i) { n       s.insert(num[i]); n       it = s.upper_bound(num[i]); n       if (it != s.end()) s.erase(it); n       dp[i][tp] = (long long)(s).size(); n     } n   } else { n     for (long long i = m + 1 - 1; i >= 1; i--) { n       s.insert(num[i]); n       it = s.upper_bound(num[i]); n       if (it != s.end()) s.erase(it); n       dp[i][tp] = (long long)(s).size(); n     } n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> m; n   long long acum[100009]; n   memset(acum, 0, sizeof acum); n   for (long long i = 0; i < (n); ++i) { n     long long a, b; n     cin >> a >> b; n     acum[a]++; n     acum[b + 1]--; n   } n   for (long long i = 1; i < (m + 2); ++i) { n     acum[i] += acum[i - 1]; n   } n   for (long long i = 0; i < (m + 1); ++i) { n     num.push_back(acum[i]); n   } n   solve(0); n   solve(1); n   long long ans = 0; n   for (long long i = 1; i < (m + 1); ++i) { n     ans = max({dp[i][0] + dp[i][1] - 1, ans}); n   } n   cout << ans <<   n ; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n class substr { n  public: n   string str; n   int id; n   bool operator<(const substr &a) const { return str > a.str; } n }; n priority_queue<substr> Q; n int main() { n   string s; n   int k, n; n   cin >> s >> k; n   n = s.length(); n   for (int i = 0; i < n; i++) { n     substr a; n     a.str = s[i]; n     a.id = i; n     Q.push(a); n   } n   while (!Q.empty()) { n     substr p; n     p = Q.top(); n     Q.pop(); n     k--; n     if (!k) { n       cout << p.str << endl; n       return 0; n     } n     if (p.id != n - 1) { n       p.str = p.str + s[p.id + 1]; n       p.id = p.id + 1; n       Q.push(p); n     } n   } n   cout <<  No such line.  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double dp[1 << 20][20], p[20][20]; n bool mark[1 << 20][20]; n int n; n double mem(int mask, int winner) { n   if (!mask) return (winner == 1); n   if (mark[mask][winner]) return dp[mask][winner]; n   mark[mask][winner] = true; n   double &ret = dp[mask][winner]; n   for (int i = 1; i < n; i++) n     if (mask & (1 << i)) { n       double tmp = 0; n       tmp += p[i][winner] * mem(mask ^ (1 << i), i); n       tmp += p[winner][i] * mem(mask ^ (1 << i), winner); n       ret = max(ret, tmp); n     } n   return ret; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin >> n; n   for (int i = 1; i <= n; i++) { n     p[i][0] = 1; n     for (int j = 1; j <= n; j++) cin >> p[i][j]; n   } n   n++; n   cout << setprecision(10) << fixed << mem((1 << n) - 2, 0) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = int(1e9) + 7; n int main() { n   ios::sync_with_stdio(false); n   ios_base::sync_with_stdio(false); n   long long n, m; n   cin >> n >> m; n   vector<long long> a(n); n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n   } n   if (n >= 1001) { n     cout << 0; n     return 0; n   } n   long long res = 1; n   for (int i = 0; i < n; ++i) n     for (int j = i + 1; j < n; ++j) res = (res * (abs(a[i] - a[j]))) % m; n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int dp[100005]; n   vector<int> t; n   vector<pair<int, int> > p; n   int r, n; n   cin >> r >> n; n   t.push_back(0); n   p.push_back(make_pair(1, 1)); n   for (int i = 0; i < n; i++) { n     dp[i] = 0; n     int x, y; n     cin >> x; n     t.push_back(x); n     cin >> x >> y; n     p.push_back(make_pair(x, y)); n   } n   int maxi = 0, ans = 0; n   dp[n] = 0; n   for (int i = n; i >= 0; i--) { n     for (int j = i - 1; j >= max(0, i - 2000); j--) { n       int dif = abs(p[i].first - p[j].first) + abs(p[i].second - p[j].second); n       if (dif <= abs(t[i] - t[j])) { n         dp[j] = max(dp[j], dp[i] + 1); n       } n     } n   } n   cout << dp[0] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 10005; n void solve() { n   string s; n   cin >> s; n   int n = s.size(); n   s =  a  + s; n   set<int> one, zero; n   vector<int> ans[n]; n   for (int i = 0; i < n; i++) zero.insert(i); n   for (int i = 1; i <= n; i++) { n     if (s[i] ==  0 ) { n       if (zero.size() == 0) { n         cout << -1; n         return; n       } else { n         auto it = zero.begin(); n         int val = *it; n         ans[val].push_back(i); n         zero.erase(it); n         one.insert(val); n       } n     } else { n       if (one.size() == 0) { n         cout << -1; n         return; n       } else { n         auto it = one.begin(); n         int val = *it; n         ans[val].push_back(i); n         one.erase(it); n         zero.insert(val); n       } n     } n   } n   int cnt = 0; n   for (int i = 0; i < n; i++) { n     if (ans[i].size() == 0) break; n     if (s[ans[i][ans[i].size() - 1]] ==  1 ) { n       cout <<  -1 ; n       return; n     } n     cnt++; n   } n   cout << cnt <<   n ; n   for (int i = 0; i < cnt; i++) { n     cout << ans[i].size() <<    ; n     for (auto X : ans[i]) cout << X <<    ; n     cout <<   n ; n   } n } n int main() { n   ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   srand(time(NULL)); n   ; n   int t = 1; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int a[3010], id[3010], num[3010]; n vector<int> ans; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n   } n   int m; n   scanf( %d , &m); n   int need = n - m; n   while (true) { n     int tot = 0; n     memset(id, -1, sizeof(id)); n     memset(num, 0, sizeof(num)); n     for (int i = 1; i <= n; i++) { n       if (id[i] == -1) { n         id[i] = tot; n         int now = a[i]; n         while (now != i) { n           id[now] = tot; n           now = a[now]; n         } n         tot++; n       } n     } n     for (int i = 1; i <= n; i++) { n       num[id[i]]++; n     } n     if (tot == need) break; n     if (tot > need) { n       for (int i = 2; i <= n; i++) { n         if (id[i] != id[1]) { n           ans.push_back(1); n           ans.push_back(i); n           swap(a[1], a[i]); n           break; n         } n       } n     } else { n       for (int i = 1; i <= n; i++) { n         if (num[id[i]] == 1) continue; n         for (int j = i + 1; j <= n; j++) { n           if (id[i] == id[j]) { n             ans.push_back(i); n             ans.push_back(j); n             swap(a[i], a[j]); n             break; n           } n         } n         break; n       } n     } n   } n   printf( %d n , ans.size() / 2); n   for (int i = 0; i < ans.size(); i++) { n     if (i) printf(   ); n     printf( %d , ans[i]); n   } n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   int fl = 1; n   cin >> s; n   for (int i = 0; i < s.size(); ++i) { n     if (s[i] ==  1  || s[i] ==  2 ) n       fl = 0; n     else if (s[i] ==  3  && s[s.size() - i - 1] !=  3 ) n       fl = 0; n     else if (s[i] ==  7  && s[s.size() - i - 1] !=  7 ) n       fl = 0; n     else if (s[i] ==  4  && s[s.size() - i - 1] !=  6 ) n       fl = 0; n     else if (s[i] ==  6  && s[s.size() - i - 1] !=  4 ) n       fl = 0; n     else if (s[i] ==  5  && s[s.size() - i - 1] !=  9 ) n       fl = 0; n     else if (s[i] ==  9  && s[s.size() - i - 1] !=  5 ) n       fl = 0; n     else if (s[i] ==  8  && s[s.size() - i - 1] !=  0 ) n       fl = 0; n     else if (s[i] ==  0  && s[s.size() - i - 1] !=  8 ) n       fl = 0; n   } n   if (fl == 1) n     cout <<  Yes ; n   else n     cout <<  No ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool debug; n const int inf = 1e9 + 5; n const int nax = 205; n int k; n namespace MinCost { n struct Edge { n   int w, c, v, rev; n   Edge(int _w, int _c, int _v, int _rev) : w(_w), c(_c), v(_v), rev(_rev) {} n }; n int odl[nax], pot[nax], pop[nax], pop_kraw[nax]; n vector<Edge> v[nax]; n bool bylo[nax]; n void init(int n) { n   for (int i = 0; i <= n; ++i) v[i].clear(); n } n void AddEdge(int a, int b, int cap, int cost) { n   v[a].push_back(Edge(b, cap, cost, int(v[b].size()) + (a == b))); n   v[b].push_back(Edge(a, 0, -cost, int(v[a].size() - 1))); n } n pair<int, int> MinCostMaxFlow(int s, int t, int n) { n   int flow = 0, cost = 0; n   for (int i = 0; i <= n; ++i) pot[i] = 0; n   while (true) { n     for (int i = 0; i <= n; ++i) { n       odl[i] = inf; n       bylo[i] = false; n     } n     odl[s] = 0; n     for (int k = 0; k <= (n)-1; ++k) { n       int x = 0; n       int najm = inf; n       for (int i = 0; i <= n; ++i) n         if (!bylo[i] && najm > odl[i]) { n           x = i; n           najm = odl[i]; n         } n       bylo[x] = true; n       int dl = v[x].size(); n       for (int i = 0; i <= (dl)-1; ++i) n         if (v[x][i].c > 0 && n             odl[v[x][i].w] > odl[x] + pot[x] - pot[v[x][i].w] + v[x][i].v) { n           odl[v[x][i].w] = odl[x] + pot[x] - pot[v[x][i].w] + v[x][i].v; n           pop[v[x][i].w] = x; n           pop_kraw[v[x][i].w] = i; n         } n     } n     if (odl[t] == inf) break; n     for (int i = 0; i <= n; ++i) pot[i] += odl[i]; n     int x = t; n     int cap = inf; n     while (x != s) { n       cap = min(cap, v[pop[x]][pop_kraw[x]].c); n       x = pop[x]; n     } n     x = t; n     int one_path_cost = 0; n     while (x != s) { n       one_path_cost += v[pop[x]][pop_kraw[x]].v; n       v[pop[x]][pop_kraw[x]].c -= cap; n       v[x][v[pop[x]][pop_kraw[x]].rev].c += cap; n       x = pop[x]; n     } n     int ile_zmiesci = one_path_cost ? (k - cost) / one_path_cost : inf; n     cost += min(cap, ile_zmiesci) * one_path_cost; n     flow += min(cap, ile_zmiesci); n     if (ile_zmiesci < cap) break; n   } n   return make_pair(flow, cost); n } n };  // namespace MinCost n using namespace MinCost; n int n, a; n int main(int argc, char* argv[]) { n   scanf( %d%d , &n, &k); n   init(n - 1); n   for (int i = 0; i <= (n)-1; ++i) n     for (int j = 0; j <= (n)-1; ++j) { n       scanf( %d , &a); n       if (a > 0) { n         AddEdge(i, j, a, 0); n         AddEdge(i, j, k, 1); n       } n     } n   printf( %d n , MinCostMaxFlow(0, n - 1, n - 1).first); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int num[10]; n int main(void) { n   int T, n; n   scanf( %d , &T); n   while (T--) { n     memset(num, 0, sizeof(num)); n     scanf( %d , &n); n     num[1] = n / 2; n     if (n & 01) { n       printf( 7 ); n       for (int i = 1; i < num[1]; ++i) printf( 1 ); n     } else n       for (int i = 0; i < num[1]; ++i) printf( 1 ); n     if (T) printf(  n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int tree[2000005] = {0}, num[500005] = {0}, dp[500005] = {1}; n void modify(int index, int l, int r, int ql, int qr, int v) { n   if (ql <= l && qr >= r) { n     tree[index] = v; n     return; n   } n   int mid = (l + r) >> 1; n   if (ql <= mid && qr >= l) modify(index << 1, l, mid, ql, qr, v); n   if (ql <= r && qr >= mid + 1) modify(index << 1 | 1, mid + 1, r, ql, qr, v); n   tree[index] = max(tree[index << 1], tree[index << 1 | 1]); n } n int query(int index, int l, int r, int ql, int qr) { n   if (ql <= l && qr >= r) return tree[index]; n   int mid = (l + r) >> 1, ans = 0; n   if (ql <= mid && qr >= l) ans = max(ans, query(index << 1, l, mid, ql, qr)); n   if (ql <= r && qr >= mid + 1) n     ans = max(ans, query(index << 1 | 1, mid + 1, r, ql, qr)); n   return ans; n } n int main() { n   int i, n, k, d, index = 0; n   scanf( %d%d%d , &n, &k, &d); n   for (i = 1; i <= n; i++) scanf( %d , &num[i]); n   sort(num + 1, num + n + 1); n   modify(1, 0, n, 0, 0, 1); n   for (i = 1; i <= n; i++) { n     while (num[i] > num[index] + d) index++; n     if (i - k >= index - 1) n       dp[i] = query(1, 0, n, index - 1, i - k); n     else n       dp[i] = 0; n     modify(1, 0, n, i, i, dp[i]); n   } n   if (dp[n]) n     puts( YES ); n   else n     puts( NO ); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, k; n   long long res = 0; n   cin >> n >> m >> k; n   map<int, long long> dp; n   bool arr[n + 1], s[m + 1]; n   for (int i = 0; i < n; i++) cin >> arr[i]; n   arr[n] = 0; n   for (int i = 0; i < m; i++) cin >> s[i]; n   s[m] = 0; n   int ones = 0; n   for (int i = 0; i < n + 1; i++) { n     if (arr[i]) n       ones++; n     else if (ones) { n       if (dp.find(ones) != dp.end()) { n         res += dp[ones]; n         ones = 0; n         continue; n       } n       int tones = 0; n       long long dpres = 0; n       long long prev = 0; n       for (int j = 0; j < m + 1; j++) { n         if (s[j]) { n           tones++; n           dpres += prev; n           if (k % tones == 0 && tones * ones >= k) { n             dpres += ones - (k / tones) + 1; n             prev += ones - (k / tones) + 1; n           } n         } else { n           tones = 0; n           prev = 0; n         } n       } n       dp[ones] = dpres; n       res += dpres; n       ones = 0; n     } n   } n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s; n int n; n signed main() { n   ios_base ::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> s; n   vector<pair<int, int>> a; n   vector<int> p(n, 0), nxt(n, 0); n   stack<pair<int, int>> S; n   S.push({0, -1}); n   for (int i = 0; i < n; ++i) { n     if (s[i] ==  ( ) { n       p[i] = S.top().second; n       a.push_back({S.top().first + 1, i}); n       S.push({a.back().first, i}); n     } else { n       nxt[S.top().second] = i; n       S.pop(); n     } n   } n   sort(a.rbegin(), a.rend()); n   string k(n,  x ); n   for (int i = 0; i < int(a.size()); ++i) { n     int y = a[i].second; n     if (k[y] !=  x ) { n       k[nxt[y]] = k[y]; n       continue; n     } n     int c = -1, cnt = 0; n     for (y = a[i].second; y != -1 && k[y] ==  x ; y = p[y]) cnt ^= 1; n     if (y != -1) c = k[y] -  0 ; n     int t = 1; n     if (c != -1) t = c ^ cnt; n     for (y = a[i].second; y != -1 && k[y] ==  x ; t ^= 1, y = p[y]) n       k[y] = t +  0 ; n     k[nxt[a[i].second]] = k[a[i].second]; n   } n   cout << k <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n set<int> S; n void dfs(int Cur, int dgt, int x, int y) { n   if (dgt == 11) return; n   if (Cur > 0) S.insert(Cur); n   dfs(Cur * 10 + x, dgt + 1, x, y); n   dfs(Cur * 10 + y, dgt + 1, x, y); n   return; n } n int main() { n   int n, Ans = 0; n   cin >> n; n   S.insert(1e9); n   for (int i = 0; i < 10; i++) { n     for (int j = 0; j < 10; j++) { n       dfs(0, 1, i, j); n     } n   } n   for (auto x : S) { n     if (x > n) n       break; n     else n       Ans++; n   } n   cout << Ans << endl; n   return 0; n } n