#include <bits/stdc++.h> n using namespace std; n pair<int, pair<int, int> > A[2003 * 2003], B[2003 * 2003]; n pair<int, int> a[2003], b[2003]; n int m, n, r, s, x, y, i, j, asd; n long long t1, t2, mn; n bool h; n int main() { n   cin >> m; n   for (i = 1; i <= m; i++) { n     cin >> a[i].first; n     t1 += a[i].first; n     a[i].second = i; n   } n   cin >> n; n   for (i = 1; i <= n; i++) { n     cin >> b[i].first; n     t2 += b[i].first; n     b[i].second = i; n   } n   for (i = 1; i <= m; i++) n     for (j = i + 1; j <= m; j++) n       A[++r] = make_pair(a[i].first + a[j].first, make_pair(i, j)); n   for (i = 1; i <= n; i++) n     for (j = i + 1; j <= n; j++) n       B[++s] = make_pair(b[i].first + b[j].first, make_pair(i, j)); n   sort(a + 1, a + m + 1); n   sort(b + 1, b + n + 1); n   sort(A + 1, A + r + 1); n   sort(B + 1, B + s + 1); n   if (t1 < t2) { n     h = 1; n     swap(t1, t2); n     swap(m, n); n     swap(r, s); n     swap(a, b); n     swap(A, B); n   } n   mn = t1 - t2; n   for (i = j = 1; i <= m; i++) { n     while (abs(t1 - a[i].first + b[j].first - (t2 - b[j].first + a[i].first)) > n                abs(t1 - a[i].first + b[j + 1].first - n                    (t2 - b[j + 1].first + a[i].first)) && n            j < n) n       j++; n     if (mn > n         abs(t1 - a[i].first + b[j].first - (t2 - b[j].first + a[i].first))) { n       mn = abs(t1 - a[i].first + b[j].first - (t2 - b[j].first + a[i].first)); n       asd = 1; n       x = i; n       y = j; n     } n   } n   for (i = j = 1; i <= r && s; i++) { n     while (abs(t1 - A[i].first + B[j].first - (t2 - B[j].first + A[i].first)) > n                abs(t1 - A[i].first + B[j + 1].first - n                    (t2 - B[j + 1].first + A[i].first)) && n            j < s) n       j++; n     if (mn > n         abs((t1 - A[i].first + B[j].first) - (t2 - B[j].first + A[i].first))) { n       mn = abs((t1 - A[i].first + B[j].first) - (t2 - B[j].first + A[i].first)); n       asd = 2; n       x = i; n       y = j; n     } n   } n   cout << mn << endl << asd << endl; n   if (asd == 1) { n     if (!h) n       cout << a[x].second <<     << b[y].second; n     else n       cout << b[y].second <<     << a[x].second; n   } else if (asd) { n     if (!h) { n       cout << A[x].second.first <<     << B[y].second.first << endl; n       cout << A[x].second.second <<     << B[y].second.second; n     } else { n       cout << B[y].second.first <<     << A[x].second.first << endl; n       cout << B[y].second.second <<     << A[x].second.second; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t, i, j, n, x, y; n   cin >> x >> y; n   bool c = 0; n   char a; n   for (i = 0; i < x; i++) { n     for (j = 0; j < y; j++) { n       cin >> a; n       if (a !=  W  && a !=  B  && a !=  G ) c = 1; n     } n   } n   if (c) n     puts( #Color ); n   else n     puts( #Black&White ); n   return 0; n } n


#include <bits/stdc++.h> n double const EPS = 3e-8; n using namespace std; n template <class T> n T gcd(T a, T b) { n   return (b != 0 ? gcd<T>(b, a % b) : a); n } n template <class T> n T lcm(T a, T b) { n   return (a / gcd<T>(a, b) * b); n } n struct debugger { n   template <typename T> n   debugger& operator,(const T& v) { n     cerr << v <<   t ; n     return *this; n   } n } dbg; n template <class A, class B> n ostream& operator<<(ostream& o, const pair<A, B>& p) { n   return o <<  (  << p.first <<  ,   << p.second <<  ) ; n } n template <class T> n ostream& operator<<(ostream& o, const vector<T>& v) { n   o <<  [ ; n   for (__typeof((v).end()) it = (v).begin(); it != (v).end(); ++it) n     o << *it <<  ,  ; n   return o <<  ] ; n } n template <class T> n ostream& operator<<(ostream& o, const set<T>& v) { n   o <<  [ ; n   for (__typeof((v).end()) it = (v).begin(); it != (v).end(); ++it) n     o << *it <<  ,  ; n   return o <<  ] ; n } n template <class T> n inline bool read(T& x) { n   int c = getchar(); n   int sgn = 1; n   while (~c && c <  0  || c >  9 ) { n     if (c ==  - ) sgn = -1; n     c = getchar(); n   } n   for (x = 0; ~c &&  0  <= c && c <=  9 ; c = getchar()) x = x * 10 + c -  0 ; n   x *= sgn; n   return ~c; n } n int dx[] = {1, 0, -1, 0}; n int dy[] = {0, 1, 0, -1}; n const int NX = 1e6 + 10; n int fromFront[NX], fromBack[NX]; n string a, b; n void solve(int cs) { n   cin >> a >> b; n   int asz = a.size(); n   int bsz = b.size(); n   int backIdx = bsz - 1; n   int cur = 0; n   for (int i = asz - 1; i >= 0; i--) { n     if (backIdx >= 0 && a[i] == b[backIdx]) { n       backIdx--; n       cur++; n     } n     fromBack[i] = cur; n   } n   int frontIdx = 0; n   cur = 0; n   for (int i = 0; i < asz; i++) { n     if (frontIdx < bsz && a[i] == b[frontIdx]) { n       frontIdx++; n       cur++; n     } n     fromFront[i] = cur; n   } n   int low = 0, high = 0; n   int ans = 0; n   while (high < asz) { n     int f = 0, b = 0; n     if (low > 0) f = fromFront[low - 1]; n     if (high + 1 < asz) b = fromBack[high + 1]; n     if (f + b >= bsz) { n       ans = max(ans, high - low + 1); n     } else { n       while (f + b < bsz && low <= high) { n         low++; n         f = fromFront[low - 1]; n         if (high + 1 < asz) b = fromBack[high + 1]; n       } n     } n     high++; n   } n   printf( %d n , ans); n } n int main() { n   int t = 1; n   for (int cs = 1; cs <= t; cs++) solve(cs); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n, k; n   while (~scanf( %d%d , &n, &k)) { n     int ant[55] = {0}; n     int temp = 0; n     int count = k; n     for (int i = 1; i <= n; i++) { n       scanf( %d , &ant[i - 1]); n       if (i == k) temp = ant[i - 1]; n     } n     if (temp) n       for (int i = k + 1; i <= n; i++) { n         if (ant[i - 1] >= temp) n           count++; n         else n           break; n       } n     else n       for (int i = k; i >= 1; i--) { n         if (!ant[i - 1]) count--; n       } n     printf( %d n , count); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void err(istream_iterator<string> it) {} n template <typename T, typename... Args> n void err(istream_iterator<string> it, T a, Args... args) { n   cerr <<     << *it <<   =   << a; n   err(++it, args...); n } n template <typename T, typename U> n inline void min_self(T& x, U y) { n   if (y < x) x = y; n } n template <typename T, typename U> n inline void max_self(T& x, U y) { n   if (x < y) x = y; n } n template <class T1, class T2> n ostream& operator<<(ostream& out, pair<T1, T2> pair) { n   return out <<  (  << pair.first <<  ,   << pair.second <<  ) ; n } n template <class T> n ostream& operator<<(ostream& out, vector<T> vec) { n   out <<  ( ; n   for (auto& v : vec) out << v <<  ,  ; n   return out <<  ) ; n } n template <class T> n ostream& operator<<(ostream& out, set<T> vec) { n   out <<  ( ; n   for (auto& v : vec) out << v <<  ,  ; n   return out <<  ) ; n } n template <class L, class R> n ostream& operator<<(ostream& out, map<L, R> vec) { n   out <<  ( ; n   for (auto& v : vec) out <<  [  << v.first <<  ,   << v.second <<  ] ; n   return out <<  ) ; n } n template <class A, class B> n istream& operator>>(istream& in, pair<A, B>& a) { n   return in >> a.first >> a.second; n } n template <class A> n istream& operator>>(istream& in, vector<A>& a) { n   for (A& i : a) in >> i; n   return in; n } n long long XX[4] = {-1, 0, 1, 0}; n long long YY[4] = {0, -1, 0, 1}; n void solve() { n   long long n, res = 0; n   cin >> n; n   vector<pair<long long, long long>> a(n); n   for (long long i = 0; i <= n - 1; i++) cin >> a[i]; n   sort(a.begin(), a.end()); n   long long maxi = a[0].second; n   for (long long i = 1; i <= n - 1; i++) { n     if (a[i].second < maxi && a[i].first > a[i - 1].first) { n       res++; n     } n     max_self(maxi, a[i].second); n   } n   cout << res <<   n ; n   return; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int N, K; n   cin >> N >> K; n   double res = 0; n   long double t1x, t1y, t2x, t2y; n   cin >> t1x >> t1y; n   for (int i = 1; i < N; ++i) { n     t2x = t1x; n     t2y = t1y; n     cin >> t1x >> t1y; n     res += sqrt((t1x - t2x) * (t1x - t2x) + (t1y - t2y) * (t1y - t2y)); n   } n   printf( %.15lf n , res * K / 50); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100005; n int down[N], sz[N], up[N], n; n vector<pair<int, bool> > adj[N]; n void dfs(int s, int p) { n   sz[s] = 1; n   for (auto it : adj[s]) { n     if (it.first != p) { n       dfs(it.first, s); n       sz[s] += sz[it.first]; n       if (it.second) n         down[s] += sz[it.first]; n       else n         down[s] += down[it.first]; n     } n   } n } n void dfs2(int s, int p) { n   for (auto it : adj[s]) { n     if (it.first != p) { n       if (it.second) n         up[it.first] = n - sz[it.first]; n       else n         up[it.first] = down[s] - down[it.first] + up[s]; n       dfs2(it.first, s); n     } n   } n } n int main() { n   int i; n   scanf( %d , &n); n   for (i = 0; i < n - 1; i++) { n     int a, b, c, te; n     bool flag = true; n     scanf( %d%d%d , &a, &b, &c); n     while (c > 0) { n       if (c % 10 == 4 || c % 10 == 7) n         ; n       else n         flag = false; n       c /= 10; n     } n     adj[a - 1].push_back(make_pair(b - 1, flag)); n     adj[b - 1].push_back(make_pair(a - 1, flag)); n   } n   long long ans = 0; n   dfs(0, -1); n   dfs2(0, -1); n   for (i = 0; i < n; i++) { n     long long temp = up[i] + down[i]; n     ans += temp * (temp - 1); n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int one, two, three, four; n bool check(deque<int> x) { n   int cnt1 = 0, cnt2 = 0, cnt3 = 0, cnt4 = 0; n   for (int i = 0; i < x.size(); i++) { n     if (x[i] == 0) cnt1++; n     if (x[i] == 1) cnt2++; n     if (x[i] == 2) cnt3++; n     if (x[i] == 3) cnt4++; n   } n   if (cnt1 == one && cnt2 == two && cnt3 == three & cnt4 == four) { n     for (int i = 0; i + 1 < x.size(); i++) { n       if (abs(x[i] - x[i + 1]) != 1) return 0; n     } n     cout <<  YES  <<   n ; n     for (int i : x) cout << i <<    ; n     exit(0); n   } n   return 0; n } n deque<int> merge(deque<int> x, deque<int> y) { n   for (int i : y) x.push_back(i); n   return x; n } n void go2(pair<int, int> a, pair<int, int> b, deque<int> ans) { n   deque<int> ans2 = ans; n   if (a.first < b.first) { n     for (int i = 0; i < a.first; i++) { n       ans.push_back(b.second); n       ans.push_back(a.second); n     } n     ans.push_back(b.second); n   } else { n     for (int i = 0; i < b.first; i++) { n       ans.push_back(a.second); n       ans.push_back(b.second); n     } n     if (a.first != b.first) ans.push_back(a.second); n   } n   check(ans); n   ans = ans2; n   if (a.first <= b.first) { n     for (int i = 0; i < a.first; i++) { n       ans.push_back(b.second); n       ans.push_back(a.second); n     } n     if (a.first != b.first) ans.push_back(b.second); n   } else { n     for (int i = 0; i < b.first; i++) { n       ans.push_back(a.second); n       ans.push_back(b.second); n     } n     if (a.first != b.first) ans.push_back(a.second); n   } n   ans = ans2; n   deque<int> zl; n   if (a.first < b.first) { n     for (int i = 0; i < a.first; i++) { n       zl.push_back(b.second); n       zl.push_back(a.second); n     } n     zl.push_back(b.second); n   } else { n     for (int i = 0; i < b.first; i++) { n       zl.push_back(a.second); n       zl.push_back(b.second); n     } n     if (a.first != b.first) zl.push_back(a.second); n   } n   check(merge(zl, ans)); n   zl.clear(); n   if (a.first <= b.first) { n     for (int i = 0; i < a.first; i++) { n       zl.push_back(b.second); n       zl.push_back(a.second); n     } n     if (a.first != b.first) zl.push_back(b.second); n   } else { n     for (int i = 0; i < b.first; i++) { n       zl.push_back(a.second); n       zl.push_back(b.second); n     } n     if (a.first != b.first) zl.push_back(a.second); n   } n   check(merge(zl, ans)); n } n void go3(pair<int, int> a, pair<int, int> b, pair<int, int> c, deque<int> cur) { n   deque<int> ans; n   if (a.first <= b.first) { n     for (int i = 0; i < a.first; i++) { n       ans.push_back(a.second); n       ans.push_back(b.second); n     } n     go2(pair<int, int>(b.first - a.first, b.second), c, merge(cur, ans)); n     go2(pair<int, int>(b.first - a.first, b.second), c, merge(ans, cur)); n     ans.clear(); n     for (int i = 0; i < a.first; i++) { n       ans.push_back(b.second); n       ans.push_back(a.second); n     } n     go2(pair<int, int>(b.first - a.first, b.second), c, merge(cur, ans)); n     go2(pair<int, int>(b.first - a.first, b.second), c, merge(ans, cur)); n   } n   ans.clear(); n   if (c.first <= b.first) { n     for (int i = 0; i < c.first; i++) { n       ans.push_back(c.second); n       ans.push_back(b.second); n     } n     go2(a, pair<int, int>(b.first - c.first, b.second), merge(cur, ans)); n     go2(a, pair<int, int>(b.first - c.first, b.second), merge(ans, cur)); n     ans.clear(); n     for (int i = 0; i < c.first; i++) { n       ans.push_back(b.second); n       ans.push_back(c.second); n     } n     go2(a, pair<int, int>(b.first - c.first, b.second), merge(cur, ans)); n     go2(a, pair<int, int>(b.first - c.first, b.second), merge(ans, cur)); n   } n } n void go4(int a, int b, int c, int d) { n   if (a <= b) { n     deque<int> lul; n     for (int i = 0; i < a; i++) { n       lul.push_back(0); n       lul.push_back(1); n     } n     go3(pair<int, int>(b - a, 1), pair<int, int>(c, 2), pair<int, int>(d, 3), n         lul); n     lul.clear(); n     for (int i = 0; i < a; i++) { n       lul.push_back(1); n       lul.push_back(0); n     } n     go3(pair<int, int>(b - a, 1), pair<int, int>(c, 2), pair<int, int>(d, 3), n         lul); n   } n   if (d <= c) { n     deque<int> lul; n     for (int i = 0; i < d; i++) { n       lul.push_back(3); n       lul.push_back(2); n     } n     go3(pair<int, int>(a, 0), pair<int, int>(b, 1), pair<int, int>(c - d, 2), n         lul); n     lul.clear(); n     for (int i = 0; i < d; i++) { n       lul.push_back(2); n       lul.push_back(3); n     } n     go3(pair<int, int>(a, 0), pair<int, int>(b, 1), pair<int, int>(c - d, 2), n         lul); n   } n } n int main() { n   cin >> one >> two >> three >> four; n   go4(one, two, three, four); n   cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long range(long long L) { n   switch (L & 3) { n     case 0: n       return L; n     case 1: n       return 1; n     case 2: n       return L + 1; n     case 3: n       return 0; n   } n } n int main() { n   for (int N; cin >> N;) { n     long long sum = 0; n     for (int i = 0; i < (int)(N); ++i) { n       long long m, x; n       cin >> x >> m; n       long long b = range(x - 1); n       long long a = range(x + m - 1); n       sum ^= b; n       sum ^= a; n     } n     if (sum == 0) n       cout <<  bolik  << endl; n     else n       cout <<  tolik  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct trie { n   char ch; n   int son, next, father, suffix; n   vector<int> danger; n   int sum; n }; n trie a[205]; n int now, m; n void clear(int x) { n   a[x].son = a[x].next = 0; n   a[x].danger.clear(); n } n void insert(char *s, int l, int t, int x) { n   if (!a[x].son) { n     a[x].son = ++m; n     clear(m); n     a[m].father = x; n     a[m].ch = s[t]; n     if (t + 1 == l) n       a[m].danger.push_back(now); n     else n       insert(s, l, t + 1, m); n     return; n   } n   int i = a[x].son; n   while (1) { n     if (!a[i].next || a[i].ch == s[t]) break; n     i = a[i].next; n   } n   if (a[i].ch == s[t] && t + 1 == l) n     a[i].danger.push_back(now); n   else if (a[i].ch == s[t]) n     insert(s, l, t + 1, i); n   else { n     a[i].next = ++m; n     clear(m); n     a[m].father = x; n     a[m].ch = s[t]; n     if (t + 1 == l) n       a[m].danger.push_back(now); n     else n       insert(s, l, t + 1, m); n   } n } n int q[205]; n int child(int x, char ch) { n   for (int i = a[x].son; i; i = a[i].next) n     if (a[i].ch == ch) return (i); n   if (x == 1) return (1); n   return (child(a[x].suffix, ch)); n } n void build_trie() { n   int l, r; n   l = r = 1; n   q[1] = 1; n   while (l <= r) { n     int x = q[l++]; n     for (int i = a[x].son; i; i = a[i].next) q[++r] = i; n   } n   a[1].suffix = 1; n   for (int i = 2; i <= r; i++) { n     int x = q[i]; n     if (a[x].father == 1) { n       a[x].suffix = 1; n       continue; n     } n     a[x].suffix = child(a[a[x].father].suffix, a[x].ch); n     for (int j = 0; j < (int)a[a[x].suffix].danger.size(); j++) n       a[x].danger.push_back(a[a[x].suffix].danger[j]); n   } n } n int n, base, k; n void read(char *s) { n   int l; n   scanf( %d , &l); n   s[l] =   0 ; n   for (int i = 0; i < l; ++i) { n     int a; n     scanf( %d , &a); n     s[i] = a +  a ; n   } n } n int v[205]; n char l[205], r[205], s[205]; n const int MOD = 1000000007; n int curl; n char cur[205]; n int f[205][205][505]; n int dp(int i, int state, int sum, bool cmp, bool first) { n   if (sum > k) { n     return 0; n   } n   if (i == 0) { n     return 1; n   } n   if (!first && !cmp && f[i][state][sum] != -1) { n     return f[i][state][sum]; n   } n   int ret = 0; n   int r = cur[curl - i] -  a ; n   for (int d = first; d < base; ++d) { n     if (cmp && d > r) { n       continue; n     } n     int nstate = child(state, d +  a ), nsum = sum + a[nstate].sum; n     bool ncmp = cmp && d == r; n     ret += dp(i - 1, nstate, nsum, ncmp, false); n     if (ret >= MOD) { n       ret -= MOD; n     } n   } n   if (!cmp && !first) { n     f[i][state][sum] = ret; n   } n   return ret; n } n int cal(char *s) { n   int l = strlen(s); n   curl = l; n   for (int i = 0; i < l; ++i) { n     cur[i] = s[i]; n   } n   int ret = 0; n   while (curl) { n     ret += dp(curl, 1, 0, curl == l, true); n     --curl; n     if (ret >= MOD) { n       ret -= MOD; n     } n   } n   return ret; n } n int check(char *s) { n   int sum = 0, cur = 1, l = strlen(s); n   for (int i = 0; i < l; ++i) { n     cur = child(cur, s[i]); n     sum += a[cur].sum; n   } n   return sum <= k; n } n int main() { n   memset(f, -1, sizeof(f)); n   scanf( %d%d%d , &n, &base, &k); n   read(l), read(r); n   clear(m = 1); n   for (int i = 0; i < n; i++) { n     read(s); n     scanf( %d , v + i); n     now = i; n     insert(s, strlen(s), 0, 1); n   } n   build_trie(); n   for (int i = 1; i <= m; ++i) { n     a[i].sum = 0; n     for (int j = 0; j < (int)a[i].danger.size(); ++j) { n       a[i].sum += v[a[i].danger[j]]; n     } n   } n   long long ansl = cal(l), ansr = cal(r); n   cout << ((ansr - ansl + check(l)) % MOD + MOD) % MOD << endl; n   return 0; n } n


#include <bits/stdc++.h> n #include <cmath> n #include <regex> n #define ff first n #define ss second n #define pb push_back n #define mp make_pair n #define ba back n #define ppb pop_back n #define eb emplace_back n #define eps 1e-6 n #define vec vector<long long int> n #define sz(x) (int((x.size()))) n #define all(x) (x).begin(),(x).end() n #define FAST ios_base :: sync_with_stdio (false); cin.tie (NULL) n #define ll long long n #define loop(i,s,n) for(int i=s;i<n;i++) n using namespace std; n //----------------------------------------------------------------------------------------------------------------- n const ll M = 1e9 + 7; n const ll N = 2e5 + 5; n const ll inf = 2e18; n ll mod(ll x){   return (x%M);} n ll mod_minus(ll a, ll b){ ll ans= (mod(a)-mod(b)); if(ans<0) ans=mod(ans+M); return ans;} n   n ll mod_mul(ll a,ll b){  return mod(mod(a)*mod(b));} n   n ll mod_add(ll a,ll b){ return mod(a+b);} n   n ll power(ll a,ll n){   n     if(n==0) return 1;  n      else if(n==1) return a;  n       ll R=power(a,n/2)%M;  n        if(n%2==0) {    return mod(mod_mul(R,R));  }  n      n       else {   return mod(mod_mul(mod_mul(R,a),mod(R)));  } n     } n   n ll mod_inv(ll n){  return power(n,M-2);} n   n // only if M is prime n ll mod_div(ll a,ll b){   n      ll ans=mod(a);   ll b1=power(b,M-2);  n        ans= mod(mod_mul(ans,b1));   n         return ans; n         } n   n ll fact_mod(ll n){  n     vec fact(n+1); n      fact[0]=1;    n      loop(i,1,n+1){   n           fact[i]=mod_mul(fact[i-1],i); n            }   n             return fact[n]; n     } n   n unsigned long long nCr_mod(unsigned long long n,  ll r)  n {  n     if (r == 0)  n         return 1;  n     unsigned long long fac[n + 1];  n     fac[0] = 1;  n     for (int i = 1; i <= n; i++)  n         fac[i] = (fac[i - 1] * i) % M;  n    n     return (fac[n] * mod_inv(fac[r]) % M * mod_inv(fac[n - r]) % M) % M;  n }  n //-------------------------------------------------------------------------------------------------------------- n void solve(){ n     ll n; n     cin>>n; n     ll a[n]; n     ll sum=0; n     loop(i,0,n){ n         cin>>a[i]; n         sum+=a[i]; n     } n     sort(a,a+n); n     ll mx=a[n-1],mn=a[0]; n     ll z=(n-1)-sum%(n-1); n     if(z==(n-1)){ n         z=0; n     } n     sum+=z; n     if(mx<=sum/(n-1)){ n         cout<<z<<endl; n         return; n     } n     cout<<mx*(n-1)-sum+z<<endl; n } n  n int main(){ n     FAST; n     int t; n     cin>>t; n     while(t--){ n         solve();   n     } n }


#include <bits/stdc++.h> n using namespace std; n int read() { n   int x; n   scanf( %d , &x); n   return x; n } n const int N = 1123456; n int a[N]; n vector<int> v[N]; n int c[N]; n priority_queue<int> q[N]; n void dfs(int x, int t) { n   c[x] = t; n   q[t].push(a[x]); n   for (int i = 0; i < v[x].size(); i++) { n     int to = v[x][i]; n     if (!c[to]) { n       dfs(to, t); n     } n   } n } n int main() { n   int n, m, i, j, x, y; n   n = read(); n   m = read(); n   for (i = 1; i <= n; i++) { n     a[i] = read(); n   } n   for (i = 1; i <= m; i++) { n     x = read(); n     y = read(); n     v[x].push_back(y); n     v[y].push_back(x); n   } n   int cnt = 1; n   for (i = 1; i <= n; i++) { n     if (!c[i]) { n       dfs(i, cnt++); n     } n   } n   for (i = 1; i <= n; i++) { n     printf( %d  , q[c[i]].top()); n     q[c[i]].pop(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n stack<pair<int, int> > a; n int main() { n   int n, m = 0, i = 1; n   cin >> n; n   while (n--) { n     int b, c; n     char d, e, f[40]; n     scanf(    ); n     scanf( %c , &d); n     scanf( %d%c%d , &b, &e, &c); n     gets(f); n     b %= 12; n     if (f[1] ==  p ) b += 12; n     if (m) { n       if ((b < a.top().first) || (b == a.top().first && c < a.top().second)) n         ; n       else n         m--; n       if (b == a.top().first && c == a.top().second) n         i++; n       else n         i = 1; n     } n     if (i > 10) { n       m++; n       i -= 10; n     } n     a.push({b, c}); n     m++; n   } n   cout << m << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 100010; n const long long inf = 1e9 + 7; n long long n, a[maxn]; n long long minv = inf; n long long sum[3]; n int main() { n   scanf( %I64d , &n); n   for (long long i = 1; i <= n; i++) { n     scanf( %I64d , &a[i]); n     minv = min(minv, a[i]); n   } n   for (long long i = 1; i <= n; i++) a[i] -= minv; n   for (long long i = 1; i <= n; i++) sum[a[i]]++; n   sort(a + 1, a + n + 1); n   if (a[n] < 2) { n     printf( %I64d n , n); n     for (long long i = 1; i <= n; i++) printf( %I64d  , a[i] + minv); n     return 0; n   } else { n     long long temp = min(sum[0], sum[2]); n     long long temp2 = sum[1] >> 1; n     if (temp > temp2) { n       sum[1] += (temp << 1); n       sum[0] -= temp; n       sum[2] -= temp; n     } else { n       temp = temp2; n       sum[1] -= (temp << 1); n       sum[0] += temp; n       sum[2] += temp; n     } n     printf( %I64d n , n - (temp << 1)); n     for (long long i = 0; i < 3; i++) n       for (long long j = 1; j <= sum[i]; j++) printf( %I64d  , i + minv); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long gcd(long long a, long long b) { n   return b == 0 ? a : gcd(b, a % b); n } n inline long long lcm(long long a, long long b) { return a * b / gcd(a, b); } n long long n, k, x, type(1), amt, box; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> k; n   for (int i = 1; i <= k; i++) { n     cin >> x; n     if (x * (n / x) > amt) { n       amt = x * (n / x); n       box = n / x; n       type = i; n     } n   } n   cout << type <<     << box << endl; n } n


#include <bits/stdc++.h> n using namespace std; n void debug_out() { cerr << endl; } n void clock_out() { n   cerr <<   nTime Elapsed :   << 1.0 * clock() / CLOCKS_PER_SEC <<   s n ; n } n void fileio() { n   freopen( /home/dwai/Desktop/cp/input.txt ,  r , stdin); n   freopen( /home/dwai/Desktop/cp/output.txt ,  w , stdout); n   freopen( /home/dwai/Desktop/cp/debug.txt ,  w , stderr); n } n void ofileio() { n   freopen( input.txt ,  r , stdin); n   freopen( output.txt ,  w , stdout); n } n template <typename Head, typename... Tail> n void debug_out(Head H, Tail... T) { n   cerr <<     << H; n   debug_out(T...); n } n int IT_MAX = 1 << 20; n const long long MOD = 1000000007; n const int INF = 0x3f3f3f3f; n const long long LL_INF = 0x3f3f3f3f3f3f3f3f; n const double PI = acos(-1); n const double ERR = 1e-10; n int main() { n   42; n   ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   srand(time(NULL)); n   long long n, m; n   cin >> n >> m; n   long long a[n], b[m]; n   long long i; n   long long mn = 0, mx = 0; n   for (i = 0; i < n; i++) { n     cin >> a[i]; n     if (i == 0) { n       mx = a[i]; n       mn = a[i]; n     } else n       mx = max(mx, a[i]); n     mn = min(mn, a[i]); n   } n   long long ans = mn * 2; n   for (i = 0; i < n; i++) { n     if (a[i] > ans) ans = a[i]; n   } n   for (i = 0; i < m; i++) { n     cin >> b[i]; n     if (b[i] <= ans) return cout << -1, 0; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N, K; n int T[200005]; n vector<int> danger; n vector<pair<int, int> > v; n bool cf(pair<int, int> x, pair<int, int> y) { n   return x.second * y.first > y.second * x.first; n } n int main() { n   scanf( %d %d , &N, &K); n   for (int i = 0; i < N; i++) { n     scanf( %d , &T[i]); n     if (T[i] < 0) { n       danger.push_back(i); n     } n   } n   if (danger.size() > K) n     puts( -1 ); n   else { n     int change = danger.size() * 2; n     K -= danger.size(); n     for (int i = 0; i < (int)danger.size(); i++) { n       if (i == danger.size() - 1) n         v.push_back(make_pair(N - danger[i] - 1, 1)); n       else n         v.push_back(make_pair(danger[i + 1] - danger[i] - 1, 2)); n     } n     sort(v.begin(), v.end(), cf); n     int idx = 0; n     while (idx < v.size()) { n       if (K - v[idx].first >= 0) { n         K -= v[idx].first; n         change -= v[idx].second; n       } n       idx++; n     } n     printf( %d n , change); n   } n   return 0; n } n


#include <bits/stdc++.h> n #define ff first n #define ss second n #define ll long long n #define ld long double n #define pb push_back n #define eb emplace_back n #define mp make_pair n #define mt make_tuple n #define pii pair<int, int> n #define pll pair<ll,ll> n #define vi vector<int> n #define vl vector<ll> n #define vii vector<pii> n #define sws ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); n #define endl   n  n #define teto(a, b) ((a+b-1)/(b)) n using namespace std; n  n // Extra n #define forn(i, n) for(int i = 0; i < (int)n; i++) n #define forne(i, a, b) for(int i = a; i <= b; i++) n #define all(x) x.begin(), x.end() n #define dbg(msg, var) cout << msg <<     << var << endl; n // n  n const int MAX = 200010; n const ll MOD = (int)1e9 +7; n const int INF = 1e9; n const ll LLINF = 0x3f3f3f3f3f3f3f3f; n const ld EPS = 1e-8; n  n int n, l, r, s; n vector<vector<bool>> tab; n vector<vector<bool>> btab; n  n int dp(int idx, int soma) { n     if(soma > s) return 0; n     if(idx >= n) { n         return (soma == s); n     } n     if(btab[idx][soma]) return tab[idx][soma]; n  n     // cout << idx <<     << soma << endl; n     int res = 0; n     int pega = dp(idx+1, soma + idx+1); n     int npega = dp(idx+1, soma); n  n     if(npega >= pega) { n         res = npega; n     } n     else { n         res = pega; n     } n  n     btab[idx][soma] = true; n     return tab[idx][soma] = res; n } n  n vi solve(vi vals, int sz, bool& ruim) { n     int t = vals.size(); n     vi foi(n+1, 0); n     forn(i, t) n         foi[vals[i]] = 1; n  n     ruim = false; n     while(t < sz and !ruim) { n         bool brk = false; n         forne(i, 1, n) { n             if(!foi[i]) continue; n             forne(j, 1, i-1) { n                 if(j == i-j) continue; n                 if(!foi[j] and !foi[i-j]) { n                     foi[i] = 0; n                     foi[j] = 1; foi[i-j] = 1; n                     t++; n                     brk = true; n                     break; n                 } n             } n             if(brk) break; n         } n         if(!brk) ruim = true; n     } n  n     return foi; n } n  n int32_t main() { n     // sws; n     int t; n     cin >> t; n  n     while(t--) { n         cin >> n >> l >> r >> s; n  n         tab.assign(n, vector<bool>(s+1, -1)); n         btab.assign(n, vector<bool>(s+1, 0)); n  n         bool win = dp(0, 0); n         if(win) { n             vi vals, p(n+1, -1); n             set<int> pos; n             forne(i, 1, n) pos.insert(i); n  n             int idx = 0, soma = 0; n             while(idx < n) { n                 if(dp(idx+1, soma)) { n                     idx++; n                 } n                 else { n                     soma += idx+1; n                     vals.pb(idx+1); n                     idx++; n                 } n             } n  n             if(vals.size() > (r-l+1)) { n                 cout << -1 << endl; n                 continue; n             } n  n             bool ruim; n             // for(auto val : vals) cout << val <<    ; n             // cout << endl; n  n             vi foi = solve(vals, (r-l+1), ruim); n             // cout <<  ruim =   << ruim << endl; n  n             // forne(i, 1, n) cout << foi[i] <<    ; n             // cout << endl; n  n             if(ruim) cout << -1 << endl; n             else { n                 int id1 = 1, id2 = 1; n                 forne(i, 1, n) { n                     if(l <= i and i <= r) { n                         while(id1 <= n and !foi[id1]) id1++; n                         cout << id1 <<    ; n                         id1++; n                     } n                     else { n                         while(id2 <= n and foi[id2]) id2++; n                         cout << id2 <<    ; n                         id2++; n                     } n                 } n                 cout << endl; n             } n  n         } n         else { n             cout << -1 << endl; n         } n     } n  n     // verificar limites, long long n     return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC optimization( unroll-loops ) n std::pair<int, int> DR[] = {{-1, 0}, {0, 1},   {1, 0}, {0, -1}, n                             {-1, 1}, {-1, -1}, {1, 1}, {1, -1}}; n using namespace std; n const long long mod = 998244353; n long long nop = 70; n vector<int> primes(nop + 1, 1); n void sieve() { n   primes[1] = primes[0] = 0; n   for (long long i = 2; i * i <= nop; i++) { n     if (primes[i]) { n       for (long long j = i * i; j <= nop; j += i) { n         primes[j] = 0; n       } n     } n   } n } n long long gcd(long long a, long long b) { n   if (b > a) swap(b, a); n   if (b == 0) return a; n   return gcd(b, a % b); n } n void extgcd(long long a, long long b, long long& x, long long& y) { n   if (a == 0) { n     x = a; n     y = b; n     return; n   } n   extgcd(b % a, a, x, y); n   long long y1 = y, x1 = x; n   x = y1 - (b / a) * x1; n   y = x1; n } n long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); } n long long bexp(long long num, long long e) { n   long long ans = 1; n   while (e > 0) { n     if (e & 1) { n       ans *= num; n     } n     num *= num; n     e >>= 1; n   } n   return ans; n } n long long mexp(long long num, long long e) { n   long long ans = 1; n   while (e > 0) { n     if (e & 1) { n       ans = (ans * num) % mod; n     } n     num *= num; n     num %= mod; n     e >>= 1; n   } n   return ans % mod; n } n long long modinv(long long a) { n   a %= mod; n   return mexp(a, mod - 2); n } n void solve() { n   long long n, k; n   cin >> n >> k; n   string s; n   cin >> s; n   sort(s.begin(), s.end()); n   vector<long long> cnt(26, 0); n   for (long long i = 0; i < (n); i++) { n     cnt[s[i] -  a ]++; n   } n   int first = -1, last = -1, nod = 0; n   for (long long i = 0; i < (26); i++) { n     if (cnt[i]) { n       nod++; n       if (first == -1) first = i; n       last = i; n     } n   } n   if (cnt[first] < k) { n     cout << s[k - 1] <<   n ; n     return; n   } n   string worst =   ; n   worst += char( a  + first); n   if (nod >= 3 || (nod == 2 && cnt[first] > k)) { n     for (long long i = k; i < n; i++) { n       worst += s[i]; n     } n   } else { n     for (long long i = k; i < n; i++) { n       worst += s[i]; n       i += (k - 1); n     } n   } n   cout << worst <<   n ; n } n int main() { n   cin.tie(0); n   cout.tie(0); n   ios::sync_with_stdio(0); n   int t = 1; n   cin >> t; n   for (long long T = 1; T <= (t); T++) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char str[100010]; n int ct[100010][4]; n int main() { n   int N, x[5], R, L, M; n   scanf( %s , str); n   N = strlen(str); n   for (int i = 1; i <= N; i++) { n     for (int j = 0; j < 3; j++) ct[i][j] = ct[i - 1][j]; n     ct[i][str[i - 1] -  x ]++; n   } n   scanf( %d , &M); n   for (int i = 0; i < M; i++) { n     scanf( %d%d , &L, &R); n     L--; n     if (R - L < 3) { n       puts( YES ); n       continue; n     } n     for (int j = 0; j < 3; j++) x[j] = ct[R][j] - ct[L][j]; n     if (x[0] <= x[1] && x[1] <= x[2] && x[2] - x[0] <= 1) n       puts( YES ); n     else if (x[1] <= x[2] && x[2] <= x[0] && x[0] - x[1] <= 1) n       puts( YES ); n     else if (x[2] <= x[0] && x[0] <= x[1] && x[1] - x[2] <= 1) n       puts( YES ); n     else n       puts( NO ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 5; n int n, a[maxn]; n struct node { n   int pre; n   bool flag; n   int other; n } num[2 * maxn]; n int main(int argc, char const *argv[]) { n   while (cin >> n) { n     bool flag = true; n     for (int i = 0; i <= n; i++) { n       cin >> a[i]; n       if (a[i] != 1) flag = false; n     } n     if (flag == true) { n       cout <<  perfect  << endl; n       continue; n     } n     bool temp = false; n     bool ans = false; n     int cnt = 1; n     for (int i = 0; i <= n; i++) { n       if (i > 0 && a[i] > 1 && a[i - 1] > 1 && ans == false) { n         ans = true; n         int par = cnt - 1; n         int k = a[i]; n         k--; n         while (k--) { n           num[cnt].pre = par; n           num[cnt].other = par - 1; n           num[cnt].flag = true; n           cnt++; n         } n         num[cnt].pre = num[cnt].other = par; n         num[cnt].flag = true; n         cnt++; n       } else { n         int par = cnt - 1; n         int k = a[i]; n         while (k--) { n           num[cnt].pre = par; n           num[cnt].flag = false; n           cnt++; n         } n       } n     } n     if (ans == false) { n       cout <<  perfect  << endl; n       continue; n     } n     cout <<  ambiguous  << endl; n     for (int i = 1; i < cnt; i++) cout << num[i].pre <<    ; n     cout << endl; n     for (int i = 1; i < cnt; i++) { n       if (num[i].flag == false) n         cout << num[i].pre; n       else n         cout << num[i].other; n       cout <<    ; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int mymax(int x) { n   int max = 0; n   while (x) { n     int s = x % 10; n     if (s > max) max = s; n     x /= 10; n   } n   return max; n } n int zh(int x, int n) { n   int s = 0, p = 1; n   while (x) { n     s += x % 10 * p; n     x /= 10; n     p *= n; n   } n   return s; n } n int main() { n   int a, b; n   cin >> a >> b; n   int n = max(mymax(a), mymax(b)) + 1; n   int num = zh(a, n) + zh(b, n); n   int j = 0; n   while (num) { n     int s = num % n; n     num /= n; n     j++; n   } n   cout << j << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n class Constellation { n   static const long long N = 100010; n   inline long long sign(long long x) { return x > 0 ? 1 : x == 0 ? 0 : -1; } n   struct point { n     long long x, y, num; n   } b[N]; n   long long n; n   long long cross(const point &a, const point &b, const point &c) { n     return sign((b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x)); n   } n   bool contain(const point &a, const point &b, const point &c, const point &p) { n     long long v1 = cross(a, b, p), v2 = cross(b, c, p), v3 = cross(c, a, p); n     return v1 * v2 >= 0 && v2 * v3 >= 0 && v3 * v1 >= 0; n   } n  n  public: n   void solve() { n     cin >> n; n     for (long long i = 1; i <= n; ++i) cin >> b[i].x >> b[i].y; n     long long p1 = 1, p2 = 2, p3 = 0; n     for (long long i = 3; i <= n; ++i) n       if (cross(b[p1], b[p2], b[i])) { n         p3 = i; n         break; n       } n     for (long long i = 1; i <= n; ++i) n       if (i != p1 && i != p2 && i != p3 && contain(b[p1], b[p2], b[p3], b[i])) { n         if (cross(b[p1], b[p2], b[i])) n           p3 = i; n         else n           p2 = i; n       } n     cout << p1 <<     << p2 <<     << p3 << endl; n   } n } T; n int main() { T.solve(); } n


#include <bits/stdc++.h> n double eps = 1e-9; n using namespace std; n int main() { n   std::ios::sync_with_stdio(false); n   int x, y, z, w, b, a; n   cin >> x >> y >> z >> w >> b >> a; n   cout << (2 * (2 * x + a + y) * (z + y) - (a * a + b * b + y * y + z * z)) / 2; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dirx[] = {1, 0, -1, 0}; n int diry[] = {0, 1, 0, -1}; n int a; n vector<int> graph[100005]; n int dist[100005]; n bool visit[100005]; n int cnt = 0; n bool flag = false; n int dfs(int u, int par) { n   visit[u] = true; n   for (int i = 0; i < graph[u].size(); i++) { n     if (!visit[graph[u][i]]) dist[u] += dfs(graph[u][i], u); n   } n   if (dist[u] != 0 && (dist[u] + 1) % 2 == 0) { n     cnt++; n     dist[u] = 0; n     return 0; n   } else n     return 1; n } n int main() { n   scanf( %d , &a); n   int x, y; n   int start = 0; n   for (int i = 0; i < a - 1; i++) { n     scanf( %d %d , &x, &y); n     start = x; n     graph[x].push_back(y); n     graph[y].push_back(x); n   } n   if (a % 2 == 1) { n     cout << -1 << endl; n     return 0; n   } n   dfs(start, 0); n   cout << cnt - 1 << endl; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:167772160000 ) n using namespace std; n long long dp[5100][5100]; n long long x[5100]; n int a[5100], b[5100], c[5100], d[5100]; n int main() { n   int leftminright = 0; n   int n, s, e; n   cin >> n >> s >> e; n   s--; n   e--; n   for (int i = 0; i < n; i++) cin >> x[i]; n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 0; i < n; i++) cin >> b[i]; n   for (int i = 0; i < n; i++) cin >> c[i]; n   for (int i = 0; i < n; i++) cin >> d[i]; n   x[n] = 0; n   for (int i = 0; i < n + 2; i++) n     for (int j = 0; j < n + 2; j++) dp[i][j] = 1E18; n   dp[0][0] = 0; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       if (i != 0) { n         if (j == 0 && leftminright != 1) continue; n       } n       if (i == s) { n         leftminright--; n         long long now = dp[i][j]; n         now += d[i]; n         dp[i + 1][j + 1] = n             min(dp[i + 1][j + 1], n                 now + (x[i + 1] - x[i]) * ((j + 1) * 2 + leftminright)); n         now -= d[i]; n         now += c[i]; n         if (j + leftminright + 1 > 0) n           dp[i + 1][j] = min(dp[i + 1][j], n                              now + (x[i + 1] - x[i]) * (j * 2 + leftminright)); n         leftminright++; n         continue; n       } n       if (i == e) { n         leftminright++; n         long long now = dp[i][j]; n         now += b[i]; n         dp[i + 1][j] = n             min(dp[i + 1][j], now + (x[i + 1] - x[i]) * (j * 2 + leftminright)); n         now -= b[i]; n         now += a[i]; n         if (j == 0) { n           leftminright--; n           continue; n         } n         dp[i + 1][j - 1] = n             min(dp[i + 1][j - 1], n                 now + (x[i + 1] - x[i]) * (2 * (j - 1) + leftminright)); n         leftminright--; n         continue; n       } n       long long now = dp[i][j]; n       now += b[i]; n       now += d[i]; n       dp[i + 1][j + 1] = n           min(dp[i + 1][j + 1], n               now + (x[i + 1] - x[i]) * ((j + 1) * 2 + leftminright)); n       now -= d[i]; n       now += c[i]; n       if (j + leftminright > 0) n         dp[i + 1][j] = n             min(dp[i + 1][j], now + (x[i + 1] - x[i]) * (j * 2 + leftminright)); n       now -= b[i]; n       now += a[i]; n       now -= c[i]; n       now += d[i]; n       if (j > 0) n         dp[i + 1][j] = n             min(dp[i + 1][j], now + (x[i + 1] - x[i]) * (j * 2 + leftminright)); n       now -= d[i]; n       now += c[i]; n       if (j > 0 && j + leftminright > 0) n         dp[i + 1][j - 1] = n             min(dp[i + 1][j - 1], n                 now + (x[i + 1] - x[i]) * ((j - 1) * 2 + leftminright)); n     } n     if (i == s) leftminright--; n     if (i == e) leftminright++; n   } n   cout << dp[n][0]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = acos(-1.0); n const double EPS = 1e-8; n const long long INF = 0x3f3f3f3f3f3f3f3f; n inline long long readll() { n   long long x(0), op(1); n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) op = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) x = x * 10 + ch -  0 , ch = getchar(); n   return x * op; n } n const int sz = 2e5 + 9; n long long a[sz], n; n long long solve(long long a, long long b) { n   if (a == 0 || a == b) return 0; n   int l = 1, r = n; n   if (b) { n     if (b < a) n       l = b + 1; n     else n       r = b - 1; n   } n   return (a - l + 1) * (r - a + 1); n } n int main() { n   n = readll(); n   for (int i = 1; i < n + 1; ++i) a[i] = readll(); n   a[0] = a[n + 1] = 0; n   long long ans = 0; n   for (int i = 0; i < n + 1; ++i) { n     ans += solve(a[i], a[i + 1]); n     ans += solve(a[i + 1], a[i]); n   } n   cout << ans / 2 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int max_n = 200222, inf = 1000111222; n int n, m, a[max_n]; n bool check(int cnt) { n   long long sum = 0; n   int last = 1; n   for (int i = 0; i < n; ++i) { n     if (i % cnt == 0) --last; n     if (a[i] + last < 0) break; n     sum += (a[i] + last); n   } n   return sum >= m; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < n; ++i) scanf( %d , &a[i]); n   sort(a, a + n); n   reverse(a, a + n); n   int l = 0, r = n + 1; n   while (r - l > 1) { n     int mid = (l + r) / 2; n     if (check(mid)) n       r = mid; n     else n       l = mid; n   } n   if (r > n) n     puts( -1 ); n   else n     printf( %d n , r); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long t[4 * 100100], dp[100100], a[100100], b[100100]; n long long K = 1, n, m; n void build() { n   while (K < m + 2) K <<= 1; n } n long long query(long long l, long long r) { n   long long ans = 0; n   for (l += K - 1, r += K + 1; l ^ r ^ 1; l >>= 1, r >>= 1) { n     if (~l ^ 1) ans = ((ans) >= (t[l + 1]) ? (ans) : (t[l + 1])); n     if (r ^ 1) ans = ((ans) >= (t[r - 1]) ? (ans) : (t[r - 1])); n   } n   return ans; n } n void update(long long id, long long v) { n   for (t[id += K] = v, id >>= 1; id >= 1; id >>= 1) n     t[id] = max(t[id << 1 | 1], t[id << 1]); n } n long long get_id(long long x) { n   long long l = 1, r = m, mid; n   while (l < r) { n     mid = (l + r + 1) >> 1; n     if (b[mid] > x) n       r = mid - 1; n     else n       l = mid; n   } n   return l; n } n void unique() { n   m = 1; n   for (long long i = 2; i <= n; i++) n     if (b[i] != b[m]) b[++m] = b[i]; n } n void input() { n   scanf( %I64d , &n); n   for (long long i = 1, r, h; i <= n; i++) { n     scanf( %I64d%I64d , &r, &h); n     a[i] = b[i] = r * r * h; n   } n   return; n } n void init() { n   sort(b + 1, b + n + 1); n   unique(); n   build(); n   return; n } n void work() { n   for (long long i = 1; i <= n; i++) { n     long long id = get_id(a[i]); n     dp[i] = query(1, id - 1) + a[i]; n     update(id, dp[i]); n   } n   return; n } n void output() { n   printf( %.15lf , query(1, m) * M_PI); n   return; n } n int main() { n   input(); n   init(); n   work(); n   output(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k, c[100005], visit[100005], tp = 0; n vector<int> a[100005], ans; n bool ok[100005]; n void dfs(int u) { n   visit[u] = tp; n   for (int i = 0; i < a[u].size(); i++) { n     int v = a[u][i]; n     if (ok[v]) continue; n     if (visit[v] == tp) { n       cout << -1; n       exit(0); n     } else n       dfs(v); n   } n   ans.push_back(u); n   ok[u] = 1; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> k; n   for (int i = 1; i <= k; i++) cin >> c[i]; n   for (int i = 1; i <= n; i++) { n     int t; n     cin >> t; n     while (t--) { n       int u; n       cin >> u; n       a[i].push_back(u); n     } n   } n   for (int i = 1; i <= k; i++) { n     if (!visit[c[i]]) { n       tp++; n       dfs(c[i]); n     } n   } n   cout << ans.size() <<   n ; n   for (int i = 0; i < ans.size(); i++) cout << ans[i] <<    ; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n template <typename F, typename S> n ostream &operator<<(ostream &os, const pair<F, S> &p) { n   return os <<  (  << p.first <<  ,   << p.second <<  ) ; n } n template <typename T> n ostream &operator<<(ostream &os, const vector<T> &v) { n   os <<  { ; n   typename vector<T>::const_iterator it; n   for (it = v.begin(); it != v.end(); it++) { n     if (it != v.begin()) os <<  ,  ; n     os << *it; n   } n   return os <<  } ; n } n template <typename T> n ostream &operator<<(ostream &os, const set<T> &v) { n   os <<  [ ; n   typename set<T>::const_iterator it; n   for (it = v.begin(); it != v.end(); it++) { n     if (it != v.begin()) os <<  ,  ; n     os << *it; n   } n   return os <<  ] ; n } n template <typename F, typename S> n ostream &operator<<(ostream &os, const map<F, S> &v) { n   os <<  [ ; n   typename map<F, S>::const_iterator it; n   for (it = v.begin(); it != v.end(); it++) { n     if (it != v.begin()) os <<  ,  ; n     os << it->first <<   =   << it->second; n   } n   return os <<  ] ; n } n int n, m; n string hor, ver; n int give_dir(char ch) { n   if (ch ==  >  || ch ==  v ) n     return 1; n   else n     return -1; n } n bool valid(int x, int y) { n   if (x < 0 || y < 0 || x >= n || y >= m) return false; n   return true; n } n void dfs(int x, int y, vector<vector<bool> > &visited) { n   visited[x][y] = true; n   int nx, ny; n   nx = x + give_dir(ver[y]); n   ny = y + give_dir(hor[x]); n   if (valid(nx, y) && !visited[nx][y]) dfs(nx, y, visited); n   if (valid(x, ny) && !visited[x][ny]) dfs(x, ny, visited); n } n int main() { n   cin >> n >> m; n   cin >> hor >> ver; n   for (int k = 0; k < (n); k++) { n     for (int l = 0; l < (m); l++) { n       vector<vector<bool> > visited(n, vector<bool>(m, false)); n       dfs(k, l, visited); n       for (int i = 0; i < (n); i++) { n         for (int j = 0; j < (m); j++) { n           if (!visited[i][j]) { n             cout <<  NO  << endl; n             return 0; n           } n         } n       } n     } n   } n   cout <<  YES  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #define me(a, b) memset(a, b, sizeof(a)) n #define IOS() ios::sync_with_stdio(false), cin.tie(0) n #define endl   n  n  n typedef long long ll; n typedef pair<ll, ll> pll; n typedef pair<int, int> pii; n const int INF = 0x3f3f3f3f; n const int maxn = 2e5 + 5; n const ll mod = 1e9 + 7; n  n int cnt[26]; n int n, k; n vector<pii> ans; n int check() n { n  ans.clear(); n  int sum = 0; n  for(int i = 0; i < 26; ++i) { n   int r = cnt[i] % k; n   if(r){ n    ans.push_back(pii(i, k - r)); n    sum += k - r; n   } n  } n  return sum; n } n  n void solve() n { n  me(cnt, 0); n  string s; n  cin >> n >> k >> s; n  for(char &c : s) n   ++cnt[c -  a ]; n  if(check() == 0) { n   cout << s << endl; n   return ; n  } n  for(int i = s.size()-1; i >= 0; --i) { n   char &c = s[i]; n   --cnt[c -  a ]; n   for(int j = s[i] -  a  + 1; j < 26; ++j) { n    c = j +  a ; n    ++cnt[j]; n    int sum = s.size()-i-1 - check(); n    --cnt[j]; n    if(sum >= 0 && sum % k == 0) { n     string t; n     for(int p = 0; p <= i; ++p) n      t.push_back(s[p]); n     t.append(sum,  a ); n     for(pii p : ans) { n      t.append(p.second, p.first+ a ); n     } n     cout << t << endl; n     return ; n    } n  n   } n  } n  cout <<  -1 n ; n } n  n  n int main() n { n  IOS(); n  int T; n  cin >> T; n  while(T--) n   solve(); n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n int f[100001], g[100001], h[100001]; n int n, cnt; n bool work() { n   cnt = -1; n   for (int i = 0; i < n; ++i) { n     if (g[i] != -1) { n       if (f[i] != i) return false; n     } else { n       if (g[f[i]] == -1) { n         g[i] = ++cnt; n         h[cnt] = f[i]; n         g[f[i]] = cnt; n       } else { n         if (h[g[f[i]]] != f[i]) return false; n         h[g[f[i]]] = f[i]; n         g[i] = g[f[i]]; n       } n     } n   } n   return true; n } n int main() { n   cin >> n; n   for (int i = 0; i < n; ++i) cin >> f[i]; n   for (int i = 0; i < n; ++i) --f[i]; n   for (int i = 0; i < n; ++i) g[i] = h[i] = -1; n   if (work()) { n     cout << ++cnt << endl; n     for (int i = 0; i < n; ++i) cout << ++g[i] <<    ; n     cout << endl; n     for (int i = 0; i < cnt; ++i) cout << ++h[i] <<    ; n   } else n     cout << -1 << endl; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const int maxl = 200005; n int T, n, mk, seq[maxl]; n char s[maxl]; n inline char read() { n   char ch = getchar(); n   while (ch !=  <  && ch !=  > ) ch = getchar(); n   return ch; n } n int main() { n   scanf( %d , &T); n   while (T--) { n     scanf( %d , &n); n     mk = 0; n     memset(seq, 0, sizeof(seq)); n     for (register int i = 1; i < n; i++) s[i] = read(); n     s[0] =  > ; n     s[n] =  < ; n     for (register int i = n; i; i--) n       if (s[i - 1] ==  >  && s[i] ==  < ) seq[i] = ++mk; n     mk = n; n     for (register int i = 1, j, tmp; i <= n; i++) { n       if (seq[i]) continue; n       if (s[i - 1] ==  > ) n         seq[i] = mk--; n       else { n         for (j = i; s[j] !=  >  && j < n; j++) n           ; n         tmp = j; n         for (; j >= i; j--) seq[j] = mk--; n         i = tmp; n       } n     } n     for (register int i = 1; i <= n; i++) printf( %d  , seq[i]); n     printf(  n ); n     memset(seq, 0, sizeof(seq)); n     mk = 0; n     for (register int i = 1; i <= n; i++) n       if (s[i - 1] ==  >  && s[i] ==  < ) seq[i] = ++mk; n     mk = n; n     for (register int i = n, j, tmp; i; i--) { n       if (seq[i]) continue; n       if (s[i] ==  < ) n         seq[i] = mk--; n       else { n         for (j = i; s[j - 1] !=  <  && j > 1; j--) n           ; n         tmp = j; n         for (; j <= i; j++) seq[j] = mk--; n         i = tmp; n       } n     } n     for (register int i = 1; i <= n; i++) printf( %d  , seq[i]); n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ifstream in( date.in ); n   ofstream out( date.out ); n   long long nr2, k, rez[4001], con = 0, nr, n, pli[4001], plp[4001], inc[4001], n                                i, j; n   cin >> n; n   for (i = 1; i <= n; i++) cin >> pli[i] >> plp[i] >> inc[i]; n   for (i = 1; i <= n; i++) { n     nr = nr2 = 0; n     if (inc[i] >= 0) { n       rez[++con] = i; n       nr = pli[i]; n       for (j = i + 1; nr > 0 && j <= n; j++) { n         if (inc[j] >= 0) { n           inc[j] -= nr--; n         } else { n           if (pli[j]) { n             inc[j] -= nr; n             pli[j] = 0; n             nr2 = plp[j]; n             for (k = j + 1; k <= n; k++) inc[k] -= nr2; n           } n         } n       } n     } else { n       if (pli[i]) { n         pli[i] = 0; n         nr2 = plp[i]; n         for (k = i + 1; k <= n; k++) inc[k] -= nr2; n       } n     } n   } n   cout << con <<   n ; n   for (j = 1; j <= con; j++) cout << rez[j] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1000000007; n const int N = 128; n int num[N]; n int main() { n   int n; n   cin >> n; n   for (int i = 1; i <= n; ++i) { n     cin >> num[i]; n   } n   int m; n   cin >> m; n   for (int i = 0; i < m; ++i) { n     int x, y; n     cin >> x >> y; n     num[x - 1] += y - 1; n     num[x + 1] += num[x] - y; n     num[x] = 0; n   } n   for (int i = 1; i <= n; ++i) { n     cout << num[i] << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int p[3010000], n; n int main() { n   scanf( %d , &n); n   for (int i = 1; i < n + 1; i++) p[i] = i; n   for (int i = 2; i < n + 1; i++) { n     int s = p[i - 1], ns; n     for (int j = i - 1; j <= n + i - 2; j += i) { n       ns = p[j + i]; n       if (j + i <= n + i - 2) n         p[j + i] = s; n       else n         p[n + i - 1] = s; n       s = ns; n     } n   } n   for (int i = 1; i < n + 1; i++) printf( %d  , p[n - 1 + i]); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int arr[101]; n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n   } n   sort(arr, arr + n); n   for (int i = 0; i < n; i++) { n     cout << arr[i] <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 100; n long long arr[N], d[N], tree[N << 2], mx[N << 2]; n void build(int rt, int l, int r) { n   if (l == r) { n     tree[rt] = mx[rt] = arr[l]; n     return; n   } n   int m = (l + r) / 2; n   build(rt << 1, l, m); n   build(rt << 1 | 1, m + 1, r); n   tree[rt] = tree[rt << 1] + tree[rt << 1 | 1]; n   mx[rt] = max(mx[rt << 1], mx[rt << 1 | 1]); n } n void update(int L, int R, int rt, int l, int r) { n   if (mx[rt] <= 2) return; n   if (l == r) { n     tree[rt] = mx[rt] = d[tree[rt]]; n     return; n   } n   int m = (l + r) >> 1; n   if (L <= m) update(L, R, rt << 1, l, m); n   if (R > m) update(L, R, rt << 1 | 1, m + 1, r); n   tree[rt] = tree[rt << 1] + tree[rt << 1 | 1]; n   mx[rt] = max(mx[rt << 1], mx[rt << 1 | 1]); n } n long long query(int L, int R, int rt, int l, int r) { n   if (L <= l && r <= R) { n     return tree[rt]; n   } n   int m = (l + r) >> 1; n   long long ans = 0; n   if (L <= m) ans += query(L, R, rt << 1, l, m); n   if (R > m) ans += query(L, R, rt << 1 | 1, m + 1, r); n   return ans; n } n int main() { n   ios::sync_with_stdio(false); n   int n, m, t, l, r; n   for (int i = 1; i < N; i++) { n     for (int j = i; j < N; j += i) d[j]++; n   } n   cin >> n >> m; n   for (int i = 1; i <= n; i++) cin >> arr[i]; n   build(1, 1, n); n   while (m--) { n     cin >> t >> l >> r; n     if (t == 1) n       update(l, r, 1, 1, n); n     else n       cout << query(l, r, 1, 1, n) << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline bool fs(T &x) { n   int c = getchar(); n   int sgn = 1; n   while (~c && c <  0  || c >  9 ) { n     if (c ==  - ) sgn = -1; n     c = getchar(); n   } n   for (x = 0; ~c &&  0  <= c && c <=  9 ; c = getchar()) x = x * 10 + c -  0 ; n   x *= sgn; n   return ~c; n } n int main() { n   long long int n, m, cnt = 0; n   fs(n); n   fs(m); n   for (long long int i = 1; i * i <= m; i++) { n     if (m % i == 0 && m / i <= n) { n       if (m / i != i) cnt++; n       cnt++; n     } n   } n   cout << cnt << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int solve(vector<pair<int, int> > v, int price) { n   if (v.size() < 2) return 0; n   vector<int> p, b; n   for (int i = 0; i < v.size(); i++) n     p.push_back(v[i].first), b.push_back(v[i].second); n   int amx[v.size()]; n   amx[0] = b[0]; n   for (int i = 1; i < b.size(); i++) amx[i] = max(amx[i - 1], b[i]); n   int ans = 0; n   for (int i = 0; i < v.size(); i++) { n     int id = upper_bound(p.begin(), p.begin() + i, price - p[i]) - p.begin(); n     id--; n     if (id < 0 || id >= v.size()) continue; n     if (id != i) ans = max(ans, b[i] + amx[id]); n   } n   return ans; n } n int main() { n   int i, j, k; n   int n, m; n   int c, d; n   int x, y; n   char ch; n   cin >> n >> c >> d; n   int mc = 0, md = 0; n   vector<pair<int, int> > coins, diamonds; n   for (i = 0; i < n; i++) { n     cin >> x >> y >> ch; n     if (ch ==  C ) { n       if (c >= y) mc = max(mc, x), coins.push_back({y, x}); n     } else { n       if (d >= y) md = max(md, x), diamonds.push_back({y, x}); n     } n   } n   int ans = 0; n   if (mc > 0 && md > 0) ans = mc + md; n   sort(coins.begin(), coins.end()); n   sort(diamonds.begin(), diamonds.end()); n   ans = max(ans, solve(coins, c)); n   ans = max(ans, solve(diamonds, d)); n   printf( %d  , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int _, n; n char s[100005]; n int main() { n   scanf( %d , &_); n   while (_--) { n     scanf( %d , &n); n     s[0] =  a ; n     scanf( %s , s + 1); n     int p = -1; n     for (int i = n; i >= 1; i--) { n       if (s[i] ==  0 ) { n         p = i; n         break; n       } n     } n     bool f = 0; n     for (int i = 1; i <= n; i++) { n       if (s[i] ==  1 ) { n         if (s[i + 1] ==  0 ) n           f = 1; n         else n           f = 0; n         if (i < p) { n           continue; n         } n         printf( 1 ); n       } else { n         if (f) { n           printf( 0 ); n           i = p; n           continue; n         } n         printf( 0 ); n       } n     } n     puts(  ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, ans, notOK; n map<int, int> M; n int main() { n   cin >> n; n   for (int i = 0, x; i < n; i++) cin >> x, M[x]++; n   for (map<int, int>::iterator it = M.begin(); it != M.end(); it++) { n     if (it->first) { n       if (it->second == 2) n         ans++; n       else if (it->second > 2) n         notOK = 1; n     } n   } n   cout << (notOK ? -1 : ans); n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> cr1[27]; n vector<int> cr2[27]; n int s1[27]; n int s2[27]; n struct node { n   int x; n   int y; n }; n int main() { n   int n; n   cin >> n; n   char c; n   for (int i = 0; i < n; i++) { n     cin >> c; n     if (c ==  ? ) { n       s1[26]++; n       cr1[26].push_back(i); n     } else { n       s1[c -  a ]++; n       cr1[c -  a ].push_back(i); n     } n   } n   for (int i = 0; i < n; i++) { n     cin >> c; n     if (c ==  ? ) { n       s2[26]++; n       cr2[26].push_back(i); n     } else { n       s2[c -  a ]++; n       cr2[c -  a ].push_back(i); n     } n   } n   int cnt = 0; n   vector<node> nd; n   for (int i = 0; i < 26; i++) { n     int x = s1[i] - 1; n     int y = s2[i] - 1; n     while (x >= 0 && y >= 0) { n       node n1; n       n1.x = cr1[i][x]; n       n1.y = cr2[i][y]; n       nd.push_back(n1); n       cnt++; n       x--; n       y--; n     } n     if (x >= 0 && y < 0) { n       node n1; n       while (x >= 0 && s2[26] > 0) { n         n1.x = cr1[i][x]; n         n1.y = cr2[26][s2[26] - 1]; n         nd.push_back(n1); n         cnt++; n         s2[26]--; n         x--; n       } n     } else if (x < 0 && y >= 0) { n       node n1; n       while (y >= 0 && s1[26] > 0) { n         n1.x = cr1[26][s1[26] - 1]; n         n1.y = cr2[i][y]; n         nd.push_back(n1); n         cnt++; n         s1[26]--; n         y--; n       } n     } n   } n   while (s1[26] > 0 && s2[26] > 0) { n     node n1; n     n1.x = cr1[26][s1[26] - 1]; n     n1.y = cr2[26][s2[26] - 1]; n     nd.push_back(n1); n     cnt++; n     s1[26]--; n     s2[26]--; n   } n   cout << cnt << endl; n   for (int i = 0; i < nd.size(); i++) n     cout << nd[i].x + 1 <<     << nd[i].y + 1 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n void actionFun() { n   long long n; n   cin >> n; n   long long c = 2; n   for (int i = 0; i < n; i++) { n     cout << c <<    ; n     ++c; n   } n   cout << endl; n   return; n } n int main() { n   int t; n   cin >> t; n   while (t--) { n     actionFun(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   while (~scanf( %d , &n)) { n     int cnt = 0; n     for (int i = 1; i < n; i++) { n       for (int j = i + 1; j < n; j++) { n         int sum = i * i + j * j; n         if (sum <= n * n) { n           if ((int)sqrt(sum) * (int)sqrt(sum) == sum) cnt++; n         } n       } n     } n     printf( %d n , cnt); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int k[1005], n, i, j; n long long kx[1005]; n int main() { n   cin >> n; n   for (i = 1; i <= n; i++) cin >> k[i]; n   kx[1] = 2; n   for (i = 2; i <= n; i++) { n     if (k[i] == i) n       kx[i] = 2; n     else { n       for (j = k[i]; j < i; j++) kx[i] = (kx[i] + kx[j]) % 1000000007; n       kx[i] = (kx[i] + 2) % 1000000007; n     } n   } n   long long ans = 0; n   for (i = 1; i <= n; i++) ans = (ans + kx[i]) % 1000000007; n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false), cin.tie(NULL); n   long long n; n   cin >> n; n   vector<long long> a(n); n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   if (n == 1) { n     cout <<  YES ; n     return 0; n   } n   vector<long long> v; n   long long cnt0 = 1; n   for (int i = 1; i < n; i++) { n     if (a[i] != a[i - 1]) { n       v.push_back(cnt0); n       cnt0 = 1; n     } else n       cnt0++; n     if (i == n - 1) { n       v.push_back(cnt0); n     } n   } n   long long cnt = 1; n   for (int i = 0; i < v.size(); i++) { n     if (v[i] == v[i + 1]) cnt++; n   } n   if (cnt == v.size()) n     cout <<  YES ; n   else n     cout <<  NO ; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma GCC push_options n #pragma GCC optimize( unroll-loops ) n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long int INF = 10e18; n   long long t; n   cin >> t; n   while (t--) { n     long long n; n     cin >> n; n     long long a[n], b[n], c[n]; n     for (long long i = 0; i < n; i++) { n       cin >> a[i] >> b[i]; n     } n     for (long long i = 1; i <= n; i++) { n       c[i % n] = max(0ll, a[i % n] - b[i - 1]); n     } n     long long sum = accumulate(c, c + n, 0ll); n     long long res = INF; n     for (long long i = 0; i < n; i++) { n       res = min(res, sum + a[i] - c[i]); n     } n     cout << res << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int contrib[312345] = {0}; n bool in_set[312345] = {0}; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n, k; n   cin >> n >> k; n   vector<int> ans; n   int cnt = 0; n   for (int i = 1; i <= n / 2; i++) { n     if (cnt + contrib[i] <= k) { n       ans.push_back(i); n       in_set[i] = true; n       cnt += contrib[i]; n       for (int j = i + i; j <= n; j += i) contrib[j]++; n     } else { n       while (cnt + contrib[i] > k) { n         cnt -= contrib[ans.back()]; n         for (int j = ans.back() + ans.back(); j <= n; j += ans.back()) n           contrib[j]--; n         in_set[ans.back()] = false; n         ans.pop_back(); n       } n       ans.push_back(i); n       in_set[i] = true; n       cnt += contrib[i]; n       for (int j = i + i; j <= n; j += i) contrib[j]++; n     } n     if (cnt == k) break; n   } n   priority_queue<pair<int, int> > pq; n   for (int i = n / 2 + 1; i <= n; i++) { n     if (in_set[i]) continue; n     pq.push(make_pair(contrib[i], i)); n   } n   while (!pq.empty()) { n     while (!pq.empty() && cnt + pq.top().first > k) pq.pop(); n     if (pq.empty()) break; n     cnt += pq.top().first; n     ans.push_back(pq.top().second); n     pq.pop(); n   } n   if (cnt != k) { n     cout <<  No  << endl; n     return 0; n   } n   cout <<  Yes  << endl; n   cout << ans.size() << endl; n   for (int i = 0; i < ans.size(); i++) { n     if (i) cout <<    ; n     cout << ans[i]; n   } n   cout << endl; n } n


#include <bits/stdc++.h> n #pragma GCC optimize(2) n using namespace std; n void qread(int &x) { n   int neg = 1; n   x = 0; n   char c = getchar(); n   while (c <  0  || c >  9 ) { n     if (c ==  - ) neg = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) x = 10 * x + c -  0 , c = getchar(); n   x *= neg; n } n const int maxn = 200005; n char c[maxn]; n int n, m; n string s, t; n int main() { n   scanf( %d%d , &n, &m); n   scanf( %s , c); n   s = c; n   scanf( %s , c); n   t = c; n   bool ok = false; n   for (int(i) = 0; (i) < n; i++) n     if (s[i] ==  * ) ok = true; n   if (!ok) { n     puts(s == t ?  YES  :  NO ); n     return 0; n   } n   int pre = 0, suf = 0; n   while (s[pre] !=  *  && s[pre] == t[pre]) pre++; n   while (s[n - suf - 1] !=  *  && s[n - suf - 1] == t[m - suf - 1]) suf++; n   if (s[pre] ==  *  && s[n - suf - 1] ==  *  && pre - 1 < m - suf) n     puts( YES ); n   else n     puts( NO ); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n using int64 = long long; n using pii = pair<int, int>; n using vchar = vector<char>; n using vvchar = vector<vchar>; n using vint = vector<int>; n using vint64 = vector<int64>; n using vvint = vector<vint>; n using vbool = vector<bool>; n using vpii = vector<pii>; n template <typename T> n T sqr(T x) { n   return x * x; n } n template <typename T> n T gcd(T a, T b) { n   return b == 0 ? a : gcd(b, a % b); n } n pair<int, vint> solveD(vvint g, int k) { n   enum { NOT_VISITED, STARTED, FINISHED }; n   const int n = g.size(); n   vint depth(n, -1); n   depth[0] = 0; n   vint parent(n, -1); n   vint loopState(n, NOT_VISITED); n   pair<int, vint> result; n   result.first = 0; n   std::function<void(int)> DFS = [&](int v) { n     loopState[v] = STARTED; n     const int previous = parent[v]; n     if (result.first != 0) return; n     int backVertex = -1; n     int backDepth = -1; n     for (auto to : g[v]) { n       if (to == previous) continue; n       if (loopState[to] == STARTED && depth[to] > backDepth) { n         backVertex = to; n         backDepth = depth[to]; n       } n     } n     if (backVertex != -1) { n       vint loop; n       for (int u = v; u != backVertex; u = parent[u]) { n         loop.push_back(u); n       } n       loop.push_back(backVertex); n       reverse(std::begin(loop), std::end(loop)); n       assert(depth[v] - depth[backVertex] + 1 == loop.size()); n       const int loopLength = loop.size(); n       if (loopLength > k) { n         result.first = 1; n         for (int i = 0; i < loop.size(); i += 2) { n           result.second.push_back(loop[i]); n         } n         result.second.resize((k + 1) / 2); n       } else { n         result.first = 2; n         result.second = loop; n       } n       return; n     } n     for (auto to : g[v]) { n       if (loopState[to] == NOT_VISITED) { n         parent[to] = v; n         depth[to] = depth[v] + 1; n         DFS(to); n         if (result.first != 0) return; n       } n     } n     loopState[v] = FINISHED; n   }; n   DFS(0); n   if (result.first != 0) { n     return result; n   } n   vint colors(n, -1); n   std::function<void(int, int)> biColor = [&](int v, int color) { n     colors[v] = color; n     for (auto to : g[v]) { n       if (colors[to] == -1) { n         biColor(to, 1 - color); n       } n     } n   }; n   biColor(0, 0); n   int colorCount[2] = {0}; n   for (int i = 0; i < n; ++i) { n     colorCount[colors[i]]++; n   } n   const int bestColor = colorCount[0] > colorCount[1] ? 0 : 1; n   result.first = 1; n   for (int i = 0; i < n; ++i) { n     if (colors[i] == bestColor) { n       result.second.push_back(i); n     } n   } n   result.second.resize((k + 1) / 2); n   return result; n } n int main() { n   int T = 1; n   std::ios::sync_with_stdio(false); n   while (T--) { n     int n; n     cin >> n; n     ; n     int m; n     cin >> m; n     ; n     int k; n     cin >> k; n     ; n     vvint g(n); n     while (m--) { n       int u; n       cin >> u; n       ; n       int v; n       cin >> v; n       ; n       u--, v--; n       g[u].push_back(v); n       g[v].push_back(u); n     } n     auto result = solveD(g, k); n     assert(result.first == 1 || result.first == 2); n     cout << result.first << endl; n     for (int& v : result.second) { n       v++; n     } n     if (result.first == 2) { n       cout << result.second.size() << endl; n     } n     std::copy(std::begin(result.second), std::end(result.second), n               ostream_iterator<int>(cout,    )); n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[500000]; n int mina, maxa; n int n, k; n long long sum = 0; n bool check(int c) { n   sum = 0; n   for (int i = 0; i < n; i++) { n     if (a[i] + c < mina) { n       sum += mina - c - a[i]; n     } else { n       break; n     } n   } n   if (sum > k) return false; n   return true; n } n bool checkp(int c) { n   sum = 0; n   for (int i = 0; i < n; i++) { n     if (a[i] - c > maxa) { n       sum += a[i] - c - maxa; n     } n   } n   if (sum > k) return false; n   return true; n } n int main() { n   scanf( %d %d , &n, &k); n   long long sum = 0; n   for (int i = 0; i < n; i++) { n     scanf( %d , &a[i]); n     sum += a[i]; n   } n   if (sum % n == 0) { n     mina = maxa = sum / n; n   } else { n     mina = sum / n; n     maxa = sum / n + 1; n   } n   sort(a, a + n); n   int ll = 0, rr = 1000000000; n   while (ll != rr) { n     int mid = (ll + rr) / 2; n     if (check(mid)) { n       rr = mid; n     } else { n       ll = mid + 1; n     } n   } n   int lll = 0, rrr = 1000000000; n   while (lll != rrr) { n     int mid = (lll + rrr) / 2; n     if (checkp(mid)) { n       rrr = mid; n     } else { n       lll = mid + 1; n     } n   } n   printf( %lld , (long long)lll + ll + maxa - mina); n   scanf( %d , &n); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, s, f, i; n   cin >> n >> m >> s >> f; n   long long t; n   map<long long, pair<int, int>> h; n   pair<int, int> R; n   for (i = 0; i < m; i++) { n     cin >> t >> R.first >> R.second; n     h[t] = R; n   } n   i = 1; n   if (s < f) n     while (s != f) { n       if (h.find(i) == h.end()) { n         cout <<  R ; n         s++; n       } else { n         if ((h[i].first <= s && s <= h[i].second) || n             (h[i].first <= s + 1 && s + 1 <= h[i].second)) { n           cout <<  X ; n         } else { n           cout <<  R ; n           s++; n         } n       } n       i++; n     } n   else { n     while (s != f) { n       if (h.find(i) == h.end()) { n         cout <<  L ; n         s--; n       } else { n         if ((h[i].first <= s && s <= h[i].second) || n             (h[i].first <= s - 1 && s - 1 <= h[i].second)) { n           cout <<  X ; n         } else { n           cout <<  L ; n           s--; n         } n       } n       i++; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int d[n]; n   int total = 0; n   for (int i = 0; i < n; i++) { n     cin >> d[i]; n     total += d[i]; n   } n   int s, t; n   cin >> s >> t; n   if (s == t) { n     cout << 0; n     return 0; n   } n   if (s > t) swap(s, t); n   int ans = 0; n   for (int i = s; i < t; i++) { n     ans += d[i - 1]; n   } n   cout << min(ans, total - ans); n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, Q; n double sy, l[200100], r[200100], sum[200100], zl, zr, x, y, ans; n struct Node { n   double x, y; n } bj[2][200100], tmp, L, R; n inline bool lef(Node u, Node v, Node w) { n   if (u.x == v.x) return w.x < u.x; n   double t; n   t = v.x - (v.x - u.x) * (v.y - w.y) / (v.y - u.y); n   return w.x < t; n } n inline double ask(int u) { n   double p, q; n   if (bj[0][u].x == x) n     p = x; n   else { n     p = x - (x - bj[0][u].x) * (y - sy) / y; n   } n   if (bj[1][u].x == x) n     q = x; n   else { n     q = x - (x - bj[1][u].x) * (y - sy) / y; n   } n   return max((double)0, min(q, zr) - max(p, zl)); n } n int main() { n   int i, j, p, q, mid, a, b; n   cin >> sy >> zl >> zr >> n; n   L.y = R.y = sy, L.x = zl, R.x = zr; n   for (i = 1; i <= n; i++) { n     scanf( %lf%lf , &l[i], &r[i]); n     sum[i] = sum[i - 1] + r[i] - l[i]; n     bj[0][i].y = 0, bj[0][i].x = l[i]; n     bj[1][i].y = 0, bj[1][i].x = r[i]; n   } n   cin >> m; n   for (i = 1; i <= m; i++) { n     scanf( %lf%lf , &x, &y); n     ans = 0; n     tmp.x = x, tmp.y = y; n     for (p = 1, q = n; p < q;) { n       mid = ((p + q) >> 1); n       if (lef(tmp, bj[0][mid], L)) n         q = mid; n       else n         p = mid + 1; n     } n     a = p - 1; n     for (p = 1, q = n; p < q;) { n       mid = ((p + q) >> 1); n       if (!lef(tmp, bj[1][mid], R)) n         p = mid + 1; n       else n         q = mid; n     } n     b = p; n     printf( %.10f n , ask(a) + ask(b) * (a != b) + n                           max((double)0, sum[b - 1] - sum[a]) * (y - sy) / y); n   } n } n


#include <bits/stdc++.h> n using namespace std; n #pragma warning(disable : 4996) n const int MAX_N = 1000009; n const long long mod = 998244353; n long long modpow(long long a, long long b, long long m) { n   long long p = 1, q = a; n   for (int i = 0; i < 32; i++) { n     if ((b / (1LL << i)) % 2 == 1) { n       p *= q; n       p %= m; n     } n     q *= q; n     q %= m; n   } n   return p; n } n long long N, fact[MAX_N], factinv[MAX_N]; n void init() { n   fact[0] = 1; n   for (int i = 1; i <= 1000000; i++) fact[i] = (1LL * fact[i - 1] * i) % mod; n   for (int i = 0; i <= 1000000; i++) factinv[i] = modpow(fact[i], mod - 2, mod); n } n long long ncr(long long n, long long r) { n   if (r < 0 || n < r) return 0; n   return (fact[n] * factinv[r] % mod) * factinv[n - r] % mod; n } n int main() { n   cin >> N; n   init(); n   long long V1 = modpow(modpow(3, N, mod), N, mod) - n                  modpow((modpow(3, N, mod) - 3LL + mod), N, mod); n   V1 += mod; n   V1 *= 2LL; n   V1 %= mod; n   long long V2 = 0; n   for (int i = 1; i <= N; i++) { n     long long E1 = ncr(N, i); n     long long E2 = modpow(3, N - i, mod); n     long long E3 = (E2 + mod - 1) % mod; n     E2 = modpow(E2, N, mod); n     E3 = modpow(E3, N, mod); n     if (i % 2 == 1) V2 += E1 * (E2 - E3 + mod); n     if (i % 2 == 0) V2 -= E1 * (E2 - E3 + mod); n     while (V2 < 0) V2 += mod * mod; n     V2 %= mod; n   } n   V2 *= 3LL; n   V2 %= mod; n   cout << (V1 - V2 + mod) % mod << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long double pi = 4 * atan((long double)1); n const long long mod = 1e9 + 7; n const long long inf = 922337203685477; n const long long nax = 1e5 + 5; n long long n, p, m; n long long d[nax], t[nax]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cin >> n >> p >> m; n   for (long long i = 1; i <= n; i++) { n     cin >> d[i] >> t[i]; n   } n   d[0] = 1; n   d[n + 1] = m + 1; n   long long cur = 0, ans = 0; n   for (long long i = 1; i <= n + 1; i++) { n     long long dif = 0; n     if (cur > 0) { n       dif = cur / p; n     } n     long long minus = d[i] - d[i - 1] - dif; n     if (minus < 0) { n       minus = 0; n     } n     ans += minus; n     cur -= (d[i] - d[i - 1]) * p; n     cur += t[i]; n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 10; n const int SIZ_TREE = 3e5 + 10; n int n, m; n struct Node { n   int u, v; n   Node(int U = 0, int V = 0) { n     u = U, v = V; n     if (u < v) swap(u, v); n   } n   bool operator<(const Node A) const { n     if (u < A.u || (u == A.u && v < A.v)) return true; n     return false; n   } n }; n vector<int> f[N], ans[2]; n vector<Node> edg; n map<Node, bool> col; n bool v[N], opr[N], flag1, flag2, add[N]; n int cnt_p, cnt_opr; n void init() { n   char s; n   int x, y; n   scanf( %d %d , &n, &m); n   for (int i = 1; i <= m; i++) { n     scanf( %d %d , &x, &y); n     while ((s = getchar()) ==    ) n       ; n     f[y].push_back(x); n     f[x].push_back(y); n     edg.push_back(Node(x, y)); n     col[Node(x, y)] = (s ==  R  ? false : true); n   } n } n bool dfs(int x) { n   v[x] = true; n   cnt_p++; n   cnt_opr += opr[x]; n   for (int i = 0; i < f[x].size(); i++) { n     int y = f[x][i]; n     if (v[y]) { n       if ((col[Node(x, y)] ^ opr[x] ^ opr[y]) == true) return false; n     } else { n       opr[y] = col[Node(x, y)] ^ opr[x]; n       if (!dfs(y)) return false; n     } n   } n   return true; n } n void add_ans(int x, bool s1, int s2) { n   if (add[x]) return; n   opr[x] ^= s1; n   add[x] = true; n   if (opr[x]) ans[s2].push_back(x); n   for (int i = 0; i < f[x].size(); i++) add_ans(f[x][i], s1, s2); n } n bool solve(int sign) { n   memset(opr, 0, sizeof(opr)); n   memset(v, 0, sizeof(v)); n   memset(add, 0, sizeof(add)); n   for (int i = 1; i <= n; i++) n     if (!v[i]) { n       cnt_opr = cnt_p = 0; n       if (!dfs(i)) return false; n       add_ans(i, (cnt_opr * 2 > cnt_p), sign); n     } n   return true; n } n void print(int s) { n   printf( %d n , ans[s].size()); n   for (int i = 0; i < ans[s].size(); i++) printf( %d  , ans[s][i]); n   printf(  n ); n } n void work() { n   flag1 = solve(0); n   for (int i = 0; i < edg.size(); i++) col[edg[i]] = (!col[edg[i]]); n   flag2 = solve(1); n   if (!flag1 && !flag2) n     printf( -1 n ); n   else if (flag1 && !flag2) n     print(0); n   else if (!flag1 && flag2) n     print(1); n   else if (ans[0].size() < ans[1].size()) n     print(0); n   else n     print(1); n } n int main() { n   init(); n   work(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e6 + 3; n long long power(long long base, long long exp) { n   long long ans = 1; n   while (exp) { n     if (exp & 1) ans = (ans * base) % MOD; n     base = (base * base) % MOD; n     exp >>= 1; n   } n   return ans; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   long long n, k; n   cin >> n >> k; n   if (n <= 63 && k > (1LL << n)) { n     cout << 1 <<     << 1; n     return 0; n   } n   long long v2 = 0; n   int digits = __builtin_popcountll(k - 1); n   v2 = k - 1 - digits; n   long long ntmp = n % (MOD - 1); n   if (ntmp < 0) ntmp += (MOD - 1); n   long long ktmp = k % (MOD - 1); n   if (ktmp < 0) ktmp += (MOD - 1); n   long long v2tmp = v2 % (MOD - 1); n   if (v2tmp < 0) v2tmp += (MOD - 1); n   long long exponent = ntmp * (ktmp - 1) - v2tmp; n   exponent %= (MOD - 1); n   if (exponent < 0) exponent += MOD - 1; n   long long denom = power(2, exponent); n   long long numpart = 0; n   if (k - 1 >= MOD) { n     numpart = 0; n   } else { n     long long prod = 1; n     long long ntmp2 = power(2, ntmp); n     prod = power(2, v2tmp); n     prod = power(prod, MOD - 2); n     if (prod < 0) prod += MOD; n     for (long long y = 1; y <= k - 1; y++) { n       prod = (prod * (ntmp2 - y)) % MOD; n     } n     numpart = prod; n   } n   long long num = (denom - numpart) % MOD; n   num %= MOD; n   denom %= MOD; n   if (num < 0) num += MOD; n   if (denom < 0) denom += MOD; n   cout << num <<     << denom; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   ; n   long long n, k, i; n   cin >> n >> k; n   vector<pair<long long, long long> > v; n   long long a[n], b[n]; n   long long x, y; n   for (i = 0; i < n; i++) cin >> a[i]; n   for (i = 0; i < n; i++) cin >> b[i]; n   for (i = 0; i < n; i++) { n     x = a[i] - b[i]; n     v.push_back(make_pair(x, i)); n   } n   sort(v.begin(), v.end()); n   i = 0; n   y = 0; n   while (i < n) { n     if (i < k || v[i].first <= 0) n       y = y + a[v[i].second]; n     else if (i >= k && v[i].first > 0) n       break; n     ++i; n   } n   while (i < n) { n     y = y + b[v[i].second]; n     ++i; n   } n   cout << y; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long s, x; n   cin >> s >> x; n   long long ans = __builtin_popcountll(x); n   ans = 1LL << ans; n   if (s == x) ans -= 2; n   for (int i = 0; i < 60; i++) { n     if (x & (1LL << i)) s -= 1LL << i; n   } n   for (int i = 0; i < 60; i++) { n     int j = i + 1; n     if ((s & (1LL << j)) && !(x & (1LL << i))) s -= 1LL << j; n   } n   if (s != 0) n     puts( 0 ); n   else n     cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, tmp; n   bool ok = 1; n   int ar[5]; n   memset(ar, 0, sizeof(ar)); n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) { n     scanf( %d , &tmp); n     tmp /= 25; n     if (!ok) continue; n     if (tmp == 1) { n       ar[1]++; n     } else if (tmp == 2) { n       if (ar[1] > 0) { n         ar[1]--; n         ar[2]++; n       } else n         ok = 0; n     } else if (tmp == 4) { n       if (ar[1] > 0 && ar[2] > 0) { n         ar[1]--; n         ar[2]--; n         ar[4]++; n       } else if (ar[1] > 2) { n         ar[1] -= 3; n         ar[4]++; n       } else n         ok = 0; n     } n   } n   if (ok) n     cout <<  YES  << endl; n   else n     cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Que { n   int a, b, w; n } Q[300010]; n bool operator<(Que a, Que b) { return a.b < b.b; } n int n, i, m, Lim, j; n long long ans[301000], w[301000], s[301000]; n int main() { n   scanf( %d , &n); n   for (i = 0; i < n; i++) scanf( %I64d , &w[i]); n   scanf( %d , &m); n   for (i = 0; i < m; i++) scanf( %d%d , &Q[i].a, &Q[i].b), Q[i].a--, Q[i].w = i; n   sort(Q, Q + m); n   Lim = (int)sqrt(n); n   for (i = 0; i < m; i++) { n     if (Q[i].b > Lim) { n       long long tmp = 0; n       for (j = Q[i].a; j < n; j += Q[i].b) tmp += w[j]; n       ans[Q[i].w] = tmp; n     } else { n       memset(s, 0, sizeof(s)); n       for (j = n - 1; j >= 0; j--) s[j] = w[j] + s[j + Q[i].b]; n       j = i; n       do { n         ans[Q[j].w] = s[Q[j].a]; n         j++; n       } while (Q[j].b == Q[j - 1].b); n       i = j - 1; n     } n   } n   for (i = 0; i < m; i++) printf( %I64d n , ans[i]); n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 1000000 + 5; n const long long DIFF = 2000; n const long long mod = 1000000000 + 7; n long long dp[DIFF + 5][DIFF + 5]; n char s[maxn]; n int main() { n   dp[0][0] = 1; n   for (int i = 1; i <= DIFF; dp[i][0] = dp[i - 1][1], i++) n     for (int j = 1; j <= i; j++) n       dp[i][j] = (dp[i - 1][j - 1] + dp[i - 1][j + 1]) % mod; n   long long n, m; n   scanf( %I64d%I64d%s , &n, &m, s); n   long long len = strlen(s), diff = 0, Min = 0, ans = 0; n   for (long long i = 0; i < len; i++) { n     diff += (s[i] ==  (  ? 1 : -1); n     Min = min(Min, diff); n   } n   for (long long i = 0; i <= n - m; i++) n     for (long long j = 0; j <= i; j++) n       if (j + Min >= 0 && j + diff >= 0 && j + diff <= n - m) n         ans = (ans + (dp[i][j] * dp[n - m - i][j + diff]) % mod) % mod; n   printf( %I64d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int x, y, a, b, i, c = 0, j = 0; n   cin >> x >> y; n   a = 240 - y; n   for (i = 1; c <= a; i++) { n     c = c + (5 * i); n     j++; n   } n   if (j - 1 <= x) { n     cout << j - 1 << endl; n   } else { n     cout << x << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long mx = (1 << 22); n long long f[mx], ans[mx]; n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   long long n; n   cin >> n; n   long long a[n]; n   for (long long i = 0; i < n; i++) cin >> a[i]; n   memset(ans, -1, sizeof(ans)); n   for (long long i = 0; i < n; i++) { n     f[a[i]]++; n     ans[a[i]] = a[i]; n   } n   for (long long j = 0; j < 22; j++) n     for (long long i = 0; i < mx; i++) n       if (i & (1 << j)) { n         f[i] += f[i ^ (1 << j)]; n         if (f[i ^ (1 << j)]) ans[i] = ans[i ^ (1 << j)]; n       } n   for (long long i = 0; i < n; i++) { n     long long p = (mx - 1) & (~a[i]); n     cout << ans[p] <<    ; n   } n   cout <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n struct node { n   int x, y, c; n } s[2005], tmp[2005]; n int n, k, l, cx, cy; n int lx[2005], ly[2005], bk[2005]; n multiset<int> t[2005]; n namespace Tree { n int mn[8005], mx[8005], sum[8005], lzy[8005]; n inline void pushtag(int p, int l, int r, int val) { n   mx[p] = mn[p] = lzy[p] = val; n   sum[p] = 1ll * (ly[r + 1] - ly[l]) * val % mod; n } n inline void down(int p, int l, int mid, int r) { n   pushtag(p << 1, l, mid, lzy[p]); n   pushtag(p << 1 | 1, mid + 1, r, lzy[p]); n   lzy[p] = -1; n } n void insert(int p, int id, int val, int l, int r) { n   if (l == r) n     return mx[p] = mn[p] = val, n            sum[p] = 1ll * (ly[id + 1] - ly[id]) * val % mod, void(); n   const int mid = l + r >> 1; n   if (id <= mid) n     insert(p << 1, id, val, l, mid); n   else n     insert(p << 1 | 1, id, val, mid + 1, r); n   mn[p] = min(mn[p << 1], mn[p << 1 | 1]); n   mx[p] = max(mx[p << 1], mx[p << 1 | 1]); n   sum[p] = (sum[p << 1] + sum[p << 1 | 1]) % mod; n } n void qmin(int p, int val, int l, int r, int L, int R) { n   if (L >= l && R <= r) { n     if (mx[p] <= val) n       return; n     else if (mn[p] >= val) n       pushtag(p, L, R, val); n     else { n       const int mid = L + R >> 1; n       if (~lzy[p]) down(p, L, mid, R); n       qmin(p << 1, val, l, r, L, mid); n       qmin(p << 1 | 1, val, l, r, mid + 1, R); n       mn[p] = min(mn[p << 1], mn[p << 1 | 1]); n       mx[p] = max(mx[p << 1], mx[p << 1 | 1]); n       sum[p] = (sum[p << 1] + sum[p << 1 | 1]) % mod; n     } n     return; n   } n   const int mid = L + R >> 1; n   if (~lzy[p]) down(p, L, mid, R); n   if (l <= mid) qmin(p << 1, val, l, r, L, mid); n   if (r > mid) qmin(p << 1 | 1, val, l, r, mid + 1, R); n   mn[p] = min(mn[p << 1], mn[p << 1 | 1]); n   mx[p] = max(mx[p << 1], mx[p << 1 | 1]); n   sum[p] = (sum[p << 1] + sum[p << 1 | 1]) % mod; n } n }  // namespace Tree n int main() { n   scanf( %d%d%d , &n, &k, &l); n   lx[++cx] = 0; n   ly[++cy] = 0; n   lx[++cx] = l + 1; n   ly[++cy] = l + 1; n   int ans = 0; n   for (int i = 1; i <= n; ++i) n     scanf( %d%d%d , &s[i].x, &s[i].y, &s[i].c), lx[++cx] = ++s[i].x, n                                                 ly[++cy] = ++s[i].y; n   sort(lx + 1, lx + cx + 1); n   cx = unique(lx + 1, lx + cx + 1) - lx - 1; n   sort(ly + 1, ly + cy + 1); n   cy = unique(ly + 1, ly + cy + 1) - ly - 1; n   for (int i = 1; i <= n; ++i) n     s[i].x = lower_bound(lx + 1, lx + cx + 1, s[i].x) - lx, n     s[i].y = lower_bound(ly + 1, ly + cy + 1, s[i].y) - ly, tmp[i] = s[i]; n   sort(s + 1, s + n + 1, n        [](const node &a, const node &b) { return a.x < b.x; }); n   sort(tmp + 1, tmp + n + 1, n        [](const node &a, const node &b) { return a.y < b.y; }); n   for (int u = 1; u < cx; ++u) { n     memset(bk, 0, sizeof(bk)); n     memset(Tree::lzy, -1, sizeof(Tree::lzy)); n     for (int i = 1, j = 1, p = 1, cnt = 0; i < cy; ++i) { n       while (j <= n && tmp[j].y <= i) { n         if (tmp[j].x <= u) n           ++j; n         else { n           if (!bk[tmp[j].c]) ++cnt; n           ++bk[tmp[j++].c]; n         } n       } n       while (cnt == k) { n         if (tmp[p].x <= u) n           ++p; n         else { n           --bk[tmp[p].c]; n           if (!bk[tmp[p++].c]) --cnt; n         } n       } n       Tree::insert(1, i, ly[tmp[p - 1].y], 1, cy - 1); n     } n     for (int i = 1; i <= k; ++i) t[i].clear(); n     for (int i = 1; i <= n; ++i) n       if (s[i].x > u) t[s[i].c].insert(s[i].y); n     for (int d = cx - 1, i = n; d > u; --d) { n       (ans += 1ll * (lx[d + 1] - lx[d]) * (lx[u + 1] - lx[u]) % mod * n               Tree::sum[1] % mod) %= mod; n       while (i && s[i].x >= d) { n         auto it = t[s[i].c].find(s[i].y); n         int pre = it == t[s[i].c].begin() ? 0 : *prev(it); n         int nxt = next(it) == t[s[i].c].end() ? cy : *next(it); n         t[s[i].c].erase(it); n         if (s[i].y < nxt) Tree::qmin(1, ly[pre], s[i].y, nxt - 1, 1, cy - 1); n         --i; n       } n     } n   } n   return printf( %d n , ans), 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T1, class T2> n inline void upd1(T1& a, const T2& b) { n   a = a < b ? a : b; n } n template <class T1, class T2> n inline void upd2(T1& a, const T2& b) { n   a = a > b ? a : b; n } n template <class T> n inline bool equ(const T& a, const T& b) { n   return !memcmp(&a, &b, sizeof a); n } n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n struct ano { n   operator long long() { n     long long x = 0, y = 0, c = getchar(); n     while (c < 48) y = c == 45, c = getchar(); n     while (c > 47) x = x * 10 + c - 48, c = getchar(); n     return y ? -x : x; n   } n } buf; n const int p = 1e9 + 7; n const int N = 1e6 + 5; n int w[N], a[N], l[N], r[N]; n long long cal(long long a, long long n, long long d) { n   return (a * n + n * (n - 1) / 2 % p * d) % p; n } n int main() { n   int n = buf, m = buf; n   for (int i = 1; i <= n; ++i) w[i] = buf; n   w[n + 1] = 1 << 30; n   int j = 0; n   for (int i = 1; i <= n + 1; ++i) { n     while (j > 0 && w[a[j]] <= w[i]) r[a[j--]] = i - 1; n     l[i] = a[j] + 1; n     a[++j] = i; n   } n   long long ans = 0; n   for (int i = 1; i <= n; ++i) { n     int a = i - l[i] + 1; n     int b = r[i] - i + 1; n     int c = (min(a, b) - 1) / (m - 1); n     long long s = cal(m, c, m - 1); n     int v = (c + 1) * (m - 1) + 1; n     if (max(a, b) >= v) { n       long long d = (max(a, b) - v) / (m - 1); n       s += ((d + 1) * min(a, b)) % p; n       v += (d + 1) * (m - 1); n     } n     v = a + b - v; n     if (v >= 1) s += cal(v, (v - 1) / (m - 1) + 1, 1 + p - m); n     ans += w[i] * s % p; n   } n   cout << ans % p << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 105, maxm = 5005; n const double eps = 1e-9; n int N, M, m, X[maxm], Y[maxm], Z[maxm]; n double f[maxn][maxn], p[maxn]; n int main() { n   scanf( %d%d , &N, &M); n   for (int i = 1; i <= M; i++) { n     scanf( %d%d%d , &X[i], &Y[i], &Z[i]); n     if (X[i] > 1 && X[i] < N) f[X[i]][X[i]]++, f[X[i]][Y[i]]--; n     if (Y[i] > 1 && Y[i] < N) f[Y[i]][Y[i]]++, f[Y[i]][X[i]]--; n   } n   f[1][N + 1] = 1, f[1][1] = 1; n   f[N][N + 1] = 0, f[N][N] = 1; n   for (int i = 1; i <= N; i++) { n     if (abs(f[i][i]) < eps) n       for (int j = i + 1; j <= N; j++) n         if (abs(f[j][i]) > eps) { n           for (int k = i; k <= N + 1; k++) f[i][k] += f[j][k]; n           break; n         } n     for (int j = i + 1; j <= N; j++) n       if (abs(f[j][i]) > eps) { n         double r(f[j][i] / f[i][i]); n         for (int k = i; k <= N + 1; k++) f[j][k] -= f[i][k] * r; n       } n   } n   for (int i = N; i; i--) n     if (abs(f[i][i]) > eps) { n       p[i] = f[i][N + 1]; n       for (int j = i + 1; j <= N; j++) p[i] -= f[i][j] * p[j]; n       p[i] /= f[i][i]; n     } n   double rate(1.0 / 0), ans(0); n   for (int i = 1; i <= M; i++) rate = min(rate, Z[i] / abs(p[X[i]] - p[Y[i]])); n   for (int i = 1; i <= M; i++) { n     if (X[i] == 1) ans += p[1] - p[Y[i]]; n     if (Y[i] == 1) ans += p[1] - p[X[i]]; n   } n   printf( %.7f n , isfinite(rate) ? ans * rate : 0); n   for (int i = 1; i <= M; i++) n     printf( %.7f n , isfinite(rate) ? (p[X[i]] - p[Y[i]]) * rate : 0); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n; n   scanf( %d , &n); n   int i = 1; n   while ((i * (i + 1) / 2) <= n) { n     if ((i * (i + 1) / 2) == n) { n       printf( YES ); n       return 0; n     } n     i++; n   } n   printf( NO ); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 123, inf = 1e9 + 123, mod = 1e9 + 7; n const long long INF = 1e18; n int n, a[N]; n int main() { n   cin.tie(0); n   cout.tie(0); n   ios_base::sync_with_stdio(0); n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   sort(a + 1, a + n + 1); n   reverse(a + 1, a + n + 1); n   int mn = inf; n   for (int i = 1; i <= n; i++) { n     if (a[i] + 1 < mn) { n       a[i]++; n       mn = a[i]; n     } else { n       if (a[i] < mn) { n         mn = a[i]; n       } else if (a[i] - 1 < mn && a[i] > 1) { n         mn = a[i] - 1; n         a[i]--; n       } n     } n   } n   set<int> st; n   for (int i = 1; i <= n; i++) { n     st.insert(a[i]); n   } n   cout << st.size(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n void rd(T &x) { n   x = 0; n   int f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) n     x = (x << 3) + (x << 1) + ch -  0 , ch = getchar(); n   x *= f; n } n const int maxn = 1e5 + 5; n int n, m; n int ans[maxn]; n struct node { n   int x, op, id; n   bool operator<(const node &b) const { return x == b.x ? id < b.id : x < b.x; } n } p[maxn]; n struct segment_tree { n   int op[maxn << 2], tag[maxn << 2]; n   void pushdown(int u) { n     if (tag[u]) { n       op[u << 1] = op[u << 1 | 1] = 1; n       tag[u << 1] = tag[u << 1 | 1] = tag[u]; n       tag[u] = 0; n     } n     if (op[u] == -1) { n       op[u << 1] *= -1; n       op[u << 1 | 1] *= -1; n       op[u] = 1; n     } n   } n   void build(int u, int l, int r) { n     op[u] = 1; n     if (l == r) return (void)(op[u] = p[l].op); n     int mid = l + r >> 1; n     build(u << 1, l, mid); n     build(u << 1 | 1, mid + 1, r); n   } n   void cover(int u, int l, int r, int ql, int qr, int v) { n     if (ql <= l && r <= qr) return (void)(op[u] = 1, tag[u] = v); n     int mid = l + r >> 1; n     pushdown(u); n     if (ql <= mid) cover(u << 1, l, mid, ql, qr, v); n     if (qr > mid) cover(u << 1 | 1, mid + 1, r, ql, qr, v); n   } n   void change(int u, int l, int r, int ql, int qr) { n     if (ql <= l && r <= qr) return (void)(op[u] *= -1); n     int mid = l + r >> 1; n     pushdown(u); n     if (ql <= mid) change(u << 1, l, mid, ql, qr); n     if (qr > mid) change(u << 1 | 1, mid + 1, r, ql, qr); n   } n   void query(int u, int l, int r) { n     if (l == r) { n       int res = tag[u] ? tag[u] * op[u] : op[u]; n       ans[p[l].id] = res * p[l].x; n       return; n     } n     int mid = l + r >> 1; n     pushdown(u); n     query(u << 1, l, mid); n     query(u << 1 | 1, mid + 1, r); n   } n } seg; n int main() { n   rd(n), rd(m); n   for (int i = (1); i <= (n); ++i) { n     int x; n     rd(x); n     p[i] = (node){abs(x), x > 0 ? 1 : -1, i}; n   } n   sort(p + 1, p + n + 1); n   seg.build(1, 1, n); n   while (m--) { n     char s[5]; n     int x; n     scanf( %s , s); n     rd(x); n     if (s[0] ==  < ) { n       if (x < 0) { n         int pos = (lower_bound(p + 1, p + n + 1, (node){abs(x) + 1, 0, 0}) - p); n         if (pos <= n) seg.cover(1, 1, n, pos, n, 1); n       } else { n         int pos = (lower_bound(p + 1, p + n + 1, (node){abs(x), 0, 0}) - p); n         if (pos <= n) seg.cover(1, 1, n, pos, n, 1); n         if (pos > 1) seg.change(1, 1, n, 1, pos - 1); n       } n     } else { n       if (x > 0) { n         int pos = (lower_bound(p + 1, p + n + 1, (node){abs(x) + 1, 0, 0}) - p); n         if (pos <= n) seg.cover(1, 1, n, pos, n, -1); n       } else { n         int pos = (lower_bound(p + 1, p + n + 1, (node){abs(x), 0, 0}) - p); n         if (pos <= n) seg.cover(1, 1, n, pos, n, -1); n         if (pos > 1) seg.change(1, 1, n, 1, pos - 1); n       } n     } n   } n   seg.query(1, 1, n); n   for (int i = (1); i <= (n); ++i) printf( %d  , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int pic[610][610]; n int ABS(int x) { n   if (x < 0) return -x; n   return x; n } n int main(int argc, char const *argv[]) { n   int q; n   scanf( %d , &q); n   for (int i = 0; i < q; i++) { n     int n, m, pix = 0, sum1 = 0, sum2 = 0; n     scanf( %d%d , &n, &m); n     for (int k = 0; k < n; k++) n       for (int j = 0; j < m; j++) { n         scanf( %d , &pic[k][j]); n       } n     int dif = 0; n     for (int j = 0; j < m; j++) n       dif = dif + ABS(pic[n / 2][j] - pic[n / 2 - 1][j]); n     if (dif < 6000) n       printf( NO n ); n     else n       printf( YES n ); n   } n   return 0; n } n