#include <bits/stdc++.h> n using namespace std; n const int MAXN = (1 << 20) + 50; n const int MAXLOG = 21; n long long int a[MAXN]; n long long int data[MAXLOG][MAXN]; n long long int answer[2][MAXN]; n long long int n; n void build(int s = 0, int e = (1 << n), int h = 0) { n   if (e - s < 2) { n     data[h][s] = a[s]; n     return; n   } n   int mid = (s + e) / 2; n   build(s, mid, h + 1); n   build(mid, e, h + 1); n   merge(data[h + 1] + s, data[h + 1] + mid, data[h + 1] + mid, data[h + 1] + e, n         data[h] + s); n   for (int i = s; i < mid; i++) { n     long long int now = lower_bound(data[h + 1] + mid, data[h + 1] + e, a[i]) - n                         (data[h + 1] + mid); n     answer[0][h] += now; n   } n   for (int i = mid; i < e; i++) { n     long long int now = lower_bound(data[h + 1] + s, data[h + 1] + mid, a[i]) - n                         (data[h + 1] + s); n     answer[1][h] += now; n   } n } n int main() { n   scanf( %I64d , &n); n   for (int i = 0; i < (1 << n); i++) { n     scanf( %I64d , a + i); n   } n   build(); n   long long int q; n   scanf( %I64d , &q); n   while (q--) { n     long long int ans = 0; n     long long int cur; n     scanf( %I64d , &cur); n     for (int i = n - cur; i < n; ++i) { n       swap(answer[0][i], answer[1][i]); n     } n     for (int i = 0; i < n; i++) { n       ans += answer[0][i]; n     } n     printf( %I64d n , ans); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200005; n int n, a[N], cnt[N], sum[N]; n vector<int> pos[N]; n set<int> S; n int main() { n   scanf( %d , &n); n   for (int i = (1); i <= (n); ++i) n     scanf( %d , &a[i]), ++cnt[a[i]], pos[a[i]].push_back(i); n   int tt = 0, mx = 0; n   for (int i = (1); i <= (n); ++i) { n     if (cnt[i] > cnt[mx]) { n       mx = i; n       tt = 0; n     } n     if (cnt[i] == cnt[mx]) { n       ++tt; n     } n   } n   if (tt > 1) { n     printf( %d n , n); n     return 0; n   } n   for (int i = (1); i <= (n + 1); ++i) n     sum[i] = sum[i - 1] + (a[i] == mx), a[i] == mx && (S.insert(i), 0); n   int ans = 0; n   for (int i = (1); i <= (n); ++i) n     if (i != mx && ((int)(pos[i]).size())) { n       vector<int> v; n       for (auto x : pos[i]) { n         { n           auto it = S.lower_bound(x); n           if (it != S.begin()) { n             v.push_back(*prev(it)); n             S.erase(prev(it)); n           } n           if (it != S.end()) { n             v.push_back(*it); n             S.erase(*it); n           } n         } n         { n           auto it = S.lower_bound(x); n           if (it != S.begin()) { n             v.push_back(*prev(it)); n             S.erase(prev(it)); n           } n           if (it != S.end()) { n             v.push_back(*it); n             S.erase(*it); n           } n         } n       } n       vector<int> t(v); n       for (auto x : pos[i]) v.push_back(x); n       { n         auto it = S.lower_bound(*min_element(v.begin(), v.end())); n         if (it != S.begin()) { n           --it; n           v.push_back(*it); n         } n         v.push_back(0); n         it = S.upper_bound(*max_element(v.begin(), v.end())); n         if (it != S.end()) { n           v.push_back(*it); n         } n         v.push_back(n + 1); n       } n       sort(v.begin(), v.end()); n       map<int, int> fi; n       fi[sum[v[0]]] = v[0]; n       int s = 0; n       for (int j = (1); j <= (((int)(v).size()) - 1); ++j) { n         if (fi.count(sum[v[j] - 1] + s)) { n           ans = max(ans, v[j] - 1 - fi[sum[v[j] - 1] + s]); n         } n         if (a[v[j]] == i) --s; n         if (!fi.count(sum[v[j]] + s)) fi[sum[v[j]] + s] = v[j]; n       } n       for (auto x : t) S.insert(x); n     } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k; n long long a[1 << 24], dp[1 << 24], b[2]; n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) cin >> a[1 << i]; n   cin >> k; n   for (int i = 0; i < k; i++) cin >> b[i]; n   for (int i = 1; i < (1 << n); i++) { n     long long s = 0; n     int t = i; n     while (t) { n       s += (a[t & (-t)]); n       t -= t & (-t); n     } n     for (int j = 0; j < k; j++) { n       if (s == b[j]) { n         dp[i] = -1; n         break; n       } n     } n   } n   dp[0] = 1; n   for (int i = 1; i < (1 << n); i++) { n     if (dp[i] == -1) { n       dp[i] = 0; n       continue; n     } n     for (int j = 0; j < n; j++) { n       if (1 & (i >> j)) { n         dp[i] += dp[i ^ (1 << j)]; n       } n     } n     if (dp[i] >= 1000000007ll) dp[i] %= 1000000007ll; n   } n   cout << dp[(1 << n) - 1] << endl; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma GCC optimize( O3 ) n #pragma GCC optimize( no-stack-protector ) n #pragma GCC optimize( unroll-loops ) n #pragma GCC optimize( fast-math ) n #pragma GCC target( sse,sse2,sse3,ssse3,popcnt,abm,mmx,tune=native ) n mt19937 rng(time(NULL)); n const int inf = 1e9 + 100; n const long long linf = 2e18; n const long long maxn = 2e5 + 100; n const int mod = 1e9 + 7; n vector<int> g[maxn]; n int c[maxn]; n bool f = 1; n void dfs(int v, int cl) { n   c[v] = cl; n   for (int i : g[v]) { n     if (c[i] == 0) n       dfs(i, cl == 1 ? 2 : 1); n     else if (c[i] == cl) n       f = 0; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int n, m; n   cin >> n >> m; n   vector<pair<int, int> > ans(m); n   for (int i = 0; i < m; ++i) { n     cin >> ans[i].first >> ans[i].second; n     g[ans[i].first - 1].push_back(ans[i].second - 1); n     g[ans[i].second - 1].push_back(ans[i].first - 1); n   } n   for (int i = 0; i < n; ++i) n     if (!c[i]) dfs(i, 1); n   if (f) { n     cout <<  YES n ; n     for (auto i : ans) { n       if (c[i.first - 1] == 2) n         cout << 1; n       else n         cout << 0; n     } n   } else n     cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void wldd() { n   long long n, a, b; n   cin >> n >> a >> b; n   b = b % n; n   long long sum = 0; n   if (b < 0) { n     sum = n + a + b; n   } else { n     sum = a + b; n   } n   if (sum % n == 0) n     cout << n <<   n ; n   else n     cout << abs(sum % n) <<   n ; n } n int32_t main() { n   wldd(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int sgn(double x) { return (x > 1e-8) - (x < -1e-8); } n int count_bit(int x) { return x == 0 ? 0 : count_bit(x >> 1) + (x & 1); } n template <class T> n inline void to_min(T &a, const T b) { n   if (b < a) a = b; n } n template <class T> n inline void to_max(T &a, const T b) { n   if (b > a) a = b; n } n const int maxn = 200000 + 10; n int n, a[3], w[maxn]; n bool used[maxn]; n int rearray(int n) { n   int newn = 0; n   for (int i = 0; i < (n); ++i) { n     if (!used[i]) { n       w[newn++] = w[i]; n     } n   } n   return newn; n } n int solve() { n   int r = a[2] + a[1] + a[0]; n   if (r < w[n - 1]) { n     return -1; n   } n   int ret = 0; n   int l = a[2] + a[1]; n   while (n >= 0 && w[n - 1] > l) { n     ++ret; n     --n; n   } n   r = l; n   l = a[2] + a[0]; n   memset(used, 0, sizeof(used)); n   int max_idx = upper_bound(w, w + n, a[0]) - w - 1; n   while (n >= 0 && w[n - 1] > l) { n     ++ret; n     --n; n     if (max_idx >= 0) { n       used[max_idx--] = true; n     } n   } n   n = rearray(n); n   r = l; n   l = max(a[2], a[1] + a[0]); n   memset(used, 0, sizeof(used)); n   max_idx = upper_bound(w, w + n, a[1]) - w - 1; n   while (n >= 0 && w[n - 1] > l) { n     ++ret; n     --n; n     if (max_idx >= 0) { n       used[max_idx--] = true; n     } n   } n   n = rearray(n); n   if (a[1] + a[0] > a[2]) { n     r = a[1] + a[0]; n     l = a[2]; n     memset(used, 0, sizeof(used)); n     max_idx = upper_bound(w, w + n, a[2]) - w - 1; n     while (n >= 0 && w[n - 1] > l) { n       ++ret; n       --n; n       if (max_idx >= 0) { n         used[max_idx--] = true; n       } n     } n     n = rearray(n); n   } n   memset(used, 0, sizeof(used)); n   int idx_3 = n - 1; n   int idx_2 = upper_bound(w, w + n, a[1]) - w; n   int idx_1 = upper_bound(w, w + n, a[1]) - w - 1; n   int idx_0 = 0; n   while (true) { n     while (idx_3 >= 0 && used[idx_3]) --idx_3; n     if (idx_3 < 0) break; n     used[idx_3--] = true; n     ++ret; n     if (idx_1 >= idx_0) { n       while (idx_1 >= idx_0 && used[idx_1]) --idx_1; n       if (idx_1 >= idx_0) { n         used[idx_1--] = true; n       } n       while (idx_0 <= idx_1 && w[idx_0] <= a[0] && used[idx_0]) ++idx_0; n       if (idx_0 <= idx_1 && w[idx_0] <= a[0]) { n         used[idx_0++] = true; n       } n     } else { n       if (!used[idx_2] && w[idx_2] <= a[0] + a[1]) { n         used[idx_2++] = true; n       } n     } n   } n   return ret; n } n int main() { n   ios::sync_with_stdio(false); n   cout << fixed << setprecision(8); n   cin >> n; n   for (int i = 0; i < (3); ++i) { n     cin >> a[i]; n   } n   sort(a, a + 3); n   for (int i = 0; i < (n); ++i) { n     cin >> w[i]; n   } n   sort(w, w + n); n   cout << solve() << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int main() { n   scanf( %d , &n); n   printf( +------------------------+ n ); n   printf( | ); n   if (n >= 1) n     printf( O. ); n   else n     printf( #. ); n   for (int i = 2; i <= 11; i++) n     if ((i - 2) * 3 + 4 + 1 <= n) n       printf( O. ); n     else n       printf( #. ); n   printf( |D|) n ); n   printf( | ); n   if (n >= 2) n     printf( O. ); n   else n     printf( #. ); n   for (int i = 2; i <= 11; i++) n     if ((i - 2) * 3 + 4 + 2 <= n) n       printf( O. ); n     else n       printf( #. ); n   printf( |.| n ); n   if (n >= 3) n     printf( |O.......................| n ); n   else n     printf( |#.......................| n ); n   printf( | ); n   if (n >= 4) n     printf( O. ); n   else n     printf( #. ); n   for (int i = 2; i <= 11; i++) n     if ((i - 2) * 3 + 4 + 3 <= n) n       printf( O. ); n     else n       printf( #. ); n   printf( |.|) n ); n   printf( +------------------------+ n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n T gcd(T a, T b) { n   if (!b) return a; n   while (b ^= a ^= b ^= a %= b) n     ; n   return a; n } n template <class T> n T max(const T& t1, const T& t2, const T& t3) { n   return max(max(t1, t2), t3); n } n template <class T> n T max(const T& t1, const T& t2, const T& t3, const T& t4) { n   return max(max(t1, t2), max(t3, t4)); n } n long long fpow(long long x, long long y, long long mod = 0x7fffffffffffffffll) { n   long long ret = 1; n   while (y) { n     if (y & 1) ret *= x, ret %= mod; n     x *= x; n     x %= mod; n     y >>= 1; n   } n   return ret; n } n void ex_gcd(long long a, long long b, long long& x, long long& y) { n   if (b == 0) { n     x = a; n     y = 0; n     return; n   } n   ex_gcd(b, a % b, y, x); n   y = y - a / b * x; n } n long long modrev(long long x, long long mod) { n   long long x1, x2; n   ex_gcd(x, mod, x1, x2); n   return x1; n } n unsigned long long T_rand() { return (unsigned long long)(rand()); } n unsigned long long Rand() { n   return (T_rand() << 48) | (T_rand() << 32) | (T_rand() << 16) | T_rand(); n } n template <class T> n bool PRE_INPUT_INTENGER(T& ret) { n   ; n   ret = 0; n   char c = getchar(); n   if (c == -1) return 0; n   bool f = 0; n   while ((c <  0  || c >  9 ) && (c != -1) && (c !=  - )) c = getchar(); n   if (c ==  - ) { n     f = 1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) ret *= 10, ret += c -  0 , c = getchar(); n   if (f) ret = -ret; n   if (c == -1) return 0; n   return 1; n } n bool input(int& ret) { return PRE_INPUT_INTENGER(ret); } n bool input(long long& ret) { return PRE_INPUT_INTENGER(ret); } n bool input(unsigned long long& ret) { return PRE_INPUT_INTENGER(ret); } n bool input(unsigned int& ret) { return PRE_INPUT_INTENGER(ret); } n template <class T> n bool PRE_INPUT_DOUBLE(T& ret) { n   ; n   ret = 0; n   char c = getchar(); n   if (c == -1) return 0; n   bool f = 0; n   while ((c <  0  || c >  9 ) && (c != -1) && (c !=  . ) && (c !=  - )) n     c = getchar(); n   bool isright = 0; n   if (c ==  - ) { n     f = 1; n     c = getchar(); n   } n   T t = 1; n   while ((c >=  0  && c <=  9 ) || (c ==  . )) { n     if (c ==  . ) n       isright = 1; n     else { n       ret *= 10, ret += c -  0 ; n       if (isright) t *= 10; n     } n     c = getchar(); n   } n   ret /= t; n   if (f) ret = -ret; n   if (c == -1) return 0; n   return 1; n } n bool input(double& ret) { return PRE_INPUT_DOUBLE(ret); } n bool input(float& ret) { return PRE_INPUT_DOUBLE(ret); } n bool IS_SPECIAL_CHAR(const char& ret) { n   return (ret ==     || ret ==   n  || ret ==   t  || ret ==   r  || n           ret ==   v ); n } n bool input(char& ret) { n   ; n   ret = getchar(); n   if (ret == -1) return 0; n   while (IS_SPECIAL_CHAR(ret)) ret = getchar(); n   return ~ret; n } n bool input(char* ret) { n   ; n   char c; n   if (!input(c)) return 0; n   *ret++ = c; n   while (!IS_SPECIAL_CHAR(*(ret - 1))) *ret++ = getchar(); n   *(ret - 1) = 0; n   return 1; n } n template <class T1, class T2> n bool input(T1& t1, T2& t2) { n   return input(t1) && input(t2); n } n template <class T1, class T2, class T3> n bool input(T1& t1, T2& t2, T3& t3) { n   return input(t1) && input(t2) && input(t3); n } n template <class T1, class T2, class T3, class T4> n bool input(T1& t1, T2& t2, T3& t3, T4& t4) { n   return input(t1) && input(t2) && input(t3) && input(t4); n } n template <class T1, class T2, class T3, class T4, class T5> n bool input(T1& t1, T2& t2, T3& t3, T4& t4, T5& t5) { n   return input(t1) && input(t2) && input(t3) && input(t4) && input(t5); n } n template <class T> n void input(T* a, int n) { n   for (int i = 0; i <= n - 1; i++) input(a[i]); n } n template <class T> n void PRE_OUTPUT_INTENGER(const T& t) { n   ; n   if (!t) { n     putchar( 0 ); n     return; n   } n   T key = t; n   int tt[25]; n   if (key < 0) { n     putchar( - ); n     key = -key; n   } n   int cnt = 0; n   while (key) tt[cnt++] = key % 10, key /= 10; n   while (cnt) putchar(tt[--cnt] +  0 ); n   ; n } n void output(const int& t) { PRE_OUTPUT_INTENGER(t); } n void output(const long long& t) { PRE_OUTPUT_INTENGER(t); } n void output(const unsigned long long& t) { PRE_OUTPUT_INTENGER(t); } n void output(const unsigned int& t) { PRE_OUTPUT_INTENGER(t); } n template <class T> n void PRE_OUTPUT_DOUBLE(const T& key) { n   ; n   printf( %.*f , 6, key); n   ; n } n void output(const double& t) { PRE_OUTPUT_DOUBLE(t); } n void output(const float& t) { PRE_OUTPUT_DOUBLE(t); } n void output(const char* ch) { n   ; n   int cnt = 0; n   while (ch[cnt]) putchar(ch[cnt++]); n   ; n } n void output(const char& c) { n   ; n   putchar(c); n   ; n } n void output(const string& s) { output(s.c_str()); } n template <class T1, class T2> n void output(const T1& t1, const T2& t2) { n   output(t1); n   output(t2); n } n template <class T1, class T2, class T3> n void output(const T1& t1, const T2& t2, const T3& t3) { n   output(t1); n   output(t2); n   output(t3); n } n template <class T1, class T2, class T3, class T4> n void output(const T1& t1, const T2& t2, const T3& t3, const T4& t4) { n   output(t1); n   output(t2); n   output(t3); n   output(t4); n } n template <class T1, class T2, class T3, class T4, class T5> n void output(const T1& t1, const T2& t2, const T3& t3, const T4& t4, n             const T5& t5) { n   output(t1); n   output(t2); n   output(t3); n   output(t4); n   output(t5); n } n template <class T1, class T2, class T3, class T4, class T5, class T6> n void output(const T1& t1, const T2& t2, const T3& t3, const T4& t4, n             const T5& t5, const T6& t6) { n   output(t1); n   output(t2); n   output(t3); n   output(t4); n   output(t5); n   output(t6); n } n template <class T1, class T2, class T3, class T4, class T5, class T6, class T7> n void output(const T1& t1, const T2& t2, const T3& t3, const T4& t4, n             const T5& t5, const T6& t6, const T7& t7) { n   output(t1); n   output(t2); n   output(t3); n   output(t4); n   output(t5); n   output(t6); n   output(t7); n } n template <class T> n void printal(T* a, int n) { n   for (int i = 0; i < n - 1; i++) output(a[i],    ); n   output(a[n - 1],   n ); n } n template <class T> n void printel(T* a, int n) { n   for (int i = 0; i < n; i++) output(a[i],   n ); n } n template <class T> n void inputal(T* a, int n) { n   for (int i = 0; i <= n - 1; i++) input(a[i]); n } n const int maxn = 1e5 + 10; n struct Node { n   string s; n   int cnt; n   char last; n   bool operator<(const Node& t) const { n     if (cnt < t.cnt) return true; n     if (last < t.last) return true; n     return false; n   } n   bool operator==(const Node& t) const { n     return cnt == t.cnt && last == t.last; n   } n }; n Node node[maxn]; n const char* vowel =  aeiou ; n bool is_vowel(char c) { n   switch (c) { n     case  a : n     case  e : n     case  i : n     case  o : n     case  u : n       return true; n     default: n       return false; n   } n } n map<int, map<char, vector<int>>> mp; n int ans[4 * maxn]; n int main() { n   int n; n   input(n); n   for (int i = 0; i <= n - 1; i++) { n     cin >> node[i].s; n     node[i].cnt = 0; n     for (const char& c : node[i].s) { n       if (is_vowel(c)) { n         ++node[i].cnt; n         node[i].last = c; n       } n     } n     if (mp.count(node[i].cnt) == 0) { n       mp[node[i].cnt] = map<char, vector<int>>(); n     } n     if (mp[node[i].cnt].count(node[i].last) == 0) { n       mp[node[i].cnt][node[i].last] = vector<int>(); n     } n     mp[node[i].cnt][node[i].last].push_back(i); n   } n   vector<pair<int, int>> has_same; n   map<int, vector<int>> single; n   for (const pair<int, map<char, vector<int>>>& t : mp) { n     for (const pair<char, vector<int>>& t2 : t.second) { n       for (int i = 0; i < t2.second.size(); i += 2) { n         if (i == t2.second.size() - 1) { n           if (single.count(t.first) == 0) { n             single[t.first] = vector<int>(); n           } n           single[t.first].push_back(t2.second[i]); n         } else { n           has_same.push_back(make_pair(t2.second[i], t2.second[i + 1])); n         } n       } n     } n   } n   int cnt = 0; n   int last_double_idx = 0; n   for (const pair<int, vector<int>>& t : single) { n     int last_single_idx = 0; n     if (last_double_idx == has_same.size()) break; n     while (t.second.size() - last_single_idx > 1) { n       if (last_double_idx == has_same.size()) break; n       pair<int, int>& cur = has_same[last_double_idx++]; n       ans[cnt++] = t.second[last_single_idx++]; n       ans[cnt++] = cur.first; n       ans[cnt++] = t.second[last_single_idx++]; n       ans[cnt++] = cur.second; n     } n   } n   while (last_double_idx + 1 < has_same.size()) { n     ans[cnt++] = has_same[last_double_idx].first; n     ans[cnt++] = has_same[last_double_idx + 1].first; n     ans[cnt++] = has_same[last_double_idx].second; n     ans[cnt++] = has_same[last_double_idx + 1].second; n     last_double_idx += 2; n   } n   output(cnt / 4); n   putchar(  n ); n   for (int i = 0; i < cnt; i += 2) { n     output(node[ans[i]].s,    , node[ans[i + 1]].s); n     putchar(  n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, x, y; n   int a[1005], b[1005], c[1005], d[1005]; n   cin >> n >> m >> x >> y; n   for (int i = 1; i <= m; i++) { n     cin >> a[i] >> b[i] >> c[i] >> d[i]; n   } n   int dp[1005][11]; n   int temp, i, j, k, t; n   for (int i = 0; i <= n; i++) { n     dp[i][0] = 0; n   } n   for (i = 1; i <= n; i++) { n     for (j = 1; j <= m; j++) { n       temp = -1; n       t = a[j] / b[j]; n       for (k = 0; k <= t; k++) n         if (k * c[j] <= i) { n           temp = max(temp, dp[i - k * c[j]][j - 1] + k * d[j]); n         } n       dp[i][j] = temp; n     } n   } n   int Max = -1; n   for (int l = 0; l <= n; l++) { n     Max = max(Max, dp[l][m] + ((n - l) / x) * y); n   } n   cout << Max << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     long long int a, b, ans = 0; n     cin >> a >> b; n     ans = min(a, b); n     ans = min(ans, (a + b) / 3); n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   long long x; n   int c; n   friend bool operator<(node x, node y) { return x.x < y.x; } n } a[400001]; n long long leftdata[400001], rightdata[400001]; n int n; n long long ans; n long long max1, max2, l1, r1, ttt; n void work1() { n   long long tans; n   l1 = 0; n   for (int i = 1; i <= n; ++i) n     if (a[i].c == 0) { n       tans = ans; n       if (l1 != 0) ans += a[i].x - l1; n       l1 = a[i].x; n     } n   l1 = 0; n   for (int i = 1; i <= n; ++i) n     if (a[i].c == 1) { n       tans = ans; n       if (l1 != 0) ans += a[i].x - l1; n       l1 = a[i].x; n     } n   cout << ans << endl; n } n void work2() { n   long long tans; n   long long f1 = 0, f2 = 0; n   int sum = 0; n   for (int i = 1; i <= n; ++i) { n     if (a[i].c == 2) { n       max2 = max(a[i].x - r1, max2); n       max1 = max(a[i].x - l1, max1); n       sum++; n       if (sum == 1) { n         tans = ans; n         if (f1 != 0) ans = ans + a[i].x - f1; n         if (f2 != 0) ans = ans + a[i].x - f2; n       } else { n         long long t1 = a[i].x + a[i].x - ttt - ttt; n         long long t2 = n             (a[i].x + a[i].x - ttt - ttt) - max1 + (a[i].x - ttt) - max2; n         long long yzc = min(t1, t2); n         ans = ans + yzc; n       } n       l1 = r1 = ttt = a[i].x; n       max1 = 0; n       max2 = 0; n     } else if (a[i].c == 0) { n       if (f1 == 0) f1 = a[i].x; n       if (max1 < a[i].x - l1) max1 = a[i].x - l1; n       l1 = a[i].x; n     } else { n       if (f2 == 0) f2 = a[i].x; n       if (max2 < a[i].x - r1) max2 = a[i].x - r1; n       r1 = a[i].x; n     } n   } n   f1 = 0; n   f2 = 0; n   for (int i = n; i >= 1; --i) { n     if (a[i].c == 2) { n       tans = ans; n       if (f1 != 0) ans = ans + f1 - a[i].x; n       if (f2 != 0) ans = ans + f2 - a[i].x; n       break; n     } else if (a[i].c == 0) { n       if (f1 == 0) f1 = a[i].x; n     } else { n       tans = ans; n       if (f2 == 0) f2 = a[i].x; n     } n   } n   cout << ans << endl; n } n int main() { n   scanf( %d , &n); n   char tmp[123]; n   int sumx = 0; n   for (int i = 1; i <= n; ++i) { n     long long x; n     scanf( %lld , &x); n     scanf( %s , tmp); n     a[i].x = x; n     if (tmp[0] ==  R ) n       a[i].c = 0; n     else if (tmp[0] ==  B ) n       a[i].c = 1; n     else n       a[i].c = 2, sumx++; n   } n   sort(a + 1, a + n + 1); n   ans = max1 = max2 = l1 = r1 = ttt = 0; n   if (sumx == 0) n     work1(); n   else n     work2(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, d, h; n vector<pair<int, int> > ans; n int main() { n   ios::sync_with_stdio(0); n   cin >> n >> d >> h; n   if (2 * h < d || (d == 1 && n > 2)) { n     cout << -1 <<   n ; n     return 0; n   } n   for (int i = 2; i <= h + 1; i++) ans.push_back(make_pair(i - 1, i)); n   int idx = 1; n   bool done = false; n   for (int i = h + 2; i <= d + 1; i++) { n     ans.push_back(make_pair(idx, i)); n     done = true; n     idx = i; n   } n   if (done) n     idx = 1; n   else n     idx = 2; n   for (int i = d + 2; i <= n; i++) ans.push_back(make_pair(idx, i)); n   for (int i = 0; i < ans.size(); i++) n     cout << ans[i].first <<     << ans[i].second <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int i, j, k, n, m, t, l; n   cin >> t; n   while (t--) { n     string s, p, q =  abacaba , r; n     cin >> n >> s; n     int cnt = 0, cntt = 0; n     p = s; n     r = s; n     for (i = 0; i < n; i++) { n       int c = 0; n       for (j = i, k = 0; j < i + 7 && j < n; j++) { n         if (q[k] == s[j]) { n           k++; n           c++; n         } else { n           k = 0; n           break; n         } n       } n       if (c == 7) cnt++; n       if (s[i] ==  ? ) cntt++; n     } n     if (cnt > 1) n       cout <<  No  << endl; n     else if (cntt == 0 && cnt == 0) n       cout <<  No  << endl; n     else { n       int ans = 0; n       if (cnt == 0) { n         for (i = 0; i + 6 < n; i++) { n           bool f = 1; n           s = p; n           for (j = 0; j < i; j++) { n             if (s[j] ==  ? ) s[j] =  x ; n           } n           for (j = 0; j < 7; j++) { n             if (s[j + i] != q[j] && s[j + i] !=  ? ) { n               f = 0; n               break; n             } else if (s[j + i] ==  ? ) n               s[j + i] = q[j]; n           } n           if (f == 0) continue; n           int con = 0; n           if (f == 1) { n             for (l = 0; l + 6 < n; l++) { n               int ok = 1; n               for (j = 0; j < 7; j++) { n                 if (s[j + l] != q[j]) { n                   ok = 0; n                   break; n                 } n               } n               con += ok; n             } n             if (con == 1) { n               cout <<  Yes  << endl; n               for (i = 0; i < n; i++) n                 if (s[i] ==  ? ) s[i] =  x ; n               cout << s << endl; n               ans = 1; n               break; n             } else n               continue; n           } n         } n         if (ans == 0) cout <<  No  << endl; n       } else if (cnt == 1) { n         for (i = 0; i < n; i++) { n           if (s[i] ==  ? ) s[i] =  x ; n         } n         cout <<  Yes  << endl; n         cout << s << endl; n       } else n         cout <<  No  << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long d, n, m; n   cin >> d >> n >> m; n   vector<pair<long long, long long>> V(m); n   for (auto &x : V) cin >> x.first >> x.second; n   sort(V.begin(), V.end()); n   bool poss = true; n   for (int i = 0; i < V.size() - 1; i++) n     poss = poss && V[i].first + n >= V[i + 1].first; n   poss = poss && V[0].first <= n; n   poss = poss && d - V.back().first <= n; n   if (!poss) { n     printf( -1 n ); n     return 0; n   } n   sort(V.begin(), V.end(), n        [](pair<long long, long long> a, pair<long long, long long> b) { n          return a.second < b.second; n        }); n   set<int> pos; n   pos.insert(0); n   pos.insert(d); n   long long ans = 0; n   for (auto &x : V) { n     auto aft = pos.lower_bound(x.first); n     auto bef = prev(aft); n     long long reach = max(0ll, n - (x.first - *bef)); n     long long leave = min(n, *aft - x.first); n     ans += x.second * max(0ll, leave - reach); n     pos.insert(x.first); n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 3e5, N = 2e6 + 90, SQ = 300, base = 1999, mod = 1e9 + 7, n                 INF = 1e9 + 1, lg = 30; n const long double eps = 1e-4; n long long n, a[maxn], b[maxn], c[maxn], d[maxn], cnt[maxn], s1, s2, tot; n inline bool check() { n   for (long long i = 0; i < n; ++i) { n     for (long long j = 0; j < lg; ++j) { n       if (a[i] & (1ll << j)) { n         cnt[j]++; n       } n     } n   } n   for (long long i = 0; i < n; ++i) { n     long long res1 = 0, res2 = 0; n     for (long long j = 0; j < lg; ++j) { n       if (a[i] & (1ll << j)) { n         res1 += (cnt[j] * (1ll << j)); n         res2 += (n - cnt[j]) * (1ll << j); n       } n       res2 += (cnt[j] * (1ll << j)); n     } n     if (res1 != b[i] || res2 != c[i]) { n       return 0; n     } n   } n   return 1; n } n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> n; n   for (long long i = 0; i < n; ++i) { n     cin >> b[i]; n     s1 += b[i]; n   } n   for (long long i = 0; i < n; ++i) { n     cin >> c[i]; n     s2 += c[i]; n   } n   if ((s1 + s2) % (2 * n) != 0) { n     cout << -1 <<   n ; n     return 0; n   } n   tot = (s1 + s2) / (2 * n); n   for (long long i = 0; i < n; ++i) { n     d[i] = b[i] + c[i]; n   } n   for (long long i = 0; i < n; ++i) { n     a[i] = (d[i] - tot) / n; n   } n   if (check()) { n     for (long long i = 0; i < n; ++i) { n       cout << a[i] <<    ; n     } n     cout <<   n ; n   } else { n     cout << -1 <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t) { n     t--; n     int a, b, c, d; n     cin >> a >> b >> c >> d; n     cout << b <<     << c <<     << c <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, k, l, mx = 0, m, i, j, s = 0; n   cin >> n >> k; n   long long int a[n], t[n], sum[n], dv[n]; n   for (i = 0; i < n; i++) { n     cin >> a[i]; n     s += a[i]; n     dv[i] = s; n   } n   s = 0; n   for (i = 0; i < n; i++) { n     cin >> t[i]; n     if (t[i] == 1) s += a[i]; n     sum[i] = s; n   } n   l = n - k + 1; n   for (i = 0; i < l; i++) { n     m = 0; n     if (i > 0) n       m += dv[i + k - 1] - dv[i - 1]; n     else n       m += dv[i + k - 1]; n     if (i > 0) m += sum[i - 1]; n     m += sum[n - 1] - sum[i + k - 1]; n     if (m > mx) mx = m; n   } n   cout << mx << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e9; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int n, m; n   cin >> n >> m; n   int sum = 0; n   vector<int> a(n); n   map<int, int> c; n   c[0] = 1; n   bool fl = 0; n   long long ans = 0; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     if (a[i] > m) sum++; n     if (a[i] < m) sum--; n     if (a[i] == m) fl = 1; n     if (fl) n       ans += c[sum] + c[sum - 1]; n     else n       c[sum]++; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e6 + 3; n bool in[113]; n bool odw[113]; n bool prze[113]; n vector<int> v[113]; n string tab[MAXN]; n vector<char> ans; n bool por = false; n void DFS(int x) { n   prze[x] = true; n   odw[x] = true; n   for (auto it : v[x]) { n     if (prze[it]) n       por = true; n     else if (!odw[it]) n       DFS(it); n   } n   ans.push_back(char(x +  a )); n   prze[x] = false; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   int n, k, p; n   bool check; n   cin >> n >> k; n   for (int i = 1; i <= n; i++) { n     cin >> p; n     for (int j = 0; j < k; j++) { n       cin >> tab[p * k + j]; n       for (auto &it : tab[p * k + j]) in[it -  a ] = true; n     } n   } n   for (int i = 1; i < n * k; i++) { n     check = false; n     for (unsigned int j = 0; j < min(tab[i].size(), tab[i - 1].size()); j++) { n       if (tab[i][j] != tab[i - 1][j]) { n         check = true; n         v[tab[i - 1][j] -  a ].push_back(tab[i][j] -  a ); n         break; n       } n     } n     if (!check && tab[i - 1].size() > tab[i].size()) por = true; n   } n   for (int i = 0; i < 26; i++) { n     if (!odw[i] && in[i]) DFS(i); n   } n   if (por) n     cout <<  IMPOSSIBLE ; n   else { n     for (int i = (int)(ans.size()) - 1; i >= 0; i--) cout << ans[i]; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int Maxn = 200005; n int n, m; n long long ans[Maxn]; n struct Segment { n   long long l, r; n   int pos; n } A[Maxn], land[Maxn]; n bool cmp(const Segment &a, const Segment &b) { n   return a.l < b.l || a.l == b.l && a.r < b.r; n } n struct node { n   int pos; n   long long val; n   node() {} n   node(int _pos, long long _val) { n     pos = _pos; n     val = _val; n   } n } p[Maxn]; n bool operator<(node a, node b) { return a.val > b.val; } n bool cmp2(const node &a, const node &b) { return a.val < b.val; } n priority_queue<node> H; n int Get() { n   char ch; n   int v = 0; n   bool f = false; n   while (!isdigit(ch = getchar())) n     if (ch ==  - ) f = true; n   v = ch - 48; n   while (isdigit(ch = getchar())) v = v * 10 + ch - 48; n   if (f) n     return -v; n   else n     return v; n } n int main() { n   n = Get(), m = Get(); n   for (int i = 1; i <= n; i++) scanf( %I64d%I64d , &land[i].l, &land[i].r); n   for (int i = 1; i <= m; i++) scanf( %I64d , &p[i].val), p[i].pos = i; n   for (int i = 1; i <= n - 1; i++) n     A[i].l = land[i + 1].l - land[i].r, A[i].r = land[i + 1].r - land[i].l, n     A[i].pos = i; n   n--; n   sort(A + 1, A + 1 + n, cmp); n   sort(p + 1, p + 1 + m, cmp2); n   if (m < n) { n     puts( No ); n     return 0; n   } n   long long mx = 0; n   for (int i = 1, j = 1; i <= m; i++) { n     while (A[j].l <= p[i].val && j <= n) H.push(node(A[j].pos, A[j].r)), j++; n     while (!H.empty() && H.top().val < p[i].val) H.pop(); n     if (!H.empty()) { n       node tmp = H.top(); n       ans[tmp.pos] = p[i].pos; n       H.pop(); n     } n   } n   bool flag = true; n   for (int i = 1; i <= n; i++) n     if (ans[i] == 0) flag = false; n   if (flag) { n     puts( Yes ); n     for (int i = 1; i <= n - 1; i++) printf( %d  , ans[i]); n     printf( %d n , ans[n]); n   } else n     puts( No ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int x, k, n, q, cnt; n long long num[10]; n int bel[1 << 10]; n struct Square { n   long long num[75][75]; n   Square() { n     for (int i = 1; i <= 70; i++) { n       for (int j = 1; j <= 70; j++) num[i][j] = 1e18; n       num[i][i] = 0; n     } n   } n   Square operator*(const Square &a) const { n     Square tmp; n     for (int i = 1; i <= cnt; i++) tmp.num[i][i] = 1e18; n     for (int i = 1; i <= cnt; i++) n       for (int j = 1; j <= cnt; j++) n         for (int k = 1; k <= cnt; k++) n           tmp.num[i][j] = tmp.num[i][j] < (num[i][k] + a.num[k][j]) n                               ? tmp.num[i][j] n                               : (num[i][k] + a.num[k][j]); n     return tmp; n   } n   Square operator^(const int &x) { n     if (!x) return Square(); n     Square tmp, tmp1; n     int times = x; n     for (int i = 1; i <= cnt; i++) n       for (int j = 1; j <= cnt; j++) tmp1.num[i][j] = num[i][j]; n     while (times) { n       if (times & 1) tmp = tmp * tmp1; n       times >>= 1; n       tmp1 = tmp1 * tmp1; n     } n     return tmp; n   } n } one; n struct Stone { n   int place; n   long long val; n } stone[30]; n bool calc(int tmp) { n   int many = 0; n   for (int i = 1; i <= 8; i++) n     if (tmp & (1 << (i - 1))) many++; n   return many == x; n } n bool cmp(Stone a, Stone b) { return a.place < b.place; } n int main() { n   scanf( %d%d%d%d , &x, &k, &n, &q); n   for (int i = 0; i <= (1 << k) - 1; i++) n     if (calc(i)) bel[i] = ++cnt; n   for (int i = 1; i <= k; i++) scanf( %lld , &num[i]); n   for (int i = 1; i <= q; i++) scanf( %d%lld , &stone[i].place, &stone[i].val); n   sort(stone + 1, stone + q + 1, cmp); n   for (int i = 1; i <= cnt; i++) one.num[i][i] = 1e18; n   for (int i = 1; i <= (1 << k) - 1; i++) { n     if (i & 1) { n       if (!bel[i]) continue; n       for (int j = 1; j <= k; j++) n         if (!((1 << j) & i)) one.num[bel[i]][bel[((1 << j) | i) >> 1]] = num[j]; n     } else if (bel[i]) n       one.num[bel[i]][bel[i >> 1]] = 0; n   } n   int now = 1; n   long long sum = 0; n   Square ans; n   for (int i = 1; i <= q; i++) { n     if (stone[i].place > n - x) { n       sum += stone[i].val; n       continue; n     } n     ans = ans * (one ^ (stone[i].place - now)), now = stone[i].place; n     for (int j = 1; j <= (1 << k) - 1; j += 2) n       if (bel[j]) n         for (int k = 1; k <= cnt; k++) ans.num[k][bel[j]] += stone[i].val; n   } n   ans = ans * (one ^ (n - x + 1 - now)); n   printf( %lld , ans.num[1][1] + sum); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 110; n int a[N * N], n, T, dp[N * N], rec[305]; n int cal(int x) { n   int m = n * x, len = 0; n   for (int i = 1; i <= m; i++) { n     if (a[i] >= dp[len]) n       dp[++len] = a[i]; n     else { n       int pos = upper_bound(dp + 1, dp + len + 1, a[i]) - dp; n       dp[pos] = a[i]; n     } n   } n   return len; n } n int main() { n   scanf( %d%d , &n, &T); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   for (int i = 1; i < n; i++) { n     for (int j = 1; j <= n; j++) { n       a[i * n + j] = a[j]; n     } n   } n   if (T <= n) n     printf( %d n , cal(T)); n   else { n     memset(rec, 0, sizeof(rec)); n     for (int i = 1; i <= n; i++) ++rec[a[i]]; n     int tmp = 0; n     for (int i = 1; i <= n; i++) tmp = max(tmp, rec[a[i]]); n     int ans = tmp * (T - n) + cal(n); n     printf( %d n , ans); 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 r = 0; n   int rr = 0; n   int prev = 10000; n   while (n--) { n     int a, b; n     cin >> a >> b; n     if (a != b) n       r = 1; n     else { n       if (a > prev) rr = 1; n     } n     prev = a; n   } n   if (r == 1) n     cout <<  rated  << endl; n   else { n     if (rr == 0) n       cout <<  maybe  << endl; n     else n       cout <<  unrated  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n  n using namespace std; n typedef long long ll; n #define endl   n  n #define x1 fgdfgdg n const ll DIM = 2E5+7; n ll A[DIM]; n ll GCD(ll a,ll b){ n     if (a==0 || b==0)return a+b; n     return GCD(b%a,a); n } n int main() { n     ios_base::sync_with_stdio(0); n     cin.tie(0); n     cout.tie(0); n     ll n,m; n     cin>>n>>m; n     for(ll i = 1;i<=n;++i){ n         cin>>A[i]; n     } n     sort(A+1,A+1+n); n     ll gcd = A[2]-A[1]; n     for(ll i = 2;i<=n;++i){ n         if (i==2)gcd = A[i]-A[i-1]; n         else gcd = GCD(A[i]-A[i-1],gcd); n     } n     for(ll i = 1;i<=m;++i){ n         ll x; n         cin>>x; n         if (n!=1) n         cout<<GCD(A[1]+x,gcd)<<   ; n         else cout<<A[1]+x<<   ; n     } n     cout<<endl; n     return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int k, d; n string a, b =  CODEFORCES ; n int main() { n   cin >> a; n   if (a == b) { n     cout <<  YES n ; n     return 0; n   } n   for (int i = 0; i < a.size(); i++) { n     for (int j = i; j < a.size(); j++) { n       string h; n       for (int l = 0; l < i; l++) h += a[l]; n       for (int l = j + 1; l < a.size(); l++) h += a[l]; n       if (h == b) k = 1; n     } n   } n   if (k == 1) n     cout <<  YES n ; n   else n     cout <<  NO n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 10; n struct node { n   int x, flag; n } a[2 * N]; n bool cmp(const node a, const node b) { n   if (a.x == b.x) n     return a.flag > b.flag; n   else n     return a.x < b.x; n } n vector<pair<int, int> > ans; n int main(void) { n   int n, k, l, r; n   scanf( %d%d , &n, &k); n   for (int i = 0; i < n; i++) { n     scanf( %d%d , &l, &r); n     a[2 * i].x = l; n     a[2 * i].flag = 1; n     a[2 * i + 1].x = r; n     a[2 * i + 1].flag = -1; n   } n   n *= 2; n   sort(a, a + n, cmp); n   for (int i = 1; i < n; i++) a[i].flag += a[i - 1].flag; n   for (int i = 0; i < n; i++) { n     int cnt = 0; n     if (a[i].flag >= k) { n       l = a[i].x; n       while (i < n && a[i].flag >= k) i++; n       r = a[i].x; n       ans.push_back(make_pair(l, r)); n     } n   } n   cout << ans.size() << endl; n   for (int i = 0; i < ans.size(); i++) n     cout << ans[i].first <<     << ans[i].second << endl; 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[3000]; n   int ert = 0, at = 0, as = 0; n   int k = 1; n   while (k < 3000) { n     ert++; n     if (ert % 10 == 0) at++; n     if (ert % 10 == 0 and at % 10 == 0) as++; n     if (k <= 9) { n       a[k] = ert % 10; n       k++; n     } else { n       if (k > 9 and k < 190) { n         a[k] = at % 10; n         k++; n         a[k] = ert % 10; n         k++; n       } else { n         a[k] = as % 10; n         k++; n         a[k] = at % 10; n         k++; n         a[k] = ert % 10; n         k++; n       } n     } n   } n   cout << a[n]; n } n


#include <bits/stdc++.h> n using namespace std; n long long prime[2000006]; n int main() { n   long long i, j; n   prime[1] = 1; n   for (i = 2; i < 2000006; i++) { n     for (j = 2 * i; j < 2000006; j += i) prime[j] = 1; n   } n   long long n; n   cin >> n; n   long long a[n], co = 0; n   for (i = 0; i < n; i++) { n     cin >> a[i]; n     if (a[i] == 1) co++; n   } n   if (co > 1) { n     for (i = 0; i < n; i++) { n       if (prime[a[i] + 1] == 0 && a[i] > 1) { n         cout << co + 1 << endl << a[i] <<    ; n         for (j = 0; j < co; j++) cout << 1 <<    ; n         return 0; n       } n     } n     cout << co << endl; n     for (i = 0; i < co; i++) cout << 1 <<    ; n     return 0; n   } n   for (i = 0; i < n; i++) { n     for (j = i + 1; j < n; j++) { n       if (prime[a[i] + a[j]] == 0) { n         cout << 2 << endl; n         cout << a[i] <<     << a[j]; n         return 0; n       } n     } n   } n   cout << 1 << endl << a[0]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double EPS = 1e-6; n const double PI = acos(-1); n const int INF = 1e9; n void FAST() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n } n signed main() { n   FAST(); n   int n; n   cin >> n; n   vector<long long> a(n + n); n   for (int i = 0; i < n + n; i++) { n     cin >> a[i]; n   } n   sort(a.begin(), a.end()); n   long long ans = (a[n - 1] - a[0]) * (a[n + n - 1] - a[n]); n   for (int i = 1; i < n; i++) { n     ans = min(ans, (a[n - 1 + i] - a[i]) * (a[n + n - 1] - a[0])); n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, i, j, first, end, count = 0; n   cin >> n >> m; n   vector<int> a(m + 1, 0); n   for (i = 0; i < n; i++) { n     cin >> first >> end; n     for (j = first; j <= end; j++) a[j] = 1; n   } n   for (i = 1; i < m + 1; i++) { n     if (a[i] == 0) count++; n   } n   cout << count << endl; n   for (i = 1; i < m + 1; i++) { n     if (a[i] == 0) cout << i <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void swap(int& a, int& b) { n   int tmp; n   tmp = a; n   a = b; n   b = tmp; n } n void QuickSort(int l, int h, int a[], int b[]) { n   int i = l, j = h; n   int pivot = a[l + rand() % (h - l + 1)]; n   while (i <= j) { n     while (a[i] < pivot) i++; n     while (a[j] > pivot) j--; n     if (i <= j) { n       swap(a[i], a[j]); n       swap(b[i], b[j]); n       i++; n       j--; n     } n   } n   if (l < j) QuickSort(l, j, a, b); n   if (i < h) QuickSort(i, h, a, b); n } n int main() { n   int n, r, avg; n   long long s = 0; n   cin >> n >> r >> avg; n   int b[n + 1], a[n + 1]; n   for (int i = 1; i <= n; i++) { n     cin >> a[i] >> b[i]; n     s += (long long)a[i]; n   } n   long long d = (long long)n * avg - s; n   if (d <= 0) { n     cout << 0; n     return 0; n   } n   QuickSort(1, n, b, a); n   long long ans = 0; n   for (int i = 1; i <= n; i++) { n     if (d >= r - a[i]) { n       ans += (long long)(r - a[i]) * b[i]; n       d -= r - a[i]; n       if (!d) break; n     } else { n       ans += (long long)d * b[i]; n       break; n     } n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   set<int> s; n   long long int n, m, k, a; n   cin >> n >> m; n   while (n--) { n     cin >> k; n     while (k--) { n       cin >> a; n       s.insert(a); n     } n   } n   if (s.size() == m) { n     cout <<  YES  n          <<   n ; n   } else { n     cout <<  NO  n          <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, q; n pair<int, bool> a[400100]; n int dsu[200100]; n pair<int, int> query[200100]; n long long ans[200100]; n int last[200100]; n long long prsum[400100]; n int f(int x) { n   if (dsu[x] < 0) return x; n   int r = f(dsu[x]); n   dsu[x] = r; n   return r; n } n void join(int x, int y) { n   int r1 = f(x); n   int r2 = f(y); n   if (r1 != r2) { n     if (dsu[r1] > dsu[r2]) swap(r1, r2); n     last[r1] = max(last[r1], last[r2]); n     dsu[r1] += dsu[r2]; n     dsu[r2] = r1; n   } n } n priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>, n                greater<pair<int, pair<int, int>>>> n     pq; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long sum = 0; n   cin >> n >> m >> q; n   for (int i = 1; i <= n; i++) { n     cin >> a[i].first; n     sum += a[i].first; n     a[i].second = 1; n   } n   for (int i = 1; i <= m; i++) { n     cin >> a[i + n].first; n     a[i + n].second = 0; n   } n   sort(a + 1, a + 1 + n + m); n   for (int i = 0; i <= n + 10; i++) { n     dsu[i] = -1; n   } n   int cur = 1; n   for (int i = 1; i <= n + m; i++) { n     if (a[i].second == 1) { n       if (i < n + m) pq.push({a[i + 1].first - a[i].first, {i, cur}}); n       last[cur] = i; n       cur++; n     } n   } n   for (int i = 0; i < q; i++) { n     cin >> query[i].first; n     query[i].second = i; n   } n   sort(query, query + q); n   for (int i = 1; i <= n + m; i++) { n     prsum[i] = prsum[i - 1] + a[i].first; n   } n   for (int i = 0; i < q; i++) { n     int v = query[i].first; n     while (!pq.empty() && v >= pq.top().first) { n       pair<int, int> tmp = pq.top().second; n       pq.pop(); n       if (a[tmp.first + 1].second == 1) { n         int r1 = f(tmp.second); n         int r2 = f(tmp.second + 1); n         sum += prsum[last[r2] + dsu[r2]] - prsum[last[r2] + dsu[r2] + dsu[r1]]; n         sum -= prsum[last[r1]] - prsum[last[r1] + dsu[r1]]; n         join(tmp.second, tmp.second + 1); n       } else if (tmp.first + 1 == n + m) { n         sum += a[tmp.first + 1].first - n                a[tmp.first + dsu[f(tmp.second)] + 1].first; n         last[f(tmp.second)]++; n       } else { n         pq.push({a[tmp.first + 2].first - a[tmp.first + 1].first, n                  {tmp.first + 1, tmp.second}}); n         sum += a[tmp.first + 1].first - n                a[tmp.first + dsu[f(tmp.second)] + 1].first; n         last[f(tmp.second)]++; n       } n     } n     ans[query[i].second] = sum; n   } n   for (int i = 0; i < q; i++) { n     cout << ans[i] <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n const int maxi = 2000000000; n const int maxq = 1000000000; n const double eps = 1e-10; n const double pi = 3.1415926535897932; n const double inf = 1e+18; n const int mo = 1000000007; n using namespace std; n long long l, r; n int main() { n   cin >> l >> r; n   if (l == r) { n     puts( 0 ); n     return 0; n   } n   for (int i = 62; i >= 0; i--) { n     if ((1ll << (i)) > l && (1ll << (i)) <= r) { n       cout << (1ll << (i + 1)) - 1 << endl; n       return 0; n     } n     if ((1ll << (i)) <= l) { n       l -= (1ll << (i)); n       r -= (1ll << (i)); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double PI = 3.1415926535897932384626433; n double DP[(1 << 20)]; n vector<double> fx, fy, lg; n double calc(double px, double py, double t, double al) { n   double d = sqrt((px - t) * (px - t) + py * py); n   double sina = sin(al * (PI / 180.0)); n   double sinb = fabs(py) / d; n   double bt = asin(sinb) * (180.0 / PI); n   if (px < t) bt = 180 - bt; n   if (al + bt >= 180) return t + 300000.0; n   double gm = 180 - bt - al; n   double sinc = sin(gm * (PI / 180.0)); n   return t + (sina * (d / sinc)); n } n int main() { n   int i, mask, n, l, r, cr; n   cin >> n >> l >> r; n   int xx, yy, al; n   double cl = l; n   bool hasp = false; n   for (i = 0; i < n; ++i) { n     cin >> xx >> yy >> al; n     if (yy == 0) { n       if (hasp || xx >= r || xx <= l) { n         cout << r - l <<   n ; n         return 0; n       } n       hasp = true; n       cl = xx; n     } else { n       fx.push_back(xx); n       fy.push_back(yy); n       lg.push_back(al); n     } n   } n   n = fx.size(); n   DP[0] = cl; n   for (mask = 1; mask < (1 << n); ++mask) { n     DP[mask] = cl; n     for (i = 0; i < n; ++i) { n       if (mask & (1 << i)) { n         cr = mask - (1 << i); n         DP[mask] = max(DP[mask], calc(fx[i], fy[i], DP[cr], lg[i])); n       } n     } n   } n   printf( %.8lf n , min((double)r, DP[(1 << n) - 1]) - l); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int i; n int main() { n   string s; n   cin >> s; n   int k; n   i = 0; n   while (s[i] ==  0 ) { n     i++; n   } n   int f = 0, v = 0; n   bool b = false; n   while (i != s.size()) { n     if (s[i] ==  4 ) n       f++; n     else if (s[i] ==  7 ) n       v++; n     i++; n   } n   if (b) n     cout << -1; n   else { n     if (f != 0 && f == v) n       cout << 4; n     else if (f > v) n       cout << 4; n     else if (v > f) n       cout << 7; n     else n       cout << -1; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, k; n struct node { n   int sz; n   vector<int> m, M; n   node() { n     sz = 1; n     for (int i = 0; i < k; ++i) { n       int tmp; n       scanf( %d , &tmp); n       m.push_back(tmp); n       M.push_back(tmp); n     } n   } n   bool operator<(const node &nd) const { n     for (int i = 0; i < k; ++i) { n       if (M[i] > nd.m[i]) return false; n     } n     return true; n   } n   void merge(const node &nd) { n     for (int i = 0; i < k; ++i) { n       m[i] = min(m[i], nd.m[i]); n       M[i] = max(M[i], nd.M[i]); n     } n     sz += nd.sz; n   } n }; n set<node> st; n int main() { n   scanf( %d%d , &n, &k); n   for (int i = 0; i < n; ++i) { n     node nd = node(); n     auto lb = st.lower_bound(nd), ub = st.upper_bound(nd); n     while (lb != ub) { n       nd.merge(*lb); n       lb = st.erase(lb); n     } n     st.insert(nd); n     printf( %d n , (st.rbegin())->sz); n   } n   return 0; n } n


#include <bits/stdc++.h> n const int N = 100005; n using namespace std; n int n, a[N], b[N], flag[N]; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]), b[i] = 1; n   int now = a[n]; n   for (int i = n - 1; i >= 1; i--) { n     if (now > a[i]) n       b[i] = -1, flag[i] = 1, now = now - a[i]; n     else n       b[i] = 1, flag[i] = -1, now = a[i] - now; n   } n   int yh = 1; n   for (int i = 1; i <= n; i++) { n     if (b[i] * yh == 1) n       printf( + ); n     else n       printf( - ); n     yh *= flag[i]; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 100; n const int LOG = 18; n const int inf = 1e9 + 10; n struct node { n   int nex[2], val; n   node() { n     nex[0] = nex[1] = -1; n     val = inf; n   } n }; n vector<node> v; n vector<int> div2[N]; n bool mark[N]; n int t[N]; n node tmp; n int go(int curr, int t) { n   if (v[curr].nex[t] != -1) return v[curr].nex[t]; n   v[curr].nex[t] = v.size(); n   v.push_back(tmp); n   return v[curr].nex[t]; n } n void add(int curr, int x) { n   v[curr].val = min(v[curr].val, x); n   for (int i = LOG - 1; i >= 0; i--) { n     curr = go(curr, (x >> i) % 2); n     v[curr].val = min(v[curr].val, x); n   } n } n int query(int curr, int x, int maxi, int lev = LOG - 1) { n   if (v[curr].val > maxi) return -1; n   if (lev == -1) return (v[curr].val != inf) ? v[curr].val : -1; n   int R = go(curr, 1); n   int L = go(curr, 0); n   if (v[R].val > maxi) { n     return query(L, x, maxi, lev - 1); n   } else if (v[L].val > maxi) { n     return query(R, x, maxi, lev - 1); n   } else { n     if ((v[L].val ^ x) > (v[R].val ^ x)) { n       return query(L, x, maxi, lev - 1); n     } else { n       return query(R, x, maxi, lev - 1); n     } n   } n } n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   memset(t, -1, sizeof t); n   int q; n   cin >> q; n   for (int i = 1; i < N; i++) { n     for (int j = i; j < N; j += i) { n       div2[j].push_back(i); n     } n   } n   for (int i = 0, tp, x, k, s, u; i < q; i++) { n     cin >> tp; n     if (tp == 1) { n       cin >> u; n       if (mark[u]) continue; n       mark[u] = true; n       for (auto x : div2[u]) { n         if (t[x] == -1) { n           t[x] = v.size(); n           v.push_back(tmp); n         } n         add(t[x], u); n       } n     } else { n       cin >> x >> k >> s; n       if (x % k != 0) { n         cout <<  -1 n ; n         continue; n       } n       s -= x; n       if (t[k] == -1) { n         t[k] = v.size(); n         v.push_back(tmp); n       } n       cout << query(t[k], x, s) <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e6 + 7; n const int inf = 0x3f3f3f3f; n const long long INF = 0x3f3f3f3f3f3f3f3f; n const int mod = 1000000007; n const double eps = 1e-6; n const double PI = acos(-1); n int n, k, miu[N], Pown[N], sum[N]; n int Power(int a, int b) { n   int ans = 1; n   while (b) { n     if (b & 1) ans = 1LL * ans * a % mod; n     a = 1LL * a * a % mod; n     b >>= 1; n   } n   return ans; n } n int main() { n   miu[1] = 1; n   for (int i = 1; i < N; i++) n     for (int j = i + i; j < N; j += i) miu[j] -= miu[i]; n   scanf( %d%d , &n, &k); n   for (int i = 1; i <= k; i++) Pown[i] = Power(i, n); n   int ans = 0, tmp = 0; n   for (int i = 1; i <= k; i++) { n     for (int j = i; j <= k; j += i) { n       sum[j] = n           ((sum[j] + 1LL * miu[i] * (Pown[j / i] - Pown[j / i - 1])) % mod + n            mod) % n           mod; n     } n     tmp = (tmp + sum[i]) % mod; n     ans = (ans + (tmp ^ i)) % mod; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int q; n   scanf( %d , &q); n   for (int i = 0; i < q; i++) { n     int l, r, d; n     scanf( %d %d %d , &l, &r, &d); n     if ((double)l / d > 1) n       printf( %d n , d); n     else { n       int f = ((double)r / d) + 1; n       printf( %d n , f * d); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int _ = 1e5 + 7; n const int __ = 4e5 + 7; n const int L = 11; n int n, Q; n long long a[_], pw[L + 7]; n namespace SGT { n long long num[__], minx[__], tag[__]; n bool cov[__]; n bool flag; n void Build(int k, int l, int r) { n   if (l == r) { n     int j = 0; n     while (pw[j] < a[l]) ++j; n     minx[k] = pw[j] - a[l], num[k] = a[l]; n     return; n   } n   Build((k << 1), l, ((l + r) >> 1)); n   Build((k << 1 | 1), ((l + r) >> 1) + 1, r); n   minx[k] = min(minx[(k << 1)], minx[(k << 1 | 1)]); n   num[k] = n       minx[(k << 1)] < minx[(k << 1 | 1)] ? num[(k << 1)] : num[(k << 1 | 1)]; n } n void upd(int k, long long w) { minx[k] -= w, num[k] += w, tag[k] += w; } n void Psd(int k) { n   if (cov[k]) { n     cov[(k << 1)] = 1, num[(k << 1)] = num[k], minx[(k << 1)] = minx[k]; n     cov[(k << 1 | 1)] = 1, num[(k << 1 | 1)] = num[k], n                   minx[(k << 1 | 1)] = minx[k]; n     cov[k] = 0, tag[k] = 0; n   } else if (tag[k]) { n     upd((k << 1), tag[k]); n     upd((k << 1 | 1), tag[k]); n     tag[k] = 0; n   } n } n void Modify(int k, int l, int r, int x, int y, int w) { n   if (l >= x and r <= y) { n     if (minx[k] > w) { n       upd(k, w); n       return; n     } else if (l == r or cov[k]) { n       num[k] += w; n       int j = 1; n       while (pw[j] < num[k]) ++j; n       minx[k] = pw[j] - num[k]; n       if (!minx[k]) flag = 1; n       return; n     } n   } n   Psd(k); n   if (x <= ((l + r) >> 1)) Modify((k << 1), l, ((l + r) >> 1), x, y, w); n   if (y > ((l + r) >> 1)) Modify((k << 1 | 1), ((l + r) >> 1) + 1, r, x, y, w); n   minx[k] = min(minx[(k << 1)], minx[(k << 1 | 1)]); n   num[k] = n       minx[(k << 1)] < minx[(k << 1 | 1)] ? num[(k << 1)] : num[(k << 1 | 1)]; n } n void Cover(int k, int l, int r, int x, int y, int w) { n   if (l >= x and r <= y) { n     tag[k] = 0, num[k] = w, cov[k] = 1; n     int j = 1; n     while (pw[j] < w) ++j; n     minx[k] = pw[j] - w; n     return; n   } n   Psd(k); n   if (x <= ((l + r) >> 1)) Cover((k << 1), l, ((l + r) >> 1), x, y, w); n   if (y > ((l + r) >> 1)) Cover((k << 1 | 1), ((l + r) >> 1) + 1, r, x, y, w); n   minx[k] = min(minx[(k << 1)], minx[(k << 1 | 1)]); n   num[k] = n       minx[(k << 1)] < minx[(k << 1 | 1)] ? num[(k << 1)] : num[(k << 1 | 1)]; n } n long long Query(int k, int l, int r, int x) { n   if (l == r) return num[k]; n   Psd(k); n   if (x <= ((l + r) >> 1)) n     return Query((k << 1), l, ((l + r) >> 1), x); n   else n     return Query((k << 1 | 1), ((l + r) >> 1) + 1, r, x); n } n void Init() { Build(1, 1, n); } n void Add(int l, int r, int w) { n   do { n     flag = 0; n     Modify(1, 1, n, l, r, w); n   } while (flag); n } n void Cover(int l, int r, int w) { Cover(1, 1, n, l, r, w); } n long long Ask(int x) { return Query(1, 1, n, x); } n }  // namespace SGT n int gi() { n   int x = 0; n   char c = getchar(); n   while (!isdigit(c)) c = getchar(); n   while (isdigit(c)) x = (x << 3) + (x << 1) + c -  0 , c = getchar(); n   return x; n } n void Init() { n   n = gi(), Q = gi(); n   for (int i = 1; i <= n; ++i) a[i] = gi(); n   pw[0] = 1; n   for (int i = 1; i <= L; ++i) pw[i] = (long long)pw[i - 1] * 42; n   SGT::Init(); n } n void Run() { n   int ty, l, r, x; n   while (Q--) { n     ty = gi(); n     if (ty == 1) n       printf( %lld n , SGT::Ask(gi())); n     else { n       l = gi(), r = gi(), x = gi(); n       if (ty == 2) n         SGT::Cover(l, r, x); n       else n         SGT::Add(l, r, x); n     } n   } n } n int main() { n   Init(); n   Run(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n long long f[15], all[15]; n long long n, k; n int solve(int rem) { n   if (!rem) return 1; n   return solve(rem - 1) * 2; n } n long long solve(int i, string& str) { n   if (i == str.size()) return 1; n   long long ret = solve(i + 1, str); n   if (str[i] ==  1 ) ret += all[str.size() - i - 1]; n   return ret; n } n bool lucky(long long x) { n   while (x) { n     if (x % 10 != 4 && x % 10 != 7) return 0; n     x /= 10; n   } n   return 1; n } n int main() { n   all[0] = f[0] = 1; n   for (int i = 1; i < 15; ++i) { n     f[i] = f[i - 1] * i; n     all[i] = 2ll * all[i - 1]; n   } n   cin >> n >> k; n   if (n < 15 && f[n] < k) return cout << -1, 0; n   long long nn = n; n   int rem = 0; n   for (int i = 0; i < 14; ++i) n     if (f[i] < k) rem = i; n   long long ans = 0; n   set<long long> rem_num; n   if (n > rem + 1) { n     long long m = n - rem - 1; n     string cur =   , s =   ; n     stringstream mycin; n     mycin << m; n     mycin >> cur; n     bool ok = 0, is = 0; n     int lstseven = -1; n     for (int i = 0; i < cur.size(); ++i) { n       int ch = cur[i] -  0 ; n       if (ok) break; n       if (ch < 4) { n         is = 1; n         break; n       } else if (ch > 7) n         ok = 1, lstseven = 1e9 + 5; n       else if (ch == 7) n         lstseven = max(lstseven, i); n       else if (ch > 4) n         ok = 1; n     } n     if (is && lstseven == -1) { n       int sz = cur.size(); n       --sz; n       while (sz--) s +=  1 ; n     } else { n       ok = 0; n       for (int i = 0; i < cur.size(); ++i) { n         int ch = cur[i] -  0 ; n         if (ok) n           s +=  1 ; n         else if (is && lstseven == i) n           s +=  0 , ok = 1; n         else if (ch > 7) n           ok = 1, s +=  1 ; n         else if (ch == 7) n           s +=  1 ; n         else if (ch > 4) n           s +=  0 , ok = 1; n         else n           s +=  0 ; n       } n     } n     if (s.size()) { n       ans += solve(0, s); n       for (int i = 1; i < s.size(); ++i) ans += all[i]; n     } n     for (int i = n; rem_num.size() <= rem; --i) rem_num.insert(i); n     n = rem + 1; n   } else n     for (int i = 1; i <= n; ++i) rem_num.insert(i); n   for (int i = nn - n + 1; i <= nn; ++i) { n     vector<int> v; n     while (f[nn - i] < k) { n       k -= f[nn - i]; n       v.push_back(*rem_num.begin()); n       rem_num.erase(rem_num.begin()); n     } n     int x = *rem_num.begin(); n     rem_num.erase(rem_num.begin()); n     for (int j : v) rem_num.insert(j); n     ans += lucky(i) && lucky(x); n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n const int mod = 1e9 + 7; n const int p_mod = 998244353; n using namespace std; n long long fast_pow(long long a, long long b) { n   long long res = 1; n   while (b) { n     if (b & 1) res = (res * a) % mod; n     b >>= 1; n     a = (a * a) % mod; n   } n   return res % mod; n } n const int N = 1e5 + 5; n stack<char> s; n char str[N]; n int cnt = 0; n int main() { n   std::ios::sync_with_stdio(false); n   std::cin.tie(0); n   cin >> (str + 1); n   int len = strlen(str + 1); n   for (int i = 1; i <= len; i++) { n     if (!s.empty() && s.top() == str[i]) { n       s.pop(); n       cnt++; n     } else n       s.push(str[i]); n   } n   if (cnt % 2) { n     cout <<  Yes  << endl; n   } else n     cout <<  No  << endl; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n struct item { n   string n, c; n   int a, s; n }; n item make(string n, string c, int a, int s) { n   item z; n   z.a = a; n   z.c = c; n   z.n = n; n   z.s = s; n   return z; n } n struct rez { n   string n, t, h; n   int b; n }; n bool f2(rez a, rez b) { return a.b > b.b; } n struct equip { n   string n; n   vector<string> r; n   int s; n }; n int n, k, s, ya[3], yb[3]; n item a[3][100]; n rez b[3][1000]; n equip z[3]; n int sum(int h, int l) { n   int sss = 0; n   for (int i = 0; i < l; i++) sss += b[h][i].b; n   return sss; n } n int main() { n   string name, clas; n   int atk, def, rz, siz; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> name >> clas >> atk >> def >> rz >> siz; n     if (clas ==  weapon ) { n       a[0][ya[0]] = make(name, clas, atk, siz); n       ya[0]++; n     } n     if (clas ==  armor ) { n       a[1][ya[1]] = make(name, clas, def, siz); n       ya[1]++; n     } n     if (clas ==  orb ) { n       a[2][ya[2]] = make(name, clas, rz, siz); n       ya[2]++; n     } n     s += siz; n   } n   cin >> k; n   rez y; n   for (int i = 0; i < k; i++) { n     cin >> y.n >> y.t >> y.b >> y.h; n     if (y.t ==  gladiator ) { n       b[0][yb[0]] = y; n       yb[0]++; n     } n     if (y.t ==  sentry ) { n       b[1][yb[1]] = y; n       yb[1]++; n     } n     if (y.t ==  physician ) { n       b[2][yb[2]] = y; n       yb[2]++; n     } n   } n   for (int h = 0; h < 3; h++) sort(b[h], b[h] + yb[h], f2); n   if (s == k) { n     for (int h = 0; h < 3; h++) { n       int mx = -1, m; n       for (int i = 0; i < ya[h]; i++) { n         int ss = 0; n         for (int j = 0; j < yb[h]; j++) n           if (b[h][j].h == a[h][i].n) ss += b[h][j].b; n         if (ss + a[h][i].a > mx) { n           mx = ss + a[h][i].a; n           m = i; n         } n       } n       z[h].n = a[h][m].n; n       for (int i = 0; i < 3; i++) n         for (int j = 0; j < yb[i]; j++) n           if (b[i][j].h == z[h].n) z[h].r.push_back(b[i][j].n); n     } n   } else { n     int m[3]; n     for (int h = 0; h < 3; h++) { n       int mx = -1, mm, l; n       for (int i = 0; i < ya[h]; i++) { n         int sm = sum(h, min(a[h][i].s, yb[h])); n         if (a[h][i].a + sm > mx) { n           mx = a[h][i].a + sm; n           mm = i; n           l = min(a[h][i].s, yb[h]); n         } n       } n       z[h].n = a[h][mm].n; n       z[h].s = a[h][mm].s; n       for (int i = 0; i < l; i++) z[h].r.push_back(b[h][i].n); n       m[h] = l; n     } n     for (int h = 0; h < 3; h++) n       for (int i = 0; i < 3; i++) n         while (m[i] < yb[i] && z[h].r.size() < z[h].s) { n           z[h].r.push_back(b[i][m[i]].n); n           m[i]++; n         } n   } n   for (int i = 0; i < 3; i++) { n     cout << z[i].n <<     << z[i].r.size(); n     for (int j = 0; j < z[i].r.size(); j++) cout <<     << z[i].r[j]; n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int x, y, sum = 0; n   vector<pair<int, int>> v(1e5); n   map<int, int> ways; n   scanf( %d , &x); n   for (int i = 0; i < x; i++) { n     scanf( %d , &y); n     sum += y; n     v[i] = pair<int, int>(y, sum); n     if (i != x - 1) ways[sum]++; n   } n   printf( %d n , ((sum % 2) ? 0 : ways[sum / 2])); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n bool ison[100010]; n char temp[100]; n int q, t; n int isprime[100010]; n int prime[100010]; n vector<int> pr[100010]; n void preprocess() { n   fill(isprime, isprime + 100010, 0); n   fill(ison, ison + 100010, false); n   fill(prime, prime + 100010, -1); n   for (int i = 2; i < 100010; i++) { n     if (isprime[i]) { n       continue; n     } n     pr[i].push_back(i); n     for (int j = i + i; j < 100010; j += i) { n       pr[j].push_back(i); n       isprime[j] = i; n     } n   } n } n void solve1(int a) { n   if (ison[a]) { n     printf( Already on n ); n     return; n   } n   int len = pr[a].size(); n   int i; n   for (i = 0; i < len; i++) { n     int val = pr[a][i]; n     if (prime[val] == -1) { n       continue; n     } else { n       printf( Conflict with %d n , prime[val]); n       return; n     } n   } n   for (i = 0; i < len; i++) { n     int val = pr[a][i]; n     prime[val] = a; n   } n   ison[a] = true; n   printf( Success n ); n } n void solve2(int b) { n   if (!ison[b]) { n     printf( Already off n ); n     return; n   } n   int len = pr[b].size(); n   int i, val; n   for (i = 0; i < len; i++) { n     val = pr[b][i]; n     prime[val] = -1; n   } n   ison[b] = false; n   printf( Success n ); n } n int main() { n   scanf( %d%d , &n, &q); n   preprocess(); n   for (int i = 0; i < q; i++) { n     scanf( %s , temp); n     scanf( %d , &t); n     if (temp[0] ==  + ) { n       solve1(t); n     } else { n       solve2(t); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, x; n void ans(int x1, int x2, int y1, int y2, int now) { n   if (x1 + 1 > x2 - 1 || y1 + 1 > y2 - 1) { n     cout << 0; n     return; n   } else { n     if (now + 1 == x) { n       if ((x2 - x1 - 1) == 1 || (y2 - y1 - 1) == 1) { n         if ((x2 - x1 - 1) == 1 && (y2 - y1 - 1) == 1) n           cout << 1; n         else n           cout << ((x2 - x1 - 1) + (y2 - y1 - 1) - 1) / 2; n       } else n         cout << ((x2 - x1 - 1) + (y2 - y1 - 1) - 2); n     } else n       ans(x1 + 1, x2 - 1, y1 + 1, y2 - 1, now + 1); n   } n } n int main() { n   cin >> n >> m >> x; n   if (n == 1 || m == 1) { n     if (n == 1 && m == 1) { n       if (x == 1) { n         cout << 1; n       } else n         cout << 0; n       return 0; n     } n     if (x == 1) { n       cout << (n + m) / 2; n     } else n       cout << 0; n     return 0; n   } n   if (x == 1) { n     cout << (n + m - 2); n     return 0; n   } n   ans(1, n, 1, m, 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int32_t main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   long long t; n   cin >> t; n   while (t--) { n     long long d, m; n     cin >> d >> m; n     long long j = 1, ans = 1, sum = 0; n     while (sum + j <= d) { n       ans = ans * (j + 1) % m; n       sum += j; n       j *= 2; n     } n     ans = ans * (d - sum + 1) % m; n     ans += m - 1; n     ans %= m; n     cout << ans <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   int ind = s.size() - 1; n   for (int i = 0; i < s.size(); ++i) { n     if (s[i] ==  0 ) { n       ind = i; n       break; n     } n   } n   bool flag = 0; n   for (int i = 0; i < s.size(); ++i) { n     if (!flag && s[i] ==  0 ) continue; n     if (s[i] ==  1 ) flag = 1; n     if (flag && i != ind) printf( %c , s[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long solve(int n) { n   int x = n; n   long long cnt = 0, ans = 0; n   while (x) { n     x = x / 10; n     cnt++; n   } n   ans = n * cnt + cnt - 1; n   int k = 1; n   for (int i = 0; i < cnt - 1; i++) { n     k = k * 10; n     ans = ans - k; n   } n   return ans; n } n int main() { n   int n; n   long long ans; n   scanf( %d , &n); n   if (n < 10) { n     cout << n; n   } else { n     cout << solve(n); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline T qmin(const T &a, const T &b) { n   return a < b ? a : b; n } n template <typename T> n inline T qmax(const T &a, const T &b) { n   return a > b ? a : b; n } n template <typename T> n inline void getmin(T &a, const T &b) { n   if (a > b) a = b; n } n template <typename T> n inline void getmax(T &a, const T &b) { n   if (a < b) a = b; n } n inline void fileio(string s) { n   freopen((s +  .in ).c_str(),  r , stdin); n   freopen((s +  .out ).c_str(),  w , stdout); n } n const int inf = (int)1e9 + 7; n const long long linf = (long long)1e17 + 7; n const int N = 2e5 + 7; n struct vec { n   long long x, y; n   inline vec(const long long &_x = 0, const long long &_y = 0) : x(_x), y(_y) {} n   inline vec operator+(const vec &b) { return vec(x + b.x, y + b.y); } n   inline vec operator-(const vec &b) { return vec(x - b.x, y - b.y); } n   inline long long dot(const vec &b) { return x * b.x + y * b.y; } n   inline long long det(const vec &b) { return x * b.y - y * b.x; } n } a[N]; n long long val[N]; n int n; n double ans = 1e18; n inline void calc() { n   for (register int i = (n + 1); i <= (n << 1); ++i) a[i] = a[i - n]; n   for (register int i = (2); i <= (n << 1); ++i) val[i] = a[i].det(a[i - 1]); n   for (register int i = (2); i <= (n << 1); ++i) val[i] += val[i - 1]; n   int lst = 2; n   for (register int i = (1); i <= (n); ++i) { n     vec v1 = a[i + 1] - a[i]; n     long long cur = v1.dot(a[lst] - a[i]), tmp; n     double len = (double)((a[i + 1].x - a[i].x) * (a[i + 1].x - a[i].x)) + n                  (double)((a[i + 1].y - a[i].y) * (a[i + 1].y - a[i].y)); n     while (lst < (n << 1) && (tmp = v1.dot(a[lst + 1] - a[i])) >= cur) n       cur = tmp, ++lst; n     getmin(ans, cur / len * fabs(v1.det(a[lst] - a[i])) - n                     fabs(val[lst] - val[i] + a[i].det(a[lst]))); n   } n } n int main() { n   scanf( %d , &n); n   for (register int i = (1); i <= (n); ++i) scanf( %lld%lld , &a[i].x, &a[i].y); n   ans = 1e18; n   calc(); n   reverse(a + 1, a + n + 1); n   calc(); n   printf( %.10lf n , ans / 2); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int k; n   vector<int> A; n   for (int i = 0; i < 4; ++i) { n     cin >> k; n     A.push_back(k); n   } n   sort(A.begin(), A.end()); n   if (A[0] + A[1] + A[2] == A[3] or A[0] + A[3] == A[1] + A[2]) { n     cout <<  YES ; n   } else { n     cout <<  NO ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n queue<pair<int, int> > q; n int n, s[3][7005], f[3][7005], k[3], dd[3][7005]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   for (int i = 1; i <= 2; i++) { n     cin >> k[i]; n     for (int j = 1; j <= k[i]; j++) cin >> s[i][j]; n   } n   memset(f, 0, sizeof f); n   q.push(pair<int, int>(1, 0)); n   q.push(pair<int, int>(2, 0)); n   f[1][0] = 1; n   f[2][0] = 1; n   while (!q.empty()) { n     int t = q.front().first; n     int u = q.front().second; n     q.pop(); n     if (f[t][u] == 1) { n       for (int i = 1; i <= k[3 - t]; i++) { n         int v = (u + n - s[3 - t][i]) % n; n         if (f[3 - t][v] > 0) continue; n         f[3 - t][v] = 2; n         q.push(pair<int, int>(3 - t, v)); n       } n     } else { n       for (int i = 1; i <= k[3 - t]; i++) { n         int v = (u + n - s[3 - t][i]) % n; n         if (f[3 - t][v] > 0) continue; n         ++dd[3 - t][v]; n         if (dd[3 - t][v] == k[3 - t]) { n           f[3 - t][v] = 1; n           q.push(pair<int, int>(3 - t, v)); n         } n       } n     } n   } n   for (int i = 1; i <= 2; i++) { n     for (int j = 1; j <= n - 1; j++) { n       if (f[i][j] < 1) cout <<  Loop  ; n       if (f[i][j] == 1) cout <<  Lose  ; n       if (f[i][j] > 1) cout <<  Win  ; n     } n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 1e9 + 10; n const double pi = acos(-1.0); n const double eps = 1e-8; n struct node1 { n   int y, x1, x2; n } a[5050]; n int tp1; n struct node2 { n   int x, y1, y2; n } b[5050]; n int tp2; n bitset<5050> bs[5050]; n int main() { n   int n; n   tp1 = tp2 = 0; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     int x1, y1, x2, y2; n     scanf( %d %d %d %d , &x1, &y1, &x2, &y2); n     if (x1 == x2) { n       b[++tp2].x = x1; n       if (y1 > y2) swap(y1, y2); n       b[tp2].y1 = y1; n       b[tp2].y2 = y2; n     } else { n       a[++tp1].y = y1; n       if (x1 > x2) swap(x1, x2); n       a[tp1].x1 = x1; n       a[tp1].x2 = x2; n     } n   } n   if (tp1 > tp2) { n     int tp = max(tp1, tp2); n     for (int i = 1; i <= tp; i++) { n       int tmp = a[i].y; n       a[i].y = b[i].x; n       b[i].x = tmp; n       tmp = a[i].x1; n       a[i].x1 = b[i].y1; n       b[i].y1 = tmp; n       tmp = a[i].x2; n       a[i].x2 = b[i].y2; n       b[i].y2 = tmp; n     } n     swap(tp1, tp2); n   } n   for (int i = 1; i <= tp1; i++) { n     for (int j = 1; j <= tp2; j++) { n       if (a[i].y >= b[j].y1 && a[i].y <= b[j].y2 && b[j].x >= a[i].x1 && n           b[j].x <= a[i].x2) { n         bs[i][j] = 1; n       } n     } n   } n   long long ans = 0; n   for (int i = 1; i < tp1; i++) { n     if (bs[i].count() == 0) continue; n     for (int j = i + 1; j <= tp1; j++) { n       bitset<5050> bb = bs[i] & bs[j]; n       int tmp = bb.count(); n       ans += tmp * (tmp - 1) / 2; n     } n   } n   printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> prime; n int isprime[1000005]; n void Find() { n   for (int i = 2; i < 1000005; i++) { n     if (isprime[i] == 0) { n       prime.push_back(i); n       for (int j = i * 2; j < 1000005; j += i) isprime[j] = 1; n     } n   } n } n vector<int> v[1000005]; n int dis[1000005]; n int from[1000005]; n int main() { n   int n; n   scanf( %d , &n); n   vector<int> vv; n   for (int i = 2; i <= 1000; i++) { n     vv.push_back(i * i); n   } n   vector<int> ok, ok2; n   Find(); n   for (int i = 1; i <= n; i++) { n     int first; n     scanf( %d , &first); n     for (auto it : vv) { n       while (first % it == 0) first /= it; n     } n     if (first == 1) { n       printf( 1 n ); n       return 0; n     } n     if (isprime[first] == 0) n       ok.push_back(first); n     else { n       for (int j = 2; j < 1000; j++) { n         if (first % j == 0) { n           v[first / j].push_back(j); n           v[j].push_back(first / j); n           break; n         } n       } n       ok2.push_back(first); n     } n   } n   sort(ok.begin(), ok.end()); n   for (int i = 1; i < ok.size(); i++) { n     if (ok[i] == ok[i - 1]) { n       printf( 2 n ); n       return 0; n     } n   } n   sort(ok2.begin(), ok2.end()); n   for (int i = 1; i < ok.size(); i++) { n     if (ok[i] == ok[i - 1]) { n       printf( 2 n ); n       return 0; n     } n   } n   queue<int> q; n   memset(dis, -1, sizeof(dis)); n   for (auto it : ok) { n     dis[it] = 0; n     q.push(it); n   } n   int ans = 1e9; n   while (!q.empty()) { n     int first = q.front(); n     q.pop(); n     for (auto it : v[first]) { n       if (dis[it] == -1) { n         dis[it] = dis[first] + 1; n         from[it] = first; n         q.push(it); n       } else { n         if (from[first] == it) n           ; n         else { n           ans = min(ans, dis[it] + dis[first] + 1 + 2); n         } n       } n     } n   } n   for (auto it : prime) { n     if (it > 1000) break; n     for (auto it2 : prime) dis[it2] = -1; n     dis[it] = 0; n     queue<int> q; n     q.push(it); n     while (!q.empty()) { n       int first = q.front(); n       q.pop(); n       for (auto it2 : v[first]) { n         if (dis[it2] == -1) { n           dis[it2] = dis[first] + 1; n           from[it2] = first; n           q.push(it2); n         } else { n           if (from[first] == it2) n             ; n           else { n             ans = min(ans, dis[first] + dis[it2] + 1); n           } n         } n       } n     } n   } n   if (ans == 1e9) { n     printf( -1 n ); n   } else { n     printf( %d n , ans); n   } n } n


#include <bits/stdc++.h> n using namespace std; n struct Node { n   int l, r; n   long long maxi, lzy, maxiwthpre; n }; n struct Line { n   int l, r, v; n   bool operator<(Line L) const { return l < L.l; } n }; n int N, M; n Node seg[800050]; n long long pre[200005]; n Line arr[200005]; n void build(int l, int r, int idx) { n   seg[idx].l = l; n   seg[idx].r = r; n   seg[idx].maxi = seg[idx].maxiwthpre = LLONG_MIN >> 2; n   if (l == r) { n     return; n   } n   int mid = (l + r) >> 1; n   build(l, mid, 2 * idx); n   build(mid + 1, r, 2 * idx + 1); n } n void pd(int idx) { n   seg[2 * idx].maxi += seg[idx].lzy; n   seg[2 * idx].maxiwthpre += seg[idx].lzy; n   seg[2 * idx].lzy += seg[idx].lzy; n   seg[2 * idx + 1].maxi += seg[idx].lzy; n   seg[2 * idx + 1].maxiwthpre += seg[idx].lzy; n   seg[2 * idx + 1].lzy += seg[idx].lzy; n   seg[idx].lzy = 0; n } n void pu(int idx) { n   seg[idx].maxi = max(seg[2 * idx].maxi, seg[2 * idx + 1].maxi); n   seg[idx].maxiwthpre = n       max(seg[2 * idx].maxiwthpre, seg[2 * idx + 1].maxiwthpre); n } n void add(int l, int r, long long v, int idx) { n   if (seg[idx].l == l && seg[idx].r == r) { n     seg[idx].lzy += v; n     seg[idx].maxi += v; n     seg[idx].maxiwthpre += v; n     return; n   } n   int mid = (seg[idx].l + seg[idx].r) >> 1; n   if (seg[idx].lzy) { n     pd(idx); n   } n   if (r <= mid) { n     add(l, r, v, 2 * idx); n   } else if (l > mid) { n     add(l, r, v, 2 * idx + 1); n   } else { n     add(l, mid, v, 2 * idx); n     add(mid + 1, r, v, 2 * idx + 1); n   } n   pu(idx); n } n void setval(int p, long long v, int idx) { n   if (seg[idx].l == seg[idx].r) { n     seg[idx].maxi = v; n     seg[idx].maxiwthpre = v + pre[p]; n     return; n   } n   int mid = (seg[idx].l + seg[idx].r) >> 1; n   if (seg[idx].lzy) { n     pd(idx); n   } n   if (p <= mid) { n     setval(p, v, 2 * idx); n   } else { n     setval(p, v, 2 * idx + 1); n   } n   pu(idx); n } n long long query(int l, int r, int withpre, int idx) { n   if (seg[idx].l == l && seg[idx].r == r) { n     return withpre ? seg[idx].maxiwthpre : seg[idx].maxi; n   } n   int mid = (seg[idx].l + seg[idx].r) >> 1; n   if (seg[idx].lzy) { n     pd(idx); n   } n   if (r <= mid) { n     return query(l, r, withpre, 2 * idx); n   } else if (l > mid) { n     return query(l, r, withpre, 2 * idx + 1); n   } else { n     return max(query(l, mid, withpre, 2 * idx), n                query(mid + 1, r, withpre, 2 * idx + 1)); n   } n } n int main() { n   cin >> N >> M; n   for (int i = 1; i <= N; i++) { n     cin >> pre[i]; n     pre[i] += pre[i - 1]; n   } n   for (int i = 1; i <= M; i++) { n     cin >> arr[i].l >> arr[i].r >> arr[i].v; n   } n   sort(arr + 1, arr + 1 + M); n   build(0, 200000, 1); n   setval(0, 0, 1); n   for (int i = 1; i <= M; i++) { n     add(arr[i].r, 200000, arr[i].v, 1); n     long long n = query(arr[i].r, arr[i].r, 0, 1); n     if (arr[i].l != arr[i].r) { n       n = max(n, n               query(arr[i].l, arr[i].r - 1, 1, 1) - pre[arr[i].r] + arr[i].v); n     } n     n = max(n, query(0, arr[i].l - 1, 0, 1) - pre[arr[i].r] + n                    pre[arr[i].l - 1] + arr[i].v); n     setval(arr[i].r, n, 1); n   } n   cout << seg[1].maxi << endl; n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   long long ct; n   cin >> ct; n   while (ct--) { n     long long n, k; n     cin >> n >> k; n     vector<long long> a(n), was(n + 1), s; n     long long tmp = 0; n     for (long long i = 0; i < (n); ++i) { n       cin >> a[i]; n       if (!was[a[i]]) { n         tmp++; n         was[a[i]]++; n       } n     } n     if (tmp > k) { n       cout <<  -1 n ; n     } else { n       for (long long i = 1; i < (n + 1); ++i) { n         if (tmp < k and !was[i]) { n           s.push_back(i); n           tmp++; n         } n         if (was[i]) s.push_back(i); n       } n       cout << n * s.size() <<   n ; n       for (long long i = 0; i < (n); ++i) { n         for (long long j = 0; j < (s.size()); ++j) { n           cout << s[j] <<    ; n         } n       } n       cout <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T, typename U> n ostream& operator<<(ostream& os, const pair<T, U>& _p) { n   return os <<  (  << _p.first <<  ,  << _p.second <<  ) ; n } n template <typename T> n ostream& operator<<(ostream& os, const vector<T>& _V) { n   bool f = true; n   os <<  [ ; n   for (auto v : _V) { n     os << (f ?    :  , ) << v; n     f = false; n   } n   return os <<  ] ; n } n template <typename T> n ostream& operator<<(ostream& os, const set<T>& _S) { n   bool f = true; n   os <<  ( ; n   for (auto s : _S) { n     os << (f ?    :  , ) << s; n     f = false; n   } n   return os <<  ) ; n } n template <typename T, typename U> n ostream& operator<<(ostream& os, const map<T, U>& _M) { n   return os << set<pair<T, U>>(_M.begin(), _M.end()); n } n const signed long long INF = 1000000100; n const long double EPS = 1e-9; n namespace MCMF { n const int MAXN = 10005; n const int MAXM = 500500; n int nodes, nedge, src, dest; n int point[MAXM], nex[MAXM], flow[MAXM], capa[MAXM], cost[MAXM]; n int head[MAXN], dist[MAXN], expand[MAXN], pre[MAXN], edge[MAXN]; n bool changed[MAXN]; n void init(int n, int s, int t) { n   nodes = n; n   src = s; n   dest = t; n   nedge = 0; n   for (int(i) = (0); (i) < (nodes); (i)++) head[i] = -1; n } n int add_edge(int u, int v, int c, int w) { n   point[nedge] = v, capa[nedge] = c, cost[nedge] = w, flow[nedge] = 0, n   nex[nedge] = head[u], head[u] = (nedge++); n   point[nedge] = u, capa[nedge] = 0, cost[nedge] = -w, flow[nedge] = 0, n   nex[nedge] = head[v], head[v] = (nedge++); n   return nedge - 2; n } n pair<int, int> run() { n   int mflow = 0, mcost = 0; n   while (true) { n     for (int(i) = (0); (i) < (nodes); (i)++) n       dist[i] = INF, pre[i] = -1, changed[i] = 0; n     dist[src] = 0; n     changed[src] = 1; n     expand[src] = INF; n     while (true) { n       bool ok = 1; n       for (int(i) = (0); (i) < (nodes); (i)++) n         if (changed[i]) { n           changed[i] = 0; n           for (int k = head[i]; k >= 0; k = nex[k]) { n             if (flow[k] < capa[k] && dist[i] + cost[k] < dist[point[k]]) { n               dist[point[k]] = dist[i] + cost[k]; n               changed[point[k]] = 1; n               pre[point[k]] = i; n               edge[point[k]] = k; n               expand[point[k]] = min(expand[i], capa[k] - flow[k]); n               ok = 0; n             } n           } n         } n       if (ok) break; n     } n     if (pre[dest] < 0) break; n     int d = expand[dest]; n     mflow += d; n     mcost += d * dist[dest]; n     for (int k = dest; k != src; k = pre[k]) { n       flow[edge[k]] += d; n       flow[edge[k] ^ 1] -= d; n     } n   } n   return make_pair(mflow, mcost); n } n };  // namespace MCMF n int M, N; n const int MAXN = 88; n int A[MAXN][MAXN]; n void read_data() { n   scanf( %d %d , &M, &N); n   for (int(i) = (1); (i) <= (M); (i)++) n     for (int(j) = (1); (j) <= (N); (j)++) scanf( %d , &A[i][j]); n } n int encode(int x, int y) { return x + M * (y - 1); } n bool ok(int x, int y) { return 1 <= x and x <= M and 1 <= y and y <= N; } n int dx[] = {-1, 1, 0, 0}; n int dy[] = {0, 0, -1, 1}; n void solve() { n   int S = M * N + 1; n   int T = M * N + 2; n   MCMF::init(T + 1, S, T); n   for (int(x) = (1); (x) <= (M); (x)++) n     for (int(y) = (1); (y) <= (N); (y)++) { n       if ((x + y) % 2 == 1) { n         MCMF::add_edge(S, encode(x, y), 1, 0); n         for (int(d) = (0); (d) <= (3); (d)++) { n           int nx = x + dx[d]; n           int ny = y + dy[d]; n           if (!ok(nx, ny)) continue; n           int w = (A[x][y] != A[nx][ny]); n           MCMF::add_edge(encode(x, y), encode(nx, ny), 1, w); n         } n       } else { n         MCMF::add_edge(encode(x, y), T, 1, 0); n       } n     } n   cout << MCMF::run().second << endl; n } n int main() { n   read_data(); n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, k, ans; n   int a[105]; n   while (cin >> n >> m >> k) { n     ans = 1000000; n     for (int i = 0; i < n; i++) { n       cin >> a[i]; n     } n     m--; n     for (int i = 1; m - i >= 0; i++) { n       if (a[m - i] <= k && a[m - i]) { n         ans = min(ans, i * 10); n         break; n       } n     } n     for (int i = 1; m + i < n; i++) { n       if (a[m + i] && k >= a[m + i]) { n         ans = min(ans, i * 10); n         break; n       } n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int x, y, n; n   cin >> x >> y >> n; n   int k = (n / x) * x + y; n   if (k <= n) { n     cout << k; n   } else { n     cout << k - x; n   } n   cout << endl; n } n int main() { n   int test_cases; n   cin >> test_cases; n   while (test_cases--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long pw(long long a, long long b, long long md) { n   return (!b ? 1 n              : (b & 1 ? a * pw(a * a % md, b / 2, md) % md n                       : pw(a * a % md, b / 2, md) % md)); n } n const long long MOD = 1e9 + 7, MAXN = 1e6 + 10; n bool prime[MAXN]; n long long n, ans, p; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   cin >> n; n   for (long long i = 0; i < MAXN; i++) prime[i] = true; n   for (int p = 2; p * p <= n; p++) { n     if (prime[p] == true) { n       for (int i = p * 2; i <= n; i += p) prime[i] = false; n     } n   } n   for (long long i = 1; i <= n; i++) { n     p = 0; n     for (long long j = 2; j <= i; j++) { n       if (i % j == 0 && prime[j]) p++; n     } n     if (p == 2) ans++; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1000000007; n const int MAX = 1000005; n int SetBit(int n, int x) { return n | (1 << x); } n int ClearBit(int n, int x) { return n & ~(1 << x); } n int ToggleBit(int n, int x) { return n ^ (1 << x); } n bool CheckBit(int n, int x) { return (bool)(n & (1 << x)); } n long long gcd(long long a, long long b) { n   if (b == 0) n     return a; n   else n     return gcd(b, a % b); n } n int main(void) { n   long long l, r, a, b, x, y, G, L, i, j; n   while (scanf( %lld %lld %lld %lld , &l, &r, &a, &b) != EOF) { n     vector<int> v; n     for (i = 1; i * i <= b; i++) { n       if (b % i == 0) { n         v.push_back(i); n         if (i != b / i) v.push_back(b / i); n       } n     } n     sort(v.begin(), v.end()); n     int len = v.size(); n     int ans = 0; n     for (i = 0; i < len; i++) { n       for (j = 0; j < len; j++) { n         x = v[i], y = v[j]; n         G = gcd(x, y); n         L = (x * y) / G; n         if (G == a && L == b && x >= l && x <= r && y >= l && y <= r) ++ans; n       } n     } n     printf( %d n , ans); n     v.clear(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n static inline void canhazfast() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n } n template <typename T> n T gcd(T a, T b) { n   return b == 0 ? a : gcd(b, a % b); n } n template <typename T> n T extgcd(T a, T b, T &x, T &y) { n   T x0 = 1, y0 = 0, x1 = 0, y1 = 1; n   while (b) { n     T q = a / b; n     a %= b; n     swap(a, b); n     x0 -= q * x1; n     swap(x0, x1); n     y0 -= q * y1; n     swap(y0, y1); n   } n   x = x0; n   y = y0; n   return a; n } n static inline int ctz(unsigned x) { return __builtin_ctz(x); } n static inline int ctzll(unsigned long long x) { return __builtin_ctzll(x); } n static inline int clz(unsigned x) { return __builtin_clz(x); } n static inline int clzll(unsigned long long x) { return __builtin_clzll(x); } n static inline int popcnt(unsigned x) { return __builtin_popcount(x); } n static inline int popcntll(unsigned long long x) { n   return __builtin_popcountll(x); n } n static inline int bsr(unsigned x) { return 31 ^ clz(x); } n static inline int bsrll(unsigned long long x) { return 63 ^ clzll(x); } n int main() { n   canhazfast(); n   static long long a[200016], x[200016], b[200016]; n   int n; n   long long t, lb; n   cin >> n >> t; n   for (int i = 1; i <= n; ++i) cin >> a[i]; n   for (int i = 1; i <= n; ++i) cin >> x[i]; n   if (x[n] != n) return cout <<  No , 0; n   lb = a[n]; n   b[n] = a[n] + n; n   for (int i = n - 1; i > 0; --i) { n     b[i] = b[i + 1] - 1; n     if (x[i] == i) n       b[i] = lb - 1; n     else if (x[i] != x[i + 1] || b[i] < lb) n       return cout <<  No , 0; n     lb = a[i]; n   } n   cout <<  Yes n ; n   for (int i = 1; i <= n; ++i) cout << b[i] + t <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { n   if (a % b == 0) n     return b; n   else n     return gcd(b, a % b); n } n void palindrome(string s) { n   int i = 0; n   int j = s.size() - 1; n   while (i < j) { n     if (s[i] != s[j]) return; n     i++; n     j--; n   } n   cout << s; n   exit(0); n } n int recursive(int n, int a[]) { n   int x = 1; n   if (a[n] != 0) return a[n]; n   if (n < 10) return n; n   while (n != 0) { n     if (n % 10 != 0) x = x * (n % 10); n     n = n / 10; n   } n   return recursive(x, a); n } n int myXOR(int x, int y) { return (x | y) & (~x | ~y); } n int main() { n   string s; n   cin >> s; n   int a, b; n   if (s.size() >= 2) { n     a = s[s.size() - 2] -  0 ; n     b = s[s.size() - 1] -  0 ; n     a = 10 * a + b; n     if (a % 4 == 0) n       cout << 4; n     else n       cout << 0; n   } n   if (s.size() == 1) { n     int x = s[0] -  0 ; n     if (x % 4 == 0) n       cout << 4; n     else n       cout << 0; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int sum(int[][3], int, int); n bool valid(int); n int main() { n   int A[3][3]; n   for (int i = 0; i < 3; ++i) n     for (int j = 0; j < 3; ++j) cin >> A[i][j]; n   for (int i = 0; i < 3; ++i) { n     for (int j = 0; j < 3; ++j) cout << !(sum(A, i, j) % 2); n     cout <<   n ; n   } n   return 0; n } n bool valid(int x) { return (x > -1 && x < 3); } n int sum(int A[][3], int i, int j) { n   int s = 0, k; n   for (k = -1; k < 2; ++k) { n     int I = i + k; n     if (valid(I)) s += A[I][j]; n     if (k) { n       int J = j + k; n       if (valid(J)) s += A[i][J]; n     } n   } n   return s; n } n


#include <bits/stdc++.h> n using namespace std; n int const mod = 1e9 + 7; n inline void SAFE_ADD(int& a, int b) { n   a += b; n   if (a >= mod) a -= mod; n } n struct Matr { n   vector<vector<int> > _data; n   Matr(int sz) : _data(sz, vector<int>(sz, 0)) {} n   void ones() { n     for (int i = 0; i < _data.size(); ++i) _data[i][i] = 1; n   } n }; n Matr operator*(Matr const& a, Matr const& b) { n   int n = a._data.size(); n   Matr ans(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         SAFE_ADD(ans._data[i][k], a._data[i][j] * 1LL * b._data[j][k] % mod); n       } n   return ans; n } n inline int getnum(char c) { n   if (c >=  a  && c <=  z ) n     return c -  a ; n   else n     return 26 + c -  A ; n } n Matr pow(Matr const& a, long long b) { n   Matr ans(a._data.size()); n   ans.ones(); n   Matr tmp(a); n   while (b) { n     if (b & 1) ans = ans * tmp; n     tmp = tmp * tmp; n     b >>= 1; n   } n   return ans; n } n int main() { n   ios_base::sync_with_stdio(false); n   long long n; n   int m, k; n   cin >> n >> m >> k; n   if (n == 1) { n     cout << m <<   n ; n     return 0; n   }; n   Matr a(m); n   for (int i = 0; i < m; ++i) n     for (int j = 0; j < m; ++j) a._data[i][j] = 1; n   for (int i = 0; i < k; ++i) { n     string tmp; n     cin >> tmp; n     a._data[getnum(tmp[0])][getnum(tmp[1])] = 0; n   } n   a = pow(a, n - 1); n   int ans = 0; n   for (int i = 0; i < m; ++i) n     for (int j = 0; j < m; ++j) SAFE_ADD(ans, a._data[i][j]); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_N = 100 + 10; n int a[MAX_N]; n int main() { n   int n, count = 0, f = 0, h = 0, b; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> b; n     if (b < 0) count++; n     if (count == 3) { n       f++; n       a[f] = i - h; n       h += a[f]; n       count = 1; n     } n     if (count < 3 && i == n - 1) { n       f++; n       a[f] = i + 1 - h; n       h += a[f]; n     } n   } n   cout << f << endl; n   for (int i = 1; i <= f; i++) cout << a[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100005; n const int maxm = 300005; n int n, m, cnt1, cnt2, ans; n int head[maxn], Head[maxn]; n int dis1[maxn], vis1[maxn]; n int use1[maxn], use2[maxn]; n int num[maxn], ok[maxn]; n struct nood { n   int nex, to, w; n   int fr; n }; n nood e[maxm], E[maxm]; n inline void jia(int u, int v, int c) { n   e[++cnt1].nex = head[u]; n   e[cnt1].fr = u; n   head[u] = cnt1; n   e[cnt1].to = v; n   e[cnt1].w = c; n } n inline void Jia(int u, int v, int c) { n   E[++cnt2].nex = Head[u]; n   Head[u] = cnt2; n   E[cnt2].to = v; n   E[cnt2].w = c; n } n inline void dfs(int u) { n   use1[u] = 1; n   for (int i = head[u]; i; i = e[i].nex) { n     int v = e[i].to; n     if (use1[v]) continue; n     dfs(v); n   } n } n inline void Dfs(int u) { n   use2[u] = 1; n   for (int i = Head[u]; i; i = E[i].nex) { n     int v = E[i].to; n     if (use2[v]) continue; n     Dfs(v); n   } n } n inline void spfa(int s) { n   queue<int> q; n   memset(dis1, 127 / 3, sizeof(dis1)); n   memset(vis1, 0, sizeof(vis1)); n   q.push(s), dis1[s] = 0; n   num[s] = 1, vis1[s] = 1; n   while (!q.empty()) { n     int u = q.front(); n     q.pop(); n     vis1[u] = 0; n     for (int i = Head[u]; i; i = E[i].nex) { n       int v = E[i].to; n       if (dis1[v] > dis1[u] + E[i].w) { n         dis1[v] = dis1[u] + E[i].w; n         if (!vis1[v]) { n           vis1[v] = 1; n           q.push(v); n           num[v]++; n         } n         if (num[v] > n) { n           printf( No n ); n           exit(0); n         } n       } n     } n   } n } n inline int read() { n   int sum = 0; n   char ch = getchar(); n   while (!isdigit(ch)) ch = getchar(); n   while (isdigit(ch)) sum = sum * 10 + (ch ^ 48), ch = getchar(); n   return sum; n } n int main() { n   n = read(); n   m = read(); n   for (int i = 1; i <= m; i++) { n     int u = read(); n     int v = read(); n     jia(u, v, 0); n     Jia(v, u, 0); n   } n   dfs(1); n   Dfs(n); n   for (int i = 1; i <= n; i++) n     if (use1[i] && use2[i]) ok[i] = 1; n   if (!use1[n]) return printf( No n ), 0; n   memset(E, 0, sizeof(E)); n   memset(Head, 0, sizeof(Head)); n   cnt2 = 0; n   ok[1] = 1, ok[n] = 1; n   for (int i = 1; i <= m; i++) { n     int u = e[i].fr; n     int v = e[i].to; n     if (ok[u] && ok[v]) { n       Jia(u, v, -1); n       Jia(v, u, 2); n     } n   } n   spfa(1); n   printf( Yes n ); n   for (int i = 1; i <= m; i++) { n     int u = e[i].fr; n     int v = e[i].to; n     if (ok[u] && ok[v]) n       printf( %d n , abs(dis1[u] - dis1[v])); n     else n       printf( 1 n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_N = 2e5; n vector<int> gr[MAX_N]; n int indx(int h, int t) { return h * 450 + t; } n int win = 1e9; n int lose = 0; n bool draw = false; n int used[MAX_N] = {0}; n void bfs(int st_indx) { n   queue<pair<int, int>> q; n   q.push({0, st_indx}); n   used[st_indx] = 1; n   while (!q.empty()) { n     auto [d, v] = q.front(); n     q.pop(); n     if (v == 0) { n       win = min(win, d); n     } n     for (auto e : gr[v]) { n       if (!used[e]) { n         used[e] = 1; n         q.push({d + 1, e}); n       } n     } n   } n } n void dfs(int v) { n   used[v] = 1; n   for (auto e : gr[v]) { n     if (used[e] == 1) { n       draw = true; n     } else if (!used[e]) { n       dfs(e); n     } n   } n   used[v] = 2; n } n int d[MAX_N]; n void dij(int st) { n   fill(d, d + MAX_N, 1e9); n   set<pair<int, int>> s; n   d[st] = 0; n   s.insert({0, st}); n   while (!s.empty()) { n     int v = s.begin()->second; n     if (gr[v].empty()) { n       lose = max(lose, -d[v]); n     } n     s.erase(s.begin()); n     for (auto e : gr[v]) { n       if (d[e] > d[v] - 1) { n         s.erase({d[e], e}); n         d[e] = d[v] - 1; n         s.insert({d[e], e}); n       } n     } n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   int h, t, r, n; n   cin >> h >> t >> r >> n; n   vector<pair<int, int>> a(n + 1); n   for (int i = 1; i <= n; ++i) { n     cin >> a[i].first >> a[i].second; n   } n   int m; n   cin >> m; n   vector<pair<int, int>> b(m + 1); n   for (int i = 1; i <= m; ++i) { n     cin >> b[i].first >> b[i].second; n   } n   for (int h1 = 0; h1 <= r; ++h1) { n     for (int t1 = 0; t1 <= r; ++t1) { n       if (h1 + t1 > r || !(h1 + t1)) { n         continue; n       } n       for (int chop_h = 1; chop_h <= min(h1, n); ++chop_h) { n         int h2 = h1 - chop_h + a[chop_h].first; n         int t2 = t1 + a[chop_h].second; n         gr[indx(h1, t1)].push_back(indx(h2, t2)); n       } n       for (int chop_t = 1; chop_t <= min(t1, m); ++chop_t) { n         int h2 = h1 + b[chop_t].first; n         int t2 = t1 - chop_t + b[chop_t].second; n         gr[indx(h1, t1)].push_back(indx(h2, t2)); n       } n     } n   } n   bfs(indx(h, t)); n   memset(used, 0, sizeof(used)); n   dfs(indx(h, t)); n   if (win != 1e9) { n     cout <<  Ivan n  << win; n   } else if (draw) { n     cout <<  Draw ; n   } else { n     dij(indx(h, t)); n     cout <<  Zmey n  << lose; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   int a[n]; n   for (int i = 0; i < n; ++i) cin >> a[i]; n   m--; n   int cnt = 0; n   for (int i = 0; i < n; ++i) n     if (1) { n       bool ok = 0; n       for (int j = i + m; j >= 0 && j >= i - m; --j) n         if (j >= n) n           continue; n         else if (a[j] == 1) { n           cnt++; n           i = j + m; n           ok = 1; n           break; n         } n       if (!ok) return cout << -1, 0; n     } n   cout << cnt; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 250; n int a[N + 5], ans; n double fac[20005]; n double POI(int P) { n   double ans = 0; n   double v = log(P); n   for (int i = (int)(1); i <= (int)(N); i++) n     if (a[i] < 0) n       return -1e300; n     else n       ans += v * a[i] - P - fac[a[i]]; n   return ans; n } n double UNI(int P) { n   double ans = 0; n   double v = log(2 * P + 1); n   for (int i = (int)(1); i <= (int)(N); i++) n     if (a[i] < 0 || a[i] > 2 * P) n       return -1e300; n     else n       ans -= v; n   return ans; n } n void solve() { n   double pos = -1e300; n   for (int i = (int)(1); i <= (int)(N); i++) scanf( %d , &a[i]); n   for (int i = (int)(1); i <= (int)(1000); i++) { n     for (int j = (int)(1); j <= (int)(N); j++) ++a[j]; n     double v1 = POI(i), v2 = UNI(i); n     if (v1 > pos) pos = v1, ans = 0; n     if (v2 > pos) pos = v2, ans = 1; n   } n   puts(ans ?  uniform  :  poisson ); n } n int main() { n   for (int i = (int)(1); i <= (int)(10000); i++) fac[i] = fac[i - 1] + log(i); n   int T; n   scanf( %d , &T); n   while (T--) solve(); n } n