#include <bits/stdc++.h> n using namespace std; n int n, m, t1, t2; n vector<int> q1, q2; n vector<vector<int> > Visted; n int des(int point1, int point2, int point3, int point4) { n   return abs(point1 - point3) + abs(point2 - point4); n } n int main() { n   cin >> n >> m >> t1; n   Visted.resize(n + 10, vector<int>(m + 10)); n   q1.resize(t1); n   for (int i = 0; i < t1; i++) cin >> q1[i]; n   cin >> t2; n   q2.resize(t2); n   for (int i = 0; i < t2; i++) cin >> q2[i]; n   sort(q1.begin(), q1.end()); n   sort(q2.begin(), q2.end()); n   for (int i = 0; i < t1; i++) { n     bool take = false; n     int desEnd = -1e9, X, Y; n     for (int j = 1; j <= n; j++) { n       for (int z = 1; z <= m; z++) { n         if (!Visted[j][z] && des(0, 0, j, z) <= q1[i] && n             des(0, m + 1, j, z) > desEnd) { n           desEnd = des(0, m + 1, j, z); n           take = true; n           X = j, Y = z; n         } n       } n     } n     if (take) { n       Visted[X][Y] = 1; n     } else { n       cout <<  NO ; n       return 0; n     } n   } n   for (int i = 0; i < t2; i++) { n     bool take = false; n     int desEnd = 1e9, X, Y; n     for (int j = 1; j <= n; j++) { n       for (int z = 1; z <= m; z++) { n         if (!Visted[j][z] && des(0, m + 1, j, z) <= q2[i] && n             des(0, m + 1, j, z) < desEnd) { n           take = true; n           X = j, Y = z; n         } n       } n     } n     if (take) { n       Visted[X][Y] = 1; n     } else { n       cout <<  NO ; n       return 0; n     } n   } n   cout <<  YES ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class TH> n void _dbg(const char *sdbg, TH h) { n   cerr << sdbg <<  =  << h << endl; n } n template <class TH, class... TA> n void _dbg(const char *sdbg, TH h, TA... a) { n   while (*sdbg !=  , ) cerr << *sdbg++; n   cerr <<  =  << h <<  , ; n   _dbg(sdbg + 1, a...); n } n template <class T> n ostream &operator<<(ostream &os, vector<T> V) { n   os <<  [ ; n   for (auto vv : V) os << vv <<  , ; n   return os <<  ] ; n } n template <class L, class R> n ostream &operator<<(ostream &os, pair<L, R> P) { n   return os <<  (  << P.first <<  ,  << P.second <<  ) ; n } n template <class T> n using min_heap = priority_queue<T, vector<T>, greater<T>>; n template <class T> n using max_heap = priority_queue<T>; n const int mod = 1e9 + 7; n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n struct PairHash { n   template <typename T1, typename T2> n   std::size_t operator()(const pair<T1, T2> &p) const { n     return hash<T1>()(p.first) ^ hash<T2>()(p.second); n   } n }; n void solve(int ncase) { n   int a, b, c, d; n   cin >> a >> b >> c >> d; n   int minx = min({a, b, c}); n   a -= minx; n   b -= minx; n   c -= minx; n   cout << (((a % 2 + b % 2 + c % 2 + (d + 3 * 1LL * minx) % 2) <= 1) || n                    (minx >= 1 && ((a + 1) % 2 + (b + 1) % 2 + (c + 1) % 2 + n                                   (d + 3 * 1LL * minx - 3) % 2) <= 1) n                ?  Yes  n                :  No ) n        << endl; n } n void solve_all_cases() { n   int T = 1; n   cin >> T; n   int ncase = 0; n   while (T--) { n     solve(++ncase); n   } n } n int main() { n   cout << std::fixed; n   cout << setprecision(6); n   solve_all_cases(); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   scanf( %d , &n); n   int a[n]; n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 0; i < n - 1; i++) cout << (a[i] + a[i + 1]) <<    ; n   cout << a[n - 1]; n } n


#include <bits/stdc++.h> n using namespace std; n int j, i, a, b, c, k, d, e, f[1005], n, mi, ma; n int main() { n   cin >> n; n   for (i = 0; i < n; i++) { n     cin >> f[i]; n     ma = max(f[i], ma); n   } n   for (i = 0; i < n - 1; i++) n     for (j = i + 1; j < n; j++) { n       c = 0; n       for (k = i; k <= j; k++) c = f[k] ^ c; n       ma = max(ma, c); n     } n   cout << ma; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC target(   n      avx,avx2,fma,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) n using namespace std; n long long MOD = 1e9 + 7; n long long k; n vector<long long> v, w, num; n vector<pair<long long, pair<long long, long long> > > zp; n vector<vector<long long> > dp; n long long st(long long t, long long id, long long mod) { n   long long res = 1, a = t, n = id; n   while (n) { n     if (n & 1) res = (res * a) % mod; n     a = (a * a) % mod; n     n >>= 1; n   } n   return res % mod; n } n void add(long long id) { n   vector<long long> mem(k + 1); n   dp.push_back(mem); n   long long lst = dp.size() - 1; n   for (int i = 0; i < k + 1; i++) { n     dp[lst][i] = dp[lst - 1][i]; n     if (i >= w[id]) n       dp[lst][i] = max(dp[lst - 1][i], dp[lst - 1][i - w[id]] + v[id]); n   } n } n void del() { dp.pop_back(); } n long long ans() { n   long long lst = dp.size() - 1; n   long long anss = 0; n   for (int i = 1; i < k + 1; i++) { n     anss = ((10000019 * (long long)anss) + (long long)dp[lst][k - i + 1]) % MOD; n     if (anss > MOD) anss -= MOD; n   } n   return anss; n } n void get(long long l, long long r) { n   if (r == l + 1) { n     if (zp[l].first == 3) { n       zp[l].second.first = ans(); n     } n     return; n   } n   long long mid = (l + r) / 2; n   long long kol = 0; n   for (int i = mid; i < r; i++) { n     if (zp[i].first == 2) { n       if (num[zp[i].second.second] < l) { n         add(zp[i].second.second); n         kol++; n       } n     } n   } n   get(l, mid); n   while (kol--) del(); n   kol = 0; n   for (int i = l; i < mid; i++) { n     if (zp[i].first == 1) { n       if (zp[i].second.second >= r) { n         add(zp[i].second.first); n         kol++; n       } n     } n   } n   get(mid, r); n   while (kol--) del(); n } n int main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0), n       cout << fixed << setprecision(20); n   long long n; n   cin >> n >> k; n   v.resize(n), w.resize(n), num.resize(n); n   vector<long long> mem(k + 1); n   dp.push_back(mem); n   for (int i = 0; i < n; i++) { n     cin >> v[i] >> w[i]; n     zp.push_back({1, {i, MOD}}); n     num[i] = i; n   } n   long long q; n   cin >> q; n   while (q--) { n     long long sz = zp.size(); n     zp.push_back({0, {num.size(), MOD}}); n     cin >> zp[zp.size() - 1].first; n     if (zp.back().first == 1) { n       long long vv, ww; n       cin >> vv >> ww; n       v.push_back(vv); n       w.push_back(ww); n       num.push_back(sz); n     } n     if (zp.back().first == 2) { n       long long x; n       cin >> x; n       x--; n       zp[num[x]].second.second = sz; n       zp[sz].second.second = x; n     } n   } n   get(0, zp.size()); n   for (auto i : zp) { n     if (i.first == 3) { n       cout << i.second.first << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long read() { n   long long w = 0; n   char c = getchar(); n   while (c <  0  || c >  9 ) c = getchar(); n   while (c >=  0  && c <=  9 ) w = w * 10 + c - 48, c = getchar(); n   return w; n } n long long n, s[100005], e[100005]; n bool f[100005][2]; n bool check(long long x, long long y, long long op) { n   if ((y & 1) && (!op)) return (y - x) & 1; n   long long h = y >> 1; n   if (x > h) return op ? 1 : (y - x) & 1; n   return check(x, h, op ^ 1); n } n signed main() { n   n = read(); n   for (long long i = 1; i <= n; i++) n     s[i] = read(), e[i] = read(), f[i][0] = check(s[i], e[i], 0), n     f[i][1] = check(s[i], e[i], 1); n   bool need = f[n][0]; n   for (long long i = n - 1; i > 0; i--) need = f[i][need]; n   printf(need ?  1   :  0  ); n   need = f[n][1]; n   for (long long i = n - 1; i > 0; i--) need = f[i][need]; n   printf(need ?  1  :  0 ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 16, Inf = 1e9, lastS = (1 << N) - 1; n static int n, sqrn, R, B, ans, lasts; n struct TCard { n   bool c; n   int r, b; n } card[N]; n static int f[lastS][N * N + 1][2], cardr[lastS], cardb[lastS]; n inline int GetF(int s, int rem, bool c) { n   rem = min(rem, sqrn); n   if (s == lasts) return 0; n   auto &fs = f[s][rem][c]; n   if (fs != -1) return fs; n   fs = Inf; n   for (int i = 0; i < n; ++i) { n     if (s & 1 << i) continue; n     int R = max(0, card[i].r - cardr[s]), B = max(0, card[i].b - cardb[s]), n         turn, _r, _b; n     if (c) { n       turn = max(R - rem, B); n       _r = rem + turn - R; n       _b = turn - B; n     } else { n       turn = max(B - rem, R); n       _b = rem + turn - B; n       _r = turn - R; n     } n     if (_r) n       fs = min(fs, turn + GetF(s ^ 1 << i, _r, 1)); n     else n       fs = min(fs, turn + GetF(s ^ 1 << i, _b, 0)); n   } n   return fs; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n; n   sqrn = n * n; n   lasts = (1 << n) - 1; n   for_each(card, card + n, [](TCard &C) { n     char tmp; n     cin >> tmp >> C.r >> C.b; n     C.c = tmp ==  R ; n     if (C.r > n) R += C.r - n, C.r = n; n     if (C.b > n) B += C.b - n, C.b = n; n   }); n   for (int s = 1; s <= lasts; ++s) { n     int i = __builtin_ctz(s); n     cardr[s] = cardr[s ^ 1 << i] + card[i].c; n     cardb[s] = cardb[s ^ 1 << i] + !card[i].c; n   } n   memset(f, -1, sizeof f); n   if (R > B) n     ans = R + GetF(0, R - B, 0); n   else n     ans = B + GetF(0, B - R, 1); n   cout << ans + n; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n ifstream file( input.txt ); n using namespace std; n void maker(int x); n int main() { n   int y; n   cin >> y; n   maker(y + 1); n } n void maker(int x) { n   int a, b, c, d; n   a = (x / 1000) % 10; n   b = (x / 100) % 10; n   c = (x / 10) % 10; n   d = (x / 1) % 10; n   while (a == b || a == c || a == d || b == c || b == d || c == d) { n     x++; n     a = (x / 1000) % 10; n     b = (x / 100) % 10; n     c = (x / 10) % 10; n     d = (x / 1) % 10; n   } n   cout << (a * 1000 + b * 100 + c * 10 + d); n } n


#include <bits/stdc++.h> n using namespace std; n template <typename... Args> n void logger(string vars, Args&&... values) {} n template <typename T> n inline void maxa(T& a, T b) { n   a = max(a, b); n } n template <typename T> n inline void mina(T& a, T b) { n   a = min(a, b); n } n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n const int mxN = 2e5 + 5; n const int mod = 1e9 + 7; n int main() { n   cin.sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   vector<int> a(n); n   vector<char> ans(n); n   for (int& i : a) cin >> i; n   string s; n   cin >> s; n   sort(a.begin(), a.end()); n   int cnt = 0; n   for (int i = 1; i < n; i++) cnt += (s[i] != s[i - 1]); n   int l = n - cnt - 1; n   int r = n - cnt - 1; n   cout << a[l] <<     << s[0] <<   n ; n   ans[l] = s[0]; n   for (int i = 1; i < (int)(s.size()); i++) { n     if (s[i] != s[i - 1]) { n       ans[++r] = ans[r] ==  L  ?  R  :  L ; n       cout << a[r] <<     << ans[r] <<   n ; n     } else { n       ans[--l] = ans[l] ==  L  ?  R  :  L ; n       cout << a[l] <<     << ans[l] <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2000 + 100; n map<pair<long long, long long>, int> rat; n int n; n pair<long long, long long> px[maxn], py[maxn]; n long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } n int main() { n   cin >> n; n   for (int i = 1, a, b, c; i <= n; i++) { n     cin >> a >> b >> c; n     px[i] = make_pair(c * b, a * a + b * b); n     py[i] = make_pair(a * c, a * a + b * b); n   } n   long long ans = 0; n   for (int i = 1; i <= n; i++) { n     rat.clear(); n     long long two0 = 0; n     for (int j = i + 1; j <= n; j++) { n       long long a = px[j].first * px[i].second - px[j].second * px[i].first; n       long long b = py[j].first * py[i].second - py[j].second * py[i].first; n       long long g = gcd(abs(a), abs(b)); n       if (g != 0) a /= g, b /= g; n       if (a == 0 && b == 0) { n         two0++; n         ans += j - i - 1; n       } else { n         if (a == 0) b = 1; n         if (b == 0) a = 1; n         if (a < 0 && b < 0) a *= -1, b *= -1; n         if (a > 0 && b < 0) a *= -1, b *= -1; n         ans += two0; n         if (rat.count(make_pair(a, b))) { n           ans += rat[make_pair(a, b)]; n           rat[make_pair(a, b)]++; n         } else n           rat[make_pair(a, b)] = 1; n       } n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 25; n int dp[MAXN][MAXN][MAXN]; n int n, t; n int main() { n   scanf( %d%d , &n, &t); n   t = 2 * t; n   dp[1][0][1] = dp[1][0][2] = dp[1][0][3] = 1; n   for (int i = 1; i < n; ++i) n     for (int j = 0; j <= t; ++j) n       for (int k = 1; k <= 4; ++k) n         for (int l = 1; l <= 4; ++l) { n           if ((i == 1 && l > k) || (i != 1 && l != k)) { n             int nj = j; n             if (j % 2 && l < k) n               ++nj; n             else if (j % 2 == 0 && l > k) n               ++nj; n             dp[i + 1][nj][l] += dp[i][j][k]; n           } n         } n   int ans = 0; n   for (int i = 1; i <= 4; ++i) ans += dp[n][t][i]; n   printf( %d n , ans); 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   int t; n   cin >> t; n   multiset<long long int> s; n   map<string, int> mp; n   while (t--) { n     char ch; n     long long int x; n     cin >> ch >> x; n     string str(18,  0 ); n     string temp = to_string(x); n     int j = 17; n     for (int i = ((int)(temp).size()) - 1; i >= 0; i--) { n       if ((temp[i] -  0 ) % 2 != 0) { n         str[j] =  1 ; n       } n       j--; n     } n     if (ch ==  + ) { n       mp[str]++; n     } else if (ch ==  - ) { n       mp[str]--; n     } else { n       cout << mp[str] <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n #pragma warning(disable : 4996) n #pragma comment(linker,  /STACK:666000000 ) n using namespace std; n const int INF = (1 << 30) - 1; n const long double EPS = 1e-9; n const long double PI = fabs(atan2(0.0, -1.0)); n int n; n void load() { cin >> n; } n const int SZ = 1010; n int sv[SZ]; n int rec(const int v) { n   if (v < SZ && -1 != sv[v]) return sv[v]; n   bool can[10]; n   int p9; n   memset(can, false, sizeof(can)); n   { n     int p = 0; n     int tmp = v; n     while (tmp != 0) { n       can[tmp % 10] = true; n       if (9 == tmp % 10) p9 = p; n       tmp /= 10; n       p *= 10; n     } n   } n   if (v >= SZ) { n     if (can[9] && p9 > 10) { n       int cnt = 9 * (p9 / 10) - 8; n       return cnt + rec(v - cnt * 9); n     } n     for (int i = 9; i >= 1; i--) n       if (can[i]) return 1 + rec(v - i); n   } n   int res = INT_MAX; n   for (int i = 1; i <= 9; i++) n     if (can[i]) res = min(res, 1 + rec(v - i)); n   if (v < SZ) sv[v] = res; n   return res; n } n void solve() { n   memset(sv, -1, sizeof(sv)); n   sv[0] = 0; n   int res = rec(n); n   cout << res; n } n int main() { n   load(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5; n map<int, int> dic; n int n, a[N + 10], ma[N + 10], ma2[N + 10]; n int main() { n   ios::sync_with_stdio(0), cin.tie(0); n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   ma[1] = a[1]; n   ma2[1] = -1; n   for (int i = 2; i <= n; i++) { n     if (a[i] > ma[i - 1]) { n       ma2[i] = ma[i - 1]; n       ma[i] = a[i]; n     } else { n       ma[i] = ma[i - 1]; n       if (ma2[i - 1] < a[i]) { n         ma2[i] = a[i]; n       } else n         ma2[i] = ma2[i - 1]; n     } n     if (a[i] != ma[i] && a[i] == ma2[i]) { n       dic[ma[i]]++; n     } n   } n   int tot = -1e6, idx = -1; n   for (int i = 1; i <= n; i++) { n     int temp = dic[a[i]]; n     if (a[i] == ma[i]) temp--; n     if (temp > tot) { n       tot = temp; n       idx = a[i]; n     } else if (temp == tot) { n       idx = min(idx, a[i]); n     } n   } n   cout << idx << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[1000005] = {0}; n long long b[1000005] = {0}; n long long c[1000005] = {0}; n long long d[1000005] = {0}; n long long s; n long long i, j = 0, l; n long long count1 = 0; n long long count2 = 0; n long long count3 = 0; n long long count4 = 0; n long long ans = 0; n bool cmp(long long aa, long long bb) { return aa > bb; } n int main() { n   long long n; n   scanf( %lld n , &n); n   for (i = 0; i < n; i++) { n     cin >> s >> j; n     if (s == 11) { n       a[count1++] = j; n       ans += j; n     } else if (s == 10) { n       b[count2++] = j; n     } else if (s == 1) { n       c[count3++] = j; n     } else { n       d[count4++] = j; n     } n   } n   sort(b, b + count2, cmp); n   sort(c, c + count3, cmp); n   for (i = 0; i < min(count2, count3); i++) ans += (b[i] + c[i]); n   for (i = 0; i < abs(count2 - count3); i++) { n     if (count3 > count2) n       d[count4 + i] = c[min(count2, count3) + i]; n     else if (count3 < count2) n       d[count4 + i] = b[min(count2, count3) + i]; n   } n   count4 += abs(count2 - count3); n   sort(d, d + count4, cmp); n   for (i = 0; i < count1 && i < count4; i++) ans += d[i]; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[35], b[35], diff[35]; n int main() { n   std::ios::sync_with_stdio(false); n   int t; n   t = 1; n   while (t--) { n     long long x, x1, val, val1, i, j, res = 0; n     cout <<  ? 0 0  << endl; n     cin >> x; n     x1 = x; n     if (x == 0) { n       for (i = 0; i < 30; i++) { n         val = (1 << i); n         cout <<  ?   << val <<     << 0 << endl; n         cin >> x; n         if (x == -1) { n           res += (1 << i); n         } n       } n       cout <<  !   << res <<     << res << endl; n     } else { n       { n         x1 = x; n         i = 29; n         val = 0; n         val1 = 0; n         for (i = 29; i >= 0; i--) { n           val += (1 << i); n           val1 += (1 << i); n           cout <<  ?   << val <<     << val1 << endl; n           cin >> x; n           if (x != x1) { n             diff[i] = 1; n             val -= (1 << i); n             if (x == -1) { n               a[i] = 1; n               b[i] = 0; n             } else { n               a[i] = 0; n               b[i] = 1; n             } n             cout <<  ?   << val <<     << val1 << endl; n             cin >> x; n             x1 = x; n           } else { n             diff[i] = 0; n           } n         } n         for (i = 29; i >= 0; i--) { n           val = 0; n           val1 = 0; n           if (diff[i]) { n             continue; n           } n           for (j = 0; j < 30; j++) { n             if (j == i) { n               continue; n             } n             if (diff[j]) { n               val += (1 << j); n             } n           } n           { n             val += (1 << i); n             cout <<  ?   << val <<     << val1 << endl; n             cin >> x; n             if (x == -1) { n               a[i] = 1; n               b[i] = 1; n             } else { n               a[i] = 0; n               b[i] = 0; n             } n           } n         } n         val = 0; n         val1 = 0; n         for (i = 0; i < 30; i++) { n           if (a[i]) { n             val += (1 << i); n           } n           if (b[i]) { n             val1 += (1 << i); n           } n         } n         cout <<  !   << val <<     << val1 << endl; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long int mod = 1e9 + 7; n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long int n, k; n   cin >> n >> k; n   long long int ans = 1; n   while (k % 2 == 0) { n     k = k / 2; n     ans++; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e4 + 10, mod = 1e9 + 7, M = 510; n const double PI = 3.1415926535; n long long res[40], p[40], ei[40], sum[N], cnt; n double pre[N]; n int path[100]; n void dfs(int u, int s, long long va) { n   if (u == -1) { n     sum[s] = ((long long)sum[s] + va) % mod; n     return; n   } n   for (int i = 0; i <= cnt; i++) { n     path[i]++; n     if (res[i] - path[i] + 1) n       dfs(u - 1, s + i, (long long)va * (res[i] - path[i] + 1) % mod); n     path[i]--; n   } n } n int main() { n   int n; n   scanf( %d , &n); n   vector<int> temp; n   int t = n; n   while (t) t /= 10, cnt++; n   p[1] = 1; n   for (int i = 2; i <= 11; i++) p[i] = p[i - 1] * 10; n   ei[0] = 1; n   for (int i = 1; i < 11; i++) ei[i] = ei[i - 1] * 8; n   for (int i = 0; i < (1 << cnt); i++) { n     vector<int> temp; n     for (int j = 0; j < cnt; j++) n       if (i >> j & 1) temp.push_back(j); n     int len = temp.size(); n     for (int j = 0; j < 1 << len; j++) { n       int a[15] = {0}; n       for (int k = 0; k < len; k++) n         if (j >> k & 1) n           a[temp[k]] = 7; n         else n           a[temp[k]] = 4; n       long long f = p[cnt], va = 0; n       for (long long k = cnt - 1, r = 1; k >= 0; k--, r *= 10) n         va += (long long)a[k] * r; n       if (va > n) continue; n       for (int k = 0; k < cnt; k++) { n         if (a[k]) { n           f /= 10; n           continue; n         } n         for (int r = 9; r >= 0; r--) n           if ((long long)r * f + va <= n) { n             a[k] = r, va = r * f + va; n             break; n           } n         f /= 10; n       } n       long long num = 0, ans = 0; n       for (int k = 0; k < cnt; k++) { n         if (num < temp.size() && temp[num] == k) { n           num++; n           if (k == cnt - 1) ans++; n           continue; n         } n         if (a[k] == 7 || a[k] == 4) { n           int sum = cnt - 1 - k; n           int ts = temp.size() - num; n           int tnum = a[k] + 1; n           if (a[k] == 4) n             tnum--; n           else n             tnum -= 2; n           ans = ans + ei[sum - ts] * (long long)tnum; n           break; n         } else { n           int sum = cnt - 1 - k; n           int ts = temp.size() - num; n           int tnum = a[k] + 1; n           if (a[k] > 7) n             tnum -= 3; n           else if (a[k] > 4) n             tnum -= 2; n           else n             tnum--; n           if (k == cnt - 1) tnum++; n           ans = ans + ei[sum - ts] * (long long)tnum; n         } n       } n       res[len] = (res[len] + (long long)ans); n     } n   } n   res[0]--; n   dfs(5, 0, 1); n   long long ans = 0, pre = 0; n   for (int i = 1; i <= cnt; i++) { n     pre = pre + sum[i - 1]; n     ans = (ans + pre * res[i]) % mod; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N, good; n bool prime[100010]; n vector<int> fact, num, lis; n int main() { n   memset(prime, true, sizeof(prime)); n   for (int i = 2; i * i <= 100000; i++) n     if (prime[i]) n       for (int j = 2; i * j <= 100000; j++) prime[i * j] = false; n   for (int i = 2; i <= 100000; i++) n     if (prime[i]) { n       num.push_back(i); n       lis.push_back(0); n     } n   scanf( %d , &N); n   while (N--) { n     scanf( %d , &good); n     int temp = good; n     fact.clear(); n     int longest = 0; n     for (int i = 0; i < num.size() && num[i] * num[i] <= good; i++) n       if (good % num[i] == 0) { n         fact.push_back(i); n         longest = max(longest, lis[i] + 1); n         while (temp % num[i] == 0) temp /= num[i]; n       } n     if (temp > 1) { n       int i = lower_bound(num.begin(), num.end(), temp) - num.begin(); n       fact.push_back(i); n       longest = max(longest, lis[i] + 1); n     } n     for (int i = 0; i < fact.size(); i++) n       lis[fact[i]] = max(lis[fact[i]], longest); n   } n   int ans = 1; n   for (int i = 0; i < num.size(); i++) ans = max(ans, lis[i]); n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2300; n int dp[N], a[N], res = 0, n; n void dfs(int u, int d) { n   if (d == n) { n     dp[u] = 0; n     return; n   } n   int l = u * 2, r = l + 1; n   dfs(l, d + 1); n   dfs(r, d + 1); n   int x = dp[l] + a[l], y = dp[r] + a[r]; n   if (x < y) swap(x, y); n   dp[u] = x; n   res += x - y; n } n int main() { n   cin >> n; n   for (int i = 2; i < (1 << n + 1); i++) { n     scanf( %d , &a[i]); n   } n   dfs(1, 0); n   cout << res << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, root, val, rem; n int main() { n   cin >> n; n   root = floor((sqrt(1.0 + 8.0 * n) - 1.0) / 2.0); n   val = root * (root + 1) / 2; n   if (n == val) --root; n   val = root * (root + 1) / 2; n   rem = n - val; n   cout << rem << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1e5 + 2; n const long long M = 1e6 + 2; n const long long mod = 998244353; n long long ar[N], fact[M], inv[M], cnt[N]; n long long binpow(long long x, long long y) { n   long long tich = 1; n   while (y) { n     if (y & 1) { n       tich *= x; n       tich %= mod; n     } n     x *= x; n     x %= mod; n     y >>= 1; n   } n   return tich; n } n long long C(long long x, long long y) { n   return ((fact[y] * inv[y - x]) % mod * inv[x]) % mod; n } n signed main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   long long n, i, j, k, l, ans = 0, sum = 0; n   cin >> n; n   for (i = 1; i <= n; i++) { n     cin >> ar[i]; n     cnt[ar[i] % n]++; n     sum += ar[i]; n   } n   fact[0] = 1; n   for (i = 1; i < M - 1; i++) { n     fact[i] = (fact[i - 1] * i) % mod; n   } n   inv[M - 2] = binpow(fact[M - 2], mod - 2); n   for (i = M - 2; i > 0; i--) { n     inv[i - 1] = (inv[i] * i) % mod; n   } n   sort(ar + 1, ar + 1 + n); n   long long pos = 0; n   for (i = 1; i < n; i++) { n     if (ar[i + 1] < i) { n       pos = i; n       break; n     } n   } n   if (pos) { n     j = 1; n     for (i = 0; i < pos; i++) { n       while (ar[j] < i) { n         j++; n       } n       ans += C(n - 1, i - (j - 1) + n - 1); n       if (ans >= mod) { n         ans -= mod; n       } n     } n     cout << ans; n   } else { n     for (i = 1; i <= n; i++) { n       sum -= ar[i] % n; n     } n     sum /= n; n     for (i = 1; i <= n; i++) { n       sum = sum + cnt[n - i] - 1; n       if (sum >= 0) { n         ans += C(n - 1, sum + n - 1); n         if (ans >= mod) { n           ans -= mod; n         } n       } n     } n     cout << ans; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   vector<pair<int, int>> v; n   for (int i = 0; i < m; i++) { n     int a, b; n     cin >> a >> b; n     v.push_back(make_pair(a, b)); n   } n   int x1 = v[0].first; n   int y1 = v[0].second; n   vector<int> p1, p2; n   for (int i = 1; i < m; i++) { n     int a = v[i].first; n     int b = v[i].second; n     if (x1 != a && x1 != b) { n       p1.push_back(i); n     } n     if (y1 != a && y1 != b) { n       p2.push_back(i); n     } n   } n   if (p1.size() == 0 || p2.size() == 0) { n     cout <<  YES  << endl; n     return 0; n   } n   x1 = v[p1[0]].first; n   y1 = v[p1[0]].second; n   int c1 = 0, c2 = 0, c3 = 0, c4 = 0; n   for (int i = 1; i < p1.size(); i++) { n     int a = v[p1[i]].first; n     int b = v[p1[i]].second; n     if (x1 != a && x1 != b) { n       c1++; n     } n     if (y1 != a && y1 != b) { n       c2++; n     } n   } n   x1 = v[p2[0]].first; n   y1 = v[p2[0]].second; n   for (int i = 1; i < p2.size(); i++) { n     int a = v[p2[i]].first; n     int b = v[p2[i]].second; n     if (x1 != a && x1 != b) { n       c3++; n     } n     if (y1 != a && y1 != b) { n       c4++; n     } n   } n   if (c1 == 0 || c2 == 0) { n     cout <<  YES  << endl; n   } else if (c3 == 0 || c4 == 0) { n     cout <<  YES  << endl; n   } else { n     cout <<  NO  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline bool chmax(T &a, T b) { n   return a < b ? (a = b, 1) : 0; n } n template <class T> n inline bool chmin(T &a, T b) { n   return a > b ? (a = b, 1) : 0; n } n int main() { n   int n, m; n   cin >> n >> m; n   map<int, vector<int>> obs; n   int left = n; n   while (m--) { n     int x, y; n     cin >> x >> y; n     if (x == 1) n       chmin(left, y - 1); n     else n       obs[x].emplace_back(y); n   } n   vector<pair<int, int>> segs = {{1, left}}; n   int prv = 1; n   for (auto &p : obs) { n     auto &v = p.second; n     sort(begin(v), end(v)); n     if (p.first != prv + 1) { n       segs.assign(1, {segs.begin()->first, n}); n     } n     vector<pair<int, int>> segs2; n     auto it = segs.begin(); n     int p_v = 0, sz_v = v.size(); n     int lst = -1; n     while (it != segs.end() && p_v < sz_v) { n       chmax(it->first, lst); n       if (it->first > it->second) { n         it++; n         continue; n       } n       if (v[p_v] <= it->first) { n         it->first += v[p_v++] == it->first; n         continue; n       } n       segs2.emplace_back(it->first, v[p_v] - 1); n       lst = v[p_v++] + 1; n     } n     if (p_v == sz_v) { n       while (it != segs.end()) { n         chmax(it->first, lst); n         if (it->first <= it->second) break; n         it++; n       } n       if (it != segs.end()) { n         segs2.emplace_back(it->first, n); n       } n     } n     if ((segs = segs2).empty()) return !(cout << -1 << endl); n     prv = p.first; n   } n   if (prv < n) { n     segs.assign(1, {segs.begin()->first, n}); n   } n   cout << (segs.rbegin()->second == n ? (n - 1) * 2 : -1) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int t; n   cin >> t; n   while (t--) { n     long long n, k; n     cin >> n >> k; n     if (n > k) n       cout << k << endl; n     else { n       int ne = (k - 1) / (n - 1); n       cout << k + ne << endl; n     } n   } n } n


#include <bits/stdc++.h> n int mat[110][110]; n int main() { n   int n, k, i, j; n   scanf( %d %d , &n, &k); n   if (n * n < k) { n     printf( -1 n ); n     return 0; n   } n   for (i = 0; i < n; i++) { n     for (j = i; j < n; j++) { n       if (i == j) { n         if (k > 0) { n           k--; n           mat[i][j] = 1; n         } else n           break; n       } else { n         if (k >= 2) { n           mat[i][j] = 1; n           mat[j][i] = 1; n           k = k - 2; n         } else if (k == 0) n           break; n       } n     } n   } n   for (i = 0; i < n; i++) { n     for (j = 0; j < n; j++) { n       printf( %d  , mat[i][j]); n     } n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n int n, k; n bool visited[101][101][1010]; n double ar[101], dp[101][101][1010]; n double F(int i, int j, int k) { n   if (i > j) return 1.0 - F(j, i, k); n   if (k == 0) return 1.0; n   if (visited[i][j][k]) return dp[i][j][k]; n   int a, b, c, d, l; n   double x, y, p = 1.0 / ((n * (n + 1)) >> 1), res = 0.0; n   for (a = 0; a < n; a++) { n     for (b = a; b < n; b++) { n       c = i, d = j; n       if (c >= a && c <= b) c = b - (c - a); n       if (d >= a && d <= b) d = b - (d - a); n       res += (p * F(c, d, k - 1)); n     } n   } n   visited[i][j][k] = true; n   return (dp[i][j][k] = res); n } n int main() { n   int i, j, l; n   while (scanf( %d %d , &n, &k) != EOF) { n     if (k > 1000) k = 1000; n     for (i = 0; i < n; i++) scanf( %d , &ar[i]); n     memset(visited, 0, sizeof(visited)); n     double res = 0.0; n     for (i = 0; i < n; i++) { n       for (j = i + 1; j < n; j++) { n         if (ar[i] < ar[j]) n           res += (1.0 - F(i, j, k)); n         else n           res += F(i, j, k); n       } n     } n     printf( %0.12f n , res + 1e-15); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n T gcd(T a, T b) { n   return b ? gcd(b, a % b) : a; n } n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n const double EPS = 1e-9; n const double PI = acos(-1.); n const long long LL_INF = 1e17 + 16; n const int INF = 1e9 + 10; n const long long MOD = 1e9 + 7; n const int MAXN = 20; n set<long long> goods; n long long f[MAXN]; n void gen() { n   for (int i = (1); i <= (int)(10); i++) { n     for (int j = (0); j < (int)(1 << i); j++) { n       long long v = 0; n       long long st10 = 1; n       for (int k = (0); k < (int)(i); k++) { n         if (j & (1 << k)) { n           v += 7 * st10; n         } else { n           v += 4 * st10; n         } n         st10 *= 10; n       } n       goods.insert(v); n     } n   } n } n int per[MAXN]; n int used[MAXN]; n void solve() { n   gen(); n   long long n, k; n   cin >> n >> k; n   f[0] = 1; n   int m; n   for (m = 1;; m++) { n     f[m] = f[m - 1] * m; n     if (f[m] >= k) { n       break; n     } n   } n   if (m > n) { n     printf( -1 n ); n     return; n   } n   int ans = 0; n   int offset = n - m; n   for (__typeof((goods).begin()) it = (goods).begin(); it != (goods).end(); n        it++) { n     if ((*it) <= offset) { n       ans++; n     } n   } n   k--; n   int top = 1; n   for (int i = m; i >= 1; i--) { n     int v = k / f[i - 1] + 1; n     int cnt = 0; n     for (int j = (1); j <= (int)(m); j++) { n       if (!used[j]) { n         cnt++; n         if (cnt == v) { n           per[top++] = j; n           used[j] = 1; n           break; n         } n       } n     } n     k %= f[i - 1]; n   } n   for (int i = (1); i <= (int)(m); i++) { n     if (goods.count(i + offset) && goods.count(per[i] + offset)) { n       ans++; n     } n   } n   cout << ans << endl; n } n int main() { n   int t = 1; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n void dfs(set<int> &temp, list<int> adj[], vector<bool> &v, int s, int &t1, n          int &t2, vector<int> b, vector<int> w) { n   v[s] = true; n   list<int>::iterator it; n   for (it = adj[s].begin(); it != adj[s].end(); it++) { n     if (!v[*it]) { n       temp.insert(*it); n       t1 += b[*it]; n       t2 += w[*it]; n       dfs(temp, adj, v, *it, t1, t2, b, w); n     } n   } n } n list<int> adj[1001]; n int main() { n   int n, m, weight; n   cin >> n >> m >> weight; n   vector<int> b(n + 1); n   vector<int> w(n + 1); n   for (int i = 1; i <= n; i++) cin >> w[i]; n   for (int i = 1; i <= n; i++) cin >> b[i]; n   for (int i = 0; i < m; i++) { n     int a, b; n     cin >> a >> b; n     adj[a].push_back(b); n     adj[b].push_back(a); n   } n   vector<bool> v(n + 1, false); n   vector<set<int> > arr; n   vector<int> tw; n   vector<int> tb; n   for (int i = 1; i <= n; i++) { n     if (!v[i]) { n       set<int> temp; n       temp.insert(i); n       int t1 = b[i], t2 = w[i]; n       dfs(temp, adj, v, i, t1, t2, b, w); n       arr.push_back(temp); n       tb.push_back(t1); n       tw.push_back(t2); n     } n   } n   vector<vector<int> > dp(arr.size() + 1, vector<int>(weight + 1, 0)); n   for (int i = 1; i <= arr.size(); i++) { n     for (int j = 1; j <= weight; j++) { n       int max = 0; n       if (dp[i - 1][j] > max) max = dp[i - 1][j]; n       if (j - tw[i - 1] >= 0) n         if (dp[i - 1][j - tw[i - 1]] + tb[i - 1] > max) n           max = dp[i - 1][j - tw[i - 1]] + tb[i - 1]; n       set<int>::iterator it; n       for (it = arr[i - 1].begin(); it != arr[i - 1].end(); it++) { n         if (j - w[*it] >= 0) n           if (dp[i - 1][j - w[*it]] + b[*it] > max) n             max = dp[i - 1][j - w[*it]] + b[*it]; n       } n       dp[i][j] = max; n     } n   } n   int max = 0; n   for (int i = 0; i <= weight; i++) n     if (dp[arr.size()][i] > max) max = dp[arr.size()][i]; n   cout << max; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, ans, x, i, j, s; n double a[100], b[100]; n map<double, pair<long long, long long> > mp; n map<double, pair<long long, long long> >::iterator it, it2; n long long num(long long x) { n   s = 0; n   while (x) { n     s++; n     x -= x & -x; n   } n   return s; n } n int main() { n   cin >> n >> m; n   for (i = 0; i < n; i++) cin >> a[i]; n   for (i = 0; i < m; i++) cin >> b[i]; n   for (i = 0; i < n; i++) n     for (j = 0; j < m; j++) { n       mp[(a[i] + b[j]) / 2.0].first |= (1LL << i); n       mp[(a[i] + b[j]) / 2.0].second |= (1LL << j); n     } n   for (it = mp.begin(); it != mp.end(); it++) n     for (it2 = it; it2 != mp.end(); it2++) { n       x = num((*it).second.first | (*it2).second.first) + n           num((*it).second.second | (*it2).second.second); n       if (x > ans) ans = x; n     } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   long long n, k; n   scanf( %I64d , &n); n   while (n % 3 == 0) n /= 3; n   printf( %I64d , 1 + n / 3); n   getchar(); n   getchar(); n } n


#include <bits/stdc++.h> n using namespace std; n constexpr long long INF = numeric_limits<long long>::max() / 4; n constexpr long long n_max = 2e5 + 10; n template <typename A, typename B> n string to_string(pair<A, B> p); n string to_string(const string &s) { return     + s +    ; } n string to_string(const char c) { return to_string((string)&c); } n string to_string(bool b) { return (b ?  true  :  false ); } n template <size_t N> n string to_string(bitset<N> v) { n   string res =   ; n   for (size_t i = 0; i < N; i++) res += static_cast<char>( 0  + v[i]); n   return res; n } n template <typename A> n string to_string(A v) { n   bool first = true; n   string res =  { ; n   for (const auto &x : v) { n     if (!first) res +=  ,  ; n     first = false; n     res += to_string(x); n   } n   res +=  } ; n   return res; n } n template <typename A, typename B> n string to_string(pair<A, B> p) { n   return  (  + to_string(p.first) +  ,   + to_string(p.second) +  ) ; n } n void debug_out() { cerr << endl; } n template <typename Head, typename... Tail> n void debug_out(Head H, Tail... T) { n   cerr <<     << to_string(H); n   debug_out(T...); n } n template <class T> n bool chmax(T &a, T b) { n   if (a < b) { n     a = b; n     return true; n   } n   return false; n } n template <class T> n bool chmin(T &a, T b) { n   if (a > b) { n     a = b; n     return true; n   } n   return false; n } n signed main() { n   cin.tie(nullptr); n   ios::sync_with_stdio(false); n   long long t; n   cin >> t; n   for (long long _ = 0; _ < (long long)(t); _++) { n     long long n; n     cin >> n; n     vector<long long> a(n); n     for (long long i = 0; i < (long long)(n); i++) cin >> a[i]; n     vector<long long> dp(n + 1); n     for (long long i = 1; i < (long long)(n) + 1; i++) { n       dp[i]++; n       for (long long j = i * 2; j <= n; j += i) { n         if (a[j - 1] > a[i - 1]) chmax(dp[j], dp[i]); n       } n     } n     long long ans = 0; n     for (long long i = 0; i < (long long)(n + 1); i++) chmax(ans, dp[i]); n     cout << ans <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int test = 1; n   cin >> test; n   while (test--) { n     long long int n, m; n     cin >> n >> m; n     long long int sum1 = 0, sum2 = 0; n     long long int a[n], b[n], mina[n], mi = 0; n     for (int i = 0; i < n; i++) { n       cin >> a[i] >> b[i]; n       sum1 += a[i]; n       sum2 += b[i]; n       mina[i] = max(0LL, m - b[i]); n       mi += mina[i]; n       a[i] = min(a[i], m); n     } n     long long int ans = sum1 - sum2 + (n * m); n     long long int x = ans - 2LL * mi; n     for (int i = 0; i < n; i++) { n       if (x <= 0) break; n       long long int temp = min(2LL * (a[i] - mina[i]), x); n       temp /= 2; n       mina[i] += temp; n       x -= 2LL * temp; n     } n     cout << abs(x) <<   n ; n     for (int i = 0; i < n; i++) cout << mina[i] <<     << m - mina[i] <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma warning(disable : 4996) n using namespace std; n bool compare(int a, int b) { return a > b; } n int main() { n   int t, p, f, cnts, cntw, s, w; n   scanf( %d , &t); n   while (t--) { n     scanf( %d%d , &p, &f); n     scanf( %d%d , &cnts, &cntw); n     scanf( %d%d , &s, &w); n     if (s > w) { n       int temp = s; n       s = w; n       w = temp; n       temp = cnts; n       cnts = cntw; n       cntw = temp; n     } n     int mx = min(cnts, p / s); n     int ans = 0; n     for (int s1 = 0; s1 <= mx; s1++) { n       int w1 = min(cntw, (p - s1 * s) / w); n       int s2 = min(cnts - s1, f / s); n       int w2 = min(cntw - w1, (f - s2 * s) / w); n       ans = max(ans, s1 + s2 + w1 + w2); n     } n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e3 + 10; n long long n, k; n int main() { n   scanf( %I64d%I64d , &n, &k); n   long long x; n   x = sqrt(9 + 8 * (n + k)) - 3; n   x /= 2; n   printf( %I64d n , n - x); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 998244353; n const int N = 3e3; n long long dmod(long long x) { return x % MOD; } n int n, k; n int h[N + 5]; n long long memo[N + 5][2 * N + 5]; n long long dp(int idx, int gap) { n   if (idx > n) { n     if (gap > 0) return 1; n     return 0; n   } n   long long &ret = memo[idx][gap + N]; n   if (ret != -1) return ret; n   ret = 0ll; n   if (h[idx] == h[idx % n + 1]) { n     ret = dmod((long long)k * dp(idx + 1, gap)); n   } else { n     ret = dp(idx + 1, gap + 1); n     ret = dmod(ret + dp(idx + 1, gap - 1)); n     ret = dmod(ret + dmod((long long)(k - 2) * dp(idx + 1, gap))); n   } n   return ret; n } n int main() { n   memset(memo, -1, sizeof(memo)); n   scanf( %d%d , &n, &k); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &h[i]); n   } n   printf( %lld n , dp(1, 0)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, x, m = 0, sum = 0; n   cin >> n >> x; n   int *arr = new int[n]; n   for (long long i = 0; i < n; i++) cin >> arr[i], sum += arr[i]; n   if (sum > 0) n     while (sum > 0) { n       sum -= x; n       m++; n     } n   else n     while (sum < 0) { n       sum += x; n       m++; n     } n   cout << m; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int brute(int s, int x) { n   int ans = 0; n   for (int i = 1; i <= s; ++i) n     for (int j = 1; j <= s; ++j) n       if (i + j == s && (i ^ j) == x) ++ans; n   return ans; n } n long long ways0(long long s, long long x) { n   if (s == x) return 2; n   return 0; n } n long long solve(long long s, long long x) { n   long long dp[60][2] = {}; n   if (s % 2 != x % 2) { n     return 0; n   } n   if (x & (1LL << 0)) { n     dp[0][0] = 2; n   } else { n     dp[0][0] = 1; n     dp[0][1] = 1; n   } n   for (int i = 1; i < 60; ++i) { n     if (x & (1LL << i)) { n       if (s & (1LL << i)) { n         dp[i][0] += 2 * dp[i - 1][0]; n       } else { n         dp[i][1] += 2 * dp[i - 1][1]; n       } n     } else { n       if (s & (1LL << i)) { n         dp[i][0] += dp[i - 1][1]; n         dp[i][1] += dp[i - 1][1]; n       } else { n         dp[i][0] += dp[i - 1][0]; n         dp[i][1] += dp[i - 1][0]; n       } n     } n   } n   return dp[59][0] - ways0(s, x); n } n int main() { n   long long s, x; n   cin >> s >> x; n   cout << solve(s, x) <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline void dmin(T& x, T y) { n   y < x ? x = y : 0; n } n template <class T> n inline void dmax(T& x, T y) { n   y > x ? x = y : 0; n } n template <class T> n inline void dmin(T& x, vector<T> y) { n   for (auto t : y) t < x ? x = t : 0; n } n template <class T> n inline void dmax(T& x, vector<T> y) { n   for (auto t : y) t > x ? x = t : 0; n } n const int N = 5 + 1e6; n const long long INF = -2e18; n int n, k; n long long gk(int x, long long a[], int i, long long f) { n   return a[x] + (i - x) / 3 * f; n } n bool cmp(int x, int y, long long a[], int i, long long f) { n   return gk(x, a, i, f) > gk(y, a, i, f); n } n void opera(long long a[], int n, long long f, int k) { n   static long long b[N]; n   static long long q[3][N], left[3], right[3]; n   for (int i = 0; i < 3; ++i) left[i] = 1, right[i] = 0; n   for (int i = 0; i < n; ++i) b[i] = INF; n   for (int i = 0; i < n; ++i) { n     int t = i % 3; n     while (left[t] <= right[t] && q[t][left[t]] < i - 3 * k) ++left[t]; n     while (left[t] <= right[t] && cmp(i, q[t][right[t]], a, i, f)) --right[t]; n     q[t][++right[t]] = i; n     b[i] = gk(q[t][left[t]], a, i, f); n   } n   for (int i = 0; i < n; ++i) a[i] = b[i]; n } n void opera2(long long a[], int n, long long f, int k) { n   static long long f1[N], f2[N]; n   for (int i = 0; i < n; ++i) { n     f1[i] = f2[i] = a[i]; n     for (int j = 1; j < 10 && i - j >= 0; ++j) f2[i] = max(f2[i], a[i - j]); n   } n   opera(f1, n, f, 3 * k); n   opera(f2, n, f, 3 * (k - 1)); n   for (int i = 0; i < n; ++i) a[i] = max(f1[i], f2[i]); n } n long long a[N], b[N]; n int main() { n   int k; n   scanf( %d , &k); n   long long f[6]; n   for (int i = 0; i < 6; ++i) scanf( %lld , &f[i]); n   a[0] = 0; n   n = 1; n   for (int i = 5; i >= 0; --i) { n     for (int j = 0; j < 10 * n; ++j) b[j] = INF; n     for (int j = 0; j < n; ++j) b[j * 10] = a[j]; n     n *= 10; n     opera2(b, n, f[i], k); n     for (int j = 0; j < n; ++j) a[j] = b[j]; n   } n   int q; n   scanf( %d , &q); n   while (q--) { n     int x; n     scanf( %d , &x); n     printf( %lld n , a[x]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     int arr[n]; n     for (int i = 0; i < n; i++) { n       cin >> arr[i]; n     } n     sort(arr, arr + n); n     bool f = true; n     for (int j = 1; j < n; j++) { n       int diff = abs(arr[j] - arr[j - 1]); n       if (diff > 1) { n         f = false; n       } n     } n     if (f) { n       cout <<  YES n ; n     } else { n       cout <<  NO n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long int modulo = 1000000007; n const int limite = 1000001; n long long int elev[limite]; n int ocupado[limite]; n int main() { n   ios_base::sync_with_stdio(false); n   int n, m, k; n   cin >> n >> m >> k; n   elev[0] = 1; n   for (int i = 1; i < limite; i++) elev[i] = (elev[i - 1] * 2) % modulo; n   int ini = 0; n   int fin = n + 1; n   int inf = k + 2; n   int sup = n; n   for (int i = 0; i < m; i++) { n     int u, v; n     cin >> u >> v; n     if (v == u + 1) n       ; n     else if (v == u + k + 1) { n       ocupado[v] = 1; n       ini = max(ini, u); n       fin = min(fin, v); n       inf = max(inf, u + 1); n       sup = min(sup, v + k); n     } else { n       cout << 0 << endl; n       exit(0); n     } n   } n   if (fin <= ini) { n     cout << 0 << endl; n     exit(0); n   } n   int libres = 0; n   long long int maneras = 1; n   for (int i = inf; i <= sup; i++) { n     if (not ocupado[i]) { n       maneras = (maneras + elev[libres]) % modulo; n       libres++; n     } n     if (i - k >= inf and not ocupado[i - k]) libres--; n   } n   cout << maneras << endl; n } n


#include <bits/stdc++.h> n using namespace std; n bool v[6]; n int a[100009][6], maxx = -1, t; n long long m = 1e9 + 7; n struct pi { n   long long ah[60][60]; n }; n pi multi(pi x, pi y) { n   pi z; n   for (int i = 1; i <= t; i++) { n     for (int j = 1; j <= t; j++) { n       z.ah[i][j] = 0; n       for (int k = 1; k <= t; k++) n         z.ah[i][j] = (z.ah[i][j] % m + x.ah[i][k] % m * y.ah[k][j] % m) % m; n       z.ah[i][j] %= m; n     } n   } n   return z; n } n pi quimul(pi x, long long int n) { n   pi an; n   for (int i = 1; i <= t; i++) an.ah[i][i] = 1; n   for (; n; n >>= 1) { n     if (n & 1) an = multi(an, x); n     x = multi(x, x); n   } n   return an; n } n int main() { n   long long n; n   int x, v, p[600], zh[90][90]; n   char c =  a , u; n   for (int i = 1; i <= 52; i++) n     for (int j = 1; j <= 52; j++) zh[i][j] = 1; n   for (int i = 1; i <= 26; i++) p[(int)c + i - 1] = i; n   c =  A ; n   for (int i = 27; i <= 52; i++) p[(int)c + i - 27] = i; n   cin >> n >> t >> v; n   for (int i = 0; i < v; i++) { n     scanf(  %c %c , &c, &u); n     zh[p[(int)c]][p[(int)u]] = 0; n   } n   pi ans; n   for (int i = 1; i <= t; i++) n     for (int j = 1; j <= t; j++) ans.ah[i][j] = zh[i][j]; n   ans = quimul(ans, n - 1); n   long long int pp = 0; n   for (int i = 1; i <= t; i++) n     for (int j = 1; j <= t; j++) pp = (pp + ans.ah[i][j]) % m; n   cout << (pp + m) % m << endl; n } n


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


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void display(T a[], int size) { n   for (int i = 0; i < size; i++) { n     cout << a[i] <<    ; n   } n   cout << endl; n } n void display(vector<pair<int, int> > a) { n   for (int i = 0; i < a.size(); i++) { n     cout <<  (  << a[i].first <<  ,  << a[i].second <<  )  n          <<    ; n   } n   cout << endl; n } n void display(const vector<int>& a) { n   for (int i = 0; i < a.size(); i++) { n     cout << a[i]; n     if (i == a.size() - 1) n       cout << endl; n     else n       cout <<    ; n   } n } n template <typename T> n void initialise(T a[], T value, int length) { n   for (int i = 0; i < length; i++) a[i] = value; n } n template <typename T> n void initialise(vector<T>& a, T value) { n   for (int i = 0; i < a.size(); i++) a[i] = value; n } n bool compare(pair<int, int> a, pair<int, int> b) { n   if (a.first != b.first) n     return a.first < b.first; n   else n     return a.second < b.second; n } n int max(int a[], int n) { n   int max = -1000000000; n   for (int i = 0; i < n; i++) { n     if (a[i] > max) max = a[i]; n   } n   return max; n } n bool find(string a[], string s, int n) { n   int left = 0; n   int right = n - 1; n   while (left < right) { n     int mid = (left + right) / 2; n     if (s.compare(a[mid]) == 0) n       return true; n     else if (s.compare(a[mid]) < 0) { n       right = mid; n     } else { n       left = mid + 1; n     } n   } n   return false; n } n void factor(int a[], int base, int num, int n) { n   for (int i = n - 1; i >= 0; i--) { n     a[i] = num % base; n     num = num / base; n   } n } n int findLength(int n, int base) { n   int i = 0; n   while (n > 0) { n     i++; n     n = n / base; n   } n   return i; n } n int gcd(int a, int b) { n   while (a % b != 0 && b % a != 0) { n     if (b > a) { n       b = b % a; n     } else if (a > b) { n       a = a % b; n     } n   } n   if (a <= b) n     return a; n   else n     return b; n } n int main() { n   int n; n   cin >> n; n   int dir[n]; n   int step[n]; n   for (int i = 0; i < n; i++) { n     char now; n     cin >> now; n     if (now ==  > ) n       dir[i] = 1; n     else n       dir[i] = -1; n   } n   for (int i = 0; i < n; i++) { n     cin >> step[i]; n   } n   int trace[n]; n   initialise(trace, 0, n); n   int curr = 0; n   while (true) { n     if (trace[curr] == 1) { n       cout <<  INFINITE  << endl; n       break; n     } else { n       trace[curr] = 1; n       curr = curr + dir[curr] * step[curr]; n       if (curr >= n || curr < 0) { n         cout <<  FINITE  << endl; n         break; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, c = 0, d = 0, k; n   string str; n   cin >> n >> str; n   for (int i = 0; i < n; i++) { n     if (str[i] ==  - ) c++; n   } n   if (c == n) { n     cout << 0 << endl; n     return 0; n   } else if (str[0] ==  - ) { n     for (int i = 0; i < n; i++) { n       if (str[i] ==  - ) { n         continue; n       } else { n         k = i; n         break; n       } n     } n     for (int i = k; i < n; i++) { n       if (str[i] ==  + ) n         d += 1; n       else { n         d -= 1; n         if (d < 0) d = 0; n       } n     } n   } else { n     for (int i = 0; i < n; i++) { n       if (str[i] ==  + ) n         d += 1; n       else { n         d -= 1; n         if (d < 0) d = 0; n       } n     } n   } n   cout << d << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int M; n const int MAX = 100001; n vector<int> _size, marked, _pow, _ipow; n vector<pair<int, int> > adjList[MAX + 1]; n int getSize(int u, int p = -1) { n   _size[u] = 1; n   for (auto [v, w] : adjList[u]) n     if (v != p && !marked[v]) _size[u] += getSize(v, u); n   return _size[u]; n } n int getCentroid(int u, int p, int n) { n   for (auto [v, w] : adjList[u]) n     if (v != p && !marked[v] && _size[v] * 2 > n) return getCentroid(v, u, n); n   return u; n } n void add(unordered_map<int, int> &aux, int u, int p, int lvl, int rem) { n   aux[rem]++; n   for (auto [v, w] : adjList[u]) n     if (v != p && !marked[v]) n       add(aux, v, u, lvl + 1, (int(1LL * w * _pow[lvl] % M) + rem) % M); n } n void remove(unordered_map<int, int> &aux, int u, int p, int lvl, int rem) { n   aux[rem]--; n   for (auto [v, w] : adjList[u]) n     if (v != p && !marked[v]) n       remove(aux, v, u, lvl + 1, (int(1LL * w * _pow[lvl] % M) + rem) % M); n } n long long count(unordered_map<int, int> &aux, int u, int p, int lvl, int rem) { n   int nxt = int(1LL * _ipow[lvl] * (M - rem) % M); n   long long ans = aux[nxt]; n   for (auto [v, w] : adjList[u]) n     if (v != p && !marked[v]) n       ans += count(aux, v, u, lvl + 1, (rem * 10LL + w) % M); n   return ans; n } n long long solve(int u) { n   int N = getSize(u); n   u = getCentroid(u, -1, N); n   marked[u] = 1; n   unordered_map<int, int> aux; n   aux[0] = 1; n   for (auto [v, w] : adjList[u]) n     if (!marked[v]) add(aux, v, u, 1, w % M); n   long long ans = aux[0] - 1; n   for (auto [v, w] : adjList[u]) n     if (!marked[v]) { n       remove(aux, v, u, 1, w % M); n       ans += count(aux, v, u, 1, w % M); n       add(aux, v, u, 1, w % M); n     } n   aux.clear(); n   for (auto [v, w] : adjList[u]) n     if (!marked[v]) ans += solve(v); n   return ans; n } n int gcd(int a, int b, int &x, int &y) { n   if (b == 0) { n     x = 1; n     y = 0; n     return a; n   } n   int x1, y1; n   int d = gcd(b, a % b, x1, y1); n   x = y1; n   y = x1 - y1 * (a / b); n   return d; n } n int getInv() { n   int x, y; n   int g = gcd(10, M, x, y); n   assert(g == 1); n   return (x % M + M) % M; n } n int main() { n   ios_base ::sync_with_stdio(false); n   cin.tie(nullptr); n   int n; n   cin >> n >> M; n   _size = vector<int>(n), _pow = vector<int>(n + 1, 1), n   _ipow = vector<int>(n + 1, 1), marked = vector<int>(n + 1); n   _ipow[1] = getInv(); n   _pow[1] = 10; n   for (int i = 2; i <= n; i++) { n     _pow[i] = int(1LL * _pow[i - 1] * 10 % M); n     _ipow[i] = int(1LL * _ipow[i - 1] * _ipow[1] % M); n   } n   for (int i = 0; i < n - 1; i++) { n     int u, v, w; n     cin >> u >> v >> w; n     adjList[u].push_back({v, w}); n     adjList[v].push_back({u, w}); n   } n   cout << solve(0) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e6 + 100, M = 1e6 + 100; n int n, up[N], down[M], nei[M][2]; n int lim; n bool mark[M], bad[M]; n vector<int> vec; n int f(int a) { return lower_bound(vec.begin(), vec.end(), a) - vec.begin(); } n bool dfs(int v) { n   mark[v] = true; n   for (int i = 0; i < 2 && nei[v][i] <= lim; i++) { n     int u = nei[v][i]; n     if (up[u] == -1 || (mark[up[u]] == false && dfs(up[u]))) { n       up[u] = v; n       down[v] = u; n       return true; n     } n   } n   return false; n } n bool ok(int X) { n   lim = X; n   fill(up, up + N, -1); n   fill(down, down + M, -1); n   memset(bad, false, sizeof bad); n   int matching = 0; n   bool done = false; n   for (int v = 0; v < n; v++) n     if (nei[v][0] > lim) n       return false; n     else if (nei[v][1] > lim) { n       if (up[nei[v][0]] != -1) return false; n       down[v] = nei[v][0]; n       up[nei[v][0]] = v; n       bad[v] = true; n       matching++; n     } n   while (done == false) { n     done = true; n     copy(bad, bad + n, mark); n     for (int v = 0; v < n; v++) n       if (mark[v] == false && down[v] == -1 && dfs(v)) { n         matching++; n         done = false; n       } n   } n   return matching == n; n } n int main() { n   ios::sync_with_stdio(false), cin.tie(0); n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> nei[i][0] >> nei[i][1]; n     vec.push_back(nei[i][0]); n     vec.push_back(nei[i][1]); n   } n   sort(vec.begin(), vec.end()); n   vec.resize(unique(vec.begin(), vec.end()) - vec.begin()); n   for (int i = 0; i < n; i++) n     nei[i][0] = f(nei[i][0]), nei[i][1] = f(nei[i][1]); n   if (ok(vec.size() - 1) == false) { n     cout << -1; n     return 0; n   } n   if (ok(vec.size() - 2) == false) { n     cout << vec.back(); n     return 0; n   } n   int l = -1, r = vec.size() - 1; n   while (r - l > 1) { n     if (ok((l + r) >> 1)) n       r = lim; n     else n       l = lim; n   } n   cout << vec[r]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int val[200007], add[200007]; n int main() { n   int n, t, a, x, k, l = 1; n   long long sum = 0; n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) { n     scanf( %d , &t); n     if (t == 1) { n       scanf( %d%d , &a, &x); n       add[a] += x; n       sum += a * x; n     } else if (t == 2) { n       scanf( %d , &k); n       val[++l] = k; n       sum += val[l]; n     } else if (l > 1) { n       sum -= val[l]; n       sum -= add[l]; n       add[l - 1] += add[l]; n       add[l] = 0; n       --l; n     } n     printf( %.6lf n , (double)sum / l); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n constexpr int N = 2e5 + 10; n long long n, w, k, happy[N], nice, t[N], mytime, ans; n bool in[N]; n pair<int, int> lowest; n set<pair<int, int>> st; n queue<int> q; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> w >> k; n   for (int i = 0; i < n; i++) cin >> happy[i]; n   for (int i = 0; i < n; i++) cin >> t[i]; n   for (int i = n - 1; ~i; i--) { n     st.insert({t[i], -i}); n     if ((int)st.size() == w) { n       lowest = *st.begin(); n       mytime += (t[i] + 1) / 2; n       in[i] = true; n     } else if ((int)st.size() > w) { n       if (t[i] >= lowest.first) { n         in[-lowest.second] = false; n         mytime += (t[i] + 1) / 2; n         mytime -= (lowest.first + 1) / 2; n         mytime += lowest.first; n         lowest = *st.upper_bound(lowest); n         in[i] = true; n       } else n         mytime += t[i]; n     } else { n       in[i] = true; n       mytime += (t[i] + 1) / 2; n     } n     nice += happy[i]; n     q.push(i); n     while (mytime > k) { n       nice -= happy[q.front()]; n       if (in[q.front()]) { n         mytime -= (t[q.front()] + 1) / 2; n         in[q.front()] = false; n         st.erase({t[q.front()], -q.front()}); n         if ((int)st.size() >= w) { n           lowest = *(--st.lower_bound(lowest)); n           in[-lowest.second] = true; n           mytime -= lowest.first; n           mytime += (lowest.first + 1) / 2; n         } n       } else { n         mytime -= t[q.front()]; n         st.erase({t[q.front()], -q.front()}); n       } n       q.pop(); n     } n     ans = max(ans, nice); n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline void speedUp() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n } n long long int t, n, m, p[100005], q[100005], Podd, Peven, Qodd, Qeven, tot; n int main() { n   speedUp(); n   int t, n; n   cin >> t; n   while (t--) { n     tot = Podd = Peven = Qodd = Qeven = 0; n     cin >> n; n     for (int i = 1; i <= n; i++) { n       cin >> p[i]; n       if (p[i] % 2 == 0) n         Peven++; n       else n         Podd++; n     } n     cin >> m; n     for (int i = 1; i <= m; i++) { n       cin >> q[i]; n       if (q[i] % 2 == 0) n         Qeven++; n       else n         Qodd++; n     } n     for (int i = 1; i <= n; i++) { n       if (p[i] % 2 == 0) n         tot += Qeven; n       else n         tot += Qodd; n     } n     cout << tot << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main(void) { n   int n; n   scanf( %d , &n); n   int v[n]; n   for (int i = 0; i < n; i++) scanf( %d , v + i); n   if (n > 2) { n     for (int i = 0; i + 3 <= n; i++) { n       int a = v[i], b = v[i + 1], c = v[i + 2]; n       int ans1 = -1, ans2 = -1; n       set<int> ms; n       ms.insert(a); n       ms.insert(b); n       ms.insert(c); n       if (ms.size() == 1) n         continue; n       else if (ms.size() == 3) { n         if (a == *ms.begin()) n           ans1 = i, ans2 = i + 1; n         else if (a == *(++ms.begin())) n           ans1 = i, ans2 = i + 2; n         else if (a == *ms.rbegin()) n           ans1 = i, ans2 = i + 1; n       } else if (ms.size() == 2) { n         if (a == c) { n           int d = -1; n           if (i > 0) { n             d = v[i - 1]; n             if (d != a) n               ans1 = i - 1, ans2 = i; n             else n               ans1 = i, ans2 = i + 1; n           } else if (i + 3 < n) { n             d = v[i + 3]; n             if (d != a) n               ans1 = i + 2, ans2 = i + 3; n             else n               ans1 = i + 1, ans2 = i + 2; n           } else n             continue; n         } else if (a == b) n           ans1 = i + 1, ans2 = i + 2; n         else if (b == c) n           ans1 = i, ans2 = i + 1; n       } n       printf( %d %d n , ans1 + 1, ans2 + 1); n       return 0; n     } n   } n   printf( -1 n ); n   return (0); n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-2; n struct cplx { n   double x, y; n   cplx(double x1 = 0, double x2 = 0) { x = x1, y = x2; } n } g[2000001], f[2000001]; n cplx operator+(cplx a, cplx b) { return cplx(a.x + b.x, a.y + b.y); } n cplx operator-(cplx a, cplx b) { return cplx(a.x - b.x, a.y - b.y); } n cplx operator*(cplx a, cplx b) { n   return cplx(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x); n } n void swap(cplx &a, cplx &b) { swap(a.x, b.x), swap(a.y, b.y); } n int a[2000001], b[2000001]; n int r[2000001]; n const double Pi = acos(-1); n void fft(cplx f[], int op, int n) { n   for (int i = 0; i < n; i++) n     if (i < r[i]) swap(f[i], f[r[i]]); n   for (int w = 2; w <= n; w <<= 1) { n     int len = w / 2; n     cplx tmp(cos(Pi / len), op * sin(Pi / len)); n     for (int k = 0; k < n; k += w) { n       cplx buf(1, 0); n       for (int l = k; l < k + len; l++) { n         cplx tt = f[len + l] * buf; n         f[len + l] = f[l] - tt; n         f[l] = f[l] + tt; n         buf = buf * tmp; n       } n     } n   } n   if (op == -1) { n     for (int i = 0; i < n; i++) f[i].x /= n, f[i].y /= n; n   } n } n char p[2000001]; n char q[2000001]; n int res[2000001]; n int l1, l2, k, n, m; n void solve(char c) { n   for (int i = 0; i <= n; i++) f[i].x = f[i].y = g[i].x = g[i].y = 0; n   int far = -1; n   for (int i = 0; i < l1; i++) { n     if (p[i] == c) far = i + k; n     if (i <= far) f[i].x = 1; n   } n   far = l1; n   for (int i = l1 - 1; i >= 0; i--) { n     if (p[i] == c) far = i - k; n     if (i >= far) f[i].x = 1; n   } n   for (int i = 0; i < l2; i++) n     if (q[i] == c) g[i].x = 1; n   fft(f, 1, n); n   fft(g, 1, n); n   for (int i = 0; i < n; i++) f[i] = f[i] * g[i]; n   fft(f, -1, n); n   for (int i = 0; i < n; i++) res[i] += (int)(f[i].x + eps); n } n signed main() { n   int ans = 0; n   scanf( %d%d%d , &l1, &l2, &k); n   cin >> p; n   cin >> q; n   reverse(p, p + l1); n   n = l1, m = l2; n   for (m += n, n = 1; n <= m; n <<= 1) n     ; n   for (int i = 0; i < n; i++) r[i] = (r[i >> 1] >> 1) | ((i & 1) ? n >> 1 : 0); n   solve( A ); n   solve( T ); n   solve( G ); n   solve( C ); n   for (int i = 0; i <= l1 - l2; i++) n     if (res[l1 - i - 1] >= l2) ans++; n   printf( %d , ans); n   return 0; n } n


#include <bits/stdc++.h> n struct edge { n   int to, nxt; n } e[100005], e2[100005]; n int h[100005], n, cnt, hei[100005], gson[100005], node[100005], N, fa[100005]; n void ins(int x, int y) { n   e[++cnt].nxt = h[x]; n   e[cnt].to = y; n   h[x] = cnt; n } n void dfs1(int x) { n   hei[x] = 1; n   for (int i = h[x]; i; i = e[i].nxt) { n     dfs1(e[i].to); n     hei[x] = std::max(hei[x], hei[e[i].to] + 1); n     if (hei[gson[x]] < hei[e[i].to]) gson[x] = e[i].to; n   } n } n void dfs2(int x) { n   node[++N] = x; n   for (int i = h[x]; i; i = e[i].nxt) n     if (e[i].to != gson[x]) dfs2(e[i].to); n   if (gson[x]) dfs2(gson[x]); n } n int main() { n   scanf( %d , &n); n   for (int i = 2; i <= n; i++) scanf( %d , &fa[i]), ins((++fa[i]), i); n   dfs1(1); n   dfs2(1); n   for (int i = 1; i <= n; i++) printf( %d  , node[i] - 1); n   printf(  n%d n , n - hei[1]); n   for (int i = 1, j = 1, k = 0; i <= n - hei[1]; i++) { n     while (j <= n && fa[node[j]] == k) k = node[j++]; n     if (j > n) break; n     printf( %d  , node[j] - 1); n     k = fa[k]; n   } n } n


#include <bits/stdc++.h> n using namespace std; n char a[1005], b[1005]; n int main() { n   int s1 = 0, s2 = 0; n   scanf( %s %s , &a, &b); n   int len1 = strlen(a); n   int len2 = strlen(b); n   for (int i = 0; i < len1; i++) n     if (a[i] ==  1 ) s1++; n   for (int i = 0; i < len2; i++) n     if (b[i] ==  1 ) s2++; n   if (s1 & 1) s1++; n   if (s1 >= s2) n     puts( YES ); n   else n     puts( NO ); n } n


#include <bits/stdc++.h> n using namespace std; n int n, Q, x, y, z, kk; n int dis[500005], head[500005], father[500005], top[500005], q[500005]; n bool vis[500005]; n int far[500005], heavyson[500005], rak[500005], sum[500005], leaf[500005], ans, n     d[500005]; n struct Tree { n   int nxt, to, step; n } e[500005]; n inline bool cmp(int u, int v) { n   return dis[u] - dis[father[top[u]]] > dis[v] - dis[father[top[v]]]; n } n inline void link(int x, int y, int z) { n   e[++kk].nxt = head[x]; n   e[kk].to = y; n   e[kk].step = z; n   head[x] = kk; n } n inline int bfs(int S) { n   int left1 = 1; n   int right1 = 1; n   q[left1] = S; n   memset(d, 0, sizeof(d)); n   memset(vis, false, sizeof(vis)); n   vis[S] = true; n   while (left1 <= right1) { n     int u = q[left1++]; n     for (int i = head[u]; i; i = e[i].nxt) { n       int v = e[i].to; n       if (vis[v]) continue; n       vis[v] = true; n       q[++right1] = v; n       d[v] = d[u] + e[i].step; n     } n   } n   int kkk = 0; n   for (int i = 1; i <= n; i++) { n     if (d[i] > d[kkk]) kkk = i; n   } n   return kkk; n } n void dfs1(int u, int fa) { n   far[u] = dis[u]; n   for (int i = head[u]; i; i = e[i].nxt) { n     int v = e[i].to; n     if (v == fa) continue; n     father[v] = u; n     dis[v] = dis[u] + e[i].step; n     dfs1(v, u); n     if (!heavyson[u] || far[u] < far[v]) heavyson[u] = v, far[u] = far[v]; n   } n } n void dfs2(int u, int first) { n   top[u] = first; n   if (!heavyson[u]) { n     leaf[++leaf[0]] = u; n     return; n   } n   dfs2(heavyson[u], first); n   for (int i = head[u]; i; i = e[i].nxt) { n     int v = e[i].to; n     if (v == father[u] || v == heavyson[u]) continue; n     dfs2(v, v); n   } n } n inline int solve(int x, int y) { n   if (rak[top[x]] <= 2 * y - 1) return sum[min(y * 2 - 1, leaf[0])]; n   int u = x; n   while (rak[top[u]] > y * 2 - 1) u = father[top[u]]; n   return sum[y * 2 - 1] - n          min(min(sum[y * 2 - 1] - sum[y * 2 - 2], far[u] - dis[u]), dis[u]) + n          far[x] - dis[u]; n } n int main() { n   scanf( %d%d , &n, &Q); n   for (int i = 1; i < n; i++) { n     scanf( %d%d%d , &x, &y, &z); n     link(x, y, z); n     link(y, x, z); n   } n   int first = bfs(1); n   int root = bfs(first); n   dfs1(root, -1); n   dfs2(root, root); n   sort(leaf + 1, leaf + leaf[0] + 1, cmp); n   for (int i = 1; i <= leaf[0]; i++) { n     rak[top[leaf[i]]] = i; n     sum[i] = sum[i - 1] + dis[leaf[i]] - dis[father[top[leaf[i]]]]; n   } n   while (Q--) { n     scanf( %d%d , &x, &y); n     x = ((x + ans - 1) % n) + 1; n     y = ((y + ans - 1) % n) + 1; n     printf( %d n , ans = solve(x, y)); n   } n   return 0; n } n


#include<bits/stdc++.h> n  n #define For(i, x, n) for(int i = x; i < n; i++) n #define ROF(i,x,n) for(int i = n - 1; i >= x; i--) n  n #define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0) n #define F first n #define S second n #define pb push_back n #define po pop_back n #define mp make_pair n #define En endl n ///#define endl   n  n  n #define ALL(x) x.begin(), x.end() n  n using namespace std; n  n typedef double db; n typedef long long LL; n typedef string str; n typedef pair<int, int> PII; n typedef pair<LL, LL> PLL; n  n typedef vector<int> VI; n typedef vector<LL> VLL; n typedef vector<bool> VB; n typedef vector<char> VC; n typedef vector<PII> VPII; n typedef vector<string> VSTR; n typedef set<int> SI; n typedef set<LL> SLL; n typedef set<PII> SPII; n  n typedef const int CI; n typedef const long long CLL; n  n /// CI MAX_N = ; n CI MOD = 1e9 + 7; n  n int t, n, a; n  n int main () { n     IOS; n     cin >> t; n     while (t--) { n         cin >> n; n         int sumz = 0; n         int sumf = 0; n         For (i, 0, 2 * n) { n             cin >> a; n             if (a % 2 == 0) sumz++; n             else sumf++; n         } n         if (sumz == sumf) cout <<  YES  << En; n         else cout <<  NO  << En; n     } n } n  n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 200010; n int n, m, X, a[maxn], b[maxn], c[maxn], d[maxn], tr[maxn]; n long long ans; n vector<int> M[maxn]; n vector<pair<int, int>> Q[maxn]; n void add(int p) { n   for (; p <= X; p += p & -p) tr[p]++; n } n int sum(int p) { n   int s = 0; n   for (; p; p -= p & -p) s += tr[p]; n   return s; n } n int main() { n   scanf( %d %d %d , &n, &m, &X); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]), a[i] = min(a[i], X); n   } n   for (int i = 1; i <= m; i++) { n     scanf( %d , &b[i]), b[i] = min(b[i], X); n   } n   auto init = [&](int n, int *a, int *c) { n     static int mx[20][maxn], lb[maxn], rb[maxn]; n     for (int i = 1; i <= n; i++) { n       mx[0][i] = a[i], lb[i] = rb[i] = 0; n     } n     for (int i = 1; i < 20; i++) { n       for (int j = 1; j + (1 << i) - 1 <= n; j++) { n         mx[i][j] = max(mx[i - 1][j], mx[i - 1][j + (1 << (i - 1))]); n       } n     } n     auto query = [&](int l, int r) { n       int k = log2(r - l + 1); n       return max(mx[k][l], mx[k][r - (1 << k) + 1]); n     }; n     stack<int> st; n     for (int i = 1; i <= n; i++) { n       while (!st.empty() && a[st.top()] > a[i]) rb[st.top()] = i, st.pop(); n       if (!st.empty()) lb[i] = st.top(); n       st.push(i); n     } n     for (int i = 1; i <= n; i++) { n       c[i] = min(!lb[i] ? X : query(lb[i], i), !rb[i] ? X : query(i, rb[i])); n     } n   }; n   init(n, a, c), init(m, b, d); n   for (int i = 1; i <= n; i++) { n     Q[X - c[i]].emplace_back(a[i], -1); n     Q[X - a[i]].emplace_back(a[i], 1); n   } n   for (int i = 1; i <= m; i++) { n     M[b[i]].push_back(X - d[i] + 1); n   } n   for (int i = 0; i < X; i++) { n     for (int d : M[i]) add(d); n     for (auto p : Q[i]) ans += p.second * sum(p.first); n   } n   printf( %lld n , ans); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a = 1023, b = 0, n, e; n   char op; n   cin >> n; n   while (n--) { n     cin >> op >> e; n     if (op ==  | ) { n       a |= e; n       b |= e; n     } else if (op ==  & ) { n       a &= e; n       b &= e; n     } else if (op ==  ^ ) { n       a ^= e; n       b ^= e; n     } n   } n   int ouxor = 0; n   int ou = 0; n   int exor = 0; n   e = 0; n   for (int i = 0; i < 10; i++) { n     int num = 1 << i; n     if ((a & num) && (b & num)) n       ou |= num; n     else if ((b & num)) n       exor |= num; n     else if (!(a & num) && !(b & num)) n       ouxor |= num; n   } n   int count = 0; n   count += ouxor ? 2 : 0; n   count += ou ? 1 : 0; n   count += exor ? 1 : 0; n   cout << count << endl; n   if (ou) cout <<  |   << ou << endl; n   if (ouxor) { n     cout <<  |   << ouxor << endl; n     cout <<  ^   << ouxor << endl; n   } n   if (exor) cout <<  ^   << exor << endl; n } n


#include <bits/stdc++.h> n int main() { n   int t; n   scanf( %d , &t); n   int a, b, c, d, k, x, y; n   while (t--) { n     scanf( %d %d %d %d %d , &a, &b, &c, &d, &k); n     if (a % c == 0) { n       x = a / c; n     } else { n       x = a / c + 1; n     } n     if (b % d == 0) { n       y = b / d; n     } else { n       y = b / d + 1; n     } n     if (x + y > k) { n       printf( -1 n ); n     } else n       printf( %d %d n , k - y, y); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = (long long)1e15 + 696969; n long long n, k; n long long a[1LL << 20], b[1LL << 20]; n long long opt[20][1LL << 20]; n signed main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   memset(opt, -1, sizeof opt); n   cin >> n; n   for (long long i = 1; i < n; i <<= 1) k++; n   long long ok = false; n   for (long long i = 0; i < n; i++) { n     long long x; n     cin >> x; n     if (x == -1) n       ok = true; n     else if (!ok) n       a[i] = 0; n     else n       a[i - 1] = x; n   } n   n--; n   reverse(a, a + n); n   for (long long i = k - 1, j = 0; i > 0; i--) { n     j += 1 << i; n     b[i] = j; n   } n   for (long long i = 0; i < n; i++) { n     opt[0][i] = a[i]; n   } n   for (long long i = 1; i < k; i++) { n     long long mn = INF; n     for (long long j = n - 1; j >= 0; j--) { n       if (j > b[i]) n         opt[i][j] = INF; n       else { n         opt[i][j] = mn + a[j]; n         mn = min(mn, opt[i - 1][j]); n       } n     } n   } n   cout << opt[k - 1][0] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     long long x1, y1, z1; n     long long x2, y2, z2; n     cin >> x1 >> y1 >> z1; n     cin >> x2 >> y2 >> z2; n     long long sum = 0; n     if (z2 > x1) { n       z2 -= x1; n       if (z2 > z1) { n         z2 -= z1; n         sum -= (2 * z2); n       } else { n         z1 -= z2; n         sum += 2 * min(z1, y2); n       } n     } else { n       sum += 2 * min(z1, y2); n     } n     cout << sum << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename Arg1> n void __f(const char* name, Arg1&& arg1) { n   cerr << name <<   :   << arg1 << std::endl; n } n template <typename Arg1, typename... Args> n void __f(const char* names, Arg1&& arg1, Args&&... args) { n   const char* comma = strchr(names + 1,  , ); n   cerr.write(names, comma - names) <<   :   << arg1 <<   |  ; n   __f(comma + 1, args...); n } n const int N = 1510; n int DP[27][N][N]; n int ans[27][N]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n; n   cin >> n; n   string s; n   cin >> s; n   memset(DP, 0, sizeof(DP)); n   memset(ans, 0, sizeof(ans)); n   DP[s[0] -  a ][0][0] = 1; n   for (int i = (int)1; i < (int)N; i++) DP[s[0] -  a ][0][i] = 1; n   for (char ch =  a ; ch <=  z ; ch++) { n     if (s[0] == ch) continue; n     for (int i = (int)1; i < (int)N; i++) DP[ch -  a ][0][i] = 1; n   } n   for (int i = (int)1; i < (int)(int)(s.size()); i++) { n     for (char ch =  a ; ch <=  z ; ch++) { n       if (s[i] == ch) { n         int len = 0; n         for (int k = i; k >= 0; k--) { n           if (s[k] != ch) break; n           len++; n         } n         DP[ch -  a ][i][0] = len; n       } n       for (int j = (int)1; j < (int)N; j++) { n         if (ch == s[i]) n           DP[ch -  a ][i][j] = 1 + DP[ch -  a ][i - 1][j]; n         else n           DP[ch -  a ][i][j] = 1 + DP[ch -  a ][i - 1][j - 1]; n       } n     } n   } n   for (char ch =  a ; ch <=  z ; ch++) { n     for (int j = 0; j < N; j++) { n       for (int i = (int)0; i < (int)(int)(s.size()); i++) n         ans[ch -  a ][j] = max(ans[ch -  a ][j], DP[ch -  a ][i][j]); n     } n   } n   int q; n   cin >> q; n   for (int i = (int)0; i < (int)q; i++) { n     int m; n     char ch; n     cin >> m >> ch; n     cout << ans[ch -  a ][m] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = INT_MAX; n const long long INFL = LLONG_MAX; n int N, ans, P[100100], ord[100100]; n int comp(int a, int b) { return P[a] > P[b]; } n int main() { n   ios_base::sync_with_stdio(0); n   cout.precision(15); n   cin >> N; n   for (int(i) = 1; (i) <= (N); (i)++) { n     cin >> P[i]; n     ord[i] = i; n   } n   sort(ord + 1, ord + 1 + N, comp); n   cout << ord[1] <<     << P[ord[2]] <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   vector<int> a(n); n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   int inv = 0; n   for (int i = 1; i < n; i++) { n     for (int j = 0; j < i; j++) { n       if (a[i] < a[j]) { n         inv++; n       } n     } n   } n   inv %= 2; n   int m; n   cin >> m; n   for (int i = 0; i < m; i++) { n     int l, r; n     cin >> l >> r; n     int len = r - l + 1; n     int di = (len * (len - 1)) / 2; n     inv += di; n     inv %= 2; n     if (inv == 1) { n       cout <<  odd  << endl; n     } else { n       cout <<  even  << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n const int MAXN = 23; n int parseInt() { n   int d; n   scanf( %d , &d); n   ; n   return d; n } n int n; n int a[MAXN], b[MAXN]; n vector<pair<int, int> > v; n map<int, int> rang; n int main() { n   cin >> n; n   for (int i = 0; i < n; ++i) { n     a[i] = parseInt(); n     v.push_back(make_pair(a[i], i)); n   } n   sort(v.begin(), v.end()); n   for (int i = 0; i < n; ++i) { n     rang[v[i].first] = i; n     b[i] = v[i].first; n   } n   int rng; n   for (int i = 0; i < n; ++i) { n     rng = rang[a[i]]; n     rng = (rng + 1) % n; n     cout << b[rng] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int P = 1e9 + 7, INF = 0x3f3f3f3f; n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n long long qpow(long long a, long long n) { n   long long r = 1 % P; n   for (a %= P; n; a = a * a % P, n >>= 1) n     if (n & 1) r = r * a % P; n   return r; n } n long long inv(long long first) { n   return first <= 1 ? 1 : inv(P % first) * (P - P / first) % P; n } n inline int rd() { n   int first = 0; n   char p = getchar(); n   while (p <  0  || p >  9 ) p = getchar(); n   while (p >=  0  && p <=  9 ) first = first * 10 + p -  0 , p = getchar(); n   return first; n } n const int N = 1e6 + 10; n int n, a, b; n struct _ { n   long long a, b, c; n   void pr() { printf( a=%lld,b=%lld n , a, b); } n   bool operator<(const _& rhs) const { return c > rhs.c; } n } e[N]; n long long ans; n int vis[N]; n int main() { n   scanf( %d%d%d , &n, &a, &b); n   for (int i = 1; i <= n; ++i) { n     scanf( %lld%lld , &e[i].a, &e[i].b); n     e[i].c = e[i].a - e[i].b; n   } n   for (int i = 1; i <= n; ++i) ans += e[i].b; n   if (!b) return printf( %lld n , ans), 0; n   if (b == 1) { n     long long t = 0; n     for (int i = 1; i <= n; ++i) t = max(t, (e[i].a << a) - e[i].b); n     return printf( %lld n , ans + t), 0; n   } n   sort(e + 1, e + 1 + n); n   for (int i = 1; i <= n; ++i) n     if (e[i].a > e[i].b) vis[i] = 1; n   int cnt = 0; n   for (int i = 1; i <= b; ++i) n     if (vis[i]) ans += e[i].a - e[i].b, ++cnt; n   long long t = 0; n   for (int i = 1; i <= b; ++i) { n     if (vis[i]) n       t = max(t, (e[i].a << a) - e[i].a); n     else n       t = max(t, (e[i].a << a) - e[i].b); n   } n   long long ret = ans + t; n   t = 0; n   for (int i = b + 1; i <= n; ++i) t = max(t, (e[i].a << a) - e[i].b); n   if (cnt < b) n     ret = max(ret, ans + t); n   else n     ret = max(ret, ans + t - e[b].a + e[b].b); n   printf( %lld n , ret); n } n


#include <bits/stdc++.h> n using namespace std; n int Length[1001], n, x, a[1001], Concern, Pos, Ppl; n vector<int> Queues, adj[1001], Ans; n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   cin >> n >> x; n   for (int i = 1; i <= n; ++i) { n     cin >> a[i]; n     if (a[i]) adj[a[i]].push_back(i); n   } n   for (int i = 1; i <= n; ++i) { n     if (a[i] == 0) { n       Ppl = i; n       int len = 0; n       while (Ppl) { n         ++len; n         if (Ppl == x) Concern = i, Pos = len; n         if (adj[Ppl].size()) n           Ppl = adj[Ppl][0]; n         else n           Ppl = 0; n       } n       if (Concern != i) Queues.push_back(len); n     } n   } n   for (int i = Queues.size() - 1; i >= 0; --i) { n     vector<int> Add; n     for (int j = 0; j < Ans.size(); ++j) { n       if (!Length[Ans[j] + Queues[i]]) n         Add.push_back(Ans[j] + Queues[i]), Length[Ans[j] + Queues[i]] = 1; n     } n     for (int j = 0; j < Add.size(); ++j) Ans.push_back(Add[j]); n     if (!Length[Queues[i]]) Ans.push_back(Queues[i]), Length[Queues[i]] = 1; n   } n   cout << Pos <<   n ; n   sort(Ans.begin(), Ans.end()); n   for (int i = 0; i < Ans.size(); ++i) cout << Pos + Ans[i] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int Min = -10000000; n int Max = -Min; n int A[8] = {1, 1, 0, 0, 1, 1, 1, 1}; n int B[8] = {0, 0, 1, 1, 1, 1, -1, -1}; n int C[8]; n struct Nodes { n   int x, y; n } p[100010]; n bool operator<(Nodes &p1, Nodes p2) { n   if (p1.x == p2.x) return p1.y < p2.y; n   return p1.x < p2.x; n } n long long cross(long long x1, long long y1, long long x2, long long y2) { n   return x1 * y2 - y1 * x2; n } n int main() { n   int i, j, k; n   int n; n   long long a, b, c, res; n   while (scanf( %d , &n) != EOF) { n     for (i = 0; i < 8; i++) { n       if (i & 1) { n         C[i] = Max; n       } else n         C[i] = Min; n     } n     for (i = 1; i <= n; i++) { n       scanf( %d%d , &p[i].x, &p[i].y); n       C[0] = max(C[0], p[i].x); n       C[1] = min(C[1], p[i].x); n       C[2] = max(C[2], p[i].y); n       C[3] = min(C[3], p[i].y); n       C[4] = max(C[4], p[i].x + p[i].y); n       C[5] = min(C[5], p[i].x + p[i].y); n       C[6] = max(C[6], p[i].x - p[i].y); n       C[7] = min(C[7], p[i].x - p[i].y); n     } n     res = 0; n     for (i = 0; i < 8; i++) { n       Nodes p1, p2; n       p1.x = p1.y = Min; n       p2.x = p2.y = Max; n       for (j = 0; j < 8; j += 2) { n         Nodes tmp[2]; n         for (k = j; k < j + 2; k++) { n           c = cross(A[i], B[i], A[k], B[k]); n           a = cross(A[i], C[i], A[k], C[k]); n           b = cross(C[i], B[i], C[k], B[k]); n           if (c == 0) break; n           if (b % c || a % c) break; n           tmp[k - j].x = b / c; n           tmp[k - j].y = a / c; n         } n         if (k < j + 2) continue; n         if (tmp[1] < tmp[0]) swap(tmp[0], tmp[1]); n         if (p1 < tmp[0]) p1 = tmp[0]; n         if (tmp[1] < p2) p2 = tmp[1]; n       } n       res += max(abs(p2.x - p1.x), abs(p2.y - p1.y)); n     } n     cout << res + 4 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5e4 + 2; n int head[N], grp_edge[N][30], max1[N][30], grp[N], level[N], okfine = 0, sum[N], n                                                              dem = 0; n char edge[N]; n bool cac = true; n vector<pair<int, char> > adj[N]; n vector<int> adj_grp[N]; n stack<pair<int, char> > lis; n void dfs(int x) { n   int i = 0, j; n   if (adj[x].size() == 0) { n     if (!okfine) { n       okfine = level[x]; n     } n     if (okfine != level[x]) { n       cac = false; n     } n     while (lis.size()) { n       i = lis.top().first; n       if (lis.top().second !=  ? ) { n         grp_edge[x][(int)(lis.top().second -  a  + 1)]++; n       } n       grp[i] = x; n       lis.pop(); n     } n     if (i) { n       adj_grp[head[i]].push_back(x); n       head[x] = head[i]; n     } n   } n   if (adj[x].size() == 1) { n     level[adj[x][0].first] = level[x] + 1; n     lis.push({adj[x][0].first, edge[adj[x][0].first]}); n     dfs(adj[x][0].first); n   } n   if (adj[x].size() == 2) { n     while (lis.size()) { n       i = lis.top().first; n       if (lis.top().second !=  ? ) { n         grp_edge[x][(int)(lis.top().second -  a  + 1)]++; n       } n       grp[i] = x; n       lis.pop(); n     } n     if (i) { n       adj_grp[head[i]].push_back(x); n       head[x] = head[i]; n     } n     level[adj[x][0].first] = level[x] + 1; n     lis.push({adj[x][0].first, edge[adj[x][0].first]}); n     dfs(adj[x][0].first); n     level[adj[x][1].first] = level[x] + 1; n     lis.push({adj[x][1].first, edge[adj[x][1].first]}); n     dfs(adj[x][1].first); n   } n } n void dfs1(int x) { n   int i, j; n   for (i = 0; i < adj_grp[x].size(); i++) { n     dfs1(adj_grp[x][i]); n     for (j = 1; j <= 26; j++) { n       max1[x][j] = n           max(max1[x][j], max1[adj_grp[x][i]][j] + grp_edge[adj_grp[x][i]][j]); n     } n   } n   for (j = 1; j <= 26; j++) { n     sum[x] += max1[x][j]; n   } n   if (sum[x] > okfine - level[x]) { n     dem++; n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n, i, j, k, l, q, ans, ans1; n   char x, x1; n   cin >> n >> q; n   for (i = 2; i <= n; i++) { n     cin >> head[i] >> edge[i]; n     adj[head[i]].push_back({i, edge[i]}); n   } n   dfs(1); n   dfs1(1); n   head[1] = 0; n   for (i = 1; i <= q; i++) { n     if (!cac) { n       cout <<  Fou n ; n       continue; n     } n     cin >> j >> x; n     x1 = edge[j]; n     if (edge[j] !=  ? ) { n       grp_edge[grp[j]][(int)(edge[j] -  a  + 1)]--; n     } n     edge[j] = x; n     if (edge[j] !=  ? ) { n       grp_edge[grp[j]][(int)(edge[j] -  a  + 1)]++; n     } n     k = grp[j]; n     while (k != 1) { n       if (adj_grp[head[k]].size() == 2) { n         if (sum[head[k]] > okfine - level[head[k]]) { n           if (x1 !=  ? ) { n             sum[head[k]] -= max1[head[k]][(int)(x1 -  a  + 1)]; n             max1[head[k]][(int)(x1 -  a  + 1)] = n                 max(max1[adj_grp[head[k]][0]][(int)(x1 -  a  + 1)] + n                         grp_edge[adj_grp[head[k]][0]][(int)(x1 -  a  + 1)], n                     max1[adj_grp[head[k]][1]][(int)(x1 -  a  + 1)] + n                         grp_edge[adj_grp[head[k]][1]][(int)(x1 -  a  + 1)]); n             sum[head[k]] += max1[head[k]][(int)(x1 -  a  + 1)]; n           } n           if (x !=  ? ) { n             sum[head[k]] -= max1[head[k]][(int)(x -  a  + 1)]; n             max1[head[k]][(int)(x -  a  + 1)] = n                 max(max1[adj_grp[head[k]][0]][(int)(x -  a  + 1)] + n                         grp_edge[adj_grp[head[k]][0]][(int)(x -  a  + 1)], n                     max1[adj_grp[head[k]][1]][(int)(x -  a  + 1)] + n                         grp_edge[adj_grp[head[k]][1]][(int)(x -  a  + 1)]); n             sum[head[k]] += max1[head[k]][(int)(x -  a  + 1)]; n           } n           if (sum[head[k]] <= okfine - level[head[k]]) { n             dem--; n           } n         } else { n           if (x1 !=  ? ) { n             sum[head[k]] -= max1[head[k]][(int)(x1 -  a  + 1)]; n             max1[head[k]][(int)(x1 -  a  + 1)] = n                 max(max1[adj_grp[head[k]][0]][(int)(x1 -  a  + 1)] + n                         grp_edge[adj_grp[head[k]][0]][(int)(x1 -  a  + 1)], n                     max1[adj_grp[head[k]][1]][(int)(x1 -  a  + 1)] + n                         grp_edge[adj_grp[head[k]][1]][(int)(x1 -  a  + 1)]); n             sum[head[k]] += max1[head[k]][(int)(x1 -  a  + 1)]; n           } n           if (x !=  ? ) { n             sum[head[k]] -= max1[head[k]][(int)(x -  a  + 1)]; n             max1[head[k]][(int)(x -  a  + 1)] = n                 max(max1[adj_grp[head[k]][0]][(int)(x -  a  + 1)] + n                         grp_edge[adj_grp[head[k]][0]][(int)(x -  a  + 1)], n                     max1[adj_grp[head[k]][1]][(int)(x -  a  + 1)] + n                         grp_edge[adj_grp[head[k]][1]][(int)(x -  a  + 1)]); n             sum[head[k]] += max1[head[k]][(int)(x -  a  + 1)]; n           } n           if (sum[head[k]] > okfine - level[head[k]]) { n             dem++; n           } n         } n       } else { n         if (sum[head[k]] > okfine - level[head[k]]) { n           if (x1 !=  ? ) { n             sum[head[k]] -= max1[head[k]][(int)(x1 -  a  + 1)]; n             max1[head[k]][(int)(x1 -  a  + 1)] = n                 max1[adj_grp[head[k]][0]][(int)(x1 -  a  + 1)] + n                 grp_edge[adj_grp[head[k]][0]][(int)(x1 -  a  + 1)]; n             sum[head[k]] += max1[head[k]][(int)(x1 -  a  + 1)]; n           } n           if (x !=  ? ) { n             sum[head[k]] -= max1[head[k]][(int)(x -  a  + 1)]; n             max1[head[k]][(int)(x -  a  + 1)] = n                 max1[adj_grp[head[k]][0]][(int)(x -  a  + 1)] + n                 grp_edge[adj_grp[head[k]][0]][(int)(x -  a  + 1)]; n             sum[head[k]] += max1[head[k]][(int)(x -  a  + 1)]; n           } n           if (sum[head[k]] <= okfine - level[head[k]]) { n             dem--; n           } n         } else { n           if (x1 !=  ? ) { n             sum[head[k]] -= max1[head[k]][(int)(x1 -  a  + 1)]; n             max1[head[k]][(int)(x1 -  a  + 1)] = n                 max1[adj_grp[head[k]][0]][(int)(x1 -  a  + 1)] + n                 grp_edge[adj_grp[head[k]][0]][(int)(x1 -  a  + 1)]; n             sum[head[k]] += max1[head[k]][(int)(x1 -  a  + 1)]; n           } n           if (x !=  ? ) { n             sum[head[k]] -= max1[head[k]][(int)(x -  a  + 1)]; n             max1[head[k]][(int)(x -  a  + 1)] = n                 max1[adj_grp[head[k]][0]][(int)(x -  a  + 1)] + n                 grp_edge[adj_grp[head[k]][0]][(int)(x -  a  + 1)]; n             sum[head[k]] += max1[head[k]][(int)(x -  a  + 1)]; n           } n           if (sum[head[k]] > okfine - level[head[k]]) { n             dem++; n           } n         } n       } n       k = head[k]; n     } n     if (dem) { n       cout <<  Fou n ; n     } else { n       ans = 0; n       ans1 = 0; n       for (j = 1; j <= 26; j++) { n         ans += max1[1][j] * j; n         ans1 += max1[1][j]; n       } n       cout <<  Shi   << ans + (okfine - ans1) * 27 * 13 << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   while (scanf( %d , &n) == 1) { n     string s; n     cin >> s; n     s =  #  + s; n     bool used[n + 1]; n     memset(used, 0, sizeof used); n     int d[n + 1]; n     for (int i = 1; i <= n; i++) { n       scanf( %d , d + i); n     } n     int pos = 1; n     bool ok = false; n     while (!used[pos]) { n       used[pos] = true; n       if (s[pos] ==  < ) { n         pos -= d[pos]; n       } else { n         pos += d[pos]; n       } n       if (pos <= 0 || pos > n) { n         ok = true; n         break; n       } n     } n     if (ok) { n       puts( FINITE ); n     } else { n       puts( INFINITE ); n     } n   } n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,tune=native ) n #pragma GCC optimize( unroll-loops ) n using namespace std; n template <typename T, typename U> n inline ostream &operator<<(ostream &_out, const pair<T, U> &_p) { n   _out << _p.first <<     << _p.second; n   return _out; n } n template <typename T, typename U> n inline istream &operator>>(istream &_in, pair<T, U> &_p) { n   _in >> _p.first >> _p.second; n   return _in; n } n template <typename T> n inline ostream &operator<<(ostream &_out, const vector<T> &_v) { n   if (_v.empty()) { n     return _out; n   } n   _out << _v.front(); n   for (auto _it = ++_v.begin(); _it != _v.end(); ++_it) { n     _out <<     << *_it; n   } n   return _out; n } n template <typename T> n inline istream &operator>>(istream &_in, vector<T> &_v) { n   for (auto &_i : _v) { n     _in >> _i; n   } n   return _in; n } n const long long MAXN = 3604; n const long long MAXM = MAXN / 2; n const long long INF = 1e9; n const long long MOD = 998244353; n long long sum(long long a, long long b) { n   if (a + b >= MOD) { n     return a + b - MOD; n   } n   return a + b; n } n long long mul(long long a, long long b) { n   if (a * b >= MOD) { n     return (a * b) % MOD; n   } n   return a * b; n } n long long sqr(long long a) { return (a * a) % MOD; } n long long bin_pow(long long a, long long b) { n   if (b == 0) { n     return 1; n   } n   if (b % 2) { n     return mul(a, bin_pow(a, b - 1)); n   } n   return sqr(bin_pow(a, b / 2)); n } n long long fact[MAXN]; n long long rfact[MAXN]; n long long dp[2][MAXM][MAXN]; n bool connected[MAXN]; n long long dp1[MAXM]; n long long dp2[MAXN][MAXM]; n inline long long c(long long n, long long k) { n   return mul(fact[n], mul(rfact[k], rfact[n - k])); n } n class FSbalansirovannieRaspolozheniyaDomino { n  public: n   void solve(std::istream &in, std::ostream &out) { n     fact[0] = 1; n     for (long long i = 1; i < MAXN; i++) { n       fact[i] = mul(i, fact[i - 1]); n     } n     rfact[MAXN - 1] = bin_pow(fact[MAXN - 1], MOD - 2); n     for (long long j = MAXN - 1; j >= 1; j--) { n       rfact[j - 1] = mul(rfact[j], j); n     } n     long long h, w; n     in >> h >> w; n     long long n; n     in >> n; n     vector<pair<pair<long long, long long>, pair<long long, long long>>> a(n); n     in >> a; n     { n       for (long long i = 0; i <= h; i++) { n         for (long long j = 0; j < MAXM; j++) { n           dp2[i][j] = 0; n           dp[0][j][i] = 0; n         } n         connected[i] = 0; n       } n       for (long long j = 0; j < MAXM; j++) { n         dp1[j] = 0; n       } n       for (auto v : a) { n         connected[v.first.first - 1] = 1; n         connected[v.second.first - 1] = 1; n       } n       long long counts = 0; n       for (long long j = 0; j < h; j++) { n         counts += !connected[j]; n       } n       dp1[0] = 1; n       for (long long i = 0; i <= h; i++) { n         for (long long j = 0; j < MAXM; j++) { n           dp1[j] = sum(dp1[j], dp2[i][j]); n         } n         if (i + 1 < h && connected[i] == 0 && connected[i + 1] == 0) { n           for (long long j = 0; j + 1 < MAXM; j++) { n             dp2[i + 2][j + 1] = dp1[j]; n           } n         } n       } n       for (long long i = 0; i < MAXM; i++) { n         for (long long j = 0; j <= counts - 2 * i; j++) { n           dp[0][i][j] = n               mul(dp1[i], mul(fact[counts - 2 * i], n                               mul(fact[i], rfact[counts - 2 * i - j]))); n         } n       } n     } n     { n       for (long long i = 0; i <= w; i++) { n         for (long long j = 0; j < MAXM; j++) { n           dp2[i][j] = 0; n           dp[1][j][i] = 0; n         } n         connected[i] = 0; n       } n       for (long long j = 0; j < MAXM; j++) { n         dp1[j] = 0; n       } n       for (auto v : a) { n         connected[v.first.second - 1] = 1; n         connected[v.second.second - 1] = 1; n       } n       long long counts = 0; n       for (long long j = 0; j < w; j++) { n         counts += !connected[j]; n       } n       dp1[0] = 1; n       for (long long i = 0; i <= w; i++) { n         for (long long j = 0; j < MAXM; j++) { n           dp1[j] = sum(dp1[j], dp2[i][j]); n         } n         if (i + 1 < w && connected[i] == 0 && connected[i + 1] == 0) { n           for (long long j = 0; j + 1 < MAXM; j++) { n             dp2[i + 2][j + 1] = dp1[j]; n           } n         } n       } n       for (long long i = 0; i < MAXM; i++) { n         for (long long j = 0; j <= counts - 2 * i; j++) { n           dp[1][i][j] = mul(dp1[i], c(counts - 2 * i, j)); n         } n       } n     } n     long long ans = 0; n     for (long long i = 0; 2 * i <= h; i++) { n       for (long long j = 0; 2 * j <= w; j++) { n         ans += dp[0][i][j] * dp[1][j][i]; n         if (ans >= MOD * MOD) { n           ans -= MOD * MOD; n         } n       } n     } n     ans %= MOD; n     out << ans; n   } n }; n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   FSbalansirovannieRaspolozheniyaDomino solver; n   std::istream &in(std::cin); n   std::ostream &out(std::cout); n   solver.solve(in, out); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char _; n int num_rectangles; n pair<pair<int, int>, pair<int, int> > rectangles[135000], pre[135000], n     suff[135000]; n pair<pair<int, int>, pair<int, int> > intersection( n     pair<pair<int, int>, pair<int, int> > first, n     pair<pair<int, int>, pair<int, int> > second) { n   if (first.first.first == INT_MIN || second.first.first == INT_MIN) n     return {{INT_MIN, INT_MIN}, {INT_MIN, INT_MIN}}; n   int tx = max(first.first.first, second.first.first), n       bx = min(first.second.first, second.second.first); n   int ty = max(first.first.second, second.first.second), n       by = min(first.second.second, second.second.second); n   if (bx < tx || by < ty) return {{INT_MIN, INT_MIN}, {INT_MIN, INT_MIN}}; n   return {{tx, ty}, {bx, by}}; n } n int main() { n   scanf( %d , &num_rectangles); n   for (int i = 0; i < num_rectangles; i++) n     scanf(  %d %d %d %d , &rectangles[i].first.first, n           &rectangles[i].first.second, &rectangles[i].second.first, n           &rectangles[i].second.second); n   sort(rectangles, rectangles + num_rectangles); n   pre[0] = rectangles[0]; n   suff[num_rectangles - 1] = rectangles[num_rectangles - 1]; n   for (int i = 1; i < num_rectangles; i++) { n     pre[i] = intersection(pre[i - 1], rectangles[i]); n     suff[num_rectangles - i - 1] = intersection( n         suff[num_rectangles - i], rectangles[num_rectangles - i - 1]); n   } n   for (int i = 0; i < num_rectangles; i++) { n     if (i == 0) { n       if (suff[1].first.first != INT_MIN) { n         printf( %d %d n , suff[1].first.first, suff[1].first.second); n         return 0; n       } n     } else if (i == num_rectangles - 1) { n       if (pre[num_rectangles - 2].first.first != INT_MIN) { n         printf( %d %d n , pre[num_rectangles - 2].first.first, n                pre[num_rectangles - 2].first.second); n         return 0; n       } n     } else { n       pair<pair<int, int>, pair<int, int> > first = pre[i - 1], n                                             second = suff[i + 1], n                                             intersect = n                                                 intersection(first, second); n       if (intersect.first.first != INT_MIN) { n         printf( %d %d n , intersect.first.first, intersect.first.second); n         return 0; n       } n     } n   } n   if (pre[num_rectangles - 1].first.first != INT_MIN) { n     printf( %d %d n , pre[num_rectangles - 1].first.first, n            pre[num_rectangles - 1].first.second); n     return 0; n   } n   printf( -1 n ); n } n