#include <bits/stdc++.h> n using namespace std; n const long long int p = 1e9 + 7; n const int N = 1e5 + 1; n long long int fact[N]; n long long int ncr[N]; n long long int n_1cr[N]; n void factorial() { n   fact[0] = fact[1] = 1; n   for (int i = 2; i < N; i++) { n     fact[i] = i * fact[i - 1]; n     fact[i] %= p; n   } n } n long long int fastPower(long long int a, long long int b) { n   if (b == 0) return 1; n   long long int tmp = fastPower(a, b / 2); n   if (b & 1) return (tmp % p * tmp % p * a % p) % p; n   return (tmp % p * tmp % p) % p; n } n long long int nCr(int n) { n   ncr[0] = 1; n   n_1cr[0] = 1; n   for (int i = 1; i <= n; i++) { n     long long int iPower = fastPower(i, p - 2); n     ncr[i] = (ncr[i - 1] * (n - i + 1)) % p; n     ncr[i] *= iPower; n     ncr[i] %= p; n     n_1cr[i] = (n_1cr[i - 1] % p * (n - i) % p) % p; n     n_1cr[i] *= iPower; n     n_1cr[i] %= p; n   } n } n long long int nCr(int n, int r) { n   long long ans = 1; n   ans = (fact[n - r] * fact[r]) % p; n   ans = (fact[n] * fastPower(ans, p - 2)) % p; n   return ans; n } n int main() { n   int n; n   long long int sum = 0; n   cin >> n; n   factorial(); n   for (int i = 1; i <= n; i++) { n     sum += nCr(n - 1, i - 1) * nCr(n, i); n     sum %= p; n   } n   sum *= 2; n   sum %= p; n   sum -= n; n   cout << sum << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, M; n vector<set<int> > A; n vector<set<pair<int, int> > > B; n vector<int> d, f, F, G; n vector<bool> v; n inline void upd(int& a, int b) { n   if (d[a] > d[b]) a = b; n } n void ff(int x, int y) { n   f[x] = x, v[x] = 1; n   for (set<int>::iterator i = A[x].begin(); i != A[x].end(); i++) n     if (v[*i]) { n       if (*i != y) upd(f[x], *i); n     } else n       d[*i] = d[x] + 1, ff(*i, x), upd(f[x], f[*i]); n } n void ff(int x) { n   f[x] = f[f[x]], v[x] = 0; n   for (set<int>::iterator i = A[x].begin(); i != A[x].end(); i++) n     if (v[*i]) ff(*i); n } n bool gg(int x, int y, int fa) { n   if (x == y) return 1; n   for (set<pair<int, int> >::iterator i = B[x].begin(); i != B[x].end(); i++) n     if (i->first != fa) n       if (gg(i->first, y, x)) { n         v[i->second] = v[i->second ^ 1] = 1; n         return 1; n       } n   return 0; n } n int main() { n   scanf( %d%d , &n, &m); n   A.resize(n); n   for (int i = 0, x, y; i < m; i++) n     scanf( %d%d , &x, &y), --x, --y, A[x].insert(y), A[y].insert(x); n   if (n == 2) { n     puts( -1 ); n     return 0; n   } n   d.resize(n, 0), f.resize(n), v.resize(n, 0); n   ff(0, -1), ff(0); n   F.resize(n); n   for (int i = 0; i < n; i++) n     if (f[i] == i) F[i] = (int)G.size(), G.push_back(i); n   for (int i = 0; i < n; i++) F[i] = F[f[i]]; n   B.resize((int)G.size()); n   for (int i = 0; i < n; i++) n     for (set<int>::iterator j = A[i].begin(); j != A[i].end(); j++) n       if (F[i] != F[*j]) { n         set<pair<int, int> >::iterator p = n             B[F[i]].lower_bound(make_pair(F[*j], -1)); n         if (p != B[F[i]].end() && p->first == F[*j]) continue; n         B[F[i]].insert(make_pair(F[*j], M++)), n             B[F[*j]].insert(make_pair(F[i], M++)); n       } n   if ((int)G.size() == 1) { n     puts( 0 ); n     return 0; n   } n   if ((int)G.size() == 2) { n     puts( 1 ); n     for (int i = 0; i < n; i++) n       for (int j = i + 1; j < n; j++) n         if (F[i] != F[j] && A[i].find(j) == A[i].end()) { n           printf( %d %d n , i + 1, j + 1); n           return 0; n         } n   } n   vector<int> L; n   for (int i = 0; i < (int)G.size(); i++) n     if ((int)B[i].size() == 1) L.push_back(i); n   printf( %d n , ((int)L.size() + 1) / 2); n   while (1) { n     random_shuffle(L.begin(), L.end()); n     bool F = 0, FF = 1; n     if ((int)L.size() & 1) F = 1, L.push_back(L[0]); n     v.clear(), v.resize(M, 0); n     for (int i = 0; i < (int)L.size(); i += 2) gg(L[i], L[i + 1], -1); n     for (int i = 0; i < M; i++) n       if (!v[i]) { n         FF = 0; n         break; n       } n     if (FF) { n       for (int i = 0; i < (int)L.size(); i += 2) n         printf( %d %d n , G[L[i]] + 1, G[L[i + 1]] + 1); n       return 0; n     } n     if (F) L.erase(L.end() - 1); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long int T = 1; n   while (T--) { n     long long int n, i, j; n     cin >> n; n     long long int a[n + 5]; n     long long int b[n + 5]; n     unordered_map<long long int, long long int> rnk; n     long long int ans = 0; n     for (i = 0; i < n; i++) { n       cin >> a[i]; n     } n     j = 0; n     for (i = 0; i < n; i++) { n       cin >> b[i]; n       if (b[i] != a[j]) { n         ans++; n         rnk[b[i]]++; n       } else { n         j++; n         while (j < n && rnk.find(a[j]) != rnk.end()) j++; n       } n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n const long long MAXN = 2e5 + 7; n long long a[MAXN]; n bool good[MAXN]; n long long want[MAXN], pref[MAXN], next_[MAXN]; n signed main() { n   ios_base::sync_with_stdio(false); n   cout.precision(40); n   long long n; n   cin >> n; n   for (long long i = 0; i < n; ++i) { n     cin >> a[i]; n   } n   stack<long long> st; n   for (long long i = 0; i < n; ++i) { n     while (st.size() && a[st.top()] < a[i]) { n       next_[st.top()] = i; n       st.pop(); n     } n     st.push(i); n   } n   while (st.size()) { n     next_[st.top()] = n; n     st.pop(); n   } n   long long mx1 = -MOD; n   long long mx2 = -MOD; n   for (long long i = 0; i < n; ++i) { n     if (a[i] < mx1 && a[i] > mx2) { n       want[i] = true; n     } n     if (a[i] > mx1) { n       mx2 = mx1; n       mx1 = a[i]; n     } else if (a[i] > mx2) { n       mx2 = a[i]; n     } n   } n   pref[0] = want[0]; n   for (long long i = 1; i < n; ++i) { n     pref[i] = pref[i - 1] + want[i]; n   } n   long long mx = -MOD; n   long long ans = -MOD; n   long long best = -1; n   for (long long i = 0; i < n; ++i) { n     mx = max(mx, a[i]); n     if (a[i] != mx) continue; n     long long j = next_[i]; n     long long new_ans = pref[j - 1] - pref[i] - 1; n     if (new_ans > ans || (new_ans == ans && a[i] < a[best])) { n       ans = new_ans; n       best = i; n     } n   } n   mx = -MOD; n   for (long long i = 0; i < n; ++i) { n     mx = max(mx, a[i]); n     if (a[i] == mx) continue; n     long long new_ans = 0; n     if (new_ans > ans || (new_ans == ans && a[i] < a[best])) { n       ans = new_ans; n       best = i; n     } n   } n   cout << a[best] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long t; n   cin >> t; n   while (t--) { n     long long n, m; n     cin >> n >> m; n     long long sum = 0; n     for (long long i = 0; i < n; i++) { n       long long x; n       cin >> x; n       sum += x; n     } n     cout << min(sum, m) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int x, y; n int p[1000003]; n int r[1000003]; n map<int, int> a; n int ans[200003]; n int s; n int parent(int x) { n   if (p[x] < 0) return x; n   return p[x] = parent(p[x]); n } n void join(int x, int y) { p[y] = parent(x); } n int main() { n   fill_n(p, 1000003, -1); n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) { n     scanf( %d%d , &x, &y); n     if (y) join(x, y); n     a.insert(pair<int, int>(x, y)); n   } n   int i = 2; n   int j = 0; n   while (a[j] != 0) { n     ans[i] = a[j]; n     j = a[j]; n     i += 2; n   } n   i = 1; n   j = 0; n   while (p[j] < 0 || parent(j) == 0) j++; n   j = parent(j); n   ans[i] = j; n   i += 2; n   while (a[j] != 0) { n     ans[i] = a[j]; n     j = a[j]; n     i += 2; n   } n   for (i = 1; i <= n; ++i) printf( %d  , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long C(int a, int b) { n   long long ans = 1; n   for (int i = 0; i < b; ++i) ans *= (a - i), ans /= (i + 1); n   return ans; n } n int main() { n   int n; n   while (cin >> n) cout << C(n + 4, 5) * C(n + 2, 3) << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 1e5 + 10; n struct Seg { n   long long mn, id; n }; n long long arr[maxn], N, P, ans[maxn]; n Seg seg[maxn << 2]; n inline Seg pushup(long long cur) { n   Seg l = seg[cur << 1], r = seg[cur << 1 | 1]; n   if (l.mn == r.mn) { n     return l; n   } else if (l.mn < r.mn) { n     Seg mer; n     mer.id = l.id, mer.mn = l.mn; n     return mer; n   } else { n     Seg mer; n     mer.id = r.id, mer.mn = r.mn; n     return mer; n   } n } n long long query(long long cur, long long L, long long R, long long l, n                 long long r) { n   if (L > R) return -1; n   if (l >= L && r <= R) { n     return seg[cur].id; n   } n   long long mid = (l + r) >> 1; n   long long lid = -1, rid = -1; n   if (mid >= L) lid = query(cur << 1, L, R, l, mid); n   if (mid < R) rid = query(cur << 1 | 1, L, R, mid + 1, r); n   if (lid == -1) n     return rid; n   else if (rid == -1) n     return lid; n   else { n     return arr[lid] > arr[rid] ? rid : lid; n   } n } n long long query2(long long cur, long long l, long long r, const long long x) { n   if (l == r) { n     return l; n   } n   long long mid = (l + r) >> 1; n   if (seg[cur << 1].mn <= x) { n     return query2(cur << 1, l, mid, x); n   } n   return query2(cur << 1 | 1, mid + 1, r, x); n } n void build(long long l, long long r, long long cur) { n   seg[cur].id = l, seg[cur].mn = arr[l]; n   if (l == r) return; n   long long mid = (l + r) >> 1; n   build(l, mid, cur << 1); n   build(mid + 1, r, cur << 1 | 1); n   seg[cur] = pushup(cur); n } n void update(long long pos, long long l, long long r, long long cur, n             long long x) { n   if (l == r) { n     seg[cur].mn = x; n     arr[pos] = x; n     return; n   } n   long long mid = (l + r) >> 1; n   if (mid >= pos) { n     update(pos, l, mid, cur << 1, x); n   } else { n     update(pos, mid + 1, r, cur << 1 | 1, x); n   } n   seg[cur] = pushup(cur); n } n signed main() { n   ios::sync_with_stdio(false); n   cin >> N >> P; n   for (register long long i = 1; i <= N; ++i) { n     cin >> arr[i]; n   } n   build(1, N, 1); n   long long cnt = 0, cur = 0; n   while (cnt < N) { n     cur = max(cur, seg[1].mn); n     long long x = query2(1, 1, N, cur); n     ans[x] = cur += P; n     ++cnt; n     update(x, 1, N, 1, 0x3f3f3f3f3f3f3f3f); n     while (cnt < N) { n       x = query(1, 1, x - 1, 1, N); n       if (x == -1 || arr[x] > cur) break; n       ans[x] = cur += P, ++cnt; n       update(x, 1, N, 1, 0x3f3f3f3f3f3f3f3f); n     } n   } n   for (register long long i = 1; i <= N; ++i) { n     cout << ans[i] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int count = 0; n   while ((count * (count + 1)) / 2 <= n) { n     ++count; n   } n   --count; n   int val = (count * (count + 1)) / 2; n   int diff = n - val; n   cout << count <<   n ; n   for (int i = 1; i <= count; ++i) { n     if (i == count) { n       cout << count + diff <<    ; n       break; n     } n     cout << i <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int t; n int n; n int a[100086]; n int main() { n   scanf( %d , &t); n   while (t--) { n     scanf( %d , &n); n     for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n     int x = 0, y = a[1]; n     for (int i = 2; i <= n; i++) { n       if (a[i] > a[i - 1]) { n         x += a[i] - a[i - 1]; n       } else { n         y -= a[i - 1] - a[i]; n       } n     } n     puts(y >= 0 ?  YES  :  NO ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int k, b, n; n map<int, int>::iterator it; n int main() { n   map<int, int> mp; n   while (scanf( %d%d%d , &k, &b, &n) != EOF) { n     mp.clear(); n     if (b == 0) { n       int a, sum = 0; n       long long res = 0; n       mp[0] = 1; n       for (int i = 1; i <= n; i++) { n         scanf( %d , &a); n         if (a) sum++; n         if (mp.find(sum) == mp.end()) n           mp[sum] = 1; n         else n           mp[sum]++; n       } n       for (it = mp.begin(); it != mp.end(); it++) { n         long long tmp = it->second; n         res += tmp * (tmp - 1) / 2; n       } n       cout << res << endl; n     } else { n       int a, ss = 0; n       long long res = 0, pre = 0; n       mp[0] = 1; n       for (int i = 1; i <= n; i++) { n         scanf( %d , &a); n         if (a) { n           ss += a; n           ss %= (k - 1); n           int ff = (ss - b + k - 1) % (k - 1); n           int st = mp.find(ff) == mp.end() ? 0 : mp[ff]; n           pre = st; n           res += st; n           if (mp.find(ss) == mp.end()) n             mp[ss] = 1; n           else n             mp[ss]++; n         } else { n           res += pre; n           mp[ss]++; n         } n       } n       cout << res << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 5; n int a[N], cnt[N * 2], n, m; n long long solve(int m) { n   int s = n; n   memset(cnt, 0, sizeof(cnt)); n   cnt[s] = 1; n   long long add = 0, ans = 0; n   for (int i = 1; i <= n; i++) { n     if (a[i] >= m) n       add += cnt[s], s++; n     else n       s--, add -= cnt[s]; n     cnt[s]++; n     ans += add; n   } n   return ans; n } n int main() { n   scanf( %d %d , &n, &m); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   printf( %lld n , solve(m) - solve(m + 1)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long t1, t2, x1, x2, t; n bool Check(long long y1, long long y2) { n   return t1 * y1 + t2 * y2 >= t * (y1 + y2); n } n double HotBath(long long y1, long long y2) { n   return 1.0 * (t1 * y1 + t2 * y2) / (y1 + y2); n } n int main() { n   cin >> t1 >> t2 >> x1 >> x2 >> t; n   long long Y1 = 0, Y2 = 0; n   if (HotBath(x1, x2) >= t) Y1 = x1, Y2 = x2; n   if (HotBath(x1, 0) >= t && x1 >= Y1 + Y2) Y1 = x1, Y2 = 0; n   if (HotBath(0, x2) >= t && x2 >= Y1 + Y2) Y1 = 0, Y2 = x2; n   for (long long y1 = 0; y1 <= x1; y1++) { n     long long y2 = 0; n     long long Start = 0, End = x2; n     while (Start <= End) { n       long long Mid = Start + End >> 1; n       if (Check(y1, Mid)) { n         y2 = Mid; n         End = Mid - 1; n       } else n         Start = Mid + 1; n     } n     if (y1 + y2 == 0) continue; n     double New = HotBath(y1, y2); n     double Old = HotBath(Y1, Y2); n     if (New >= 1.0 * t) { n       if (New < Old) { n         Old = New; n         Y1 = y1; n         Y2 = y2; n       } else if (fabs(New - Old) <= 1e-9 && (Y1 + Y2 < y1 + y2)) { n         Y1 = y1; n         Y2 = y2; n       } n     } n   } n   cout << Y1 <<     << Y2 <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxN = 2000 + 5; n int n, mark[maxN], m, st[maxN], start, high[maxN], on[maxN], cnt; n vector<int> adj[maxN]; n vector<int> s; n void DFS(int v) { n   s.push_back(v); n   on[v] = mark[v] = 1; n   st[v] = high[v] = start++; n   for (int i = 0; i < adj[v].size(); i++) { n     int x = adj[v][i]; n     if (mark[x]) { n       if (on[x]) high[v] = min(high[v], high[x]); n     } else { n       DFS(x); n       high[v] = min(high[v], high[x]); n     } n   } n   if (st[v] == high[v]) { n     int x = s.back(); n     while (!s.empty() && st[x] >= st[v]) { n       on[x] = 0; n       s.pop_back(); n       x = s.back(); n     } n     cnt++; n   } n } n int main() { n   ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   cin >> n; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       int x; n       cin >> x; n       if (x) adj[i].push_back(j); n     } n   } n   for (int i = 0; i < n; i++) n     if (!mark[i]) DFS(i); n   if (cnt == 1) n     cout <<  YES n ; n   else n     cout <<  NO n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int w, h, ring, plate = 0; n   cin >> w >> h >> ring; n   for (int i = 0; i < ring; i++) { n     if (i > 0) { n       w -= 4; n       h -= 4; n     } n     plate += (w * 2) + ((h - 2) * 2); n   } n   cout << plate; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, q, i, num, x, y; n   cin >> n >> q; n   for (i = 0; i < q; i++) { n     cin >> x >> y; n     if ((x + y) % 2 == 0) { n       x--; n       num = (n / 2 + n % 2) * (x / 2 + x % 2) + (n / 2) * (x / 2) + n             (y / 2 + y % 2); n     } else { n       x--; n       num = (n / 2 + n % 2) * (x / 2) + (n / 2) * (x / 2 + x % 2) + n             (y / 2 + y % 2) + (n * n) / 2 + (n * n) % 2; n     } n     cout << num << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   long long int n; n   std::cin >> n; n   if (n % 2 == 0) { n     std::cout << n / 2 << std::endl; n   } else { n     std::cout << -(n + 1) / 2 << std::endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n const long long INF = 1e18; n const string nl =   n ; n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(nullptr); n   long long n, k, second, t; n   cin >> n >> k >> second >> t; n   vector<pair<long long, long long>> a(n); n   for (auto& i : a) { n     cin >> i.first >> i.second; n   } n   vector<long long> b(k); n   for (auto& i : b) { n     cin >> i; n   } n   sort(b.begin(), b.end()); n   vector<long long> temp = {0}; n   for (auto& i : b) { n     temp.push_back(i); n   } n   temp.push_back(second); n   b = temp; n   auto check = [&](long long x) -> bool { n     long long ans = 0; n     for (long long i = 0; i < ((long long)(b).size()); ++i) { n       long long dist = b[i] - (i == 0 ? 0 : b[i - 1]); n       long long hld = min(x - dist, dist); n       if (hld < 0) { n         return 0; n       } n       ans += hld + (dist - hld) * 2; n     } n     return ans <= t; n   }; n   long long l = 0, r = 2e9; n   while (l < r) { n     long long mid = (l + r) / 2; n     if (check(mid)) { n       r = mid; n     } else { n       l = mid + 1; n     } n   } n   auto cmp = [&](pair<long long, long long> a, n                  pair<long long, long long> b) -> bool { n     return a.second < b.second; n   }; n   sort(a.begin(), a.end(), cmp); n   long long ans = INF; n   for (long long i = 0; i < ((long long)(a).size()); ++i) { n     if (a[i].second >= l) { n       ans = min(ans, a[i].first); n     } n   } n   cout << (ans == INF ? -1 : ans) << nl; n } n


#include <bits/stdc++.h> n using namespace std; n const int oo = (int)1e9; n const double PI = 2 * acos(0.0); n const double eps = 1e-9; n int main() { n   ios_base::sync_with_stdio(false); n   int n, arra[100010], arrb[100010]; n   cin >> n; n   for (int i = 0; i < (int)(n); i++) { n     cin >> arra[i]; n     arrb[i] = arra[i]; n   } n   sort(arrb, arrb + n); n   int cnt = 0; n   for (int i = 0; i < (int)(n); i++) n     if (arra[i] != arrb[i]) cnt++; n   if (cnt == 2 || cnt == 0) n     cout <<  YES  << endl; n   else n     cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int x = 0; n   char ch = getchar(); n   bool positive = 1; n   for (; ch <  0  || ch >  9 ; ch = getchar()) n     if (ch ==  - ) positive = 0; n   for (; ch >=  0  && ch <=  9 ; ch = getchar()) x = x * 10 + ch -  0 ; n   return positive ? x : -x; n } n int N, K; n int a[105], b[105], s[105]; n int t[105]; n inline void trans_one(int p[]) { n   for (int i = 1; i <= N; ++i) t[i] = p[a[i]]; n   for (int i = 1; i <= N; ++i) p[i] = t[i]; n } n inline void trans_two(int p[]) { n   for (int i = 1; i <= N; ++i) t[a[i]] = p[i]; n   for (int i = 1; i <= N; ++i) p[i] = t[i]; n } n inline bool equal(int a[], int b[]) { n   for (int i = 1; i <= N; ++i) n     if (a[i] != b[i]) return 0; n   return 1; n } n int main() { n   N = read(), K = read(); n   for (int i = 1; i <= N; ++i) a[i] = read(); n   for (int i = 1; i <= N; ++i) s[i] = read(); n   for (int i = 1; i <= N; ++i) b[i] = i; n   if (equal(b, s)) return printf( NO n ), 0; n   int l = 1 << 30, r = 1 << 30; n   for (int i = 1; i <= K + 1; ++i) { n     trans_one(b); n     if (equal(b, s)) { n       r = i; n       break; n     } n   } n   for (int i = 1; i <= N; ++i) b[i] = i; n   for (int i = 1; i <= K + 1; ++i) { n     trans_two(b); n     if (equal(b, s)) { n       l = i; n       break; n     } n   } n   if (l % 2 == K % 2 && K >= l) { n     if (l > 1 || r > 1 || K - l == 0) return printf( YES n ), 0; n   } n   if (r % 2 == K % 2 && K >= r) { n     if (r > 1 || l > 1 || K - r == 0) return printf( YES n ), 0; n   } n   printf( NO n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long MOD = 1e9 + 7, MAX = 1e18; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   ; n   long long t; n   cin >> t; n   while (t--) { n     long long n0, n1, n2, i; n     cin >> n0 >> n1 >> n2; n     string second =   ; n     if (n1) { n       if (n1 % 2) { n         for (i = 0; i <= n1; i++) { n           if (i % 2) n             second.push_back( 0 ); n           else n             second.push_back( 1 ); n         } n         while (n2--) cout << 1; n         cout << second; n         while (n0--) cout << 0; n         cout <<   n ; n       } else { n         for (i = 1; i <= n1; i++) { n           if (i % 2) n             second.push_back( 0 ); n           else n             second.push_back( 1 ); n         } n         while (n2--) cout << 1; n         cout << 1; n         while (n0--) cout << 0; n         cout << second <<   n ; n       } n     } else { n       if (n2) n         for (i = 0; i <= n2; i++) cout << 1; n       if (n0) n         for (i = 0; i <= n0; i++) cout << 0; n       cout <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int l[123], r[123]; n int d[123]; n int main() { n   ios_base::sync_with_stdio(false); n   int n; n   cin >> n; n   l[0] = 0; n   for (int i = 1; i < n; ++i) { n     int x; n     cin >> x; n     l[i] = l[i - 1] + x; n   } n   r[n - 1] = 0; n   vector<int> a; n   for (int i = 0; i < (int)n - 1; ++i) { n     int x; n     cin >> x; n     a.push_back(x); n   } n   int k = n - 2; n   for (int i = n - 2; i >= 0; --i) { n     r[i] = r[i + 1] + a[k]; n     k--; n   } n   for (int i = 0; i < (int)n; ++i) cin >> d[i]; n   int res[123]; n   for (int i = 0; i < (int)n; ++i) res[i] = d[i] + l[i] + r[i]; n   sort(res, res + n); n   cout << res[0] + res[1]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long max3(long long a, long long b, long long c) { n   return (a) > (b) ? ((a) > (c) ? (a) : (c)) : ((b) > (c) ? (b) : (c)); n } n inline long long min3(long long a, long long b, long long c) { n   return (a) < (b) ? ((a) < (c) ? (a) : (c)) : ((b) < (c) ? (b) : (c)); n } n long long extgcd(long long a, long long b, long long &x, long long &y) { n   if (b == 0) { n     x = 1; n     y = 0; n     return a; n   } n   long long d = extgcd(b, a % b, y, x); n   y -= a / b * x; n   return d; n } n inline long long mod(long long a, long long m) { return (a % m + m) % m; } n long long modinv(long long a) { n   long long x, y; n   extgcd(a, 1000000007, x, y); n   return mod(x, 1000000007); n } n long long gcd(long long u, long long v) { n   long long r; n   while (0 != v) { n     r = u % v; n     u = v; n     v = r; n   } n   return u; n } n long long lcm(long long u, long long v) { return u / gcd(u, v) * v; } n void get_all_factors(int a[], long long int n) { n   long long int w = sqrt(n); n   for (int i = 1; i <= w; i++) { n     if (n % i == 0) { n       a[i]++; n       a[n / i]++; n     } n   } n   long long int x = ceil(sqrt(n)); n   if (sqrt(n) == ceil(sqrt(n))) a[(int)sqrt(n)]--; n } n long long int memo[100000][110] = {0}; n long long C(long long n, long long r) { n   if (r == 0 || r == n) return 1; n   if (memo[n][r] != -1) return memo[n][r]; n   return memo[n][r] = n              (C(n - 1, r - 1) % 1000000007 + C(n - 1, r) % 1000000007) % n              1000000007; n } n int valid(string s, int i) { n   if (s ==  lol  or i == -1000050000) { n     return 0; n   } n   return 1; n } n void status(string s1 =  lol , long long int i1 = -1000050000, n             string s2 =  lol , long long int i2 = -1000050000, n             string s3 =  lol , long long int i3 = -1000050000) { n   if (1) { n     if (valid(s1, i1)) { n       cout << s1 <<  =  << i1 <<     ; n     } n     if (valid(s2, i2)) { n       cout << s2 <<  =  << i2 <<     ; n     } n     if (valid(s3, i3)) { n       cout << s3 <<  =  << i3 <<     ; n     } n     cout << endl; n   } n } n long long int poww(long long b, long long e) { n   if (e == 0) n     return 1; n   else if (e % 2 == 0) { n     long long a = pow(b, e / 2); n     return a * a; n   } else { n     long long a = pow(b, e / 2); n     return b * a * a; n   } n } n long long int powm(long long x, long long y, long long m = 1000000007) { n   x = x % m; n   long long res = 1; n   while (y) { n     if (y & 1) res = res * x; n     res %= m; n     y = y >> 1; n     x = x * x; n     x %= m; n   } n   return res; n } n vector<pair<int, int> > range(1000000); n vector<int> adj[1000000]; n vector<int> d(1000000, 0); n int p[1000000] = {0}; n int T = 0; n void dfs(int v, int pa = -1, int dep = 0) { n   p[v] = pa; n   d[v] = dep; n   range[v].first = T; n   T++; n   for (auto i : adj[v]) { n     if (i != pa) { n       dfs(i, v, dep + 1); n     } n   } n   range[v].second = T; n   T++; n } n int main() { n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < n - 1; i++) { n     int u, v; n     cin >> u >> v; n     adj[u].push_back(v); n     adj[v].push_back(u); n   } n   dfs(1); n   while (m--) { n     int k; n     cin >> k; n     int a[k]; n     for (int i = 0; i < k; i++) { n       cin >> a[i]; n     } n     int it = a[0]; n     for (auto i : a) { n       if (d[i] > d[it]) { n         it = i; n       } n     } n     for (auto &i : a) { n       if (it == i) { n         continue; n       } n       if (p[i] != -1) { n         i = p[i]; n       } n     } n     int ok = 1; n     for (auto i : a) { n       if (range[i].first <= range[it].first and n           range[i].second >= range[it].second) { n       } else { n         ok = 0; n         break; n       } n     } n     if (ok) { n       cout <<  YES  << endl; n     } else { n       cout <<  NO  << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n bool mmax(T &m, const T q) { n   if (m < q) { n     m = q; n     return true; n   } else n     return false; n } n template <typename T> n bool mmin(T &m, const T q) { n   if (m > q) { n     m = q; n     return true; n   } else n     return false; n } n const int MOD = 1e9 + 7; n const int MAXN = 5e5 + 3; n const int INF = 1e9; n int main(int argc, char **argv) { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int ntest; n   cin >> ntest; n   while (ntest--) { n     long long x, y, a, b; n     cin >> x >> y >> a >> b; n     long long ans = a * (abs(x) + abs(y)); n     mmin(ans, b * abs(x) + a * abs(y - x)); n     mmin(ans, b * abs(y) + a * abs(x - y)); n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 5; n int lazy[maxn << 2]; n int a[maxn], c[maxn]; n void pushup(int l, int r, int rt) { n   lazy[rt << 1] = lazy[rt << 1 | 1] = lazy[rt]; n   lazy[rt] = 0; n } n void update(int L, int R, int c, int l, int r, int rt) { n   if (L <= l && R >= r) { n     lazy[rt] = c; n     return; n   } n   if (lazy[rt]) pushup(l, r, rt); n   if (L <= ((l + r) >> 1)) update(L, R, c, l, ((l + r) >> 1), rt << 1); n   if (R > ((l + r) >> 1)) update(L, R, c, ((l + r) >> 1) + 1, r, rt << 1 | 1); n } n void query(int l, int r, int rt) { n   if (l == r) { n     c[l] = lazy[rt]; n     return; n   } n   if (lazy[rt]) pushup(l, r, rt); n   query(l, ((l + r) >> 1), rt << 1); n   query(((l + r) >> 1) + 1, r, rt << 1 | 1); n } n struct T { n   int l, r; n } b[maxn]; n int main() { n   map<int, int> mp; n   int n, k; n   cin >> n >> k; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   for (int i = 1; i <= n; i++) { n     if (a[i] == 0) continue; n     if (mp[a[i]]) n       b[a[i]].r = i; n     else n       mp[a[i]] = 1, b[a[i]].l = i, b[a[i]].r = i; n   } n   for (int i = 1; i <= k; i++) { n     if (mp[i]) { n       update(b[i].l, b[i].r, i, 1, n, 1); n     } n   } n   query(1, n, 1); n   bool d = true; n   for (int i = 1; i <= n; i++) { n     if (c[i] != a[i] && a[i] != 0) d = false; n   } n   if (!d) { n     cout <<  NO  << endl; n     return 0; n   } n   bool ff = true; n   for (int i = 1; i <= n; i++) { n     if (a[i] == k) ff = false; n   } n   if (ff) { n     bool dd = false; n     for (int i = 1; i <= n; i++) n       if (a[i] == 0) { n         dd = true; n         c[i] = k; n         break; n       } n     if (!dd) { n       cout <<  NO  << endl; n       return 0; n     } n   } n   cout <<  YES  << endl; n   for (int i = 1; i <= n; i++) { n     if (a[i] == 0 && c[i] == 0) { n       c[i] = 1; n     } n     cout << c[i] <<    ; n   } n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t, n, x; n   scanf( %d , &n); n   priority_queue<int, vector<int>, greater<int> > que; n   long long ans = 0; n   for (int i = 0; i < n; i++) { n     scanf( %d , &x); n     if (que.empty() || que.top() > x) n       que.push(x); n     else { n       ans += x - que.top(); n       que.pop(); n       que.push(x); n       que.push(x); n     } n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rndll(chrono::steady_clock::now().time_since_epoch().count()); n const int INF = 0x3f3f3f3f, MOD = 1e9 + 7; n const int N = 2e5 + 5; n int n, q, a, b, cnt, k, par[N]; n vector<int> adj[N], v; n pair<int, int> rng[N]; n void dfs(int u, int p) { n   par[u] = (p != -1 ? p : u); n   rng[u] = {INF, -INF}; n   for (auto v : adj[u]) n     if (v != p) { n       dfs(v, u); n       rng[u].first = min(rng[u].first, rng[v].first); n       rng[u].second = max(rng[u].second, rng[v].second); n     } n   if (rng[u].first == INF) cnt++, rng[u] = {cnt, cnt}; n } n bool query(vector<int> &v) { n   int l = -INF, r = INF; n   for (auto x : v) n     l = max(l, rng[par[x]].first), r = min(r, rng[par[x]].second); n   return l <= r; n } n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0); n   cin >> n >> q; n   for (int i = 0; i < n - 1; i++) { n     cin >> a >> b; n     adj[a].push_back(b), adj[b].push_back(a); n   } n   dfs(1, -1); n   while (q--) { n     cin >> k; n     v.resize(k); n     for (int i = 0; i < k; i++) cin >> v[i]; n     cout << (query(v) ?  YES n  :  NO n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   vector<int> v; n   cin >> n; n   int x = 1; n   for (int i = 1; i <= n; i++) { n     int d; n     cin >> d; n     if (d == x) { n       v.push_back(i + 2000); n       x++; n     } n   } n   cout << v.size() << endl; n   for (int i = 0; i < v.size(); i++) cout << v[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 7; n const int inf = 0x3f3f3f3f; n const int mod = 1e9 + 7; n long long ans; n int a[maxn]; n int ned[maxn]; n int n; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     ned[i] = a[i] + 1; n   } n   for (int i = 2; i <= n; i++) { n     ned[i] = max(ned[i], ned[i - 1]); n   } n   for (int i = n; i >= 1; i--) { n     ned[i] = max(ned[i + 1] - 1, ned[i]); n     ans += (ned[i] - a[i] - 1); n   } n   printf( %I64d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> A; n long long B[1000000] = {0}; n bool cmp(long long x, long long y) { return x > y; } n int main() { n   A.clear(); n   string S1, S2; n   cin >> S1; n   for (long long i = 0; i < S1.size(); i++) A.push_back(S1[i] -  0 ); n   cin >> S2; n   for (long long i = 0; i < S2.size(); i++) B[i] = S2[i] -  0 ; n   sort(B, B + S2.size(), cmp); n   long long find = 0; n   for (long long i = 0; i < A.size(); i++) n     if (A[i] < B[find]) A[i] = B[find++]; n   for (long long i = 0; i < A.size(); i++) cout << A[i]; n } n


#include <bits/stdc++.h> n using namespace std; n struct Point { n   int num; n   int next[3001]; n   int count; n }; n Point a[3001]; n int dis[3001]; n int p[3001]; n int cir[3001]; n int cir1[3001]; n int f(int n) { n   int i, num = 0, d = 1; n loop: n   while (1) { n     num = 0; n     for (i = 1; i <= n; i++) { n       if (a[i].num - a[i].count == 1) { n         a[i].count++; n         p[num++] = i; n       } n     } n     for (i = 0; i < num; i++) { n       for (int j = 0; j < a[p[i]].num; j++) a[a[p[i]].next[j]].count++; n     } n     for (i = 1; i <= n; i++) { n       if (a[i].num - a[i].count > 0 && a[i].num - a[i].count != 2) goto loop; n     } n     num = 0; n     for (i = 1; i <= n; i++) { n       if (a[i].num - a[i].count == 2) cir[num++] = i; n     } n     return num; n   } n } n int main() { n   int b[4], n, i, t, sum, num, flag; n   int from, to; n   cin >> n; n   for (i = 1; i <= n; i++) { n     cin >> from >> to; n     a[from].next[a[from].num++] = to; n     a[to].next[a[to].num++] = from; n   } n   int step = 0; n   sum = num = f(n); n   memset(dis, 0xff, sizeof(dis)); n   for (i = 0; i < num; i++) dis[cir[i]] = step; n   while (sum < n) { n     int k = 0; n     step++; n     flag = 0; n     for (i = 0; i < num; i++) { n       for (int j = 0; j < a[cir[i]].num; j++) { n         int next = a[cir[i]].next[j]; n         if (dis[next] == -1) { n           sum++; n           flag = 1; n           dis[next] = step; n           cir1[k++] = next; n           continue; n         } n       } n       if (!flag) cir[i] = -1; n     } n     memcpy(cir, cir1, k * sizeof(cir1[0])); n     num = k; n   } n   for (i = 1; i <= n; i++) n     if (i != n) n       cout << dis[i] <<    ; n     else n       cout << dis[i] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline bool chkmin(T &x, T y) { n   return x > y ? x = y, 1 : 0; n } n template <typename T> n inline bool chkmax(T &x, T y) { n   return x < y ? x = y, 1 : 0; n } n template <typename T> n inline void read(T &x) { n   char ch = getchar(); n   int f = 1; n   x = 0; n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) x = x * 10 + ch -  0 , ch = getchar(); n   x *= f; n } n template <typename T, typename... Args> n inline void read(T &x, Args &...args) { n   char ch = getchar(); n   int f = 1; n   x = 0; n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) x = x * 10 + ch -  0 , ch = getchar(); n   x *= f; n   read(args...); n } n int n, m, C, emp, dlt, len[300050], crs[300050], ans[300050]; n set<int> se; n map<int, int> both[300050]; n struct Time { n   int t, x; n   bool operator<(const Time &rhs) const { return t < rhs.t; } n } a[300050 << 2]; n struct cloud { n   int l, r, c; n   bool operator<(const cloud &rhs) const { return c < rhs.c; } n } c[300050]; n struct Query { n   int x, id; n   bool operator<(const Query &rhs) const { return x < rhs.x; } n } q[300050]; n int maxv[300050 << 2]; n void modify(int u, int l, int r, int x) { n   if (l == r) return (void)(maxv[u] = len[x]); n   if (x <= ((l + r) >> 1)) n     modify(u << 1, l, ((l + r) >> 1), x); n   else n     modify(u << 1 | 1, ((l + r) >> 1) + 1, r, x); n   maxv[u] = max(maxv[u << 1], maxv[u << 1 | 1]); n } n int query(int u, int l, int r, int tl, int tr) { n   if (tl > tr) return 0; n   if (tl <= l && r <= tr) return maxv[u]; n   int ret = 0; n   if (tl <= ((l + r) >> 1)) ret = query(u << 1, l, ((l + r) >> 1), tl, tr); n   if (((l + r) >> 1) < tr) n     chkmax(ret, query(u << 1 | 1, ((l + r) >> 1) + 1, r, tl, tr)); n   return ret; n } n int main() { n   read(n, C); n   for (register int i = 1; i <= n; ++i) read(c[i].l, c[i].r, c[i].c); n   sort(c + 1, c + n + 1); n   int tot = 0; n   for (register int i = 1; i <= n; ++i) n     a[++tot] = Time{c[i].l, i}, a[++tot] = Time{c[i].r, -i}; n   a[++tot] = Time{int(2e9), 0}; n   sort(a + 1, a + tot + 1); n   read(m); n   for (register int i = 1; i <= m; ++i) read(q[i].x), q[i].id = i; n   sort(q + 1, q + m + 1); n   int pos = 1; n   for (register int i = 1; i <= tot; ++i) { n     int T = a[i].t - a[i - 1].t; n     if (se.empty()) n       emp += T; n     else if (se.size() == 1) { n       int x = *se.begin(), cur = -1e9; n       len[x] += T, crs[x] += T, modify(1, 1, n, x); n       int rr = upper_bound(c + 1, c + n + 1, cloud{0, 0, C - c[x].c}) - c - 1; n       if (rr >= x) n         chkmax(cur, query(1, 1, n, 1, x - 1)), n             chkmax(cur, query(1, 1, n, x + 1, rr)); n       else n         chkmax(cur, query(1, 1, n, 1, rr)); n       chkmax(crs[x], len[x] + cur); n       if (c[x].c <= C) chkmax(dlt, crs[x]); n     } else if (se.size() == 2) { n       int x = *se.begin(), y = *se.rbegin(); n       if (c[x].c + c[y].c <= C) { n         both[x][y] += T; n         chkmax(crs[x], both[x][y] + len[x] + len[y]), n             chkmax(crs[y], both[x][y] + len[x] + len[y]); n         chkmax(dlt, max(crs[x], crs[y])); n       } n     } n     while (pos <= m && dlt + emp >= q[pos].x) n       ans[q[pos].id] = a[i].t - (dlt + emp - q[pos].x), pos++; n     if (a[i].x > 0) n       se.insert(a[i].x); n     else n       se.erase(-a[i].x); n   } n   for (register int i = 1; i <= m; ++i) printf( %d n , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int _ = 600 + 7; n const int __ = 1e5 + 7; n const int S = 601, T = 602; n const int inf = 0x3f3f3f3f; n int n; n int lst[_], nxt[__], to[__], cap[__], tot = 1; n long long ans; n int gi() { n   int x = 0; n   bool f = 0; n   char c = getchar(); n   while (!isdigit(c) and c !=  - ) c = getchar(); n   if (c ==  - ) f = 1, c = getchar(); n   while (isdigit(c)) x = (x << 3) + (x << 1) + c -  0 , c = getchar(); n   return f ? -x : x; n } n void Add(int x, int y, int c) { n   nxt[++tot] = lst[x]; n   to[tot] = y; n   cap[tot] = c; n   lst[x] = tot; n   nxt[++tot] = lst[y]; n   to[tot] = x; n   cap[tot] = 0; n   lst[y] = tot; n } n void Init() { n   n = gi(); n   for (int i = 1; i <= n; ++i) { n     int m = gi(); n     for (int j = 1; j <= m; ++j) Add(i, gi() + n, inf); n   } n   for (int i = 1; i <= n; ++i) { n     int v = gi(); n     ans -= v; n     Add(S, i, inf - v), Add(i + n, T, inf); n   } n } n int d[_], cur[_]; n queue<int> q; n bool Bfs() { n   memset(d, -1, sizeof d); n   while (!q.empty()) q.pop(); n   d[S] = 0, q.push(S); n   while (!q.empty()) { n     int u = q.front(); n     q.pop(); n     for (int i = lst[u]; i; i = nxt[i]) n       if (cap[i] and d[to[i]] == -1) { n         d[to[i]] = d[u] + 1, q.push(to[i]); n         if (to[i] == T) return 1; n       } n   } n   return 0; n } n int Dfs(int u, int flow) { n   if (u == T) return flow; n   int res = flow; n   for (int &i = cur[u]; i; i = nxt[i]) n     if (cap[i] and d[to[i]] == d[u] + 1) { n       int tmp = Dfs(to[i], min(res, cap[i])); n       cap[i] -= tmp, cap[i ^ 1] += tmp; n       res -= tmp; n       if (!res) return flow; n     } n   return flow - res; n } n void Run() { n   long long maxFlow = -1ll * n * inf; n   int flow; n   while (Bfs()) { n     memcpy(cur, lst, sizeof cur); n     do { n       flow = Dfs(S, inf); n       maxFlow += flow; n     } while (flow); n   } n   cout << min(0ll, -(ans - maxFlow)) << endl; n } n int main() { n   Init(); n   Run(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k, a[100010]; n   cin >> n >> k; n   if (n / 2 > k) n     printf( -1 n ); n   else { n     if (n == 1) { n       if (k == 0) n         printf( 1 n ); n       else n         printf( -1 n ); n     } else { n       int t = k - (n / 2 - 1); n       printf( %d %d  , t, 2 * t); n       int cnt = 1; n       for (int i = 1; i < n / 2; i++) { n         printf(  %d %d  , 900000000 + cnt, 900000000 + cnt + 1); n         cnt += 2; n       } n       if (n % 2) printf( 1000000000 ); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int t; n   cin >> t; n   while (t--) { n     long long int a, b, n, s; n     cin >> a >> b >> n >> s; n     long long int r = s % n; n     long long int q = s / n; n     if (q <= a) { n       if (r <= b) { n         cout <<  YES  <<   n ; n       } else n         cout <<  NO  <<   n ; n     } else if (((q - a) * n + r) <= b) { n       cout <<  YES  <<   n ; n     } else { n       cout <<  NO  <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n string itos(int x) { n   stringstream ss; n   ss << x; n   return ss.str(); n } n int xorsum, n, a[110]; n map<int, int> grun; n int sieve[31700]; n vector<int> p; n int mex(set<int> &s) { n   for (int i = 0;; i++) n     if (not s.count(i)) return i; n } n int getGrundy(int x) { n   if (grun.count(x)) return grun[x]; n   if (x == 1) return grun[x] = 0; n   set<int> s; n   for (int i = 1; (1 << i) <= x; i++) n     s.insert(getGrundy(x >> i | (x & ((1 << i) - 1)))); n   return grun[x] = mex(s); n } n void solve(int x) { n   int mask = 0; n   for (int i = 0; i < n; i++) { n     int cnt = 0; n     while (a[i] % x == 0) cnt++, a[i] /= x; n     mask |= (1 << cnt); n   } n   xorsum ^= getGrundy(mask); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin >> n; n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 2; i < 31700; i++) { n     if (not sieve[i]) { n       p.push_back((i)); n       for (int j = i * i; j < 31700; j += i) sieve[j] = 1; n     } n   } n   for (int i = 0; i < n; i++) { n     auto it1 = p.begin(); n     while (a[i] > 1) { n       if (it1 == p.end()) { n         solve(a[i]); n         break; n       } else if (a[i] % *it1 == 0) n         solve(*it1); n       it1++; n     } n   } n   cout << (xorsum ?  Mojtaba  :  Arpa ) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int Maxn3 = 1e3 + 10; n const long long int Maxn4 = 1e4 + 10; n const long long int Maxn5 = 1e5 + 10; n const long long int Maxn6 = 1e6 + 10; n const long long int Maxn7 = 1e7 + 10; n const long long int Maxn8 = 1e8 + 10; n const long long int Maxn9 = 1e9 + 10; n const long long int Maxn18 = 1e18 + 10; n const long long int Mod1 = 1e7 + 7; n const long long int Mod2 = 1e9 + 7; n const long long int LLMax = LLONG_MAX; n const long long int LLMin = LLONG_MIN; n const long long int INTMax = INT_MAX; n const long long int INTMin = INT_MIN; n long long int mn = LLMax, mx = LLMin; n char s[Maxn6]; n int main() { n   ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); n   string s1( ... n.X. n ), s2( .X. n ), s; n   long long int res = 0; n   cin >> s; n   for (long long int i = 0; i < s.size(); i++) { n     if (res < s[i]) res += 256; n     while (res > s[i]) { n       res--; n       cout << s1; n     } n     cout << s2; n   } n } n


#include <bits/stdc++.h> n std::mt19937 rng( n     (int)std::chrono::steady_clock::now().time_since_epoch().count()); n int main() { n   std::ios_base::sync_with_stdio(false); n   std::cin.tie(NULL); n   int n, w, m; n   std::cin >> n >> w >> m; n   std::cout << std::setprecision(10) << std::fixed; n   std::vector<std::vector<std::pair<int, int>>> ans(m); n   w *= m; n   int cup = n * w / m; n   int rest = w; n   for (int i = 0, j = 0; i < m; i++) { n     if (rest > 0 && rest != w && rest > cup) { n       std::cout <<  NO n ; n       return 0; n     } n     int want = cup; n     while (want > 0) { n       int use = std::min(rest, want); n       assert(use != 0); n       ans[i].emplace_back(use, j + 1); n       want -= use; n       rest -= use; n       if (rest == 0) { n         j++; n         rest = w; n       } n     } n   } n   std::cout <<  YES n ; n   for (auto vec : ans) { n     for (int i = 0; i < (int)vec.size(); i++) { n       std::cout << vec[i].second <<     << (double)vec[i].first / m n                 << (i + 1 == (int)vec.size() ?   n  :    ); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n constexpr int INF = 1e9 + 1; n constexpr long long LLINF = 1e18 + 1; n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } n void solve() { n   int n, t; n   cin >> n; n   vector<int> arr(101); n   for (__typeof(n) i = 0; i < n; i++) { n     cin >> t; n     arr[t] = 1; n   } n   int col = 0; n   for (int i = 1; i <= 100; i++) n     if (arr[i]) { n       for (int j = i; j <= 100; j += i) { n         arr[j] = 0; n       } n       col++; n     } n   printf( %d n , col); n } n int main() { n   ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   int t = 1; n   while (t--) solve(); n } n


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


#include <bits/stdc++.h> n using namespace std; n const int mod = 998244353; n int power(int v, int u) { n   if (!u) return 1; n   int ok = power(v, u >> 1); n   if (u & 1) return (long long)ok * ok % mod * v % mod; n   return (long long)ok * ok % mod; n } n const int maxm = 2e5; n int b[maxm + 10]; n int main() { n   int n, m, A; n   scanf( %d%d%d , &n, &m, &A); n   for (int i = 1; i <= m; i++) scanf( %d , &b[i]); n   int ans = 1; n   int inv2 = power(2, mod - 2); n   for (int i = 1; i <= m; i++) n     ans = (long long)ans * n           (((long long)power(A, 2 * (b[i] - b[i - 1])) + n             power(A, b[i] - b[i - 1])) % n            mod * inv2 % mod) % n           mod; n   ans = (long long)ans * power(A, n - b[m] - b[m]) % mod; n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long double eps = 1e-9; n const int inf = (1 << 30) - 1; n const long long inf64 = ((long long)1 << 62) - 1; n const long double pi = acos(-1); n template <class T> n T sqr(T x) { n   return x * x; n } n template <class T> n T abs(T x) { n   return x < 0 ? -x : x; n } n const int MAXK = 105; n const int magic = 100 * 1000; n int a[MAXK]; n int ans_small[MAXK][magic]; n long long get_ans(long long n, int k) { n   if (k == 0) { n     return n; n   } n   if (n < magic) { n     return ans_small[k][n]; n   } n   return get_ans(n, k - 1) - get_ans(n / a[k - 1], k - 1); n } n int main() { n   ios_base::sync_with_stdio(0); n   long long n; n   int k; n   cin >> n >> k; n   for (int i = 0; i < k; ++i) { n     cin >> a[i]; n   } n   sort(a, a + k); n   for (int i = 0; i < magic; ++i) { n     ans_small[0][i] = i; n   } n   for (int i = 1; i <= k; ++i) { n     for (int j = 0; j < magic; ++j) { n       ans_small[i][j] = ans_small[i - 1][j] - ans_small[i - 1][j / a[i - 1]]; n     } n   } n   cout << get_ans(n, k) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1000000007; n long long Power(long long a, long long b) { n   if (a == 0) return 0LL; n   long long ans = 1; n   while (b) { n     if (b & 1) ans = (ans * a) % mod; n     a = (a * a) % mod; n     b >>= 1; n   } n   return ans; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long n, m, k; n   cin >> n >> m >> k; n   if (k == -1 && n % 2 != m % 2) { n     cout << 0 << endl; n     return 0; n   } n   long long ans = Power(Power(2, (n - 1)), m - 1); n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n long long dp[20][20][20]; n int a[20]; n long long num[20]; n long long dfs(int pos, int pre, int sta, bool flag) { n   if (pos == 0) { n     if (pre == sta) n       return 1; n     else n       return 0; n   } n   if (dp[pos][pre][sta] != -1 && !flag) return dp[pos][pre][sta]; n   int maxx = flag ? a[pos] : 9; n   long long sum = 0; n   for (int i = 0; i <= maxx; i++) { n     sum += dfs(pos - 1, pre + (i == 4 || i == 7), sta, flag && (i == a[pos])); n   } n   if (!flag) dp[pos][pre][sta] = sum; n   return sum; n } n long long solve(int pos, int res) { n   long long ans = 0; n   if (pos == 7) return 1; n   for (int i = 0; i <= res; i++) { n     if (num[i]) { n       num[i]--; n       ans = (ans + (num[i] + 1) * solve(pos + 1, res - i) % mod) % mod; n       num[i]++; n     } n   } n   return ans; n } n void cal(int x) { n   int len = 0; n   while (x) { n     len++; n     a[len] = x % 10; n     x /= 10; n   } n   for (int i = 0; i <= len; i++) { n     num[i] = dfs(len, 0, i, 1); n   } n   num[0]--; n   long long ans = 0; n   for (int i = 1; i <= len; i++) { n     ans = (ans + num[i] * solve(1, i - 1) % mod) % mod; n   } n   printf( %lld n , ans); n } n int main() { n   memset(dp, -1, sizeof dp); n   int m; n   scanf( %d , &m); n   cal(m); n } n


#include <bits/stdc++.h> n using namespace std; n int a[29]; n int main() { n   int n, k; n   char s[105]; n   scanf( %d , &n); n   scanf( %d , &k); n   scanf( %s , s + 1); n   bool f(true); n   for (int i = 1; i <= n; ++i) { n     int temp = s[i] -  a ; n     a[temp]++; n     if (a[temp] > k) { n       f = false; n       break; n     } n   } n   if (f) n     printf( YES n ); n   else n     printf( NO n ); n   return 0; n } n


#include <bits/stdc++.h> n const int mod = 1000003; n char str[2010]; n int f[2010][2010]; n int main() { n   int n, i, j, len; n   bool flag = true; n   gets(str); n   n = strlen(str); n   for (i = 0; i <= n; i++) { n     if (str[i] ==  *  || str[i] ==  /  || i == n) n       if (i == 0 || str[i - 1] <  0  || str[i - 1] >  9 ) flag = false; n   } n   if (!flag) { n     printf( 0 n ); n     return 0; n   } n   f[0][0] = 1; n   len = 0; n   for (i = 1; i <= n; i++) { n     flag = ( 0  <= str[i - 1] && str[i - 1] <=  9 ); n     if (!flag) len++; n     for (j = len; j >= 0; j--) { n       if (flag) { n         f[i][j] = f[i - 1][j]; n         if ( 0  > str[i] || str[i] >  9 ) n           f[i][j] = (f[i][j] + f[i][j + 1]) % mod; n       } else if (j > 0) n         f[i][j] = f[i - 1][j - 1]; n     } n   } n   printf( %d n , f[n][0]); 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 vl = vector<ll>; n using pi = pair<int, int>; n namespace output { n void pr(int x) { cout << x; } n void pr(long x) { cout << x; } n void pr(ll x) { cout << x; } n void pr(unsigned x) { cout << x; } n void pr(unsigned long x) { cout << x; } n void pr(unsigned long long x) { cout << x; } n void pr(float x) { cout << x; } n void pr(double x) { cout << x; } n void pr(long double x) { cout << x; } n void pr(char x) { cout << x; } n void pr(const char* x) { cout << x; } n void pr(const string& x) { cout << x; } n void pr(bool x) { pr(x ?  true  :  false ); } n template <class T1, class T2> n void pr(const pair<T1, T2>& x); n template <class T> n void pr(const T& x); n template <class T, class... Ts> n void pr(const T& t, const Ts&... ts) { n   pr(t); n   pr(ts...); n } n template <class T1, class T2> n void pr(const pair<T1, T2>& x) { n   pr( { , x.first,  ,  , x.second,  } ); n } n template <class T> n void pr(const T& x) { n   pr( { ); n   bool fst = 1; n   for (const auto& a : x) pr(!fst ?  ,   :   , a), fst = 0; n   pr( } ); n } n void ps() { pr(  n ); } n template <class T, class... Ts> n void ps(const T& t, const Ts&... ts) { n   pr(t); n   if (sizeof...(ts)) pr(   ); n   ps(ts...); n } n void pc() { cout <<  ]  << endl; } n template <class T, class... Ts> n void pc(const T& t, const Ts&... ts) { n   pr(t); n   if (sizeof...(ts)) pr( ,  ); n   pc(ts...); n } n }  // namespace output n using namespace output; n const int MX = 200005; n const int MOD = (int)(1e9 + 7); n const ll INF = (ll)1e18; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n; n   cin >> n; n   vi a(n); n   vi cnt(101); n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     cnt[a[i]]++; n   } n   pi best = make_pair(-1, -1); n   for (int i = 1; i <= 100; i++) { n     if (cnt[i] > best.second) best = make_pair(i, cnt[i]); n   } n   for (int i = 1; i <= 100; i++) { n     if (i != best.first && cnt[i] == best.second) { n       cout << n <<   n ; n       return 0; n     } n   } n   int ret = 0; n   for (int v = 1; v <= 100; v++) { n     if (v == best.first) continue; n     int ans = 0; n     vi nv(n); n     for (int i = 0; i < n; i++) { n       if (a[i] == v) n         nv[i] = -1; n       else if (a[i] == best.first) n         nv[i] = 1; n     } n     vi mn(2 * n + 1, -2); n     mn[n] = -1; n     int sum = 0; n     for (int i = 0; i < n; i++) { n       sum += nv[i]; n       if (mn[sum + n] != -2) n         ans = max(ans, i - mn[sum + n]); n       else n         mn[sum + n] = i; n     } n     ret = max(ret, ans); n   } n   cout << ret <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 10; n long long int cal(long long int num) { return (num * (num + 1)) / 2ll; } n void solve() { n   long long int n; n   scanf( %lld , &n); n   n--; n   long long int ans = 0, k = 1; n   for (long long int i = 1; i <= n; i <<= 1) { n     ans += ((n - i) / (i << 1) + 1) * i; n   } n   printf( %lld n , ans); n } n int main() { n   int t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 9; n const double eps = 1e-5; n const int maxN = 2e3 + 10; n const int inf = 1e9; n int n, m; n vector<pair<int, int> > a[maxN]; n int vis[maxN], c[maxN]; n vector<int> pat; n inline void dfs(int v, int f = -1) { n   for (int i = 0; i < a[v].size(); i++) { n     if (a[v][i].first <= f) continue; n     if (!vis[a[v][i].second]) { n       vis[a[v][i].second] = 1; n       dfs(a[v][i].first); n     } n   } n   pat.push_back(v); n } n inline void init() { n   cin >> n >> m; n   for (int i = 0; i < m + 1; i++) { n     cin >> c[i]; n     c[i]--; n   } n   for (int i = 1; i < m + 1; i++) { n     int v = c[i - 1], u = c[i]; n     a[v].push_back(make_pair(u, i - 1)); n     a[u].push_back(make_pair(v, i - 1)); n   } n   for (int i = 0; i < n; i++) sort(a[i].begin(), a[i].end()); n } n inline void solve() { n   for (int i = m - 1; i >= 0; i--) { n     memset(vis, 0, sizeof vis); n     pat.clear(); n     for (int j = 0; j < i; j++) vis[j] = 1; n     dfs(c[i], c[i + 1]); n     if (i + pat.size() == m + 1) { n       reverse(pat.begin(), pat.end()); n       for (int j = 0; j < i; j++) cout << c[j] + 1 <<    ; n       for (int j = 0; j < pat.size(); j++) cout << pat[j] + 1 <<    ; n       cout << endl; n       return; n     } n   } n   cout <<  No solution  << endl; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(); n   cout.tie(); n   init(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n vector<int> edge[110000]; n vector<pair<int, int> > R; n set<int> N; n int find(int rt, int to) { n   int i; n   for (i = 0; i < edge[rt].size(); i++) { n     if (edge[rt][i] == to) return 1; n   } n   return 0; n } n int dfs(int rt) { n   if (R.size() == m || R.size() == n - 1) return 1; n   N.erase(rt); n   set<int>::iterator it; n   for (it = N.begin(); it != N.end(); it++) { n     if (find(rt, *it)) continue; n     R.push_back(make_pair(rt, *it)); n     if (dfs(*it)) return 1; n     R.pop_back(); n     it = N.find(*it); n   } n   N.insert(rt); n   return 0; n } n int main() { n   int a, b, i; n   scanf( %d%d , &n, &m); n   for (i = 1; i <= m; i++) { n     scanf( %d%d , &a, &b); n     edge[a].push_back(b); n     edge[b].push_back(a); n   } n   for (i = 1; i <= n; i++) { n     N.insert(i); n   } n   for (i = 1; i <= n; i++) { n     if (dfs(i)) { n       if (n == m) R.push_back(make_pair(R[0].first, R[R.size() - 1].second)); n       for (i = 0; i < m; i++) printf( %d %d n , R[i].first, R[i].second); n       return 0; n     } n   } n   puts( -1 ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s[40] = { n        Washington ,  Adams ,      Jefferson ,  Madison ,     Monroe , n        Adams ,       Jackson ,    Van Buren ,  Harrison ,    Tyler , n        Polk ,        Taylor ,     Fillmore ,   Pierce ,      Buchanan , n        Lincoln ,     Johnson ,    Grant ,      Hayes ,       Garfield , n        Arthur ,      Cleveland ,  Harrison ,   Cleveland ,   McKinley , n        Roosevelt ,   Taft ,       Wilson ,     Harding ,     Coolidge , n        Hoover ,      Roosevelt ,  Truman ,     Eisenhower ,  Kennedy , n        Johnson ,     Nixon ,      Ford ,       Carter ,      Reagan }; n   int a; n   cin >> a; n   cout << s[a - 1] <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void out(T x) { n   cout << x << endl; n   exit(0); n } n const int maxn = 1e6 + 5; n int n, m; n int a[maxn]; n vector<int> g[maxn]; n vector<pair<int, pair<int, int>>> edges; n int par[maxn], siz[maxn]; n int f(int x) { return par[x] == x ? x : par[x] = f(par[x]); } n void join(int x, int y) { n   x = f(x); n   y = f(y); n   if (x == y) return; n   if (siz[x] < siz[y]) swap(x, y); n   siz[x] += siz[y]; n   par[y] = x; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     par[i] = i; n     siz[i] = 1; n   } n   for (int i = 0; i < m; i++) { n     int u, v; n     cin >> u >> v; n     g[u].push_back(v); n     g[v].push_back(u); n     edges.push_back({min(a[u], a[v]), {u, v}}); n   } n   auto cmp = [](pair<int, pair<int, int>> p1, pair<int, pair<int, int>> p2) { n     return p1.first > p2.first; n   }; n   long double num = 0; n   long double den = 0; n   sort(edges.begin(), edges.end(), cmp); n   for (auto p : edges) { n     int wei = p.first; n     int u = f(p.second.first); n     int v = f(p.second.second); n     if (u == v) continue; n     long double paths = 1LL * siz[u] * siz[v]; n     den += paths; n     num += 1.0 * wei * paths; n     join(u, v); n   } n   long double ans = num / den; n   cout << fixed << setprecision(9) << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n class flow { n  private: n   int n; n   vector<int> head, d, cur; n   vector<tuple<int, ll, int>> edge; n   bool bfs(int s, int t) { n     d.assign(n, -1); n     queue<int> que; n     que.push(s); n     d[s] = 0; n     while (!que.empty()) { n       int u = que.front(); n       que.pop(); n       for (int i = head[u]; i != -1; i = get<2>(edge[i])) { n         int v = get<0>(edge[i]); n         if (get<1>(edge[i]) && d[v] == -1) { n           d[v] = d[u] + 1; n           if (v == t) { n             return true; n           } n           que.push(v); n         } n       } n     } n     return false; n   } n   ll dfs(int u, int t, ll f) { n     if (u == t) { n       return f; n     } n     ll res = f; n     for (int &i = cur[u]; i != -1 && res; i = get<2>(edge[i])) { n       int v = get<0>(edge[i]); n       ll &c = get<1>(edge[i]); n       if (d[v] == d[u] + 1 && c) { n         ll aug = dfs(v, t, min(res, c)); n         res -= aug; n         c -= aug; n         get<1>(edge[i ^ 1]) += aug; n       } n     } n     return f - res; n   } n  n  public: n   flow(int _n) { n     n = _n; n     head.assign(n, -1); n   } n   void insert_edge(int u, int v, ll c) { n     edge.emplace_back(v, c, head[u]); n     head[u] = edge.size() - 1; n     edge.emplace_back(u, 0, head[v]); n     head[v] = edge.size() - 1; n   } n   ll max_flow(int s, int t) { n     ll flow = 0; n     while (bfs(s, t)) { n       cur = head; n       flow += dfs(s, t, 1e18); n     } n     return flow; n   } n }; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n, m; n   cin >> n >> m; n   vector<vector<int>> dis(n, vector<int>(n, 1e9)); n   for (int i = 0; i < n; ++i) { n     dis[i][i] = 0; n   } n   for (int i = 0; i < m; ++i) { n     int u, v; n     cin >> u >> v; n     --u; n     --v; n     dis[u][v] = 1; n     dis[v][u] = 1; n   } n   for (int k = 0; k < n; ++k) { n     for (int i = 0; i < n; ++i) { n       for (int j = 0; j < n; ++j) { n         dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]); n       } n     } n   } n   int s, b, k; n   cin >> s >> b >> k; n   vector<tuple<int, int, int, int>> ship; n   vector<vector<pair<int, int>>> base(n); n   for (int i = 0; i < s; ++i) { n     int x, a, f, p; n     cin >> x >> a >> f >> p; n     --x; n     ship.emplace_back(x, a, f, p); n   } n   for (int i = 0; i < b; ++i) { n     int x, d, g; n     cin >> x >> d >> g; n     --x; n     base[x].emplace_back(d, g); n   } n   for (int i = 0; i < n; ++i) { n     sort(base[i].begin(), base[i].end()); n     for (int j = 1; j < base[i].size(); ++j) { n       base[i][j].second = max(base[i][j].second, base[i][j - 1].second); n     } n   } n   vector<ll> gain(s); n   for (int i = 0; i < s; ++i) { n     int x = get<0>(ship[i]); n     int a = get<1>(ship[i]); n     int f = get<2>(ship[i]); n     int p = get<3>(ship[i]); n     gain[i] = -1e14; n     for (int j = 0; j < n; ++j) { n       if (dis[x][j] <= f && !base[j].empty() && a >= base[j][0].first) { n         gain[i] = n             max(gain[i], 1ll * (--upper_bound(base[j].begin(), base[j].end(), n                                               make_pair(a, (int)1e9))) n                                      ->second - n                              p); n       } n     } n   } n   vector<bool> used(s); n   vector<pair<int, int>> rel; n   for (int i = 0; i < k; ++i) { n     int x, y; n     cin >> x >> y; n     --x; n     --y; n     used[x] = true; n     used[y] = true; n     rel.emplace_back(x, y); n   } n   int cnt = 0; n   ll ans = 0; n   vector<int> id(s, -1); n   for (int i = 0; i < s; ++i) { n     if (used[i]) { n       id[i] = cnt++; n     } else if (gain[i] > 0) { n       ans += gain[i]; n     } n   } n   flow G(cnt + 2); n   int S = cnt; n   int T = cnt + 1; n   for (int i = 0; i < s; ++i) { n     if (used[i]) { n       if (gain[i] > 0) { n         G.insert_edge(S, id[i], gain[i]); n         ans += gain[i]; n       } else if (gain[i] < 0) { n         G.insert_edge(id[i], T, -gain[i]); n       } n     } n   } n   for (int i = 0; i < k; ++i) { n     G.insert_edge(id[rel[i].first], id[rel[i].second], 1e18); n   } n   ans -= G.max_flow(S, T); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = (int)1e9 + 7; n const int FFTMOD = 119 << 23 | 1; n const int INF = (int)1e9 + 23111992; n const long long LINF = (long long)1e18 + 23111992; n const long double PI = acos((long double)-1); n const long double EPS = 1e-9; n inline long long gcd(long long a, long long b) { n   long long r; n   while (b) { n     r = a % b; n     a = b; n     b = r; n   } n   return a; n } n inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } n inline long long fpow(long long n, long long k, int p = MOD) { n   long long r = 1; n   for (; k; k >>= 1) { n     if (k & 1) r = r * n % p; n     n = n * n % p; n   } n   return r; n } n template <class T> n inline int chkmin(T& a, const T& val) { n   return val < a ? a = val, 1 : 0; n } n template <class T> n inline int chkmax(T& a, const T& val) { n   return a < val ? a = val, 1 : 0; n } n inline unsigned long long isqrt(unsigned long long k) { n   unsigned long long r = sqrt(k) + 1; n   while (r * r > k) r--; n   return r; n } n inline long long icbrt(long long k) { n   long long r = cbrt(k) + 1; n   while (r * r * r > k) r--; n   return r; n } n inline void addmod(int& a, int val, int p = MOD) { n   if ((a = (a + val)) >= p) a -= p; n } n inline void submod(int& a, int val, int p = MOD) { n   if ((a = (a - val)) < 0) a += p; n } n inline int mult(int a, int b, int p = MOD) { return (long long)a * b % p; } n inline int inv(int a, int p = MOD) { return fpow(a, p - 2, p); } n inline int sign(long double x) { return x < -EPS ? -1 : x > +EPS; } n inline int sign(long double x, long double y) { return sign(x - y); } n mt19937 mt(chrono::high_resolution_clock::now().time_since_epoch().count()); n inline int mrand() { return abs((int)mt()); } n inline int mrand(int k) { return abs((int)mt()) % k; } n template <class num_t> n struct SegmentTree { n   struct node_t { n     num_t max[2]; n     num_t pref[2]; n     num_t suff[2]; n     num_t pref2[2]; n     num_t suff2[2]; n     num_t lz; n     int size; n     node_t() { n       lz = 0; n       size = -1; n     } n   }; n   int n; n   vector<node_t> st; n   void init(int n_) { n     n = n_; n     st.resize(n << 2); n   } n   node_t merge(node_t u, node_t v) { n     if (u.size == -1) return v; n     if (v.size == -1) return u; n     node_t c; n     for (int i = (0); i < (2); ++i) { n       c.max[i] = max(u.max[i], v.max[i]); n       c.pref[i] = u.pref[i]; n       c.suff[i] = v.suff[i]; n       c.pref2[i] = u.pref2[i]; n       c.suff2[i] = v.suff2[i]; n     } n     for (int i = (0); i < (2); ++i) { n       if (u.pref[i] == u.size) { n         c.pref[i] = u.pref[i] + v.pref[i]; n       } n       if (v.suff[i] == v.size) { n         c.suff[i] = u.suff[i] + v.suff[i]; n       } n       if (u.pref[i ^ 1] == u.size) { n         c.pref2[i] = u.pref[i ^ 1] + v.pref2[i]; n       } n       if (v.suff[i ^ 1] == v.size) { n         c.suff2[i] = u.suff2[i] + v.suff[i ^ 1]; n       } n       if (u.pref2[i] == u.size) { n         chkmax(c.pref2[i], u.pref2[i] + v.pref[i]); n       } n       if (v.suff2[i] == v.size) { n         chkmax(c.suff2[i], u.suff[i] + v.suff2[i]); n       } n     } n     for (int i = (0); i < (2); ++i) { n       chkmax(c.pref2[i], c.pref[i]); n       chkmax(c.suff2[i], c.suff[i]); n     } n     for (int i = (0); i < (2); ++i) { n       chkmax(c.max[i], c.pref[i]); n       chkmax(c.max[i], c.suff[i]); n       chkmax(c.max[i], c.pref2[i]); n       chkmax(c.max[i], c.suff2[i]); n       chkmax(c.max[i], u.suff[i] + v.pref2[i]); n       chkmax(c.max[i], u.suff2[i] + v.pref[i]); n     } n     c.size = u.size + v.size; n     return c; n   } n   void build(int p, int L, int R, vector<int>& a) { n     if (L == R) { n       st[p].max[0] = st[p].max[1] = 1; n       for (int i = (0); i < (2); ++i) { n         st[p].pref[i] = st[p].pref2[i] = (a[L] == i); n         st[p].suff[i] = st[p].suff2[i] = (a[R] != i); n       } n       st[p].size = 1; n       return; n     } n     build(p << 1, L, L + R >> 1, a); n     build(p << 1 | 1, (L + R >> 1) + 1, R, a); n     st[p] = merge(st[p << 1], st[p << 1 | 1]); n   } n   void push(int p, int L, int R) { n     if (st[p].lz) { n       swap(st[p].max[0], st[p].max[1]); n       swap(st[p].pref[0], st[p].pref[1]); n       swap(st[p].suff[0], st[p].suff[1]); n       swap(st[p].pref2[0], st[p].pref2[1]); n       swap(st[p].suff2[0], st[p].suff2[1]); n       if (L < R) { n         st[p << 1].lz ^= st[p].lz; n         st[p << 1 | 1].lz ^= st[p].lz; n       } n       st[p].lz = 0; n     } n   } n   void upd(int p, int l, int r, int L, int R, num_t val) { n     push(p, L, R); n     if (r < L || R < l) return; n     if (l <= L && R <= r) { n       st[p].lz = val; n       push(p, L, R); n       return; n     } n     upd(p << 1, l, r, L, L + R >> 1, val); n     upd(p << 1 | 1, l, r, (L + R >> 1) + 1, R, val); n     st[p] = merge(st[p << 1], st[p << 1 | 1]); n   } n   node_t query(int p, int l, int r, int L, int R) { n     push(p, L, R); n     if (r < L || R < l) return node_t(); n     if (l <= L && R <= r) { n       return st[p]; n     } n     return merge(query(p << 1, l, r, L, L + R >> 1), n                  query(p << 1 | 1, l, r, (L + R >> 1) + 1, R)); n   } n   void upd(int l, int r, num_t val) { upd(1, l, r, 0, n - 1, val); } n   node_t query(int l, int r) { return query(1, l, r, 0, n - 1); } n }; n SegmentTree<int> seg; n void chemthan() { n   int n, q; n   cin >> n >> q; n   string s; n   cin >> s; n   vector<int> a(n); n   for (int i = (0); i < (n); ++i) a[i] = s[i] ==  > ; n   seg.init(n); n   seg.build(1, 0, n - 1, a); n   while (q--) { n     int l, r; n     cin >> l >> r; n     l--, r--; n     seg.upd(l, r, 1); n     cout << seg.query(l, r).max[0] <<   n ; n   } n } n int main(int argc, char* argv[]) { n   ios_base::sync_with_stdio(0), cin.tie(0); n   if (argc > 1) { n     assert(freopen(argv[1],  r , stdin)); n   } n   if (argc > 2) { n     assert(freopen(argv[2],  wb , stdout)); n   } n   chemthan(); n   cerr <<   nTime elapsed:   << 1000 * clock() / CLOCKS_PER_SEC <<  ms n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(int argc, char** argv) { n   int T; n   cin >> T; n   while (T--) { n     long long a, b, c, d; n     cin >> a >> b >> c >> d; n     d -= min(a, d / c) * c; n     if (d > b) n       puts( NO ); n     else n       puts( YES ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s[10]; n   s[0] =  O-|-OOOO ; n   s[1] =  O-|O-OOO ; n   s[2] =  O-|OO-OO ; n   s[3] =  O-|OOO-O ; n   s[4] =  O-|OOOO- ; n   s[5] =  -O|-OOOO ; n   s[6] =  -O|O-OOO ; n   s[7] =  -O|OO-OO ; n   s[8] =  -O|OOO-O ; n   s[9] =  -O|OOOO- ; n   int n; n   cin >> n; n   if (n == 0) cout << s[0] << endl; n   while (n != 0) { n     int z = n % 10; n     cout << s[z] << endl; n     n /= 10; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int ROW[] = {+0, +0, -1, +1}; n int COL[] = {+1, -1, +0, +0}; n int X[] = {+0, +0, +1, -1, -1, +1, -1, +1}; n int Y[] = {-1, +1, +0, +0, +1, +1, -1, -1}; n int KX[] = {-2, -2, -1, -1, 1, 1, 2, 2}; n int KY[] = {-1, 1, -2, 2, -2, 2, -1, 1}; n long long calc(long long num, long long val, long long cnt1, long long cnt2) { n   while (cnt1 >= cnt2) { n     num /= val; n     cnt1--; n   } n   return num; n } n int main() { n   int t; n   scanf( %d , &t); n   while (t--) { n     long long p, q, n, m; n     scanf( %lld %lld , &p, &q); n     n = p; n     m = q; n     if (p % q) { n       printf( %lld n , p); n       continue; n     } n     map<long long, long long> mp, mpp; n     for (long long i = 2; i * i <= q; i++) { n       if (q % i) continue; n       while (q % i == 0) { n         mp[i]++; n         q /= i; n       } n     } n     if (q > 1) mp[q]++; n     long long ans = 1LL; n     for (auto it : mp) { n       while (p % it.first == 0) { n         p /= it.first; n         mpp[it.first]++; n       } n       ans = max(ans, calc(n, it.first, mpp[it.first], it.second)); n     } n     printf( %lld n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s; n int poz, sol; n int main() { n   sol = INT_MAX; n   cin >> s; n   int n = s.size(); n   for (int c = 1; c <= 26; c++) { n     char ch = c +  a  - 1; n     poz = -1; n     int maxx = 0; n     for (int i = 0; i < n; i++) { n       if (s[i] == ch) { n         if (i - poz > maxx) maxx = i - poz; n         poz = i; n       } n     } n     if (n - poz > maxx) maxx = n - poz; n     if (sol > maxx) sol = maxx; n   } n   cout << sol; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   cout << n <<    ; n   for (int i = 1; i < n; ++i) cout << i <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n int32_t main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   long long n, w; n   cin >> n >> w; n   vector<long long> arr(n); n   for (long long i = 0; i < n; ++i) cin >> arr[i]; n   vector<long long> runsum(n); n   long long min_elem = arr[0]; n   long long max_elem = arr[0]; n   vector<long long> run(n); n   long long cur = 0; n   long long tillnow = -1e9; n   for (long long i = 0; i < n; ++i) { n     cur = max(arr[i], cur + arr[i]); n     tillnow = max(tillnow, cur); n   } n   if (tillnow > w) { n     cout << 0 << endl; n     return 0; n   } n   max_elem = tillnow; n   cur = 0; n   tillnow = 1e9; n   for (long long i = 0; i < n; ++i) { n     cur = min(arr[i], cur + arr[i]); n     tillnow = min(tillnow, cur); n   } n   if (tillnow < -w) { n     cout << 0 << endl; n     return 0; n   } n   min_elem = tillnow; n   long long answer = w - max(abs(max_elem), abs(min_elem)) + 1; n   cout << answer << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void ASS(bool bb) { n   if (!bb) { n     ++*(int*)0; n   } n } n #pragma comment(linker,  /STACK:106777216 ) n int main() { n   int a, b, m; n   cin >> a >> b >> m; n   for (int i = 0; i < (int)(min(a + 1, m)); i++) { n     int x = i * (long long)1000000000 % m; n     x = (m - x) % m; n     if (x > b) { n       printf( 1 %09d n , i); n       return 0; n     } n   } n   cout << 2 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int start, ed = 0, sz = 130000000; n long long marked[(130000000 / 64) + 5]; n vector<int> primes; n inline bool check(int pos) { return !(marked[pos / 64] & (1LL << (pos % 64))); } n int pos; n bool isPrime(int n) { n   if (n != 2 and n % 2 == 0) return false; n   int sq = sqrt(n); n   for (int i = 3; i <= sq; i += 2) { n     if (n % i == 0) return false; n   } n   return true; n } n void genPrimes(int n) { n   int sq = sqrt(n); n   for (int i = 2; i <= sq; i++) { n     if (isPrime(i)) primes.push_back(i); n   } n } n void segSieve(int L, int R) { n   if (L <= 1 and R >= 1) n     pos = 1 - start, marked[pos / 64] |= (1LL << (pos % 64)); n   int sq = sqrt(R), st; n   for (int i = 0; primes[i] <= sq and i < primes.size(); i++) { n     if (L <= primes[i]) n       st = primes[i] + primes[i]; n     else n       st = ((L + primes[i] - 1) / primes[i]) * primes[i]; n     for (int j = st; j <= R; j += primes[i]) { n       pos = j - start, marked[pos / 64] |= (1LL << (pos % 64)); n     } n   } n } n unsigned int ans = 0, a, b, c, d; n inline unsigned int F(unsigned int x) { return x * (x * (a * x + b) + c) + d; } n void call(int n) { n   int sum = 0; n   long long cur; n   for (int i = 2; i <= n; i++) { n     if (i > ed) { n       memset(marked, 0, sizeof(marked)); n       start = ed + 1; n       ed = start + sz - 1; n       segSieve(start, min(ed, 300000000)); n     } n     if (!check(i - start)) continue; n     cur = i, sum = 0; n     while (cur <= n) { n       sum += (n / cur); n       cur *= i; n     } n     ans += sum * F(i); n   } n } n int main() { n   int n; n   cin >> n >> a >> b >> c >> d; n   genPrimes(n); n   call(n); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   int l, r; n   int num; n } Treex[4000009], Treey[4000009]; n map<int, int> vis; n int i; n int tot = 1; n int Tot = 1; n int x, y; n char str[5]; n void update_y(int ll, int rr, int c, int l, int r, int t) { n   if (ll <= l && r <= rr) n     Treey[t].num = max(Treey[t].num, c); n   else { n     if (ll <= ((l + r) >> 1)) { n       if (!Treey[t].l) Treey[t].l = ++tot; n       update_y(ll, rr, c, l, ((l + r) >> 1), Treey[t].l); n     } n     if (rr > ((l + r) >> 1)) { n       if (!Treey[t].r) Treey[t].r = ++tot; n       update_y(ll, rr, c, ((l + r) >> 1) + 1, r, Treey[t].r); n     } n   } n } n void update_x(int ll, int rr, int c, int l, int r, int t) { n   if (ll <= l && r <= rr) n     Treex[t].num = max(Treex[t].num, c); n   else { n     if (ll <= ((l + r) >> 1)) { n       if (!Treex[t].l) Treex[t].l = ++Tot; n       update_x(ll, rr, c, l, ((l + r) >> 1), Treex[t].l); n     } n     if (rr > ((l + r) >> 1)) { n       if (!Treex[t].r) Treex[t].r = ++Tot; n       update_x(ll, rr, c, ((l + r) >> 1) + 1, r, Treex[t].r); n     } n   } n } n void query_x(int c, int l, int r, int rt) { n   i = max(i, Treex[rt].num); n   if (l == r) return; n   if (((l + r) >> 1) >= c) n     query_x(c, l, ((l + r) >> 1), Treex[rt].l); n   else n     query_x(c, ((l + r) >> 1) + 1, r, Treex[rt].r); n } n void query_y(int c, int l, int r, int rt) { n   i = max(i, Treey[rt].num); n   if (l == r) return; n   if (((l + r) >> 1) >= c) n     query_y(c, l, ((l + r) >> 1), Treey[rt].l); n   else n     query_y(c, ((l + r) >> 1) + 1, r, Treey[rt].r); n } n int main() { n   int n, q; n   scanf( %d%d , &n, &q); n   update_x(0, n, 0, 0, n, 1); n   update_y(0, n, 0, 0, n, 1); n   while (q--) { n     scanf( %d%d%s , &x, &y, str); n     if (vis[x]) { n       puts( 0 ); n     } else { n       vis[x] = 1; n       i = 0; n       if (str[0] ==  U ) { n         query_x(x, 0, n, 1); n         printf( %d n , y - i); n         update_y(i, y, x, 0, n, 1); n       } else { n         query_y(y, 0, n, 1); n         printf( %d n , x - i); n         update_x(i, x, y, 0, n, 1); n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long PINF = 1E9; n const long long NINF = -1E9; n const long long M = 1E9 + 7; n const double EPS = 1E-9; n const double PI = 2 * acos(0); n void print_arr(const vector<long long> &a) { n   for (int i = 0; i < (int)a.size(); i++) cout << a[i] <<    ; n   cout <<   n ; n } n int m; n vector<long long> weights, ans; n bool visited[1001][11][11]; n void dfs(int step, int balance, int prev_weight) { n   if (visited[step][balance][prev_weight]) return; n   visited[step][balance][prev_weight] = true; n   if (step == m) { n     cout <<  YES  n          <<   n ; n     print_arr(ans); n     exit(0); n   } n   for (int i = 0; i < (int)weights.size(); i++) { n     int w = weights[i]; n     if (prev_weight != w && abs(balance) < w) { n       ans[step] = w; n       if (step % 2 == 0) n         dfs(step + 1, balance + w, w); n       else n         dfs(step + 1, balance - w, w); n     } n     ans[step] = 0; n   } n   visited[step][balance][prev_weight] = false; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   string s; n   cin >> s; n   for (int i = 0; i < 10; i++) { n     if (s[i] ==  1 ) weights.push_back(i + 1); n   } n   cin >> m; n   ans.resize(m); n   dfs(0, 0, 0); n   cout <<  NO  n        <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline T bigmod(T p, T e, T M) { n   long long ret = 1; n   for (; e > 0; e >>= 1) { n     if (e & 1) ret = (ret * p) % M; n     p = (p * p) % M; n   } n   return (T)ret; n } n template <class T> n inline T gcd(T a, T b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n template <class T> n inline T modinverse(T a, T M) { n   return bigmod(a, M - 2, M); n } n template <class T> n inline T lcm(T a, T b) { n   a = abs(a); n   b = abs(b); n   return (a / gcd(a, b)) * b; n } n template <class T, class X> n inline bool getbit(T a, X i) { n   T t = 1; n   return ((a & (t << i)) > 0); n } n template <class T, class X> n inline T setbit(T a, X i) { n   T t = 1; n   return (a | (t << i)); n } n template <class T, class X> n inline T resetbit(T a, X i) { n   T t = 1; n   return (a & (~(t << i))); n } n inline long long power(long long a, long long b) { n   long long multiply = 1; n   for (int i = (0); i < (b); i++) { n     multiply *= a; n   } n   return multiply; n } n vector<int> v[102]; n int main() { n   int n, m, x; n   int first = 0, second = 0; n   cin >> n; n   for (int i = (0); i < (n); i++) { n     cin >> m; n     for (int j = (0); j < (m); j++) { n       cin >> x; n       v[i].push_back(x); n     } n   } n   vector<int> rest; n   for (int i = (0); i < (n); i++) { n     if (v[i].size() % 2 == 0) { n       for (int j = (0); j < (v[i].size() / 2); j++) first += v[i][j]; n       for (int j = (v[i].size() / 2); j < (v[i].size()); j++) second += v[i][j]; n     } else { n       for (int j = (0); j < (v[i].size() / 2); j++) first += v[i][j]; n       for (int j = (v[i].size() / 2 + 1); j < (v[i].size()); j++) n         second += v[i][j]; n     } n   } n   for (int j = (0); j < (n); j++) { n     if (v[j].size() % 2 == 0) continue; n     rest.push_back(v[j][v[j].size() / 2]); n   } n   sort(rest.begin(), rest.end(), greater<int>()); n   for (int j = (0); j < (rest.size()); j++) { n     if (j % 2 == 0) n       first += rest[j]; n     else n       second += rest[j]; n   } n   cout << first <<     << second << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct edge { n   int t, nxt; n } e[600010]; n int n, cnt, be[300010], mn_mx[300010], f, vid[300010], tot; n void add(const int& x, const int& y) { n   e[++cnt].t = y, e[cnt].nxt = be[x], be[x] = cnt; n } n void dfs(const int& x) { n   if (!be[x]) return (void)(++tot, vid[x] = 1); n   if (mn_mx[x]) { n     vid[x] = 1e9; n     for (int i = be[x]; i; i = e[i].nxt) n       dfs(e[i].t), vid[x] = min(vid[x], vid[e[i].t]); n   } else n     for (int i = be[x]; i; i = e[i].nxt) dfs(e[i].t), vid[x] += vid[e[i].t]; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) scanf( %d , &mn_mx[i]); n   for (int i = 2; i <= n; ++i) scanf( %d , &f), add(f, i); n   dfs(1), printf( %d , tot - vid[1] + 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int mod = 1000000007; n long long int MAX = 1e18; n void solve() { n   long long int n; n   scanf( %lld , &n); n   long long int a[n]; n   vector<pair<long long int, long long int> > v(n), ans; n   for (long long int i = 0; i < n; i++) { n     long long int num; n     scanf( %lld , &num); n     a[i] = num; n     v[i] = {num - 1, i}; n   } n   sort(begin(v), end(v)); n   for (long long int i = n / 2, j = n / 2 - 1; i < n; i++, j--) { n     if (v[i].second > i) { n       ans.push_back({v[i].second + 1, 1}); n       ans.push_back({1, i + 1}); n       ans.push_back({v[i].second + 1, 1}); n       v[a[i] - 1].second = v[i].second; n       swap(a[i], a[v[i].second]); n     } else if (v[i].second < i) { n       ans.push_back({v[i].second + 1, n}); n       ans.push_back({n, 1}); n       ans.push_back({1, i + 1}); n       ans.push_back({n, 1}); n       ans.push_back({v[i].second + 1, n}); n       v[a[i] - 1].second = v[i].second; n       swap(a[i], a[v[i].second]); n     } n     if (v[j].second < j) { n       ans.push_back({v[j].second + 1, n}); n       ans.push_back({j + 1, n}); n       ans.push_back({n, v[j].second + 1}); n       v[a[j] - 1].second = v[j].second; n       swap(a[j], a[v[j].second]); n     } else if (v[j].second > j) { n       ans.push_back({v[j].second + 1, 1}); n       ans.push_back({1, n}); n       ans.push_back({j + 1, n}); n       ans.push_back({n, 1}); n       ans.push_back({v[j].second + 1, 1}); n       v[a[j] - 1].second = v[j].second; n       swap(a[j], a[v[j].second]); n     } n   } n   cout << ans.size() << endl; n   for (long long int i = 0; i < ans.size(); i++) n     printf( %lld %lld n , ans[i].first, ans[i].second); n } n int main() { n   long long int t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, c = 0; n   cin >> n; n   long long int d; n   cin >> d; n   long long int s[n + 5]; n   for (long long int i = 0; i < n; i++) { n     cin >> s[i]; n   } n   sort(s, s + n); n   for (long long int i = 0; i < n - 1; i++) { n     for (long long int j = i + 1; j < n; j++) { n       if (s[j] - s[i] <= d) { n         c++; n       } n     } n   } n   cout << c * 2 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long power(long long x, long long y, long long p); n inline int D() { n   int t; n   scanf( %d , &t); n   return t; n } n inline long long llD() { n   long long t; n   scanf( %lld , &t); n   return t; n } n const int N = 1e6 + 5; n long long arr[N], cost[N]; n int main() { n   long long n = llD(), k = llD(), l = 1, r = 0, mid = -1; n   for (int i = 0; i < n; ++i) r += pow(2, i); n   while (mid != k) { n     mid = (l + r) / 2; n     if (mid > k) n       r = mid - 1, --n; n     else if (mid < k) n       l = mid + 1, --n; n   } n   printf( %lld , n); n } n long long power(long long x, long long y, long long p) { n   long long res = 1; n   x = x % p; n   while (y > 0) { n     if (y & 1) res = (res * x) % p; n     y = y >> 1; n     x = (x * x) % p; n   } n   return res; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 1e6 + 20; n struct Edge { n   int to, w, next; n } edge[M]; n int head[M], cnt, fa[M]; n inline void add(int u, int v, int w) { n   edge[++cnt].to = v, edge[cnt].w = w, edge[cnt].next = head[u], head[u] = cnt; n } n long long mxp[M]; n int val[M]; n long long dfs1(int now) { n   mxp[now] = val[now]; n   for (int i = head[now]; i; i = edge[i].next) { n     if (edge[i].to == fa[now]) continue; n     fa[edge[i].to] = now, n     mxp[now] = max(mxp[now], val[now] - edge[i].w + dfs1(edge[i].to)); n   } n   return mxp[now]; n } n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) scanf( %d , &val[i]); n   for (int i = 1; i <= n - 1; ++i) { n     int u, v, w; n     scanf( %d%d%d , &u, &v, &w), add(u, v, w), add(v, u, w); n   } n   dfs1(1); n   long long ans = 0; n   for (int i = 1; i <= n; ++i) { n     long long mx1 = 0, mx2 = 0; n     for (int j = head[i]; j; j = edge[j].next) { n       if (edge[j].to == fa[i]) continue; n       long long c = mxp[edge[j].to] - edge[j].w; n       if (c > mx1) n         mx2 = mx1, mx1 = c; n       else if (c > mx2) n         mx2 = c; n     } n     ans = max(ans, val[i] + mx1 + mx2); n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n int main() { n   int n; n   scanf( %d , &n); n   vector<vector<pair<int, int>>> a(n); n   for (int i = 0; i < n - 1; i++) { n     int u, v; n     scanf( %d %d , &u, &v); n     --u, --v; n     a[u].emplace_back(v, i); n     a[v].emplace_back(u, i); n   } n   vector<int> lev(n, 1), pr(n); n   function<void(int, int)> dfs = [&](int u, int p) { n     pr[u] = p; n     for (auto [v, id] : a[u]) { n       if (v != p) { n         lev[v] = 1 + lev[u]; n         dfs(v, u); n       } n     } n   }; n   dfs(0, -1); n   vector<vector<int>> sp(n, vector<int>(22, -1)); n   for (int i = 0; i < 22; i++) { n     for (int j = 0; j < n; j++) { n       if (i == 0) { n         sp[j][i] = pr[j]; n       } else if (sp[j][i - 1] != -1) { n         sp[j][i] = sp[sp[j][i - 1]][i - 1]; n       } n     } n   } n   function<int(int, int)> lca = [&](int u, int v) { n     if (lev[u] < lev[v]) { n       swap(u, v); n     } n     int lg = 0; n     while ((1 << lg) <= lev[u]) { n       ++lg; n     } n     --lg; n     for (int i = lg; i >= 0; i--) { n       if (lev[u] >= lev[v] + (1 << i)) { n         u = sp[u][i]; n       } n     } n     if (u == v) { n       return u; n     } n     for (int i = lg; i >= 0; i--) { n       if (sp[u][i] != sp[v][i]) { n         u = sp[u][i]; n         v = sp[v][i]; n       } n     } n     return (u == v) ? u : pr[u]; n   }; n   int k; n   scanf( %d , &k); n   vector<int> val(n); n   for (int i = 0; i < k; i++) { n     int u, v; n     scanf( %d %d , &u, &v); n     --u, --v; n     int g = lca(u, v); n     ++val[u], ++val[v]; n     val[g] -= 2; n   } n   vector<int> ans(n - 1); n   function<void(int, int)> dfs2 = [&](int u, int p) { n     for (auto [v, id] : a[u]) { n       if (v != p) { n         dfs2(v, u); n         val[u] += val[v]; n         ans[id] = val[v]; n       } n     } n   }; n   dfs2(0, -1); n   for (int v : ans) { n     printf( %d  , v); n   } n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 200010; n const int MODD = 1000000007; n struct Stop { n   int s, t; n   bool operator<(const Stop &ss) const { return t < ss.t; } n } bus[MAXN]; n int n, m, nn, dp[MAXN], sum[MAXN]; n int main() { n   while (scanf( %d%d , &n, &m) != EOF) { n     map<int, int> stops; n     stops[0] = stops[n] = 0; n     for (int i = 0; i < m; ++i) { n       scanf( %d%d , &bus[i].s, &bus[i].t); n       stops[bus[i].s] = stops[bus[i].t] = 0; n     } n     nn = 0; n     for (map<int, int>::iterator it = stops.begin(); it != stops.end(); ++it) n       it->second = ++nn; n     sort(bus, bus + m); n     int bi = 0; n     dp[1] = sum[1] = 1; n     for (int i = 2; i <= nn; ++i) { n       dp[i] = 0; n       for (; bi < m && stops[bus[bi].t] == i; ++bi) { n         int ms = stops[bus[bi].s]; n         int mt = stops[bus[bi].t]; n         dp[i] = ((long long)dp[i] + sum[mt - 1] - sum[ms - 1] + MODD) % MODD; n       } n       sum[i] = ((long long)sum[i - 1] + dp[i]) % MODD; n     } n     printf( %d n , dp[nn]); n   } n   return 0; n } n


#include <bits/stdc++.h> n const int N = 1e6; n const long long mod = 998244353; n const int INF = 0x7fffffff; n const int inf = 0x3f3f3f3f; n using namespace std; n void exgcd(long long a, long long b, long long &x, long long &y) { n   if (b == 0) { n     x = 1; n     y = 0; n     return; n   } n   exgcd(b, a % b, y, x); n   y -= a / b * x; n } n long long getlnverse(long long x, long long y) { n   long long res, tmp; n   exgcd(x, y, res, tmp); n   return (res + y) % y; n } n long long a[N + 5]; n long long cal(long long n, long long m) { n   long long tmp1 = getlnverse(a[m], mod); n   tmp1 = a[n] * tmp1 % mod; n   long long tmp2 = getlnverse(a[n - m], mod); n   return tmp1 * tmp2 % mod; n } n long long qpow(long long a, long long b) { n   long long sum = 1; n   while (b) { n     if (b & 1) { n       sum = (sum * a) % mod; n       b--; n     } n     b /= 2; n     a = a * a % mod; n   } n   return sum; n } n int main() { n   std::ios::sync_with_stdio(false); n   long long n, ans2 = 0; n   cin >> n; n   a[0] = 1; n   for (int i = 1; i <= n; i++) { n     a[i] = i * a[i - 1] % mod; n   } n   for (long long i = 1; i <= n; i++) { n     ans2 += n         (((cal(n, i) * qpow(-1, i)) % mod) * n          (qpow((qpow(3, n - i) - 1), n) + mod - qpow(3, n * (n - i))) % mod) % n         mod; n     ans2 = ans2 % mod; n   } n   long long ans1 = n       (2 * (qpow(3, n * n) - qpow((mod + qpow(3, n) - 3) % mod, n) + mod)) % n       mod; n   cout << (3 * mod + ans1 - (3 * ans2)) % mod << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, top, tot, cnt; n int fir[202020], nxt[202020 << 1], to[202020 << 1]; n int dfn[202020], fa[202020], st[202020]; n struct evol { n   int v, nm; n } l1[202020], l2[202020]; n void add(int x, int y) { n   nxt[++cnt] = fir[x]; n   fir[x] = cnt; n   to[cnt] = y; n } n void dfs(int x) { n   dfn[x] = ++tot; n   l1[x] = l2[x] = (evol){tot, x}; n   for (int i = fir[x]; i; i = nxt[i]) n     if (to[i] != fa[x]) { n       if (!dfn[to[i]]) { n         fa[to[i]] = x; n         dfs(to[i]); n         if (l1[to[i]].v < l1[x].v) { n           if (l2[to[i]].v < l1[x].v) n             l1[x] = l1[to[i]], l2[x] = l2[to[i]]; n           else n             l2[x] = l1[x], l1[x] = l1[to[i]]; n         } else if (l1[to[i]].v < l2[x].v) n           l2[x] = l1[to[i]]; n       } else if (dfn[to[i]] < l1[x].v) n         l2[x] = l1[x], l1[x] = (evol){dfn[to[i]], x}; n       else if (dfn[to[i]] < l2[x].v) n         l2[x] = (evol){dfn[to[i]], x}; n     } n   if (l2[x].v < dfn[x]) { n     puts( YES ); n     int ed = l2[x].v; n     top = 0; n     for (int i = x;; i = fa[i]) { n       st[++top] = i; n       if (dfn[i] == ed) { n         ed = i; n         break; n       } n     } n     printf( %d  , top); n     for (int i = 1; i <= top; i++) printf( %d  , st[i]); n     puts(  ); n     st[top = 1] = ed; n     for (int i = l2[x].nm; st[top] != x; i = fa[i]) st[++top] = i; n     printf( %d  , top); n     while (top) printf( %d  , st[top--]); n     puts(  ); n     dfn[0] = tot + 1; n     for (int i = ed; dfn[st[top]] > l1[x].v; i = fa[i]) st[++top] = i; n     for (int i = l1[x].nm; st[top] != x; i = fa[i]) st[++top] = i; n     printf( %d  , top); n     while (top) printf( %d  , st[top--]); n     puts(  ); n     exit(0); n   } n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= m; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     add(x, y); n     add(y, x); n   } n   for (int i = 1; i <= n; i++) n     if (!dfn[i]) dfs(i); n   puts( NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e6 + 10; n int k, q; n long long f[10], dp[maxn]; n void update(long long v, long long w) { n   for (int i = 1000000; i >= w; i--) dp[i] = max(dp[i], dp[i - w] + v); n   return; n } n void solve(long long v, long long w) { n   int num = min((long long)k, 1000000 / w); n   for (int i = 0; (1 << i) <= num; i++) { n     int c = (1 << i); n     update(v * c, w * c); n     num -= c; n   } n   if (num) update(v * num, w * num); n   return; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   cin >> k; n   k = 3 * (k - 1); n   for (int i = 0; i <= 5; i++) cin >> f[i]; n   for (int i = 0; i <= 1000000; i++) { n     int cur = 0, x = i; n     while (x) { n       int low = x % 10; n       if (low % 3 == 0) { n         dp[i] += f[cur] * (low / 3); n       } n       x /= 10; n       ++cur; n     } n   } n   int sz = 1; n   for (int i = 0; i <= 5; i++) { n     solve(f[i], sz * 3); n     sz *= 10; n   } n   cin >> q; n   while (q--) { n     int n; n     cin >> n; n     cout << dp[n] <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 500050; n const long long inf = 1e18; n int a[maxn]; n vector<int> G[maxn]; n int f[maxn][20], root; n long long ans = 0; n void dfs(int x, int par) { n   f[x][0] = par; n   long long t = inf; n   if (par) t = a[x] + a[par]; n   for (int i = 1; f[x][i - 1]; ++i) { n     f[x][i] = f[f[x][i - 1]][i - 1]; n     if (f[x][i]) n       t = min(t, (1ll + i) * a[f[x][i]] + a[x]); n     else n       t = min(t, (1ll + i) * a[root] + a[x]); n   } n   if (par) ans += t; n   for (auto &v : G[x]) { n     if (v != par) dfs(v, x); n   } n } n int main() { n   int n; n   scanf( %d , &n); n   root = 1; n   for (int i = 1; i <= n; ++i) n     scanf( %d , a + i), a[i] < a[root] ? root = i : 0; n   for (int i = 0; i < n - 1; ++i) { n     int x, y; n     scanf( %d%d , &x, &y); n     G[x].push_back(y); n     G[y].push_back(x); n   } n   dfs(root, 0); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double PI = 3.14159265358979323843; n int BS(int arr[], int sz, int n) { n   int l = 0, r = sz - 1, mid; n   while (l <= r) { n     if (arr[mid] >= n) n       r = mid - 1; n     else n       l = mid + 1; n   } n   return l; n } n bool prime(long long x) { n   for (int i = 2; i <= sqrt(x); i++) { n     if (x % i == 0) { n       return false; n     } n   } n   return true; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   string s; n   cin >> s; n   int c = 0; n   if (s.size() == 1) { n     return puts( 0 ), 0; n   } n   while (true) { n     long long sum = 0; n     for (int i = 0; i < s.size(); i++) { n       sum += s[i] -  0 ; n     } n     s.clear(); n     if (sum / 10 == 0) { n       break; n     } n     c++; n     s = to_string(sum); n   } n   cout << ++c << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int sta[10][100005]; n int top[10]; n int tail[10]; n int a[100005][10]; n int ans[10]; n int main() { n   int n, m; n   int k; n   scanf( %d %d %d , &n, &m, &k); n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       scanf( %d , &a[i][j]); n     } n   } n   for (int i = 1; i <= m; i++) { n     top[i] = 1; n   } n   int ma = -1; n   int i = 1; n   for (int l = 1; l <= n; l++) { n     if (l > 1) { n       for (int j = 1; j <= m; j++) { n         if (top[j] <= tail[j] && sta[j][top[j]] < l) { n           top[j]++; n         } n       } n     } n     for (i = max(i, l); i <= n; i++) { n       int sum = 0; n       for (int j = 1; j <= m; j++) { n         sum += max(a[sta[j][top[j]]][j], a[i][j]); n       } n       if (sum > k) break; n       for (int j = 1; j <= m; j++) { n         while (tail[j] >= top[j] && a[sta[j][tail[j]]][j] < a[i][j]) { n           tail[j]--; n         } n         sta[j][++tail[j]] = i; n       } n       if (i - l + 1 > ma) { n         ma = i - l + 1; n         for (int j = 1; j <= m; j++) ans[j] = a[sta[j][top[j]]][j]; n       } n     } n   } n   for (int i = 1; i <= m; i++) printf( %d  , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int imax = INT32_MAX; n const int imin = INT32_MIN; n const long long int inf = 1e18; n const long long int MOD = 1e9 + 7; n const long long int range = 1e9 + 7; n vector<vector<int>> g; n template <class T> n T cal(T n) { n   return (n * 1ll * (n + 1)); n } n template <class T> n T mx(T begin, T end) { n   return max_element(begin, end); n } n template <class T> n T mn(T begin, T end) { n   return min_element(begin, end); n } n template <class T> n void getUnique(vector<T>& nums) { n   sort(nums.begin(), nums.end()); n   nums.erase(unique(nums.begin(), nums.end()), nums.end()); n } n template <class T> n void print(vector<T> nums) { n   for (T i : nums) cout << i <<    ; n   cout <<   n ; n } n template <class T> n void read(vector<T>& nums) { n   for (T& i : nums) cin >> i; n } n template <class T> n void alloc(vector<vector<T>*> nums, T n) { n   for (auto nums : nums) nums->resize(n + 1); n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   long long int t = 1; n   while (t--) { n     long long int n; n     cin >> n; n     vector<long long int> dp(n + 1, imax); n     dp[0] = 0; n     for (long long int i = 1; i <= n; ++i) { n       long long int num = i; n       while (num > 0) { n         dp[i] = min(dp[i], 1 + dp[i - num % 10]); n         num /= 10; n       } n     } n     cout << dp[n]; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int x, t, a, b, da, db; n   cin >> x >> t >> a >> b >> da >> db; n   for (int i = 0; i < t; i++) n     for (int h = 0; h <= 1; h++) n       for (int j = 0; j < t; j++) n         for (int s = 0; s <= 1; s++) n           if (((a - (da * i)) * h) + ((b - (db * j)) * s) == x) { n             cout <<  YES  << endl; n             return 0; n           } n   cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve(); n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int times = 1; n   cin >> times; n   for (int i = 0; i < times; i++) { n     solve(); n     cout <<   n ; n   } n   return 0; n } n long long a, b, c, d, e, f, g, h; n long long arr[500000 + 50]; n void solve() { n   cin >> a >> b >> c; n   for (int i = 1; i <= a; i++) cin >> arr[i]; n   sort(arr + 1, arr + a + 1); n   long long dp[a + 50][2]; n   for (int i = 1; i <= a; i++) dp[i][0] = dp[i][1] = 21474836477777; n   dp[0][0] = dp[0][1] = 0; n   for (int i = 1; i <= a; i++) { n     if (i > 0) { n       dp[i][0] = min(dp[i][0], dp[i - 1][0] + arr[i]); n       dp[i][0] = min(dp[i][0], dp[i - 1][1] + arr[i]); n     } n     if (i >= c) { n       dp[i][1] = min(dp[i][1], dp[i - c][0] + arr[i]); n       dp[i][1] = min(dp[i][1], dp[i - c][1] + arr[i]); n     } n   } n   int ans = 0; n   for (int i = 1; i <= a; i++) { n     if (dp[i][0] <= b || dp[i][1] <= b) ans = i; n   } n   cout << ans; n } n