#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3fffffff; n const long long oo = 3000000000000000000LL; n int tes, n; n long long ans[3], mn[4], mx[4], ll[4], rr[4], lt[4], rt[4], ret[3]; n bool get(long long s, long long x, long long y, long long z) { n   if (s <= x) { n     ret[0] = s; n     ret[1] = 0; n     ret[2] = 0; n     return 1; n   } else if (s <= x + y) { n     ret[0] = x; n     ret[1] = s - x; n     ret[2] = 0; n     return 1; n   } else if (s <= x + y + z) { n     ret[0] = x; n     ret[1] = y; n     ret[2] = s - x - y; n     return 1; n   } n   return 0; n } n long long shr1(long long x) { return (x - (x & 1)) / 2; } n bool jd(long long t) { n   int i, j; n   long long s, x, y, z; n   for (i = 0; i <= 3; i++) { n     if (mn[i] + t < mx[i] - t) return 0; n     ll[i] = mx[i] - t; n     rr[i] = mn[i] + t; n   } n   for (i = 0; i < 2; i++) { n     lt[0] = shr1(ll[0] - 3 * i + 1); n     rt[0] = shr1(rr[0] - 3 * i); n     for (j = 1; j <= 3; j++) { n       lt[j] = shr1(ll[j] - i + 1); n       rt[j] = shr1(rr[j] - i); n     } n     if (lt[0] <= rt[0] && lt[1] <= rt[1] && lt[2] <= rt[2] && lt[3] <= rt[3] && n         lt[1] + lt[2] + lt[3] <= rt[0]) { n       if (lt[1] + lt[2] + lt[3] >= lt[0]) { n         ret[0] = lt[1]; n         ret[1] = lt[2]; n         ret[2] = lt[3]; n         ans[0] = ret[1] + ret[2] + i; n         ans[1] = ret[0] + ret[2] + i; n         ans[2] = ret[0] + ret[1] + i; n         return 1; n       } n       if (get(lt[0] - lt[1] - lt[2] - lt[3], rt[1] - lt[1], rt[2] - lt[2], n               rt[3] - lt[3])) { n         ret[0] += lt[1]; n         ret[1] += lt[2]; n         ret[2] += lt[3]; n         ans[0] = ret[1] + ret[2] + i; n         ans[1] = ret[0] + ret[2] + i; n         ans[2] = ret[0] + ret[1] + i; n         return 1; n       } n     } n   } n   return 0; n } n int main() { n   long long x, y, z, s, l, r, mid; n   int i; n   scanf( %d , &tes); n   while (tes--) { n     scanf( %d , &n); n     for (i = 0; i <= 3; i++) { n       mx[i] = -oo; n       mn[i] = oo; n     } n     for (i = 1; i <= n; i++) { n       scanf( %I64d%I64d%I64d , &x, &y, &z); n       s = x + y + z; n       mx[0] = max(mx[0], s); n       mn[0] = min(mn[0], s); n       mx[1] = max(mx[1], s - x - x); n       mn[1] = min(mn[1], s - x - x); n       mx[2] = max(mx[2], s - y - y); n       mn[2] = min(mn[2], s - y - y); n       mx[3] = max(mx[3], s - z - z); n       mn[3] = min(mn[3], s - z - z); n     } n     l = 0; n     r = oo; n     while (l < r) { n       mid = (l + r) >> 1; n       if (jd(mid)) n         r = mid; n       else n         l = mid + 1; n     } n     jd(l); n     printf( %I64d %I64d %I64d n , ans[0], ans[1], ans[2]); n   } n } n


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


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, a, b, i, j, arr[1005] = {0}; n   cin >> n >> m; n   for (i = 1; i <= m; i++) { n     cin >> a >> b; n     arr[a] = 1; n     arr[b] = 1; n   } n   for (i = 1; i <= n; i++) { n     if (arr[i] == 0) break; n   } n   cout << n - 1 << endl; n   for (j = 1; j <= n; j++) { n     if (i != j) { n       cout << i <<     << j << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct block { n   string str; n   int pos; n }; n static bool compare(block b1, block b2) { n   int n = b1.str.length(); n   for (int i = 0; i < n; i++) { n     if (b1.str[i] != b2.str[i]) { n       if ((i + 1) % 2) { n         return (b1.str[i] < b2.str[i]); n       } else { n         return (b1.str[i] > b2.str[i]); n       } n     } n   } n } n int main() { n   int n, m; n   cin >> n >> m; n   vector<block> arr(n); n   for (int i = 0; i < n; i++) { n     cin >> arr[i].str; n     arr[i].pos = i + 1; n   } n   sort(arr.begin(), arr.end(), compare); n   for (int i = 0; i < n; i++) { n     cout << arr[i].pos <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n double ans[3], temp[3]; n int a[200200]; n int n; n bool f(double d) { n   double x = 0; n   int cnt = 0; n   for (int i = 0; i < n; i++) { n     if (x < a[i]) { n       if (cnt == 3) return false; n       x = a[i] + 2 * d; n       temp[cnt++] = a[i] + d; n     } n   } n   for (int i = 0; i < 3; i++) ans[i] = temp[i]; n   return true; n } n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) cin >> a[i]; n   sort(a, a + n); n   double ul = 0, ur = a[n - 1]; n   for (int k = 0; k < 400; k++) { n     double mid = (ul + ur) / 2; n     if (f(mid)) n       ur = mid; n     else n       ul = mid; n   } n   printf( %.6f n , ul); n   printf( %.6f %.6f %.6f n , ans[0], ans[1], ans[2]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n const int maxn = 3e6; n const double eps = 0.00000001; n long long ksm(long long a, long long n) { n   long long ans = 1; n   while (n) { n     if (n & 1) ans = ans * a % mod; n     a = a * a % mod; n     n >>= 1; n   } n   return ans; n } n int main() { n   long long c, n, ans; n   std::ios::sync_with_stdio(false); n   cin >> n; n   c = 1; n   for (int i = 1; i <= n; i++) c = c * i % mod; n   ans = (c + mod - ksm(2, n - 1)) % mod; n   cout << ans << endl; n } n


#include <bits/stdc++.h> n /*#include <iostream> n #include <algorithm> n #include <math.h> n #include <iomanip> n #include <string> n #include <vector> n #include <set> n #include <sstream>*/ n  n #define ll long long n #define fop(i,m,n) for(int i=m; i<n; i++) n #define fastIO ios::sync_with_stdio(0), cin.tie(0), cout.tie(0) n #define X first n #define Y second n #define pb push_back n #define mp make_pair n #define all(v) v.begin(),v.end() n #define ep emplace_back n  n using namespace std; n  n const long long MOD = 1e9+7; n const long long N = 3e5+5; n const long long Nlog = 17; n const long long Hash = 257; n  n const double PI = 2.0*acos(0.0);//<cmath> n //const double PI = 3.141592653; n const double E = 2.718281828; n  n struct bot{ n     int p, w; n }x[N]; n  n bool cmp(int a, int b){ n     return x[a].p < x[b].p; n } n  n void solve(){ n      n     int n, m; n     cin >> n >> m; n     fop(i,0,n) cin >> x[i].p; n     fop(i,0,n){ n         char in; n         cin >> in; n         if(in ==  R ) x[i].w = 1; n         else x[i].w = -1; n     } n      n     vector<int> v[2]; n     fop(i,0,n){ n         v[x[i].p%2].pb(i); n     } n     fop(i,0,2) sort(all(v[i]), cmp); n      n     vector<int> ans(n, -1); n      n     fop(t,0,2){ n         vector<int> s; n         for(int i : v[t]){ n             if(x[i].w == 1){ n                 s.pb(i); n             }else{ n                 if(s.empty()){ n                     s.pb(i); n                 }else{ n                     int j = s.back(); n                     s.pop_back(); n                      n                     if(x[j].w == 1) ans[i] = ans[j] = (x[i].p-x[j].p)/2; n                     else ans[i] = ans[j] = (x[i].p+x[j].p)/2; n                     //cout << i <<     << j <<   :   << ans[i] <<   n ; n                 } n             } n         } n          n         while(s.size() > 1){ n             int i = s.back(); n             s.pop_back(); n             int j = s.back(); n             s.pop_back(); n             if(x[j].w == 1) ans[i] = ans[j] = m - (x[i].p + x[j].p)/2; n             else ans[i] = ans[j] = m - (x[i].p - x[j].p)/2; n             //cout << i <<     << j <<   :   << ans[i] <<   n ; n         } n     } n      n     fop(i,0,n){ n         cout << ans[i] <<    ; n     } n     cout <<   n ; n      n } n  n  n int main() n { n     fastIO; n  n     int t=1; n     cin >> t; n     while(t--) solve(); n  n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n const int mod = 1000000007; n inline void add(int& x, int y) { n   x += y; n   if (x >= mod) x -= mod; n   if (x < 0) x += mod; n } n inline int mul(int x, int y) { return ((long long)x * y) % mod; } n int gcd(int a, int b) { n   if (b) return gcd(b, a % b); n   return a; n } n int power(int a, int p) { n   int ret = 1; n   while (p) { n     if (p & 1) ret = mul(ret, a); n     a = mul(a, a); n     p /= 2; n   } n   return ret; n } n int phi(int n) { n   int ret = n; n   int i = 2; n   if (n % i == 0) { n     ret -= ret / i; n     while (n % i == 0) n /= i; n   } n   for (i = 3; i * i <= n; i++) n     if (n % i == 0) { n       ret -= ret / i; n       while (n % i == 0) n /= i; n     } n   if (n > 1) ret -= ret / n; n   return ret; n } n bool E[400][400]; n int D[2][400][400]; n int main() { n   int N; n   scanf( %d , &N); n   int M; n   scanf( %d , &M); n   memset(D, 31, sizeof(D)); n   queue<pair<pair<int, int>, int>> Q; n   int inf = D[0][0][0]; n   ; n   D[0][0][0] = 0; n   while (M--) { n     int x, y; n     scanf( %d , &x); n     scanf( %d , &y); n     E[x - 1][y - 1] = E[y - 1][x - 1] = true; n   } n   Q.push({{0, 0}, 0}); n   while (!Q.empty()) { n     int ty = Q.front().second; n     pair<int, int> a = Q.front().first; n     Q.pop(); n     int nd = D[ty][a.first][a.second] + 1; n     int x = a.first; n     int y = a.second; n     if (ty == 0) { n       if (x != N - 1) { n         for (int i = 0; i < N; i++) n           if (i != x and E[x][i] == true and D[1][i][y] == inf) { n             D[1][i][y] = nd; n             Q.push({{i, y}, 1}); n             ; n           } n       } else if (D[1][x][y] == inf) { n         D[1][x][y] = nd; n         Q.push({{x, y}, 1}); n       } n     } n     if (ty == 1) { n       if (x == N - 1 and E[y][N - 1] == false) { n         cout << nd / 2 << endl; n         return 0; n       } n       if (y != N - 1) { n         for (int i = 0; i < N; i++) n           if (i != x and i != y and E[y][i] == false and D[0][x][i] == inf) { n             D[0][x][i] = nd; n             Q.push({{x, i}, 0}); n             ; n           } n       } else if (D[0][x][y] == inf) { n         D[0][x][y] = nd; n         Q.push({{x, y}, 0}); n       } n     } n   } n   cout << -1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n const int inf = 1e9 + 10; n const ll inf_ll = 1e18 + 10; n template <ll M> n struct modint { n   static ll reduce(ll n) { n     constexpr static ll b = -1ull / M; n     ll r = n - (ll)(__uint128_t(b) * n >> 64) * M; n     return r >= M ? r - M : r; n   } n   static ll inv(ll n) { n     ll r = 1; n     for (ll k = M - 2; k > 0; k >>= 1, n = reduce(n * n)) n       if (k & 1) r = reduce(r * n); n     return r; n   } n   ll v; n   modint(ll n = 0) : v(reduce(n)) { v += (M & 0 - (v < 0)); } n   friend string to_string(const modint n) { return to_string(n.v); } n   friend istream& operator>>(istream& i, modint& n) { return i >> n.v; } n   friend ostream& operator<<(ostream& o, const modint n) { return o << n.v; } n   template <typename T> n   explicit operator T() { n     return T(v); n   } n   friend bool operator==(const modint n, const modint m) { return n.v == m.v; } n   friend bool operator!=(const modint n, const modint m) { return n.v != m.v; } n   friend bool operator<(const modint n, const modint m) { return n.v < m.v; } n   friend bool operator<=(const modint n, const modint m) { return n.v <= m.v; } n   friend bool operator>(const modint n, const modint m) { return n.v > m.v; } n   friend bool operator>=(const modint n, const modint m) { return n.v >= m.v; } n   modint& operator+=(const modint n) { n     v += n.v; n     v -= (M & 0 - (v >= M)); n     return *this; n   } n   modint& operator-=(const modint n) { n     v -= n.v; n     v += (M & 0 - (v < 0)); n     return *this; n   } n   modint& operator*=(const modint n) { n     v = reduce(v * n.v); n     return *this; n   } n   modint& operator/=(const modint n) { n     v = reduce(v * inv(n.v)); n     return *this; n   } n   friend modint operator+(const modint n, const modint m) { n     return modint(n) += m; n   } n   friend modint operator-(const modint n, const modint m) { n     return modint(n) -= m; n   } n   friend modint operator*(const modint n, const modint m) { n     return modint(n) *= m; n   } n   friend modint operator/(const modint n, const modint m) { n     return modint(n) /= m; n   } n   modint& operator++() { return *this += 1; } n   modint& operator--() { return *this -= 1; } n   modint operator++(int) { n     modint t = *this; n     return *this += 1, t; n   } n   modint operator--(int) { n     modint t = *this; n     return *this -= 1, t; n   } n   modint operator+() { return *this; } n   modint operator-() { return modint(0) -= *this; } n   modint pow(const ll k) const { n     if (k < 1) return k == 0 ? modint(1) : modint(inv(this->v)).pow(-k); n     return k & 1 ? *this * (this->pow(k - 1)) : (*this * *this).pow(k >> 1); n   } n }; n using mod = modint<int(1e9 + 7)>; n const int N = 2e5 + 5; n ll l[N], r[N]; n mod p[N], w[N], s; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ll n; n   cin >> n; n   mod ans = n * n; n   for (int i = 0; i < n; i++) cin >> l[i]; n   for (int i = 0; i < n; i++) cin >> r[i]; n   for (int i = 0; i < n; i++) w[i] = r[i] - l[i] + 1; n   for (int i = 0; i < n - 1; i++) { n     p[i] = mod(max(0ll, min(r[i], r[i + 1]) - max(l[i], l[i + 1]) + 1)) / n            (w[i] * w[i + 1]); n     if (i >= 2) s += p[i - 2]; n     ans += p[i] * (2 * s + 1 - 2 * n); n   } n   for (int i = 0; i < n - 2; i++) { n     mod x = mod(max(0ll, min({r[i], r[i + 1], r[i + 2]}) - n                              max({l[i], l[i + 1], l[i + 2]}) + 1)) / n             (w[i] * w[i + 1] * w[i + 2]); n     ans += 2 * x; n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long N, K; n   cin >> N >> K; n   vector<long long> P(N); n   for (long long i = 0; i < N; i++) { n     cin >> P[i]; n   } n   sort(P.begin(), P.end()); n   long long min = P[0], vid = 0; n   for (long long i = 1; i < P.size(); i++) { n     if ((P[i] - P[0]) % K == 0) { n       vid += abs(P[i] - P[0]) / K; n     } else { n       cout << -1 << endl; n       return 0; n     } n   } n   cout << vid << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = (int)1e9; n const long long INFLL = (long long)1e16; n const int MOD = (int)1e9 + 7; n template <typename T> n void out(T x) { n   cout << x << endl; n   exit(0); n } n bool bysec(const pair<int, int> &a, const pair<int, int> &b) { n   return a.second < b.second; n } n const int maxn = (int)2e5 + 5; n int n, a[maxn]; n vector<int> zero, pos; n set<pair<int, int> > neg; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   bool deleted = false; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     int x; n     cin >> x; n     if (x > 0) { n       pos.push_back(i); n     } else if (x < 0) { n       neg.insert(make_pair(x, i)); n     } else { n       zero.push_back(i); n     } n   } n   if (pos.size() == 0 && neg.size() == 1 && zero.size() > 0) { n     if (zero.size() > 1) { n       for (int i = 1; i < (int)zero.size(); i++) { n         cout <<  1   << zero[i] <<     << zero[0] << endl; n       } n     } n     cout <<  2   << neg.begin()->second << endl; n     return 0; n   } n   if (pos.size() == 0 && neg.size() == 0 && zero.size() > 0) { n     if (zero.size() > 1) { n       for (int i = 1; i < (int)zero.size(); i++) { n         cout <<  1   << zero[i] <<     << zero[0] << endl; n       } n     } n     return 0; n   } n   if (zero.size() > 1) { n     for (int i = 1; i < (int)zero.size(); i++) { n       cout <<  1   << zero[i] <<     << zero[0] << endl; n     } n   } n   if (pos.size() > 1) { n     for (int i = 1; i < (int)pos.size(); i++) { n       cout <<  1   << pos[i] <<     << pos[0] << endl; n     } n   } n   if (neg.size() % 2 == 1) { n     int ind = neg.rbegin()->second; n     if (zero.size() > 0) { n       cout <<  1   << ind <<     << zero[0] << endl; n     } else { n       deleted = true; n       cout <<  2   << ind << endl; n     } n   } n   int sz = neg.size(); n   if (sz > 1) { n     int end = sz % 2 == 1 ? sz - 1 : sz; n     int f = neg.begin()->second; n     int i = 0; n     for (auto &p : neg) { n       if (i == 0) { n         i++; n         continue; n       } n       if (i == end) break; n       int cur = p.second; n       cout <<  1   << cur <<     << f << endl; n       i++; n     } n   } n   if (zero.size() > 0 && !deleted) { n     cout <<  2   << zero[0] << endl; n     deleted = true; n   } n   if (pos.size() > 0 && sz > 1) { n     cout <<  1   << pos[0] <<     << neg.begin()->second << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   string s; n   cin >> s; n   long long i = 0; n   while (i + 1 < s.size() && s[i] == s[i + 1]) { n     if (s[i] ==  0 ) { n       cout << 3 <<     << 1 <<   n ; n       cout << 1 <<     << 1 <<   n ; n     } else { n       cout << 1 <<     << 1 <<   n ; n       cout << 1 <<     << 3 <<   n ; n     } n     i += 2; n   } n   bool r = 0, c = 0; n   while (i < s.size()) { n     if (s[i] ==  0 ) { n       if (r) { n         cout << 1 <<     << 1 <<   n ; n         r = 0; n       } else { n         cout << 3 <<     << 1 <<   n ; n         r = 1; n       } n     } else { n       if (c) { n         cout << 1 <<     << 1 <<   n ; n         c = 0; n       } else { n         cout << 1 <<     << 3 <<   n ; n         c = 1; n       } n     } n     i++; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int x, y; n   cin >> x >> y; n   if (x > y) { n     int t = x; n     x = y; n     y = t; n   } n   cout << x <<     << (y - x) / 2 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 5; n string s; n int main() { n   cin >> s; n   int i = 0, cnt = 0; n   int len = s.size(); n   while ((i = s.find( VK )) != -1) { n     s[i] = s[i + 1] =  . ; n     cnt++; n   } n   if (s.find( VV ) != -1 || s.find( KK ) != -1) cnt++; n   printf( %d n , cnt); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-8; n const double PI = acos(-1.0); n int a[100005], s[100005], dp1[100005], dp2[100005], b[100005], ans[100005]; n int main() { n   int n, i, maxlen, k; n   scanf( %d , &n); n   for (i = 1; i <= n; i++) scanf( %d , &a[i]), b[n - i + 1] = -a[i]; n   for (i = 0; i <= n; i++) s[i] = 0x3f3f3f3f; n   maxlen = 0; n   for (i = 1; i <= n; i++) { n     k = lower_bound(s, s + n, a[i]) - s; n     s[k] = a[i]; n     dp1[i] = k + 1; n     maxlen = max(maxlen, dp1[i]); n   } n   for (i = 0; i <= n; i++) s[i] = 0x3f3f3f3f; n   for (i = 1; i <= n; i++) { n     k = lower_bound(s, s + n, b[i]) - s; n     s[k] = b[i]; n     dp2[n - i + 1] = k + 1; n   } n   k = 0; n   memset(ans, -1, sizeof(ans)); n   for (i = 1; i <= n; i++) { n     if (dp1[i] + dp2[i] - 1 < maxlen) n       ans[i] = 1; n     else n       s[k++] = dp1[i]; n   } n   sort(s, s + k); n   for (i = 1; i <= n; i++) { n     if (ans[i] == 1) n       printf( 1 ); n     else { n       if (upper_bound(s, s + k, dp1[i]) - lower_bound(s, s + k, dp1[i]) > 1) n         printf( 2 ); n       else n         printf( 3 ); n     } n   } n   puts(  ); n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, q, tot, rec[100010], cnt[100010][360], num[360], Limit, W, n     tmp[100010]; n long long delta[360], sum[360], a[100010]; n struct Set { n   int k, l, r; n } s[100010]; n long long AskBig(int now) { n   int pos = tmp[now]; n   long long ans = sum[pos]; n   for (int i = 1; i <= W; i++) ans += (long long)cnt[now][i] * delta[i]; n   return ans; n } n long long AskSmall(int now) { n   long long ans = 0; n   for (int i = s[now].l; i <= s[now].r; i++) ans += a[rec[i]]; n   for (int i = 1; i <= W; i++) ans += (long long)cnt[now][i] * delta[i]; n   return ans; n } n void ChangeSmall(int now, int v) { n   for (int i = s[now].l; i <= s[now].r; i++) a[rec[i]] += v; n   for (int i = 1; i <= W; i++) sum[i] += (long long)cnt[now][i] * v; n } n int main() { n   scanf( %d%d%d , &n, &m, &q); n   Limit = ceil(sqrt(n)); n   for (int i = 1; i <= n; i++) scanf( %I64d , &a[i]); n   for (int i = 1; i <= m; i++) { n     int k; n     scanf( %d , &k); n     s[i].k = k; n     s[i].l = tot + 1; n     for (int j = 1; j <= k; j++) { n       int x; n       scanf( %d , &x); n       rec[++tot] = x; n     } n     s[i].r = tot; n     if (k > Limit) num[++W] = i; n   } n   for (int i = 1; i <= W; i++) { n     int u = num[i]; n     for (int j = s[u].l; j <= s[u].r; j++) { n       ++tmp[rec[j]]; n       sum[i] += a[rec[j]]; n     } n     for (int j = 1; j <= m; j++) n       for (int k = s[j].l; k <= s[j].r; k++) cnt[j][i] += tmp[rec[k]]; n     for (int j = s[u].l; j <= s[u].r; j++) --tmp[rec[j]]; n   } n   for (int i = 1; i <= W; i++) tmp[num[i]] = i; n   for (int i = 1; i <= q; i++) { n     char c = getchar(); n     int x, v; n     while (c !=  ?  && c !=  + ) c = getchar(); n     if (c ==  ? ) { n       scanf( %d , &x); n       if (s[x].k > Limit) n         printf( %I64d n , AskBig(x)); n       else n         printf( %I64d n , AskSmall(x)); n     } else { n       scanf( %d%d , &x, &v); n       if (s[x].k > Limit) n         delta[tmp[x]] += v; n       else n         ChangeSmall(x, v); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 1e5 + 5; n long long n, a[maxn], dp[maxn][2], f[maxn][2], d[maxn]; n long long head[maxn], ecnt; n struct edge { n   long long to, next; n } e[maxn << 1]; n void adde(long long u, long long v) { n   e[++ecnt].to = v; n   e[ecnt].next = head[u]; n   head[u] = ecnt; n } n long long read() { n   long long res = 0, f = 1; n   char ch; n   do { n     ch = getchar(); n     if (ch ==  - ) f = -1; n   } while (!isdigit(ch)); n   do { n     res = res * 10 + ch -  0 ; n     ch = getchar(); n   } while (isdigit(ch)); n   return res * f; n } n void dfs1(long long u, long long fa) { n   dp[u][0] = dp[u][1] = f[u][0] = d[u] = a[u]; n   for (long long i = head[u]; i; i = e[i].next) { n     long long v = e[i].to; n     if (v == fa) continue; n     dfs1(v, u); n     dp[u][0] = max(dp[u][0], dp[v][0]); n     dp[u][0] = max(dp[u][0], dp[u][1] + dp[v][1]); n     dp[u][0] = max(dp[u][0], d[u] + f[v][0]); n     dp[u][0] = max(dp[u][0], d[v] + f[u][0]); n     dp[u][1] = max(dp[u][1], dp[v][1]); n     dp[u][1] = max(dp[u][1], d[u] + d[v]); n     f[u][0] = max(f[u][0], a[u] + f[v][0]); n     f[u][0] = max(f[u][0], d[u] + dp[v][1]); n     f[u][0] = max(f[u][0], a[u] + d[v] + f[u][1]); n     f[u][1] = max(f[u][1], dp[v][1]); n     d[u] = max(d[u], d[v] + a[u]); n   } n } n signed main() { n   n = read(); n   for (long long i = (1); i <= (n); ++i) a[i] = read(); n   for (long long i = (1); i <= (n - 1); ++i) { n     long long u = read(), v = read(); n     adde(u, v); n     adde(v, u); n   } n   dfs1(1, 0); n   printf( %lld n , dp[1][0]); n } n


#include<bits/stdc++.h> n #define FOR(i,a,b) for( int i=(a); i<(b); ++i) n #define REP(i,a,b) for( int i=(a); i<=(b); ++i) n #define SHOW(x) for(auto i:(x)){ cout << i <<    ;} cout << endl; n #define MAXN 25 n using namespace std; n  n typedef long long LL; n typedef pair<int,int> PII; n typedef vector<int> VI; n void solve(){ n     string s;cin >> s; n     bool f1 = false,f2 = false; n     for(int i = 1; i < s.size(); i++){ n         if(s[i] == s[i-1] && s[i] ==  1 ){ n             f1 = true; n         } n         if(f1 && s[i] == s[i-1] && s[i] ==  0 ){ n             f2 = true; n             break; n         } n     } n     if(f2) cout <<  NO  << endl; n     else cout <<  YES  << endl; n } n int main(){ n     ios::sync_with_stdio(false); n     cin.tie(0); n     int n; cin >> n; n     while(n--){ n         solve(); n     } n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n const int N = 2000000 + 77, Mod = 1000000007, L = 30; n int n, m, k, dp[N], T[L], P[N], Ps[N], SS[L]; n char S[N]; n int main() { n   scanf( %d %d , &n, &k); n   scanf( %s , S + 1); n   m = strlen(S + 1); n   dp[0] = Ps[0] = 1; n   for (int i = 1; i <= m; ++i) { n     P[i] = T[S[i] -  a ], T[S[i] -  a ] = i; n     dp[i] = (Ps[i - 1] - SS[S[i] -  a ] + Mod) % Mod; n     Ps[i] = (Ps[i - 1] + dp[i]) % Mod; n     SS[S[i] -  a ] = (SS[S[i] -  a ] + dp[i]) % Mod; n   } n   for (int i = m + 1; i <= n + m; ++i) { n     int Mn = 0; n     for (int j = 1; j < k; ++j) n       if (T[j] < T[Mn]) Mn = j; n     P[i] = T[Mn], T[Mn] = i; n     dp[i] = (Ps[i - 1] - SS[Mn] + Mod) % Mod; n     Ps[i] = (Ps[i - 1] + dp[i]) % Mod; n     SS[Mn] = (SS[Mn] + dp[i]) % Mod; n   } n   printf( %d n , Ps[n + m]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n unordered_map<string, int> mp; n char ans[2][1005]; n bool x[5005]; n int oprd[2][5005]; n char orig[5005][1005]; n char op[5005][20]; n char input[4][1005]; n char line[4 * 1005]; n int main() { n   int n, m; n   while (scanf( %d%d  , &n, &m) != EOF) { n     mp.clear(); n     for (int i = (1); i <= (n); i++) { n       fgets(line, 4 * 1005, stdin); n       int num = sscanf(line,  %s := %s %s %s , input[0], input[1], input[2], n                        input[3]); n       mp[string(input[0])] = i; n       if (num == 4) { n         if (input[1][0] ==  ? ) n           oprd[0][i] = -1; n         else n           oprd[0][i] = mp[string(input[1])]; n         strcpy(op[i], input[2]); n         if (input[3][0] ==  ? ) n           oprd[1][i] = -1; n         else n           oprd[1][i] = mp[string(input[3])]; n       } else n         op[i][0] =  # , strcpy(orig[i], input[1]); n     } n     for (int i = (0); i <= (m - 1); i++) { n       int val[2]; n       val[0] = val[1] = 0; n       for (int j = (0); j <= (1); j++) { n         for (int k = (1); k <= (n); k++) { n           if (op[k][0] ==  # ) n             x[k] = orig[k][i] -  0 ; n           else { n             int oprd1 = (oprd[0][k] == -1 ? j : x[oprd[0][k]]); n             int oprd2 = (oprd[1][k] == -1 ? j : x[oprd[1][k]]); n             if (op[k][0] ==  X ) n               x[k] = oprd1 ^ oprd2; n             else if (op[k][0] ==  O ) n               x[k] = oprd1 | oprd2; n             else n               x[k] = oprd1 & oprd2; n           } n           val[j] += x[k]; n         } n       } n       ans[0][i] = (val[0] <= val[1] ?  0  :  1 ); n       ans[1][i] = (val[0] < val[1] ?  1  :  0 ); n     } n     ans[0][m] = ans[1][m] =   0 ; n     printf( %s n , ans[0]); n     printf( %s n , ans[1]); n   } n   return 0; n } n


#include <bits/stdc++.h> n const int inf = 0x3f3f3f3f; n const int maxn = 500050; n const int mod = 1e9 + 7; n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   int n, a, b; n   cin >> n >> a >> b; n   int c = 0, ans = 0; n   for (int i = 1; i <= n; i++) { n     int x; n     cin >> x; n     if (x == 1) { n       if (a > 0) n         a--; n       else { n         if (b > 0) { n           b--; n           c++; n         } else if (c > 0) n           c--; n         else n           ans++; n       } n     } else { n       if (b > 0) n         b--; n       else n         ans += 2; n     } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 2e5 + 2; n string s; n long long n; n long long func(vector<long long> a) { n   stack<long long> st; n   long long max_ = 0; n   for (long long i = 0; i < n; i++) { n     while (!st.empty() && a[i] > st.top()) { n       long long cur = st.top(); n       st.pop(); n       max_ = max(cur ^ a[i], max_); n     } n     st.push(a[i]); n   } n   return max_; n } n void solve() { n   cin >> n; n   vector<long long> a(n); n   for (long long i = 0; i < n; i++) { n     cin >> a[i]; n   } n   long long ans = func(a); n   reverse(a.begin() + 0, a.end()); n   ans = max(ans, func(a)); n   cout << ans; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   solve(); n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   for (int i = 1; i <= 400; i++) s += to_string(i); n   int n; n   cin >> n; n   cout << s[n - 1]; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, b, g, ans = 0; n   cin >> b >> g >> n; n   if (n < b && n < g) n     ans = n + 1; n   else if (n < b && n > g) n     ans = g + 1; n   else if (n < g && n > b) n     ans = b + 1; n   else { n     ans = min(b, g) + 1 - (n - max(b, g)); n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline void checkMin(T &a, T b) { n   if (b < a) a = b; n } n template <typename T> n inline void checkMax(T &a, T b) { n   if (a < b) a = b; n } n const int P = 1e7 + 19; n const int Q = 1e9 + 7; n const int MAX_N = 5005 + 30000; n int n, k, w[MAX_N], v[MAX_N]; n long long dp[32][1001]; n int beg[MAX_N], qc[MAX_N]; n set<pair<int, int>, greater<pair<int, int>>> es[MAX_N]; n void dfs(int depth, int l, int r) { n   copy(dp[depth - 1], dp[depth], dp[depth]); n   for (auto iter = es[l].begin(); iter != es[l].end();) { n     if (iter->first < r) break; n     int i = iter->second; n     if (iter->first > r) { n       es[r + 1].emplace(iter->first, i); n     } n     iter = es[l].erase(iter); n     for (int j = k; j >= w[i]; --j) { n       checkMax(dp[depth][j], dp[depth][j - w[i]] + v[i]); n     } n   } n   if (l == r) { n     if (!qc[l]) return; n     int ans = 0; n     for (int i = k; i >= 1; --i) { n       ans = ((long long)ans * P + dp[depth][i]) % Q; n     } n     for (int i = 0; i < int(qc[l]); ++i) { n       printf( %d n , ans); n     } n   } else { n     int m = (l + r) >> 1; n     dfs(depth + 1, l, m); n     dfs(depth + 1, m + 1, r); n   } n } n int main() { n   while (~scanf( %d%d , &n, &k)) { n     fill(qc, qc + MAX_N, 0); n     fill(es, es + MAX_N, set<pair<int, int>, greater<pair<int, int>>>()); n     int ts = 1; n     for (int i = 1; i <= n; ++i) { n       scanf( %d%d , &v[i], &w[i]); n       beg[i] = ts; n     } n     int q, type, x; n     scanf( %d , &q); n     for (int i = 0; i < int(q); ++i) { n       scanf( %d , &type); n       if (type == 1) { n         ++ts; n         ++n; n         scanf( %d%d , &v[n], &w[n]); n         beg[n] = ts; n       } else if (type == 2) { n         scanf( %d , &x); n         es[beg[x]].emplace(ts++, x); n         beg[x] = -1; n       } else { n         ++qc[ts]; n       } n     } n     for (int i = 1; i <= n; ++i) { n       if (beg[i] == -1) continue; n       es[beg[i]].emplace(ts, i); n     } n     dfs(1, 1, ts); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n using namespace std; n inline long long getint() { n   long long _x = 0, _tmp = 1; n   char _tc = getchar(); n   while ((_tc <  0  || _tc >  9 ) && _tc !=  - ) _tc = getchar(); n   if (_tc ==  - ) _tc = getchar(), _tmp = -1; n   while (_tc >=  0  && _tc <=  9 ) _x *= 10, _x += (_tc -  0 ), _tc = getchar(); n   return _x * _tmp; n } n inline long long add(long long _x, long long _y, n                      long long _mod = 1000000007LL) { n   _x += _y; n   return _x >= _mod ? _x - _mod : _x; n } n inline long long sub(long long _x, long long _y, n                      long long _mod = 1000000007LL) { n   _x -= _y; n   return _x < 0 ? _x + _mod : _x; n } n inline long long mul(long long _x, long long _y, n                      long long _mod = 1000000007LL) { n   _x *= _y; n   return _x >= _mod ? _x % _mod : _x; n } n long long mypow(long long _a, long long _x, long long _mod) { n   if (_x == 0) return 1LL; n   long long _ret = mypow(mul(_a, _a, _mod), _x >> 1, _mod); n   if (_x & 1) _ret = mul(_ret, _a, _mod); n   return _ret; n } n long long mymul(long long _a, long long _x, long long _mod) { n   if (_x == 0) return 0LL; n   long long _ret = mymul(add(_a, _a, _mod), _x >> 1, _mod); n   if (_x & 1) _ret = add(_ret, _a, _mod); n   return _ret; n } n inline bool equal(double _x, double _y) { n   return _x > _y - 1e-9 && _x < _y + 1e-9; n } n int __ = 1, _cs; n struct Maxflow { n   static const int MAXV = 20010; n   struct Edge { n     int v, c, r; n     Edge(int _v, int _c, int _r) : v(_v), c(_c), r(_r) {} n   }; n   int s, t; n   vector<Edge> G[MAXV * 2]; n   int iter[MAXV * 2], d[MAXV * 2], gap[MAXV * 2], tot; n   void init(int x) { n     tot = x + 2; n     s = x + 1, t = x + 2; n     for (int i = 0; i <= tot; i++) { n       G[i].clear(); n       iter[i] = d[i] = gap[i] = 0; n     } n   } n   void addEdge(int u, int v, int c) { n     G[u].push_back(Edge(v, c, (int)(G[v]).size())); n     G[v].push_back(Edge(u, 0, (int)(G[u]).size() - 1)); n   } n   int dfs(int p, int flow) { n     if (p == t) return flow; n     for (int &i = iter[p]; i < (int)(G[p]).size(); i++) { n       Edge &e = G[p][i]; n       if (e.c > 0 && d[p] == d[e.v] + 1) { n         int f = dfs(e.v, min(flow, e.c)); n         if (f) { n           e.c -= f; n           G[e.v][e.r].c += f; n           return f; n         } n       } n     } n     if ((--gap[d[p]]) == 0) n       d[s] = tot; n     else { n       d[p]++; n       iter[p] = 0; n       ++gap[d[p]]; n     } n     return 0; n   } n   int solve() { n     int res = 0; n     gap[0] = tot; n     for (res = 0; d[s] < tot; res += dfs(s, 1000000LL)) n       ; n     return res; n   } n } flow; n void build() {} n int n, m, s, t; n int ui[10210], vi[10210], gi[10210]; n vector<int> g[10210], rg[10210]; n void init() { n   n = getint(); n   m = getint(); n   s = getint(); n   t = getint(); n   flow.init(n); n   for (int i = 0; i < m; i++) { n     ui[i] = getint(); n     vi[i] = getint(); n     gi[i] = getint(); n     g[ui[i]].push_back(i); n     rg[vi[i]].push_back(i); n     if (gi[i]) { n       flow.addEdge(ui[i], vi[i], 1); n       flow.addEdge(vi[i], ui[i], 1000000LL); n     } else n       flow.addEdge(ui[i], vi[i], 1000000LL); n   } n   flow.addEdge(flow.s, s, 1000000LL); n   flow.addEdge(t, flow.t, 1000000LL); n } n set<int> reachable; n bool tg[10210], sat[10210]; n int f[10210], c[10210]; n int sbk[10210], sbke[10210]; n int tbk[10210], tbke[10210]; n bool vst[10210], vst2[10210]; n int fucktag[10210], fuck; n bool go_to_s(int cur) { n   assert(vst[cur]); n   ++fuck; n   int st = cur; n   while (cur != s) { n     if (fucktag[sbke[cur]] == fuck) return false; n     fucktag[sbke[cur]] = fuck; n     f[sbke[cur]]++; n     cur = sbk[cur]; n     if (cur == st) return false; n   } n   return true; n } n bool go_to_t(int cur) { n   assert(vst2[cur]); n   ++fuck; n   int st = cur; n   while (cur != t) { n     if (fucktag[tbke[cur]] == fuck) return false; n     fucktag[tbke[cur]] = fuck; n     f[tbke[cur]]++; n     cur = tbk[cur]; n     if (cur == st) return false; n   } n   return true; n } n int ss[10210], stmp; n int who[10210], ee[10210]; n bool find_cycle(int now, int st, int dep = 0) { n   ss[now] = stmp; n   who[dep] = now; n   for (auto eid : g[now]) { n     if (gi[eid] == 0) continue; n     int nxt = vi[eid]; n     if (nxt == st) { n       sbke[st] = eid; n       sbk[st] = now; n       vst[st] = true; n       for (int i = 1; i <= dep; i++) { n         sbk[who[i]] = who[i - 1]; n         vst[who[i]] = true; n         sbke[who[i]] = ee[i - 1]; n       } n       return true; n     } n     if (ss[nxt] == stmp) continue; n     ee[dep] = eid; n     if (find_cycle(nxt, st, dep + 1)) return true; n   } n   return false; n } n bool find_cycle2(int now, int st, int dep = 0) { n   ss[now] = stmp; n   who[dep] = now; n   for (auto eid : rg[now]) { n     if (gi[eid] == 0) continue; n     int nxt = ui[eid]; n     if (nxt == st) { n       tbke[st] = eid; n       tbk[st] = now; n       vst2[st] = true; n       for (int i = 1; i <= dep; i++) { n         tbk[who[i]] = who[i - 1]; n         vst2[who[i]] = true; n         tbke[who[i]] = ee[i - 1]; n       } n       return true; n     } n     if (ss[nxt] == stmp) continue; n     ee[dep] = eid; n     if (find_cycle2(nxt, st, dep + 1)) return true; n   } n   return false; n } n void pre_build() { n   { n     vst[s] = true; n     queue<int> Q; n     Q.push(s); n     while (Q.size()) { n       int tn = Q.front(); n       Q.pop(); n       for (auto eid : g[tn]) { n         if (gi[eid] == 0) continue; n         if (vst[vi[eid]]) continue; n         vst[vi[eid]] = true; n         sbk[vi[eid]] = ui[eid]; n         sbke[vi[eid]] = eid; n         Q.push(vi[eid]); n       } n     } n     for (int i = 1; i <= n; i++) { n       if (!vst[i]) { n         ++stmp; n         if (find_cycle(i, i)) vst[i] = true; n       } n     } n   } n   { n     vst2[t] = true; n     queue<int> Q; n     Q.push(t); n     while (Q.size()) { n       int tn = Q.front(); n       Q.pop(); n       for (auto eid : rg[tn]) { n         if (gi[eid] == 0) continue; n         if (vst2[ui[eid]]) continue; n         vst2[ui[eid]] = true; n         tbk[ui[eid]] = vi[eid]; n         tbke[ui[eid]] = eid; n         Q.push(ui[eid]); n       } n     } n     for (int i = 1; i <= n; i++) n       if (!vst2[i]) { n         ++stmp; n         if (find_cycle2(i, i)) vst2[i] = true; n       } n   } n } n int ff[10210]; n void solve() { n   int ans = flow.solve(); n   queue<int> Q; n   reachable.insert(s); n   tg[s] = true; n   Q.push(s); n   while (Q.size()) { n     int tn = Q.front(); n     Q.pop(); n     for (auto e : flow.G[tn]) { n       if (e.c == 0) continue; n       if (1 <= e.v and e.v <= n and !tg[e.v]) { n         tg[e.v] = true; n         Q.push(e.v); n         reachable.insert(e.v); n       } n     } n   } n   int cc = 0; n   for (int i = 0; i < m; i++) n     if (reachable.find(ui[i]) != reachable.end() and n         reachable.find(vi[i]) == reachable.end()) { n       sat[i] = true; n       ++cc; n     } n   assert(cc == ans); n   pre_build(); n   for (int i = 0; i < m; i++) { n     if (f[i]) continue; n     if (gi[i] == 0) continue; n     bool x = go_to_s(ui[i]); n     bool y = go_to_t(vi[i]); n     if (x and y) f[i]++; n     if (f[i] == 0) f[i] = 1; n   } n   for (int i = 0; i < m; i++) { n     if (gi[i] == 0) n       c[i] = 1000000LL; n     else { n       c[i] = f[i]; n       if (!sat[i]) c[i]++; n     } n   } n   ans = 0; n   for (int i = 0; i < m; i++) n     if (f[i] == c[i]) ans++; n   printf( %d n , ans); n   for (int i = 0; i < m; i++) { n     printf( %d %d n , f[i], c[i]); n     ff[ui[i]] -= f[i]; n     ff[vi[i]] += f[i]; n   } n } n int main() { n   build(); n   while (__--) { n     init(); n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long dp[10000006]; n int main() { n   long long n, x, y; n   scanf( %lld%lld%lld , &n, &x, &y); n   for (int i = 0; i <= n; ++i) dp[i] = 1e18 + 100; n   dp[1] = x; n   for (int i = 2; i <= n; ++i) { n     if (i % 2 == 1) n       dp[i] = min(dp[i - 1] + x, min(dp[i / 2] + y + x, dp[i / 2 + 1] + y + x)); n     else n       dp[i] = min(dp[i - 1] + x, dp[i / 2] + y); n   } n   printf( %lld n , dp[n]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   while (t--) { n     long long n, m; n     cin >> n >> m; n     long long tot = n * (n + 1) / 2, zr = n - m, gr = m + 1; n     long long k = zr / gr, ans = 0; n     if (zr % gr == 0) { n       ans = tot - (k * (k + 1) / 2) * gr; n     } else { n       ans = tot - (zr % gr) * ((k + 1) * (k + 2) / 2) - n             (gr - (zr % gr)) * (k * (k + 1) / 2); n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long fp(long long n, long long p) { n   if (p == 0) return 1; n   if (p == 1) return n; n   long long res = fp(n, p / 2) % (long long)(1e9 + 7); n   res = ((res) * (res)) % (long long)(1e9 + 7); n   if (p % 2) res = (res * n) % (long long)(1e9 + 7); n   return res; n } n int n, m, x[100005], a[100005], b[100005], cnt, suspects = 0; n int ans[100005]; n char c[100005]; n int main() { n   ios::sync_with_stdio(0); n   ios_base::sync_with_stdio(0); n   cin.tie(0), cout.tie(0); n   ; n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     cin >> c[i] >> x[i]; n     if (c[i] ==  + ) { n       a[x[i]]++; n     } else { n       b[x[i]]++; n       cnt++; n     } n   } n   for (int i = 1; i <= n; i++) { n     if (a[i] - b[i] + cnt == m) ans[i] = 1, suspects++; n   } n   for (int i = 1; i <= n; i++) { n     if (suspects > 1 && ans[x[i - 1]] == 1) n       cout <<  Not defined  <<   n ; n     else if (c[i - 1] ==  +  && ans[x[i - 1]] == 1) n       cout <<  Truth  <<   n ; n     else if (c[i - 1] ==  -  && ans[x[i - 1]] == 0) n       cout <<  Truth  <<   n ; n     else n       cout <<  Lie  <<   n ; n   } n   return 0; n } n


#include<bits/stdc++.h> n using namespace std; n #define int long long int n #define fio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL) n #define pii pair<int,int> n #define F first n #define S second n #define pb push_back n const int mod1=998244353,mod2=(int) 1e9+7; n int power(int n,int k,int mod){int ans=1; n=n%mod;while(k>0){if(k&1)ans=(ans*n)%mod;n=(n*n)%mod;k>>=1;}return (ans+mod)%mod;}//negative nos & 0^0 n struct triplet{int a,b,c;};bool operator < (const triplet &t1,const triplet &t2){if(t1.a<t2.a)return true;if(t1.a==t2.a && t1.b <t2.b)return true;if(t1.a==t2.a && t1.b ==t2.b && t1.c<t2.c)return true;return false;} n // memset(dp, -1, sizeof dp); n  n  n void problemSolver() n { n  n  int h,w; n  cin>>h>>w; n  string s[h]; n  for(int i=0;i<h;i++) n   cin>>s[i]; n  int i=0,j=0,c=0; n  if(s[0][0]== * ) n    c++; n    n  while(i<=h-1 && j<=w-1) n  { n  n   if(j+1<w && s[i][j+1]== * ) n    j++; n   else if(i+1<h && s[i+1][j]== * ) n    i++; n   else if(j+1<w) n    j++; n   else  n    i++; n  n   if(s[i][j]== * ) n    c++; n   if(i==h-1 && j==w-1) n    break; n  n  } n  cout<<c; n }  n  n  n  n int32_t main() n { n  n //  fio; n  #ifndef ONLINE_JUDGE n  freopen( input.txt ,  r , stdin); n  freopen( output.txt ,  w , stdout); n  #endif n  int testcases=1; n     // cin>>testcases; n     while(testcases--) n     problemSolver();  n  n }


#include <bits/stdc++.h> n using namespace std; n struct qnode { n   int id; n   long long val; n   bool operator<(qnode b) const { return val > b.val; } n }; n queue<int> qu[200100]; n int w[200100], vis[200100], add[200100]; n int n, m, Q, opt, x, y, z; n long long dis[200100]; n priority_queue<qnode> q; n vector<pair<int, int> > edge[200100]; n void Dijkstra() { n   memset(dis, 0x3f, sizeof(dis)); n   dis[1] = 0; n   memset(vis, false, sizeof(vis)); n   q.push((qnode){1, 0}); n   while (!q.empty()) { n     int x = q.top().id; n     q.pop(); n     if (vis[x]) continue; n     vis[x] = true; n     for (auto i : edge[x]) { n       int upup = i.first, z = w[i.second]; n       dis[upup] = min(dis[upup], dis[x] + z); n       if (!vis[upup]) q.push((qnode){upup, dis[upup]}); n     } n   } n } n int main() { n   scanf( %d%d%d , &n, &m, &Q); n   for (int i = 1; i <= m; i++) { n     scanf( %d%d%lld , &x, &y, &w[i]); n     edge[x].push_back(make_pair(y, i)); n   } n   Dijkstra(); n   while (Q--) { n     scanf( %d , &opt); n     if (opt == 1) { n       scanf( %d , &x); n       if (dis[x] == 0x3f3f3f3f3f3f3f3f) n         puts( -1 ); n       else n         printf( %lld n , dis[x]); n     } else { n       scanf( %d , &x); n       for (int i = 1; i <= x; i++) { n         scanf( %d , &y); n         w[y]++; n       } n       for (int i = 1; i <= n; i++) add[i] = x + 1; n       qu[add[1] = 0].push(1); n       for (int i = 0; i <= x; i++) n         for (; !qu[i].empty(); qu[i].pop()) { n           int now = qu[i].front(); n           if (add[now] != i) continue; n           for (auto j : edge[now]) { n             int y = j.first, z = w[j.second]; n             long long d = dis[now] + z + add[now] - dis[y]; n             if (d < add[y]) qu[add[y] = d].push(y); n           } n         } n       for (int i = 1; i <= n; i++) n         if (add[i] != x + 1) dis[i] += add[i]; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2019, mod = 998244353; n int n, m, ans, f[N << 1][N], g[N << 1][N]; n int qpow(int a, int b) { n   int ret = 1; n   while (b) { n     if (b & 1) ret = 1ll * ret * a % mod; n     a = 1ll * a * a % mod, b >>= 1; n   } n   return ret; n } n int main() { n   scanf( %d%d%d , &n, &m, &ans); n   f[0][0] = 1; n   for (int i = 1; i <= 2 * n; i++) n     for (int j = 0; j <= min(i, n); j++) { n       if (!j) n         f[i][j] = f[i - 1][j + 1], g[i][j] = g[i - 1][j + 1]; n       else n         f[i][j] = (f[i - 1][j - 1] + 1ll * (j + 1) * f[i - 1][j + 1]) % mod, n         g[i][j] = (g[i - 1][j - 1] + 1ll * (j + 1) * g[i - 1][j + 1]) % mod; n       if (j >= m) g[i][j] = (f[i][j] + g[i][j]) % mod; n     } n   ans = 1ll * ans * qpow(1ll * f[2 * n][0] * (2 * n + 1) % mod, mod - 2) % mod * n         g[2 * n][0] % mod; n   printf( %d , ans); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 4040; n int dp[N][N]; n int n, m; n int main() { n   while (scanf( %d%d , &n, &m) != EOF) { n     int i, j; n     memset(dp, 63, sizeof(dp)); n     memset(dp[0], 0, sizeof(dp[0])); n     int ret = 0; n     for (i = 1; i <= n; i++) { n       int x, y; n       scanf( %d%d , &x, &y); n       for (j = 0; j <= m; j++) { n         int w = dp[i - 1][j]; n         dp[i][j] = min(dp[i][j], max(w, x - 1) + y); n         dp[i][j + 1] = min(dp[i][j + 1], w); n         ret = max(ret, x - 1 - w); n       } n     } n     for (i = 0; i <= m; i++) ret = max(ret, 86400 - dp[n][i]); n     printf( %d n , ret); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long LINF = 0x3f3f3f3f3f3f3f3f; n const long long NLINF = 0xf7f7f7f7f7f7f7f7; n const int INF = 0x3f3f3f3f, NINF = 0xf7f7f7f7; n const int MOD1 = 1e9 + 7, MOD2 = 1e9 + 9; n const int N = 2e5 + 10; n int n, freq[30], mark[30]; n vector<char> odd, ch; n char s[N], r[N]; n int main() { n   scanf( %s , &s), n = strlen(s); n   for (int i = 0; i < n; i++) { n     freq[s[i] -  a ]++; n   } n   for (int i = 0; i < n; i++) { n     if (freq[s[i] -  a ] % 2 == 0) ch.push_back(s[i]); n     if (freq[s[i] -  a ] % 2 && !mark[s[i] -  a ]) n       odd.push_back(s[i]), mark[s[i] -  a ] = 1; n   } n   sort(odd.begin(), odd.end()); n   for (int i = 0; i < odd.size(); i++) { n     if (odd.size() % 2 && i == odd.size() / 2) n       while (freq[odd[i] -  a ]--) ch.push_back(odd[i]); n     else if (i >= odd.size() / 2) { n       freq[odd[i] -  a ]--; n       while (freq[odd[i] -  a ]--) ch.push_back(odd[i]); n     } else { n       freq[odd[i] -  a ]++; n       while (freq[odd[i] -  a ]--) ch.push_back(odd[i]); n     } n   } n   sort(ch.begin(), ch.end()); n   assert(ch.size() == n); n   bool par = 1; n   for (int i = 0, lo = 0, hi = n - 1; i < n; i++) { n     if (i % 2 == par) n       r[hi--] = ch[i]; n     else if (ch[i] == ch[i + 1]) n       r[lo++] = ch[i]; n     else n       par = !par, r[n / 2] = ch[i]; n   } n   printf( %s n , r); n } n


#include <bits/stdc++.h> n #pragma GCC target( pclmul ) n using ul = std::uint32_t; n using ull = std::uint64_t; n using li = std::int32_t; n using ll = std::int64_t; n using llf = long double; n using uss = std::uint8_t; n const ul tau = 15; n const ul fib[] = { n     1,       1,       2,       3,        5,        8,        13,      21, n     34,      55,      89,      144,      233,      377,      610,     987, n     1597,    2584,    4181,    6765,     10946,    17711,    28657,   46368, n     75025,   121393,  196418,  317811,   514229,   832040,   1346269, 2178309, n     3524578, 5702887, 9227465, 14930352, 24157817, 39088169, 63245986}; n const ul sz = fib[tau]; n char f[tau + 1][sz + 1]; n ul ord[sz + sz]; n class sam_t { n  public: n   ul las; n   class node { n    public: n     ul ch[2]; n     ul len = 0; n     ul fa = 0; n     ul cnt = 0; n     node() { std::memset(ch, 0, sizeof(ch)); } n   }; n   std::vector<node> st = std::vector<node>(sz + sz); n   void add(ul c) { n     ul p = las; n     ul np = las = st.size(); n     st.push_back(node()); n     st[np].len = st[p].len + 1; n     for (; p && !st[p].ch[c]; p = st[p].fa) { n       st[p].ch[c] = np; n     } n     if (!p) { n       st[np].fa = 1; n     } else { n       ul q = st[p].ch[c]; n       if (st[q].len == st[p].len + 1) { n         st[np].fa = q; n       } else { n         ul nq = st.size(); n         st.push_back(node()); n         st[nq] = st[q]; n         st[nq].cnt = 0; n         st[nq].len = st[p].len + 1; n         st[q].fa = st[np].fa = nq; n         for (; p && st[p].ch[c] == q; p = st[p].fa) { n           st[p].ch[c] = nq; n         } n       } n     } n   } n   void init() { n     las = 1; n     st.resize(0); n     st.resize(2); n   } n }; n sam_t sams[tau + 1]; n ul n, m; n ull k; n const ul maxn = 200; n ull lambda[maxn + 1][4]; n const ul maxm = 200; n ul tim = 0; n char ans[maxm + 2]; n ul currs[4]; n ul nexts0[4]; n ul nexts1[4]; n ull getnum(ul st[4]) { n   ull ret = 0; n   for (ul i = 0; i != 4; ++i) { n     ret += lambda[n][i] * sams[tau - i].st[st[i]].cnt; n   } n   return ret; n } n int main() { n   f[0][1] =  0 ; n   f[1][1] =  1 ; n   std::scanf( %u%llu%u , &n, &k, &m); n   for (ul x = 2; x <= std::min(n, tau); ++x) { n     std::memcpy(f[x] + 1, f[x - 2] + 1, fib[x - 2]); n     std::memcpy(f[x] + fib[x - 2] + 1, f[x - 1] + 1, fib[x - 1]); n   } n   if (n <= tau) { n     sams[n].init(); n     for (ul i = 1; i <= fib[n]; ++i) { n       sams[n].add(f[n][i] -  0 ); n       ++sams[n].st[sams[n].las].cnt; n     } n     for (ul i = 1; i != sams[n].st.size(); ++i) { n       ord[i] = i; n     } n     std::sort(ord + 1, ord + sams[n].st.size(), [&](ul a, ul b) { n       return sams[n].st[a].len < sams[n].st[b].len; n     }); n     for (ul it = sams[n].st.size() - 1; it >= 2; --it) { n       ul i = ord[it]; n       sams[n].st[sams[n].st[i].fa].cnt += sams[n].st[i].cnt; n     } n     ul curr = 1; n     ull cc = sams[n].st[1].cnt; n     while (tim < m) { n       ul next0 = sams[n].st[curr].ch[0]; n       ul next1 = sams[n].st[curr].ch[1]; n       ull cn0 = sams[n].st[next0].cnt; n       ull cn1 = sams[n].st[next1].cnt; n       if (cc - cn0 - cn1 >= k) { n         break; n       } else { n         k -= cc - cn0 - cn1; n       } n       if (cn0 >= k) { n         ++tim; n         ans[tim] =  0 ; n         curr = next0; n         cc = cn0; n       } else { n         ++tim; n         ans[tim] =  1 ; n         curr = next1; n         k -= cn0; n         cc = cn1; n       } n     } n     std::printf(ans + 1); n     std::putchar(  n ); n   } else { n     for (ul x = tau - 3; x <= tau; ++x) { n       sams[x].init(); n       for (ul i = 1; i <= fib[x]; ++i) { n         sams[x].add(f[x][i] -  0 ); n         ++sams[x].st[sams[x].las].cnt; n       } n       for (ul i = 1; i != sams[x].st.size(); ++i) { n         ord[i] = i; n       } n       std::sort(ord + 1, ord + sams[x].st.size(), [&](ul a, ul b) { n         return sams[x].st[a].len < sams[x].st[b].len; n       }); n       for (ul it = sams[x].st.size() - 1; it >= 2; --it) { n         ul i = ord[it]; n         sams[x].st[sams[x].st[i].fa].cnt += sams[x].st[i].cnt; n       } n       lambda[x][tau - x] = 1; n     } n     for (ul x = tau + 1; x <= n; ++x) { n       for (ul i = 0; i != 4; ++i) { n         lambda[x][i] = lambda[x - 1][i] + lambda[x - 2][i] + lambda[x - 2][i] - n                        lambda[x - 3][i] - lambda[x - 4][i]; n       } n     } n     for (ul i = 0; i != 4; ++i) { n       currs[i] = 1; n     } n     ull cc = getnum(currs); n     while (tim < m) { n       for (ul i = 0; i != 4; ++i) { n         nexts0[i] = sams[tau - i].st[currs[i]].ch[0]; n         nexts1[i] = sams[tau - i].st[currs[i]].ch[1]; n       } n       ull cn0 = getnum(nexts0); n       ull cn1 = getnum(nexts1); n       if (cc - cn0 - cn1 >= k) { n         break; n       } else { n         k -= cc - cn0 - cn1; n       } n       if (cn0 >= k) { n         ++tim; n         ans[tim] =  0 ; n         std::memcpy(currs, nexts0, sizeof(currs)); n         cc = cn0; n       } else { n         ++tim; n         ans[tim] =  1 ; n         std::memcpy(currs, nexts1, sizeof(currs)); n         k -= cn0; n         cc = cn1; n       } n     } n     std::printf(ans + 1); n     std::putchar(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n const int N = 1e5 + 1; n const int INF = 1e9 + 9; n int n; n int d[N]; n std::vector<int> a[N]; n int dfs(int v) { n   for (int u : a[v]) { n     d[v] += dfs(u); n   } n   if (d[v] == 0) return d[v] = 1; n   return d[v]; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i < n; i++) { n     int x; n     scanf( %d , &x); n     x--; n     a[x].push_back(i); n   } n   dfs(0); n   std::sort(d, d + n); n   for (int i = 0; i < n; i++) printf( %d  , d[i]); n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long x, long long y) { n   if (x > y) swap(x, y); n   if (x <= 0) return y; n   if (y % x == 0) return x; n   return gcd(y % x, x); n } n int main() { n   vector<long long> a; n   long long b[400000]; n   bool c[400000]; n   for (long long i = 0; i < (long long)(400000); i++) { n     b[i] = 0; n     c[i] = false; n   } n   int n, k; n   long long g; n   long long x, y, z, xx, yy, zz; n   long long s, ans; n   bool v = true; n   ans = 0; n   scanf( %I64d %I64d %I64d , &z, &x, &y); n   g = gcd(x, y); n   a.push_back(0); n   c[0] = true; n   xx = 0; n   while (true) { n     if (xx >= y) n       xx -= y; n     else n       xx += x; n     if (c[xx]) n       break; n     else n       c[xx] = true; n     a.push_back(xx); n   } n   xx = 0; n   yy = 0; n   zz = 0; n   while (true) { n     if (xx < a[yy]) { n       b[xx] = yy; n       zz = max(zz, yy); n       xx++; n     } else n       yy++; n     if (yy >= a.size()) break; n   } n   ans = 0; n   if (z < (x + y - g)) { n     for (long long i = 0; i < (long long)(z + 1); i++) ans += b[i]; n     printf( %I64d n , ans); n     return 0; n   } n   for (long long i = 0; i < (long long)(x + y - g); i++) { n     ans += b[i]; n   } n   xx = (x + y) / g; n   yy = z / g; n   zz = (xx + yy) * (yy - xx + 1) / 2; n   zz *= g; n   ans += zz; n   zz = (z % g) + 1; n   zz *= ((z / g) + 1); n   ans += zz; n   printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1e9; n struct outgoing { n   long long wt = INF, to = -1; n }; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   vector<long long> stations; n   vector<pair<long long, long long>> connections; n   long long n, i, j, k, t, temp; n   cin >> n; n   vector<outgoing> edge(n + 1); n   pair<long long, long long> vertices[n + 1]; n   long long cost[n + 1]; n   long long mult[n + 1]; n   for (i = 1; i <= n; i++) { n     cin >> vertices[i].first >> vertices[i].second; n   } n   for (i = 1; i <= n; i++) { n     cin >> cost[i]; n     edge[i].wt = cost[i]; n     edge[i].to = i; n   } n   for (i = 1; i <= n; i++) { n     cin >> mult[i]; n   } n   long long total = 0; n   vector<bool> selected(n + 1, false); n   for (i = 1; i <= n; i++) { n     long long v = -1; n     for (j = 1; j <= n; j++) { n       if (!selected[j] && (v == -1 || edge[j].wt < edge[v].wt)) { n         v = j; n       } n     } n     selected[v] = true; n     long long x_now = vertices[v].first; n     long long y_now = vertices[v].second; n     if (edge[v].to == v) { n       stations.push_back(v); n     } else { n       connections.push_back({v, edge[v].to}); n     } n     total += edge[v].wt; n     for (j = 1; j <= n; j++) { n       long long x_diff = fabs(vertices[j].first - vertices[v].first); n       long long y_diff = fabs(vertices[j].second - vertices[v].second); n       if ((mult[v] + mult[j]) * (x_diff + y_diff) < edge[j].wt) { n         edge[j].wt = (mult[v] + mult[j]) * (x_diff + y_diff); n         edge[j].to = v; n       } n     } n   } n   cout << total << endl; n   cout << stations.size() << endl; n   for (auto x : stations) { n     cout << x <<    ; n   } n   cout <<   n ; n   cout << connections.size() << endl; n   for (auto x : connections) { n     cout << x.first <<     << x.second << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<char> v; n vector<char> vv; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   char s[3][3], ss[3][3]; n   for (int i = 0; i < 2; i++) n     for (int j = 0; j < 2; j++) cin >> s[i][j]; n   for (int i = 0; i < 2; i++) n     for (int j = 0; j < 2; j++) cin >> ss[i][j]; n   if (s[0][0] !=  X ) v.push_back(s[0][0]); n   if (s[0][1] !=  X ) v.push_back(s[0][1]); n   if (s[1][1] !=  X ) v.push_back(s[1][1]); n   if (s[1][0] !=  X ) v.push_back(s[1][0]); n   if (v[0] == ss[0][0]) { n     if (ss[0][0] !=  X ) vv.push_back(ss[0][0]); n     if (ss[0][1] !=  X ) vv.push_back(ss[0][1]); n     if (ss[1][1] !=  X ) vv.push_back(ss[1][1]); n     if (ss[1][0] !=  X ) vv.push_back(ss[1][0]); n   } else if (ss[0][1] == v[0]) { n     if (ss[0][1] !=  X ) vv.push_back(ss[0][1]); n     if (ss[1][1] !=  X ) vv.push_back(ss[1][1]); n     if (ss[1][0] !=  X ) vv.push_back(ss[1][0]); n     if (ss[0][0] !=  X ) vv.push_back(ss[0][0]); n   } else if (v[0] == ss[1][1]) { n     vv.push_back(ss[1][1]); n     if (ss[1][0] !=  X ) vv.push_back(ss[1][0]); n     if (ss[0][0] !=  X ) vv.push_back(ss[0][0]); n     if (ss[0][1] !=  X ) vv.push_back(ss[0][1]); n   } else if (v[0] == ss[1][0]) { n     if (ss[1][0] !=  X ) vv.push_back(ss[1][0]); n     if (ss[0][0] !=  X ) vv.push_back(ss[0][0]); n     if (ss[0][1] !=  X ) vv.push_back(ss[0][1]); n     if (ss[1][1] !=  X ) vv.push_back(ss[1][1]); n   } n   for (int i = 0; i < 3; i++) n     if (v[i] != vv[i]) { n       cout <<  NO  << endl; n       return 0; n     } n   cout <<  YES  << endl; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const long long N = 1e6 + 10; n const long long mod = 1e9 + 7; n const long long INF = 1e18 + 10; n int main() { n   long long n, m, i; n   string s; n   cin >> n; n   while (n--) { n     cin >> m >> s; n     if (s.size() < 11) n       cout <<  NO n ; n     else { n       for (i = 0; i < s.size(); ++i) { n         if (s[i] ==  8 ) break; n       } n       if (i == s.size()) n         cout <<  NO n ; n       else { n         if (s.size() - i >= 11) n           cout <<  YES n ; n         else n           cout <<  NO n ; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5, mod = 1000000007, INF = 1e9 + 7; n const int block = 316; n int n, k, type[N], a[N], m, cnt[N * 3], l[N][5]; n long long ans, res[N], p[N][5]; n set<long long> s; n map<long long, int> conv; n struct query { n   int l, r, id, num; n   query(){}; n   query(int a, int b, int c) : l(a), r(b), id(a / block), num(c){}; n } q[N]; n bool cmp(query q1, query q2) { n   if (q1.id != q2.id) return q1.id < q2.id; n   return q1.r < q2.r; n } n void add(int v, int cond) { n   ans += cnt[l[v][cond]]; n   cnt[l[v][1]]++; n } n void remove(int v, int cond) { n   cnt[l[v][1]]--; n   ans -= cnt[l[v][cond]]; n } n int main() { n   cin >> n >> k; n   for (int i = 1; i <= n; i++) { n     cin >> type[i]; n   } n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   for (int i = 0; i <= n; i++) { n     if (type[i] == 2) a[i] *= -1; n     p[i][1] = p[i - 1][1] + a[i]; n     p[i][2] = p[i][1] - k; n     p[i][3] = p[i][1] + k; n     s.insert(p[i][1]); n     s.insert(p[i][2]); n     s.insert(p[i][3]); n   } n   int cnt = 0; n   for (long long e : s) { n     conv[e] = ++cnt; n   } n   for (int i = 0; i <= n; i++) { n     l[i][1] = conv[p[i][1]]; n     l[i][2] = conv[p[i][2]]; n     l[i][3] = conv[p[i][3]]; n   } n   cin >> m; n   for (int i = 1; i <= m; i++) { n     int l, r; n     cin >> l >> r; n     l--; n     q[i] = query(l, r, i); n   } n   sort(q + 1, q + m + 1, &cmp); n   int l = 0, r = -1; n   for (int i = 1; i <= m; i++) { n     int L = q[i].l, R = q[i].r; n     while (r < R) add(++r, 2); n     while (l < L) remove(l++, 3); n     while (R < r) remove(r--, 2); n     while (L < l) add(--l, 3); n     res[q[i].num] = ans; n   } n   for (int i = 1; i <= m; i++) { n     cout << res[i] << endl; n   } n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 3e5 + 5; n const int MAXQ = 3e5 + 5; n const int inf = 0x3f3f3f3f; n struct Query { n   int id, l, r; n   Query(void) {} n   Query(int _id, int _l, int _r) : id(_id), l(_l), r(_r) {} n }; n long long res[MAXQ]; n struct Solver { n   vector<int> a, b; n   vector<Query> qs; n   void solve(void) { n     int n = (int)a.size(); n     a.push_back(-inf); n     b.push_back(-inf); n     vector<vector<Query> > f(n), g(n); n     for (Query x : qs) x.r = min(x.r, n - 1), f[x.l].push_back(x); n     vector<int> sta(1, n), val(1, -inf); n     for (int i = n - 1; i >= 0; --i) { n       int pos = lower_bound(val.begin(), val.end(), b[i]) - val.begin(); n       pos = sta[pos - 1]; n       for (Query x : f[i]) { n         int id = x.id, l = x.l, r = x.r; n         if (pos > r) { n           res[id] = (long long)(r - l + 1) * b[i]; n           continue; n         } n         res[id] = (long long)(pos - l) * b[i]; n         x.l = pos; n         g[pos].push_back(x); n       } n       while (val.back() >= a[i]) val.pop_back(), sta.pop_back(); n       sta.push_back(i); n       val.push_back(a[i]); n     } n     sta = vector<int>(1, n); n     val = vector<int>(1, -inf); n     vector<long long> sum(1, 0); n     for (int i = n - 1; i >= 0; --i) { n       while (val.back() >= a[i]) { n         val.pop_back(); n         sta.pop_back(); n         sum.pop_back(); n       } n       sum.push_back((long long)a[i] * (sta.back() - i) + sum.back()); n       sta.push_back(i); n       val.push_back(a[i]); n       for (Query x : g[i]) { n         int id = x.id, r = x.r; n         int pos = lower_bound(sta.begin(), sta.end(), r, greater<int>()) - n                   sta.begin(); n         res[id] += sum.back() - sum[pos]; n         res[id] += (long long)(x.r - sta[pos] + 1) * val[pos]; n       } n     } n   } n } slv[MAXN]; n int a[MAXN], f[MAXN]; n int main(void) { n   int n, Q, d; n   scanf( %d%d%d , &n, &Q, &d); n   for (int i = 0; i < n; ++i) scanf( %d , &a[i]); n   deque<int> q; n   for (int i = 0; i < n; ++i) { n     while (q.size() && q.front() <= i - d) q.pop_front(); n     while (q.size() && a[q.back()] >= a[i]) q.pop_back(); n     q.push_back(i); n     f[i] = a[q.front()]; n   } n   for (int i = 0; i < n; ++i) { n     int t = i % d; n     slv[t].a.push_back(f[i]); n     slv[t].b.push_back(a[i]); n   } n   for (int i = 1; i <= Q; ++i) { n     int l, r; n     scanf( %d%d , &l, &r); n     --l; n     --r; n     int t = l % d; n     if (r % d < l % d) r -= d; n     l /= d; n     r /= d; n     slv[t].qs.emplace_back(i, l, r); n   } n   for (int i = 0; i < d; ++i) slv[i].solve(); n   for (int i = 1; i <= Q; ++i) printf( %lld n , res[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long A[300010], B[300010]; n int n, m; n struct node { n   int negpos, negpos_pref, negpos_suf, neg, neg_pref, neg_suf, pos, pos_pref, n       pos_suf, len; n   node() { n     negpos = negpos_pref = negpos_suf = neg = neg_pref = neg_suf = pos = n         pos_pref = pos_suf = 0; n   } n }; n node ST[3000010]; n void merge(node a, node b, node &x) { n   x.negpos = x.negpos_pref = x.negpos_suf = -0x3f3f3f3f; n   x.neg = x.neg_pref = x.neg_suf = -0x3f3f3f3f; n   x.pos = x.pos_pref = x.pos_suf = -0x3f3f3f3f; n   x.negpos = max(a.negpos, max(b.negpos, a.neg_suf + b.pos_pref)); n   x.negpos = n       max(x.negpos, max(a.negpos_suf + b.pos_pref, a.neg_suf + b.negpos_pref)); n   x.negpos_pref = a.negpos_pref; n   if (a.neg == a.len) x.negpos_pref = max(x.negpos_pref, a.neg + b.negpos_pref); n   if (a.negpos == a.len) n     x.negpos_pref = max(x.negpos_pref, a.negpos + b.pos_pref); n   x.negpos_suf = b.negpos_suf; n   if (b.pos == b.len) x.negpos_suf = max(x.negpos_suf, a.negpos_suf + b.pos); n   if (b.negpos == b.len) x.negpos_suf = max(x.negpos_suf, a.neg_suf + b.negpos); n   x.pos = a.pos + b.pos; n   x.neg = a.neg + b.neg; n   x.pos_pref = a.pos_pref; n   if (a.pos_pref == a.len) x.pos_pref = a.len + b.pos_pref; n   x.pos_suf = b.pos_suf; n   if (b.pos_suf == b.len) x.pos_suf = a.pos_suf + b.len; n   x.neg_pref = a.neg_pref; n   if (a.neg_pref == a.len) x.neg_pref = a.len + b.neg_pref; n   x.neg_suf = b.neg_suf; n   if (b.neg_suf == b.len) x.neg_suf = a.neg_suf + b.len; n } n void init_node(node &x, long long v) { n   x.negpos = x.negpos_pref = x.negpos_suf = 0; n   x.pos = x.pos_pref = x.pos_suf = 0; n   x.neg = x.neg_pref = x.neg_suf = 0; n   if (v < 0) { n     x.neg = x.neg_pref = x.neg_suf = 1; n   } else if (v > 0) { n     x.pos = x.pos_pref = x.pos_suf = 1; n   } n   if (v) { n     x.negpos = x.negpos_pref = x.negpos_suf = 1; n   } n } n void init(int N, int li, int ls) { n   ST[N].len = (ls - li + 1); n   if (li == ls) { n     init_node(ST[N], B[li]); n   } else { n     int mid = (li + ls) / 2; n     init(2 * N, li, mid); n     init(2 * N + 1, mid + 1, ls); n     merge(ST[2 * N], ST[2 * N + 1], ST[N]); n   } n } n void upd(int N, int li, int ls, int p, long long v) { n   if (p > ls || p < li) return; n   if (li == p && ls == p) { n     B[p] += v; n     init_node(ST[N], B[p]); n     return; n   } n   int mid = (li + ls) / 2; n   upd(2 * N, li, mid, p, v); n   upd(2 * N + 1, mid + 1, ls, p, v); n   merge(ST[2 * N], ST[2 * N + 1], ST[N]); n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %lld , &A[i]); n   scanf( %d , &m); n   for (int i = 0; i < n - 1; i++) B[i] = A[i] - A[i + 1]; n   n--; n   if (n == 0) { n     while (m--) printf( 1 n ); n     return 0; n   } n   init(1, 0, n - 1); n   for (int i = 0; i < m; i++) { n     int l, r; n     long long v; n     scanf( %d %d %lld , &l, &r, &v); n     l -= 2; n     r--; n     if (l >= 0 && l < n) upd(1, 0, n - 1, l, -v); n     if (r >= 0 && r < n) upd(1, 0, n - 1, r, v); n     printf( %d n , ST[1].negpos + 1); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[110], b[110], cnt, n; n map<int, int> mp; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     int num; n     scanf( %d , &num); n     if (!mp[num]) { n       mp[num]++; n       b[cnt++] = num; n     } n   } n   sort(b, b + cnt); n   if (cnt == 1) n     printf( 0 ); n   else if (cnt == 2) { n     if (abs(b[1] - b[0]) % 2) n       printf( %d , abs(b[1] - b[0])); n     else n       printf( %d , abs(b[1] - b[0]) / 2); n   } else if (cnt == 3 && ((b[1] - b[0]) == (b[2] - b[1]))) n     printf( %d , b[1] - b[0]); n   else n     printf( -1 ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   string s; n   cin >> n >> s; n   int ans = 15; n   for (int i = 0; i < n; i++) { n     if (s[i] ==  0 ) ans &= 8; n     if (s[i] ==  1  || s[i] ==  2  || s[i] ==  3 ) ans &= 7; n     if (s[i] ==  1  || s[i] ==  4  || s[i] ==  7 ) ans &= 13; n     if (s[i] ==  3  || s[i] ==  6  || s[i] ==  9 ) ans &= 14; n     if (s[i] ==  7  || s[i] ==  9 ) ans &= 11; n   } n   if (ans) n     cout <<  NO ; n   else n     cout <<  YES ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n set<string> normal; n set<string> grumpy; n void build_normal() { n   normal.insert( great! ); n   normal.insert( don t think so ); n   normal.insert( don t touch me ); n   normal.insert( not bad ); n   normal.insert( cool ); n } n void build_grumpy() { n   grumpy.insert( no way ); n   grumpy.insert( don t even ); n   grumpy.insert( are you serious? ); n   grumpy.insert( go die in a hole ); n   grumpy.insert( worse ); n   grumpy.insert( terrible ); n } n int main() { n   build_normal(); n   build_grumpy(); n   for (int i = 0; i < 10; i++) { n     cout << i << endl; n     string s; n     getline(cin, s); n     if (normal.count(s)) { n       cout <<  normal  << endl; n       return 0; n     } else if (grumpy.count(s)) { n       cout <<  grumpy  << endl; n       return 0; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const int maxn = 100010; n int dp[550][550], A[550], B[550], path[550][550], n, m; n bool print(int x) { n   if (!x) return false; n   if (print(path[n][x])) n     printf(  %d , B[x]); n   else n     printf( %d , B[x]); n   return true; n } n int main() { n   int i, j, tp, ans, pos; n   while (~scanf( %d , &n)) { n     for (i = 1; i <= n; i++) scanf( %d , &A[i]); n     scanf( %d , &m); n     for (i = 1; i <= m; i++) scanf( %d , &B[i]); n     memset(dp, 0, sizeof dp); n     for (i = 1; i <= n; i++) { n       tp = pos = 0; n       for (j = 1; j <= m; j++) { n         dp[i][j] = dp[i - 1][j]; n         path[i][j] = path[i - 1][j]; n         if (A[i] == B[j] && tp + 1 > dp[i][j]) n           dp[i][j] = tp + 1, path[i][j] = pos; n         if (B[j] < A[i] && dp[i - 1][j] > tp) tp = dp[i - 1][j], pos = j; n       } n     } n     ans = 1; n     for (i = 1; i <= m; i++) n       if (dp[n][i] > dp[n][ans]) ans = i; n     printf( %d n , dp[n][ans]); n     if (dp[n][ans]) print(ans); n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1000000007LL; n void solve() { n   long long n; n   cin >> n; n   vector<long long> a(n); n   for (long long &i : a) cin >> i; n   unordered_map<long long, bool> m; n   for (int i = 0; i < n; i++) { n     long long sum = a[i]; n     for (int j = i + 1; j < n; j++) { n       sum += a[j]; n       if (sum > n) break; n       m[sum] = true; n     } n   } n   long long count = 0; n   for (long long i : a) { n     count += m[i]; n   } n   cout << count <<   n ; n } n int main(int argc, char const *argv[]) { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int T; n   cin >> T; 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, a, maxi = 0; n   cin >> n; n   std::stack<int> stk; n   for (int i = 0; i < n; ++i) { n     cin >> a; n     a %= 2; n     maxi = max(maxi, a); n     if (!stk.empty()) { n       if (stk.top() == a) n         stk.pop(); n       else n         stk.push(a); n     } else n       stk.push(a); n   } n   if (stk.size() < 2) n     cout <<  YES ; n   else n     cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int a, b; n   long long c, nr = 0; n   for (a = 1; a <= n; ++a) { n     for (b = a; b <= n; ++b) { n       c = sqrt(a * a + b * b); n       if (c * c == a * a + b * b && c >= b && c <= n) ++nr; n     } n   } n   cout << nr; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void fastIO() { n   ios_base::sync_with_stdio(false); n   cout.setf(ios::fixed); n   cout.precision(10); n   cout.tie(nullptr); n   cin.tie(nullptr); n } n template <class T> n T power(T N, T P) { n   return (P == 0) ? 1 : N * power(N, P - 1); n } n template <class T> n T GCD(T A, T B) { n   return (A % B == 0) ? B : GCD(B, A % B); n } n const int MOD = 1e9 + 7; n const int MX = 2e5 + 5; n const double delta = 1e-9; n const long long INF = 1e18; n const long double PI = acos((long double)-1); n void setIn(string s) { freopen(s.c_str(),  r , stdin); } n void setOut(string s) { freopen(s.c_str(),  w , stdout); } n bool sort_rev(int a, int b) { return a > b; } n bool sort_(pair<string, int> &p1, pair<string, int> &p2) { n   if (p1.first == p2.first) return p1.second < p2.second; n   return p1.first < p2.first; n } n bool func(pair<int, int> a, pair<int, int> b) { return a.first > b.first; } n int main() { n   fastIO(); n   int t = 1; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     pair<int, int> ara[n + 1]; n     for (int i = 0; i < n; i++) { n       cin >> ara[i].first; n     } n     for (int i = 0; i < n; i++) cin >> ara[i].second; n     sort(ara, ara + n, func); n     long long aa = 0, bb = 0; n     for (int i = 0; i < n; i++) { n       long long canB = bb + ara[i].second; n       long long canA = max(aa, (long long)ara[i].first); n       if (canA <= canB) n         aa = canA; n       else n         bb = canB; n     } n     cout << max(aa, bb) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = acos(-1); n template <class T> n T sqr(T x) { n   return x * x; n } n template <class T> n T alex(T x) { n   return (x > 0) ? x : -x; n } n template <class T> n void read(T& x) { n   x = 0; n   T f = 1; n   char ch = getchar(); n   while ((ch < 48 || ch > 57) && ch != 45) ch = getchar(); n   if (ch == 45) f = -1, ch = getchar(); n   while (ch >= 48 && ch <= 57) x = x * 10 + ch - 48, ch = getchar(); n   x *= f; n } n int n, q; n char s[200005]; n int t[200005 << 1]; n int st[200005], ed[200005], len; n int tmp[200005 << 1], sa[200005 << 1], rnk[200005 << 1], sum[200005 << 1]; n bool cmp(int* tmp, int x, int y, int l) { n   return tmp[x] == tmp[y] && tmp[x + l] == tmp[y + l]; n } n void getsa(int n, int m) { n   memset(sum, 0, sizeof(sum)); n   for (int i = (1); i <= (n); i++) sum[rnk[i] = t[i]]++; n   for (int i = (1); i <= (m); i++) sum[i] += sum[i - 1]; n   for (int i = (n); i >= (1); i--) sa[sum[rnk[i]]--] = i; n   for (int j = 1; j <= n; j <<= 1) { n     int p = 0; n     for (int i = (n); i >= (n - j + 1); i--) tmp[++p] = i; n     for (int i = (1); i <= (n); i++) n       if (sa[i] > j) tmp[++p] = sa[i] - j; n     memset(sum, 0, sizeof(sum)); n     for (int i = (1); i <= (n); i++) sum[rnk[tmp[i]]]++; n     for (int i = (1); i <= (max(n, m)); i++) sum[i] += sum[i - 1]; n     for (int i = (n); i >= (1); i--) sa[sum[rnk[tmp[i]]]--] = tmp[i]; n     for (int i = (1); i <= (n); i++) tmp[i] = rnk[i]; n     p = 1; n     rnk[sa[1]] = 1; n     for (int i = (2); i <= (n); i++) n       rnk[sa[i]] = p += !cmp(tmp, sa[i - 1], sa[i], j); n   } n } n int height[200005 << 1][20]; n void getheight(int n) { n   for (int i = (1); i <= (n); i++) { n     int l = max(height[rnk[i - 1]][0] - 1, 0), j = sa[rnk[i] - 1]; n     while (i + l <= n && j + l <= n && t[i + l] == t[j + l]) l++; n     height[rnk[i]][0] = l; n   } n   for (int j = (1); j < (20); j++) n     for (int i = (1); i <= (n - (1 << j) + 1); i++) n       height[i][j] = min(height[i][j - 1], height[i + (1 << (j - 1))][j - 1]); n } n int _log2[200005 << 1]; n int LCP(int x, int y) { n   if (x == y) return n - sa[x] + 1; n   if (x > y) swap(x, y); n   x++; n   int j = _log2[y - x + 1]; n   return min(height[x][j], height[y - (1 << j) + 1][j]); n } n struct Node { n   int idx, l, sign; n   Node() {} n   Node(int idx, int l, int sign) : idx(idx), l(l), sign(sign) {} n }; n int sum_bit[200005 << 1]; n int lowbit(int x) { return x & (-x); } n void insert(int v, int x) { n   for (int i = x; i <= len; i += lowbit(i)) sum_bit[i]++; n } n int ask(int l, int r) { n   int res = 0; n   for (int i = l - 1; i; i -= lowbit(i)) res -= sum_bit[i]; n   for (int i = r; i; i -= lowbit(i)) res += sum_bit[i]; n   return res; n } n vector<Node> que[200005]; n int ans[500005]; n void insert(int idx) { n   for (int i = (st[idx]); i <= (ed[idx]); i++) insert(1, rnk[i]); n } n int findl(int l, int r, int pos, int val) { n   while (l <= r) { n     int mid = (l + r) >> 1; n     if (LCP(mid, pos) >= val) n       r = mid - 1; n     else n       l = mid + 1; n   } n   return l; n } n int findr(int l, int r, int pos, int val) { n   while (l <= r) { n     int mid = (l + r) >> 1; n     if (LCP(pos, mid) >= val) n       l = mid + 1; n     else n       r = mid - 1; n   } n   return r; n } n int query(int idx) { n   int len2 = ed[idx] - st[idx] + 1; n   int l = findl(1, rnk[st[idx]] - 1, rnk[st[idx]], len2); n   int r = findr(rnk[st[idx]] + 1, len, rnk[st[idx]], len2); n   return ask(l, r); n } n int main() { n   read(n); n   read(q); n   int m = 26; n   len = 0; n   for (int i = (1); i <= (n); i++) { n     scanf( %s , s + 1); n     int l = strlen(s + 1); n     st[i] = len + 1; n     for (int k = (1); k <= (l); k++) t[++len] = s[k] -  a  + 1; n     ed[i] = len; n     t[++len] = ++m; n   } n   getsa(len, m); n   getheight(len); n   for (int i = (0); i <= (len); i++) _log2[i] = log2(i); n   for (int i = (1); i <= (q); i++) { n     int l, r, k; n     read(l); n     read(r); n     read(k); n     que[l - 1].push_back(Node(i, k, -1)); n     que[r].push_back(Node(i, k, 1)); n   } n   for (int i = (1); i <= (n); i++) { n     insert(i); n     for (int k = (0); k < (que[i].size()); k++) n       if (que[i][k].sign == -1) n         ans[que[i][k].idx] -= query(que[i][k].l); n       else n         ans[que[i][k].idx] += query(que[i][k].l); n   } n   for (int i = (1); i <= (q); i++) printf( %d n , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s, ss; n int main() { n   ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   cin >> s; n   ss = s; n   reverse(ss.begin(), ss.end()); n   cout << s + ss; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, i, j, k, l, a[1000009], b[1000009], p[1000009], ans, cn, t, x, y, z, n     mx, mn, s; n char c[1000009], d[1000009], ch; n struct point { n   int num, pos; n } pp[1000009]; n bool cmp(point a1, point a2) { n   if (a1.num == a2.num) { n     return a1.pos < a2.pos; n   } else { n     return a1.num < a2.num; n   } n } n int main() { n   while (scanf( %d , &n) != EOF) { n     ans = 0; n     x = -1; n     for (i = 0; i < n; i++) { n       scanf( %d , &pp[i].num); n       pp[i].pos = i; n     } n     sort(pp, pp + n, cmp); n     for (i = 0; i < n; i++) { n       if (i > x) { n         x = pp[i].pos; n         ans++; n       } else { n         x = max(x, pp[i].pos); n       } n     } n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 5009; n bool f[maxn][maxn], vis[maxn]; n int pre[maxn][maxn], a[maxn]; n int n, k, v, sum, cnt; n inline int read() { n   int x = 0, 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 * 10 + ch -  0 ; n     ch = getchar(); n   } n   return x * f; n } n void update(int x, int y, int opt) { n   f[x][y] = 1; n   pre[x][y] = opt; n } n int calc(int x) { return x / k + bool(x % k); } n void work(int x, int y) { n   if (x == 0) return; n   if (pre[x][y]) { n     vis[x] = 1; n     cnt++; n     sum += a[x]; n     work(x - 1, (y - a[x] % k + k) % k); n   } else n     work(x - 1, y); n } n int main() { n   n = read(); n   k = read(); n   v = read(); n   for (int i = 1; i <= n; i++) a[i] = read(), sum += a[i]; n   if (sum < v) { n     puts( NO ); n     return 0; n   } n   f[0][0] = 1; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < k; j++) n       if (f[i][j]) { n         update(i + 1, j, 0); n         update(i + 1, (j + a[i + 1]) % k, 1); n       } n   int tmp; n   for (tmp = n; tmp; tmp--) n     if (f[tmp][v % k]) break; n   if (tmp == 0) n     puts( NO ); n   else { n     puts( YES ); n     work(tmp, v % k); n     if (cnt == 0 || cnt == n) { n       for (int i = 2; i <= n; i++) { n         if (calc(a[i])) printf( %d %d 1 n , calc(a[i]), i); n         a[1] += a[i]; n       } n       if (cnt == 0 && v) printf( %d 1 2 n , v / k); n       if (cnt && a[1] - v) printf( %d 1 2 n , (a[1] - v) / k); n     } else { n       int L = 1, R = 1; n       for (; L <= n && !vis[L]; L++) n         ; n       for (; R <= n && vis[R]; R++) n         ; n       for (int i = 1; i <= n; i++) n         if (vis[i] && i != L) { n           if (calc(a[i])) printf( %d %d %d n , calc(a[i]), i, L); n           a[L] += a[i]; n         } n       for (int i = 1; i <= n; i++) n         if (!vis[i] && i != R) { n           if (calc(a[i])) printf( %d %d %d n , calc(a[i]), i, R); n           a[R] += a[i]; n         } n       if (a[L] > v) n         printf( %d %d %d n , (a[L] - v) / k, L, R); n       else if (a[L] < v) n         printf( %d %d %d n , (v - a[L]) / k, R, L); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n const int INF = 1e8 + 5; n const long long mod = 1e9 + 7; n const double pi = 3.1415926536; n int dx[] = {1, -1, 0, 0, 1, 1, -1, -1}; n int dy[] = {0, 0, 1, -1, 1, -1, -1, 1}; n int xa, ya, xb, yb, n; n vector<pair<int, int> > v; n vector<int> r; n int sqr(int a) { return a * a; } n int dist(int xa, int ya, int xb, int yb) { return sqr(xa - xb) + sqr(ya - yb); } n bool check(int x, int y) { n   for (int i = 0; i < n; i++) n     if (dist(v[i].first, v[i].second, x, y) <= sqr(r[i])) return true; n   return false; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   ios_base::sync_with_stdio(0); n   ; n   cin >> xa >> ya >> xb >> yb >> n; n   if (xa > xb) swap(xa, xb); n   if (ya > yb) swap(ya, yb); n   for (int i = 0; i < n; i++) { n     int x, y, r1; n     cin >> x >> y >> r1; n     v.push_back({x, y}); n     r.push_back(r1); n   } n   int cnt = 0; n   for (int x = xa; x <= xb; x++) { n     if (!check(x, ya)) cnt++; n     if (!check(x, yb)) cnt++; n   } n   for (int y = ya + 1; y <= yb - 1; y++) { n     if (!check(xa, y)) cnt++; n     if (!check(xb, y)) cnt++; n   } n   cout << cnt; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100010; n int n; n int q; n long long a[201]; n multiset<long long> s; n int main() { n   cin >> q; n   while (q--) { n     int flag = 0; n     s.clear(); n     cin >> n; n     long long x; n     for (int i = 1; i <= n; i++) { n       cin >> x; n       if (x == 2048) flag = 1; n       s.insert(x); n     } n     if (flag) { n       printf( YES n ); n       continue; n     } n     multiset<long long>::iterator it1; n     multiset<long long>::iterator it2; n     while (s.size() >= 2) { n       it1 = s.begin(); n       it2 = s.begin(); n       it2++; n       long long t1 = *it1; n       long long t2 = *it2; n       if (t1 == t2) { n         if (t1 + t2 == 2048) { n           flag = 1; n           break; n         } n         s.erase(it1); n         s.erase(it2); n         s.insert(t1 + t2); n       } else { n         if (t1 == 2048 || t2 == 2048) { n           flag = 1; n           break; n         } n         s.erase(it1); n       } n     } n     if (flag) n       printf( YES n ); n     else n       printf( NO n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int solve() { n   int n, x; n   cin >> n >> x; n   vector<int> inp(n, 0); n   for (int i = 0; i < n; i++) cin >> inp[i]; n   for (int i = 0; i < n; i++) { n     if (inp[i] != x) break; n     if (i == (n - 1)) return 0; n   } n   for (int i = 0; i < n; i++) { n     if (inp[i] == x) return 1; n   } n   int dec = 0, inc = 0; n   for (int i = 0; i < n; i++) { n     if (inp[i] > x) n       inc += (inp[i] - x); n     else n       dec += (x - inp[i]); n   } n   if (inc == dec) n     return 1; n   else n     return 2; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   int t; n   cin >> t; n   for (int i = 1; i <= t; i++) cout << solve() <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int tc; n   int n; n   string str; n   cin >> n >> tc; n   cin >> str; n   vector<char> vc(3); n   vector<vector<int>> dp(6, vector<int>(n + 1, 0)); n   vc[0] =  a ; n   vc[1] =  b ; n   vc[2] =  c ; n   int j, a = 0; n   do { n     for (int i = 0; i <= n - 1; i++) { n       dp[a][i + 1] = dp[a][i]; n       j = i % 3; n       if (str[i] != vc[j]) dp[a][i + 1]++; n     } n     a++; n   } while (next_permutation(vc.begin(), vc.end())); n   while (tc--) { n     int l, r, ans; n     cin >> l >> r; n     ans = r - l + 1; n     l--; n     for (int i = 0; i < 6; i++) { n       ans = min(ans, dp[i][r] - dp[i][l]); n     } n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n struct debugger { n   template <typename T> n   debugger& operator,(const T& v) { n     cerr << v <<    ; n     return *this; n   } n } dbg; n void debugarr(int* arr, int n) { n   cout <<  [ ; n   for (int i = 0; i < n; i++) cout << arr[i] <<    ; n   cout <<  ]  << endl; n } n int main() { n   int n; n   cin >> n; n   if (n % 4 == 2 || n % 4 == 3) { n     cout << -1 << endl; n     return 0; n   } n   int arr[100010] = {0}; n   int x = 2, y = n; n   for (int i = 0; i < n / 2; i += 2) { n     arr[i] = x; n     arr[i + 1] = y; n     arr[n - 1 - i] = n + 1 - x; n     arr[n - 1 - i - 1] = n + 1 - y; n     x += 2; n     y -= 2; n   } n   if (n % 2) arr[(n) / 2] = (n + 1) / 2; n   for (int i = 0; i <= (int)n - 1; i++) cout << arr[i] <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = (int)1e5 + 1, maxs = 21; n int n, m, q, stot, etot, que[maxn], rnk[maxn]; n long long bit[maxn], ans[maxn]; n struct Point { n   int x, y; n   void read() { scanf( %d%d , &x, &y); } n   bool operator<(Point const &t) const { n     return x < t.x || (x == t.x && y < t.y); n   } n   Point operator-(Point const &t) const { return (Point){x - t.x, y - t.y}; } n   Point operator+(Point const &t) const { return (Point){x + t.x, y + t.y}; } n   Point operator*(int const &t) const { return (Point){x * t, y * t}; } n   long long det(Point const &t) const { n     return (long long)x * t.y - (long long)y * t.x; n   } n } stk[maxs], vec; n pair<Point, int> a[maxn], b[maxn]; n Point p[maxn << 1 | 1]; n struct Event { n   int typ, idx; n   long long dis; n   bool operator<(Event const &t) const { n     return dis < t.dis || (dis == t.dis && typ < t.typ); n   } n } eve[maxn << 2 | 1]; n int main() { n   scanf( %d%d%d , &m, &n, &q); n   stk[stot++] = (Point){0, 0}; n   while (m--) { n     vec.read(); n     for (int i = 0; i < stot; ++i) { n       stk[stot + i] = stk[i] + vec; n       stk[i] = stk[i] - vec; n     } n     sort(stk, stk + stot + stot); n     int sz = 0; n     for (int i = 0; i < stot + stot; ++i) { n       for (; n            sz > 1 && (stk[sz - 1] - stk[sz - 2]).det(stk[i] - stk[sz - 2]) <= 0; n            --sz) n         ; n       stk[sz++] = stk[i]; n     } n     stot = sz; n   } n   stot -= stot > 1 && !(stk[stot - 1] - stk[stot - 2]).x; n   for (int i = 0; i < n; ++i) { n     a[i].first.read(); n     scanf( %d , &a[i].second); n     que[i] = a[i].first.x; n   } n   sort(que, que + n); n   m = unique(que, que + n) - que; n   for (int i = 0; i < n; ++i) n     rnk[i] = lower_bound(que, que + m, a[i].first.x) - que + 1; n   for (int i = 0; i < q; ++i) { n     b[i].first.read(); n     scanf( %d , &b[i].second); n   } n   for (int i = 0; i + 1 < stot; ++i) { n     vec = stk[i + 1] - stk[i]; n     etot = 0; n     for (int j = 0; j < n; ++j) n       eve[etot++] = (Event){0, j, vec.det(a[j].first)}; n     for (int j = 0; j < q; ++j) { n       p[j << 1] = b[j].first + stk[i] * b[j].second; n       eve[etot++] = (Event){-1, j << 1, vec.det(p[j << 1])}; n       p[j << 1 | 1] = b[j].first - stk[i + 1] * b[j].second; n       eve[etot++] = (Event){1, j << 1 | 1, vec.det(p[j << 1 | 1])}; n     } n     sort(eve, eve + etot); n     memset(bit + 1, 0, m * sizeof(long long)); n     auto bit_upd = [&](int x, int v) { n       for (; x <= m; x += x & -x) bit[x] += v; n     }; n     auto bit_sum = [&](int x) { n       long long ret = 0; n       for (; x > 0; x -= x & -x) ret += bit[x]; n       return ret; n     }; n     for (int j = 0; j < etot; ++j) { n       int idx = eve[j].idx; n       if (!eve[j].typ) { n         bit_upd(rnk[idx], a[idx].second); n       } else { n         Point lft = p[idx], rht = lft + vec * b[idx >> 1].second; n         lft.x += eve[j].typ < 0 && i > 0 || eve[j].typ > 0 && i + 2 < stot; n         int L = lower_bound(que, que + m, lft.x) - que + 1; n         int R = upper_bound(que, que + m, rht.x) - que; n         long long las = ans[idx >> 1]; n         ans[idx >> 1] += eve[j].typ * (bit_sum(R) - bit_sum(L - 1)); n         las = ans[idx >> 1] - las; n       } n     } n   } n   for (int i = 0; i < q; ++i) printf( %lld n , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, c; n   scanf( %d%d%d , &a, &b, &c); n   int n = a * c - b * c; n   if (n % b > 0) n     printf( %d n , n / b + 1); n   else n     printf( %d n , n / b); n   return 0; n } n


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


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


#include <bits/stdc++.h> n using namespace std; n int n; n long long int K; n int ans[100]; n long long int fib[100]; n int main() { n   scanf( %d%I64d , &n, &K); n   fib[0] = fib[1] = 1, fib[2] = 2; n   for (int i = 3; i <= n; i++) fib[i] = fib[i - 1] + fib[i - 2]; n   for (int i = 1; i <= n; i++) ans[i] = i; n   for (int i = 1; i <= n; i++) n     if (K > fib[n - i]) { n       K -= fib[n - i]; n       swap(ans[i], ans[i + 1]); n       i++; n     } n   for (int i = 1; i <= n; i++) printf( %d  , ans[i]); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n ostream &operator<<(ostream &os, const pair<T, T> &p) { n   return os <<  (  << p.first <<  ,  << p.second <<  ) ; n } n template <class T> n ostream &operator<<(ostream &os, const pair<const T, T> &p) { n   return os <<  (  << p.first <<  ,  << p.second <<  ) ; n } n template <class T> n ostream &operator<<(ostream &os, const array<T, 3> &p) { n   return os <<  (  << p[0] <<  ,  << p[1] <<  ,  << p[2] <<  ) ; n } n template <class T> n ostream &operator<<(ostream &os, const vector<T> &p) { n   for (auto x : p) os << x <<    ; n   return os; n } n const double EPS = 1e-9; n const double PI = acos(-1); n const int INF = 0x3f3f3f3f; n const long long LINF = 0x3f3f3f3f3f3f3f3f; n const long long mod = 1e9 + 7; n const int SZ = 2e5 + 20; n int ciclo[SZ]; n int custo[SZ]; n int go[SZ]; n int vis[SZ]; n int dfs1(int v) { n   if (ciclo[v]) return ciclo[v]; n   if (vis[v]) return ciclo[v] = v; n   vis[v] = 1; n   return ciclo[v] = dfs1(go[v]); n } n int aux; n int tempo; n void dfs(int v) { n   if (vis[v] < tempo) { n     aux = 0; n     return; n   } n   if (vis[v] == tempo) return; n   vis[v] = tempo; n   aux = min(aux, custo[v]); n   dfs(go[v]); n } n int main() { n   int n; n   scanf(  %d , &n); n   for (int i = 1; i < n + 1; i++) { n     scanf(  %d , &custo[i]); n   } n   for (int i = 1; i < n + 1; i++) { n     scanf(  %d , &go[i]); n   } n   for (int i = 1; i < n + 1; i++) dfs1(i); n   long long res = 0; n   memset(vis, INF, sizeof(vis)); n   for (int i = 1; i < n + 1; i++) { n     aux = INF; n     tempo += 2; n     dfs(ciclo[i]); n     res += aux; n   } n   cout << res <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double p[111], q[111]; n int main() { n   int n; n   cin >> n; n   for (int i = 0, u; i < n; i++) { n     scanf( %d , &u); n     p[i] = u / 100.0; n     q[i] = 1; n   } n   double ans = 0; n   while (1) { n     int now = 0; n     double tmp = 1; n     for (int i = 0; i < n; i++) { n       if (q[i] * p[i] * (1 - q[now]) > q[now] * p[now] * (1 - q[i])) now = i; n       tmp *= 1 - q[i]; n     } n     ans += 1 - tmp; n     q[now] *= (1 - p[now]); n     if (1 - tmp < 1e-10) break; n   } n   printf( %.6lf n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n map<int, int> a; n void reset() { a.clear(); } n void input() { n   cin >> n; n   int c; n   for (int(i) = (0); (i) < (n); ++i) { n     cin >> c; n     a[c]++; n   } n } n bool check(int s) { n   for (auto it = a.begin(); it != a.end(); ++it) { n     double b = (double)(it->second); n     if ((int)ceil(b / s) != (int)floor(b / (s - 1))) { n       return false; n     } n   } n   return true; n } n void process() { n   double d = (double)n; n   int mn = n + 1; n   for (auto it = a.begin(); it != a.end(); ++it) mn = min(mn, it->second); n   int m = n + 1; n   for (int(s) = (2); (s) < (mn + 2); ++s) { n     bool ok = true; n     int tmp = 0; n     for (auto it = a.begin(); it != a.end(); ++it) { n       int f = it->second; n       int q = (int)ceil(1.0 * f / s); n       if (f < q * (s - 1)) { n         ok = false; n         break; n       } n       tmp += q; n     } n     if (ok) m = min(m, tmp); n   } n   cout << m <<   n ; n } n void solve() { n   reset(); n   input(); n   process(); n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int T; n   cin >> T; n   while (T--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, a[100005]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n; n   string s; n   cin >> s; n   if (n == 1 && s[0] ==  1 ) n     cout <<  Yes ; n   else if (n == 1) n     cout <<  No ; n   else if (n == 2 && s[0] != s[1]) n     cout <<  Yes ; n   else if (n == 2) n     cout <<  No ; n   else { n     if (s[0] ==  0  && s[1] ==  0 ) n       cout <<  No ; n     else if (s[n - 1] ==  0  && s[n - 2] ==  0 ) n       cout <<  No ; n     else { n       for (int i = 0; i < n - 2; ++i) { n         if (s[i] ==  0  && s[i + 1] ==  0  && s[i + 2] ==  0 ) { n           cout <<  No ; n           return 0; n         } else if (s[i] ==  1  && s[i + 1] ==  1 ) { n           cout <<  No ; n           return 0; n         } else if (s[i + 1] ==  1  && s[i + 2] ==  1 ) { n           cout <<  No ; n           return 0; n         } n       } n       cout <<  Yes ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> lefts[2000000], rights[2000000]; n vector<pair<pair<int, int>, bool>> lws; n int maxleft[2000000]; n int maxright[2000000]; n int minright[2000000]; n vector<int> ans; n bool fail = false; n int n; n int dfs(int v, int needed) { n   int cur = v; n   if (maxleft[v] > v) cur = dfs(cur + 1, maxleft[v]); n   ans.push_back(v); n   if (cur >= minright[v]) { n     fail = true; n     return n; n   } n   if (needed <= cur && maxright[v] <= cur) return cur; n   return dfs(cur + 1, max(needed, maxright[v])); n } n int main() { n   ios_base::sync_with_stdio(false); n   int c; n   cin >> n >> c; n   for (int i = 1; i <= n; i++) { n     maxleft[i] = maxright[i] = i; n     minright[i] = 1e9; n   } n   for (int i = 0; i < c; i++) { n     int a, b; n     string s; n     cin >> a >> b >> s; n     lws.push_back(make_pair(make_pair(a, b), s !=  LEFT )); n     if (s ==  LEFT ) { n       lefts[a].push_back(b); n       if (b <= a) fail = true; n       maxleft[a] = max(maxleft[a], b); n     } n     if (s ==  RIGHT ) { n       rights[a].push_back(b); n       if (b <= a) fail = true; n       maxright[a] = max(maxright[a], b); n       minright[a] = min(minright[a], b); n     } n   } n   for (int i = 1; i <= n; i++) { n     if (minright[i] <= maxleft[i]) fail = true; n   } n   dfs(1, n); n   if (fail) n     cout <<  IMPOSSIBLE ; n   else { n     for (int i = 0; i < n; i++) { n       cout << ans[i] <<    ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int rw, rh, bw, bh; n int main() { n   cin >> rw >> rh >> bw >> bh; n   int Cred = rh * 2 + rw + 2, Cblue = bh * 2 + bw + 2; n   int n = Cblue + Cred; n   if (bw > rw) n     n += bw - rw; n   else n     n += rw - bw; n   cout << n << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s[11]; n int a[11]; n int n, k; n int ans = 0x7fffffff; n string t[11]; n int num[11]; n int main() { n   cin >> n >> k; n   for (int i = 0; i < n; i++) { n     cin >> s[i]; n   } n   for (int i = 0; i < k; i++) { n     a[i] = i; n   } n   do { n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < k; j++) { n         t[i][j] = s[i][a[j]]; n       } n     } n     for (int i = 0; i < n; i++) { n       int tt = 0; n       for (int j = 0; j < k; j++) { n         tt = tt * 10 + t[i][j] -  0 ; n       } n       num[i] = tt; n     } n     sort(num, num + n); n     ans = min(ans, num[n - 1] - num[0]); n   } while (next_permutation(a, a + k)); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n int main(void) { n   char s[1000001] =   ; n   int n, n2, x, y, i; n   scanf( %d , &n); n   n2 = (n / 7) * 7; n   while (n2 >= 0) { n     if ((n - n2) % 4 == 0) n       break; n     else n       n2 = n2 - 7; n   } n   if (n2 < 0) n     printf( -1 n ); n   else { n     y = n2 / 7; n     x = (n - n2) / 4; n     for (i = 0; i < x; i++) printf( %c ,  4 ); n     for (i = 0; i < y; i++) printf( %c ,  7 ); n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int t; n int n, d; n int a; n int main() { n   cin >> t; n   while (t--) { n     cin >> n >> d; n     cin >> a; n     long long ans = a; n     for (int i = 1; i < n; i++) { n       cin >> a; n       if (d == 0) continue; n       if (d > i * a) { n         d -= i * a; n         ans += a; n       } else { n         ans += d / i; n         d = 0; n       } n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[200000], b[200000]; n set<pair<int, int>> se; n set<pair<int, int>>::iterator t; n int main() { n   int n, m, d; n   cin >> n >> m >> d; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     se.insert(make_pair(a[i], i)); n   } n   int cnt = 0, pos; n   while (!se.empty()) { n     pos = se.begin()->second; n     b[pos] = ++cnt; n     se.erase(se.begin()); n     while (1) { n       t = se.lower_bound({a[pos] + d + 1, 0}); n       if (t == se.end()) break; n       pos = t->second; n       b[pos] = cnt; n       se.erase(t); n     } n   } n   cout << cnt <<   n  << b[0] <<    ; n   for (int i = 1; i < n; i++) cout << b[i] <<    ; n   return 0; n } n