#include <bits/stdc++.h> n using namespace std; n struct sTree { n   int L, R; n   int pl, pr; n   long long sum_m; n   long long sum_r; n } tree[2000000]; n int treeSize; n int s[100009]; n int r[100009]; n int m[100009]; n int BuildTree(int L, int R) { n   int cur = treeSize++; n   tree[cur].L = L; n   tree[cur].R = R; n   tree[cur].sum_r = r[L]; n   if (L < R) { n     tree[cur].pl = BuildTree(L, (L + R) / 2); n     tree[cur].pr = BuildTree((L + R) / 2 + 1, R); n     tree[cur].sum_r = tree[tree[cur].pl].sum_r + tree[tree[cur].pr].sum_r; n   } n   return cur; n } n int Toggle(int i, int cur) { n   tree[treeSize] = tree[cur]; n   cur = treeSize; n   treeSize++; n   if (tree[cur].L == tree[cur].R) { n     tree[cur].sum_m = m[i]; n     tree[cur].sum_r = 0; n     return cur; n   } n   if (i <= tree[tree[cur].pl].R) n     tree[cur].pl = Toggle(i, tree[cur].pl); n   else n     tree[cur].pr = Toggle(i, tree[cur].pr); n   tree[cur].sum_m = tree[tree[cur].pl].sum_m + tree[tree[cur].pr].sum_m; n   tree[cur].sum_r = tree[tree[cur].pl].sum_r + tree[tree[cur].pr].sum_r; n   return cur; n } n pair<long long, long long> Get(int L, int R, int cur) { n   if (L <= tree[cur].L && R >= tree[cur].R) { n     return make_pair(tree[cur].sum_m, tree[cur].sum_r); n   } n   pair<long long, long long> ans = make_pair(0, 0); n   if (L <= tree[tree[cur].pl].R && tree[tree[cur].pl].L <= R) { n     pair<long long, long long> tmp = Get(L, R, tree[cur].pl); n     ans.first += tmp.first; n     ans.second += tmp.second; n   } n   if (L <= tree[tree[cur].pr].R && tree[tree[cur].pr].L <= R) { n     pair<long long, long long> tmp = Get(L, R, tree[cur].pr); n     ans.first += tmp.first; n     ans.second += tmp.second; n   } n   return ans; n } n int roots[100001]; n long long Calc(int L, int R, int time) { n   if (L == R) { n     return min((long long)m[L], s[L] + (long long)time * r[L]); n   } n   pair<long long, long long> tmp = Get(L, R, roots[time]); n   return tmp.first + (long long)time * tmp.second; n } n vector<int> ind[100001]; n int n; n int fwt[100001]; n int fwt_sum(int k) { n   int ans = 0; n   while (k) { n     ans += fwt[k]; n     k -= k & (-k); n   } n   return ans; n } n void fwt_add(int k, int d) { n   while (k <= n) { n     fwt[k] += d; n     k += k & (-k); n   } n } n int fwt_getKth(int k) { n   int ans = 0; n   for (int i = 18; i >= 0; i--) { n     if (ans + (1 << i) <= n && fwt[ans + (1 << i)] < k) { n       ans += (1 << i); n       k -= fwt[ans]; n     } n   } n   return ans + 1; n } n int lastTime[100001]; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d %d %d , &s[i], &m[i], &r[i]); n     if (m[i] == 0) r[i] = 0; n     if (r[i]) { n       ind[m[i] / r[i] + 1].push_back(i); n     } n   } n   roots[0] = BuildTree(1, n); n   for (int k = 1; k <= 100000; k++) { n     roots[k] = roots[k - 1]; n     for (int i = 0; i < ind[k].size(); i++) n       roots[k] = Toggle(ind[k][i], roots[k]); n   } n   for (int k = 1; k <= n; k++) { n     fwt_add(k, 1); n   } n   int Q; n   scanf( %d , &Q); n   while (Q--) { n     int time, L, R; n     scanf( %d %d %d , &time, &L, &R); n     long long ans = 0; n     int last, ind = L; n     while (ind <= R) { n       int cur = fwt_sum(ind); n       int next = fwt_getKth(cur + 1) - 1; n       cur = fwt_getKth(cur); n       ans += Calc(ind, min(R, next), min(time - lastTime[cur], 100000)); n       s[cur] = 0; n       if (cur >= L) fwt_add(cur, -1); n       last = lastTime[cur]; n       ind = next + 1; n     } n     printf( %I64d n , ans); n     fwt_add(L, 1); n     lastTime[L] = time; n     if (R + 1 <= n && fwt_sum(R + 1) == fwt_sum(R)) { n       lastTime[R + 1] = last; n       fwt_add(R + 1, 1); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n void rd(long long &x) { scanf( %lld , &x); } n void rd(long long &x, long long &y) { scanf( %lld%lld , &x, &y); } n void rd(long long &x, long long &y, long long &z) { n   scanf( %lld%lld%lld , &x, &y, &z); n } n void rd(long long &x, long long &y, long long &z, long long &s) { n   scanf( %lld%lld%lld%lld , &x, &y, &z, &s); n } n template <typename T> n ostream &operator<<(ostream &os, const vector<T> &v) { n   os <<  { ; n   for (auto it = v.begin(); it != v.end(); ++it) { n     if (it != v.begin()) os <<  ,  ; n     os << *it; n   } n   return os <<  } ; n } n template <typename T> n ostream &operator<<(ostream &os, const set<T> &v) { n   os <<  [ ; n   for (auto it = v.begin(); it != v.end(); ++it) { n     if (it != v.begin()) os <<  ,  ; n     os << *it; n   } n   return os <<  ] ; n } n void faltu() { cerr << endl; } n template <typename T> n void faltu(T a[], int n) { n   for (int i = 0; i < n; ++i) cerr << a[i] <<    ; n   cerr << endl; n } n template <typename T, typename... hello> n void faltu(T arg, const hello &...rest) { n   cerr << arg <<    ; n   faltu(rest...); n } n string s; n const long long N = 2e5 + 5; n long long n, m, cnt[N], mx[N][2]; n bool visit[N]; n pair<long long, long long> dp[N]; n pair<long long, long long> back(long long pos) { n   if (pos >= n) return make_pair(0LL, 0LL); n   if (visit[pos]) return dp[pos]; n   visit[pos] = 1; n   pair<long long, long long> &r = dp[pos]; n   r = back(pos + 1); n   if (mx[pos][0] == m) { n     pair<long long, long long> q = back(pos + m); n     q.first++; n     q.second += cnt[pos + m - 1]; n     if (pos) q.second -= cnt[pos - 1]; n     if (q.first > r.first || (q.first == r.first && q.second < r.second)) r = q; n   } n   return r; n } n int main() { n   rd(n); n   cin >> s; n   rd(m); n   for (long long i = 0; i < (n); i++) { n     cnt[i] = (s[i] ==  ? ); n     if (i) cnt[i] += cnt[i - 1]; n   } n   for (int i = n; i < N; i++) cnt[i] = cnt[i - 1]; n   for (int i = n - 1; i >= 0; i--) { n     if (s[i] ==  a ) { n       mx[i][0] = min(mx[i + 1][1] + 1, m); n       mx[i][1] = 0; n     } else if (s[i] ==  b ) { n       mx[i][1] = min(mx[i + 1][0] + 1, m); n       mx[i][0] = 0; n     } else { n       mx[i][0] = min(mx[i + 1][1] + 1, m); n       mx[i][1] = min(mx[i + 1][0] + 1, m); n     } n   } n   pair<long long, long long> p = back(0); n   cout << (long long)p.second; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int tc = 0; n long long a, b, c, d; n inline long long sum(long long i) { n   return -a * i + b * (d - 1) * i + i * (i - 1) / 2 * b * d; n } n inline long long calc(long long i) { n   if (i < 0) return 0; n   long long ans = 0; n   if (i < (c + 1) / d) { n     ans = min(ans, sum(i) - a + i * b); n   } else { n     i = (c + 1) / d; n     ans = min(ans, sum(i) - a + (i - 1) * b + (c + 1 % d > 0) * b); n     ans = min(ans, sum(i)); n     ans = min(ans, -a); n   } n   return ans; n } n inline void solve() { n   ++tc; n   scanf( %lld%lld%lld%lld , &a, &b, &c, &d); n   if (c < d) { n     if (b * c >= a) n       printf( %lld n , a); n     else n       puts( -1 ); n   } else { n     long long i = floor(-(-a + b * (d - 1) + b - 1.0 * b * d / 2) / (b * d)); n     i = max(i, 0ll); n     long long dmg = n         (-a + b * (d - 1) + b * d * ((c + 1) / d - 1) + ((c + 1) % d) * b); n     long long ans = 0; n     if (dmg < 0) { n       puts( -1 ); n     } else { n       ans = min(ans, calc(i - 1)); n       ans = min(ans, calc(i)); n       ans = min(ans, calc(i + 1)); n       ans = min(ans, calc(i + 2)); n       printf( %lld n , -ans); n     } n   } n   return; n } n int main() { n   int T; n   for (scanf( %d , &T); T--; solve()) n     ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int m, n; n int a[110][110], b[110][110]; n template <typename T> n inline void chkmax(T &x, T y) { n   x = max(x, y); n } n template <typename T> n inline void chkmin(T &x, T y) { n   x = min(x, y); n } n template <typename T> n inline void read(T &x) { n   T f = 1; n   x = 0; n   char c = getchar(); n   for (; !isdigit(c); c = getchar()) n     if (c ==  - ) f = -f; n   for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c -  0 ; n   x *= f; n } n int main() { n   read(m); n   read(n); n   memset(a, 255, sizeof(a)); n   for (int i = 1; i <= m; i++) { n     for (int j = 1; j <= n; j++) { n       read(b[i][j]); n       if (b[i][j] == 0) { n         for (int k = 1; k <= n; k++) a[i][k] = 0; n         for (int k = 1; k <= m; k++) a[k][j] = 0; n       } n     } n   } n   for (int i = 1; i <= m; i++) { n     for (int j = 1; j <= n; j++) { n       if (a[i][j] == -1) a[i][j] = 1; n     } n   } n   for (int i = 1; i <= m; i++) { n     for (int j = 1; j <= n; j++) { n       int res = 0; n       for (int k = 1; k <= n; k++) res |= a[i][k]; n       for (int k = 1; k <= m; k++) res |= a[k][j]; n       if (res != b[i][j]) { n         printf( NO n ); n         return 0; n       } n     } n   } n   printf( YES n ); n   for (int i = 1; i <= m; i++) { n     for (int j = 1; j < n; j++) { n       printf( %d  , a[i][j]); n     } n     printf( %d n , a[i][n]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct DT { n   int l, r, x; n }; n DT qr[300005]; n int n, m, ans[300005], tree[4 * 300005]; n void update(int nd, int b, int e, int l, int r, int v) { n   if (r < l || l < 1 || r > n) return; n   if (b > r || e < l) return; n   if (tree[nd] != 0 && b != e) { n     tree[2 * nd] = tree[nd]; n     tree[2 * nd + 1] = tree[nd]; n     tree[nd] = 0; n   } n   if (b >= l && e <= r) { n     tree[nd] = v; n     return; n   } n   int md = (b + e) / 2; n   update(2 * nd, b, md, l, r, v); n   update(2 * nd + 1, md + 1, e, l, r, v); n } n int query(int nd, int b, int e, int x, int v) { n   if (b == e) { n     if (v == 0) n       return tree[nd]; n     else n       return v; n   } n   int md = (b + e) / 2; n   if (v == 0) v = tree[nd]; n   if (x <= md) n     return query(2 * nd, b, md, x, v); n   else n     return query(2 * nd + 1, md + 1, e, x, v); n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> m; n   for (int i = 1; i <= m; i++) { n     cin >> qr[i].l >> qr[i].r >> qr[i].x; n   } n   for (int i = m; i >= 1; i--) { n     int l, r, x; n     l = qr[i].l; n     r = qr[i].r; n     x = qr[i].x; n     if (x >= l && x <= r) { n       update(1, 1, n, l, x - 1, x); n       update(1, 1, n, x + 1, r, x); n     } else { n       update(1, 1, n, l, r, x); n     } n   } n   for (int i = 1; i <= n; i++) { n     ans[i] = query(1, 1, n, i, 0); n   } n   for (int i = 1; i <= n; i++) { n     if (i < n) n       cout << ans[i] <<    ; n     else n       cout << ans[i] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, cnt = 0; n int main() { n   ios::sync_with_stdio(false); n   cin >> n; n   if (n <= 3) { n     cout <<  NO ; n     return 0; n   } n   cout <<  YES  <<   n ; n   if (n % 2) { n     cout <<  5 - 3 = 2  <<   n ; n     cout <<  2 + 1 = 3  <<   n ; n     cout <<  3 * 4 = 12  <<   n ; n     cout <<  12 * 2 = 24  <<   n ; n     while (n > 5) { n       cout << n <<   -   << n - 1 <<   =   << 1 <<   n ; n       cnt++; n       n -= 2; n     } n     cnt--; n     while (cnt > 0) { n       cout <<  1 * 1 = 1  <<   n ; n       cnt--; n     } n     if (cnt == 0) cout <<  1 * 24 = 24  <<   n ; n   } else { n     cout <<  1 * 2 = 2  <<   n ; n     cout <<  2 * 3 = 6  <<   n ; n     cout <<  6 * 4 = 24  <<   n ; n     while (n > 4) { n       cout << n <<   -   << n - 1 <<   =   << 1 <<   n ; n       n -= 2; n       cnt++; n     } n     cnt--; n     while (cnt > 0) { n       cout <<  1 * 1 = 1  <<   n ; n       cnt--; n     } n     if (cnt == 0) cout <<  1 * 24 = 24  <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100005; n const int INF = 1000000009; n int main() { n   std::ios_base::sync_with_stdio(false); n   std::cin.tie(nullptr); n   int a, b; n   cin >> a >> b; n   if ((a || b) && max(a, b) - min(a, b) < 2) { n     cout <<  YES ; n   } else { n     cout <<  NO ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<pair<int, int>, int> > v; n int a[(int)(1e5 + 50)]; n string s; n void solve() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     int x; n     scanf( %d , &x); n     s += (char)(x +  0 ); n   } n   for (int i = 0; i < s.size();) { n     int st = i, len = 0; n     char c = s[i]; n     while (i < s.size() && c == s[i]) { n       len++; n       i++; n     } n     v.push_back(make_pair(make_pair(st, len), (int)(c -  0 ))); n   } n   int len = v.size(); n   if (v[len - 1].second == 1 || n       (v[len - 1].first.second == 2 && v[len - 1].second == 0 && len < 3)) { n     printf( NO ); n     return; n   } n   printf( YES n ); n   string ans; n   if (v[len - 1].first.second == 2) { n     for (int i = 0; i < len - 3; i++) { n       char toAdd = (char)(v[i].second +  0 ); n       for (int j = 0; j < v[i].first.second; j++) { n         ans += toAdd; n         ans +=  -> ; n       } n     } n     for (int j = 0; j < v[len - 3].first.second - 1; j++) { n       ans +=  0 ; n       ans +=  -> ; n     } n     ans +=  (0->( ; n     for (int i = 0; i < v[len - 2].first.second; i++) { n       ans +=  1 ; n       ans +=  -> ; n     } n     ans +=  0))->0 ; n     cout << ans; n     return; n   } n   if (v[len - 1].first.second % 2 == 0) { n     ans +=  ( ; n     for (int i = 0; i < v[len - 1].first.second - 3; i++) { n       ans +=  0-> ; n     } n     ans +=  (0->0) ; n     ans +=  ->0 ; n     ans +=  ) ; n   } else { n     ans +=  ( ; n     for (int i = 0; i < v[len - 1].first.second - 1; i++) { n       ans +=  0-> ; n     } n     ans +=  0 ; n     ans +=  ) ; n   } n   if (len - 1 == 0) { n     cout << ans; n     return; n   } n   reverse(ans.begin(), ans.end()); n   ans +=  >- ; n   ans +=  )1 ; n   for (int i = 0; i < v[len - 2].first.second - 1; i++) { n     ans +=  >-1 ; n   } n   ans +=  ( ; n   for (int i = len - 3; i >= 0; i--) { n     ans +=  >- ; n     char toAdd = (char)(v[i].second +  0 ); n     int curlen = v[i].first.second; n     for (int j = 0; j < curlen - 1; j++) { n       ans += toAdd; n       ans +=  >- ; n     } n     ans += toAdd; n   } n   reverse(ans.begin(), ans.end()); n   cout << ans; n } n int main() { n   int t = 1; n   for (int i = 1; i <= t; i++) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int NMAX = 755; n const long long INF = 1LL << 62; n long long dp[NMAX][NMAX]; n int main() { n   cin.sync_with_stdio(false); n   int n, m; n   cin >> n >> m; n   vector<int> v(n); n   for (int i = 0; i < n; i++) { n     cin >> v[i]; n   } n   for (int i = n - 1; i >= 0; i--) { n     for (int j = 0; j <= n - i; j++) { n       if (j) { n         dp[i][j] = dp[i + 1][j - 1]; n       } else { n         dp[i][j] = INF; n       } n       if (v[i] >= 0) { n         dp[i][j] = min(dp[i][j], max(0LL, dp[i + 1][j] - v[i])); n       } else { n         dp[i][j] = min(dp[i][j], dp[i + 1][j] - v[i]); n       } n     } n   } n   for (; m; m--) { n     long long x; n     cin >> x; n     int l = 0; n     int r = n; n     int ans = n; n     while (l <= r) { n       int mi = (l + r) / 2; n       if (dp[0][mi] <= x) { n         ans = mi; n         r = mi - 1; n       } else { n         l = mi + 1; n       } n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int n; n   cin >> n; n   long long int k; n   cin >> k; n   vector<long long int> arr(n, 0); n   long long int cnt = 0; n   for (int i = 0; i < n; ++i) { n     cin >> arr[i]; n     long long int var = 0; n     while (arr[i] > 0) { n       long long int rem = arr[i] % 10; n       if (rem == 4 || rem == 7) { n         var++; n       } n       arr[i] /= 10; n     } n     if (var > k) { n       cnt++; n     } n   } n   cout << n - cnt <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T1, typename T2> n bool Max(T1& a, T2 b) { n   return a < b ? a = b, 1 : 0; n } n template <typename T1, typename T2> n bool Min(T1& a, T2 b) { n   return a > b ? a = b, 1 : 0; n } n const int N = 1000001; n bool prime[N]; n vector<long long> pri; n vector<pair<long long, long long> > nv, sv; n long long n, m, s; n long long ans; n void rec(long long i, long long prod) { n   if (i == sv.size()) { n     ans++; n     return; n   } n   auto cur = sv[i]; n   for (int j = 0; j < (cur.second + 1); ++j) { n     rec(i + 1, prod); n     if ((prod *= cur.first) > n) break; n   } n } n long long pow(long long a, long long p) { n   long long ret = 1; n   while (p) { n     if (p & 1) ret *= a; n     a *= a; n     p >>= 1; n   } n   return ret; n } n void solve() { n   long long v; n   n = 1; n   for (int i = 0; i < (3); ++i) { n     cin >> v; n     n *= v; n   } n   m = 1; n   for (int i = 0; i < (3); ++i) { n     cin >> v; n     m *= v; n   } n   s = 2; n   for (int i = 0; i < (3); ++i) { n     cin >> v; n     s *= v; n   } n   long long ts = s, tn = n; n   nv.clear(); n   sv.clear(); n   for (auto& e : pri) { n     if (e * e > ts) break; n     long long cnt = 0; n     while (ts % e == 0) { n       cnt++; n       ts /= e; n     } n     if (cnt) sv.emplace_back(e, cnt); n   } n   if (ts > 1) sv.emplace_back(ts, 1); n   for (auto& e : pri) { n     if (e * e > tn) break; n     long long cnt = 0; n     while (tn % e == 0) { n       cnt++; n       tn /= e; n     } n     if (cnt) nv.emplace_back(e, cnt); n   } n   if (tn > 1) nv.emplace_back(tn, 1); n   ans = 0; n   rec(0, 1); n   sv.emplace_back(N, 1); n   vector<long long> tmp; n   long long i = 0; n   for (auto& e : nv) { n     while (sv[i].first < e.first) i++; n     if (sv[i].first > e.first) { n       tmp.emplace_back(e.first); n     } else { n       if (sv[i].second < e.second) n         tmp.emplace_back(pow(e.first, sv[i].second + 1)); n       i++; n     } n   } n   long long sz = tmp.size(); n   for (int sub = 0; sub < (1 << sz); ++sub) { n     long long t = 1; n     bool add = 1; n     for (int i = 0; i < (sz); ++i) n       if ((sub >> i) & 1) { n         t *= tmp[i]; n         add ^= 1; n       } n     if (add) n       ans += m / t; n     else n       ans -= m / t; n   } n   cout << ans <<   n ; n } n void init() { n   for (int i = 2; i <= (N - 1); ++i) n     if (!prime[i]) { n       pri.emplace_back(i); n       for (long long j = (long long)i * i; j < N; j += i) prime[j] = 1; n     } n } n int main(void) { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.setf(ios::fixed); n   cout.precision(20); n   init(); n   int TC = 1; n   cin >> TC; n   for (int TI = 1; TI <= (TC); ++TI) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void test_case() { n   int n; n   long long k; n   cin >> n >> k; n   int l, r[2] = {-1, -1}; n   long long curr[2] = {k, k}, ans[2] = {0, 0}, monsters; n   for (int i = 0; i < n; i++) { n     int old_r[2] = {r[0], r[1]}, tmp_r; n     long long old_curr[2] = {curr[0], curr[1]}, old_ans[2] = {ans[0], ans[1]}; n     cin >> l >> tmp_r >> monsters; n     ans[0] = -1; n     for (int j = 0; j < 2; j++) { n       if (old_ans[j] == -1) continue; n       if (old_curr[j] + (tmp_r - l) * k < monsters) continue; n       if (ans[0] != -1 && ans[0] < old_ans[j] + monsters) continue; n       ans[0] = old_ans[j] + monsters; n       if (old_curr[j] >= monsters) { n         r[0] = l; n         curr[0] = old_curr[j] - monsters; n       } else { n         int turns = (monsters - old_curr[j] + k - 1) / k; n         r[0] = l + turns; n         curr[0] = k * turns + old_curr[j] - monsters; n       } n     } n     ans[1] = -1; n     for (int j = 0; j < 2; j++) { n       if (old_ans[j] == -1) continue; n       if ((tmp_r - l + 1) * k < monsters) continue; n       if (old_r[j] == l) continue; n       if (ans[1] != -1 && ans[1] <= old_ans[j] + old_curr[j] + monsters) n         continue; n       ans[1] = old_ans[j] + old_curr[j] + monsters; n       int turns = (monsters + k - 1) / k; n       r[1] = l + turns - 1; n       curr[1] = k * turns - monsters; n     } n   } n   if (ans[0] == -1) n     cout << ans[1] <<   n ; n   else if (ans[1] == -1) n     cout << ans[0] <<   n ; n   else n     cout << min(ans[0], ans[1]) <<   n ; n } n int main(void) { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int Case = 1; n   for (int i = 0; i < Case; i++) { n     test_case(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e6 + 10; n int vis[maxn]; n vector<int> e[maxn]; n vector<int> w[maxn]; n int a[maxn]; n int ans; n void add_edge(int u, int v, int ww) { n   e[u].push_back(v); n   e[v].push_back(u); n   w[u].push_back(ww); n   w[v].push_back(ww); n } n void dfs(int num, int l, int c) { n   if (l > a[num] && c != 1) { n     c = 1; n   } n   if (c == 1) { n     ans++; n   } n   vis[num] = 1; n   int cc = e[num].size(); n   for (int i = 0; i < cc; i++) { n     if (!vis[e[num][i]]) { n       if (l < 0) l = 0; n       vis[e[num][i]] = 1; n       dfs(e[num][i], l + w[num][i], c); n     } n   } n } n int main() { n   memset(vis, 0, sizeof(vis)); n   int n; n   scanf( %d , &n); n   ans = 0; n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   for (int i = 2; i <= n; i++) { n     int u = i, v, ww; n     scanf( %d%d , &v, &ww); n     add_edge(u, v, ww); n   } n   ans = 0; n   dfs(1, 0, 0); n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n  n #define ll int n #define vl vector<long long> n #define fr(i,a,n) for(ll i=a; i<n; i++) n #define all(x) x.begin(), x.end() n  n int32_t main(){ n  ios_base::sync_with_stdio(false); n  cin.tie(NULL); n  cout.tie(NULL); n  ll t; cin>>t; n  while(t--){ n   ll n, x=0, z[4]; n   cin>>n; n   for(ll i=0; i<4; i++) cin>>z[i]; n   for(ll i=0; i<=15; i++){ n    ll a=0, b=0, c=0, d=0; n    if(i&1) a++; n    if(i&2) b++; n    if(i&4) c++; n    if(i&8) d++; n    if(z[0]-a-b<=n-2 && z[0]>=a+b) x++; n    if(z[1]-c-b<=n-2 && z[1]>=c+b) x++; n    if(z[2]-c-d<=n-2 && z[2]>=c+d) x++; n    if(z[3]-a-d<=n-2 && z[3]>=a+d) x++; n    if(x==4) break; n    else x=0; n   } n   if(x==4) cout<< Yes n ; n   else cout<< No n ; n  } n  }


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n bool et[N]; n int n; n int mob[N]; n vector<int> d[N]; n int cnt[N]; n bool get(int x) { n   int coprime = 0; n   for (auto p : d[x]) coprime += cnt[p] * mob[p]; n   return coprime > 0; n } n void update(int x, int val) { n   for (auto p : d[x]) cnt[p] += val; n } n int main() { n   for (int i = 1; i < N; i++) { n     for (int j = i; j < N; j += i) d[j].push_back(i); n     if (i == 1) n       mob[i] = 1; n     else if ((i / d[i][1]) % d[i][1] == 0) n       mob[i] = 0; n     else n       mob[i] = -mob[i / d[i][1]]; n   } n   cin >> n; n   for (int i = 0; i < n; i++) { n     int x; n     cin >> x; n     et[x] = 1; n   } n   for (int i = 1; i < N; i++) { n     for (auto x : d[i]) n       if (i != x) et[x] |= et[i]; n   } n   long long ans = 0; n   stack<int> s; n   for (int i = N - 1; i >= 1; i--) { n     if (et[i] == 0) continue; n     while (get(i) > 0) { n       ans = max(ans, 1LL * i * s.top()); n       update(s.top(), -1); n       s.pop(); n     } n     update(i, 1); n     s.push(i); n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int N, X, Y; n   cin >> N >> X >> Y; n   int p = ceil(Y / 100.0 * N); n   cout << max(p - X, 0) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   int i, j, count_star, diff, res = 0, n; n   cin >> s; n   n = s.size(); n   for (i = 0; i < n; i++) { n     diff = count_star = 0; n     for (j = i; j < n; j++) { n       if (s[j] ==  ( ) n         diff++; n       else if (s[j] ==  ) ) n         diff--; n       else { n         count_star++; n         diff--; n       } n       if (diff < 0) { n         count_star--; n         if (count_star < 0) n           break; n         else n           diff += 2; n       } n       if (diff == 0 && (j - i + 1) % 2 == 0) res++; n     } n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int P; n int g1() { n   double p = rand() / 32767.0, s = 0, g = 1; n   for (int k = 0;; k++) { n     if (k) g *= k; n     double c = pow(P, k) * exp(-P) / g; n     s += c; n     if (p <= s) return k; n   } n } n int g2() { n   double p = rand() / 32767.0, s = 0; n   for (int k = 0; k <= P + P; k++) { n     s += 1.0 / (2 * P + 1); n     if (p <= s) return k; n   } n   return P + P; n } n int main() { n   int v; n   scanf( %d , &v); n   while (v--) { n     vector<int> vs; n     for (int i = 0; i < 250; i++) { n       int x; n       scanf( %d , &x); n       vs.push_back(x); n     } n     double avr = 0, ss = 0; n     for (int i = 0; i < 250; i++) avr += (double)vs[i]; n     avr /= 250; n     int mx = 0; n     for (int i = 0; i < 250; i++) n       ss += (vs[i] - avr) * (vs[i] - avr), mx = max(mx, vs[i]); n     ss /= avr; n     if (ss < 500) n       printf( %d n , int(avr + 0.5)); n     else n       printf( %d n , int(mx / 2.0 + 0.5)); n   } n } n


#include <bits/stdc++.h> n using namespace std; n unsigned gcd(unsigned a, unsigned b) { n   unsigned r; n   do { n     r = a % b; n     a = b; n     b = r; n   } while (r > 0); n   return a; n } n unsigned arr[705]; n bool dp[705][705][2]; n bool en[705][705][2]; n unsigned g[705][705]; n bool f(int l, int r, int rm) { n   if (en[l][r][rm]) return dp[l][r][rm]; n   en[l][r][rm] = true; n   if (l == r) n     dp[l][r][rm] = g[l][l - 1 + 2 * rm] > 1; n   else { n     dp[l][r][rm] |= (g[l - 1 + rm * (r - l + 2)][l] > 1) && f(l + 1, r, 0); n     dp[l][r][rm] |= (g[l - 1 + rm * (r - l + 2)][r] > 1) && f(l, r - 1, 1); n     for (int curs = 1; curs < r - l; curs++) n       dp[l][r][rm] |= (g[l - 1 + rm * (r - l + 2)][l + curs] > 1) && n                       f(l, l + curs - 1, 1) && f(l + curs + 1, r, 0); n   } n   return dp[l][r][rm]; n } n void init(int n) { n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n; j++) g[i][j] = gcd(arr[i], arr[j]); n } n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %d , &arr[i]); n   init(n); n   if (f(1, n - 1, 0)) { n     printf( Yes n ); n     return 0; n   } n   if (f(0, n - 2, 1)) { n     printf( Yes n ); n     return 0; n   } n   for (int i = 1; i < n - 1; i++) { n     if (f(0, i - 1, 1) && f(i + 1, n - 1, 0)) { n       printf( Yes n ); n       return 0; n     } else n       continue; n   } n   printf( No n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void __dbg() { cout << endl; } n template <typename Arg, typename... Args> n void __dbg(Arg A, Args... B) { n   cout <<     << A; n   __dbg(B...); n } n const int inf = INT_MAX; n const int ninf = INT_MIN; n const int mod = 1e9 + 7; n const int maxN = 1e6 + 2; n int main() { n   int n; n   scanf( %d , &n); n   map<pair<int, int>, int> ma; n   vector<int> x(n), y(n); n   for (int i = 0; i < n; i++) { n     scanf( %d , &x[i]); n     scanf( %d , &y[i]); n   } n   for (int i = 0; i < n; i++) { n     for (int j = i + 1; j < n; j++) { n       int cx = x[i] + x[j]; n       int cy = y[i] + y[j]; n       ma[{cx, cy}]++; n     } n   } n   long long ans = 0; n   for (auto it : ma) { n     ans += it.second * 1LL * (it.second - 1) / 2; n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int p[111]; n int main() { n   int n, a = 0, b = 0, c = 0; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> p[i]; n     if (i < 3 && i == 1 || i % 3 == 1) { n       a = a + p[i]; n     } else if (i < 3 && i == 2 || i % 3 == 2) { n       b = b + p[i]; n     } else if (i % 3 == 0) { n       c = c + p[i]; n     } n   } n   if (max(a, max(b, c)) == a) { n     cout <<  chest ; n   } else if (max(a, max(b, c)) == b) { n     cout <<  biceps ; n   } else if (max(a, max(b, c)) == c) { n     cout <<  back ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int t, n, l, r; n   cin >> t; n   while (t--) { n     cin >> n; n     r = n; n     l = (-1) * (n - 1); n     cout << l <<     << r <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(void) { n   int n, m, v[100][100], x, y, cnt, ans = 0, dcnt = 1; n   vector<int> del; n   memset(v, 0, sizeof(v)); n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     cin >> x >> y; n     v[x - 1][y - 1] = 1; n     v[y - 1][x - 1] = 1; n   } n   while (dcnt != 0) { n     dcnt = 0; n     for (int i = 0; i < n; i++) { n       cnt = 0; n       for (int j = 0; j < n; j++) cnt += v[i][j]; n       if (cnt == 1) { n         dcnt++; n         del.push_back(i); n         for (int j = 0; j < n; j++) v[i][j] = 0; n       } n     } n     for (int i = 0; i < n; i++) { n       for (unsigned int j = 0; j < del.size(); j++) v[i][del[j]] = 0; n     } n     if (dcnt > 0) ans++; n     del.clear(); n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N; n long long dp[2 * 100000 + 10][2], arr[2 * 100000 + 10]; n long long Solve(int, bool); n int main() { n   scanf( %d , &N); n   dp[0][0] = dp[0][1] = dp[1][0] = dp[1][1] = -2; n   for (int i = 2; i <= N; i++) { n     scanf( %I64d , &arr[i]); n     dp[i][0] = dp[i][1] = -2; n   } n   for (int i = 2; i <= N; i++) Solve(i, 0), Solve(i, 1); n   for (int i = 1; i < N; i++) n     dp[1 + i][0] != -1 ? printf( %I64d n , dp[1 + i][0] + i) : printf( -1 n ); n   return 0; n } n long long Solve(int ind, bool f) { n   if (ind <= 0 || ind > N) return 0; n   if (dp[ind][f] == -3) return -1; n   if (dp[ind][f] != -2) return dp[ind][f]; n   dp[ind][f] = -3; n   long long ret, a; n   if (f) n     a = Solve(ind + arr[ind], 0); n   else n     a = Solve(ind - arr[ind], 1); n   if (a == -1) n     ret = -1; n   else n     ret = a + (long long)arr[ind]; n   return dp[ind][f] = ret; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long n, i, j; n   cin >> n; n   map<long long, long long> mp, cp; n   vector<long long> v(n); n   for (i = 0; i < n; i++) { n     cin >> v[i]; n     mp[v[i]]++; n   } n   map<long long, long long>::iterator it; n   for (it = mp.begin(); it != mp.end(); it++) { n     cp[it->second]++; n   } n   if (cp.find(1) == cp.end() || cp[1] % 2 == 0) { n     long long cnt = 0; n     cout <<  YES  << endl; n     for (i = 0; i < n; i++) { n       if (mp[v[i]] == 1) { n         if (cnt < cp[1] / 2) { n           cout <<  A ; n         } else n           cout <<  B ; n         cnt++; n       } else n         cout <<  A ; n     } n   } else { n     if (cp[1] + 2 * cp[2] == n) { n       cout <<  NO ; n     } else { n       cout <<  YES  << endl; n       long long cnt = 0, t = 1; n       for (i = 0; i < n; i++) { n         if (mp[v[i]] == 1) { n           if (cnt < (cp[1] + 1) / 2) { n             cout <<  A ; n           } else { n             cout <<  B ; n           } n           cnt++; n         } else if (mp[v[i]] == 2) { n           cout <<  A ; n         } else { n           if (t == 1) { n             cout <<  B ; n             t++; n           } else { n             cout <<  A ; n           } n         } n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1E9 + 9; n int n, m, ans, cntNode(1), n     root(1), f[200], dp[1005][105][13], vis[1005][105][13]; n string s; n struct Trie { n   int son[4], fail, len; n   Trie() { n     for (int i = 0; i < 4; i++) son[i] = 0; n     fail = len = 0; n   } n } tr[105]; n void add(int& a, int b) { a = (a + b) % MOD; } n void insert(string& s) { n   int cur = root; n   for (int i = 0; i < s.size(); i++) { n     if (!tr[cur].son[f[s[i]]]) tr[cur].son[f[s[i]]] = ++cntNode; n     cur = tr[cur].son[f[s[i]]]; n   } n   tr[cur].len = s.size(); n } n void getFail() { n   queue<int> Q; n   Q.push(root); n   tr[root].fail = root; n   while (!Q.empty()) { n     int now = Q.front(); n     Q.pop(); n     tr[now].len = max(tr[now].len, tr[tr[now].fail].len); n     for (int i = 0; i < 4; i++) { n       if (tr[now].son[i]) { n         tr[tr[now].son[i]].fail = now != root ? tr[tr[now].fail].son[i] : root; n         Q.push(tr[now].son[i]); n       } else n         tr[now].son[i] = n             tr[tr[now].fail].son[i] ? tr[tr[now].fail].son[i] : root; n     } n   } n } n void DP() { n   queue<tuple<int, int, int> > Q; n   dp[0][1][0] = 1; n   Q.push(make_tuple(0, 1, 0)); n   while (!Q.empty()) { n     int len = get<0>(Q.front()); n     int node = get<1>(Q.front()); n     int last = get<2>(Q.front()); n     Q.pop(); n     if (len == n) { n       if (!last) add(ans, dp[len][node][last]); n       continue; n     } n     vis[len][node][last] = 0; n     for (int i = 0; i < 4; i++) { n       int to = tr[node].son[i]; n       int tmp = tr[to].len > last ? 0 : last + 1; n       if (tmp > 10) continue; n       add(dp[len + 1][to][tmp], dp[len][node][last]); n       if (!vis[len + 1][to][tmp]) { n         vis[len + 1][to][tmp] = 1; n         Q.push(make_tuple(len + 1, to, tmp)); n       } n     } n   } n   return; n } n signed main() { n   ios::sync_with_stdio(false); n   cin >> n >> m; n   f[ A ] = 0, f[ T ] = 1, f[ C ] = 2, f[ G ] = 3; n   for (int i = 1; i <= m; i++) cin >> s, insert(s); n   getFail(); n   DP(); n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T, typename U> n T pow_mod(T a, U b, int mod) { n   T r = 1; n   for (; b > 0; b >>= 1) { n     if (b & 1) r = (long long)r * a % mod; n     a = (long long)a * a % mod; n   } n   return r; n } n namespace combinatorics { n const int mod = 1e9 + 7, N = 2e3 + 5; n int fac[N], ifac[N]; n void init() { n   fac[0] = ifac[0] = 1; n   for (int i = 1; i < N; ++i) { n     fac[i] = (long long)fac[i - 1] * i % mod; n     ifac[i] = pow_mod(fac[i], mod - 2, mod); n   } n } n int comb(int n, int k) { n   return (long long)fac[n] * ifac[n - k] % mod * ifac[k] % mod; n } n }  // namespace combinatorics n using namespace combinatorics; n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0); n   init(); n   int t = 1; n   while (t--) { n     int n, x, p; n     cin >> n >> x >> p; n     vector<int> a(n), c(2); n     a[p] = 1; n     auto f = [&]() { n       int l = 0, r = n; n       while (l < r) { n         int m = (l + r) >> 1; n         if (m <= p) { n           c[0] += a[m] == 0; n           a[m] = -1; n           l = m + 1; n         } else { n           c[1] += a[m] == 0; n           a[m] = +1; n           r = m; n         } n       } n     }; n     f(); n     int ans = 0; n     if (n - x >= c[1] && x - 1 >= c[0]) n       ans = (long long)comb(n - x, c[1]) * comb(x - 1, c[0]) % mod * n             fac[n - 1 - c[0] - c[1]] % mod * fac[c[0]] % mod * fac[c[1]] % mod; n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char opt; n long long a, b, c, ansa, ansb, ansc, anslen = 15; n void dfs(long long a, long long b, long long c, long long nowa, long long nowb, n          int pre, long long len, long long step) { n   if (len >= anslen) return; n   if (!a && !b && !c && !pre) { n     ansa = nowa, ansb = nowb, anslen = len; n     return; n   } n   if (!c) { n     long long tmp = a + b + pre; n     int cnt = 0; n     while (tmp) cnt++, tmp /= 10; n     dfs(0, 0, 0, nowa + step * a, nowb + step * b, 0, len + cnt, step); n     return; n   } n   if ((a + b + pre) % 10 == c % 10) { n     dfs(a / 10, b / 10, c / 10, nowa + step * (a % 10), nowb + step * (b % 10), n         (a % 10 + b % 10 + pre) / 10, len, step * 10); n     return; n   } n   dfs(a * 10 + (c + 10 - b % 10 - pre) % 10, b, c, nowa, nowb, pre, len + 1, n       step); n   dfs(a, b * 10 + (c + 10 - a % 10 - pre) % 10, c, nowa, nowb, pre, len + 1, n       step); n   dfs(a, b, c * 10 + (a + b + pre) % 10, nowa, nowb, pre, len + 1, step); n } n int main() { n   cin >> a >> opt >> b >> opt >> c; n   dfs(a, b, c, 0, 0, 0, 0, 1); n   cout << ansa <<  +  << ansb <<  =  << ansa + ansb << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n constexpr int mod = 998244353; n int x[5], y[5]; n int px[32], nx[32], py[32], ny[32]; n int dp[32][20][20][20][20][2][2]; n void add(int& x, const int& y) { n   x += y; n   if (x >= mod) x -= mod; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < n; i += 1) cin >> x[i] >> y[i]; n   for (int mask = 0; mask < (1 << n); mask += 1) n     for (int i = 0; i < n; i += 1) n       if ((mask >> i) & 1) { n         (x[i] > 0 ? px : nx)[mask] += abs(x[i]); n         (y[i] > 0 ? py : ny)[mask] += abs(y[i]); n       } n   dp[0][0][0][0][0][0][0] = 1; n   for (int i = 0; i < (30); i += 1) n     for (int cpx = 0; cpx < (20); cpx += 1) n       for (int cnx = 0; cnx < (20); cnx += 1) n         for (int cpy = 0; cpy < (20); cpy += 1) n           for (int cny = 0; cny < (20); cny += 1) n             for (int bx = 0; bx < (2); bx += 1) n               for (int by = 0; by < (2); by += 1) n                 for (int mask = 0; mask < (1 << n); mask += 1) n                   if (dp[i][cpx][cnx][cpy][cny][bx][by]) { n                     if (0) n                       cout << i <<     << cpx <<     << cny <<     << cpy <<     n                            << cny <<     << bx <<     << by <<     n                            << dp[i][cpx][cnx][cpy][cny][bx][by] << endl; n                     int npx = cpx + px[mask], nnx = cnx + nx[mask], n                         npy = cpy + py[mask], nny = cny + ny[mask]; n                     if ((npx ^ nnx) & 1) continue; n                     if ((npy ^ nny) & 1) continue; n                     int cx = npx & 1, mx = (m >> i) & 1; n                     int cy = npy & 1, my = (m >> i) & 1; n                     add(dp[i + 1][npx >> 1][nnx >> 1][npy >> 1][nny >> 1] n                           [cx != mx ? (cx > mx) : bx] n                           [cy != my ? (cy > my) : by], n                         dp[i][cpx][cnx][cpy][cny][bx][by]); n                   } n   cout << (dp[30][0][0][0][0][0][0] + mod - 1) % mod; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool qo(bool x) { return (x ? 0 : qo(!x)); } n const int mnx = 1e5 + 9; n const int mod = 1e9 + 7; n long long n, sum; n long long a[mnx]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0), cout.tie(0); n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   for (int m = 0; m < (1 << n); m++) { n     for (int j = 0; j < n; j++) { n       if (m & (1 << j)) n         sum += a[j]; n       else n         sum -= a[j]; n     } n     if (sum % 360 == 0) { n       cout <<  YES n ; n       exit(0); n       ; n     } n     sum = 0; n   } n   cout <<  NO n ; n   exit(0); n   ; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:256000000 ) n using namespace std; n const int INF = 1000001000; n const int mod = 1000 * 1000 * 1000 + 7; n const int mod9 = 1000 * 1000 * 1000 + 9; n const double PI = 3.1415926535897932; n double sqr(double first) { return first * first; }; n long double sqr(long double first) { return first * first; }; n long long sqr(long long first) { return first * first; }; n long long sqr(int first) { return first * 1LL * first; }; n long long gcd(long long a, long long b) { n   while (b) a %= b, swap(a, b); n   return a; n } n long long bpm(long long a, long long n = -2, long long m = mod) { n   n = n >= 0 ? n : m + n; n   long long r = 1; n   while (n) { n     if (n & 1) r = (r * a) % m; n     a = (a * a) % m; n     n >>= 1; n   } n   return r; n } n long long dist(long long x1, long long y1, long long x2, long long y2) { n   return sqr(x1 - x2) + sqr(y1 - y2); n } n std::ostream& operator<<(std::ostream& os, pair<int, int> p) { n   return os <<  {  << p.first <<     << p.second <<  } ; n } n const int N = 200005; n long long a[N]; n pair<long long, long long> b[N]; n int main() { n   ios::sync_with_stdio(false); n   long long n, m, s, d; n   cin >> n >> m >> s >> d; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   sort(a, a + n); n   int nwn = -1; n   for (int i = 0; i < n; i++) { n     b[i] = {a[i], a[i]}; n     if (i && a[i] - a[i - 1] < s + 2) { n       b[nwn].second = b[i].first; n     } else { n       b[++nwn] = b[i]; n     } n   } n   n = nwn + 1; n   vector<pair<long long, long long> > res; n   long long pos = 0; n   for (int i = 0; i < n; i++) { n     long long next = b[i].first; n     long long runD = next - 1 - pos; n     bool err = false; n     if (runD < s) { n       err = true; n     } n     res.push_back({1, runD}); n     pos += runD; n     long long fin = b[i].second + 1; n     if (fin > m || fin - pos > d) { n       err = true; n     } n     res.push_back({0, fin - pos}); n     pos = fin; n     if (err) { n       pos = m; n       res.clear(); n       res.push_back({-1, 0}); n       break; n     } n   } n   if (pos != m) { n     res.push_back({1, m - pos}); n   } n   for (pair<int, int> d : res) { n     switch (d.first) { n       case -1: n         cout <<  IMPOSSIBLE n ; n         break; n       case 0: n         cout <<  JUMP   << d.second <<   n ; n         break; n       case 1: n         cout <<  RUN   << d.second <<   n ; n         break; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n const double EPS = 1e-10; n const double PI = acos(-1.0); n using namespace std; n int main(void) { n   int n; n   cin >> n; n   vector<pair<int, int> > e; n   for (int i = (0); i < (n); ++i) { n     int a, b; n     cin >> a >> b; n     e.push_back(make_pair(a, b)); n   } n   sort((e).begin(), (e).end()); n   int answer = 0; n   for (int i = (0); i < (n); ++i) { n     if (answer <= e[i].second) n       answer = e[i].second; n     else n       answer = e[i].first; n   } n   cout << answer << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char field[50][50]; n bool used[50][50]; n int n, m, k; n int counts[26]; n int cur_set; n set<string> min_ways; n int min_len = INT_MAX; n int start_x, start_y, end_x, end_y; n vector<char> alpha(0); n bool comp(pair<int, int> a, pair<int, int> b) { n   if (field[a.first][a.second] ==  T ) return true; n   if (field[b.first][b.second] ==  T ) return false; n   return field[a.first][a.second] < field[b.first][b.second]; n } n vector<pair<int, int>> find_near(int from_y, int from_x, n                                  const set<char> &from_set) { n   vector<pair<int, int>> near(0); n   if (from_y > 0 && !used[from_y - 1][from_x] && n       from_set.find(field[from_y - 1][from_x]) != from_set.end()) n     near.push_back(make_pair(from_y - 1, from_x)); n   if (from_y < n - 1 && !used[from_y + 1][from_x] && n       from_set.find(field[from_y + 1][from_x]) != from_set.end()) n     near.push_back(make_pair(from_y + 1, from_x)); n   if (from_x > 0 && !used[from_y][from_x - 1] && n       from_set.find(field[from_y][from_x - 1]) != from_set.end()) n     near.push_back(make_pair(from_y, from_x - 1)); n   if (from_x < m - 1 && !used[from_y][from_x + 1] && n       from_set.find(field[from_y][from_x + 1]) != from_set.end()) n     near.push_back(make_pair(from_y, from_x + 1)); n   sort(near.begin(), near.end(), comp); n   return near; n } n set<char> find_set_near(int from_y, int from_x) { n   set<char> near = set<char>(); n   if (from_y > 0 && !used[from_y - 1][from_x]) n     near.insert(field[from_y - 1][from_x]); n   if (from_y < n - 1 && !used[from_y + 1][from_x]) n     near.insert(field[from_y + 1][from_x]); n   if (from_x > 0 && !used[from_y][from_x - 1]) n     near.insert(field[from_y][from_x - 1]); n   if (from_x < m - 1 && !used[from_y][from_x + 1]) n     near.insert(field[from_y][from_x + 1]); n   near.erase( T ); n   near.erase( S ); n   return near; n } n set<char> find_second_near_set(int from_y, int from_x, set<char> &not_from) { n   bool l2, l1, r1, r2, u1, u2, d1, d2; n   l2 = from_x - 2 >= 0; n   l1 = from_x > 0; n   r1 = from_x + 2 < m; n   r2 = from_x + 1 < m; n   u2 = from_y - 2 >= 0; n   u1 = from_y > 0; n   d1 = from_y + 1 < n; n   d2 = from_y + 2 < n; n   int yu2 = from_y - 2; n   int yu1 = from_y - 1; n   int yd1 = from_y + 1; n   int yd2 = from_y + 2; n   int xl2 = from_x - 2; n   int xl1 = from_x - 1; n   int xr1 = from_x + 1; n   int xr2 = from_x + 2; n   set<char> ret = set<char>(); n   if (l1 && u2) ret.insert(field[yu2][xl1]); n   if (u2) ret.insert(field[yu2][from_x]); n   if (u2 && r1) ret.insert(field[yu2][xr1]); n   if (r2) { n     if (u1) ret.insert(field[yu1][xr2]); n     ret.insert(field[from_y][xr2]); n     if (d1) ret.insert(field[yd1][xr2]); n   } n   if (d2) { n     if (r1) ret.insert(field[yd2][xr1]); n     ret.insert(field[yd2][from_x]); n     if (l1) ret.insert(field[yd2][xl1]); n   } n   if (l2) { n     if (d1) ret.insert(field[yd1][xl2]); n     ret.insert(field[from_y][xl2]); n     if (u1) ret.insert(field[yu1][xl2]); n   } n   for (set<char>::iterator i = not_from.begin(); i != not_from.end(); i++) n     ret.erase(*i); n   return ret; n } n void read() { n   set<char> set_alpha = set<char>(); n   cin >> n >> m >> k; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) { n       cin >> field[i][j]; n       if (field[i][j] ==  S ) { n         start_y = i; n         start_x = j; n       } else if (field[i][j] ==  T ) { n         end_y = i; n         end_x = j; n       } else n         set_alpha.insert(field[i][j]); n     } n   for (set<char>::iterator i = set_alpha.begin(); i != set_alpha.end(); i++) n     alpha.push_back(*i); n } n struct cell { n   pair<int, int> pos; n   string s; n }; n struct comp2 { n   bool operator()(const cell &a, const cell &b) { n     if (a.s.size() != b.s.size()) n       return a.s.size() < b.s.size(); n     else n       return a.s < b.s; n   } n }; n set<set<char>> last_sets; n bool wave(set<char> &cur_set) { n   if (last_sets.find(cur_set) != last_sets.end()) n     return false; n   else n     last_sets.insert(cur_set); n   memset(used, false, 2500); n   used[start_y][start_x] = true; n   cur_set.insert( T ); n   multiset<cell, comp2> myque; n   cell tmp; n   tmp.pos = make_pair(start_y, start_x); n   tmp.s = string(  ); n   myque.insert(tmp); n   while (!myque.empty()) { n     cell cur_cell = *myque.begin(); n     myque.erase(myque.begin()); n     if (cur_cell.s.length() > min_len) break; n     vector<pair<int, int>> near = n         find_near(cur_cell.pos.first, cur_cell.pos.second, cur_set); n     for (vector<pair<int, int>>::iterator i = near.begin(); i != near.end(); n          i++) { n       if (field[i->first][i->second] ==  T ) { n         if (cur_cell.s.length() < min_len) { n           min_ways.clear(); n           min_len = cur_cell.s.length(); n         } n         min_ways.insert(cur_cell.s); n         break; n       } n       used[i->first][i->second] = true; n       cell tmp = cur_cell; n       tmp.pos = *i; n       tmp.s.push_back(field[i->first][i->second]); n       myque.insert(tmp); n     } n   } n   cur_set.erase( T ); n   return false; n } n int main() { n   read(); n   string s(  ); n   set<char> start_set = find_set_near(start_y, start_x); n   set<char> end_set = find_set_near(end_y, end_x); n   int l = alpha.size(); n   set<char> cur_set = set<char>(); n   if (!l) n     wave(cur_set); n   else { n     for (set<char>::iterator a = start_set.begin(); a != start_set.end(); a++) { n       cur_set.insert(*a); n       if (cur_set.size() == k || l < k && cur_set.size() == l) n         wave(cur_set); n       else { n         for (set<char>::iterator b = end_set.begin(); b != end_set.end(); b++) { n           cur_set.insert(*b); n           if (cur_set.size() == k || l < k && cur_set.size() == l) n             wave(cur_set); n           else { n             for (int i = 0; i < l; i++) { n               if (cur_set.end() != cur_set.find(alpha[i])) continue; n               cur_set.insert(alpha[i]); n               if (cur_set.size() == k || l < k && cur_set.size() == l) n                 wave(cur_set); n               else { n                 for (int j = i + 1; j < l; j++) { n                   if (cur_set.end() != cur_set.find(alpha[j])) continue; n                   cur_set.insert(alpha[j]); n                   if ((*a) == (*b) && cur_set.size() < k) { n                     set<char> second_set = n                         find_second_near_set(start_y, start_x, cur_set); n                     if (!second_set.size()) n                       wave(cur_set); n                     else { n                       for (set<char>::iterator p = second_set.begin(); n                            p != second_set.end(); p++) { n                         cur_set.insert(*p); n                         wave(cur_set); n                         cur_set.erase(*p); n                       } n                     } n                   } else n                     wave(cur_set); n                   cur_set.erase(alpha[j]); n                 } n               } n               cur_set.erase(alpha[i]); n             } n           } n           cur_set.erase(*b); n         } n       } n       cur_set.erase(*a); n     } n   } n label_end: n   if (min_len == INT_MAX) n     cout <<  -1 ; n   else n     cout << (*min_ways.begin()); n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> AE[1000005]; n long long par[1000005]; n enum LG { AND = 1, OR = 2, XOR = 3, NOT = 4, IN = 5 }; n long long type[1000005], inp[1000005]; n inline void nedge(long long a, long long b) { AE[a].push_back(b), par[b] = a; } n bool oans[1000005]; n bool dfs(long long p) { n   if (AE[p].size()) { n     volatile bool A = dfs(AE[p][0]), B = AE[p].size() > 1 ? dfs(AE[p][1]) : 0; n     if (type[p] == AND) n       oans[p] = (A and B); n     else if (type[p] == OR) n       oans[p] = (A or B); n     else if (type[p] == XOR) n       oans[p] = (A xor B); n     else if (type[p] == NOT) n       oans[p] = (not A); n   } else n     oans[p] = inp[p]; n   return oans[p]; n } n bool ans[1000005]; n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   ; n   long long n; n   cin >> n; n   for (long long i = 1; i <= n; i++) { n     string x; n     cin >> x; n     long long a, b; n     if (x ==  AND ) cin >> a >> b, nedge(i, a), nedge(i, b), type[i] = AND; n     if (x ==  OR ) cin >> a >> b, nedge(i, a), nedge(i, b), type[i] = OR; n     if (x ==  XOR ) cin >> a >> b, nedge(i, a), nedge(i, b), type[i] = XOR; n     if (x ==  NOT ) cin >> a, nedge(i, a), type[i] = NOT; n     if (x ==  IN ) cin >> a, inp[i] = a, type[i] = IN; n   } n   dfs(1); n   queue<long long> q; n   q.push(1); n   while (!q.empty()) { n     long long p = q.front(), pa = par[p]; n     q.pop(); n     bool nans = 0; n     if (p == 1) n       ans[p] = !oans[p]; n     else { n       long long a = AE[pa][0], b = AE[pa].size() > 1 ? AE[pa][1] : -1; n       if (type[pa] == AND) n         nans = ((oans[a] ^ (p == a)) and (oans[b] ^ (p == b))); n       else if (type[pa] == OR) n         nans = ((oans[a] ^ (p == a)) or (oans[b] ^ (p == b))); n       else if (type[pa] == XOR) n         nans = ((oans[a] ^ (p == a)) xor (oans[b] ^ (p == b))); n       else if (type[pa] == NOT) n         nans = (not(oans[a] ^ (p == a))); n       ans[p] = (nans != oans[pa]) ? ans[pa] : oans[1]; n     } n     for (long long x : AE[p]) q.push(x); n   } n   for (long long i = 1; i <= n; i++) n     if (type[i] == IN) cout << ans[i]; n   cout <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n struct osoba { n   string ime, prezime; n }; n int main() { n   int n; n   cin >> n; n   vector<osoba> osobe(n); n   vector<int> redoslijed(n); n   vector<string> minori(n); n   bool trigger = true; n   for (int i = 0; i < n; i++) cin >> osobe[i].ime >> osobe[i].prezime; n   for (int i = 0; i < n; i++) cin >> redoslijed[i]; n   if (strcmp(osobe[redoslijed[0] - 1].ime.c_str(), n              osobe[redoslijed[0] - 1].prezime.c_str()) < 0) n     minori[0] = osobe[redoslijed[0] - 1].ime; n   else n     minori[0] = osobe[redoslijed[0] - 1].prezime; n   for (int i = 1; i < n; i++) { n     if (strcmp(minori[i - 1].c_str(), osobe[redoslijed[i] - 1].ime.c_str()) > n             0 && n         strcmp(minori[i - 1].c_str(), n                osobe[redoslijed[i] - 1].prezime.c_str()) <= 0) n       minori[i] = osobe[redoslijed[i] - 1].prezime; n     else if (strcmp(minori[i - 1].c_str(), n                     osobe[redoslijed[i] - 1].ime.c_str()) <= 0 && n              strcmp(minori[i - 1].c_str(), n                     osobe[redoslijed[i] - 1].prezime.c_str()) > 0) n       minori[i] = osobe[redoslijed[i] - 1].ime; n     else if (strcmp(minori[i - 1].c_str(), n                     osobe[redoslijed[i] - 1].ime.c_str()) <= 0 && n              strcmp(minori[i - 1].c_str(), n                     osobe[redoslijed[i] - 1].prezime.c_str()) <= 0) { n       if (strcmp(osobe[redoslijed[i] - 1].ime.c_str(), n                  osobe[redoslijed[i] - 1].prezime.c_str()) < 0) n         minori[i] = osobe[redoslijed[i] - 1].ime; n       else n         minori[i] = osobe[redoslijed[i] - 1].prezime; n     } else { n       cout <<  NO  << endl; n       trigger = false; n       break; n     } n   } n   if (trigger) cout <<  YES  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void CyBerForCe() { n   long long n, k, x; n   cin >> n >> k >> x; n   vector<long long> v, u1, u2; n   long long f[1024] = {0}, temp[1024]; n   for (long long i = 0; i < n; i++) { n     long long a; n     cin >> a; n     v.push_back(a); n     f[a]++; n   } n   for (long long i = 0; i < k; i++) { n     for (long long j = 0; j < 1024; j++) temp[j] = 0; n     long long c = 0; n     for (long long j = 0; j < 1024; j++) { n       temp[j] += ((f[j] + c) / 2); n       temp[j ^ x] += ((f[j] + 1 - c) / 2); n       c = (c + f[j]) & 1; n     } n     for (long long j = 0; j < 1024; j++) f[j] = temp[j]; n   } n   long long mi = 1026, ma = -1; n   for (long long i = 0; i < 1024; i++) { n     if (f[i] > 0) { n       mi = min(mi, i); n       ma = max(ma, i); n     } n   } n   cout << ma <<     << mi <<   n ; n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   CyBerForCe(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e3 + 2; n bitset<N> equa[N], value[N]; n int ans[N], hehe[N]; n signed main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n, m, i, j, k, l, num = 0; n   cin >> n >> m; n   for (i = 0; i < n; i++) { n     value[2 * i][2 * i] = 1; n     value[2 * i + 1][2 * i + 1] = 1; n   } n   for (i = 0; i < m; i++) { n     string s; n     char x; n     cin >> s; n     if (s ==  mix ) { n       cin >> j; n       for (k = 0; k < j; k++) { n         cin >> l; n         l--; n         equa[num] ^= value[2 * l]; n         equa[num + 1] ^= value[2 * l + 1]; n       } n       cin >> x; n       if (x ==  R ) { n         ans[num + 1] = 1; n       } n       if (x ==  Y ) { n         ans[num] = 1; n       } n       if (x ==  B ) { n         ans[num] = ans[num + 1] = 1; n       } n       num += 2; n     } n     if (s ==  RB ) { n       cin >> j; n       for (k = 0; k < j; k++) { n         cin >> l; n         l--; n         value[2 * l] ^= value[2 * l + 1]; n       } n     } n     if (s ==  RY ) { n       cin >> j; n       for (k = 0; k < j; k++) { n         cin >> l; n         l--; n         swap(value[2 * n], value[2 * l]); n         swap(value[2 * l], value[2 * l + 1]); n         swap(value[2 * l + 1], value[2 * n]); n       } n     } n     if (s ==  YB ) { n       cin >> j; n       for (k = 0; k < j; k++) { n         cin >> l; n         l--; n         value[2 * l + 1] ^= value[2 * l]; n       } n     } n   } n   int now = -1; n   for (i = 0; i < 2 * n; i++) { n     for (j = now + 1; j < num; j++) { n       if (equa[j][i]) { n         now++; n         swap(equa[j], equa[now]); n         swap(ans[j], ans[now]); n         for (k = 0; k < num; k++) { n           if (equa[k][i] && k != now) { n             equa[k] ^= equa[now]; n             ans[k] ^= ans[now]; n           } n         } n         break; n       } n     } n   } n   for (i = now + 1; i < num; i++) { n     assert(!equa[i].count()); n     if (ans[i] == 1) { n       cout <<  NO ; n       return 0; n     } n   } n   for (i = now; i > -1; i--) { n     for (j = 2 * n - 1; j > -1; j--) { n       if (equa[i][j]) { n         if (equa[i].count() == 1) { n           equa[i][j] = 0; n           hehe[j] = ans[i]; n         } else { n           equa[i][j] = 0; n           for (k = j; k > -1; k--) { n             if (equa[i][k]) { n               break; n             } n           } n           for (l = 0; l < i; l++) { n             if (equa[l][k]) { n               equa[l] ^= equa[i]; n               ans[l] ^= ans[i]; n             } n           } n         } n       } n     } n   } n   cout <<  YES n ; n   for (i = 0; i < n; i++) { n     if (hehe[2 * i] && hehe[2 * i + 1]) { n       cout <<  B ; n     } n     if (hehe[2 * i] && !hehe[2 * i + 1]) { n       cout <<  Y ; n     } n     if (!hehe[2 * i] && hehe[2 * i + 1]) { n       cout <<  R ; n     } n     if (!hehe[2 * i] && !hehe[2 * i + 1]) { n       cout <<  . ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const long long LINF = 0x3f3f3f3f3f3f3f3fLL; n const double EPS = 1e-8; n const int MOD = 998244353; n const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1}; n struct IOSetup { n   IOSetup() { n     cin.tie(nullptr); n     ios_base::sync_with_stdio(false); n     cout << fixed << setprecision(20); n     cerr << fixed << setprecision(10); n   } n } iosetup; n int mod = MOD; n struct ModInt { n   unsigned val; n   ModInt() : val(0) {} n   ModInt(long long x) : val(x >= 0 ? x % mod : x % mod + mod) {} n   ModInt pow(long long exponent) { n     ModInt tmp = *this, res = 1; n     while (exponent > 0) { n       if (exponent & 1) res *= tmp; n       tmp *= tmp; n       exponent >>= 1; n     } n     return res; n   } n   ModInt &operator+=(const ModInt &rhs) { n     if ((val += rhs.val) >= mod) val -= mod; n     return *this; n   } n   ModInt &operator-=(const ModInt &rhs) { n     if ((val += mod - rhs.val) >= mod) val -= mod; n     return *this; n   } n   ModInt &operator*=(const ModInt &rhs) { n     val = static_cast<unsigned long long>(val) * rhs.val % mod; n     return *this; n   } n   ModInt &operator/=(const ModInt &rhs) { return *this *= rhs.inv(); } n   bool operator==(const ModInt &rhs) const { return val == rhs.val; } n   bool operator!=(const ModInt &rhs) const { return val != rhs.val; } n   bool operator<(const ModInt &rhs) const { return val < rhs.val; } n   bool operator<=(const ModInt &rhs) const { return val <= rhs.val; } n   bool operator>(const ModInt &rhs) const { return val > rhs.val; } n   bool operator>=(const ModInt &rhs) const { return val >= rhs.val; } n   ModInt &operator++() { n     if (++val == mod) val = 0; n     return *this; n   } n   ModInt operator++(int) { n     ModInt res = *this; n     ++*this; n     return res; n   } n   ModInt &operator--() { n     val = (val == 0 ? mod : val) - 1; n     return *this; n   } n   ModInt operator--(int) { n     ModInt res = *this; n     --*this; n     return res; n   } n   ModInt operator+() const { return *this; } n   ModInt operator-() const { return ModInt(val ? mod - val : 0); } n   ModInt operator+(const ModInt &rhs) const { return ModInt(*this) += rhs; } n   ModInt operator-(const ModInt &rhs) const { return ModInt(*this) -= rhs; } n   ModInt operator*(const ModInt &rhs) const { return ModInt(*this) *= rhs; } n   ModInt operator/(const ModInt &rhs) const { return ModInt(*this) /= rhs; } n   friend ostream &operator<<(ostream &os, const ModInt &rhs) { n     return os << rhs.val; n   } n   friend istream &operator>>(istream &is, ModInt &rhs) { n     long long x; n     is >> x; n     rhs = ModInt(x); n     return is; n   } n  n  private: n   ModInt inv() const { n     unsigned a = val, b = mod; n     int x = 1, y = 0; n     while (b) { n       unsigned tmp = a / b; n       swap(a -= tmp * b, b); n       swap(x -= tmp * y, y); n     } n     return ModInt(x); n   } n }; n ModInt abs(const ModInt &x) { return x; } n struct Combinatorics { n   int val; n   vector<ModInt> fact, fact_inv, inv; n   Combinatorics(int val = 10000000) n       : val(val), fact(val + 1), fact_inv(val + 1), inv(val + 1) { n     fact[0] = 1; n     for (int i = (1); i < (val + 1); ++i) fact[i] = fact[i - 1] * i; n     fact_inv[val] = ModInt(1) / fact[val]; n     for (int i = val; i > 0; --i) fact_inv[i - 1] = fact_inv[i] * i; n     for (int i = (1); i < (val + 1); ++i) inv[i] = fact[i - 1] * fact_inv[i]; n   } n   ModInt nCk(int n, int k) { n     if (n < 0 || n < k || k < 0) return ModInt(0); n     return fact[n] * fact_inv[k] * fact_inv[n - k]; n   } n   ModInt nPk(int n, int k) { n     if (n < 0 || n < k || k < 0) return ModInt(0); n     return fact[n] * fact_inv[n - k]; n   } n   ModInt nHk(int n, int k) { n     if (n < 0 || k < 0) return ModInt(0); n     return (k == 0 ? ModInt(1) : nCk(n + k - 1, k)); n   } n }; n int main() { n   int n, m, k; n   cin >> n >> m >> k; n   vector<vector<ModInt> > dp(k + 1, vector<ModInt>(k + 1, 0)); n   dp[0][0] = 1; n   for (int i = (0); i < (k); ++i) n     for (int j = (0); j < (i + 1); ++j) { n       dp[i + 1][j] += dp[i][j] * j; n       dp[i + 1][j + 1] += dp[i][j] * (n - j); n     } n   ModInt ans = 0; n   for (int j = (0); j < (k + 1); ++j) ans += dp[k][j] / ModInt(m).pow(j); n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int ans, t, p, q, i, j, n, m, k, l, r, br1, br0; n bool b[36][36]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cin >> t; n   while (t--) { n     cin >> n >> m; n     ans = 0; n     vector<pair<int, int> > v; n     for (i = 1; i <= n; i++) { n       for (j = 1; j <= m; j++) { n         cin >> b[i][j]; n       } n     } n     for (i = 1; i <= m; i++) { n       p = 1; n       q = i; n       br1 = br0 = 0; n       while (p <= n && q > 0) { n         if (b[p][q] == 1) n           br1++; n         else n           br0++; n         p++; n         q--; n       } n       v.push_back({br0, br1}); n     } n     for (i = 2; i <= n; i++) { n       p = i; n       q = m; n       br1 = br0 = 0; n       while (p <= n && q > 0) { n         if (b[p][q] == 1) n           br1++; n         else n           br0++; n         p++; n         q--; n       } n       v.push_back({br0, br1}); n     } n     for (i = 0; i < v.size() / 2; i++) { n       pair<int, int> pr = v[i]; n       pair<int, int> pr1 = v[v.size() - i - 1]; n       ans += min(pr.first + pr1.first, pr.second + pr1.second); n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long n; n   cin >> n; n   vector<long long> a(n), t(n); n   vector<pair<long long, long long> > vp; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     vp.push_back({a[i], i}); n   } n   for (int i = 0; i < n; i++) cin >> t[i]; n   long long ans = 0; n   sort(vp.begin(), vp.end()); n   long long sum = 0; n   priority_queue<long long> pq; n   int i = 0; n   while (i < n) { n     int j; n     for (j = i; j < n; j++) { n       if (vp[i].first == vp[j].first) { n         sum += t[vp[j].second]; n         pq.push(t[vp[j].second]); n       } else n         break; n     } n     long long num = vp[i].first; n     while (j < n && pq.empty() == 0 && num < vp[j].first) { n       sum -= pq.top(); n       pq.pop(); n       ans += sum; n       num++; n     } n     i = j; n   } n   while (pq.empty() == 0) { n     sum -= pq.top(); n     pq.pop(); n     ans += sum; n   } n   cout << ans <<   n ; n } n int main(void) { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   int t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<char, int> m; n bool judge(string str) { n   int count = 0; n   for (int i = 0; i < 3; i++) { n     if (str[i] ==  . ) break; n     if (str[i] ==  A  && m[ A ] == 0) { n       m[ A ] = 1; n       count++; n     } else if (str[i] ==  B  && m[ B ] == 0) { n       m[ B ] = 1; n       count++; n     } else if (str[i] ==  C  && m[ C ] == 0) { n       m[ C ] = 1; n       count++; n     } n     if (count >= 3) return true; n   } n   return false; n } n int main() { n   string str; n   cin >> str; n   int siz = str.size(); n   int count = 0; n   for (int i = 0; i <= siz - 3; i++) { n     m[ A ] = 0; n     m[ B ] = 0; n     m[ C ] = 0; n     string sstr = str.substr(i, 3); n     if (judge(sstr)) { n       cout <<  Yes  << endl; n       return 0; n     } n   } n   cout <<  No  << endl; n   return 0; n } n


#include <bits/stdc++.h> n const int MaxN = 1e6 + 255; n const long long INF = 1e18 + 7; n const int Mod = 1e9 + 7; n using namespace std; n long long n, a, f[MaxN], ans; n int main() { n   scanf( %lld , &n); n   for (int i = 1; i <= n; ++i) { n     scanf( %lld , &a); n     ++f[a]; n   } n   for (int i = 0; i < MaxN; ++i) { n     if (f[i]) { n       f[i + 1] += f[i] / 2; n       f[i] %= 2; n     } n   } n   for (int i = 0; i < MaxN; ++i) n     if (f[i]) ++ans; n   printf( %lld , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 200 * 1000 + 5; n pair<long long int, long long int> b[MAXN]; n long long int dp[MAXN]; n long long int rem[MAXN]; n inline pair<long long int, long long int> cst(int x, int y) { n   long long int tt = ((b[x].first - rem[y] + b[y].second - 1) / b[y].second); n   return make_pair(tt + dp[y], rem[y] + tt * b[y].second - b[x].first); n } n bool cmp(const pair<long long int, long long int>& x, n          const pair<long long int, long long int>& y) { n   if (x.first != y.first) return x.first < y.first; n   return x.second > y.second; n } n inline bool isBet(int i, int j, int k) { return !cmp(cst(i, k), cst(i, j)); } n int main() { n   ios::sync_with_stdio(false); n   int n; n   long long int s; n   cin >> n >> s; n   for (int i = 0; i < n; i++) cin >> b[i].second >> b[i].first; n   b[n].first = s; n   sort(b, b + n, cmp); n   assert(!b[0].first); n   deque<pair<int, int> > cands; n   cands.push_back(make_pair(0, 1)); n   long long int ans = cst(n, 0).first; n   long long int lst = b[0].second; n   for (int i = 1; i < n; i++) { n     if (b[i].second <= lst) continue; n     while (cands.size() > 1 && isBet(i, cands[1].first, cands[0].first)) n       cands.pop_front(); n     pair<long long int, long long int> tmp = cst(i, cands[0].first); n     dp[i] = tmp.first; n     rem[i] = tmp.second; n     ans = min(ans, cst(n, i).first); n     while (cands.size() && isBet(cands.back().second, i, cands.back().first)) n       cands.pop_back(); n     if (cands.size()) { n       int d = cands.back().second, u = n - 1; n       while (d < u - 1) { n         int mid = (d + u) / 2; n         if (isBet(mid, i, cands.back().first)) n           u = mid; n         else n           d = mid; n       } n       if (isBet(u, i, cands.back().first)) cands.push_back(make_pair(i, u)); n     } n     lst = b[i].second; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   double ans = 0; n   long long m, r, i; n   cin >> m >> r; n   if (m == 1) { n     cout << 2 * r; n     return 0; n   } n   for (i = 1; i <= m; i++) { n     if ((i == 1) || (i == m)) n       ans += (4 + sqrt(2.) + 2 * (m - 2) * sqrt(2.) + (m - 2) * (m - 1)); n     else n       ans += (6 + 2 * sqrt(2.) + (m - 3) * 2 * sqrt(2.) + (i - 2) * (i - 1) + n               (m - i - 1) * (m - i)); n   } n   printf( %.10lf n , ans * r / m / m); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int x[1001], y[1001], a[1001], b[1001]; n map<pair<int, int>, int> freq; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> x[i] >> y[i]; n   } n   for (int j = 1; j <= n; j++) { n     cin >> a[j] >> b[j]; n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       freq[{x[i] + a[j], y[i] + b[j]}]++; n     } n   } n   for (auto p : freq) { n     if (p.second == n) { n       cout << p.first.first <<     << p.first.second << endl; n       return 0; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long i, j, n, m, k, a, b; n   long long x[3], y[3]; n   for (i = 0; i < 3; i++) cin >> x[i] >> y[i]; n   cout << 3 << endl; n   for (i = 0; i < 3; i++) n     cout << (x[i] + x[(i + 1) % 3]) - x[(i + 2) % 3] <<     n          << (y[i] + y[(i + 1) % 3]) - y[(i + 2) % 3] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using vi = vector<int>; n using vvi = vector<vi>; n using pii = pair<int, int>; n constexpr auto MOD = 1000000007LL; n constexpr auto LINF = (1LL << 60); n constexpr auto INF = 2147483647LL; n constexpr auto PI = 3.1415926535897932384626433; n constexpr auto EPS = 1E-9; n template <typename T1, typename T2> n ostream &operator<<(ostream &out, const pair<T1, T2> p) { n   out << p.first <<     << p.second; n   return out; n } n template <typename T1, typename T2> n istream &operator>>(istream &in, pair<T1, T2> &p) { n   in >> p.first >> p.second; n   return in; n } n template <typename T> n istream &operator>>(istream &in, vector<T> &v) { n   for (auto &x : v) in >> x; n   return in; n } n template <typename T> n ostream &operator<<(ostream &out, vector<T> v) { n   for (int i = 0; i < v.size(); i++) n     out << v[i] << (i + 1 == v.size() ?   n  :    ); n   out.flush(); n   return out; n } n int n, k, a[2][300010]; n int tail[2], nxttail[2]; n int main() { n   ios_base::sync_with_stdio(false); n   cin >> n >> k; n   for (int i = 0; i < n; i++) cin >> a[0][i]; n   for (int i = 0; i < n; i++) cin >> a[1][i]; n   if (k == 1) { n     int tail = 3; n     for (int i = 0; i < n; i++) { n       if (abs(a[0][i] - a[1][i]) > 1) { n         cout <<  NO  << endl; n         return 0; n       } n       if (a[0][i] > a[1][i]) { n         if ((tail & 2) == 0) { n           cout <<  NO  << endl; n           return 0; n         } n         tail = 1; n       } else if (a[1][i] > a[0][i]) { n         if ((tail & 1) == 0) { n           cout <<  NO  << endl; n           return 0; n         } n         tail = 2; n       } else n         ; n     } n     cout <<  YES  << endl; n     return 0; n   } n   for (int i = 0; i < n; i++) { n     nxttail[0] = nxttail[1] = INF; n     for (int t = 0; t < 2; t++) { n       if (tail[t] > k) continue; n       if (a[t][i] >= a[t ^ 1][i]) { n         int groups = 1 + (a[t][i] - (k - tail[t]) + k - 1) / k; n         if (a[t ^ 1][i] < groups - 1) n           continue; n         else if (a[t ^ 1][i] == groups - 1) { n           nxttail[t] = min(nxttail[t], (a[t][i] - (k - tail[t]) - 1) % k + 1); n         } else if (a[t ^ 1][i] > groups - 1) { n           nxttail[t ^ 1] = nxttail[t] = 1; n         } n       } else { n         int groups = (a[t ^ 1][i] + k - 1) / k; n         if (a[t][i] < groups - 1) n           continue; n         else if (a[t][i] == groups - 1) { n           nxttail[t ^ 1] = min(nxttail[t ^ 1], (a[t ^ 1][i] - 1) % k + 1); n         } else if (a[t][i] > groups - 1) { n           nxttail[t] = nxttail[t ^ 1] = 1; n         } n       } n     } n     tail[0] = nxttail[0]; n     tail[1] = nxttail[1]; n   } n   cout << (min(tail[0], tail[1]) <= k ?  YES  :  NO ) << endl; n   return 0; n } n


#include <bits/stdc++.h> n  n using namespace std; n  n typedef int int2; n #define int long long n  n #define pi pair<int, int> n #define mp make_pair n #define pb push_back n #define eb emplace_back n #define f first n #define s second n #define INF 1e18 n  n string printed(int mask) { n     string res; n     for (int i = 0; i < 26; i++) { n         if (mask & (1 << i)) { n             res +=  1 ; n         } else { n             res +=  0 ; n         } n     } n     return res; n } n  n int2 main() { n     ios::sync_with_stdio(0); n     cin.tie(0); n  n     int n, a, b; n     cin >> n >> a >> b; n     string st; n     cin >> st; n  n     vector<int> elts_prev(n); n     vector<int> mapping(26); n     vector<int> backmap(26); n     vector<int> elts(n); n     fill(mapping.begin(), mapping.end(), -1); n     int curmap = 0; n     for (int i = 0; i < n; i++) { n         elts_prev[i] = st[i] -  a ; n         // cout <<  mapping:   << mapping[elts_prev[i]] <<   n ; n         if (mapping[elts_prev[i]] == -1) { n             mapping[elts_prev[i]] = curmap; n             backmap[curmap] = elts_prev[i]; n             curmap++; n             // cout <<  i:   << i <<   , curmap:   << curmap <<  , elts_prev:   << elts_prev[i] <<   n ; n         } n     } n     for (int i = 0; i < n; i++) { n         elts[i] = mapping[elts_prev[i]]; n     } n     vector<int> l(curmap), r(curmap), freq(curmap); n     fill(l.begin(), l.end(), INF); n     fill(r.begin(), r.end(), -INF); n     for (int i = 0; i < n; i++) { n         l[elts[i]] = min(l[elts[i]], i); n         r[elts[i]] = max(r[elts[i]], i); n         freq[elts[i]]++; n     } n  n     int maxn = 1 << curmap; n  n     vector<bool> satisfy(maxn); n     vector<int> dp(maxn); n     fill(dp.begin(), dp.end(), -1); n  n     dp[0] = 1; n  n     // cout <<  curmap:   << curmap <<   n ; n     // if (n == 5000 and a == 1 and b == 1 and st[0] ==  o ) { n     //     cout <<  curmap:   << curmap <<   n ; n     // } n     function<int(int)> solve = [&](int i) { n         if (~dp[i]) return dp[i]; n         dp[i] = 0; n         int ll = INF, rr = -INF; n         int amt = 0; n         vector<int> elts; n         for (int j = 0; j < curmap; j++) { n             if (i & (1 << j)) { n                 rr = max(rr, r[j]); n                 ll = min(ll, l[j]); n                 amt += freq[j]; n                 elts.pb(j); n             } n         } n  n         // want amt >= (a / b) * (rr - ll + 1), i.e. amt >= ceil((a / b) * (rr - ll + 1)), i.e. amt >= (a * (rr - ll + 1) + (b - 1)) / b n         bool can_change = (amt >= ((a * (rr - ll + 1) + (b - 1)) / b)); n  n         // Case 1: change all elements to color j n         if (can_change) { n             for (int j: elts) { n                 dp[i] |= solve(i ^ (1 << j)); n             } n         } n  n         // Case 2: multiple subtrees n         int cur_mask = 0; n         for (int j: elts) { n             cur_mask |= (1 << j); n             if (cur_mask == i) break; n             dp[i] |= (solve(cur_mask) & solve(i ^ cur_mask)); n         } n         // cout <<  mask:   << printed(i) <<  , ll:   << ll <<  , rr:   << rr <<  , amt:   << amt <<  , dp:   << dp[i] <<   n ; n         return dp[i]; n     }; n  n     vector<int> ans; n     for (int i = 0; i < curmap; i++) { n         if (solve((maxn - 1) ^ (1 << i)) == 1) { n             ans.pb(backmap[i]); n         } n     } n     sort(ans.begin(), ans.end()); n     cout << ans.size() <<    ; n     for (int i = 0; i < (int)ans.size(); i++) { n         cout << (char)( a  + ans[i]) <<    ; n     } n     cout <<   n ; n }


#include <bits/stdc++.h> n using namespace std; n int d[1000005]; n bool vis[1000005]; n vector<pair<int, int> > G[1000005]; n void dfs(int u) { n   for (pair<int, int> x : G[u]) n     if (!vis[x.first]) { n       vis[x.first] = 1; n       dfs(x.second); n     } n } n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   long long ans = 0; n   int tmp; n   for (int i = 0; i < m; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     tmp = x; n     if (x == y) n       ans++; n     else n       d[x]++, d[y]++; n     G[x].push_back(make_pair(i, y)); n     G[y].push_back(make_pair(i, x)); n   } n   dfs(tmp); n   bool test = 1; n   for (int i = 0; i < m; i++) n     if (!vis[i]) { n       test = 0; n       break; n     } n   if (test) { n     ans = ans * (m - 1) - ans * (ans - 1) / 2; n     for (int i = 1; i <= n; i++) ans += (long long)d[i] * (d[i] - 1) / 2; n     printf( %I64d n , ans); n   } else n     puts( 0 ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int i, t, count; n   long long x, N; n   scanf( %d n , &t); n   while (t--) { n     scanf( %lld n , &N); n     count = 0; n     for (i = 1; i < 10; i++) { n       x = i; n       while (x <= N) { n         count++; n         x *= 10; n         x += i; n       } n     } n     printf( %d n , count); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int Maxn = 20; n const int Maxmask = (1 << 16); n int out[Maxn][Maxn]; n bool making[Maxn][Maxmask]; n int n; n int s; n int a[Maxn * Maxn]; n int mod; n void input() { n   cin >> n; n   for (int i = 0; i < n * n; i++) { n     cin >> a[i]; n     s += a[i]; n   } n   s /= n; n } n void place() { n   for (int i = 0; i < n * n; i++) out[i / n][i % n] = a[i]; n } n bool is_true() { n   int t; n   for (int i = 0; i < n; i++) { n     t = 0; n     for (int j = 0; j < n; j++) t += out[i][j]; n     if (t != s) return false; n     t = 0; n     for (int j = 0; j < n; j++) t += out[j][i]; n     if (t != s) return false; n   } n   t = 0; n   for (int i = 0; i < n; i++) t += out[i][i]; n   if (t != s) return false; n   t = 0; n   for (int i = 0; i < n; i++) t += out[i][n - i - 1]; n   if (t != s) return false; n   return true; n } n bool found() { n   int b[] = {2, 7, 6, 9, 5, 1, 4, 3, 8}; n   for (int i = 0; i < n * n; i++) n     if (a[i] != b[i]) return false; n   return true; n } n void calc_3() { n   sort(a, a + n * n); n   while (1) { n     place(); n     if (is_true()) break; n     next_permutation(a, a + (n * n)); n   } n } n void output() { n   cout << s <<   n ; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) cout << out[i][j] <<    ; n     cout <<   n ; n   } n } n int main() { n   input(); n   calc_3(); n   output(); n   return 0; n } n


#include <bits/stdc++.h> n const int maxn = 200001, maxf = 19; n const long long INF = 0x3f3f3f3f3f3f3f3fLL; n int n, q, tot, lnk[maxn], dep[maxn], fa[maxn][maxf]; n struct Edge { n   int v, nxt; n } e[maxn << 1]; n template <class T> n void swap(T& x, T& y) { n   const T t = x; n   x = y; n   y = t; n } n template <class T> n const T& min(const T& x, const T& y) { n   return x < y ? x : y; n } n void dfs(int u) { n   for (int it = lnk[u]; it; it = e[it].nxt) { n     int& v = e[it].v; n     if (v == fa[u][0]) continue; n     dep[v] = dep[u] + 1; n     fa[v][0] = u; n     for (int i = 1; fa[v][i - 1]; ++i) fa[v][i] = fa[fa[v][i - 1]][i - 1]; n     dfs(v); n   } n } n int deeper(int u, int v) { return dep[u] < dep[v] ? v : u; } n int lca(int u, int v) { n   if (dep[u] > dep[v]) swap(u, v); n   for (int i = 0, j = dep[v] - dep[u]; j; ++i, j >>= 1) n     if (j & 1) v = fa[v][i]; n   if (u == v) return u; n   for (int i = maxf - 1; i >= 0; --i) n     if (fa[u][i] != fa[v][i]) { n       u = fa[u][i]; n       v = fa[v][i]; n     } n   return fa[u][0]; n } n int dist(int u, int v) { return dep[u] + dep[v] - (dep[lca(u, v)] << 1); } n int gcd(int x, int y) { return y ? gcd(y, x % y) : x; } n void exgcd(int a, int b, int& x, int& y) { n   if (!b) { n     x = 1; n     y = 0; n     return; n   } n   exgcd(b, a % b, y, x); n   y -= x * (a / b); n } n long long Case1(int t1, int d1, int t2, int d2) { n   int g = gcd(t1, t2), r = (d2 - d1) % t2; n   if (r < 0) r += t2; n   if (r % g != 0) return INF; n   int x, y; n   t1 /= g; n   t2 /= g; n   r /= g; n   exgcd(t1, t2, x, y); n   if (x < 0) x += t2; n   x = (long long)x * r % t2; n   return (long long)x * t1 * g + d1; n } n long long AMAC(int m, int d, int l, int r) { n   if (l % d == 0) return l / d; n   if (l / d != r / d) return l / d + 1; n   int g = gcd(m, d); n   if (l % g != 0 && l / g == r / g) return -1; n   long long res = AMAC(d, m % d, -r % d + d, -l % d + d); n   if (res == -1) return -1; n   return (res * m + l - 1) / d + 1; n } n long long Case2(int t1, int d1, int t2, int d2, int dd) { n   int m = t2, d = t1, l = (d2 - d1 - dd) % t2, r = (d2 - d1 + dd) % t2; n   if (l < 0) l += t2; n   if (r < 0) r += t2; n   if (l & 1) return INF; n   if (t2 == dd * 2 || l > r || l == 0) return d1 + r / 2; n   if (!(d %= m)) return INF; n   long long res = AMAC(m, d, l, r); n   if (res == -1) return INF; n   return t1 * res + d1 + dd - (t1 * res % t2 - l) / 2; n   return t1 * res + d1 + (((-t1 * res + d2 + dd - d1) / 2) % t2 + t2) % t2; n } n int main() { n   scanf( %d , &n); n   for (int i = 1, u, v; i < n; ++i) { n     scanf( %d%d , &u, &v); n     e[++tot] = (Edge){v, lnk[u]}; n     lnk[u] = tot; n     e[++tot] = (Edge){u, lnk[v]}; n     lnk[v] = tot; n   } n   dfs(1); n   scanf( %d , &q); n   while (q--) { n     int u, v, x, y; n     scanf( %d%d%d%d , &u, &v, &x, &y); n     int px = deeper(lca(u, x), lca(v, x)); n     int py = deeper(lca(u, y), lca(v, y)); n     int pp = deeper(lca(u, v), lca(x, y)); n     if (dep[px] < dep[pp] && dep[py] < dep[pp]) { n       puts( -1 ); n       continue; n     } n     if (dep[px] < dep[pp]) n       px = pp; n     else if (dep[py] < dep[pp]) n       py = pp; n     int f1 = dist(u, v) << 1, f2 = dist(x, y) << 1, d = dist(px, py); n     int t1 = dist(u, px), t2 = dist(u, py), t3 = dist(x, px), t4 = dist(x, py); n     if (t1 <= t2) n       t2 = f1 - t2; n     else n       t1 = f1 - t1; n     if (t3 <= t4) n       t4 = f2 - t4; n     else n       t3 = f2 - t3; n     long long ans = min(min(Case1(f1, t1, f2, t3), Case2(f1, t1, f2, t4, d)), n                         min(Case1(f1, t2, f2, t4), Case2(f1, t2, f2, t3, d))); n     if (ans == INF) n       puts( -1 ); n     else n       printf( %I64d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1e6 + 1; n void solve() { n   long long n, i = 0, k; n   cin >> n; n   long long a[1000], m[1000]; n   m[0] = 1; n   a[0] = 1; n   while (m[i] < n) { n     a[i + 1] = 2 * a[i]; n     m[i + 1] = 2 * a[i + 1] - 1; n     i++; n   } n   k = i; n   if (n >= m[k - 1] + a[k - 1]) n     a[k] = n - m[k - 1]; n   else { n     if (n == a[k - 1] + 1) { n       for (i = 1; i <= k; i++) a[i] = a[i - 1]; n     } else { n       while (1) { n         a[k - 1] = (n - m[k - 2]) / 2; n         m[k - 1] = a[k - 1] + m[k - 2]; n         if (n >= a[k - 1] + m[k - 1]) { n           a[k] = n - m[k - 1]; n           break; n         } n       } n     } n   } n   cout << k <<   n ; n   for (i = 1; i <= k; i++) { n     cout << a[i] - a[i - 1] <<    ; n   } n   cout <<   n ; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long t, p; n   cin >> t; n   for (p = 1; p <= t; p++) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int aa(int n) { n   int a = 0; n   for (int i = 2; i <= n - 1; i++) n     if (n % i == 0) { n       a = 1; n       break; n     } else n       a = 0; n   return a; n } n int main() { n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     if (aa(i) == 1 && aa(n - i) == 1) { n       cout << i <<     << n - i << endl; n       break; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n void blowback() { n   long long i, j, l, k, n, m, x, y, r, ans = 0, sum = 10e17; n   string s; n   long long a, b, c, d; n   cin >> a >> b >> c >> d; n   if (a > (b * c)) { n     cout <<  -1 n ; n     return; n   } n   k = (2 * a - b * d) / (2 * b * d); n   l = k + 1; n   k = min(k, c / d); n   l = min(l, c / d); n   ans = (a * (k + 1)) - ((k * (k + 1)) * b * d) / 2; n   k = l; n   sum = (a * (k + 1)) - ((k * (k + 1)) * b * d) / 2; n   cout << max({ans, sum, a}) <<   n ; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long int t = 1; n   cin >> t; n   while (t--) { n     blowback(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = numeric_limits<int>::max(); n const long long LLINF = numeric_limits<long long>::max(); n const unsigned long long ULLINF = numeric_limits<unsigned long long>::max(); n long long f[1010][1010]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     long long x, y; n     cin >> x >> y; n     x--, y--; n     f[y][x]++; n   } n   long long ans = 0; n   for (int i = 0; i < 1000; i++) { n     long long cur = 0; n     for (int j = 0; j + i < 1000; j++) { n       cur += f[j][i + j]; n     } n     ans += (cur - 1) * cur / 2ll; n     cur = 0; n     if (i > 0) { n       for (int j = 0; j + i < 1000; j++) { n         cur += f[i + j][j]; n       } n       ans += (cur - 1) * cur / 2ll; n     } n   } n   for (int i = 0; i < 1000; i++) { n     long long cur = 0; n     for (int j = 0; j <= i; j++) { n       cur += f[j][i - j]; n     } n     ans += (cur - 1) * cur / 2ll; n     cur = 0; n     if (i > 0) { n       for (int j = 0; j + i < 1000; j++) { n         cur += f[i + j][1000 - j - 1]; n       } n       ans += (cur - 1) * cur / 2ll; n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dx[] = {0, 0, 1, -1, 1, -1, 1, -1}; n int dy[] = {1, -1, 0, 0, -1, 1, 1, -1}; n long long gcd(long long first, long long second) { n   return !second ? first : gcd(second, first % second); n } n long long lcm(long long first, long long second) { n   return (first / gcd(first, second)) * second; n } n void PLAY() { n   cout << fixed << setprecision(15); n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n } n const int MAX = 6e5; n long long n, r, k; n long long archers[MAX], dist[MAX]; n bool valid(long long val) { n   memset(dist, 0, sizeof dist); n   long long tmp = 0, cursum = k; n   for (int i = 1; i <= n; ++i) { n     long long cur = val - archers[i] - tmp; n     if (cur < 0) { n       tmp += dist[i]; n       continue; n     } n     if (cur > cursum) return false; n     cursum -= cur; n     tmp += cur; n     long long idx = min(i + 2 * r, n); n     dist[idx] -= cur; n     tmp += dist[i]; n   } n   return true; n } n int main() { n   PLAY(); n   cin >> n >> r >> k; n   for (int i = 1; i <= n; i++) { n     cin >> archers[i]; n     long long x = max(i - r, 1LL); n     long long y = min(i + r, n) + 1; n     dist[x] += archers[i]; n     dist[y] -= archers[i]; n   } n   for (int i = 1; i <= n; i++) archers[i] = archers[i - 1] + dist[i]; n   long long lo = *min_element(archers + 1, archers + n + 1); n   long long hi = lo + k + 1, res = lo; n   while (lo <= hi) { n     long long mid = (lo + hi) / 2; n     if (valid(mid)) { n       res = mid; n       lo = mid + 1; n     } else n       hi = mid - 1; n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e9; n const int MODV = 1e9 + 7; n int b[1001]; n int ans[1005]; n vector<pair<int, int> > sorted; n vector<pair<int, int> > unsorted; n void make_ans(int n) { n   for (auto it : sorted) { n     int x = it.first; n     int y = it.second; n     for (int i = x; i < y; i++) b[i] = 0; n   } n   ans[1] = 100000; n   for (int i = 2; i <= n; i++) ans[i] = ans[i - 1] + b[i - 1]; n } n int non_decreasing(int x, int y) { n   for (int i = x + 1; i <= y; i++) { n     if (ans[i - 1] > ans[i]) return 0; n   } n   return 1; n } n int check() { n   for (auto it : unsorted) { n     if (non_decreasing(it.first, it.second)) return 0; n   } n   return 1; n } n int main() { n   int n, m; n   scanf( %d %d , &n, &m); n   while (m--) { n     int t, x, y; n     scanf( %d %d %d , &t, &x, &y); n     if (t) n       sorted.push_back({x, y}); n     else n       unsorted.push_back({x, y}); n   } n   sort(sorted.begin(), sorted.end()); n   memset(b, -1, sizeof(b)); n   make_ans(n); n   if (check()) { n     printf( YES n ); n     for (int i = 1; i <= n; i++) printf( %d  , ans[i]); n     printf(  n ); n   } else n     printf( NO n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int q; n   cin >> q; n   while (q--) { n     int n, rec = 0; n     long long int k; n     cin >> n >> k; n     char str[1000005]; n     cin >> str; n     for (int h = 0; h < n; h++, rec++) { n       int pos = h; n       for (int i = h; i < n; i++) { n         if (str[i] ==  0 ) { n           pos = i; n           break; n         } n       } n       h = pos; n       if (k - pos - rec >= 0) { n         char temp = str[pos]; n         str[pos] = str[rec]; n         str[rec] = temp; n         k -= (pos - rec); n       } else if (k > 0) { n         while (pos > 0 && str[pos - 1] ==  1  && k > 0) { n           char temp = str[pos]; n           str[pos] = str[pos - 1]; n           str[pos - 1] = temp; n           pos--; n           k--; n         } n       } n     } n     cout << str << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 200005; n set<int> nex[M]; n int path[M]; n int sh[M]; n vector<int> edge[M]; n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   for (int i = 0; i < m; i++) { n     int from, to; n     scanf( %d%d , &from, &to); n     edge[to].push_back(from); n   } n   for (int i = 1; i <= n; i++) { n     sh[i] = -1; n   } n   int k; n   scanf( %d , &k); n   for (int i = 1; i <= k; i++) { n     int p; n     scanf( %d , &p); n     path[i] = p; n   } n   queue<int> q; n   q.push(path[k]); n   sh[path[k]] = 0; n   int count = 1; n   while (!q.empty()) { n     int len = q.size(); n     for (int i = 0; i < len; i++) { n       int x = q.front(); n       q.pop(); n       for (int v : edge[x]) { n         if (sh[v] == -1) { n           q.push(v); n           sh[v] = count; n           nex[v].insert(x); n         } else if (sh[v] == count) { n           nex[v].insert(x); n         } n       } n     } n     count++; n   } n   int mi = 0, ma = 0; n   for (int i = 1; i < k; i++) { n     if (nex[path[i]].find(path[i + 1]) == nex[path[i]].end()) { n       mi++; n       ma++; n     } else if (nex[path[i]].size() > 1) { n       ma++; n     } n   } n   printf( %d %d n , mi, ma); n } n


#include <bits/stdc++.h> n using namespace std; n int bit[210000], n; n void upd(int i) { n   while (i <= n) bit[i] += 1, i += i & (-i); n } n void dec(int i) { n   while (i <= n) bit[i] -= 1, i += i & (-i); n } n int cal(int i) { n   int ret = 0; n   while (i > 0) ret += bit[i], i -= i & (-i); n   return ret; n } n int solve(int x) { n   int l = 1, r = n + 1; n   while (l < r) { n     int mid = (l + r) >> 1, tx = cal(mid); n     if (tx < x) n       l = mid + 1; n     else n       r = mid; n   } n   return l; n } n int a[210000], b[210000], c[210000], va[210000], vb[210000], vc[210000]; n int main() { n   while (cin >> n) { n     for (int i = 0; i < n; ++i) { n       scanf( %d , a + i); n       a[i]++; n     } n     for (int i = 0; i < n; ++i) { n       scanf( %d , b + i); n       b[i]++; n     } n     memset(bit, 0, sizeof(bit)); n     for (int i = n - 1; i >= 0; --i) { n       va[i] = cal(a[i]); n       upd(a[i]); n     } n     memset(bit, 0, sizeof(bit)); n     for (int i = n - 1; i >= 0; --i) { n       vb[i] = cal(b[i]); n       upd(b[i]); n     } n     reverse(va, va + n); n     reverse(vb, vb + n); n     memset(vc, 0, sizeof(vc)); n     int carry = 0; n     for (int i = 1; i < n; ++i) { n       vc[i] = (carry + va[i] + vb[i]) % (i + 1); n       carry = (carry + va[i] + vb[i]) / (i + 1); n     } n     reverse(vc, vc + n); n     memset(bit, 0, sizeof(bit)); n     for (int i = 1; i <= n; ++i) { n       upd(i); n     } n     for (int i = 0; i < n; ++i) { n       c[i] = solve(vc[i] + 1); n       dec(c[i]); n     } n     for (int i = 0; i < n; ++i) { n       if (i) printf(   ); n       printf( %d , c[i] - 1); n     } n     puts(  ); n     return 0; n   } n } n


#pragma GCC optimize( O2 ) n  n #include<bits/stdc++.h> n #define ll long long n //#define int long long n #define fi first n #define se second n #define mpa make_pair n #define eb emplace_back n #define all(x) (x).begin(), (x).end() n #define rall(x) (x).rbegin(), (x).rend() n using namespace std; n mt19937 gen(time(0)); n  n signed main() { n     ios_base::sync_with_stdio(false); n     cin.tie(0); n     cout.tie(0); n  n     int n, q; n     cin >> n >> q; n     vector<int> a(n); n     vector<int> c(n + 1); n     int k = 800; n     int K = (n + k - 1) / k * 2; n     for (int i = 0; i < n; ++i) { n         cin >> a[i]; n         ++c[a[i]]; n     } n     vector<int> d; n     for (int i = 0; i <= n; ++i) if (c[i] >= K) d.eb(i); n     vector<vector<int>> psum(d.size(), vector<int>(n + 1, 0)); n     for (int i = 0; i < (int)(d.size()); ++i) { n         for (int j = 1; j <= n; ++j) { n             psum[i][j] = psum[i][j - 1] + (a[j - 1] == d[i]); n         } n     } n     vector<int> temp(n + 1); n     for (int z = 0; z < q; ++z) { n         int l, r; n         cin >> l >> r; n         int mx = 0; n         if (r - l + 1 < k) { n             for (int j = l - 1; j < r; ++j) { n                 ++temp[a[j]]; n                 mx = max(mx, temp[a[j]]); n             } n             for (int j = l - 1; j < r; ++j) --temp[a[j]]; n         } else { n             for (int j = 0; j < (int)(d.size()); ++j) { n                 int cnt = psum[j][r] - psum[j][l - 1]; n                 mx = max(mx, cnt); n             } n         } n         int len = (r - l + 1); n         int L = 0, R = mx; n         while (L != R) { n             int M = (L + R) / 2; n             if ((len - M + 1) / 2 >= mx - M) { n                 R = M; n             } else { n                 L = M + 1; n             } n         } n         cout << R + 1 <<   n ; n     } n } n /* n  n */ n