#include <bits/stdc++.h> n using namespace std; n int n, m; n vector<pair<pair<int, int>, pair<int, int> > > ans; n bool invert; n void print(vector<vector<string> > &mat) { n   for (auto vs : mat) { n     for (string s : vs) { n       printf( |%6s|  , s.c_str()); n     } n     cout << endl; n   } n } n void move(int si, int sj, int ti, int tj) { n   assert(si != ti || sj != tj); n   if (si != ti && sj != tj) { n     move(si, sj, si, tj); n     move(si, tj, ti, tj); n   } else n     ans.push_back({{si + 1, sj + 1}, {ti + 1, tj + 1}}); n } n void clean(vector<vector<string> > &mat, int i, int j) { n   if (invert) reverse(mat[i][j].begin(), mat[i][j].end()); n   for (char c : mat[i][j]) { n     if (c ==  1 ) { n       move(i, j, 0, 1); n       mat[0][1].push_back(c); n     } n     if (c ==  0 ) { n       move(i, j, 0, 0); n       mat[0][0].push_back(c); n     } n   } n   mat[i][j].clear(); n } n void simulate(vector<vector<string> > mat) { n   printf(  npart 1: n ); n   print(mat); n   int cnt = 0; n   int n = ans.size(); n   assert(!ans.empty()); n   for (auto p : ans) { n     int si = p.first.first - 1; n     int sj = p.first.second - 1; n     int ti = p.second.first - 1; n     int tj = p.second.second - 1; n     assert(!mat[si][sj].empty()); n     char c = mat[si][sj].back(); n     mat[si][sj].pop_back(); n     string aux; n     aux.push_back(c); n     mat[ti][tj] = aux + mat[ti][tj]; n     cnt++; n     printf( step %d: n , cnt); n     print(mat); n     if (2 * cnt == n) printf(  npart 2 n ); n   } n } n void go(vector<vector<string> > &mat) { n   string z0, z1, o0, o1; n   if (invert) { n     reverse(mat[0][0].begin(), mat[0][0].end()); n     reverse(mat[0][1].begin(), mat[0][1].end()); n   } n   for (char c : mat[0][0]) { n     if (c ==  0 ) n       z0.push_back(c), move(0, 0, 1, 0); n     else n       o0.push_back(c), move(0, 0, 0, 1); n   } n   for (char c : mat[0][1]) { n     if (c ==  0 ) n       z1.push_back(c), move(0, 1, 0, 0); n     else n       o1.push_back(c), move(0, 1, 1, 1); n   } n   if (invert) { n     mat[1][0] = z0 + mat[1][0]; n     mat[1][1] = o1 + mat[1][1]; n   } else { n     mat[1][0] = mat[1][0] + z0; n     mat[1][1] = mat[1][1] + o1; n   } n   mat[0][0] = z1; n   mat[0][1] = o0; n   for (int i = 0; (i) < int(n); (i)++) { n     if (i == 0) continue; n     string aux; n     if (invert) reverse(mat[i][0].begin(), mat[i][0].end()); n     for (char c : mat[i][0]) { n       if (c ==  0 ) { n         move(i, 0, 0, 0); n         mat[0][0].push_back(c); n       } else { n         aux.push_back(c); n         move(i, 0, i, 1); n       } n     } n     if (invert) n       mat[i][1] = aux + mat[i][1]; n     else n       mat[i][1] = mat[i][1] + aux; n     mat[i][0].clear(); n   } n   for (int i = 0; (i) < int(n); (i)++) n     for (int j = 0; (j) < int(m); (j)++) { n       if (j == 0) continue; n       if (i == 0 && j == 1) continue; n       clean(mat, i, j); n     } n } n vector<vector<string> > from, to; n int main() { n   while (cin >> n >> m) { n     from.resize(n); n     for (int i = 0; (i) < int(n); (i)++) { n       from[i].resize(m); n       for (int j = 0; (j) < int(m); (j)++) cin >> from[i][j]; n     } n     to.resize(n); n     for (int i = 0; (i) < int(n); (i)++) { n       to[i].resize(m); n       for (int j = 0; (j) < int(m); (j)++) cin >> to[i][j]; n     } n     vector<vector<string> > aux = from; n     invert = true; n     ans.clear(); n     go(from); n     auto p1 = ans; n     invert = false; n     ans.clear(); n     go(to); n     auto p2 = ans; n     for (auto &pp : p2) swap(pp.first, pp.second); n     reverse(p2.begin(), p2.end()); n     ans = p1; n     ans.insert(ans.end(), p2.begin(), p2.end()); n     printf( %d n , int(ans.size())); n     for (auto pp : ans) { n       printf( %d %d %d %d n , pp.first.first, pp.first.second, pp.second.first, n              pp.second.second); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b); n int main() { n   long long t, w, b; n   cin >> t >> w >> b; n   long long tie = 0; n   long long total = t; n   bool overflow = false; n   long long gcdValue = gcd(w, b); n   if (LLONG_MAX / double(w) < double(b) / gcdValue) overflow = true; n   long long lcm = b / gcdValue * w; n   tie += min(w, b) * (t / lcm); n   if (t % lcm < min(w, b)) n     tie += t % lcm; n   else n     tie += min(w, b) - 1; n   if (overflow == true) { n     if (t < min(w, b)) { n       tie = 1; n       total = 1; n     } else n       tie = min(w, b) - 1; n   } n   long long gcdans = gcd(tie, total); n   tie /= gcdans; n   total /= gcdans; n   cout << tie <<  /  << total << endl; n   ; n   return 0; n } n long long gcd(long long a, long long b) { n   if (a % b == 0) return b; n   if (b % a == 0) return a; n   return gcd(max(a, b) % min(a, b), min(a, b)); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, x, rev; n   cin >> n; n   while (n % 10 == 0) n = n / 10; n   rev = 0; n   x = n; n   while (x > 0) { n     rev = rev * 10 + x % 10; n     x = x / 10; n   } n   if (rev == n) n     cout <<  YES ; n   else n     cout <<  NO ; n } n


#include <bits/stdc++.h> n using namespace std; n void solve(void); n int main(void) { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   int t = 1; n   while (t--) solve(); n   return 0; n } n long long int power(long long int x, long long int y) { n   long long int temp; n   if (y == 0) return 1; n   temp = power(x, y / 2); n   if (y % 2 == 0) n     return temp * temp; n   else { n     return x * temp * temp; n   } n } n long long int calculateK(long long int n, long long int i) { n   long long int pow = (long long int)i / n; n   return power(2, pow); n } n void solve(void) { n   long long int n, i; n   cin >> n; n   cin >> i; n   i *= 8; n   vector<int> record(n); n   vector<int> num; n   unordered_map<int, long long int> hash; n   int distinct = 0; n   for (int i = 0; i < n; i++) { n     int temp; n     cin >> temp; n     record[i] = temp; n     if (hash[temp] == 0) { n       hash[temp] = 1; n       distinct++; n       num.push_back(temp); n     } else { n       hash[temp] = hash[temp] + 1; n     } n   } n   long long int maxK = calculateK(n, i); n   sort(num.begin(), num.end()); n   long long int sum = 0; n   for (int i = 0; i < maxK && i < num.size(); i++) { n     sum += hash[num[i]]; n   } n   long long int maxSum = sum, maxL = 0, n                 maxR = maxK < num.size() ? maxK - 1 : num.size() - 1; n   for (int i = maxK, j = 1; i < num.size(); i++, j++) { n     sum -= hash[num[j - 1]]; n     sum += hash[num[i]]; n     if (sum > maxSum) { n       maxSum = sum; n       maxL = j; n       maxR = i; n     } n   } n   int valL = num[maxL]; n   int valR = num[maxR]; n   int ans = 0; n   for (int i = 0; i < n; i++) { n     if (record[i] < valL || record[i] > valR) { n       ans++; n     } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, a[100010], pos[100010], f[100010], b; n int main() { n   bool yes = true; n   cin >> n >> m; n   for (int i = 1; i < n + 1; i++) { n     cin >> f[i]; n     if (pos[f[i]]) { n       pos[f[i]] = -1; n     } else { n       pos[f[i]] = i; n     } n   } n   for (int i = 0; i < m; i++) { n     cin >> b; n     if (!pos[b]) { n       cout <<  Impossible  << endl; n       return 0; n     } else if (pos[b] == -1) { n       yes = false; n     } n     a[i] = pos[b]; n   } n   if (yes == false) { n     cout <<  Ambiguity  << endl; n     return 0; n   } else { n     cout <<  Possible  << endl; n     for (int i = 0; i < m; i++) { n       cout << a[i] <<    ; n     } n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(); n   cout.tie(); n   int s, x1, x2; n   cin >> s >> x1 >> x2; n   int t1, t2; n   cin >> t1 >> t2; n   int p, d; n   cin >> p >> d; n   int ans = abs(x1 - x2) * t2; n   if (x1 < x2) { n     if (p <= x1) { n       if (d == 1) { n         ans = min(ans, abs(x1 - p) * t1 + abs(x2 - x1) * t1); n       } else { n         ans = min(ans, abs(x2) * t1 + abs(p) * t1); n       } n     } else if (p >= x1 && p <= x2) { n       if (d == 1) { n         ans = min(ans, abs(s - p) * t1 + abs(x1 - s) * t1 + abs(x1) * t1 + n                            abs(x2) * t1); n       } else { n         ans = min(ans, abs(p - x1) * t1 + abs(x1) * t1 + abs(x2) * t1); n       } n     } else { n       if (d == 1) { n         ans = min(ans, abs(s - p) * t1 + abs(s - x1) * t1 + abs(x1) * t1 + n                            abs(x2) * t1); n       } else { n         ans = min(ans, abs(p - x1) * t1 + abs(x1) * t1 + abs(x2) * t1); n       } n     } n   } else { n     if (p <= x2) { n       if (d == 1) { n         ans = min(ans, abs(x1 - p) * t1 + abs(x1 - s) * t1 + abs(x2 - s) * t1); n       } else { n         ans = min(ans, abs(p) * t1 + abs(x1) * t1 + abs(s - x1) * t1 + n                            abs(x2 - s) * t1); n       } n     } else if (p >= x1) { n       if (d == 1) { n         ans = min(ans, abs(p - s) * t1 + abs(s - x1) * t1 + abs(x2 - x1) * t1); n       } else { n         ans = min(ans, abs(x1 - p) * t1 + abs(x2 - x1) * t1); n       } n     } else { n       if (d == 1) { n         ans = min(ans, abs(x1 - p) * t1 + abs(x1 - s) * t1 + abs(s - x2) * t1); n       } else { n         ans = min(ans, abs(p) * t1 + abs(x1) * t1 + abs(x1 - s) * t1 + n                            abs(s - x2) * t1); n       } n     } n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = (long long)1e9 + 7; n const long long N = (long long)1e5 * 4; n const long long INF = (long long)1e18; n const double eps = (double)1e-6; n string s; n vector<long long> v; n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> s; n   long long cnt = 0, col = 0; n   s +=  a ; n   bool ok = 0; n   for (long long i = 1; i < (long long)(s.size()); ++i) { n     if (s[i] ==  o ) { n       col++; n     } n     if (s[i] == s[i - 1] && s[i] ==  v ) { n       if (col != 0 && (long long)(v.size()) != 0) v.push_back(col); n       col = 0; n       cnt++; n     } else { n       if (cnt != 0) v.push_back(cnt); n       cnt = 0; n     } n   } n   long long sum = 0; n   for (long long i = 0; i < (long long)(v.size()); ++i) { n     if (i % 2 == 0) sum += v[i]; n   } n   long long ans = 0; n   cnt = 0; n   for (long long i = 0; i < (long long)(v.size()); ++i) { n     if (i % 2 == 0) { n       cnt += v[i]; n       sum -= v[i]; n     } else { n       ans += (cnt * v[i]) * sum; n     } n   } n   cout << ans; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int a, b, c, n, t, f, ans; n vector<int> v; n queue<int> q; n int main() { n   cin.tie(0); n   cout.tie(0); n   ios_base::sync_with_stdio(0); n   cin >> n >> a >> b >> c >> t; n   for (int i = 0; i < n; i++) { n     cin >> f; n     v.push_back(f); n   } n   sort(v.begin(), v.end()); n   for (auto u : v) q.push(u); n   v.clear(); n   int luku1 = 0; n   int luku2 = 0; n   int luku3 = 0; n   for (int i = 0; i <= t; i++) { n     while (q.front() == i) { n       v.push_back(q.front()); n       q.pop(); n       luku1 += a; n     } n     luku2 += v.size() * c; n   } n   luku2 -= v.size() * c; n   for (auto u : v) { n     luku2 += a + (u - t) * b; n   } n   ans = max(luku1, max(luku2, luku3)); n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T, typename S> n ostream &operator<<(ostream &os, const pair<T, S> &p) { n   return os <<  (  << p.first <<  ,   << p.second <<  ) ; n } n template <typename T, size_t N> n ostream &operator<<(ostream &os, const array<T, N> &a) { n   os <<  ( ; n   for (int i = 0; i < (int)N; i++) { n     if (i) os <<  ,  ; n     os << a[i]; n   } n   return os <<  ) ; n } n template <typename T> n ostream &operator<<(ostream &os, const vector<T> &v) { n   os <<  [ ; n   for (int i = 0; i < (int)v.size(); i++) { n     if (i) os <<  ,  ; n     os << v[i]; n   } n   return os <<  ] ; n } n void debug() { cerr <<   n ; } n template <typename T, typename... Args> n void debug(T x, Args... args) { n   cerr << x <<  ;  ; n   debug(args...); n } n int a[360]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) cin >> a[i]; n   int ret = INT_MAX; n   for (int i = 0; i < n; i++) { n     int sum = a[i]; n     ret = min(ret, abs(2 * a[i] - 360)); n     for (int j = (i + 1) % n; j != i; j = (j + 1) % n) { n       sum += a[j]; n       ret = min(ret, abs(2 * sum - 360)); n     } n   } n   cout << ret <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n void rd(int &x) { n   x = 0; n   int f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch <=  9  && ch >=  0 ) x = x * 10 + ch -  0 , ch = getchar(); n   x *= f; n } n void lrd(long long &x) { n   x = 0; n   int f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch <=  9  && ch >=  0 ) x = x * 10 + ch -  0 , ch = getchar(); n   x *= f; n } n const int INF = 1e9; n const long long LINF = 1e18; n const int N = 5e5 + 10; n const int mod = 1e9 + 7; n using namespace std; n int T; n int n, m; n map<int, long long> S; n long long c[N]; n vector<int> f[N]; n long long gcd(long long x, long long y) { return !y ? x : gcd(y, x % y); } n int main() { n   rd(T); n   while (T--) { n     rd(n); n     rd(m); n     S.clear(); n     for (int i = 1; i <= n; i++) lrd(c[i]), f[i].clear(); n     for (int i = 1; i <= m; i++) { n       int x, y; n       rd(x); n       rd(y); n       f[y].push_back(x); n     } n     for (int i = 1; i <= n; i++) sort(f[i].begin(), f[i].end()); n     for (int i = 1; i <= n; i++) { n       if (!f[i].size()) continue; n       int hs = 0; n       for (int j = 0; j < f[i].size(); j++) n         hs = (1ll * hs * (n + 1) + f[i][j]) % mod; n       if (S.find(hs) == S.end()) n         S[hs] = c[i]; n       else n         S[hs] += c[i]; n     } n     map<int, long long>::iterator it; n     long long ans = 0; n     for (it = S.begin(); it != S.end(); it++) n       if (ans) n         ans = gcd(ans, it->second); n       else n         ans = it->second; n     printf( %lld n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1001; n const int M = 300000; n int n, m, a[N], b[N]; n vector<bool> g[M]; n pair<int, int> p[N]; n priority_queue<pair<int, int> > q; n bool solve(int x) { n   if (x == 0) return true; n   for (int i = 1; i <= n; i++) b[i] = a[i] - x; n   int sum = 0; n   for (int i = 1; i <= n; i++) sum += b[i]; n   sort(b + 1, b + n + 1); n   reverse(b + 1, b + n + 1); n   if (sum & 1) { n     b[1]--; n     b[2]--; n     b[3]--; n   } n   sort(b + 1, b + n + 1); n   reverse(b + 1, b + n + 1); n   if (b[n] < 0) return false; n   for (int i = 1; i <= n; i++) n     if (b[i] > 0) q.push(make_pair(b[i], i)); n   while (!q.empty()) { n     pair<int, int> v = q.top(); n     q.pop(); n     if (q.empty()) return false; n     pair<int, int> u = q.top(); n     q.pop(); n     v.first--; n     u.first--; n     if (v.first > 0) q.push(v); n     if (u.first > 0) q.push(u); n   } n   return true; n } n void getSolution(int x) { n   if (x == 0) { n     m = 0; n     for (int i = 1; i < n; i++) { n       for (int j = 1; j <= 100; j++) { n         m++; n         for (int k = 1; k <= n; k++) n           if (k == i || k == i + 1) n             g[m][k - 1] = true; n           else n             g[m][k - 1] = false; n       } n     } n     return; n   } n   for (int i = 1; i <= n; i++) b[i] = a[i] - x; n   for (int i = 1; i <= n; i++) n     if (b[i] > 0) q.push(make_pair(b[i], i)); n   int sum = 0; n   for (int i = 1; i <= n; i++) sum += b[i]; n   int game = 0; n   while (!q.empty()) { n     game++; n     if (sum & 1) { n       pair<int, int> a = q.top(); n       q.pop(); n       if (q.empty()) assert(false); n       pair<int, int> b = q.top(); n       q.pop(); n       if (q.empty()) assert(false); n       pair<int, int> c = q.top(); n       q.pop(); n       for (int j = 1; j <= n; j++) n         if (j == a.second || j == b.second || j == c.second) n           g[game][j - 1] = true; n         else n           g[game][j - 1] = false; n       a.first--; n       b.first--; n       c.first--; n       if (a.first > 0) q.push(a); n       if (b.first > 0) q.push(b); n       if (c.first > 0) q.push(c); n       sum -= 3; n     } else { n       pair<int, int> a = q.top(); n       q.pop(); n       if (q.empty()) assert(false); n       pair<int, int> b = q.top(); n       q.pop(); n       for (int j = 1; j <= n; j++) n         if (j == a.second || j == b.second) n           g[game][j - 1] = true; n         else n           g[game][j - 1] = false; n       a.first--; n       b.first--; n       if (a.first > 0) q.push(a); n       if (b.first > 0) q.push(b); n     } n   } n   m = game; n } n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   int minval = a[1]; n   for (int i = 2; i <= n; i++) minval = min(minval, a[i]); n   for (int i = 1; i <= 10000; i++) g[i].resize(n); n   int ans = 0; n   for (int i = minval; i >= 0; i--) n     if (solve(i)) { n       ans = i; n       break; n     } n   cout << ans << endl; n   getSolution(ans); n   cout << m << endl; n   for (int i = 1; i <= m; i++) { n     for (int j = 0; j < g[i].size(); j++) cout << g[i][j]; n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n const int maxn = 5e3 + 10; n int dp[maxn][maxn]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n, m; n   cin >> n >> m; n   string a, b; n   cin >> a >> b; n   int ans = 0; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       if (a[i - 1] == b[j - 1]) { n         dp[i][j] = dp[i - 1][j - 1] + 2; n       } else n         dp[i][j] = max({0, dp[i - 1][j] - 1, dp[i][j - 1] - 1}); n       ans = max(ans, dp[i][j]); n     } n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1005; n char a[N], b[N]; n int lcp[N][N]; n int f[N][N][12]; n int main() { n   int l1, l2, n; n   scanf( %d%d%d , &l1, &l2, &n); n   scanf( %s%s , a + 1, b + 1); n   for (int i = (1); i <= (l1); i++) { n     for (int j = (1); j <= (l2); j++) { n       if (a[i] == b[j]) n         lcp[i][j] = lcp[i - 1][j - 1] + 1; n       else n         lcp[i][j] = 0; n     } n   } n   int ans = n; n   for (int i = (1); i <= (l1); i++) { n     for (int j = (1); j <= (l2); j++) { n       for (int k = (1); k <= (n); k++) { n         int A = lcp[i][j]; n         f[i][j][k] = max(f[i][j][k], f[i - A][j - A][k - 1] + A); n         if (f[i][j][k] > f[i + 1][j][k]) f[i + 1][j][k] = f[i][j][k]; n         if (f[i][j][k] > f[i][j + 1][k]) f[i][j + 1][k] = f[i][j][k]; n         ans = max(ans, f[i][j][k]); n       } n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1000 * 100 + 10; n vector<int> vec[MAXN]; n int c[MAXN]; n long long dp[2][MAXN]; n void dfs(int v) { n   vector<int> temp; n   long long all = 1; n   for (int i = 0; i < (int)vec[v].size(); i++) { n     int u = vec[v][i]; n     dfs(u); n     temp.push_back(all); n     all = (all * (dp[0][u] + dp[1][u])) % (1000 * 1000 * 1000 + 7); n   } n   if (c[v]) { n     dp[1][v] = all; n     dp[0][v] = 0; n   } else { n     dp[0][v] = all; n     all = 1; n     for (int i = (int)vec[v].size() - 1; i >= 0; i--) { n       int u = vec[v][i]; n       dp[1][v] = (dp[1][v] + n                   (((temp[i] * all) % (1000 * 1000 * 1000 + 7)) * dp[1][u]) % n                       (1000 * 1000 * 1000 + 7)) % n                  (1000 * 1000 * 1000 + 7); n       all = (all * (dp[0][u] + dp[1][u])) % (1000 * 1000 * 1000 + 7); n     } n   } n } n int main() { n   int n; n   cin >> n; n   for (int i = 1; i < n; i++) { n     int a; n     cin >> a; n     vec[a].push_back(i); n   } n   for (int i = 0; i < n; i++) cin >> c[i]; n   dfs(0); n   cout << dp[1][0] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n namespace Base { n const int inf = 0x3f3f3f3f, INF = 0x7fffffff; n const long long infll = 0x3f3f3f3f3f3f3f3fll, INFll = 0x7fffffffffffffffll; n template <typename T> n void read(T &x) { n   x = 0; n   int fh = 1; n   double num = 1.0; n   char ch = getchar(); n   while (!isdigit(ch)) { n     if (ch ==  - ) fh = -1; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   if (ch ==  . ) { n     ch = getchar(); n     while (isdigit(ch)) { n       num /= 10; n       x = x + num * (ch -  0 ); n       ch = getchar(); n     } n   } n   x = x * fh; n } n template <typename T> n void chmax(T &x, T y) { n   x = x < y ? y : x; n } n template <typename T> n void chmin(T &x, T y) { n   x = x > y ? y : x; n } n }  // namespace Base n using namespace Base; n const int N = 100010; n int n, a[N], q[N], use[N], pre[N], prel[N], prem[N], prer[N], num, l[N], m[N], n     r[N]; n void force(int *a, int n, int id) { n   int tmp = 0; n   for (int i = 1; i <= n; i++) tmp = tmp + (a[i] << (i - 1)); n   int pl = 1, pr = 1; n   q[1] = 0; n   use[0] = true; n   while (pl <= pr) { n     int x = q[pl++]; n     for (int i = 1; i <= n; i++) n       for (int j = i + 1; j <= n; j++) { n         int k = j + (j - i); n         if (k > n) continue; n         int nxt = x ^ (1 << (i - 1)) ^ (1 << (j - 1)) ^ (1 << (k - 1)); n         if (use[nxt] == false) { n           q[++pr] = nxt; n           use[nxt] = true; n           pre[nxt] = x; n           prel[nxt] = i; n           prem[nxt] = j, prer[nxt] = k; n         } n       } n   } n   if (use[tmp] == false) { n     printf( NO n ); n     exit(0); n   } n   printf( YES n ); n   while (tmp != 0) { n     num++; n     l[num] = prel[tmp] + id, m[num] = prem[tmp] + id, r[num] = prer[tmp] + id; n     tmp = pre[tmp]; n   } n   printf( %d n , num); n   for (int i = 1; i <= num; i++) printf( %d %d %d n , l[i], m[i], r[i]); n } n void doit(int nl, int nm, int nr) { n   num++; n   l[num] = nl, m[num] = nm, r[num] = nr; n   a[nl] ^= 1, a[nm] ^= 1, a[nr] ^= 1; n } n int main() { n   read(n); n   for (int i = 1; i <= n; i++) read(a[i]); n   int pl = 1, pr = n; n   while (pr - pl + 1 > 12) { n     if (a[pl] == 0) { n       pl++; n       continue; n     } n     if (a[pr] == 0) { n       pr--; n       continue; n     } n     if (a[pl] + a[pl + 1] + a[pl + 2] == 3) { n       doit(pl, pl + 1, pl + 2); n       continue; n     } n     if (a[pr] + a[pr - 1] + a[pr - 2] == 3) { n       doit(pr - 2, pr - 1, pr); n       continue; n     } n     if ((pr - pl) % 2 == 0) { n       doit(pl, (pl + pr) / 2, pr); n       continue; n     } n     if (a[pl] != a[pl + 1] && a[pl] == a[pl + 2]) { n       doit(pl, pl + 2, pl + 4); n       continue; n     } n     if (a[pr] != a[pr - 1] && a[pr] == a[pr - 2]) { n       doit(pr - 4, pr - 2, pr); n       continue; n     } n     doit(pl, (pl + pr - 1) / 2, pr - 1); n   } n   if (n <= 12) n     force(a, n, 0); n   else n     force(a + pl - 1, 12, pl - 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1000000; n int sum[maxn]; n int a[maxn]; n int sum2[maxn]; n int main() { n   int n; n   char s[maxn]; n   while (cin >> n) { n     memset(sum, 0, sizeof(sum)); n     for (int i = 1; i <= n; ++i) { n       scanf( %d , a + i); n       sum[i] = sum[i - 1] + a[i]; n     } n     scanf( %s , s + 1); n     int c = 0, ans = 0; n     for (int i = n; i >= 1; --i) { n       if (s[i] ==  1 ) { n         ans = max(ans, c + sum[i - 1]); n         c += a[i]; n       } n     } n     cout << max(c, ans) << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n pair<int, int> arr[5005]; n vector<int> primes; n int ans[5005]; n int u[5005]; n int n; n void pre() { n   vector<char> bad(10005); n   for (int i = 2; i <= 10000; ++i) { n     if (!bad[i]) { n       primes.push_back(i); n       for (int j = i * i; j <= 10000; j += i) { n         bad[j] = 1; n       } n     } n   } n } n int f(int x) { n   static vector<int> di; n   di.clear(); n   if (x == 0) { n     return 0; n   } n   bool neg = 0; n   if (x < 0) { n     neg = 1; n     x *= -1; n   } n   int y = 1; n   for (int i = 0; i < primes.size(); ++i) { n     int cnt = 0; n     while (x % primes[i] == 0) { n       x /= primes[i]; n       ++cnt; n     } n     if (cnt & 1) { n       y *= primes[i]; n     } n   } n   x *= y; n   if (neg) { n     x *= -1; n   } n   return x; n } n void compress() { n   vector<int> vv; n   for (int i = 1; i <= n; ++i) { n     vv.push_back(arr[i].first); n   } n   sort((vv).begin(), (vv).end()); n   vv.resize(unique((vv).begin(), (vv).end()) - vv.begin()); n   for (int i = 1; i <= n; ++i) { n     arr[i].second = n         upper_bound((vv).begin(), (vv).end(), arr[i].first) - vv.begin(); n   } n } n void solve() { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) { n     scanf( %d , &arr[i].first); n     arr[i].first = f(arr[i].first); n   } n   compress(); n   int ptr = 0; n   for (int l = 1; l <= n; ++l) { n     int cur = 0; n     ++ptr; n     for (int r = l; r <= n; ++r) { n       if (arr[r].first != 0) { n         if (u[arr[r].second] != ptr) { n           u[arr[r].second] = ptr; n           ++cur; n         } n       } n       ++ans[max(1, cur)]; n     } n   } n   for (int i = 1; i <= n; ++i) { n     printf( %d  , ans[i]); n   } n } n int main() { n   int tt = 1; n   pre(); n   while (tt--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, tri = 0; n   scanf( %d , &n); n   long long trilist[n + 5]; n   for (int i = 0; i < n; i++) { n     scanf( %ld , &trilist[i]); n   } n   sort(trilist, trilist + n); n   for (int i = 0; i < n - 2; i++) { n     if (trilist[i] + trilist[i + 1] > trilist[i + 2]) { n       tri = 1; n       break; n     } n   } n   if (tri == 1) { n     printf( YES n ); n   } else { n     printf( NO n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int visited1[200005], visited2[200005], step1[200005], step2[200005], n     ar[200005]; n long long int n; n void fill_array(long long int ar[], long long int value, long long int size) { n   for (long long int i = 0; i < size; i++) ar[i] = value; n } n long long int dfs(long long int node, long long int step, n                   long long int parent) { n   if (step == 1) { n     visited1[node] = 1; n     if (node + ar[node] > n) n       step1[node] = ar[node]; n     else { n       if (visited2[node + ar[node]]) { n         if (step2[node + ar[node]] < 0) n           step1[node] = -1; n         else if (step2[node + ar[node]] == 0) n           step1[node] = -1; n         else n           step1[node] = step2[node + ar[node]] + ar[node]; n       } else { n         long long int temp = dfs(node + ar[node], 2, node); n         if (temp < 0) n           step1[node] = -1; n         else n           step1[node] = temp + ar[node]; n       } n     } n     return step1[node]; n   } else { n     visited2[node] = 1; n     if (node - ar[node] < 1) n       step2[node] = ar[node]; n     else if (node - ar[node] == 1) n       step2[node] = -1; n     else { n       if (visited1[node - ar[node]]) { n         if (step1[node - ar[node]] < 0) n           step2[node] = -1; n         else if (step1[node - ar[node]] == 0) n           step2[node] = -1; n         else n           step2[node] = step1[node - ar[node]] + ar[node]; n       } else { n         long long int temp = dfs(node - ar[node], 1, node); n         if (temp < 0) n           step2[node] = -1; n         else n           step2[node] = temp + ar[node]; n       } n     } n     return step2[node]; n   } n } n int main() { n   cin >> n; n   for (long long int i = 2; i <= n; i++) cin >> ar[i]; n   fill_array(visited1, 0, n + 1); n   fill_array(visited2, 0, n + 1); n   fill_array(step1, 0, n + 1); n   fill_array(step2, 0, n + 1); n   for (long long int i = 2; i <= n; i++) { n     if (!visited1[i]) dfs(i, 1, -1); n     if (!visited2[i]) dfs(i, 2, -1); n   } n   for (long long int i = 2; i <= n; i++) { n     if (step2[i] == -1) n       cout <<  -1 n ; n     else n       cout << step2[i] + i - 1 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n struct pos { n   long long int x; n   long long int h; n   int indx; n   int sum; n } p[100005]; n bool cmp(pos a, pos b) { return a.x < b.x; } n bool cmp2(pos a, pos b) { return a.indx < b.indx; } n int bs(long long int key) { n   int l = 0; n   int r = n; n   int mid; n   while (l <= r) { n     mid = (l + r) / 2; n     if (p[mid].x == key) { n       return mid; n     } else if (p[mid].x < key) { n       l = mid + 1; n     } else { n       r = mid - 1; n     } n   } n } n int main() { n   scanf( %d , &n); n   long long int ar[n + 5]; n   for (int i = 0; i < n; i++) { n     scanf( %lld%lld , &p[i].x, &p[i].h); n     p[i].indx = i; n     p[i].sum = 1; n   } n   sort(p, p + n, cmp); n   p[n - 1].sum = 1; n   for (int i = n - 2; i >= 0; i--) { n     long long int val = p[i].h + p[i].x; n     int poscnt = 1; n     while (val > p[i + poscnt].x && poscnt + i < n) { n       p[i].sum += p[poscnt + i].sum; n       poscnt += p[poscnt + i].sum; n     } n   } n   sort(p, p + n, cmp2); n   for (int i = 0; i < n; i++) { n     printf( %d  , p[i].sum); n   } n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, p, prt[100005], num[100005], ans = 1, cnt, u, v; n inline int getfather(int u) { n   if (u == prt[u]) return prt[u]; n   prt[u] = getfather(prt[u]); n   return prt[u]; n } n int main() { n   scanf( %lld%lld%lld , &n, &m, &p); n   for (register int i = 1; i <= n; i++) prt[i] = i; n   for (register int i = 1; i <= m; i++) { n     scanf( %lld%lld , &u, &v); n     register int f1 = getfather(u), f2 = getfather(v); n     if (f1 != f2) prt[f1] = f2; n   } n   for (register int i = 1; i <= n; i++) num[getfather(i)]++; n   for (register int i = 1; i <= n; i++) n     if (num[i]) cnt++, ans = ans * num[i] % p; n   for (register int i = 1; i <= cnt - 2; i++) ans = ans * n % p; n   if (cnt == 1) ans = 1; n   printf( %lld , ans % p); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 52; n const int INF = 1e9; n int mat[N][N]; n int n, k; n struct edge { n   int from, to, cap, cost, flow; n   edge(int from, int to, int cap, int cost) n       : from(from), to(to), cap(cap), cost(cost) { n     flow = 0; n   } n }; n vector<edge> e; n vector<int> g[N]; n void add_edge(int from, int to, int cap, int cost) { n   g[from].push_back(e.size()); n   e.push_back(edge(from, to, cap, cost)); n   g[to].push_back(e.size()); n   e.push_back(edge(to, from, 0, -cost)); n } n int d[N]; n int ptr[N]; n bool bfs() { n   d[1] = 1; n   queue<int> q; n   q.push(1); n   while (!q.empty()) { n     int v = q.front(); n     q.pop(); n     for (int id : g[v]) { n       int to = e[id].to; n       if (e[id].flow == e[id].cap) continue; n       if (d[to] == -1) { n         d[to] = d[v] + 1; n         q.push(to); n       } n     } n   } n   return d[n] != -1; n } n int dfs(int v, int flow) { n   if (!flow || v == n) return flow; n   for (; ptr[v] < (int)g[v].size(); ++ptr[v]) { n     int id = g[v][ptr[v]]; n     int to = e[id].to; n     if (d[to] == d[v] + 1) { n       int pushed = dfs(to, min(flow, e[id].cap - e[id].flow)); n       if (pushed) { n         e[id].flow += pushed; n         e[id ^ 1].flow -= pushed; n         return pushed; n       } n     } n   } n   return 0; n } n int dinic() { n   int flow = 0; n   while (1) { n     fill(d, d + N, -1); n     fill(ptr, ptr + N, 0); n     if (!bfs()) return flow; n     while (int pushed = dfs(1, INF)) { n       flow += pushed; n     } n   } n   return flow; n } n char in_que[N]; n int from[N]; n void show(edge e) { n   cout <<  (  << e.from <<  ,   << e.to <<  ,   << e.flow <<  ,   << e.cap n        <<  ,   << e.cost <<  )  << endl; n } n vector<int> spfa() { n   fill(d, d + N, INF); n   fill(in_que, in_que + N, 0); n   d[1] = 0; n   queue<int> q; n   q.push(1); n   in_que[1] = 1; n   while (!q.empty()) { n     int v = q.front(); n     q.pop(); n     in_que[v] = 0; n     for (int id : g[v]) { n       int to = e[id].to; n       if (e[id].cap == e[id].flow) continue; n       if (d[to] > d[v] + e[id].cost) { n         d[to] = d[v] + e[id].cost; n         from[to] = id; n         if (!in_que[to]) { n           in_que[to] = 1; n           q.push(to); n         } n       } n     } n   } n   if (d[n] == INF) return {}; n   int v = n; n   vector<int> ret; n   while (v != 1) { n     ret.push_back(from[v]); n     v = e[from[v]].from; n   } n   reverse(ret.begin(), ret.end()); n   return ret; n } n int mcf(int flow) { n   int cost = 0; n   while (1) { n     vector<int> kek = spfa(); n     if (kek.empty()) return flow; n     int delta = 1000000; n     int sum = 0; n     for (int id : kek) { n       delta = min(delta, e[id].cap - e[id].flow); n       sum += e[id].cost; n     } n     if (sum > 0) delta = min(delta, (k - cost) / sum); n     for (int id : kek) { n       e[id].flow += delta; n       e[id ^ 1].flow -= delta; n       cost += e[id].cost * delta; n     } n     flow += delta; n     if (!delta) return flow; n   } n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> k; n   for (int i = 1; i <= n; ++i) n     for (int j = 1; j <= n; ++j) { n       cin >> mat[i][j]; n       if (!mat[i][j]) continue; n       add_edge(i, j, mat[i][j], 0); n     } n   int din = 0; n   for (int i = 1; i <= n; ++i) n     for (int j = 1; j <= n; ++j) { n       if (!mat[i][j]) continue; n       add_edge(i, j, k, 1); n     } n   int ans = mcf(din); n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n char ss[100002]; n int main() { n   int N, K, k = 0; n   scanf( %d %d , &N, &K); n   scanf( %s , ss); n   string S = ss; n   for (int i = 0; i < N - 1 && k < K; i++) { n     if (S[i] ==  4  && S[i + 1] ==  7 ) { n       int x = i + 1; n       if (x % 2) { n         if (x + 1 < N && S[x + 1] ==  7 ) { n           if ((K - k) % 2) { n             S[x] =  4 ; n           } n           printf( %s , S.c_str()); n           return 0; n         } n         S[x] =  4 ; n       } else { n         S[x - 1] =  7 ; n         i -= 2; n       } n       k++; n     } n   } n   printf( %s n , S.c_str()); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int ns, k, n, m; n char s[1000010], t[1000010], p[1000010]; n vector<int> vet; n int fail[1000010]; n void buildFail(char *p) { n   int j = fail[0] = -1; n   for (int i = 1; i <= m; i++) { n     while (j >= 0 && p[j] != p[i - 1]) j = fail[j]; n     fail[i] = ++j; n   } n } n int kmp(char *p, char *t) { n   buildFail(p); n   for (int i = 0, k = 0; i < n; i++) { n     while (k >= 0 && p[k] != t[i]) k = fail[k]; n     if (++k >= m) { n       return 1; n       k = fail[k]; n     } n   } n   return 0; n } n int test(int k) { n   int i, j; n   m = 0; n   for (i = 0, j = ns - k; i < k; i++, j++) { n     if (s[i] != s[j]) break; n     p[m++] = s[i]; n   } n   if (i < k) return 0; n   p[m] =   0 ; n   return kmp(p, t); n } n int main() { n   int lo, hi, mid; n   scanf(  %s , s); n   ns = strlen(s); n   vet.clear(); n   m = ns; n   buildFail(s); n   k = ns; n   while (fail[k] > 0) { n     vet.push_back(fail[k]); n     k = fail[k]; n   } n   sort(vet.begin(), vet.end()); n   for (int i = 1; i < ns - 1; i++) t[i - 1] = s[i]; n   t[n - 2] =   0 ; n   n = ns - 2; n   lo = 0; n   hi = vet.size() - 1; n   k = 0; n   while (lo <= hi) { n     mid = (lo + hi) / 2; n     if (test(vet[mid])) { n       k = vet[mid]; n       lo = mid + 1; n     } else n       hi = mid - 1; n   } n   if (!k) n     printf( Just a legend n ); n   else { n     for (int i = 0; i < k; i++) printf( %c , s[i]); n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MX = 400010; n struct ST { n   int st[4 * MX], p[MX]; n   ST() { build(1, MX - 1, 1); } n   void build(int i, int j, int pos) { n     st[pos] = 0; n     if (i == j) { n       p[i] = pos; n       return; n     } n     int m = (i + j) / 2; n     build(i, m, pos * 2); n     build(m + 1, j, pos * 2 + 1); n   } n   void update(int x, int k) { n     int pos = p[x]; n     st[pos] = k; n     pos /= 2; n     while (pos) { n       st[pos] = max(st[pos * 2], st[pos * 2 + 1]); n       pos /= 2; n     } n   } n   int leq(int i, int j, int pos, int x) { n     if (i > x) return 0; n     if (j <= x) return st[pos]; n     int m = (i + j) / 2; n     return max(leq(i, m, pos * 2, x), leq(m + 1, j, pos * 2 + 1, x)); n   } n   int geq(int i, int j, int pos, int x) { n     if (i >= x) return st[pos]; n     if (j < x) return 0; n     int m = (i + j) / 2; n     return max(geq(i, m, pos * 2, x), geq(m + 1, j, pos * 2 + 1, x)); n   } n } st; n int n, a[MX], res[MX]; n vector<int> c; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     a[i] -= i; n     c.push_back(a[i]); n     c.push_back(a[i] + 1); n   } n   sort(c.begin(), c.end()); n   c.erase(unique(c.begin(), c.end()), c.end()); n   for (int i = 0; i < n; i++) n     a[i] = lower_bound(c.begin(), c.end(), a[i]) - c.begin() + 1; n   for (int i = 1; i < n; i++) { n     st.update(a[i - 1], 1 + st.leq(1, MX - 1, 1, a[i - 1])); n     res[i] = st.leq(1, MX - 1, 1, a[i - 1]); n   } n   st.build(1, MX - 1, 1); n   for (int i = n - 2; i >= 0; i--) { n     st.update(a[i + 1] + 1, 1 + st.geq(1, MX - 1, 1, a[i + 1] + 1)); n     if (i - 1 > 0) res[i] += st.geq(1, MX - 1, 1, a[i - 1]); n   } n   cout << n - 1 - max(*max_element(res, res + n), st.geq(1, MX - 1, 1, 1)) n        <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 1000000007; n int n, m, t; n long long dp[61][31][31]; n int arr[61]; n long long solve(int ind, int x, int y) { n   if (x > 3 && y > 0 && x + y == t) return 1; n   if (ind == n + m + 1) return 0; n   long long &ret = dp[ind][x][y]; n   if (ret != -1) return ret; n   ret = solve(ind + 1, x, y); n   if (x < n && arr[ind] == 1) ret += solve(ind + 1, x + 1, y); n   if (y < m && arr[ind] == 2) ret += solve(ind + 1, x, y + 1); n   return ret; n } n int main() { n   scanf( %d%d%d , &n, &m, &t); n   int i = 0; n   for (; i < n; i++) arr[i] = 1; n   for (; i < n + m; i++) arr[i] = 2; n   memset(dp, -1, sizeof dp); n   printf( %lld n , solve(0, 0, 0)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int mod = 1e9 + 7; n int f[105][105][105], len[105], n, m; n char s[105]; n int qpow(int x, int y) { n   int res = 1; n   while (y) { n     if (y % 2) res = 1ll * res * x % mod; n     x = 1ll * x * x % mod; n     y /= 2; n   } n   return res; n } n int dfs(int x, int l, int r) { n   if (f[x][l][r] != -1) return f[x][l][r]; n   if (x <= 1) return (l == r) && (s[l] == (x +  0 )); n   int cnt = 0; n   cnt = (cnt + n          1ll * dfs(x - 1, l, r) * ((r == n) ? qpow(2, len[x - 2]) : 1) % mod) % n         mod; n   cnt = (cnt + n          1ll * dfs(x - 2, l, r) * ((l == 1) ? qpow(2, len[x - 1]) : 1) % mod) % n         mod; n   for (int k = l; k < r; k++) n     cnt = (cnt + 1ll * dfs(x - 1, l, k) * dfs(x - 2, k + 1, r) % mod) % mod; n   return f[x][l][r] = cnt; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin >> n >> m; n   memset(f, -1, sizeof(f)); n   len[1] = 1; n   len[0] = 1; n   for (int i = 2; i <= m; i++) len[i] = (len[i - 1] + len[i - 2]) % (mod - 1); n   cin >> s + 1; n   printf( %d n , dfs(m, 1, n)); n } n


#include <bits/stdc++.h> n using namespace std; n typedef long long LL; n #define FOR(i, a, b) for(int i = (a); i < (b); i++) n  n const int N = 1 << 10; n int p[N][N]; n int main() n { n  int m, n; n  cin >> m >> n; n  FOR(i, 0, n) n  FOR(j, 0, m) n   cin >> p[i][j]; n  int sum = 0; n  FOR(j, 0, m) n   sum += p[n - 1][j] - p[0][j]; n  assert(sum%(n - 1) == 0); n  sum /= (n - 1); n  int sum0 = 0; n  FOR(j, 0, m) n   sum0 += p[0][j]; n  int row = 0; n  FOR(i, 1, n - 1){ n   int S = 0; n   FOR(j, 0, m) n    S += p[i][j]; n   if(S == sum0 + i * sum) continue; n   row = i; n  } n  int st = (row > 2 ? 0 : (row + 1)); n  LL sum2 = 0; n  FOR(j, 0, m){ n   sum2 += p[st][j] * (LL) p[st][j]; n   sum2 += p[st + 2][j] * (LL) p[st + 2][j]; n   sum2 -= 2 * p[st + 1][j] * (LL) p[st + 1][j]; n  } n  sum2 /= 2; n  LL real = 0, real2 = 0, have = 0, have2 = 0; n  FOR(j, 0, m){ n   have += p[row][j]; n   have2 += p[row][j] * (LL) p[row][j]; n   real += p[row - 1][j]; n   real2 += p[row - 1][j] * (LL) p[row - 1][j]; n   real2 += p[row + 1][j] * (LL) p[row + 1][j]; n  } n  real += sum; n  real2 = real2/2 - sum2; n  LL a2_x2 = have2 - real2; n  LL a_x = have - real; n  LL a_plus_x = a2_x2 / a_x; n  cout << row <<     << (a_plus_x - a_x) / 2 << endl; n  return 0; n } n  n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f2f1f0f; n const long long LINF = 1ll * INF * INF; n const int MAX_N = 2e5 + 100; n int N; n long long C; n int Nr[MAX_N * 2]; n long long SumNr[MAX_N * 2]; n long long Ans[MAX_N]; n long long SumAns[MAX_N]; n long long getSum(long long val[], int a, int b) { return val[b] - val[a - 1]; } n long long getVal(int a, int b) { n   return getSum(SumNr, a * 2 - 1, b * 2 - 1) - getSum(SumAns, a, b - 1); n } n int main() { n   cin >> N >> C; n   for (int i = 1; i <= N * 2 - 1; i++) n     scanf( %d , &Nr[i]), SumNr[i] = SumNr[i - 1] + Nr[i]; n   deque<int> dQ; n   int cl = 0; n   for (int r = 1; r <= N; r++) { n     while (!dQ.empty() && getVal(dQ.back(), r) < Nr[r * 2 - 1]) dQ.pop_back(); n     dQ.push_back(r); n     while (!dQ.empty() && getSum(SumNr, dQ.front() * 2 - 1, r * 2 - 1) >= C) n       dQ.pop_front(); n     while (cl + 1 <= N && getSum(SumNr, (cl + 1) * 2 - 1, r * 2 - 1) >= C) cl++; n     if (cl != 0) Ans[r] = max(Ans[r], C - getSum(SumAns, cl, r - 1)); n     if (!dQ.empty()) Ans[r] = max(Ans[r], getVal(dQ.front(), r)); n     SumAns[r] = SumAns[r - 1] + Ans[r]; n   } n   printf( %lld n , SumAns[N]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long f[3], x, y, z, k; n int main() { n   long long a, b, c; n   long long r; n   cin >> f[0] >> f[1] >> f[2] >> k; n   sort(f, f + 3); n   k += 3; n   k = min(k, f[0] + f[1] + f[2]); n   a = f[0] * 3 <= k ? f[0] : k / 3; n   r = k - a; n   b = f[1] * 2 <= r ? f[1] : r >> 1; n   c = k - a - b; n   cout << a * b * c << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = (int)1e6 + 9; n int n, d[N]; n int digit(int x) { n   int s = 0; n   while (x > 0) { n     s += x % 10; n     x /= 10; n   } n   return s; n } n vector<int> b[10]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cin >> n; n   for (int i = 1; i < 10; ++i) d[i] = i; n   for (int i = 10; i < N; ++i) d[i] = d[digit(i)]; n   for (int i = 1; i <= n; ++i) b[d[i]].emplace_back(i); n   long long ans = 0; n   for (int i = 1, cnt; i <= n; ++i) { n     for (int j = 1; j < 10; ++j) n       if (!b[j].empty()) { n         cnt = upper_bound(b[j].begin(), b[j].end(), n / i) - b[j].begin(); n         ans += (long long)b[j].size() * b[d[d[i] * j]].size() - cnt; n       } n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 300001; n const int logn = 20; n bool s[maxn][logn]; n int nxt[maxn][logn]; n int f[maxn][logn]; n int a[maxn]; n int tmp[maxn]; n int n, q; n int main() { n   scanf( %d%d , &n, &q); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n   } n   for (int j = 0; j < logn; j++) { n     int cnt = 0; n     for (int i = 1; i <= n; i++) { n       if (!(a[i] >> j & 1)) continue; n       s[i][j] = 1; n       tmp[++cnt] = i; n     } n     for (int i = 1; i < cnt; i++) { n       nxt[tmp[i]][j] = tmp[i + 1]; n     } n   } n   for (int i = n; i >= 1; i--) { n     for (int j = 0; j < logn; j++) { n       if (s[i][j]) n         f[i][j] = i; n       else { n         f[i][j] = n + 1; n         for (int k = 0; k < logn; k++) { n           if (s[i][k] && nxt[i][k]) f[i][j] = min(f[i][j], f[nxt[i][k]][j]); n         } n       } n     } n   } n   for (int i = 1; i <= q; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     bool ans = 0; n     for (int j = 0; j < logn; j++) { n       if (s[y][j] && f[x][j] <= y) ans = 1; n     } n     if (ans) n       printf( Shi n ); n     else n       printf( Fou n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Bug { n   int a, id; n } B[100005]; n struct Stu { n   int b, c, id; n   bool operator<(const Stu &A) const { return c > A.c; } n } S[100005]; n int n, m, s; n int ans[100005]; n int ans1[100005]; n bool cmp(Bug A, Bug B) { return A.a > B.a; } n bool cmp1(Stu A, Stu B) { return A.b > B.b; } n bool check(int k) { n   int cost = s; n   int l = 0, r = 0; n   priority_queue<Stu> P; n   while (l < m) { n     while (r < n && S[r].b >= B[l].a) { n       P.push(S[r++]); n     } n     Stu T; n     if (P.size()) n       T = P.top(), P.pop(); n     else { n       return false; n     } n     cost -= T.c; n     if (cost < 0) return false; n     for (int i = l; i < l + k && i < m; i++) ans1[B[i].id] = T.id + 1; n     l += k; n   } n   for (int i = 0; i < m; i++) ans[i] = ans1[i]; n   return true; n } n int main() { n   scanf( %d%d%d , &n, &m, &s); n   int Max = 0, Min = 0; n   for (int i = 0; i < m; i++) { n     scanf( %d , &B[i].a); n     B[i].id = i; n     if (B[i].a > Min) Min = B[i].a; n   } n   for (int i = 0; i < n; i++) { n     scanf( %d , &S[i].b); n     S[i].id = i; n   } n   for (int i = 0; i < n; i++) { n     scanf( %d , &S[i].c); n     if (S[i].b > Max && S[i].c <= s) Max = S[i].b; n   } n   sort(B, B + m, cmp); n   sort(S, S + n, cmp1); n   int l = 1, r = m + 1; n   while (l < r) { n     int m = (l + r) / 2; n     if (check(m)) n       r = m; n     else n       l = m + 1; n   } n   if (Max < Min) n     puts( NO ); n   else { n     puts( YES ); n     for (int i = 0; i < m; i++) printf( %d  , ans[i]); n     puts(  ); n   } n   return 0; n } n


#include<bits/stdc++.h> n using namespace std; n #define ll long long int n #define rep(i,a,b) for(int i=a;i<b;i++) n #define arrr int n;cin>>n;int a[n];for(int i=0;i<n;i++){cin>>a[i];} n const int N = 1e6; n // bool prime[1000001]={true}; n  n // void sieve(){ n //    memset(prime,1,sizeof(prime)); n //    prime[0]=prime[1]=false; n //    for(ll i=2 ; i*i<=N ; i++){ n //          for(int j=i*2 ; j<=N;j+=i){ n //             prime[j] = false; n //          } n //    } n // } n  n  n  n   n void solve(){ n    int n; n    cin>>n; n    if(n>1099){ n       cout<< YES <<  n ; n       return; n    } n  n    while(n>=0){ n       if(n%11 == 0){ n          cout<< YES <<  n ; n          return; n       } n       n-=111; n    } n    cout<< NO <<  n ; n } n   n int main(){ n   n    ios_base::sync_with_stdio(false); n    cin.tie(NULL); cout.tie(NULL); n     n  n    int t; n    t=1; n    cin>>t; n    while(t--)solve(); n  n  n           n        n      n }


#include <bits/stdc++.h> n using namespace std; n long long MOD = 1000000007; n long long power(long long x, long long n, long long mod) { n   if (n == 0) { n     return 0; n   } n   long long temp = power(x, n / 2, mod); n   if (n % 2 == 1) { n     return (x * temp * temp) % mod; n   } else { n     return (temp * temp) % mod; n   } n } n long long abs(long long a, long long b) { n   if (a > b) { n     return a - b; n   } n   return b - a; n } n int main(int argc, const char* argv[]) { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   long long n; n   cin >> n; n   if (n % 2 == 0) { n     cout <<  NO  << endl; n     return 0; n   } n   cout <<  YES  << endl; n   for (long long i = 1; i <= (n); i++) { n     if (i != 1) { n       cout <<    ; n     } n     cout << 2 * i - 1 + (i % 2 == 0); n   } n   for (long long i = 1; i <= (n); i++) { n     if (i != 0) { n       cout <<    ; n     } n     cout << 2 * i - 1 + (i % 2 == 1); n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, s; n   int ans = 0; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> s; n     ans += s * i; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char s[200222]; n int main() { n   while (scanf( %s , s) > 0) { n     int n = strlen(s); n     int a, b, c; n     a = b = c = 0; n     for (int i = 0; i < n; i++) { n       if (s[i] ==  0 ) n         a++; n       else if (s[i] ==  1 ) n         b++; n     } n     int fst = (n - 1) / 2; n     int snd = (n - 2) / 2; n     if (b <= fst) puts( 00 ); n     if (a <= snd + 1 && b + (s[n - 1] ==  ? ) <= fst + 1 && s[n - 1] !=  0 ) n       puts( 01 ); n     if (b <= fst + 1 && a + (s[n - 1] ==  ? ) <= snd + 1 && s[n - 1] !=  1 ) n       puts( 10 ); n     if (a <= snd) puts( 11 ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int ans = 0, visited[200005] = {0}; n vector<int> edges[200005]; n void dfs(int x) { n   visited[x] = 1; n   if (visited[edges[x][0]] == 1) n     ans++; n   else n     dfs(edges[x][0]); n } n int main() { n   int n, i, x, count = 0; n   cin >> n; n   for (i = 1; i <= n; i++) { n     cin >> x; n     edges[i].push_back(x); n   } n   for (i = 1; i <= n; i++) { n     cin >> x; n     if (x == 1) count++; n   } n   for (i = 1; i <= n; i++) n     if (visited[i] == 0) dfs(i); n   if (ans == 1) ans = 0; n   if (count % 2 == 0) ans++; n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n const int Maxn = 200 * 1000 + 10; n int n, arr[Maxn], ind[Maxn], ans; n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) scanf( %d , &arr[i]); n   int a; n   for (int i = 0; i < n; i++) { n     scanf( %d , &a); n     ind[a] = i; n   } n   int prev = 0; n   for (int i = 0; i < n; i++) { n     if (ind[arr[i]] < prev) { n       cout << n - i << endl; n       return 0; n     } n     prev = ind[arr[i]]; n   } n   cout << 0 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 10; n int a[maxn], vis[maxn]; n int main() { n   int n, p, flag = 0, mark = 0, tot = 0, Min = 1e9 + 7, cur, Max = -1e9 - 7, n             Maxcur; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     if (a[i] == 0) n       vis[i] = 1, mark = 1; n     else if (a[i] < 0) { n       flag++; n       if (a[i] < Min) cur = i; n       Min = min(Min, a[i]); n       if (a[i] > Max) Maxcur = i; n       Max = max(Max, a[i]); n     } n   } n   if (flag % 2) vis[Maxcur] = 1, mark = 1; n   for (int i = 1; i <= n; i++) n     if (vis[i]) { n       p = i; n       break; n     } n   for (int i = p + 1; i <= n; i++) n     if (vis[i]) printf( 1 %d %d n , p, i), p = i, tot++; n   if (tot != n - 1 && mark) printf( 2 %d n , p); n   for (int i = 1; i <= n; i++) n     if (!vis[i]) { n       p = i; n       break; n     } n   for (int i = p + 1; i <= n; i++) n     if (!vis[i]) printf( 1 %d %d n , p, i), p = i; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1000000; n int cnt[N + 1]; n int check[N + 1]; n set<pair<int, int> > s; n int main() { n   int n, A; n   scanf( %d %d , &n, &A); n   for (int i = 1; i <= N; i++) { n     cnt[i] = 0; n     s.insert({cnt[i], i}); n   } n   for (int i = 0; i < n; i++) { n     int foo; n     scanf( %d , &foo); n     if (!check[foo]) { n       s.erase(s.find({cnt[foo], foo})); n       s.insert({++cnt[foo], foo}); n     } n     while ((!s.empty()) && s.begin()->first < cnt[A]) { n       check[s.begin()->second] = 1; n       s.erase(s.begin()); n     } n   } n   for (auto x : s) { n     if (x.second != A) { n       cout << x.second << endl; n       return 0; n     } n   } n   puts( -1 ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int l, r, sum = 0, tempVariable, doneTillHereInArrayOfNumbers = 3; n long long int arrayOfNumbers[100000]; n long long int createNextLuckyNumber() { n   if (doneTillHereInArrayOfNumbers % 2 == 0) { n     arrayOfNumbers[doneTillHereInArrayOfNumbers] = n         (arrayOfNumbers[doneTillHereInArrayOfNumbers / 2 - 1] * 10) + 7; n   } else { n     arrayOfNumbers[doneTillHereInArrayOfNumbers] = n         (arrayOfNumbers[doneTillHereInArrayOfNumbers / 2] * 10) + 4; n   } n   return arrayOfNumbers[doneTillHereInArrayOfNumbers++]; n } n long long int FirstLuckyNumberGreaterThanOrEqualToL() { n   if (4 < l) { n     goto down; n   } n   if (4 >= l && 4 <= r) { n     sum += 4 * (4 - l + 1); n     l = 5; n   } else if (4 >= l && 4 > r) { n     sum += 4 * (r - l + 1); n     l = r; n     cout << sum; n     exit(0); n   } n down: n   if (7 < l) { n     goto down2; n   } n   if (7 >= l && 7 <= r) { n     sum += 7 * (7 - l + 1); n     l = 8; n   } else if (7 >= l && 7 > r) { n     sum += 7 * (r - l + 1); n     cout << sum; n     exit(0); n   } n down2: n   long long int num = 0; n   while (num < l) { n     num = createNextLuckyNumber(); n   } n   return num; n } n int main() { n   cin >> l >> r; n   arrayOfNumbers[1] = 4; n   arrayOfNumbers[2] = 7; n   tempVariable = FirstLuckyNumberGreaterThanOrEqualToL(); n   if (tempVariable <= r) { n     sum += tempVariable * (tempVariable - l + 1); n     l = tempVariable; n   } else { n     sum += tempVariable * (r - l + 1); n     cout << sum; n     exit(0); n   } n   l++; n   while (l <= r) { n     tempVariable = createNextLuckyNumber(); n     if (tempVariable <= r) { n       sum += tempVariable * (tempVariable - l + 1); n       l = tempVariable; n     } else { n       sum += tempVariable * (r - l + 1); n       break; n     } n     l++; n   } n   cout << sum; n   return 0; n } n


#include <bits/stdc++.h> n const int INF = 1000000009; n using namespace std; n int main() { n   int n, i, l = 0, r = 0; n   string s; n   cin >> n >> s; n   s =  .  + s; n   for (i = 1; i < s.size(); i++) n     if (s[i] ==  L ) n       l = 1; n     else if (s[i] ==  R ) n       r = 1; n   if (r && l) n     cout << s.find( R ) <<     << s.find( L ) - 1 <<   n ; n   else if (r && !l) n     cout << s.find( R ) <<     << s.rfind( R ) + 1 <<   n ; n   else n     cout << s.rfind( L ) <<     << s.find( L ) - 1 <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x7FFFFFFF; n struct prioritycom { n   bool operator()(const int& l, const int& r) { return l < r; } n }; n template <typename T> n T lcm(T a, T b) { n   return a / __gcd(a, b) * b; n } n template <class T> n T bigmod(T n, T p, T m) { n   if (p == 0) return 1; n   if (p % 2 == 0) { n     T t = bigmod(n, p / 2, m); n     return (t * t) % m; n   } n   return (bigmod(n, p - 1, m) * n) % m; n } n template <class string> n long long int strton(string s) { n   long long int won; n   stringstream ss; n   ss << s; n   ss >> won; n   return won; n } n template <class T> n string ntostr(T n) { n   stringstream ss; n   ss << n; n   return ss.str(); n } n template <class T> n inline T mod(T n, T m) { n   return (n % m + m) % m; n } n template <class T> n bool isprime(T n) { n   if (n % 2 == 0) { n     if (n == 2) n       return true; n     else n       return false; n   } n   T m = sqrt(n); n   for (T i = 3; i <= m; i += 2) { n     if (n % i == 0) return false; n   } n   return true; n } n bool isvowel(char ch) { n   ch = toupper(ch); n   if (ch ==  A  || ch ==  U  || ch ==  I  || ch ==  O  || ch ==  E ) n     return true; n   return false; n } n bool isconsonant(char ch) { n   if (isalpha(ch) && !isvowel(ch)) return true; n   return false; n } n char s[15][15]; n int main() { n   int x, y, cnt = 0; n   for (int i = 1; i <= 9; i++) { n     for (int j = 1; j <= 11; j++) { n       scanf( %c , &s[i][j]); n     } n     getchar(); n     if (i % 3 == 0 && i != 9) getchar(); n   } n   scanf( %d%d , &x, &y); n   if (x % 3 == 0) { n     if (y == 3 || y == 6 || y == 9) { n       for (int i = 7; i <= 9; i++) { n         for (int j = 9; j <= 11; j++) { n           if (s[i][j] !=  . ) { n             cnt++; n           } else n             s[i][j] =  ! ; n         } n       } n     } n     if (y == 1 || y == 4 || y == 7) { n       for (int i = 7; i <= 9; i++) { n         for (int j = 1; j <= 3; j++) { n           if (s[i][j] !=  . ) { n             cnt++; n           } else n             s[i][j] =  ! ; n         } n       } n     } n     if (y == 2 || y == 5 || y == 8) { n       for (int i = 7; i <= 9; i++) { n         for (int j = 5; j <= 7; j++) { n           if (s[i][j] !=  . ) { n             cnt++; n           } else n             s[i][j] =  ! ; n         } n       } n     } n   } n   if (x == 2 || x == 5 || x == 8) { n     if (y == 3 || y == 6 || y == 9) { n       for (int i = 4; i <= 6; i++) { n         for (int j = 9; j <= 11; j++) { n           if (s[i][j] !=  . ) { n             cnt++; n           } else n             s[i][j] =  ! ; n         } n       } n     } n     if (y == 1 || y == 4 || y == 7) { n       for (int i = 4; i <= 6; i++) { n         for (int j = 1; j <= 3; j++) { n           if (s[i][j] !=  . ) { n             cnt++; n           } else n             s[i][j] =  ! ; n         } n       } n     } n     if (y == 2 || y == 5 || y == 8) { n       for (int i = 4; i <= 6; i++) { n         for (int j = 5; j <= 7; j++) { n           if (s[i][j] !=  . ) { n             cnt++; n           } else n             s[i][j] =  ! ; n         } n       } n     } n   } n   if (x == 1 || x == 4 || x == 7) { n     if (y == 3 || y == 6 || y == 9) { n       for (int i = 1; i <= 3; i++) { n         for (int j = 9; j <= 11; j++) { n           if (s[i][j] !=  . ) { n             cnt++; n           } else n             s[i][j] =  ! ; n         } n       } n     } n     if (y == 1 || y == 4 || y == 7) { n       for (int i = 1; i <= 3; i++) { n         for (int j = 1; j <= 3; j++) { n           if (s[i][j] !=  . ) { n             cnt++; n           } else n             s[i][j] =  ! ; n         } n       } n     } n     if (y == 2 || y == 5 || y == 8) { n       for (int i = 1; i <= 3; i++) { n         for (int j = 5; j <= 7; j++) { n           if (s[i][j] !=  . ) { n             cnt++; n           } else n             s[i][j] =  ! ; n         } n       } n     } n   } n   if (cnt == 9) { n     for (int i = 1; i <= 9; i++) { n       for (int j = 1; j <= 11; j++) { n         if (s[i][j] ==  . ) s[i][j] =  ! ; n         printf( %c , s[i][j]); n       } n       printf(  n ); n       if (i % 3 == 0) printf(  n ); n     } n   } else { n     for (int i = 1; i <= 9; i++) { n       for (int j = 1; j <= 11; j++) { n         printf( %c , s[i][j]); n       } n       printf(  n ); n       if (i % 3 == 0) printf(  n ); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int const N = 1e5 + 10; n vector<int> e[N], b[N], d[N]; n bool v[N], in[N]; n void dfs(int root, int parent, int depth) { n   v[root] = true, in[root] = true; n   d[depth].push_back(root); n   for (auto child : e[root]) { n     if (in[child]) b[root].push_back(child); n     if (!v[child]) dfs(child, root, depth + 1); n   } n   in[root] = false; n } n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   if (m & 1) return puts( No solution ), 0; n   for (int i = 0, x, y; i < m; ++i) { n     scanf( %d%d , &x, &y); n     e[x].push_back(y), e[y].push_back(x); n   } n   dfs(1, -1, 0); n   for (int t = n - 1, i; t >= 0; --t) { n     for (auto x : d[t]) { n       int sz = (int)b[x].size(); n       for (i = sz - 1; i >= 1; i -= 2) n         printf( %d %d %d n , b[x][i], x, b[x][i - 1]); n       if (sz & 1) b[b[x][0]].push_back(x); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int oo = 0x3f3f3f3f; n const long long ooo = 9223372036854775807ll; n const int _cnt = 1000 * 1000 + 7; n const int _p = 1000 * 1000 * 1000 + 7; n const int N = 100005; n const double PI = acos(-1.0); n const double eps = 1e-9; n int o(int x) { return x % _p; } n int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } n int lcm(int a, int b) { return a / gcd(a, b) * b; } n void file_put() { n   string s =  /home/jslijin/jslijin/code/ ; n   freopen((s +  code.in ).c_str(),  r , stdin); n   freopen((s +  code.out ).c_str(),  w , stdout); n } n int n, q, last[N], a[N], cnt[N], ans = 0, op, x, y, B; n inline void get(int x) { n   if (x + a[x] >= n || x / B != (x + a[x]) / B) n     cnt[x] = 1, last[x] = x; n   else n     cnt[x] = cnt[x + a[x]] + 1, last[x] = last[x + a[x]]; n } n int main() { n   scanf( %d%d , &n, &q), B = sqrt(n) + 0.5; n   for (int i = (0); i <= (n - 1); ++i) scanf( %d , &a[i]); n   for (int i = (n - 1); i >= (0); --i) get(i); n   while (q--) { n     scanf( %d%d , &op, &x), --x; n     if (op) { n       ans = 0; n       int ret = x; n       for (; x < n; ret = last[x], x = ret + a[last[x]]) ans += cnt[x]; n       printf( %d %d n , ++ret, ans); n     } else { n       scanf( %d , &y), a[x] = y; n       for (int i = (x); i >= (x / B * B); --i) get(i); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   scanf( %d %d , &n, &m); n   vector<string> grid; n   int start_x, start_y; n   for (int i = 0; i < n; i++) { n     string s; n     cin >> s; n     grid.push_back(s); n     for (int j = 0; j < s.size(); j++) { n       if (s[j] ==  S ) { n         start_x = i; n         start_y = j; n       } n     } n   } n   string actions; n   cin >> actions; n   int out = 0; n   vector<pair<int, int>> mapping = {{-1, 0}, {0, -1}, {0, 1}, {1, 0}}; n   do { n     bool found = false; n     int x = start_x; n     int y = start_y; n     for (int i = 0; i < actions.size(); i++) { n       int action = actions[i] -  0 ; n       x += mapping[action].first; n       y += mapping[action].second; n       if (x < 0 || x >= n || y < 0 || y >= m || grid[x][y] ==  # ) { n         break; n       } n       if (grid[x][y] ==  E ) { n         found = true; n         break; n       } n     } n     out += found; n   } while (next_permutation(mapping.begin(), mapping.end())); n   printf( %d , out); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100000; n const double pi = acos(-1.0); n int n, q, a[100100], seg[1700000], rev[1700000], tag[1700000]; n char s[10]; n int read() { n   int 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 void pushdown(int id, int l, int r) { n   if (tag[id] != -1) { n     seg[id << 1] = tag[id]; n     seg[id << 1 | 1] = tag[id]; n     tag[id << 1] = tag[id]; n     tag[id << 1 | 1] = tag[id]; n     rev[id << 1] = 0; n     rev[id << 1 | 1] = 0; n     tag[id] = -1; n   } n   if (rev[id]) { n     rev[id << 1] ^= 1; n     rev[id << 1 | 1] ^= 1; n     seg[id << 1] ^= 1; n     seg[id << 1 | 1] ^= 1; n     rev[id] = 0; n   } n } n void modify_val(int id, int l, int r, int ql, int qr, int val) { n   if (ql > qr) return; n   pushdown(id, l, r); n   if ((l >= ql) && (r <= qr)) { n     seg[id] = val; n     tag[id] = val; n     return; n   } n   int mid = (l + r) >> 1; n   if (ql <= mid) modify_val(id << 1, l, mid, ql, qr, val); n   if (qr > mid) modify_val(id << 1 | 1, mid + 1, r, ql, qr, val); n } n void modify_rev(int id, int l, int r, int ql, int qr) { n   if (ql > qr) return; n   pushdown(id, l, r); n   if ((l >= ql) && (r <= qr)) { n     seg[id] ^= 1; n     rev[id] = 1; n     return; n   } n   int mid = (l + r) >> 1; n   if (ql <= mid) modify_rev(id << 1, l, mid, ql, qr); n   if (qr > mid) modify_rev(id << 1 | 1, mid + 1, r, ql, qr); n } n int query(int id, int l, int r, int pos) { n   pushdown(id, l, r); n   if (l == r) return seg[id]; n   int mid = (l + r) >> 1; n   if (pos <= mid) n     return query(id << 1, l, mid, pos); n   else n     return query(id << 1 | 1, mid + 1, r, pos); n } n int main() { n   n = read(); n   q = read(); n   for (int i = 1; i <= n; i++) a[i] = read(); n   memset(tag, -1, sizeof(tag)); n   while (q--) { n     scanf( %s , s); n     int x = read(); n     if (s[0] ==  > ) { n       if (x >= 0) { n         modify_val(1, -N, N, x + 1, N, 1); n         modify_val(1, -N, N, -N, -x - 1, 0); n       } else { n         modify_val(1, -N, N, -x, N, 1); n         modify_val(1, -N, N, -N, x, 0); n         modify_rev(1, -N, N, x + 1, -x - 1); n       } n     } else { n       if (x <= 0) { n         modify_val(1, -N, N, -N, x - 1, 1); n         modify_val(1, -N, N, -x + 1, N, 0); n       } else { n         modify_val(1, -N, N, x, N, 0); n         modify_val(1, -N, N, -N, -x, 1); n         modify_rev(1, -N, N, -x + 1, x - 1); n       } n     } n   } n   for (int i = 1; i <= n; i++) { n     if (query(1, -N, N, a[i])) a[i] *= -1; n     printf( %d  , a[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 3000 + 10; n int sum, d[maxn], dp[maxn][2]; n vector<pair<int, int> > edge[maxn]; n int min(int a, int b) { return a < b ? a : b; } n void dfs(int u, int f, int w) { n   if (f == -1) { n     sum = 0; n     d[u] = 0; n     dp[u][0] = dp[u][1] = 0; n   } else { n     dp[u][1] = dp[f][1] + w; n     dp[u][0] = min(dp[f][0], dp[f][1]) + !w; n     d[u] = d[f] + w; n   } n   for (vector<pair<int, int> >::iterator it = edge[u].begin(); n        it != edge[u].end(); it++) { n     int to = it->first; n     int wt = it->second; n     if (to == f) continue; n     sum += wt; n     dfs(to, u, wt); n   } n } n int main() { n   int n, u, v, ret = maxn; n   cin >> n; n   for (int i = 0; i < n - 1; i++) { n     cin >> u >> v; n     edge[u].push_back(make_pair(v, 0)); n     edge[v].push_back(make_pair(u, 1)); n   } n   for (int i = 1; i <= n; i++) { n     dfs(i, -1, 0); n     for (int j = 1; j <= n; j++) { n       ret = min(ret, sum + min(dp[j][0], dp[j][1]) - d[j]); n     } n   } n   cout << ret << endl; n   return 0; n } n


#include <bits/stdc++.h> n const long double eps = 1e-9; n using namespace std; n template <class T> n inline T MAX(const T &_a, const T &_b) { n   return ((_a > _b) ? _a : _b); n } n template <class T> n inline T MIN(const T &_a, const T &_b) { n   return ((_a < _b) ? _a : _b); n } n template <class T> n inline T MAX3(const T &_a, const T &_b, const T &_c) { n   return MAX(MAX(_a, _b), _c); n } n template <class T> n inline T MIN3(const T &_a, const T &_b, const T &_c) { n   return MIN(MIN(_a, _b), _c); n } n template <class T> n inline T MAX4(const T &_a, const T &_b, const T &_c, const T &_d) { n   return MAX(MAX3(_a, _b, _c), _d); n } n template <class T> n inline T MIN4(const T &_a, const T &_b, const T &_c, const T &_d) { n   return MIN(MIN3(_a, _b, _c), _d); n } n template <class T> n inline T ABS(const T &_a) { n   return ((_a < 0) ? -_a : _a); n } n template <class T> n inline T SQR(const T &_a) { n   return _a * _a; n } n template <class T> n inline T gcd(T _a, T _b) { n   for (T _r; (_r = _a % _b); _b = _r) _a = _b; n   return _b; n } n template <class T> n inline T lcm(const T &_a, const T &_b) { n   return (_a / gcd(_a, _b)) * _b; n } n template <class T> n inline bool BETW(const T &_a, const T &_b, const T &_c) { n   return (_a >= MIN(_b, _c) && _a <= MAX(_b, _c)); n } n template <class T> n inline bool EXT(const T &_a, const T &_b, const T &_c) { n   return !BETW(_a, _b, _c); n } n const int MAXN = 100010, MAXL = 20; n int N, father[MAXL][MAXN], level[MAXN], subTree[MAXN]; n vector<int> adj[MAXN]; n void dfs(int node, int prev) { n   int i; n   father[0][node] = prev; n   level[node] = level[prev] + 1; n   subTree[node] = 1; n   for (i = 0; i < ((int)adj[node].size()); i++) n     if (adj[node][i] != prev) { n       dfs(adj[node][i], node); n       subTree[node] += subTree[adj[node][i]]; n     } n } n inline int LCA(int a, int b) { n   int k; n   if (level[a] < level[b]) swap(a, b); n   k = MAXL - 1; n   while (level[a] > level[b]) { n     while (k && level[father[k][a]] < level[b]) k--; n     a = father[k][a]; n   } n   k = MAXL - 1; n   while (a != b) { n     while (k && father[k][a] == father[k][b]) k--; n     a = father[k][a]; n     b = father[k][b]; n   } n   return a; n } n inline int getFather(int a, int H) { n   for (int k = MAXL - 1; k >= 0; k--) n     if (H >= (1 << k)) { n       a = father[k][a]; n       H -= (1 << k); n     } n   return a; n } n int main() { n   int i, a, b, k, q, ab, x, y; n   scanf( %d , &N); n   for (i = 1; i < N; i++) { n     scanf( %d%d , &a, &b); n     adj[a].push_back(b); n     adj[b].push_back(a); n   } n   dfs(1, 0); n   for (k = 1; k < MAXL; k++) n     for (i = 1; i <= N; i++) father[k][i] = father[k - 1][father[k - 1][i]]; n   scanf( %d , &q); n   while (q--) { n     scanf( %d%d , &a, &b); n     ab = LCA(a, b); n     if ((level[a] - level[ab] + level[b] - level[ab]) & 1) { n       puts( 0 ); n       continue; n     } n     if (level[a] < level[b]) swap(a, b); n     if (a == b) { n       printf( %d n , N); n       continue; n     } n     if (level[a] == level[b]) { n       x = getFather(a, level[a] - level[ab] - 1); n       y = getFather(b, level[a] - level[ab] - 1); n       printf( %d n , N - subTree[x] - subTree[y]); n     } else { n       x = getFather(a, (level[a] - level[ab] + level[b] - level[ab]) / 2 - 1); n       printf( %d n , subTree[father[0][x]] - subTree[x]); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s; n int stx, sty, i; n int f[2000][2000]; n int main() { n   cin >> s; n   stx = 100; n   sty = 100; n   f[stx][sty] = 1; n   for (i = 0; i < s.size(); i++) { n     if (s[i] ==  U ) stx--; n     if (s[i] ==  D ) stx++; n     if (s[i] ==  L ) sty--; n     if (s[i] ==  R ) sty++; n     f[stx][sty]++; n     if (f[stx - 1][sty] + f[stx + 1][sty] + f[stx][sty - 1] + f[stx][sty + 1] + n             f[stx][sty] >= n         3) { n       cout <<  BUG  << endl; n       return 0; n     } n   } n   cout <<  OK  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n void run() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n } n int32_t main() { n   run(); n   vector<long long> dp((long long)(3e6), 0); n   dp[3] = 4; n   for (long long i = 4; i < (long long)(3e6); i++) { n     long long temp1 = (2 * dp[i - 2] + dp[i - 1]); n     long long temp2 = 4 + (4 * dp[i - 4] + 4 * dp[i - 3] + dp[i - 2]); n     if (i % 3 == 0) { n       dp[i] = temp2; n     } else { n       dp[i] = temp1; n     } n     dp[i] %= (long long)(1e9 + 7); n   } n   long long t; n   cin >> t; n   while (t--) { n     long long n; n     cin >> n; n     cout << dp[n] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, dfs_low[100100], dfs_num[100100], cont; n int vis[100100]; n vector<int> AdjList[100100]; n long long int v[100100]; n long long int sum = 0, numWay = 1; n vector<int> S; n void tarjan(int u) { n   dfs_low[u] = dfs_num[u] = cont++; n   vis[u] = 1; n   S.push_back(u); n   for (int i = 0; i < (int)AdjList[u].size(); i++) { n     int v = AdjList[u][i]; n     if (vis[v] == -1) tarjan(v); n     if (vis[v]) dfs_low[u] = min(dfs_low[u], dfs_low[v]); n   } n   if (dfs_low[u] == dfs_num[u]) { n     int qtd = 1; n     long long int val = 10000000100000; n     while (true) { n       int p = S.back(); n       S.pop_back(); n       vis[p] = 0; n       if (v[p] < val) { n         val = v[p]; n         qtd = 1; n       } else if (v[p] == val) n         qtd++; n       if (u == p) break; n     } n     sum += val; n     numWay = (numWay * qtd) % 1000000007; n   } n } n int main() { n   int a, b; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %lld , &v[i]); n   } n   scanf( %d , &m); n   for (int i = 0; i < m; i++) { n     scanf( %d%d , &a, &b); n     AdjList[a].push_back(b); n   } n   memset(vis, -1, sizeof vis); n   for (int i = 1; i <= n; i++) { n     if (!dfs_num[i]) tarjan(i); n   } n   cout << sum <<     << numWay << endl; n } n


#include <bits/stdc++.h> n using namespace std; n char a[1005]; n int f[1005], flag[1005], prime[1005], g[1005]; n int n, num, ok[1005], k, i, id, ans, cnt, j; n int main() { n   scanf( %s , a + 1); n   n = strlen(a + 1); n   num = 1; n   ok[1] = 1; n   for (i = 1; a[i]; i++) g[a[i] -  a ]++; n   for (i = 2; i <= n; i++) { n     if (!flag[i]) n       if ((prime[++cnt] = i) > n / 2) ok[i] = 1, num++; n     for (j = 1; j <= cnt && prime[j] * i <= n; j++) flag[prime[j] * i] = 1; n   } n   for (i = 0; i < 26; i++) n     if (ans < g[i]) ans = g[i], id = i; n   num = n - num; n   if (ans < num) return puts( NO ), 0; n   puts( YES ); n   for (i = 1; i <= n; i++) n     if (!ok[i]) n       putchar( a  + id); n     else { n       for (k = 0; k < 26; k++) n         if (k != id && g[k]) { n           putchar( a  + k); n           g[k]--; n           break; n         } n       if (k == 26) putchar( a  + id); n     } n } n


#include <bits/stdc++.h> n using namespace std; n double PI = acos(-1); n double EPS = 1e-7; n int INF = 1000000000; n int MOD = 1000000007; n int MAXINT = 2147483647; n long long INFLL = 1000000000000000000LL; n long long MAXLL = 9223372036854775807LL; n int mx[8] = {-1, 1, 0, 0, -1, -1, 1, 1}; n int my[8] = {0, 0, -1, 1, -1, 1, -1, 1}; n int x, k, p; n double per; n int lim = (1 << 8) - 1; n double dp[205][235][2][1 << 8]; n bool vs[205][235][2][1 << 8]; n double go(int pos, int cons, int t, int mask) { n   if (vs[pos][cons][t][mask]) return dp[pos][cons][t][mask]; n   vs[pos][cons][t][mask] = 1; n   double &ret = dp[pos][cons][t][mask]; n   ret = 0.0; n   if (pos == 0) { n     if (mask == 0) { n       if (t == 1) n         return ret = 8; n       else n         return ret = 8 + cons; n     } else { n       int co = 0; n       while (mask % 2 == 0) { n         co++; n         mask /= 2; n       } n       return ret = co; n     } n   } n   if (mask < lim) { n     ret += (1.0 - per) * go(pos - 1, cons, t, mask + 1); n   } else { n     if (t == 0) n       ret += (1.0 - per) * go(pos - 1, 1, 1, 0); n     else n       ret += (1.0 - per) * go(pos - 1, cons, 0, 0); n   } n   bool d = mask & (1 << 7); n   if (d) mask -= (1 << 7); n   if (d == t) { n     ret += per * go(pos - 1, cons + 1, d, 2 * mask); n   } else { n     ret += per * go(pos - 1, 1, d, 2 * mask); n   } n   return ret; n } n int main() { n   scanf( %d%d%d , &x, &k, &p); n   per = (double)p / 100.0; n   int mask = 0; n   int cons = 0; n   bool t = (x & (1 << 8)); n   for (int(a) = (0); (a) <= (30); (a)++) { n     if (a <= 7) { n       if (x & (1 << a)) mask += (1 << a); n     } else { n       if (t && ((x & (1 << a)) > 0)) cons++; n       if (t && ((x & (1 << a)) == 0)) break; n       if (!t && ((x & (1 << a)) == 0)) cons++; n       if (!t && ((x & (1 << a)) > 0)) break; n     } n   } n   printf( %.10lf n , go(k, cons, t, mask)); n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> pa, pb; n struct fenw { n   vector<long long> fw; n   long long n; n   void ini(long long pn) { n     n = pn; n     fw.clear(); n     fw.resize(n, 0); n   } n   void ad(long long l, long long x) { n     while (l < n) { n       fw[l] += x; n       l |= (l + 1); n     } n   } n   long long sm(long long l) { n     long long r = 0; n     while (l >= 0) { n       r += fw[l]; n       l &= (l + 1); n       --l; n     } n     return r; n   } n }; n fenw fa, fb, fr; n vector<long long> resd; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   long long n; n   cin >> n; n   pa.resize(n); n   pb.resize(n); n   for (long long i = 0; i < (n); ++i) cin >> pa[i]; n   for (long long i = 0; i < (n); ++i) cin >> pb[i]; n   fa.ini(n); n   fb.ini(n); n   fr.ini(n); n   resd.resize(n); n   for (long long i = n - 1; i >= 0; --i) { n     long long cc = fa.sm(pa[i]) + fb.sm(pb[i]); n     fa.ad(pa[i], 1); n     fb.ad(pb[i], 1); n     resd[i] += cc; n     if (i > 0) resd[i - 1] += resd[i] / (n - i); n     resd[i] %= (n - i); n   } n   for (long long i = 0; i < (n); ++i) { n     long long mn = 0; n     long long mx = n; n     long long md; n     while (mx - mn > 1) { n       md = (mx + mn) / 2; n       if (md - fr.sm(md - 1) <= resd[i]) n         mn = md; n       else n         mx = md; n     } n     fr.ad(mn, 1); n     cout << mn <<    ; n   } n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1000000007LL; n const int N = 10; n char str[N][N]; n int n = 8, ans, way[4][2] = {2, 2, 2, -2, -2, 2, -2, -2}; n int flag[N][N][N][N]; n void dfs(int x, int y, int u, int v) { n   flag[x][y][u][v] = 1; n   if (x == u && v == y && str[x][y] !=  # ) { n     ans = 1; n     return; n   } n   if (ans) return; n   for (int i = 0; i < 4; i++) { n     int xx = x + way[i][0]; n     int yy = y + way[i][1]; n     if (xx >= 0 && xx < n && yy >= 0 && yy < n) { n       for (int j = 0; j < 4; j++) { n         int uu = u + way[j][0]; n         int vv = v + way[j][1]; n         if (uu >= 0 && uu < n && vv >= 0 && vv < n) { n           if (!flag[xx][yy][uu][vv]) { n             dfs(xx, yy, uu, vv); n           } n         } n       } n     } n   } n } n int main() { n   int t; n   cin >> t; n   while (t--) { n     int x = -1, y = -1, u = -1, v = -1; n     for (int i = 0; i < n; i++) { n       cin >> str[i]; n       for (int j = 0; j < n; j++) { n         if (str[i][j] ==  K ) { n           if (x == -1) { n             x = i; n             y = j; n           } else n             u = i, v = j; n           str[i][j] =  . ; n         } n       } n     } n     ans = 0; n     memset(flag, 0, sizeof(flag)); n     dfs(x, y, u, v); n     puts(ans ?  YES  :  NO ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double maxx(double a, double b) { return a > b ? a : b; } n int minn(int a, int b) { return a < b ? a : b; } n int abss(int a) { return a < 0 ? (-a) : a; } n const double eps = 1e-8, PI = 3.141592653589, INF = 1e7; n int N; n double L, R; n double inpt[30][3]; n double dp[1 << 20]; n double DP(int, double); n double Get(int, double); n int main() { n   scanf( %d%lf%lf , &N, &L, &R); n   double ans = L; n   for (int i = 0; i < 1 << N; i++) dp[i] = -INF; n   dp[0] = L; n   for (int i = 1; i <= N; i++) n     scanf( %lf%lf%lf , &inpt[i][0], &inpt[i][1], &inpt[i][2]); n   for (int mask = 0; mask < 1 << N; mask++) { n     ans = maxx(dp[mask], ans); n     if (ans > R) { n       ans = R; n       break; n     } n     for (int np = 1; np <= N; np++) { n       int nxt = 1 << np - 1; n       if (mask & nxt) continue; n       dp[mask | nxt] = maxx(dp[mask | nxt], dp[mask] + Get(np, dp[mask])); n     } n   } n   printf( %.8f n , ans - L); n   return 0; n } n double Get(int i, double stp) { n   double x1 = inpt[i][0]; n   double y1 = inpt[i][1]; n   double ang = inpt[i][2] * PI / 180; n   double dist = sqrt(y1 * y1 + (x1 - stp) * (x1 - stp)); n   double ang2 = acos((x1 - stp) / dist); n   ang2 = PI - ang2 - ang; n   double s = dist * sin(ang); n   if (ang2 < eps) n     s = INF; n   else n     s /= sin(ang2); n   return s; n } n


#include <bits/stdc++.h> n using namespace std; n string c; n int main() { n   int i, j, k, n, m, l; n   scanf( %d%d , &n, &k); n   if (n * (n - 1) / 2 <= k) { n     puts( no solution ); n     return 0; n   } n   for (i = 1; i <= n; i++) printf( %d %d n , 0, i); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int t, n, m; n long long now, ans; n char ch[1000039]; n int main() { n   scanf( %d , &t); n   while (t--) { n     scanf( %d %s , &m, ch + 1); n     n = ans = strlen(ch + 1), now = n - 1; n     for (int a = 1; a <= m; a++) { n       for (int b = 1, c = n; b < ch[a] -  0 ; b++) { n         (ans += now) %= 1000000007; n         for (int d = a + 1; d <= c && n < m; d++) { n           ch[++n] = ch[d]; n         } n       } n       (now *= ch[a] -  0 ) %= 1000000007; n       ((--now) += 1000000007) %= 1000000007; n     } n     printf( %I64d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int q; n int in[100005], out[100005]; n vector<int> g[100005]; n long long ans; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= m; i++) { n     int u, v; n     scanf( %d%d , &u, &v); n     if (u < v) swap(u, v); n     out[u]++; n     in[v]++; n     g[v].push_back(u); n   } n   for (int i = 1; i <= n; i++) ans += 1LL * in[i] * out[i]; n   printf( %lld n , ans); n   scanf( %d , &q); n   for (int i = 1; i <= q; i++) { n     int x; n     scanf( %d , &x); n     ans -= 1LL * in[x] * out[x]; n     out[x] += in[x]; n     in[x] = 0; n     for (int i = 0; i < g[x].size(); i++) { n       int v = g[x][i]; n       ans += out[v] - in[v] - 1; n       in[v]++; n       out[v]--; n       g[v].push_back(x); n     } n     g[x].clear(); n     printf( %lld n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void c_p_c() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n } n int32_t main() { n   c_p_c(); n   string str; n   getline(cin, str); n   vector<long long> vec; n   long long z = 1; n   for (long long i = 0; i < str.length(); i++) { n     if (str[i] ==  1 ) { n       z = 0; n     } else if (z == 1 && str[i] ==  0 ) { n       continue; n     } n     vec.push_back((long long)str[i] -  0 ); n   } n   long long sig = 1; n   long long csz = 0, sz = vec.size(); n   for (auto ele : vec) { n     csz++; n     if (sig == 1 && ele == 0) { n       sig = 0; n       continue; n     } n     if (sz == csz && sig == 1) { n       continue; n     } n     cout << ele; n   } n   return 0; n } n


#include <bits/stdc++.h> n int arr[500005], st[500005][25], fa[500005][25], bit[500005]; n inline int calc(int x, int y) { n   if (arr[x] <= arr[y]) return x; n   return y; n } n inline int query(int l, int r) { n   if (l < 0 || l > r) return 0; n   int len = bit[r - l + 1]; n   return calc(st[l][len], st[r - (1 << len) + 1][len]); n } n int main() { n   for (int i = 2; i < 500005; i++) bit[i] = bit[i >> 1] + 1; n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d , arr + i); n     arr[i + n] = arr[i]; n   } n   for (int i = 0; i < n << 1; i++) { n     arr[i] = i - arr[i]; n     st[i][0] = i; n   } n   for (int i = 1; i < 22; i++) { n     for (int j = 0; j + (1 << i) <= n << 1; j++) n       st[j][i] = calc(st[j][i - 1], st[j + (1 << i - 1)][i - 1]); n   } n   for (int i = 0; i < n << 1; i++) fa[i][0] = query(arr[i], i); n   for (int i = 1; i < 20; i++) { n     for (int j = 0; j < n << 1; j++) fa[j][i] = fa[fa[j][i - 1]][i - 1]; n   } n   long long ans = 0; n   for (int i = n; i < n << 1; i++) { n     int x = i; n     ans += (arr[i] > i - n + 1) + 1; n     for (int j = 19; j >= 0; j--) { n       if (arr[fa[x][j]] > i - n + 1) { n         x = fa[x][j]; n         ans += 1 << j; n       } n     } n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long double pi = acos(-1); n struct point { n   long double x, y; n   point(long double x = 0, long double y = 0) : x(x), y(y) {} n   point operator+(const point &o) const { return point(x + o.x, y + o.y); } n   point operator-(const point &o) const { return point(x - o.x, y - o.y); } n   point &operator+=(const point &o) { n     x += o.x, y += o.y; n     return *this; n   } n   point &operator-=(const point &o) { n     x -= o.x, y -= o.y; n     return *this; n   } n   long double operator*(const point &o) const { return (x * o.x + y * o.y); } n   point operator*(long double c) const { return point(x * c, y * c); } n   point &operator*=(long double c) { n     x *= c, y *= c; n     return *this; n   } n   long double snorm() { return (x * x + y * y); } n }; n point operator*(long double c, point A) { return A * c; } n long double dist(point A, point B) { return sqrt((A - B).snorm()); } n long double dist(point X, point A, point B) { n   A -= X, B -= X; n   long double t = (A.snorm() - A * B) / (B - A).snorm(); n   if (t < 0 || t > 1) return sqrt(min(A.snorm(), B.snorm())); n   return sqrt((t * (B - A) + A).snorm()); n } n int N; n point P, a[100100]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> N >> P.x >> P.y; n   cout << fixed << setprecision(10); n   int i; n   for (i = 1; i <= N; i++) cin >> a[i].x >> a[i].y; n   a[N + 1] = a[1]; n   long double maxd = 0, mind = (1 << 30); n   for (i = 1; i <= N; i++) { n     maxd = max(maxd, dist(P, a[i])); n     mind = min(mind, dist(P, a[i], a[i + 1])); n   } n   cout << pi * (maxd * maxd - mind * mind) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int 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 <=  9  && ch >=  0 ) { n     x = 10 * x + ch -  0 ; n     ch = getchar(); n   } n   return x * f; n } n void print(int x) { n   if (x < 0) putchar( - ), x = -x; n   if (x >= 10) print(x / 10); n   putchar(x % 10 +  0 ); n } n const int N = 200100, inf = 0X3f3f3f3f; n int last[N], ecnt; n struct EDGE { n   int to, nt; n } e[N]; n inline void add(int u, int v) { n   e[++ecnt] = (EDGE){v, last[u]}; n   last[u] = ecnt; n } n int n; n int dep[N], anc[N][20], low[N][20]; n int time_in[N], time_out[N], tim; n void dfs(int u) { n   time_in[u] = ++tim; n   for (int i = 1; (1 << i) <= dep[u]; ++i) n     anc[u][i] = anc[anc[u][i - 1]][i - 1]; n   for (int i = last[u]; i; i = e[i].nt) dfs(e[i].to); n   time_out[u] = tim; n } n inline int getlca(int u, int v) { n   if (dep[u] < dep[v]) swap(u, v); n   int len = dep[u] - dep[v]; n   for (int i = 0; (1 << i) <= len; ++i) n     if ((1 << i) & len) u = anc[u][i]; n   if (u == v) return u; n   for (int i = 18; ~i; --i) n     if (anc[u][i] != anc[v][i]) u = anc[u][i], v = anc[v][i]; n   return anc[u][0]; n } n inline void update(int &x, int y) { n   if (dep[y] < dep[x]) x = y; n } n struct node { n   int x, y, pos; n   friend bool operator<(const node &a, const node &b) { n     return a.x == b.x ? a.pos < b.pos : a.x < b.x; n   } n } q[N << 3]; n int tot; n int bit[N]; n inline void modify(int x) { n   for (; x <= n; x += (x & -x)) bit[x]++; n } n inline int query(int x) { n   int res(0); n   for (; x; x -= (x & -x)) res += bit[x]; n   return res; n } n int ans[N], cur[N]; n bool book[N]; n int main() { n   n = read(); n   int m, Q; n   register int i, j, u, v, grd; n   for (i = 2; i <= n; ++i) n     u = anc[i][0] = read(), dep[i] = dep[u] + 1, add(u, i); n   dfs(1); n   m = read(); n   for (i = 1; i <= n; ++i) low[i][0] = i; n   for (i = 1; i <= m; ++i) n     u = read(), v = read(), grd = getlca(u, v), update(low[u][0], grd), n     update(low[v][0], grd), q[++tot] = (node){time_in[u], time_in[v], -inf}, n     q[++tot] = (node){time_in[v], time_in[u], -inf}; n   for (i = n; i > 1; --i) update(low[anc[i][0]][0], low[i][0]); n   for (i = 1; i <= 18; ++i) n     for (u = 1; u <= n; ++u) low[u][i] = low[low[u][i - 1]][i - 1]; n   Q = read(); n   for (i = 1; i <= Q; ++i) { n     u = read(), v = read(), grd = getlca(u, v); n     if (dep[low[u][18]] > dep[grd] || dep[low[v][18]] > dep[grd]) { n       ans[i] = -1; n       continue; n     } n     for (j = 18; ~j; --j) n       if (dep[low[u][j]] > dep[grd]) u = low[u][j], ans[i] += 1 << j; n     for (j = 18; ~j; --j) n       if (dep[low[v][j]] > dep[grd]) v = low[v][j], ans[i] += 1 << j; n     if (u == grd || v == grd) { n       ans[i]++; n       continue; n     } n     book[i] = 1; n     q[++tot] = (node){time_in[u] - 1, time_in[v] - 1, i}; n     q[++tot] = (node){time_in[u] - 1, time_out[v], -i}; n     q[++tot] = (node){time_out[u], time_in[v] - 1, -i}; n     q[++tot] = (node){time_out[u], time_out[v], i}; n   } n   sort(q + 1, q + 1 + tot); n   for (i = 1; i <= tot; ++i) n     if (q[i].pos == -inf) n       modify(q[i].y); n     else n       q[i].pos > 0 ? cur[q[i].pos] += query(q[i].y) n                    : cur[-q[i].pos] -= query(q[i].y); n   for (i = 1; i <= Q; ++i) { n     if (cur[i]) n       ans[i]++; n     else if (book[i]) n       ans[i] += 2; n     print(ans[i]), puts(  ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1000000007; n const int N = 1e6; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n; n   cin >> n; n   if (n % 2 == 0) { n     cout <<  white n1 2 ; n   } else { n     cout <<  black n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, c, flag = 0, i, j; n   cin >> a >> b >> c; n   for (i = 0; i <= c; i++) { n     for (j = 0; j <= c; j++) { n       if (a * i + b * j == c) { n         flag = 1; n         break; n       } n     } n     if (flag == 1) break; n   } n   if (flag == 1) n     cout <<  Yes n ; n   else n     cout <<  No n ; n } n


#include <bits/stdc++.h> n using namespace std; n long long int power(long long int x, long long int y) { n   long long int res = 1; n   while (y) { n     if (y % 2) { n       res = (res * x) % 1000000007; n     } n     y = y >> 1; n     x = (x * x) % 1000000007; n   } n   return res; n } n void solve() { n   long long int n; n   cin >> n; n   long long int ans = 1; n   for (long long int i = 0; i < n; i++) { n     ans *= 2; n   } n   ans -= 2; n   ans = power(4, ans); n   ans *= 6; n   ans = ans % 1000000007; n   cout << ans <<   n ; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   int val, num; n } q[1000005]; n char str[1000005]; n int len; n void update() { n   int len1 = len; n   len = 0; n   for (int i = 1; i <= len1; i++) n     if (q[i].num > 0) { n       if (q[i].val == q[len].val) n         q[len].num += q[i].num; n       else n         q[++len] = q[i]; n     } n } n void work() { n   int ans = 0; n   while (len > 1) { n     ans++; n     for (int i = 2; i < len; i++) q[i].num -= 2; n     q[1].num--; n     q[len].num--; n     update(); n   } n   printf( %d n , ans); n } n int main(void) { n   scanf( %s , str + 1); n   len = strlen(str + 1); n   q[0].val = -1; n   q[0].num = 0; n   for (int i = 1; i <= len; i++) q[i].val = str[i] -  a , q[i].num = 1; n   update(); n   work(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   while (scanf( %d%d , &n, &m) == 2) { n     map<int, vector<int>> s; n     for (int i = 0; i < n; ++i) { n       int x; n       scanf( %d , &x); n       s[x].push_back(i); n     } n     int flag = 1; n     vector<int> ans(m); n     for (int i = 0; i < m; ++i) { n       int x; n       scanf( %d , &x); n       if (s.find(x) == s.end()) { n         flag = -1; n         continue; n       } n       if (flag != 1) continue; n       auto& t = s[x]; n       if (t.size() == 1) n         ans[i] = t[0] + 1; n       else n         flag = 0; n     } n     if (flag == -1) n       puts( Impossible ); n     else if (flag == 0) n       puts( Ambiguity ); n     else { n       puts( Possible ); n       for (int i = 0; i < m; ++i) { n         printf( %d%c , ans[i],    n [i == m - 1]); n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n int n, m; n vector<pair<long long, int> > v; n vector<int> u[N]; n int vis[N]; n void dfs(int s) { n   if (!vis[s]) { n     vis[s] = true; n     for (int i = 0; i < u[s].size(); i++) { n       dfs(u[s][i]); n     } n   } n } n int main() { n   ios_base ::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cin >> n >> m; n   long long cnt = 0; n   memset(vis, 0, sizeof(vis)); n   long long x, y; n   for (int i = 0; i < n; i++) { n     cin >> x; n     v.push_back(make_pair(x, i + 1)); n   } n   for (int i = 0; i < m; i++) { n     cin >> x >> y; n     u[x].push_back(y); n     u[y].push_back(x); n   } n   sort(v.begin(), v.end()); n   for (int i = 0; i < n; i++) { n     if (!vis[v[i].second]) { n       dfs(v[i].second); n       cnt += v[i].first; n     } n   } n   cout << cnt; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n T max(T a, T b, T c) { n   return max(a, max(b, c)); n } n template <class T> n T min(T a, T b, T c) { n   return min(a, min(b, c)); n } n const double PI = 2.0 * acos(0.0); n const int mx[] = {0, 1, 0, -1}; n const int my[] = {-1, 0, 1, 0}; n int n; n int P[1000050] = {0}; n int T[1000050] = {0}; n long long F[1000050] = {0}; n long long C[1000050] = {0}; n long long ans; n map<int, int> S; n map<int, int> R; n void update(long long B[], int x, int value) { n   if (x == 0) return; n   for (int s = x; s <= n; s += (s & -s)) B[s] += value; n } n long long query(long long B[], int x) { n   long long sum = 0; n   for (int s = x; s > 0; s -= (s & -s)) sum += B[s]; n   return sum; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , P + i), S[P[i]]++; n   for (map<int, int>::iterator it = S.begin(); it != S.end(); it++) n     T[it->second]++; n   for (int i = 1; i <= n; i++) update(F, i, T[i] * (i - 1)), update(C, i, T[i]); n   ans = 0; n   for (int i = n; i >= 1; i--) { n     int target = S[P[i]]--, frequency = ++R[P[i]]; n     update(F, target, -(target - 1)); n     update(F, target - 1, target - 2); n     update(C, target, -1); n     update(C, target - 1, 1); n     ans += query(F, n) - query(F, frequency) - n            (query(C, n) - query(C, frequency)) * (frequency - 1); n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n int64_t f(int64_t a, int64_t b, int64_t c, int64_t l) { n   int64_t total = 0; n   for (int64_t la = 0ll; la <= l; ++la) { n     int64_t x = std::min(a - b - c + la, l - la); n     if (x >= 0) total += (x + 1) * (x + 2) / 2ll; n   } n   return total; n } n int main() { n   int64_t a, b, c, l; n   std::cin >> a >> b >> c >> l; n   int64_t total = (l + 1) * (l + 2) * (l + 3) / 6ll; n   total -= f(a, b, c, l); n   total -= f(b, a, c, l); n   total -= f(c, a, b, l); n   std::cout << total <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n const int MAXN = 1001; n using namespace std; n char s[MAXN + 1], heidi[MAXN + 1]; n int main() { n   int n, i, poz; n   scanf( %s , &s); n   n = strlen(s); n   i = 0; n   poz = 0; n   heidi[0] =  h ; n   heidi[1] =  e ; n   heidi[2] =  i ; n   heidi[3] =  d ; n   heidi[4] =  i ; n   while (i < n && poz < 5) { n     if (s[i] == heidi[poz]) poz++; n     i++; n   } n   if (poz == 5) n     printf( YES ); n   else n     printf( NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int cases; n   cin >> cases; n   for (int i = 0; i < cases; i++) { n     int a, b; n     cin >> a >> b; n     if (a > b) swap(a, b); n     int dis = b - a; n     if (dis == 0) n       cout << 0 <<   n ; n     else if (dis == 1) n       cout << 1 <<   n ; n     else { n       long long int ans = 1; n       while (true) { n         long long int sum = (ans * (ans + 1)) / 2; n         if (sum >= dis) { n           if (sum % 2 == dis % 2) break; n         } n         ans++; n       } n       cout << ans <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { n   if (!b) return a; n   return gcd(b, a % b); n } n int main() { n   int k; n   cin >> k; n   long long a; n   while (k--) { n     cin >> a; n     long long g = gcd(a + 1, a * 4); n     long long l = (a + 1) * a * 4 / g; n     cout << l / (a + 1) + 1 <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(int argc, char const *argv[]) { n   int n, m, xa = 55, xb = -1, ya = 55, yb = -1; n   char g[50][50]; n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       cin >> g[i][j]; n       if (g[i][j] ==  * ) { n         xa = min(i, xa); n         ya = min(j, ya); n         xb = max(i, xb); n         yb = max(j, yb); n       } n     } n   } n   for (int i = xa; i <= xb; i++) { n     for (int j = ya; j <= yb; j++) { n       cout << g[i][j]; n     } n     puts(  ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int i, j; n   int k, n, s, p; n   cin >> k >> n >> s >> p; n   cout << int(ceil((ceil(n / (s * 1.0)) * k) / (p * 1.0))); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, ans = 0; n   long long sum_a = 0, sum_b = 0; n   cin >> n; n   vector<int> a(n); n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n     sum_a += a[i]; n   } n   cin >> m; n   vector<int> b(m); n   for (int i = 0; i < m; ++i) { n     cin >> b[i]; n     sum_b += b[i]; n   } n   int i = 0, j = 0; n   if (sum_a != sum_b) { n     cout << -1; n     return 0; n   } else { n     sum_a = a[0]; n     sum_b = b[0]; n     while (j < m && i < n) { n       if (sum_a > sum_b) { n         j++; n         sum_b += b[j]; n       } else if (sum_a < sum_b) { n         i++; n         sum_a += a[i]; n       } else { n         ans++; n         i++; n         j++; n         sum_a = a[i]; n         sum_b = b[j]; n       } n     } n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int ar[10000]; n int main() { n   int n, m, i, j, x = 0; n   cin >> n >> m; n   char a[n][2 * m]; n   for (i = 0; i < n; i++) { n     for (j = 0; j < 2 * m; j++) cin >> a[i][j]; n   } n   for (i = 0; i < n; i++) { n     for (j = 0; j < 2 * m; j = j + 2) { n       if (a[i][j] ==  1  || a[i][j + 1] ==  1 ) x++; n     } n   } n   cout << x << endl; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() {} n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   string s, q; n   cin >> s >> q; n   int index = 1; n   for (int i = 0; i < q.size(); i++) { n     if (s[index - 1] == q[i]) index++; n   } n   cout << index <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e6 + 5; n long long a[maxn], b[maxn]; n int n; n long long ans; n inline long long read() { n   long long ans = 0; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) ch = getchar(); n   while (ch >=  0  && ch <=  9 ) { n     ans = ans * 10 + ch -  0 ; n     ch = getchar(); n   } n   return ans; n } n long long solve(long long mod) { n   long long sol = 0; n   int i; n   for (i = 0; i < n; i++) b[i] = a[i] % mod; n   for (i = 0; i < n; i++) { n     sol += min(b[i], mod - b[i]); n     if (sol > ans) break; n     b[i + 1] = (b[i + 1] + b[i]) % mod; n   } n   return sol; n } n int main() { n   int i; n   long long tot; n   n = read(); n   tot = 0; n   for (i = 0; i < n; i++) tot += (a[i] = read()); n   if (tot == 1) { n     puts( -1 ); n     return 0; n   } n   ans = 1e18; n   for (i = 2; 1LL * i * i <= tot; i++) n     if (tot % i == 0) { n       ans = min(ans, solve(i)); n       while (tot % i == 0) tot /= i; n     } n   if (tot > 1) ans = min(ans, solve(tot)); n   printf( %lld n , ans); n   return 0; n } n