#include <bits/stdc++.h> n using namespace std; n const int MAX_N = 1e6 + 5; n long long magic_const = 3e18; n const int MAX_L = 3; n class segment_tree { n   int n; n   vector<long long> val; n   vector<long long> pushes; n   int l, r; n   long long ans, adding_value; n   void push(int v, bool is_leaf) { n     val[v] += pushes[v]; n     if (!is_leaf) { n       pushes[v << 1] += pushes[v]; n       pushes[(v << 1) + 1] += pushes[v]; n     } n     pushes[v] = 0; n   } n   void update(int v) { n     int lp = v << 1; n     int rp = lp + 1; n     val[v] = min(val[lp] + pushes[lp], val[rp] + pushes[rp]); n   } n   void build(vector<long long>& a, int v, int lb, int rb) { n     if (lb == rb) { n       val[v] = a[lb]; n       return; n     } n     int midt = (lb + rb) / 2; n     int lv = v << 1; n     int rv = lv + 1; n     build(a, lv, lb, midt); n     build(a, rv, midt + 1, rb); n     update(v); n   } n   void count(int v, int lb, int rb) { n     push(v, lb == rb); n     if (lb >= l && rb <= r) { n       ans = min(ans, val[v]); n     } else { n       int midt = (lb + rb) / 2; n       int lv = v << 1; n       int rv = lv + 1; n       if (midt >= l && lb <= r) count(lv, lb, midt); n       if (midt + 1 <= r && rb >= l) count(rv, midt + 1, rb); n     } n   } n   void add(int v, int lb, int rb) { n     push(v, lb == rb); n     if (lb >= l && rb <= r) { n       pushes[v] += adding_value; n     } else { n       int midt = (lb + rb) / 2; n       int lv = v << 1; n       int rv = lv + 1; n       if (midt >= l && lb <= r) add(lv, lb, midt); n       if (midt + 1 <= r && rb >= l) add(rv, midt + 1, rb); n       update(v); n     } n   } n  n  public: n   segment_tree(vector<long long>& a) { n     n = a.size(); n     if (n < 10) { n       val.resize(n * 4, magic_const); n       pushes.resize(n * 4); n     } else { n       val.resize(n * 4, magic_const); n       pushes.resize(n * 4); n     } n     build(a, 1, 0, n - 1); n   } n   long long get_answer(int l, int r) { n     this->l = l; n     this->r = r; n     this->ans = magic_const; n     count(1, 0, n - 1); n     return this->ans; n   } n   void add_on_segment(int l, int r, long long value) { n     this->l = l; n     this->r = r; n     this->adding_value = value; n     add(1, 0, n - 1); n   } n }; n int n, m; n vector<vector<int>> graph; n vector<vector<int>> cost; n vector<int> pr; n vector<int> pr_cost; n vector<int> h; n vector<pair<pair<int, int>, pair<int, int>>> queries; n vector<int> tin; n vector<int> tout; n vector<int> leafs; n vector<long long> used; n int t = 0; n void dfs(int v, int new_h) { n   tin[v] = t++; n   h[v] = new_h; n   if (graph[v].size() <= 1) leafs.push_back(v); n   for (int i = 0; i < graph[v].size(); i++) { n     int new_v = graph[v][i]; n     if (h[new_v] == -1) { n       pr[new_v] = v; n       pr_cost[new_v] = cost[v][i]; n       dfs(new_v, new_h + 1); n     } n   } n   tout[v] = t; n } n void dfs_2(int v, long long cnt) { n   used[v] = cnt; n   for (int i = 0; i < graph[v].size(); i++) { n     int new_v = graph[v][i]; n     if (used[new_v] == -1) { n       dfs_2(new_v, cnt + cost[v][i]); n     } n   } n } n const bool cmp(const pair<pair<int, int>, pair<int, int>>& first, n                const pair<pair<int, int>, pair<int, int>>& second) { n   if (tin[first.first.first] != tin[second.first.first]) n     return tin[first.first.first] < tin[second.first.first]; n   return first.first.second < second.first.second; n } n void solve() { n   cin >> n >> m; n   graph.resize(n); n   cost.resize(n); n   pr.resize(n); n   pr_cost.resize(n); n   h.resize(n, -1); n   tin.resize(n); n   tout.resize(n); n   used.resize(n, -1); n   for (int i = 1; i < n; i++) { n     int a, b; n     cin >> a >> b; n     a--; n     graph[i].push_back(a); n     graph[a].push_back(i); n     cost[i].push_back(b); n     cost[a].push_back(b); n   } n   dfs(0, 0); n   for (int i = 0; i < m; i++) { n     int v, a, b; n     cin >> v >> a >> b; n     v--, a--, b--; n     queries.push_back({{v, i}, {a, b}}); n   } n   vector<long long> answer(m); n   sort(queries.begin(), queries.end(), cmp); n   dfs_2(queries[0].first.first, 0); n   int cur_v = queries[0].first.first; n   int target_v = -1; n   vector<long long> new_used(n, magic_const); n   for (auto& leaf : leafs) { n     new_used[leaf] = used[leaf]; n   } n   segment_tree st(new_used); n   auto up_cur = [&]() { n     st.add_on_segment(0, new_used.size() - 1, -pr_cost[cur_v]); n     st.add_on_segment(tin[cur_v], tout[cur_v] - 1, 2 * pr_cost[cur_v]); n     cur_v = pr[cur_v]; n   }; n   auto up_target = [&]() { n     st.add_on_segment(0, new_used.size() - 1, pr_cost[target_v]); n     st.add_on_segment(tin[target_v], tout[target_v] - 1, n                       -2 * pr_cost[target_v]); n     target_v = pr[target_v]; n   }; n   for (int i = 0; i < queries.size(); i++) { n     int l = queries[i].second.first; n     int r = queries[i].second.second; n     target_v = queries[i].first.first; n     int cur_i = queries[i].first.second; n     while (h[cur_v] > h[target_v]) up_cur(); n     while (h[target_v] > h[cur_v]) up_target(); n     while (cur_v != target_v) { n       up_cur(); n       up_target(); n     } n     answer[cur_i] = st.get_answer(l, r); n     cur_v = queries[i].first.first; n   } n   for (auto& i : answer) cout << i <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   srand(555); n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxx = 1e6 + 7; n const long long maxn = 1e6 + 7; n const long long mod = 998244353; n const long long maxs = 500000 + 7; n const long long maxm = 400000 + 7; n const long long maxk = 1008 + 7; n const long long maxw = 100 + 7; n const long long maxh = 100 + 7; n const double pi = 3.1415926; n const long long ten = 10; n const long long ooi = 1000000; n const long long ool = 1e18 + 7; n const long double eps = 1e-8; n const int apsz = 26; n int seq[maxn]; n int n; n void init() { n   cin >> n; n   for (long long i = (1); i <= (n); i++) { n     cin >> seq[i]; n   } n   sort(seq + 1, seq + 1 + n); n } n void solve() { n   int ans = 0; n   int i = 1; n   int nowd = 1; n   while (i <= n) { n     while (i <= n && seq[i] < nowd) i++; n     if (i == n + 1) break; n     ans++; n     nowd++, i++; n   } n   cout << ans << endl; n } n int main(void) { n   ios::sync_with_stdio(false); n   cout.tie(0); n   cin.tie(0); n   init(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n const int mn = 10100, mm = 5010000; n char st[mm], ans[mm]; n int f[mm], s[mm], L[mn], R[mn]; n int n, m, K, i, j, d, len, now; n void del(int p) { n   for (; p <= m; p += p & (-p)) --s[p]; n } n int query(int v) { n   int i = 1, l = m >> 1; n   for (; l; l >>= 1) n     if (s[i + l - 1] < v) v -= s[i + l - 1], i += l; n   return i; n } n int main() { n   scanf( %s , st + 1); n   scanf( %d , &K); n   for (m = 1; m < K; m <<= 1) n     ; n   scanf( %d , &n); n   for (i = 1; i <= n; ++i) scanf( %d%d , L + i, R + i); n   for (i = 1; i <= m; ++i) s[i] = i & (-i); n   for (i = n; i >= 1; --i) { n     if (R[i] >= s[m]) continue; n     len = R[i] - L[i] + 1, now = L[i] + 1; n     for (j = 1; j <= len; ++j) { n       if (R[i] >= s[m]) break; n       if (now > R[i]) now = L[i]; n       d = query(R[i] + 1), del(d); n       f[d] = query(now); n       now += 2; n     } n   } n   j = 1; n   for (i = 1; i <= K; ++i) ans[i] = f[i] ? ans[f[i]] : st[j++]; n   puts(ans + 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 7; n const int INF = 1e8; n void boost() { n   cin.tie(0); n   cout.tie(0); n   ios_base::sync_with_stdio(NULL); n } n long long n, a[N], b[N], sum, sum2, ans, dp[N], t; n string s; n int main() { n   boost(); n   cin >> t; n   for (int l = 1; l <= t; ++l) { n     cin >> n; n     sum = 0; n     ans = 0; n     for (int i = 1; i <= n; ++i) { n       cin >> a[i]; n     } n     for (int i = 1; i <= n; ++i) { n       sum += a[i]; n       if (sum <= 0) { n         cout <<  NO  <<   n ; n         ans = 1; n         break; n       } n     } n     sum = 0; n     for (int i = n; i >= 1; --i) { n       sum += a[i]; n       if (sum <= 0 && ans == 0) { n         cout <<  NO  <<   n ; n         ans = 1; n         break; n       } n     } n     if (!ans) { n       cout <<  YES  <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = 3.1415926535897932384626433; n int MOD = 0; n int pl(int v1, int v2) { n   int res = v1 + v2; n   if (res < 0) n     res += MOD; n   else if (res >= MOD) n     res -= MOD; n   return res; n } n int mul(const int v1, const int v2) { return (int)((1LL * v1 * v2) % MOD); } n const int N = 250007; n vector<int> fac(N); n void initFac() { n   fac[1] = 1; n   for (int i = 2; i < N; i++) { n     fac[i] = mul(fac[i - 1], i); n   } n } n void solve() { n   int n; n   cin >> n; n   ; n   cin >> MOD; n   if (n == 1) { n     cout << 1 << endl; n     return; n   } n   initFac(); n   int ans = mul(fac[n], n); n   ans = pl(ans, fac[n]); n   for (int len = 2; len < n; len++) { n     int lans = n - len + 1; n     lans = mul(lans, fac[len]); n     lans = mul(lans, n - len + 1); n     lans = mul(lans, fac[n - len]); n     ans = pl(ans, lans); n   } n   cout << ans << endl; n } n int main() { n   cin.tie(nullptr); n   std::ios::sync_with_stdio(false); n   solve(); n } n


#include <bits/stdc++.h> n #pragma warning(disable : 4996) n using namespace std; n vector<int> get_sb(int num) { n   vector<int> res; n   while (num) { n     res.push_back(num % 10); n     num /= 10; n   } n   return res; n } n string get_a(vector<int> kol, string sub) { n   string reserve =  9 ; n   ++reserve[0]; n   if (sub[0] ==  0 ) return reserve; n   string ss =   ; n   for (int i = 0; i < kol.size(); ++i) { n     for (int j = 0; j < kol[i]; ++j) ss += (char)(i +  0 ); n   } n   return sub + ss; n } n string get_b(vector<int> kol, string sub) { n   string res =   ; n   string reserve =  9 ; n   ++reserve[0]; n   int mc = -1; n   for (int i = 1; i < kol.size(); ++i) { n     if (0 != kol[i]) { n       mc = i; n       break; n     } n   } n   if (-1 == mc) return reserve; n   int cci = sub[0] -  0 ; n   res += (char)(mc +  0 ); n   --kol[mc]; n   for (int i = 0; i < cci; ++i) { n     for (int j = 0; j < kol[i]; ++j) res += (char)(i +  0 ); n   } n   res += sub; n   for (int i = cci; i < kol.size(); ++i) { n     for (int j = 0; j < kol[i]; ++j) res += (char)(i +  0 ); n   } n   return res; n } n string get_c(vector<int> kol, string sub) { n   string res =   ; n   string reserve =  9 ; n   ++reserve[0]; n   int mc = -1; n   for (int i = 1; i < kol.size(); ++i) { n     if (0 != kol[i]) { n       mc = i; n       break; n     } n   } n   if (-1 == mc) return reserve; n   int cci = sub[0] -  0 ; n   res += (char)(mc +  0 ); n   --kol[mc]; n   for (int i = 0; i <= cci; ++i) { n     for (int j = 0; j < kol[i]; ++j) res += (char)(i +  0 ); n   } n   res += sub; n   for (int i = cci + 1; i < kol.size(); ++i) { n     for (int j = 0; j < kol[i]; ++j) res += (char)(i +  0 ); n   } n   return res; n } n int main() { n   string num, sub; n   getline(cin, num); n   getline(cin, sub); n   if (num ==  01  || num ==  10 ) { n     printf( 0 ); n     return 0; n   } n   int len_all = (int)num.size(); n   bool flag = true; n   for (int i = 1; i < sub.size(); ++i) { n     if (sub[i - 1] < sub[i]) { n       flag = false; n       break; n     } n   } n   vector<int> kol(10); n   for (int i = 0; i < num.size(); ++i) ++kol[num[i] -  0 ]; n   for (int i = 0; i < sub.size(); ++i) --kol[sub[i] -  0 ]; n   for (int aa = 1; aa <= 1000000; ++aa) { n     bool flag = true; n     vector<int> vv = get_sb(aa); n     vector<int> kol1 = kol; n     for (int i = 0; i < vv.size(); ++i) { n       --kol1[vv[i]]; n       if (kol1[vv[i]] < 0) { n         flag = false; n         break; n       } n     } n     if (!flag) continue; n     int vsz = (int)vv.size(); n     if (len_all - vsz != aa) continue; n     string a = get_a(kol1, sub); n     string b = get_b(kol1, sub); n     string c = get_c(kol1, sub); n     string ans = min(a, b); n     ans = min(ans, c); n     cout << ans; n     break; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   int n, k; n   cin >> n >> k; n   int t[n]; n   for (int i = 0; i < n; i++) cin >> t[i]; n   sort(t, t + n); n   if (k == n) n     cout << t[n - 1] << endl; n   else if (k == 0) { n     if (t[0] - 1 == 0) n       cout << -1 << endl; n     else n       cout << t[0] - 1 << endl; n   } else if (k == 1) { n     if (t[0] == t[1]) n       cout << -1 << endl; n     else n       cout << t[0] << endl; n   } else if (t[k - 1] == t[k]) n     cout << -1 << endl; n   else n     cout << t[k - 1] << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int absol(int x); n int main() { n   int n, C, left, right, top, bot; n   char c; n   int xy[10][2] = {{1, 3}, {0, 0}, {1, 0}, {2, 0}, {0, 1}, n                    {1, 1}, {2, 1}, {0, 2}, {1, 2}, {2, 2}}; n   cin >> n; n   cin >> c; n   C = c; n   C -= 48; n   left = C; n   right = C; n   top = C; n   bot = C; n   for (int i = 1; i < n; i++) { n     cin >> c; n     C = c; n     C -= 48; n     if (xy[C][0] < xy[left][0]) n       left = C; n     else if (xy[C][0] > xy[right][0]) n       right = C; n     if (xy[C][1] < xy[top][1]) n       top = C; n     else if (xy[C][1] > xy[bot][1] || (bot != 0 && (C == 7 || C == 9))) n       bot = C; n   } n   if ((absol(xy[bot][1] - xy[top][1]) == 2 && n        absol(xy[right][0] - xy[left][0]) == 2 && bot != 8 && bot != 0) || n       absol(xy[bot][1] - xy[top][1]) == 3) { n     cout <<  YES  << endl; n   } else n     cout <<  NO  << endl; n   return 0; n } n int absol(int x) { n   if (x < 0) n     return x * -1; n   else n     return x; n } n


#include <bits/stdc++.h> n int const MAX = 1e5 + 5; n using namespace std; n bool check(long long m, long long ai, long long di, long long hm, long long am, n            long long dm) { n   long long retval; n   if (hm % (ai - dm) == 0) n     retval = m - (((hm) / (ai - dm))) * (am - (di)); n   else n     retval = m - (1 + ((hm) / (ai - dm))) * (am - (di)); n   if (retval > 0) return true; n   return false; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long hy, hm, ay, am, dy, dm; n   cin >> hy >> ay >> dy >> hm >> am >> dm; n   long long h, a, d; n   cin >> h >> a >> d; n   long long ans = LLONG_MAX; n   for (long long i = 0; i < 1000; i++) { n     for (long long j = 0; j < 1000; j++) { n       long long ai = ay + i; n       long long di = dy + j; n       long long c = a * i + d * j; n       if (ai <= dm) continue; n       long long l = 0, r = 10000; n       while (r - l > 1) { n         long long mid = (l + r) >> 1; n         if (check(hy + mid, ai, di, hm, am, dm)) n           r = mid; n         else n           l = mid; n       } n       if (!check(hy + l, ai, di, hm, am, dm)) l++; n       if (l < 0) l = 0; n       c += l * h; n       ans = min(ans, c); n     } n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n void solve(long long int input) { n   long long int n; n   cin >> n; n   double a[n], b[n]; n   for (long long int i = 0; i < n; i++) cin >> a[i]; n   for (long long int i = 0; i < n; i++) cin >> b[i]; n   map<long double, long long int> mp; n   long long int ans = 0; n   long long int j = 0; n   for (long long int i = 0; i < n; i++) { n     if (a[i] == 0 && b[i] == 0) { n       j++; n       continue; n     } n     if (a[i] == 0) continue; n     mp[(b[i] / (a[i]))]++; n     ans = max(ans, mp[(b[i] / (a[i]))]); n   } n   cout << ans + j <<   n ; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long int x = 1; n   for (long long int i = 1; i <= x; i++) solve(i); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200005; n int n, a, b, k, pr[N], seq[N]; n char s[200005]; n int main() { n   scanf( %d%d%d%d , &n, &a, &b, &k); n   scanf( %s , s + 1); n   int len = strlen(s + 1), lst = 0, cur = 0; n   for (int i = 1; i <= len; ++i) { n     if (s[i] ==  1 ) { n       cur += (i - lst - 1) / b; n       lst = i; n     } n   } n   if (s[len] !=  1 ) cur += (len - lst) / b; n   int ans = 0, now = 0, da = 0; n   for (int i = 1; i <= len; ++i) { n     now++; n     if (s[i] ==  1 ) now = 0; n     if (now == b) { n       now = 0; n       ans++; n       seq[ans] = i; n       if (cur - ans < a) break; n     } n   } n   printf( %d n , ans); n   for (int i = 1; i <= ans; ++i) printf( %d  , seq[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e6 + 5; n double a[maxn], st[maxn]; n int len[maxn]; n int main() { n   int n, x; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &x); n     a[i] = x; n   } n   int p = 0; n   for (int i = 1; i <= n; i++) { n     st[++p] = a[i]; n     len[p] = 1; n     while (p > 1 && st[p] < st[p - 1]) { n       st[p - 1] = n           (st[p - 1] * len[p - 1] + st[p] * len[p]) / (len[p - 1] + len[p]); n       len[p - 1] += len[p]; n       p--; n     } n   } n   for (int i = 1; i <= p; i++) { n     for (int j = 1; j <= len[i]; j++) { n       printf( %.10f n , st[i]); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int32_t main() { n   long long a, b; n   cin >> a >> b; n   double d = ((pow(b, 2)) - (pow(a, 2))) / (2 * a); n   cout << fixed << setprecision(12) << abs(d); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline T SQR(T x) { n   return x * x; n } n template <class T> n inline string TOSTR(const T &x) { n   stringstream ss; n   ss << x; n   return ss.str(); n } n template <class T> n void UPDATE_MIN(T &x, T y) { n   if (y < x) { n     x = y; n   } n } n template <class T> n void UPDATE_MAX(T &x, T y) { n   if (x < y) { n     x = y; n   } n } n int gcd(int a, int b) { return a ? gcd(b % a, a) : b; } n long long gcd(long long a, long long b) { return a ? gcd(b % a, a) : b; } n long long powmod(long long a, long long p, long long m) { n   long long r = 1; n   while (p) { n     if (p & 1) r = r * a % m; n     p >>= 1; n     a = a * a % m; n   } n   return r; n } n template <typename K, typename V> n V GetWithDef(const std::unordered_map<K, V> &m, const K &key, const V &defval) { n   auto it = m.find(key); n   if (it == m.end()) { n     return defval; n   } else { n     return it->second; n   } n } n template <typename K, typename V> n void SetDef(std::unordered_map<K, V> &m, const K &key, const V &defval) { n   auto it = m.find(key); n   if (it == m.end()) m[key] = defval; n } n const int MOD = 1000 * 1000 * 1000 + 7; n const double PI = 3.1415926535897932384626433832795l; n struct Event { n   int id; n   long long x; n   char type; n   bool operator<(const Event &other) const { n     if (x < other.x) return true; n     if (x > other.x) return false; n     return type < other.type; n   } n }; n Event events[2 * 200123]; n int saved_max[200123]; n int evtop = 0; n int N; n int main() { n   ios_base::sync_with_stdio(0); n   cin >> N; n   for (int(i) = 0; (i) < (int)(N); (i)++) { n     int x, w; n     cin >> x >> w; n     events[evtop++] = {i, x - w, 1}; n     events[evtop++] = {i, x + w, 0}; n   } n   sort(events, events + evtop); n   int max_cur_closed = 0; n   for (int(i) = 0; (i) < (int)(evtop); (i)++) { n     Event &e = events[i]; n     if (e.type == 1) { n       saved_max[e.id] = max_cur_closed; n     } else if (e.type == 0) { n       UPDATE_MAX(max_cur_closed, saved_max[e.id] + 1); n     } else n       assert(0); n   } n   cout << max_cur_closed << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #define f first n #define s second n #define ll long long n #define ld long double n #define pii pair <int,int> n #define pll pair <ll,ll> n #define pil pair <int,ll> n #define pli pair <ll,int> n #define pss pair <short,short> n #define ull unsigned long long n #define pdd pair <ld,ld>  n #define pb push_back n #define mp make_pair n #define puu pair<ull,ull> n #define pvv pair<vector<int>, vector<int> > n #define _bp __builtin_popcount n #define ptt pair <pnode,pnode> n #define all(v) v.begin(),v.end() n #define en   n  n void file() n { n     freopen( input.txt , r ,stdin); n     freopen( f.txt , w ,stdout); n } n void boos() n { n  ios_base :: sync_with_stdio(0); n  cin.tie(0); n  cout.tie(0); n } n //const long double pi = acos(-1); n const ll INF = (ll)1e9 + 7ll; n const ll INF1 = 998244353; n const ll INF2 = (ll)1e9 + 9ll; n const ll LLINF = (ll)1e18; n const ll INTmx = (ll)1e9; n const ld EPS = (ld)1e-15; n const int N = 3e5 + 100; n string a[N]; n ll st[N],dp[N],dp1[N],dp2[N]; n int main() n { n  boos(); n  //file(); n  int n,m; n  cin >> n >> m; n  int k = 0; n  for(int i = 1;i <= n;i++) n  { n   cin >> a[i]; n   a[i] =  0  + a[i]; n   for(int j = 1;j <= m;j++) n   { n    if(a[i][j] ==  o )k++; n   } n  } n  st[0] = 1; n  dp1[0] = 1; n  for(int i = 1;i <= k;i++) n  { n   st[i] = (st[i - 1] * 2ll) % INF1; n  } n  dp2[2] = 1; n  dp1[2] = 1; n  for(ll i = 3;i <= max(n,m);i++) n  { n   dp[i] = (dp[i - 2] + (st[i - 3] * (i - 2)) % INF1) % INF1; n   if(i % 2 == 0)dp1[i] = (dp1[i - 1] * 2ll + 1ll) % INF1; n   else dp1[i] = (dp1[i - 1] * 2ll) % INF1; n   dp2[i] = dp[i]; n   dp2[i] = (dp2[i] + dp1[i]) % INF1; n  } n  //cout << dp2[3] <<     << k <<     << st[k - 2] << en; n  ll ans = 0; n  for(int i = 1;i <= n;i++) n  { n   int L = 1; n   for(int j = 1;j <= m;j++) n   { n    if(a[i][j] ==  * ) n    { n     if(L != j) n     { n      ans += (dp2[j - L] * st[k - (j - L)]) % INF1; n      //ans += (st[j - L - 2] * st[k - (j - L)]) % INF1; n      //cout << j - L <<    ; n      ans %= INF1; n     } n     L = j + 1; n    } n   } n   if(L != m + 1) n   { n    ans += (dp2[m - L + 1] * st[k - (m - L + 1)]) % INF1; n    //ans += (st[m - L - 1] * st[k - (m - L + 1)]) % INF1; n    //cout << m - L + 1 <<    ; n    ans %= INF1; n   } n  } n  //cout << dp[4] << en; n  for(int j = 1;j <= m;j++) n  { n   int L = 1; n   for(int i = 1;i <= n;i++) n   { n    if(a[i][j] ==  * ) n    { n     if(L != i) n     { n      ans += (dp2[i - L] * st[k - (i - L)]) % INF1; n      //ans += (st[i - L - 2] * st[k - (i - L)]) % INF1; n      //cout << i - L <<    ; n      ans %= INF1; n     } n     L = i + 1; n    } n   } n   if(L != n + 1) n   { n    ans += (dp2[n - L + 1] * st[k - (n - L + 1)]) % INF1; n    //ans += (st[n - L - 1] * st[k - (n - L + 1)]) % INF1; n    //cout << n- L + 1 <<    ; n    ans %= INF1; n   } n  } n  cout << ans; n }


#include <bits/stdc++.h> n using namespace std; n const int MX = 300005; n long long n, m, k; n struct node { n   int id; n   long long pos; n   long long dir; n } no[MX]; n int now[MX]; n int ans[MX]; n bool cmp(node a, node b) { n   if (a.pos < b.pos) return true; n   return false; n } n int main() { n   char ss[5]; n   scanf( %I64d%I64d%I64d , &n, &m, &k); n   long long p = 0; n   for (int i = 0; i < n; i++) { n     scanf( %I64d%s , &no[i].pos, ss); n     --no[i].pos; n     no[i].id = i; n     no[i].dir = 1; n     if (ss[0] ==  L ) no[i].dir = -1; n     if (ss[0] ==  R ) n       p += (k + no[i].pos) / m; n     else n       p -= (k + m - 1 - no[i].pos) / m; n     now[i] = ((no[i].pos + no[i].dir * k) % m + m) % m; n   } n   p = p % n; n   sort(no, no + n, cmp); n   sort(now, now + n); n   for (int i = 0; i < n; i++) { n     ans[no[((i - p) % n + n) % n].id] = now[i] + 1; n   } n   for (int i = 0; i < n; i++) { n     printf( %d%c , ans[i], i == n - 1 ?   n  :    ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n const int MAXN = 200010; n int B[MAXN], C[MAXN], a[MAXN]; n long long b[MAXN], c[MAXN]; n struct Node { n   int b, c, id; n   bool operator<(const Node ano) const { return b + c < ano.b + ano.c; } n } opt[MAXN]; n int cnt[MAXN]; n bool check(int x) { n   int now = x; n   for (int i = 1; i <= n; i++) { n     a[opt[i].id] = now; n     now += ((opt[i + 1].b + opt[i + 1].c - opt[i].b - opt[i].c) / n); n   } n   memset(cnt, 0, sizeof cnt); n   memset(b, 0, sizeof b); n   memset(c, 0, sizeof c); n   for (int i = 1; i <= n; i++) { n     for (int j = 0; j < 30; j++) { n       if ((a[i] >> j) & 1) cnt[j]++; n     } n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 0; j < 30; j++) { n       if ((a[i] >> j) & 1) { n         b[i] += 1LL * cnt[j] * (1 << j); n         c[i] += 1LL * n * (1 << j); n       } else { n         c[i] += 1LL * cnt[j] * (1 << j); n       } n     } n   } n   return b[1] + c[1] >= B[1] + C[1]; n } n bool check2(int x) { n   int now = x; n   for (int i = 1; i <= n; i++) { n     a[opt[i].id] = now; n     now += ((opt[i + 1].b + opt[i + 1].c - opt[i].b - opt[i].c) / n); n   } n   memset(cnt, 0, sizeof cnt); n   memset(b, 0, sizeof b); n   memset(c, 0, sizeof c); n   for (int i = 1; i <= n; i++) { n     for (int j = 0; j < 30; j++) { n       if ((a[i] >> j) & 1) cnt[j]++; n     } n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 0; j < 30; j++) { n       if ((a[i] >> j) & 1) { n         b[i] += 1LL * cnt[j] * (1 << j); n         c[i] += 1LL * n * (1 << j); n       } else { n         c[i] += 1LL * cnt[j] * (1 << j); n       } n     } n   } n   for (int i = 1; i <= n; i++) { n     if ((b[i] != B[i]) || (c[i] != C[i])) return 0; n   } n   return 1; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) n     scanf( %d , &opt[i].b), B[i] = opt[i].b, opt[i].id = i; n   for (int i = 1; i <= n; i++) scanf( %d , &opt[i].c), C[i] = opt[i].c; n   sort(opt + 1, opt + n + 1); n   for (int i = 2; i <= n; i++) { n     if ((opt[i].b + opt[i].c - opt[i - 1].b - opt[i - 1].c) % n != 0) { n       puts( -1 ); n       return 0; n     } n   } n   int l = 0, r = 1000000000; n   while (l < r) { n     int mid = (l + r) >> 1; n     if (check(mid)) n       r = mid; n     else n       l = mid + 1; n   } n   if (check2(l)) { n     for (int i = 1; i <= n; i++) printf( %d%c , a[i], (i == n ? 10 :    )); n   } else n     puts( -1 ); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = (1 << 18); n const double PI = acos(-1.0); n char s[N]; n int lA, lB; n int a[N], b[N]; n int inB[110][N]; n int S, B; n struct cpx { n   double x, y; n   cpx(double p = 0, double q = 0) : x(p), y(q) {} n   cpx operator+(cpx &o) { return cpx(x + o.x, y + o.y); } n   cpx operator-(cpx &o) { return cpx(x - o.x, y - o.y); } n   cpx operator*(cpx &o) { return cpx(x * o.x - y * o.y, x * o.y + y * o.x); } n } u[N], v[N]; n void fft(cpx *a, int n, int f) { n   int i, j, k; n   for (i = j = 0; i < n; ++i) { n     if (i > j) swap(a[i], a[j]); n     for (k = n >> 1; (j ^= k) < k; k >>= 1) n       ; n   } n   for (i = 1; i < n; i <<= 1) { n     cpx wn(cos(PI / i), f * sin(PI / i)); n     for (j = 0; j < n; j += i << 1) { n       cpx w(1, 0); n       for (k = 0; k < i; ++k, w = w * wn) { n         cpx x = a[j + k], y = w * a[i + j + k]; n         a[j + k] = x + y; n         a[i + j + k] = x - y; n       } n     } n   } n   if (f == -1) n     for (i = 0; i < n; ++i) a[i].x /= n, a[i].y /= n; n } n void work(int *a, int A, int id) { n   int n = 1; n   while (n <= A + lB) n <<= 1; n   for (int i = 0; i < A; ++i) u[i] = cpx(a[i], 0); n   for (int i = A; i < n; ++i) u[i] = cpx(0, 0); n   for (int i = 0; i < A - 1 - i; ++i) swap(u[i], u[A - 1 - i]); n   for (int i = 0; i < lB; ++i) v[i] = cpx(b[i], 0); n   for (int i = lB; i < n; ++i) v[i] = cpx(0, 0); n   fft(u, n, 1), fft(v, n, 1); n   for (int i = 0; i < n; ++i) u[i] = u[i] * v[i]; n   fft(u, n, -1); n   for (int i = 0; i < lB; ++i) inB[id][i] = (A - round(u[i].x)) / 2; n } n int main() { n   scanf( %s , s); n   lA = strlen(s); n   for (int i = 0; i < lA; ++i) a[i] = (s[i] ==  1  ? 1 : -1); n   scanf( %s , s); n   lB = strlen(s); n   for (int i = 0; i < lB; ++i) b[i] = (s[i] ==  1  ? 1 : -1); n   S = sqrt(lA * log2(lA)); n   B = ceil(1. * lA / S); n   for (int i = 0; i < B; ++i) { n     int l = i * S, r = min(lA, l + S); n     work(a + l, r - l, i); n   } n   int q; n   scanf( %d , &q); n   while (q--) { n     int l, r, len, res = 0; n     scanf( %d %d %d , &l, &r, &len); n     int p = l / S, q = (l + len - 1) / S; n     if (p == q) { n       for (int i = l; i < l + len; ++i) res += (a[i] != b[i - l + r]); n     } else { n       for (int i = l; i < (p + 1) * S; ++i) res += (a[i] != b[i - l + r]); n       for (int i = p + 1; i < q; ++i) res += (inB[i][(i + 1) * S - 1 - l + r]); n       for (int i = q * S; i < l + len; ++i) res += (a[i] != b[i - l + r]); n     } n     printf( %d n , res); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k, a, b; n   cin >> n >> k >> a >> b; n   bool impossible = false; n   string s; n   int curGroup = (a > b) ? 0 : 1; n   int groupSize = 0; n   for (int i = 0; i < n; ++i) { n     if (curGroup) { n       if (!b--) { n         impossible = true; n         break; n       } n       s.push_back( B ); n     } else { n       if (!a--) { n         impossible = true; n         break; n       } n       s.push_back( G ); n     } n     if (++groupSize == k || ((curGroup && a > b) || (!curGroup && b > a))) { n       curGroup = 1 - curGroup; n       groupSize = 0; n     } n   } n   cout << (impossible ?  NO  : s) << endl; n   return 0; n } n


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


#include <bits/stdc++.h> n #pragma GCC target( sse4,avx ) n struct Solution { n   int n; n   std::vector<std::vector<int>> graph; n   std::vector<std::vector<int>> matrix; n   int counter; n   std::vector<int> dirtyFlags; n   std::vector<double> maxProbs; n   double checkFirst(int a) { n     double res = 0; n     for (int dist = 0; dist < n; dist++) { n       std::vector<int> group; n       for (int i = 0; i < n; i++) { n         if (matrix[a][i] == dist) { n           group.push_back(i); n         } n       } n       if (group.size() == 0) continue; n       double caseProb = group.size() / (double)n; n       double c = 1.0 / group.size(); n       double ans = c; n       std::vector<double> t(n); n       for (int v : group) { n         for (int nv : graph[v]) { n           t[nv] += c / graph[v].size(); n         } n       } n       std::vector<int> groupT; n       std::vector<double> groupTD; n       for (int i = 0; i < n; i++) { n         if (t[i] > 1e-20) { n           groupT.push_back(i); n           groupTD.push_back(t[i]); n         } n       } n       for (int b = 0; b < n; b++) { n         counter++; n         double cur = 0; n         for (size_t i = 0; i < groupT.size(); i++) { n           int v = groupT[i]; n           double tv = groupTD[i]; n           int d = matrix[b][v]; n           if (dirtyFlags[d] == counter) { n             if (tv > maxProbs[d]) { n               cur += tv - maxProbs[d]; n               maxProbs[d] = tv; n             } n           } else { n             dirtyFlags[d] = counter; n             maxProbs[d] = tv; n             cur += tv; n           } n         } n         ans = std::max(ans, cur); n       } n       res += caseProb * ans; n     } n     return res; n   } n   void run(std::istream& in, std::ostream& out) { n     int m; n     in >> n >> m; n     graph.clear(); n     graph.resize(n); n     matrix.clear(); n     matrix.resize(n, std::vector<int>(n, 1000000)); n     for (int i = 0; i < n; i++) { n       matrix[i][i] = 0; n     } n     for (int i = 0; i < m; i++) { n       int a, b; n       in >> a >> b; n       a--; n       b--; n       graph[a].push_back(b); n       graph[b].push_back(a); n       matrix[a][b] = 1; n       matrix[b][a] = 1; n     } n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < n; j++) { n         for (int k = 0; k < n; k++) { n           matrix[j][k] = std::min(matrix[j][k], matrix[j][i] + matrix[i][k]); n         } n       } n     } n     counter = 0; n     dirtyFlags.resize(n); n     maxProbs.resize(n); n     double ans = 0; n     for (int a = 0; a < n; a++) { n       ans = std::max(ans, checkFirst(a)); n     } n     out.precision(12); n     out << ans << std::endl; n   } n }; n int main() { n   std::cin.sync_with_stdio(false); n   std::cin.tie(nullptr); n   Solution().run(std::cin, std::cout); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n set<string> f; n int main() { n   int n; n   cin >> n; n   int ans = 0; n   for (int i = 0; i < n; ++i) { n     string s =   ; n     while (1) { n       char c; n       cin >> c; n       if (isdigit(c)) s += c; n       if (((int)(s).size()) == 4) break; n     } n     swap(s[2], s[3]); n     string t = s; n     for (int i = 0; i < 4; ++i) { n       char c = s[0]; n       for (int j = 0; j < 3; ++j) s[j] = s[j + 1]; n       s[3] = c; n       if (s < t) t = s; n     } n     if (!f.count(t)) ++ans; n     f.insert(t); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ,  unroll-all-loops ) n #pragma GCC target( sse4.2 ) n using namespace std; n const long long kk = 1000; n const long long ml = kk * kk; n const long long mod = ml * kk + 7; n const long long inf = ml * ml * ml + 7; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n long long n; n bool viv = false; n string a, b; n int in[20], out[20]; n vector<vector<int>> comp; n void DFS(int v, vector<bool> &used) { n   used[v] = true; n   comp.back().push_back(v); n   for (int s = 0; s < 20; s++) n     if ((out[v] & (1 << s)) || (in[v] & (1 << s))) n       if (!used[s]) DFS(s, used); n } n void get_comp() { n   vector<bool> used(20, false); n   for (int i = 0; i < 20; i++) n     if (!used[i]) { n       comp.push_back({}); n       DFS(i, used); n     } n } n int cnt(int val) { n   int res = 0; n   while (val) res += (val & 1), val >>= 1; n   return res; n } n void solve() { n   cin >> n; n   cin >> a >> b; n   comp.clear(); n   for (int i = 0; i < 20; i++) in[i] = out[i] = 0; n   for (int i = 0; i < n; i++) n     if (a[i] != b[i]) { n       out[a[i] -  a ] |= (1 << (b[i] -  a )); n       in[b[i] -  a ] |= (1 << (a[i] -  a )); n     } n   bool dag[1 << 20]; n   for (int i = 0; i < (1 << 20); i++) dag[i] = false; n   dag[0] = true; n   for (int mask = 0; mask < (1 << 20); mask++) { n     for (int i = 0; i < 20; i++) n       if ((mask >> i) & 1) { n         if (((mask ^ (1 << i)) & (in[i])) == 0) n           dag[mask] |= dag[mask ^ (1 << i)]; n       } n   } n   get_comp(); n   int ans = 0; n   for (auto c : comp) { n     if (viv) { n       for (auto i : c) cout << i <<    ; n       cout << endl; n     } n     int sz = c.size(); n     int mx_dag = 0; n     for (int smask = 0; smask < (1 << sz); smask++) { n       int mask = 0; n       for (int i = 0; i < sz; i++) n         if ((smask >> i) & 1) mask ^= (1 << c[i]); n       if (dag[mask]) mx_dag = max(mx_dag, cnt(mask)); n     } n     ans += 2 * sz - mx_dag - 1; n   } n   cout << ans <<   n ; n   if (viv) cout <<   t____ t____ t  << endl; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long t = 1; n   cin >> t; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int n, m, k, w; n vector<vector<vector<char>>> mp; n struct dsu { n   vector<long long int> p, sz; n   dsu(long long int n) { n     p.resize(n); n     sz.resize(n, 1); n     for (long long int i = 0; i < n; i++) p[i] = i; n   } n   long long int getRoot(long long int v) { n     if (p[v] == v) n       return v; n     else n       return p[v] = getRoot(p[v]); n   } n   void unite(long long int a, long long int b) { n     a = getRoot(a); n     b = getRoot(b); n     if (a != b) { n       if (sz[a] < sz[b]) swap(a, b); n       p[b] = a; n       sz[a] += sz[b]; n     } n   } n   bool inOne(long long int a, long long int b) { n     a = getRoot(a); n     b = getRoot(b); n     return a == b; n   } n }; n long long int getR(long long int a, long long int b) { n   long long int ans = 0; n   for (long long int i = 0; i < n; i++) { n     for (long long int j = 0; j < m; j++) { n       ans += (mp[a][i][j] != mp[b][i][j]); n     } n   } n   return ans; n } n struct edge { n   long long int a, b, w; n   edge(long long int a = 0, long long int b = 0, long long int w = 0) { n     this->a = a; n     this->b = b; n     this->w = w; n   } n }; n bool cmp(const edge& a, const edge& b) { return a.w < b.w; } n vector<vector<long long int>> g; n vector<pair<long long int, long long int>> path; n void dfs(long long int v = 0, long long int p = -1) { n   if (p > -1) path.push_back({v, p}); n   for (auto to : g[v]) { n     if (to == p) continue; n     dfs(to, v); n   } n } n signed main() { n   cin >> n >> m >> k >> w; n   mp.resize(k + 1, vector<vector<char>>(n, vector<char>(m))); n   g.resize(k + 1); n   for (long long int i = 1; i <= k; i++) { n     for (long long int j = 0; j < n; j++) { n       for (long long int w = 0; w < m; w++) cin >> mp[i][j][w]; n     } n   } n   vector<edge> ed; n   for (long long int i = 1; i <= k; i++) ed.push_back(edge(0, i, n * m)); n   for (long long int i = 1; i <= k; i++) { n     for (long long int j = i + 1; j <= k; j++) n       ed.push_back(edge(i, j, getR(i, j) * w)); n   } n   sort(ed.begin(), ed.end(), cmp); n   dsu s(k + 1); n   long long int ans = 0; n   for (auto e : ed) { n     long long int a = e.a, b = e.b, w = e.w; n     if (s.inOne(a, b)) continue; n     ans += w; n     s.unite(a, b); n     g[a].push_back(b); n     g[b].push_back(a); n   } n   dfs(); n   cout << ans <<   n ; n   for (auto i : path) cout << i.first <<     << i.second <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 5000 + 10, MOD = 1000000007; n vector<pair<int, int> > lis[MAXN * 2]; n pair<int, int> p[MAXN]; n int n, f[MAXN], cnt; n bool b[MAXN]; n int dis(const pair<int, int> &a, const pair<int, int> &b) { n   return abs(a.first - b.first) + abs(a.second - b.second); n } n int getf(int first) { n   if (first == f[first]) return first; n   int fx = f[first]; n   f[first] = getf(f[first]); n   b[first] ^= b[fx]; n   return f[first]; n } n bool Merge(int first, int second) { n   int fx = getf(first), fy = getf(second); n   if (fx == fy) return b[first] ^ b[second]; n   f[fx] = fy; n   b[fx] = b[first] ^ b[second] ^ 1; n   --cnt; n   return true; n } n int Pow(int a, int b) { n   int d = 1; n   for (; b; d = (b & 1) ? (long long)d * a % MOD : d, n             a = (long long)a * a % MOD, b /= 2) n     ; n   return d; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) scanf( %d%d , &p[i].first, &p[i].second); n   for (int i = 1; i <= n; ++i) n     for (int j = i + 1; j <= n; ++j) n       lis[dis(p[i], p[j])].push_back(make_pair(i, j)); n   cnt = n; n   for (int i = 1; i <= n; ++i) f[i] = i, b[i] = 0; n   for (int i = MAXN * 2 - 1; i >= 1; --i) { n     bool flag = true; n     int sum = cnt; n     for (vector<pair<int, int> >::iterator it = lis[i].begin(); n          it != lis[i].end(); ++it) n       if (!Merge(it->first, it->second)) { n         flag = false; n         break; n       } n     if (!flag) { n       cout << i << endl; n       cout << Pow(2, sum) << endl; n       exit(0); n     } n   } n   cout << 0 << endl << 2 << endl; n   fclose(stdin); n   fclose(stdout); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma comment(linker,  /STACK:102400000,102400000 ) n const int N = 1e5 + 10, M = 1024; n int a[N], cnt[N], n, k, x; n int main() { n   scanf( %d %d %d , &n, &k, &x); n   int ai; n   for (int i = 1; i <= n; i++) { n     scanf( %d , &ai); n     a[ai]++; n   } n   for (int ca = 1; ca <= k; ca++) { n     int tot = 0, y; n     for (int i = 0; i <= M; i++) { n       if (tot & 1) n         cnt[i] = a[i] >> 1; n       else n         cnt[i] = (a[i] + 1) >> 1; n       tot += a[i]; n     } n     for (int i = 0; i <= M; i++) a[i] -= cnt[i], a[i ^ x] += cnt[i]; n   } n   for (int i = M; i >= 0; i--) n     if (a[i]) { n       printf( %d  , i); n       break; n     } n   for (int i = 0; i <= M; i++) n     if (a[i]) { n       printf( %d n , i); n       break; n     } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 333333; n vector<pair<int, int> > graph[N]; n int vis[N], d[N]; n bool has[N]; n int n, m; n int dfs(int u) { n   vis[u] = true; n   int cnt = 0; n   for (auto e : graph[u]) { n     if (!vis[e.first]) { n       if (dfs(e.first)) { n         has[e.second] = true; n         cnt++; n       } n     } n   } n   return cnt % 2 != d[u]; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> m; n   for (int i = (1); i <= (n); i++) { n     cin >> d[i]; n   } n   for (int i = (1); i <= (m); i++) { n     int u, v; n     cin >> u >> v; n     graph[u].push_back(make_pair(v, i)); n     graph[v].push_back(make_pair(u, i)); n   } n   int cnt = 0; n   for (int i = (1); i <= (n); i++) { n     cnt += d[i] == 1; n   } n   for (int i = (1); i <= (n); i++) { n     if (d[i] == -1) { n       if (cnt & 1) { n         d[i] = 1; n         cnt++; n       } else { n         d[i] = 0; n       } n     } n   } n   if (cnt & 1) { n     cout << -1 <<   n ; n     return 0; n   } n   dfs(1); n   vector<int> v; n   for (int i = (1); i <= (m); i++) { n     if (has[i]) { n       v.push_back(i); n     } n   } n   cout << (int)(v).size() <<   n ; n   for (auto x : v) { n     cout << x <<    ; n   } n   cout <<   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 a[n]; n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n   } n   int sol = 0; n   for (int i = 1; i <= n; i++) { n     int cnt = 1; n     for (int j = i - 1; j >= 1; j--) { n       if (a[j] > a[j + 1]) n         break; n       else n         cnt++; n     } n     for (int k = i + 1; k <= n; k++) { n       if (a[k] > a[k - 1]) n         break; n       else n         cnt++; n     } n     sol = max(sol, cnt); n   } n   cout << sol; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, p[1000010], cur[1000010 * 2]; n int main() { n   long long ans = 0, sum = 0; n   int l = 0, r = 0, temp = 0; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &p[i]); n   for (int i = 1; i <= n; i++) { n     sum += abs(p[i] - i); n     if (p[i] >= i) { n       l++; n       cur[p[i] - i]++; n     } else n       r++; n   } n   ans = sum; n   for (int i = 0; i < n - 1; i++) { n     l -= cur[i]; n     r += cur[i]; n     sum = sum - l + r - abs(p[n - i] - n - 1) + p[n - i] - 1; n     cur[p[n - i] + i]++; n     l++, r--; n     if (sum < ans) { n       ans = sum; n       temp = i + 1; n     } n   } n   printf( %lld %d n , ans, temp); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, w, one = 0, two = 0, sum = 0; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> w; n     if (w == 100) { n       one++; n     } else { n       two++; n     } n     sum += w; n   } n   if (two % 2 == 0) { n     if (one % 2 == 0) { n       cout <<  YES ; n     } else { n       cout <<  NO ; n     } n   } else { n     one -= 2; n     if (one < 0) { n       cout <<  NO ; n       return 0; n     } n     if (one % 2 == 0) { n       cout <<  YES ; n     } else { n       cout <<  NO ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 2000 * 1000 * 1000; n int calc(int sweet, string str) { n   int mx = -1; n   for (register int i = (0); i < (int)(((int)(str).size())); ++i) n     if (sweet > 0 && str[i] ==  H ) { n       mx = max(mx, i); n       str[i] =  . ; n       sweet--; n     } n   int pos = 0; n   vector<pair<int, int> > v; n   for (register int i = (0); i < (int)(((int)(str).size())); ++i) n     if (str[i] ==  H ) { n       while (pos < ((int)(str).size()) && str[pos] !=  S ) pos++; n       if (pos == ((int)(str).size())) return INF; n       str[pos] =  . ; n       if (pos > i) n         v.push_back(pair<int, int>(i, pos)); n       else n         mx = max(mx, i); n     } n   sort((v).begin(), (v).end()); n   vector<pair<int, int> > u; n   for (register int i = (0); i < (int)(((int)(v).size())); ++i) n     if (u.empty() || u.back().second < v[i].first) n       u.push_back(v[i]); n     else n       u.back().second = v[i].second; n   if (u.empty()) return mx + 1; n   mx = max(mx, u.back().second); n   int res = mx + 1; n   for (__typeof((u).begin()) it = (u).begin(); it != (u).end(); ++it) n     res += 2 * (it->second - it->first); n   int ans = res; n   for (int i = ((int)(u).size()) - 1; i >= 0; i--) { n     res -= u[i].second - u[i].first; n     res += i + 1 == ((int)(u).size()) ? mx - u.back().second n                                       : u[i + 1].first - u[i].second; n     ans = min(ans, res); n   } n   return ans; n } n int main() { n   int n, t; n   string s; n   cin >> n >> t >> s; n   if (calc(n, s) > t) { n     cout << -1 << endl; n     return 0; n   } n   int mn = 0, mx = n; n   while (mn < mx) { n     int mid = (mn + mx) / 2; n     if (calc(mid, s) <= t) n       mx = mid; n     else n       mn = mid + 1; n   } n   cout << mn << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1e17; n int n, s, e, nxt[5001]; n long long x[5001], a[5001], b[5001], c[5001], d[5001]; n long long dp[5001][5001]; n inline long long dis(int s, int e) { n   return (s < e) ? d[s] + a[e] : c[s] + b[e]; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n >> s >> e; n   s--, e--; n   for (int i = 0; i < n; i++) cin >> x[i]; n   for (int i = 0; i < n; i++) cin >> a[i], a[i] += x[i]; n   for (int i = 0; i < n; i++) cin >> b[i], b[i] -= x[i]; n   for (int i = 0; i < n; i++) cin >> c[i], c[i] += x[i]; n   for (int i = 0; i < n; i++) cin >> d[i], d[i] -= x[i]; n   long long ans = dis(s, e); n   nxt[s] = e; n   for (int i = 0; i < n; i++) { n     if (i == s || i == e) continue; n     long long minv = INF, ind = i; n     for (int j = s; j != e; j = nxt[j]) { n       long long val = dis(j, i) + dis(i, nxt[j]) - dis(j, nxt[j]); n       if (val < minv) { n         minv = val; n         ind = j; n       } n     } n     ans += minv; n     nxt[i] = nxt[ind]; n     nxt[ind] = i; n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N, M, P, Q; n int A[101], B[101]; n int w[202][202]; n bool V[202]; n vector<int> path; n bool dfs(int n) { n   path.push_back(n); n   V[n] = 1; n   if (n == N + N + 1) return 1; n   for (int i = 0; i <= N + N + 1; i++) n     if (!V[i] && w[n][i]) { n       if (dfs(i)) return 1; n     } n   path.pop_back(); n   return 0; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin >> N >> M; n   for (int i = 1; i <= N; i++) cin >> A[i], w[0][i] = A[i], P += A[i]; n   for (int i = 1; i <= N; i++) n     cin >> B[i], w[N + i][N + N + 1] = B[i], Q += B[i]; n   for (int i = 1; i <= N; i++) w[i][N + i] = 1e9; n   for (int i = 1; i <= M; i++) { n     int a, b; n     cin >> a >> b; n     w[a][N + b] = 1e9; n     w[b][N + a] = 1e9; n   } n   int ans = 0; n   while (dfs(0)) { n     int flow = 2e9; n     for (int i = 1; i < ((int)(path).size()); i++) n       flow = min(flow, w[path[i - 1]][path[i]]); n     for (int i = 1; i < ((int)(path).size()); i++) { n       w[path[i - 1]][path[i]] -= flow; n       w[path[i]][path[i - 1]] += flow; n     } n     ans += flow; n     path.clear(); n     for (int i = 0; i <= N + N + 1; i++) V[i] = 0; n   } n   if (P != Q || ans != P) { n     puts( NO ); n     return 0; n   } n   puts( YES ); n   for (int i = 1; i <= N; i++, puts(  )) n     for (int j = 1; j <= N; j++) { n       printf( %d  , w[N + j][i]); n     } n } n


#include <bits/stdc++.h> n using namespace std; n const long double PI = 4 * atan(1); n const int32_t MOD = 1e9 + 7; n int power(int x, unsigned int y) { n   int temp; n   if (y == 0) return 1; n   temp = power(x, y / 2); n   if (y % 2 == 0) n     return temp * temp % MOD; n   else n     return x * temp % MOD * temp % MOD; n } n void solve() { n   string s; n   cin >> s; n   int n = s.length(); n   string s1, s2; n   cin >> s1 >> s2; n   int m1 = s1.length(), m2 = s2.length(); n   int j = -1, k = -1; n   bool f1 = 0, f2 = 0; n   j = s.find(s1); n   k = s.find(s2, s1.length() + j); n   if (j != -1 && k != -1) f1 = 1; n   reverse(s.begin(), s.end()); n   j = -1, k = -1; n   j = s.find(s1); n   k = s.find(s2, s1.length() + j); n   if (j != -1 && k != -1) f2 = 1; n   if (f1 && f2) n     cout <<  both n ; n   else if (f1) n     cout <<  forward n ; n   else if (f2) n     cout <<  backward n ; n   else n     cout <<  fantasy n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(0); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 111111; n vector<int> g[N]; n bool s[N], f[N], used[N], foo[N], bar[N]; n int q[N], qb, qe; n int main() { n   int n; n   cin >> n; n   for (int i = 1; i < n; i++) { n     int u, v; n     cin >> u >> v; n     g[u].push_back(v); n     g[v].push_back(u); n   } n   for (int i = 1; i <= n; i++) cin >> s[i]; n   for (int i = 1; i <= n; i++) cin >> f[i]; n   vector<int> x; n   q[qe++] = 1; n   used[1] = true; n   if (s[1] != f[1]) { n     x.push_back(1); n     foo[1] = 1; n   } n   while (qb < qe) { n     int u = q[qb++]; n     for (int i = 0; i < g[u].size(); i++) { n       int v = g[u][i]; n       if (used[v]) continue; n       q[qe++] = v; n       used[v] = true; n       if ((s[v] ^ bar[u]) != f[v]) { n         x.push_back(v); n         foo[v] = bar[u] ^ 1; n         bar[v] = foo[u]; n       } else { n         foo[v] = bar[u]; n         bar[v] = foo[u]; n       } n     } n   } n   cout << x.size() << endl; n   for (int i = 0; i < x.size(); i++) cout << x[i] << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int d[200], f[200][200]; n int c[200]; n int dis[200][200]; n long long fa[200]; n int ans[200]; n void floyed() { n   for (int k = 1; k <= n; ++k) n     for (int i = 1; i <= n; ++i) n       for (int j = 1; j <= n; ++j) n         dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]); n } n void dfs_1(int x) { n   for (int i = 1; i <= n; ++i) f[x][i] = d[dis[x][i]] + m; n   for (int i = 1; i <= n; ++i) { n     if (dis[x][i] == 1 && (fa[x] != i)) { n       fa[i] = x; n       dfs_1(i); n       for (int j = 1; j <= n; ++j) f[x][j] += min(f[i][j] - m, f[i][c[i]]); n     } n   } n   c[x] = 1; n   for (int i = 1; i <= n; ++i) n     if (f[x][c[x]] > f[x][i]) c[x] = i; n } n void dfs_2(int x, int y) { n   ans[x] = y; n   for (int i = 1; i <= n; ++i) { n     if (dis[i][x] == 1 && (fa[i] == x)) { n       if (f[i][y] - m >= f[i][c[i]]) n         dfs_2(i, c[i]); n       else n         dfs_2(i, y); n     } n   } n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n - 1; ++i) scanf( %d , &d[i]); n   memset(dis, 53, sizeof(dis)); n   for (int i = 1; i <= n - 1; ++i) { n     int x, y; n     scanf( %d%d , &x, &y); n     dis[x][y] = 1; n     dis[y][x] = 1; n   } n   for (int i = 1; i <= n; ++i) dis[i][i] = 0; n   floyed(); n   dfs_1(1); n   cout << f[1][c[1]] << endl; n   dfs_2(1, c[1]); n   for (int i = 1; i <= n; ++i) printf( %d  , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n constexpr auto Inf = 0X3F3F3F3F; n using namespace std; n namespace IO { n inline long long read() { n   long long o = 0, f = 1; n   char c = getchar(); n   while (c <  0  || c >  9 ) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (c >  /  && c <  : ) { n     o = o * 10 + c -  0 ; n     c = getchar(); n   } n   return o * f; n } n inline char recd() { n   char o; n   while ((o = getchar()) <  0  || o >  9 ) n     ; n   return o; n } n inline double reod() { n   double o = read(), f = 1; n   char c; n   while ((c = getchar()) >  /  && c <  : ) o += (c -  0 ) / (f *= 10); n   return o; n } n }  // namespace IO n using namespace IO; n const int SIZE = 2E5 + 7, Mod = 1E9 + 7; n vector<pair<int, pair<long long, int> > > vec[SIZE]; n long long TS = 0; n struct Node { n   long long w; n   int c, cost; n   Node(long long w, long long c, long long cost) : w(w), c(c), cost(cost){}; n   bool operator<(const Node& Tmp) const { n     return (w - w / 2) * c < (Tmp.w - Tmp.w / 2) * Tmp.c; n   } n }; n priority_queue<Node> oque, sque; n int DFS(int now, int F) { n   int son = 0; n   for (int pos = 0; pos < vec[now].size(); pos++) { n     int v = vec[now][pos].first; n     if (v != F) { n       int s = DFS(v, now); n       TS += s * vec[now][pos].second.first; n       son += s; n       if (vec[now][pos].second.second == 1) n         oque.push( n             Node(vec[now][pos].second.first, s, vec[now][pos].second.second)); n       else n         sque.push( n             Node(vec[now][pos].second.first, s, vec[now][pos].second.second)); n     } n   } n   return son == 0 ? 1 : son; n } n long long aw[int(2E6)], bw[int(2E6)]; n int main() { n   int T = read(); n   while (T--) { n     int N = read(); n     long long S = read(), Tmp; n     for (int pos = 1, u, v, w, c; pos < N; pos++) { n       u = read(), v = read(), w = read(), c = read(); n       vec[u].push_back({v, {w, c}}); n       vec[v].push_back({u, {w, c}}); n     } n     DFS(1, 0); n     int ans = Inf, aInd = 1, bInd = 1; n     long long d = TS - S; n     Tmp = d; n     while (Tmp > 0 && !oque.empty()) { n       Node now = oque.top(); n       oque.pop(); n       Tmp -= (now.w - now.w / 2) * now.c; n       aw[aInd] = aw[aInd - 1] + (now.w - now.w / 2) * now.c; n       aInd++, now.w /= 2; n       if (now.w) oque.push(now); n     } n     Tmp = d; n     while (Tmp > 0 && !sque.empty()) { n       Node now = sque.top(); n       sque.pop(); n       Tmp -= (now.w - now.w / 2) * now.c; n       bw[bInd] = bw[bInd - 1] + (now.w - now.w / 2) * now.c; n       bInd++, now.w /= 2; n       if (now.w) sque.push(now); n     } n     int L = 0, R = bInd - 1; n     while (L < aInd && R >= 0) { n       while (R >= 0 && aw[L] + bw[R] >= d) ans = min(ans, L + (R << 1)), R--; n       L++; n     } n     printf( %d n , TS <= S ? 0 : ans); n     for (int pos = 1; pos <= N; pos++) vec[pos].clear(); n     TS = 0; n     while (!oque.empty()) oque.pop(); n     while (!sque.empty()) sque.pop(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int read() { n   char ch = getchar(); n   int x = 0, fl = 1; n   for (; !isdigit(ch); ch = getchar()) n     if (ch ==  - ) fl = -1; n   for (; isdigit(ch); ch = getchar()) x = (x << 3) + (x << 1) + (ch -  0 ); n   return x * fl; n } n const int N = 300005; n const long long INF = 1e18; n int n, m, s, dep[N], ff[N], fa[N][21], de[N]; n long long dis[N]; n bool vis[N]; n vector<pair<int, int> > adj[N]; n vector<int> bdj[N]; n priority_queue<pair<long long, int> > q; n void dij() { n   for (int i = 1; i <= n; i++) dis[i] = INF; n   while (!q.empty()) q.pop(); n   dis[s] = 0LL; n   q.push(make_pair(0LL, s)); n   while (!q.empty()) { n     int x = q.top().second; n     q.pop(); n     if (vis[x]) continue; n     vis[x] = 1; n     for (auto e : adj[x]) { n       int y = e.first; n       long long w = e.second; n       if (dis[y] > dis[x] + w) { n         dis[y] = dis[x] + w; n         q.push(make_pair(-dis[y], y)); n       } n     } n   } n } n int lca(int x, int y) { n   if (dep[x] < dep[y]) swap(x, y); n   for (int i = 20; ~i; i--) n     if (dep[fa[x][i]] >= dep[y]) x = fa[x][i]; n   for (int i = 20; ~i; i--) n     if (fa[x][i] != fa[y][i]) x = fa[x][i], y = fa[y][i]; n   return (x == y) ? x : fa[x][0]; n } n queue<int> Q; n int ver[N], nxt[N], fst[N], tot, siz[N], ans; n void addedge(int x, int y) { n   ver[++tot] = y; n   nxt[tot] = fst[x]; n   fst[x] = tot; n } n void dfs(int x) { n   siz[x] = 1; n   for (int i = fst[x]; i; i = nxt[i]) { n     int y = ver[i]; n     dfs(y); n     siz[x] += siz[y]; n   } n } n int main() { n   n = read(); n   m = read(); n   s = read(); n   for (int i = 1; i <= m; i++) { n     int x = read(), y = read(), w = read(); n     adj[x].push_back(make_pair(y, w)); n     adj[y].push_back(make_pair(x, w)); n   } n   dij(); n   for (int x = 1; x <= n; x++) { n     for (auto e : adj[x]) { n       int y = e.first; n       long long w = e.second; n       if (dis[y] == dis[x] + w) { n         bdj[x].push_back(y); n         de[y]++; n       } n     } n   } n   memset(ff, -1, sizeof(ff)); n   dep[0] = 1; n   for (int i = 1; i <= n; i++) { n     if (!de[i] && dis[i] < INF) { n       ff[i] = 0; n       Q.push(i); n     } n   } n   while (!Q.empty()) { n     int x = Q.front(); n     Q.pop(); n     dep[x] = dep[ff[x]] + 1; n     fa[x][0] = ff[x]; n     for (int i = 1; i <= 20; i++) fa[x][i] = fa[fa[x][i - 1]][i - 1]; n     for (auto y : bdj[x]) { n       if (ff[y] == -1) n         ff[y] = x; n       else n         ff[y] = lca(ff[y], x); n       de[y]--; n       if (!de[y]) Q.push(y); n     } n   } n   for (int i = 1; i <= n; i++) addedge(ff[i], i); n   dfs(0); n   for (int i = 1; i <= n; i++) n     if (i != s) ans = max(ans, siz[i]); n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void O_o() { n   ios::sync_with_stdio(0); n   ios_base::sync_with_stdio(0); n   cin.tie(0), cout.tie(0); n } n int main() { n   O_o(); n   int x, y; n   bool flag; n   vector<int> vec; n   cin >> x; n   y = x; n   while (x != 0) { n     vec.push_back(x % 10); n     x = x / 10; n   } n   for (int i = 0; i < vec.size(); i++) { n     if (vec[i] == 4 || vec[i] == 7) n       flag = 1; n     else { n       flag = 0; n       break; n     } n   } n   if (flag) n     cout <<  YES ; n   else if (y % 7 == 0 || y % 4 == 0 || y == 799 || y == 94 || y == 141) n     cout <<  YES ; n   else n     cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MAXN = 1e6 + 100; n const long long MAXL = 21; n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long x, r, l; n   cin >> x >> l >> r; n   long long ans = 0; n   long long a = 1; n   while (2 * a <= x) a = 2 * a; n   for (long long i = l; i <= r; i++) ans += bool(x & (a / (i & -i))); n   cout << ans; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:16777216 ) n #pragma warning(disable : 4786) n using namespace std; n int MIN(int a, int b) { return a < b ? a : b; } n int MAX(int a, int b) { return a > b ? a : b; } n int GCD(int a, int b) { n   while (b) b ^= a ^= b ^= a %= b; n   return a; n } n int LCM(int a, int b) { return a * (b / GCD(a, b)); } n void SWAP(int &a, int &b) { n   a = a ^ b; n   b = a ^ b; n   a = a ^ b; n } n const double PI = acos(-1); n const double EPS = 1e-11; n int arr[310][310], memo[310][310][310], N, M; n int dp(int i, int j, int k) { n   int l = i + j - k; n   if (i == N - 1 && j == M - 1) { n     return arr[i][j]; n   } n   if (i == N || j == M || k >= N || l >= N) { n     return -(1 << 29); n     ; n   } n   int &ret = memo[i][j][k]; n   if (ret != -1) { n     return ret; n   } n   int res, r1, val; n   res = -(1 << 29); n   ; n   if (i == k && j == l) { n     val = arr[i][j]; n   } else { n     val = arr[i][j] + arr[k][l]; n   } n   r1 = dp(i + 1, j, k + 1) + val; n   res = MAX(res, r1); n   r1 = dp(i + 1, j, k) + val; n   res = MAX(res, r1); n   r1 = dp(i, j + 1, k + 1) + val; n   res = MAX(res, r1); n   r1 = dp(i, j + 1, k) + val; n   res = MAX(res, r1); n   return ret = res; n } n int main() { n   int T, res, n, i, ind, m, j; n   while (scanf( %d , &n) != EOF) { n     int a = -(1 << 29); n     ; n     memset(memo, -1, sizeof(memo)); n     memset(arr, 0, sizeof(arr)); n     m = n; n     for (i = 0; i < (n); i++) { n       for (j = 0; j < (m); j++) { n         scanf( %d , &arr[i][j]); n       } n     } n     N = n; n     M = m; n     res = dp(0, 0, 0); n     printf( %d n , res); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int Mod = 1e9 + 7; n const int N = 1e3 + 100; n const int M = 3e4 + 100; n const int INF = 0x3f3f3f3f; n const long long LINF = 0x3f3f3f3f3f3f3f3f; n inline long long dpow(long long a, long long b) { n   long long r = 1, t = a; n   while (b) { n     if (b & 1) r = (r * t) % Mod; n     b >>= 1; n     t = (t * t) % Mod; n   } n   return r; n } n inline long long fpow(long long a, long long b) { n   long long r = 1, t = a; n   while (b) { n     if (b & 1) r = (r * t); n     b >>= 1; n     t = (t * t); n   } n   return r; n } n vector<pair<int, int> > G[N << 1]; n vector<int> g; n int dfn[N], low[N], n, m, cnt; n int val[M], sp, tp; n int edge[M], res; n bool vis[N], cut[M]; n int ID[5], ans; n bool path(int x) { n   if (x == tp) return true; n   vis[x] = true; n   for (auto it : G[x]) { n     int v = it.first, id = it.second; n     if (!vis[v] && path(v)) { n       edge[++res] = id; n       return true; n     } n   } n   return false; n } n bool dfs(int x, int no) { n   if (x == tp) return true; n   vis[x] = true; n   for (auto it : G[x]) { n     int v = it.first; n     int id = it.second; n     if (id != no && !vis[v] && dfs(v, no)) { n       g.push_back(id); n       return true; n     } n   } n   return false; n } n void tarjan(int x, int fa, int no) { n   dfn[x] = low[x] = ++cnt; n   for (auto it : G[x]) { n     int v = it.first, id = it.second; n     if (id == fa || id == no) continue; n     if (!dfn[v]) { n       tarjan(v, id, no); n       low[x] = min(low[x], low[v]); n       if (low[v] > dfn[x]) cut[id] = true; n     } else n       low[x] = min(low[x], dfn[v]); n   } n } n int main() { n   cin >> n >> m >> sp >> tp; n   for (int i = 1; i <= m; i++) { n     int u, v; n     scanf( %d %d %d , &u, &v, &val[i]); n     G[u].push_back({v, i}); n     G[v].push_back({u, i}); n   } n   if (!path(sp)) printf( 0 n0 n ), exit(0); n   ans = INT_MAX; n   for (int i = 1; i <= res; i++) { n     int ii = edge[i]; n     memset(vis, 0, sizeof(vis)), g.clear(); n     if (!dfs(sp, ii)) { n       if (val[ii] < ans) ans = val[ii], ID[1] = ii, ID[2] = 0; n     } else { n       cnt = 0; n       memset(dfn, 0, sizeof(dfn)), memset(low, 0, sizeof(low)); n       memset(cut, 0, sizeof(cut)); n       for (int j = 1; j <= n; j++) { n         if (!dfn[j]) tarjan(j, 0, ii); n       } n       for (auto it : g) { n         if (cut[it] && val[it] + val[ii] < ans) n           ans = val[it] + val[ii], ID[1] = ii, ID[2] = it; n       } n     } n   } n   if (ans == INT_MAX) n     printf( -1 n ); n   else { n     printf( %d n , ans); n     if (!ID[2]) n       printf( 1 n%d n , ID[1]); n     else n       printf( 2 n%d %d n , ID[1], ID[2]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int n, p, k; n long long int cost[200010]; n long long int price() { n   long long int dp[200010] = {0}; n   sort(cost, cost + n); n   long long int ans = 0; n   for (long long int i = 0; i < n; i++) { n     if (i < k - 1) { n       dp[i] = cost[i] + (i == 0 ? 0 : dp[i - 1]); n     } else if (i == k - 1) { n       dp[i] = cost[i]; n     } else { n       dp[i] = dp[i - k] + cost[i]; n     } n     if (dp[i] <= p) ans = i + 1; n   } n   return ans; n } n int main() { n   int t; n   cin >> t; n   while (t--) { n     cin >> n >> p >> k; n     for (int i = 0; i < n; i++) { n       cin >> cost[i]; n     }; n     cout << price() << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int ans; n int visited[1001]; n int cc = 0; n vector<int> arr[1001]; n void dfs(int node) { n   visited[node]++; n   if (visited[node] == 2) { n     ans = node; n     return; n   } n   for (auto child : arr[node]) { n     if (visited[node] < 2) dfs(child); n   } n } n int main() { n   int n, v, a, b; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a; n     arr[i].push_back(a); n   } n   for (int i = 1; i <= n; i++) { n     memset(visited, 0, sizeof(visited)); n     dfs(i); n     cout << ans <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long MOD = 1e9 + 7; n long long pow(long long x, long long v) { n   if (v == 0) return 1LL; n   if (v % 2) return (x % MOD) * pow(x % MOD, v - 1) % MOD; n   return pow((x * x) % MOD, v / 2) % MOD; n } n int main() { n   int n; n   long long x, sum = 0, v, mx = 0; n   scanf( %d%lld , &n, &x); n   map<long long, int> cnt; n   vector<long long> a(n); n   for (int i = 0; i < n; ++i) n     scanf( %lld , &a[i]), sum += a[i], mx = max(mx, a[i]); n   for (int i = 0; i < n; ++i) cnt[sum - a[i]]++; n   for (map<long long, int>::iterator it = cnt.begin(); it != cnt.end(); ++it) { n     v = it->first; n     if (it->second % x == 0) { n       cnt[v + 1] += it->second / x; n     } else n       break; n   } n   v = min(v, sum); n   printf( %lld , pow(x, v)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t, n; n   cin >> t; n   while (t--) { n     cin >> n; n     vector<int> numbers(n); n     vector<int> trayectoria(n + 1); n     for (int &i : numbers) cin >> i; n     if (numbers[0] == 1) { n       cout << n + 1 <<    ; n       for (int i = 1; i < n + 1; i++) cout << i <<    ; n       cout <<   n ; n     } else if (numbers[n - 1] == 0) { n       for (int i = 1; i < n; i++) cout << i <<    ; n       cout << n <<     << n + 1 <<    ; n       cout <<   n ; n     } else { n       bool poss = false; n       for (int i = 0; i < n; i++) { n         if (i < n - 1 && numbers[i] < numbers[i + 1]) { n           for (int j = 0; j < i; j++) cout << j + 1 <<    ; n           cout << i + 1 <<     << n + 1 <<    ; n           for (int j = i + 1; j < n; j++) cout << j + 1 <<    ; n           cout << endl; n           poss = true; n           break; n         } n       } n       if (!poss) cout <<  -1 n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, c; n   while (cin >> a >> b) { n     if (a == 0 && b == 0) n       cout <<  NO  << endl; n     else { n       c = abs(a - b); n       if (c == 0 || c == 1) n         cout <<  YES  << endl; n       else n         cout <<  NO  << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 10; n const int inf = 0x3f3f3f3f; n const int mod = 1e9 + 7; n int n, m, k, t, mx, mi, x, y, dx, dy, idx, cnt, tot, sum, res, flag, st; n vector<int> e[maxn]; n void dfs(int u, int pre, int dep, int op) { n   if (dep > mx) { n     mx = dep; n     idx = u; n   } n   if (u == y && op) { n     if (dep <= dx) { n       flag = 1; n       return; n     } n   } n   for (int v : e[u]) { n     if (v == pre) continue; n     dfs(v, u, dep + 1, op); n   } n } n int main() { n   scanf( %d , &t); n   while (t--) { n     flag = 0; n     scanf( %d %d %d %d %d , &n, &x, &y, &dx, &dy); n     for (int i = 1; i <= n; i++) { n       e[i].clear(); n     } n     for (int i = 1; i < n; i++) { n       int u, v; n       scanf( %d %d , &u, &v); n       e[u].push_back(v); n       e[v].push_back(u); n     } n     mx = 0; n     dfs(x, 0, 0, 1); n     if (flag) { n       puts( Alice ); n       continue; n     } n     mx = 0; n     dfs(idx, 0, 0, 0); n     if (2 * dx >= mx || dy <= 2 * dx) { n       puts( Alice ); n     } else { n       puts( Bob ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n int n; n int length; n int num[1000]; n int main() { n   scanf( %d , &n); n   while (n--) { n     int max = 0; n     int mark = 0; n     scanf( %d , &length); n     for (int i = 1; i <= length; i++) { n       scanf( %d , &num[i]); n       if (num[i] > (i + mark)) { n         mark += num[i] - i - mark; n       } n     } n     printf( %d n , mark); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void read(T &x) { n   x = 0; n   bool f = 0; n   char c = getchar(); n   for (; !isdigit(c); c = getchar()) n     if (c ==  - ) f = 1; n   for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); n   if (f) x = -x; n } n template <typename F> n inline void write(F x, char ed =   n ) { n   static short st[30]; n   short tp = 0; n   if (x < 0) putchar( - ), x = -x; n   do st[++tp] = x % 10, x /= 10; n   while (x); n   while (tp) putchar( 0  | st[tp--]); n   putchar(ed); n } n template <typename T> n inline void write(T *st, int n, string s =   ) { n   if (s.size()) cout << s <<  :  ; n   while (n--) write(*st++,    ); n   puts(  ); n } n const int N = 200050; n const int P = 1e9 + 7; n long long len[N], p[N], sum[N], ans; n int L[N], R[N], n; n long long fpw(long long x, long long mi, long long res = 1) { n   for (; mi; mi >>= 1, x = x * x % P) n     if (mi & 1) res = res * x % P; n   return res; n } n int main() { n   read(n); n   for (int i = 1; i <= n; ++i) read(L[i]); n   for (int i = 1; i <= n; ++i) read(R[i]), len[i] = R[i] - L[i] + 1; n   long long ans = p[0] = 1; n   len[0] = 1; n   sum[0] = 1; n   for (int i = 1; i < n; ++i) { n     p[i] = max(0, min(R[i], R[i + 1]) - max(L[i], L[i + 1]) + 1); n     p[i] = p[i] * fpw(len[i] * len[i + 1] % P, P - 2) % P, n     p[i] = (P + 1 - p[i]) % P; n     ans = (ans + p[i] + (i > 1 ? 2 * p[i] * sum[i - 2] : 0)) % P, n     sum[i] = (sum[i - 1] + p[i]) % P; n     int tr = min(min(R[i], R[i + 1]), R[i - 1]), n         tl = max(max(L[i], L[i + 1]), L[i - 1]); n     int t = max(tr - tl + 1, 0) * n             fpw(len[i - 1] * len[i] % P * len[i + 1] % P, P - 2) % P; n     t = (1ll - (1 - p[i - 1] + 1 - p[i]) + t) % P; n     ans = (ans + 2 * t) % P; n   } n   write((ans % P + P) % P); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   if (s.size() == 1 && n       (s[0] ==  A  || s[0] ==  I  || s[0] ==  M  || s[0] ==  O  || n        s[0] ==  T  || s[0] ==  U  || s[0] ==  V  || s[0] ==  W  || n        s[0] ==  X  || s[0] ==  Y  || s[0] ==  H )) { n     cout <<  YES ; n     return 0; n   } n   for (int i = 0; i < s.size() / 2; i++) n     if (s[i] == s[s.size() - 1 - i] && n         (s[i] ==  A  || s[i] ==  I  || s[i] ==  M  || s[i] ==  O  || n          s[i] ==  T  || s[i] ==  U  || s[i] ==  V  || s[i] ==  W  || n          s[i] ==  X  || s[i] ==  Y  || s[i] ==  H )) n       continue; n     else { n       cout <<  NO ; n       return 0; n     } n   if (s.size() % 2 == 1) { n     if (s[s.size() / 2] ==  B ) { n       cout <<  NO ; n       return 0; n     } n     if (s[s.size() / 2] ==  C ) { n       cout <<  NO ; n       return 0; n     } n     if (s[s.size() / 2] ==  D ) { n       cout <<  NO ; n       return 0; n     } n     if (s[s.size() / 2] ==  E ) { n       cout <<  NO ; n       return 0; n     } n     if (s[s.size() / 2] ==  F ) { n       cout <<  NO ; n       return 0; n     } n     if (s[s.size() / 2] ==  G ) { n       cout <<  NO ; n       return 0; n     } n     if (s[s.size() / 2] ==  J ) { n       cout <<  NO ; n       return 0; n     } n     if (s[s.size() / 2] ==  K ) { n       cout <<  NO ; n       return 0; n     } n     if (s[s.size() / 2] ==  L ) { n       cout <<  NO ; n       return 0; n     } n     if (s[s.size() / 2] ==  P ) { n       cout <<  NO ; n       return 0; n     } n     if (s[s.size() / 2] ==  N ) { n       cout <<  NO ; n       return 0; n     } n     if (s[s.size() / 2] ==  Q ) { n       cout <<  NO ; n       return 0; n     } n     if (s[s.size() / 2] ==  R ) { n       cout <<  NO ; n       return 0; n     } n     if (s[s.size() / 2] ==  S ) { n       cout <<  NO ; n       return 0; n     } n     if (s[s.size() / 2] ==  Z ) { n       cout <<  NO ; n       return 0; n     } n   } n   cout <<  YES ; n } n


#include <bits/stdc++.h> n struct MinCostFlow { n   const long long INF1 = 1000000000LL; n   const long long INF2 = 100000000LL; n   struct edge { n     long long u, v; n     long long cap, cost; n     edge* rev; n     edge(const long long& uu, const long long& vv, const long long& _cap, n          const long long& _cost, edge* _rev = nullptr) n         : u(uu), v(vv), cap(_cap), cost(_cost), rev(_rev) {} n   }; n   struct node { n     long long id, d; n     node(const long long& _id, const long long& _d) : id(_id), d(_d) {} n     bool operator<(const node& o) const { return d < o.d; } n     bool operator>(const node& o) const { return d > o.d; } n   }; n   std::vector<std::vector<edge*>> adj; n   long long s, t, mS, mT; n   std::vector<edge*> prev; n   std::vector<long long> w, pi; n   MinCostFlow(long long n) { n     s = n++; n     t = n++; n     mS = n++; n     mT = n++; n     adj.assign(n, std::vector<edge*>()); n   } n   ~MinCostFlow(void) { n     for (auto& row : adj) n       for (auto& e : row) delete e; n   } n   edge* add_edge(const long long& a, const long long& b, const long long& cap, n                  const long long& cost) { n     edge* e1 = new edge(a, b, cap, cost); n     edge* e2 = new edge(b, a, 0, -cost); n     e1->rev = e2; n     e2->rev = e1; n     adj[a].push_back(e1); n     adj[b].push_back(e2); n     return e1; n   } n   std::pair<long long, long long> min_cost_max_flow(void) { n     long long flow = 0; n     long long cost = 0; n     get_potential(); n     while (find_path()) { n       flow += w[t]; n       cost += update(); n     } n     return {flow, cost}; n   } n   void get_potential(void) { n     pi.assign(adj.size(), INF1); n     pi[s] = 0; n     for (long long i = 0; i + 1 < adj.size(); ++i) { n       bool upd = false; n       for (long long j = 0; j < adj.size(); ++j) { n         for (edge* e : adj[j]) { n           if (e->cap > 0 && pi[e->v] > pi[e->u] + e->cost) { n             pi[e->v] = pi[e->u] + e->cost; n             upd = true; n           } n         } n       } n       if (!upd) break; n     } n   } n   bool find_path(void) { n     std::vector<long long> d(adj.size(), INF1); n     d[s] = 0; n     prev.assign(adj.size(), nullptr); n     w.assign(adj.size(), INF1); n     std::priority_queue<node, std::vector<node>, std::greater<node>> pq; n     pq.push({s, 0}); n     while (!pq.empty()) { n       node best = pq.top(); n       pq.pop(); n       if (best.d != d[best.id]) continue; n       for (edge* e : adj[best.id]) { n         long long nd = d[best.id] + e->cost + pi[e->u] - pi[e->v]; n         if (e->cap > 0 && d[e->v] > nd) { n           d[e->v] = nd; n           prev[e->v] = e; n           w[e->v] = std::min(w[best.id], e->cap); n           pq.push({e->v, nd}); n         } n       } n     } n     for (long long i = 0; i < pi.size(); ++i) pi[i] += d[i]; n     return d[t] < INF1; n   } n   long long update(void) { n     long long v = t; n     long long curr = 0; n     while (prev[v] != nullptr) { n       prev[v]->cap -= w[t]; n       prev[v]->rev->cap += w[t]; n       curr += w[t] * prev[v]->cost; n       v = prev[v]->u; n     } n     return curr; n   } n }; n int main(void) { n   std::ios::sync_with_stdio(false); n   std::cin.tie(nullptr); n   long long n, q; n   std::cin >> n >> q; n   std::vector<std::vector<char>> adj_mat(n, std::vector<char>(n, true)); n   for (long long type, l, r, v; q > 0; --q) { n     std::cin >> type >> l >> r >> v; n     --l; n     --r; n     --v; n     if (type == 1) { n       for (long long i = 0; i < v; ++i) { n         for (long long j = l; j <= r; ++j) adj_mat[i][j] = false; n       } n     } else { n       for (long long i = v + 1; i < n; ++i) { n         for (long long j = l; j <= r; ++j) adj_mat[i][j] = false; n       } n     } n   } n   MinCostFlow graph(n + n); n   for (long long num = 0; num < n; ++num) { n     for (long long quant = 1, last = 0; quant <= n; n          last = quant * quant, quant++) { n       graph.add_edge(graph.s, num, 1, quant * quant - last); n     } n   } n   for (long long spot = n; spot < 2 * n; ++spot) n     graph.add_edge(spot, graph.t, 1, 0); n   for (long long i = 0; i < n; ++i) { n     for (long long j = 0; j < n; ++j) { n       if (adj_mat[i][j]) graph.add_edge(i, n + j, 1, 0); n     } n   } n   const std::pair<long long, long long> ret = graph.min_cost_max_flow(); n   if (ret.first != n) { n     std::cout <<  -1 n ; n   } else { n     std::cout << ret.second <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n using namespace std; n int n, m; n const int maxN = 42; n vector<int> g[maxN]; n int deg[maxN]; n bool ed[maxN][maxN]; n long long f() { return (1LL << n); } n const int maxK = 20; n bool bad[(1 << maxK) + 10]; n int from[(1 << maxK) + 10]; n bool other_bad[(1 << maxK) + 10]; n int cnt[(1 << maxK) + 10]; n long long indep() { n   int half = n / 2; n   for (int i = 0; i < half; i++) { n     for (int j = i + 1; j < half; j++) { n       if (ed[i][j]) { n         bad[(1 << i) + (1 << j)] = true; n       } n     } n     for (int j = half; j < n; j++) { n       if (ed[i][j]) { n         from[(1 << i)] |= (1 << (j - half)); n       } n     } n   } n   for (int bit = 0; bit < half; bit++) { n     for (int mask = 0; mask < (1 << half); mask++) { n       if (mask & (1 << bit)) { n         bad[mask] |= bad[mask ^ (1 << bit)]; n         from[mask] |= from[mask ^ (1 << bit)]; n       } n     } n   } n   int other_half = n - half; n   for (int i = half; i < n; i++) { n     for (int j = i + 1; j < n; j++) { n       if (ed[i][j]) { n         other_bad[(1 << (i - half)) + (1 << (j - half))] = true; n       } n     } n   } n   for (int bit = 0; bit < other_half; bit++) { n     for (int mask = 0; mask < (1 << other_half); mask++) { n       if (mask & (1 << bit)) { n         other_bad[mask] |= other_bad[mask ^ (1 << bit)]; n       } n     } n   } n   for (int mask = 0; mask < (1 << other_half); mask++) { n     if (!other_bad[mask]) cnt[mask] = 1; n   } n   for (int bit = 0; bit < other_half; bit++) { n     for (int mask = 0; mask < (1 << other_half); mask++) { n       if (mask & (1 << bit)) { n         cnt[mask] += cnt[mask ^ (1 << bit)]; n       } n     } n   } n   int all = (1 << other_half) - 1; n   long long tot = 0; n   for (int my_mask = 0; my_mask < (1 << half); my_mask++) { n     if (bad[my_mask]) continue; n     int bad_guys = from[my_mask]; n     int good_guys = all ^ bad_guys; n     tot += cnt[good_guys]; n   } n   return tot; n } n bool used[maxN]; n void dfs(int v) { n   used[v] = true; n   for (int to : g[v]) { n     if (used[to]) continue; n     dfs(to); n   } n } n long long ones() { n   int tot = 0; n   memset(used, 0, sizeof used); n   for (int i = 0; i < n; i++) { n     if (!used[i]) { n       dfs(i); n       tot++; n     } n   } n   return (1LL << tot); n } n long long f01() { n   int tot = 0; n   for (int i = 0; i < n; i++) n     if (deg[i] == 0) tot++; n   return (1LL << tot); n } n int clr[maxN]; n bool ALL_OK = true; n void other_dfs(int v) { n   used[v] = true; n   for (int to : g[v]) { n     if (used[to]) { n       if (clr[to] != clr[v] ^ 1) { n         ALL_OK = false; n         return; n       } n     } else { n       clr[to] = clr[v] ^ 1; n       other_dfs(to); n     } n   } n } n long long f02() { n   int tot = 0; n   memset(used, 0, sizeof used); n   for (int i = 0; i < n; i++) { n     if (!used[i]) { n       tot++; n       other_dfs(i); n     } n   } n   if (!ALL_OK) return 0; n   return (1LL << tot); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cin >> n >> m; n   if (m == 0) { n     cout << 0; n     return 0; n   } n   for (int i = 0; i < m; i++) { n     int a, b; n     cin >> a >> b; n     a--; n     b--; n     deg[a]++; n     deg[b]++; n     g[a].push_back(b); n     g[b].push_back(a); n     ed[a][b] = ed[b][a] = true; n   } n   long long x = f(); n   long long y = indep(); n   long long z = ones(); n   long long t = f01(); n   long long q = f02(); n   cout << x - 2 * y - z + 2 * t + q; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = (long long)3e18; n struct data { n   long long x, flag, id; n   int operator<(const data& d) const { return x < d.x; } n } d[200010 << 2]; n long long dp[200010 << 2][2], c[200010 << 2], s[200010 << 2], v[200010 << 2], n     nw, ans; n long long _s[200010 << 2], n, np, m, C, tp, pre; n bool is2[200010 << 2]; n struct zkw { n   long long zkwm, s[2100000], atg[2100000]; n   void init(int len) { n     zkwm = 1; n     while (zkwm <= len + 2) zkwm <<= 1; n     for (int i = 1; i <= zkwm + zkwm; ++i) s[i] = -inf; n   } n   long long query(int l, int r) { n     if (l > r || r <= 0 || l > np) return -inf; n     l = max(l, 1), r = min((long long)r, np); n     long long lans = -inf, rans = -inf; n     for (l += zkwm - 1 - 1, r += zkwm + 1 - 1; l ^ r ^ 1; l >>= 1, r >>= 1) { n       if (lans != -inf) lans += atg[l]; n       if (rans != -inf) rans += atg[r]; n       if (~l & 1) lans = max(lans, s[l ^ 1]); n       if (r & 1) rans = max(rans, s[r ^ 1]); n     } n     if (lans != -inf) lans += atg[l]; n     if (rans != -inf) rans += atg[r]; n     lans = max(lans, rans); n     if (lans != -inf) n       for (l >>= 1; l; l >>= 1) lans += atg[l]; n     return lans; n   } n   void mdy(int l, int r, long long a) { n     if (l > r || r <= 0 || l > np) return; n     l = max(l, 1), r = min((long long)r, np); n     int f = 0; n     for (l += zkwm - 1 - 1, r += zkwm + 1 - 1; l ^ r ^ 1; n          l >>= 1, r >>= 1, f = 1) { n       if (~l & 1) atg[l ^ 1] += a, s[l ^ 1] += a; n       if (r & 1) atg[r ^ 1] += a, s[r ^ 1] += a; n       if (f) n         s[l] = max(s[l << 1], s[l << 1 | 1]) + atg[l], n         s[r] = max(s[r << 1], s[r << 1 | 1]) + atg[r]; n     } n     s[l] = max(s[l << 1], s[l << 1 | 1]) + atg[l], n     s[r] = max(s[r << 1], s[r << 1 | 1]) + atg[r]; n     for (l >>= 1; l; l >>= 1) s[l] = max(s[l << 1], s[l << 1 | 1]) + atg[l]; n   } n   void _mdy(int l, long long a) { n     l += zkwm - 1; n     atg[l] = 0; n     for (int _l = l >> 1; _l; _l >>= 1) a -= atg[_l]; n     if (s[l] < a) n       s[l] = a; n     else n       return; n     for (l >>= 1; l; l >>= 1) s[l] = max(s[l << 1], s[l << 1 | 1]) + atg[l]; n   } n } A[4]; n int main() { n   scanf( %lld%lld%lld , &n, &m, &C); n   for (long long i = 1, L, R; i <= n; ++i) n     scanf( %lld%lld , &L, &R), d[++tp] = data{L, 1ll, 0ll}, n                                d[++tp] = data{R, -1ll, 0ll}; n   for (long long i = 1, L, R; i <= m; ++i) n     scanf( %lld%lld , &L, &R), d[++tp] = data{L, 1ll, 1ll}, n                                d[++tp] = data{R, -1ll, 1ll}; n   sort(d + 1, d + tp + 1); n   int tg = 0, ch = 0; n   for (int i = 1; i <= tp; ++i) { n     if (i > 1) { n       long long x = d[i].x - d[i - 1].x; n       if (tg == 2) n         v[i] = x, s[i] = 0, is2[i - 1] = true; n       else if (tg == 1) n         v[i] = (ch == 0 ? 1 : 0) * x, s[i] = (ch == 0 ? 1 : -1) * x; n       else n         v[i] = s[i] = 0; n       s[i] += s[i - 1], c[i] = v[i] + c[i - 1]; n     } n     if (d[i].flag > 0) n       tg++, ch = d[i].id; n     else if (d[i].flag < 0) n       tg--, ch = 1 - d[i].id; n     _s[i] = s[i]; n   } n   sort(_s + 1, _s + tp + 1); n   np = unique(_s + 1, _s + tp + 1) - _s - 1; n   for (int i = 0; i < 4; ++i) A[i].init(np); n   for (int i = 0; i <= tp + 1; ++i) dp[i][0] = dp[i][1] = -inf; n   for (int i = 1; i < tp; ++i) { n     if (is2[i]) { n       if (s[i] >= C) n         dp[i][0] = max(dp[i][0], pre - s[i] + C + v[i + 1] * 2); n       else if (s[i] <= -C) n         dp[i][1] = max(dp[i][1], pre + v[i + 1] * 2); n     } n     if (abs(nw) <= C && is2[i]) n       pre += v[i + 1] * 2; n     else n       pre += v[i + 1], nw += s[i + 1] - s[i]; n     if (!is2[i]) continue; n     dp[i][0] = n         max(dp[i][0], n             A[0].query(1, upper_bound(_s + 1, _s + np + 1, s[i]) - _s - 1) + n                 c[i + 1] - s[i] + v[i + 1]); n     dp[i][1] = max( n         dp[i][1], n         A[1].query(lower_bound(_s + 1, _s + np + 1, s[i] + 2 * C) - _s, np) + n             c[i + 1] + v[i + 1]); n     dp[i][0] = max( n         dp[i][0], n         A[2].query(1, upper_bound(_s + 1, _s + np + 1, s[i] - 2 * C) - _s - 1) - n             s[i] + v[i + 1] + c[i + 1] + 2 * C); n     dp[i][1] = max(dp[i][1], n                    A[3].query(lower_bound(_s + 1, _s + np + 1, s[i]) - _s, np) + n                        c[i + 1] + v[i + 1]); n     int l = lower_bound(_s + 1, _s + np + 1, s[i]) - _s, n         r = upper_bound(_s + 1, _s + np + 1, s[i] + 2 * C) - _s - 1; n     if (A[0].s[1] >= (long long)(-2e18)) A[0].mdy(l, r, v[i + 1]); n     if (A[1].s[1] >= (long long)(-2e18)) A[1].mdy(l, r, v[i + 1]); n     l = lower_bound(_s + 1, _s + np + 1, s[i] - 2 * C) - _s, n     r = upper_bound(_s + 1, _s + np + 1, s[i]) - _s - 1; n     if (A[2].s[1] >= (long long)(-2e18)) A[2].mdy(l, r, v[i + 1]); n     if (A[3].s[1] >= (long long)(-2e18)) A[3].mdy(l, r, v[i + 1]); n     int x = lower_bound(_s + 1, _s + np + 1, s[i + 1]) - _s; n     if (dp[i][0] >= 0) n       A[0]._mdy(x, dp[i][0] + s[i + 1] - c[i + 1]), n           A[1]._mdy(x, dp[i][0] - c[i + 1]); n     if (dp[i][1] >= 0) n       A[2]._mdy(x, dp[i][1] + s[i + 1] - c[i + 1]), n           A[3]._mdy(x, dp[i][1] - c[i + 1]); n   } n   ans = max(ans, pre); n   ans = max(ans, A[1].query(1, np) + c[tp]); n   ans = max(ans, A[3].query(1, np) + c[tp]); n   printf( %lld , ans); n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 500002; n const long long mod = 1000000000; n const long double eps = 1e-9; n const long long inf = ((1ll << 31ll) - 1ll); n const long long INF = 2000000000000000000ll; n const long double pi = acos(-1); n long long qpow(long long b, long long e) { n   if (!e) return 1; n   if (e & 1) return qpow(b, e - 1) * b; n   long long pwur = qpow(b, e >> 1); n   return pwur * pwur; n } n long long modinv(long long x) { return qpow(x, mod - 2); } n long long n, a[maxn], lst, psum[maxn], ssum[maxn]; n bool try_(long long id) { n   long long sw = 0; n   for (long long i = id; i <= n; i++) { n     if (psum[i] <= psum[i - id]) return false; n   } n   return true; n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.setf(ios::fixed); n   cout.precision(0); n   srand(time(NULL)); n   cin >> n; n   for (long long i = 1; i <= (n + 1) / 2; i++) { n     cin >> a[i]; n   } n   cin >> lst; n   for (long long i = (n + 1) / 2 + 1; i <= n; i++) { n     a[i] = lst; n   } n   for (long long i = 1; i <= n; i++) psum[i] = psum[i - 1] + a[i]; n   for (long long i = n; i >= 1; i--) ssum[i] = ssum[i + 1] + a[i]; n   if (lst >= 0) { n     if (try_(n)) n       cout << n <<   n ; n     else n       cout << -1 <<   n ; n     return 0; n   } n   long long cnt = 0; n   for (long long i = 1; i <= n; i++) { n     if ((psum[i] > 0 && ssum[n - i + 1] > 0) && (i >= (n + 1) / 2)) { n       if (try_(i)) { n         cout << i <<   n ; n         return 0; n       } else { n         cnt++; n       } n     } n   } n   cout << -1 <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long read() { n   register char ch; n   while (!isdigit(ch = getchar())) n     ; n   register long long x = ch ^  0 ; n   while (isdigit(ch = getchar())) x = (((x << 2) + x) << 1) + (ch ^  0 ); n   return x; n } n vector<pair<long long, long long> > e[200005]; n long long n, m, q, a[200005], b[200005], c[200005], sum[200005 << 2], n     lzy[200005 << 2]; n void build(long long o, long long l, long long r, long long p[]) { n   lzy[o] = 0; n   if (l == r) { n     sum[o] = p[l]; n     return; n   } n   long long mid = l + r >> 1; n   build(o << 1, l, mid, p); n   build(o << 1 | 1, mid + 1, r, p); n   sum[o] = min(sum[o << 1], sum[o << 1 | 1]); n } n void pushdown(long long o) { n   sum[o << 1] += lzy[o]; n   sum[o << 1 | 1] += lzy[o]; n   lzy[o << 1] += lzy[o]; n   lzy[o << 1 | 1] += lzy[o]; n   lzy[o] = 0; n } n void updata(long long o, long long l, long long r, long long L, long long R, n             long long val) { n   if (L == l && r == R) { n     sum[o] += val; n     lzy[o] += val; n     return; n   } n   if (lzy[o]) pushdown(o); n   long long mid = l + r >> 1; n   if (L <= mid) updata(o << 1, l, mid, L, min(mid, R), val); n   if (R > mid) updata(o << 1 | 1, mid + 1, r, max(mid + 1, L), R, val); n   sum[o] = min(sum[o << 1], sum[o << 1 | 1]); n } n signed main() { n   n = read(); n   m = read(); n   q = read(); n   for (long long i = 1; i < n; ++i) { n     a[i] = read(); n     b[i + 1] = read(); n   } n   build(1, 1, n, b); n   for (long long i = 1; i <= m; ++i) { n     long long u, v, w; n     u = read(); n     v = read(); n     w = read(); n     e[u].push_back(make_pair(v, w)); n   } n   for (long long i = 1; i <= n; ++i) { n     for (long long j = 0; j < e[i].size(); ++j) { n       long long v = e[i][j].first, w = e[i][j].second; n       updata(1, 1, n, 1, v, w); n     } n     c[i] = sum[1] + a[i]; n   } n   build(1, 1, n, c); n   printf( %lld n , sum[1]); n   while (q--) { n     long long x, y; n     x = read(); n     y = read(); n     updata(1, 1, n, x, x, y - a[x]); n     a[x] = y; n     printf( %lld n , sum[1]); n   } n   return 0; n } n


#include <bits/stdc++.h> n const int t = 50; n int f[t][t][t], g[t][t]; n long long p[t]; n int cnt; n void mul(int c[t][t], int a[t][t], int b[t][t], int n) { n   int d[t][t]; n   memset(d, 0, sizeof d); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n; j++) n       for (int k = 0; k < n; k++) d[i][k] ^= a[i][j] & b[j][k]; n   memcpy(c, d, sizeof d); n } n bool quick(long long m, int n) { n   int i, j, k, res[t], ans[t]; n   for (i = 0; i < n; i++) ans[i] = 1; n   for (i = 0; m; m >>= 1, i++) n     if (m & 1) { n       memset(res, 0, sizeof res); n       for (j = 0; j < n; j++) n         for (k = 0; k < n; k++) res[k] ^= ans[j] & f[i][j][k]; n       memcpy(ans, res, sizeof ans); n     } n   for (i = 0; i < n && ans[i]; i++) n     ; n   return i == n; n } n bool checker(long long m, int n) { n   if (!quick(m, n)) return false; n   for (int i = 0; i < cnt; i++) n     if (quick(p[i], n)) return false; n   return true; n } n int main() { n   int ans, i, j, n; n   long long s, m; n   srand(0x51ac); n   scanf( %d , &n); n   m = s = (1ll << n) - 1; n   for (cnt = 0, i = 2; i * (long long)i <= s; i++) n     if (s % i == 0) { n       p[cnt++] = m / i; n       for (; s % i == 0; s /= i) n         ; n     } n   if (s != 1) p[cnt++] = m / s; n   do { n     memset(f, 0, sizeof f); n     for (i = 1; i < n; i++) f[0][i][i - 1] = 1; n     for (f[0][0][n - 1] = i = 1; i < n; i++, j >>= 1) n       f[0][i][n - 1] = rand() & 1; n     for (i = 1; i < n; i++) mul(f[i], f[i - 1], f[i - 1], n); n   } while (!checker(m, n)); n   for (i = 0; i < n; i++) printf( %d  , f[0][n - 1 - i][n - 1]); n   for (puts(  ), i = 0; i < n; i++) printf( 1  ); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx ) n using namespace std; n namespace SimplexMethod { n const long double EPS = 1.0E-12; n vector<vector<long double>> a; n vector<long double> b, c, res; n vector<int> N, kt; n int m; n void pivot(int k, int s, int e) { n   int first = kt[s]; n   long double p = a[s][e]; n   for (int _n((k)-1), i(0); i <= _n; i++) a[s][i] /= p; n   b[s] /= p; n   N[e] = false; n   for (int _n((m)-1), i(0); i <= _n; i++) n     if (i != s) { n       b[i] -= a[i][e] * b[s]; n       a[i][first] = -a[i][e] * a[s][first]; n     } n   for (int _n((k)-1), j(0); j <= _n; j++) n     if (N[j]) { n       c[j] -= c[e] * a[s][j]; n       for (int _n((m)-1), i(0); i <= _n; i++) n         if (i != s) a[i][j] -= a[i][e] * a[s][j]; n     } n   kt[s] = e; n   N[first] = true; n   c[first] = -c[e] * a[s][first]; n } n vector<long double> doit(int k) { n   vector<long double> res; n   while (true) { n     int e = -1, s = -1; n     for (int _n((k)-1), i(0); i <= _n; i++) n       if (N[i] && c[i] > EPS) { n         e = i; n         break; n       } n     if (e == -1) break; n     for (int _n((m)-1), i(0); i <= _n; i++) n       if (a[i][e] > EPS && (s == -1 || b[i] / a[i][e] < b[s] / a[s][e])) s = i; n     if (s == -1) return vector<long double>(); n     pivot(k, s, e); n   } n   res.resize(k, 0); n   for (int _n((m)-1), i(0); i <= _n; i++) res[kt[i]] = b[i]; n   return res; n } n void apply_rand(vector<long double>& a, const vector<int>& ind) { n   for (int _n(((int)((a).size())) - 1), i(0); i <= _n; i++) { n     if (i < ind[i]) { n       swap(a[i], a[ind[i]]); n     } n   } n } n long double simplex(vector<vector<long double>> _A, vector<long double> _b, n                     vector<long double> _c) { n   vector<int> ind((int)((_A[0]).size())); n   for (int _n(((int)((ind).size())) - 1), i(0); i <= _n; i++) { n     ind[i] = i; n   } n   srand(std::chrono::duration_cast<std::chrono::nanoseconds>( n             (std::chrono::high_resolution_clock::now().time_since_epoch())) n             .count() + n         time(0)); n   auto rgen = [](int n) -> unsigned { n     return ((unsigned)(rand() << 15) + (unsigned)rand()) % n; n   }; n   random_shuffle(ind.begin(), ind.end(), rgen); n   for (int _n(((int)((_A).size())) - 1), i(0); i <= _n; i++) { n     apply_rand(_A[i], ind); n   } n   apply_rand(_c, ind); n   a = _A; n   b = _b; n   c = _c; n   m = (int)((a).size()); n   int n = (int)((a[0]).size()); n   int k = n + m + 1; n   c.resize(n + m); n   kt.resize(m); n   N = vector<int>(k, true); n   for (int _n((m)-1), i(0); i <= _n; i++) { n     a[i].resize(k); n     a[i][n + i] = 1; n     a[i][k - 1] = -1; n     kt[i] = n + i; n     N[kt[i]] = false; n   } n   int s = min_element((b).begin(), (b).end()) - b.begin(); n   if (b[s] < -EPS) { n     c = vector<long double>(k, 0); n     c[k - 1] = -1; n     pivot(k, s, k - 1); n     res = doit(k); n     if (res[k - 1] > EPS) return -1e100; n     for (int _n((m)-1), i(0); i <= _n; i++) n       if (kt[i] == k - 1) n         for (int _n((k - 1) - 1), j(0); j <= _n; j++) n           if (N[j] && (a[i][j] < -EPS || EPS < a[i][j])) { n             pivot(k, i, j); n             break; n           } n     c = _c; n     c.resize(k, 0); n     for (int _n((m)-1), i(0); i <= _n; i++) n       for (int _n((k)-1), j(0); j <= _n; j++) n         if (N[j]) c[j] -= c[kt[i]] * a[i][j]; n   } n   res = doit(k - 1); n   if (!res.empty()) res.resize(n); n   long double ans = 0.0; n   for (int _n(((int)((res).size())) - 1), i(0); i <= _n; i++) { n     ans += res[i] * _c[i]; n   } n   return ans; n } n }  // namespace SimplexMethod n int d, n, m, k, p, q; n int main() { n   scanf( %d%d%d , &n, &p, &q); n   vector<long double> a, b, c; n   a.resize(n); n   b.resize(n); n   c.resize(n); n   vector<pair<int, int>> tp; n   for (int _n((n)-1), i(0); i <= _n; i++) { n     int first, second; n     scanf( %d%d , &first, &second); n     tp.push_back(make_pair(first, second)); n   } n   for (int _n((n)-1), i(0); i <= _n; i++) { n     int first = tp[i].first, second = tp[i].second; n     a[i] = -first; n     b[i] = -second; n     c[i] = -1.0; n   } n   vector<vector<long double>> A; n   A.push_back(a); n   A.push_back(b); n   vector<long double> B; n   B.push_back(-p); n   B.push_back(-q); n   auto res = -SimplexMethod::simplex(A, B, c); n   printf( %.12lf n , (double)res); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100000 + 10; n int n; n int main() { n   scanf( %d , &n); n   if (n <= 8) { n     int a = 2; n     int b = (n) / a; n     int c = (n) % a; n     for (int i = 1; i <= n;) { n       if (c > 0) { n         printf( %d  , c); n         c--; n         i++; n       } else if (c == 0) { n         for (int j = a - 1; j >= 0; j--) { n           printf( %d  , i + j); n         } n         i += a; n       } n     } n   } else { n     int a; n     for (a = 3; a <= 1000; a++) { n       if (a * a > n) break; n     } n     a--; n     int b = (n) / a; n     int c = n % a; n     for (int i = 1; i <= n;) { n       if (c > 0) { n         printf( %d  , c); n         c--; n         i++; n       } else if (c == 0) { n         for (int j = a - 1; j >= 0; j--) { n           printf( %d  , i + j); n         } n         i += a; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 10; n int n; n int mxm[4 * maxn]; n int get(int id, int L, int R, int l, int r, int x) { n   if (l >= r) return -1; n   if (L == l and R == r) { n     if (mxm[id] < x) return -1; n     if (L + 1 == R) return L; n     int mid = (L + R) >> 1; n     if (mxm[2 * id + 0] >= x) return get(2 * id + 0, L, mid, L, mid, x); n     return get(2 * id + 1, mid, R, mid, R, x); n   } n   int mid = (L + R) >> 1; n   int k = get(2 * id + 0, L, mid, l, min(mid, r), x); n   if (k != -1) return k; n   return get(2 * id + 1, mid, R, max(l, mid), r, x); n } n void change(int id, int L, int R, int idx, int x) { n   if (L + 1 == R) { n     mxm[id] = x; n     return; n   } n   int mid = (L + R) >> 1; n   if (idx < mid) n     change(2 * id + 0, L, mid, idx, x); n   else n     change(2 * id + 1, mid, R, idx, x); n   mxm[id] = max(mxm[2 * id + 0], mxm[2 * id + 1]); n } n long long fen[maxn]; n void add(int x, int val) { n   for (; x <= n; x += x & -x) fen[x] += val; n } n long long get(int x) { n   long long ret = 0; n   for (; x; x -= x & -x) ret += fen[x]; n   return ret; n } n long long get(int L, int R) { return get(R) - get(L - 1); } n int a[maxn]; n int solve() { n   int now = 1; n   while (now < n) { n     long long x = get(1, now - 1); n     if (a[now] == x) return now; n     x += a[now]; n     if (x > int(1e9)) return -1; n     now = get(1, 1, n + 1, now + 1, n + 1, x); n     if (now == -1) return now; n   } n   if (get(1, n - 1) == a[n]) return n; n   return -1; n } n int main() { n   ios_base::sync_with_stdio(false); n   int q; n   cin >> n >> q; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     add(i, a[i]); n     change(1, 1, n + 1, i, a[i]); n   } n   for (int i = 1; i <= q; i++) { n     int idx, x; n     cin >> idx >> x; n     add(idx, x - a[idx]); n     a[idx] = x; n     change(1, 1, n + 1, idx, a[idx]); n     cout << solve() <<   n ; n   } n } n


#include <bits/stdc++.h> n char ch; n bool fs; n void re(int& x) { n   while (ch = getchar(), ch < 33) n     ; n   if (ch ==  - ) n     fs = 1, x = 0; n   else n     fs = 0, x = ch - 48; n   while (ch = getchar(), ch > 33) x = x * 10 + ch - 48; n   if (fs) x = -x; n } n using namespace std; n pair<int, int> d[62]; n int n; n int m, a[32], use[32][64][2048]; n bool f[32][64][2048], edge[64][64]; n int main() { n   re(m); n   for (int i = 1; i <= m; ++i) re(a[i]); n   sort(a + 1, a + m + 1); n   f[0][0][0] = 1; n   for (int i = 1; i <= m; ++i) n     for (int j = i; j <= 61; ++j) n       for (int k = j * (j - 1) / 2; k <= 2000; ++k) n         for (int l = 1; l <= j && l * a[i] <= k; ++l) n           if (f[i - 1][j - l][k - l * a[i]]) { n             f[i][j][k] = 1; n             use[i][j][k] = l; n             break; n           } n   for (int i = 1; i <= 61; ++i) n     if (f[m][i][i * (i - 1) / 2]) { n       printf( %d n , i); n       int a = m, b = i, c = i * (i - 1) / 2; n       while (a) { n         int t = use[a][b][c]; n         for (int j = 1; j <= t; ++j) d[++n].first = ::a[a], d[n].second = n; n         c -= t * ::a[a]; n         b -= t; n         --a; n       } n       for (int j = 1; j <= n; ++j) { n         sort(d + j, d + n + 1); n         for (int k = j + 1; k <= j + d[j].first; ++k) n           edge[d[j].second][d[k].second] = 1; n         for (int k = j + d[j].first + 1; k <= n; ++k) n           edge[d[k].second][d[j].second] = 1, --d[k].first; n         d[j].first = 0; n       } n       for (int j = 1; j <= n; ++j, puts(  )) n         for (int k = 1; k <= n; ++k) printf( %d , edge[j][k]); n       return 0; n     } n   puts( -1 ); n } n