#include <bits/stdc++.h> n using namespace std; n const int N = 200005; n int cycl[N], set_val[N], cycl_size[N]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int h, m, n; n   cin >> h >> m >> n; n   vector<int> strt; n   bool vis[h]; n   memset(vis, false, sizeof vis); n   int kk = 0; n   set<int> dmm[h + 1]; n   for (long long i = 0; i < m; i++) { n     if (vis[i]) { n       continue; n     } n     int ii = i; n     int jp = 0; n     while (1) { n       if (vis[ii]) { n         break; n       } n       set_val[ii] = jp; n       cycl[ii] = kk; n       dmm[kk].insert(jp); n       vis[ii] = true; n       ii += m; n       ii %= h; n       jp++; n     } n     cycl_size[kk] = jp; n     kk++; n   } n   map<int, int> hash; n   map<int, int> final; n   long long cnt = 0; n   while (n--) { n     char ch; n     cin >> ch; n     if (ch ==  + ) { n       int id, t; n       cin >> id >> t; n       hash[id] = t; n       if (dmm[cycl[t]].find(set_val[t]) != dmm[cycl[t]].end()) { n         final[id] = set_val[t]; n         dmm[cycl[t]].erase(set_val[t]); n         continue; n       } n       set<int>::iterator it, it1; n       it = dmm[cycl[t]].lower_bound(set_val[t]); n       int xx1; n       if (it == dmm[cycl[t]].end()) { n         it = dmm[cycl[t]].begin(); n         xx1 = cycl_size[cycl[t]] + *it - set_val[t]; n         int jj = *it; n         final[id] = jj; n         dmm[cycl[t]].erase(jj); n       } else { n         xx1 = *it - set_val[t]; n         int jj = *it; n         final[id] = jj; n         dmm[cycl[t]].erase(jj); n       } n       cnt += (long long)xx1; n     } else { n       int id; n       cin >> id; n       dmm[cycl[hash[id]]].insert(final[id]); n     } n   } n   cout << cnt << endl; n } n


#include <bits/stdc++.h> n int p[100005], q[100005], n, m, V; n double a[100005], x[100005], y[100005], dx[100005], dy[100005], h, vx, vy, t, w; n bool cmp1(const int i, const int j) { return a[i] < a[j]; } n bool cmp2(const int i, const int j) { return x[i] < x[j]; } n bool up(double t, double y) { return (h = vy * t - 9.8 * t * t / 2) > y; } n int main() { n   scanf( %d%d , &n, &V); n   for (int i = 0; i < n; i++) scanf( %lf , &a[i]), q[i] = i; n   scanf( %d , &m); n   for (int i = 0; i < m; i++) scanf( %lf %lf , &x[i], &y[i]), p[i] = i; n   std::sort(q, q + n, cmp1); n   std::sort(p, p + m, cmp2); n   int j = 0; n   for (int k = 0; k < n; k++) { n     int i = q[k]; n     vx = V * cos(a[i]), vy = V * sin(a[i]), t = vy / 4.9, w = vx * t; n     while (j < m && x[p[j]] < w && up(x[p[j]] / vx, y[p[j]])) j++; n     if (j < m && x[p[j]] < w) n       dx[i] = x[p[j]], dy[i] = h; n     else n       dx[i] = w; n   } n   for (int i = 0; i < n; i++) printf( %lf %lf n , dx[i], dy[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dx[] = {-1, 1, 0, 0, -1, -1, 1, 1}; n int dy[] = {0, 0, 1, -1, -1, 1, -1, 1}; n map<int, bool> ans; n map<long long, int> dp; n int rec(long long q) { n   if (ans[q] == 1) return dp[q]; n   vector<int> v; n   long long sq = sqrt(q); n   int mex, x; n   for (int l = 2; l < sq + 1; l++) { n     if (!(q % l)) { n       x = rec(l); n       v.push_back(x); n       x = rec(q / l); n       v.push_back(x); n     } n   } n   if (v.size() == 0) n     mex = 1; n   else { n     sort(v.begin(), v.end()); n     (v).erase(unique((v).begin(), (v).end()), (v).end()); n     int len = v.size(); n     mex = 0; n     for (int l = 0; l < len; l++) { n       if (v[l] != mex) break; n       mex++; n     } n   } n   ans[q] = 1; n   return dp[q] = mex; n } n bool chq(long long q) { n   long long sq = sqrt(q); n   for (int l = 2; l < sq + 1; l++) { n     if (!(q % l)) return 0; n   } n   return 1; n } n long long go(long long q) { n   long long sq = sqrt(q); n   int x; n   for (int l = 2; l < sq + 1; l++) { n     if (!(q % l)) { n       x = dp[l]; n       if (!x) return l; n       x = dp[q / l]; n       if (!x) return q / l; n     } n   } n } n int main() { n   int t, z = 0, len; n   long long n, k = 0, m = 0; n   scanf( %lld , &n); n   bool prime = chq(n); n   if (prime) { n     printf( 1 n ); n     printf( 0 n ); n     return 0; n   } n   k = rec(n); n   if (!k) n     printf( 2 n ); n   else { n     printf( 1 n ); n     m = go(n); n     printf( %lld , m); n     printf(  n ); n   } n   return 0; n } n


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


#include <bits/stdc++.h> n char num[10][10]; n int main() { n   for (int i = 1; i < 9; i++) scanf( %s , num[i]); n   bool yes = true; n   for (int i = 1; i <= 8; i++) { n     int cntb = 0, cntw = 0; n     char pre =  * ; n     for (int j = 0; j < 8; j++) { n       if (num[i][j] == pre) { n         yes = false; n         break; n       } n       if (num[i][j] ==  W ) { n         cntw++; n         pre = num[i][j]; n       } n       if (num[i][j] ==  B ) { n         cntb++; n         pre = num[i][j]; n       } n     } n     if (cntb != cntw) { n       yes = false; n     } n   } n   printf( %s n , yes ?  YES  :  NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct hero { n   int power; n   int nr; n }; n int main() { n   int n, m; n   int board[100]; n   hero heroes[100]; n   vector<int> left, right; n   cin >> n >> m; n   for (int i = 0; i < n; ++i) heroes[i].nr = -1; n   int l_ind = n; n   int r_ind = -1; n   for (int si, hi, i = 0; i < m; ++i) { n     cin >> si >> hi; n     heroes[si - 1].nr = i + 1; n     heroes[si - 1].power = hi; n     if (si - 1 < l_ind) l_ind = si - 1; n     if (si - 1 > r_ind) r_ind = si - 1; n   } n   for (int i = 0; i < n; ++i) cin >> board[i]; n   int cur_pow = heroes[l_ind].power; n   int cur_hero = heroes[l_ind].nr; n   if (m == 1) { n     cout << l_ind + 1 <<   n1  << endl; n     return 0; n   } n   while (l_ind < n - 1 && l_ind + 1 < r_ind) { n     if (board[l_ind + 1] >= 0) { n       cur_pow += board[l_ind + 1]; n       if (heroes[l_ind + 1].nr != -1) { n         if (heroes[l_ind + 1].power > cur_pow) { n           left.push_back(cur_hero); n           cur_hero = heroes[l_ind + 1].nr; n           cur_pow = heroes[l_ind + 1].power; n         } else { n           left.push_back(heroes[l_ind + 1].nr); n         } n       } n     } else { n       if (cur_pow + board[l_ind + 1] >= 0) n         cur_pow += board[l_ind + 1]; n       else { n         break; n       } n     } n     ++l_ind; n   } n   left.push_back(cur_hero); n   cur_hero = heroes[r_ind].nr; n   cur_pow = heroes[r_ind].power; n   while (r_ind > 0 && l_ind < r_ind - 1) { n     if (board[r_ind - 1] >= 0) { n       cur_pow += board[r_ind - 1]; n       if (heroes[r_ind - 1].nr != -1) { n         if (heroes[r_ind - 1].power > cur_pow) { n           right.push_back(cur_hero); n           cur_hero = heroes[r_ind - 1].nr; n           cur_pow = heroes[r_ind - 1].power; n         } else { n           right.push_back(heroes[r_ind - 1].nr); n         } n       } n     } else { n       if (cur_pow + board[r_ind - 1] >= 0) n         cur_pow += board[r_ind - 1]; n       else { n         break; n       } n     } n     --r_ind; n   } n   right.push_back(cur_hero); n   if (abs(l_ind - r_ind) > 1) n     cout <<  -1  << endl; n   else { n     cout << l_ind + 1 << endl; n     for (auto it = left.end() - 1; it >= left.begin(); --it) n       cout << (*it) <<    ; n     for (auto it = right.end() - 1; it >= right.begin(); --it) n       cout << (*it) <<    ; n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = 3.14159265358979323846; n const int N = 1e6 + 5; n const long long MOD = 1000000007; n int dx[] = {0, 1, 0, -1}; n int dy[] = {1, 0, -1, 0}; n bool cmp(const pair<int, int>& a, const pair<int, int>& b) { n   return (a.second < b.second); n } n int gcd(int a, int b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n long long power(long long n, long long k) { n   if (k == 0) return 1; n   long long sq = (power(n, k / 2)); n   sq = (sq * sq); n   if (k % 2 == 1) sq = (sq * n); n   return sq; n } n bool isPrime(int n) { n   if (n <= 1) return false; n   if (n <= 3) return true; n   if (n % 2 == 0 || n % 3 == 0) return false; n   for (int i = 5; i * i <= n; i = i + 6) n     if (n % i == 0 || n % (i + 2) == 0) return false; n   return true; n } n string to_binary(int x) { n   string s; n   while (x > 0) { n     s += (x % 2 ?  1  :  0 ); n     x /= 2; n   } n   reverse(s.begin(), s.end()); n   return s; n } n bool isPerfect(long long x) { n   if (x < 1) return false; n   long long root(round(sqrt(x))); n   return x == root * root * 1LL; n } n bool valid(int i, int j, int n, int m) { n   return i >= 0 && i < n && j >= 0 && j < m; n } n long long FastPower(long long x, long long y) { n   return (!y ? 1 n              : (((y & 1) ? x : 1) % MOD * n                 FastPower((x % MOD) * (x % MOD) % MOD, y >> 1) % MOD) % n                    MOD); n } n void solve() { n   int n; n   cin >> n; n   vector<pair<int, int>> arr(n); n   vector<int> ans; n   long long all_sum = 0; n   for (int i = 0; i < n; i++) n     cin >> arr[i].first, arr[i].second = i, all_sum += arr[i].first; n   sort((arr).begin(), (arr).end()); n   bool flg = true; n   for (int i = 0; i < n - 1; i++) { n     if ((all_sum - arr[i].first - arr[n - 1].first) == arr[n - 1].first) n       ans.push_back(arr[i].second + 1), flg = false; n   } n   if ((all_sum - arr[n - 1].first - arr[n - 2].first) == arr[n - 2].first) n     ans.push_back(arr[n - 1].second + 1), flg = false; n   if (((int)((ans).size())) == 0) { n     cout << ((int)((ans).size())) <<   n ; n   } else { n     cout << ((int)((ans).size())) <<   n ; n     for (int i = 0; i < ((int)((ans).size())) - 1; i++) cout << ans[i] <<    ; n     cout << ans[((int)((ans).size())) - 1] <<   n ; n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   int t = 1; n   while (t--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n ifstream in( input.txt ); n ofstream out( output.txt ); n int main() { n   vector<int> a; n   int n, x; n   cin >> n >> x; n   a.resize(100001); n   vector<int> b; n   for (int i = 0; i < n; i++) { n     int t; n     cin >> t; n     a[t]++; n     b.push_back(t); n   } n   unsigned long long ans = 0; n   for (int i = 0; i < n; i++) { n     if ((b[i] ^ x) == b[i]) { n       ans += (a[b[i]] - 1); n     } else if ((b[i] ^ x) < 100001) { n       ans += a[b[i] ^ x]; n     } n     a[b[i]]--; n   } n   cout << ans; n } n


#include <bits/stdc++.h> n #define ll long long n #define nl   n  n #define vpn 1000000 n using namespace std; n ll t,n,dismin; n ll a[vpn],b[vpn]; n ll gcd(ll x, ll y) n { n     if (y==0) return x; n     return gcd(y,x%y); n } n int main() n { n     ios::sync_with_stdio(0); n     cin.tie(0); n     cin>>t; n     while (t--) n     { n         cin>>n; n         for(ll i=1;i<=n;i++) n             cin>>a[i]; n         sort(a+1,a+n+1); n         if (a[1]<0) cout<< NO <<nl; n         else n         { n             if (a[n]<=300) n             { n                 cout<< YES <<nl; n                 if (a[1]!=0) n                 { n                     cout<<a[n]<<nl; n                 for (ll i=1;i<=a[n];i++) n                     cout<<i<<   ; n                 } n                 else n                 { n                     cout<<a[n]+1<<nl; n                     for (ll i=0;i<=a[n];i++) n                         cout<<i<<   ; n                 } n                 cout<<nl; n             } n             else n             { n                 bool ans=true; n                 vector<ll> dis; n                 for (ll i=2;i<=n;i++) n                     dis.push_back(a[i]-a[i-1]); n                 ll pos; n                 pos=dis[0]; n                 for (ll i=1;i<dis.size();i++) n                 { n                     pos=gcd(pos,dis[i]); n                     if (pos==1) n                     { n                         cout<< NO <<nl; n                         ans=false; n                         break; n                     } n                 } n                 if (ans==true) n                 { n                     if (((a[n]-pos)/pos+1)>300) cout<< NO <<nl; n                     else n                     { n                         cout<< YES <<nl; n                         if (a[1]==0) n                         { n                             cout<<((a[n]-pos)/pos+2)<<nl; n                             cout<<a[1]<<   ; n                         } n                         else cout<<((a[n]-pos)/pos+1)<<nl; n                         for (ll i=pos;i<=a[n];i+=pos) n                             cout<<i<<   ; n                         cout<<nl; n                     } n                 } n             } n         } n     } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   long long t = 0, i, j, n; n   long long arr[100002]; n   cin >> n; n   for (i = 0; i < n; ++i) { n     cin >> arr[i]; n     t += arr[i]; n   } n   if (t % (n - 1) == 0) { n     j = t / (n - 1); n   } else { n     j = t / (n - 1) + 1; n   } n   long long mx = *max_element(arr, arr + n); n   cout << max(mx, j); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ii = pair<ll, ll>; n inline int get_state(ll s, int x) { return ((s >> (5 * x)) & 0x1f); } n inline ll set_state(ll s, ll v, int x) { n   return ((s & ~(0x1fLL << (5 * x))) | (v << (5 * x))); n } n int cs[10]; n map<ll, int> st; n map<ll, ii> ps; n bool kill_all(ll s, int n, int a, int b) { n   for (int i = 2; i <= n - 1; ++i) n     if (cs[i] > n         b * get_state(s, i - 1) + a * get_state(s, i) + b * get_state(s, i + 1)) n       return false; n   return true; n } n bool all_dead(ll s, int pos, int a, int b) { n   for (int i = pos - 1; i <= pos + 1; ++i) { n     if (cs[i] > n         b * get_state(s, i - 1) + a * get_state(s, i) + b * get_state(s, i + 1)) n       return false; n   } n   return true; n } n int dp(ll s, int n, int a, int b) { n   if (s == 0) return 0; n   auto it = st.find(s); n   if (it != st.end()) { n     return it->second; n   } n   auto res = 1000000000; n   for (int i = 2; i <= n - 1; ++i) { n     if (get_state(s, i - 1) or get_state(s, i) or get_state(s, i + 1)) { n       auto r = set_state(s, max(get_state(s, i) - a, 0), i); n       r = set_state(r, max(get_state(r, i - 1) - b, 0), i - 1); n       r = set_state(r, max(get_state(r, i + 1) - b, 0), i + 1); n       auto value = dp(r, n, a, b) + 1; n       if (value < res) { n         res = value; n         ps[s] = ii(r, i); n       } n     } n   } n   st[s] = res; n   return res; n } n vector<int> solve(int n, int a, int b) { n   vector<int> shoots; n   ll s = 0; n   for (int i = 1; i <= n; ++i) s = set_state(s, cs[i], i); n   while (cs[1] > 0) { n     shoots.push_back(2); n     cs[1] -= b; n     cs[2] -= a; n     cs[3] -= b; n     s = set_state(s, max(get_state(s, 1) - b, 0), 1); n     s = set_state(s, max(get_state(s, 2) - a, 0), 2); n     s = set_state(s, max(get_state(s, 3) - b, 0), 3); n   } n   while (cs[n] > 0) { n     shoots.push_back(n - 1); n     cs[n] -= b; n     cs[n - 1] -= a; n     cs[n - 2] -= b; n     s = set_state(s, max(get_state(s, n - 2) - b, 0), n - 2); n     s = set_state(s, max(get_state(s, n - 1) - a, 0), n - 1); n     s = set_state(s, max(get_state(s, n) - b, 0), n); n   } n   dp(s, n, a, b); n   auto it = ps.find(s); n   while (it != ps.end()) { n     s = it->second.first; n     shoots.push_back(it->second.second); n     it = ps.find(s); n   } n   for (int i = 2; i <= n - 1; ++i) n     for (int j = 0; j < get_state(s, i); ++j) shoots.push_back(i); n   return shoots; n } n int main() { n   int n, a, b; n   cin >> n >> a >> b; n   for (int i = 1; i <= n; ++i) { n     cin >> cs[i]; n     ++cs[i]; n   } n   auto shoots = solve(n, a, b); n   cout << shoots.size() << endl; n   for (size_t i = 0; i < shoots.size(); ++i) n     printf( %d%c , shoots[i],    n [i + 1 == shoots.size()]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void init() {} n const int N = 63e5 + 34; n const long long INF = 2e9 + 34; n int MD = 30; n string sym =  AHIMOTUVWXYovwx ; n void fail() { n   cout <<  NIE ; n   exit(0); n } n int main() { n   init(); n   string s; n   cin >> s; n   int n = s.length(); n   if (n % 2 && sym.find(s[n / 2]) > 3000) fail(); n   for (int i = 0; i < n / 2; i++) { n     if (sym.find(s[i]) < 3000 && sym.find(s[n - i - 1]) < 300 && n         s[i] == s[n - i - 1]) n       continue; n     if (s[i] ==  b  && s[n - i - 1] ==  d ) continue; n     if (s[i] ==  d  && s[n - i - 1] ==  b ) continue; n     if (s[i] ==  p  && s[n - i - 1] ==  q ) continue; n     if (s[i] ==  q  && s[n - i - 1] ==  p ) continue; n     fail(); n   } n   cout <<  TAK ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2000 + 5; n const int MOD = 1000000000 + 7; n int n, k; n int a[maxn], e[maxn], s[maxn], t[maxn]; n inline void mm(int a[], int b[]) { n   for (int i = 0; i < n; i++) { n     t[i] = 0; n     for (int j = 0; j <= i; j++) n       t[i] = ((long long)a[j] * b[i - j] + t[i]) % MOD; n   } n   for (int i = 0; i < n; i++) a[i] = t[i]; n } n int main() { n   cin >> n >> k; n   for (int i = 0; i < n; i++) { n     scanf( %d , a + i); n     e[i] = 1; n   } n   s[0] = 1; n   while (k) { n     if (k & 1) mm(s, e); n     mm(e, e); n     k >>= 1; n   } n   mm(a, s); n   for (int i = 0; i < n; i++) printf( %d  , a[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200001; n int cs1[N]; n int cs2[N]; n int ss1[N]; n int ss2[N]; n int sum(int i, int cs[]) { n   int s = 0; n   while (i > 0) { n     s += cs[i]; n     i -= i & -i; n   } n   return s; n } n void add(int i, int k, int cs[]) { n   while (i < N) { n     cs[i] += k; n     i += i & -i; n   } n } n int main() { n   int n, k, a, b, q; n   cin >> n >> k >> a >> b >> q; n   for (int i = 0; i < q; ++i) { n     int c; n     scanf( %d , &c); n     if (c == 1) { n       int d, aa; n       scanf( %d%d , &d, &aa); n       add(d, min(aa, b - ss1[d]), cs1); n       add(d, min(aa, a - ss2[d]), cs2); n       ss1[d] = min(ss1[d] + aa, b); n       ss2[d] = min(ss2[d] + aa, a); n     } else { n       int p; n       scanf( %d , &p); n       int o = sum(n, cs2) - sum(p + k - 1, cs2) + sum(p - 1, cs1); n       printf( %d n , o); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int ans = 0, a, b, a2, b2; n   cin >> a >> b; n   while (((a != 0) and (b != 0))) { n     if ((a + b) <= 2) { n       cout << ans << endl; n       return 0; n     } n     if (2 * a <= b) { n       cout << ans + a << endl; n       return 0; n     } n     if (2 * b <= a) { n       cout << ans + b << endl; n       return 0; n     } n     if (a == 2 and b == 2) { n       cout << ans + 1 << endl; n       return 0; n     } n     int aux = a; n     int aux2 = b; n     a = min(aux, aux2); n     b = max(aux, aux2); n     while (a < b) { n       a--; n       b -= 2; n       ans++; n     } n     aux = a / 3; n     a = a - aux * 3; n     b = b - aux * 3; n     aux = aux * 2; n     ans += aux; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n  n #define modulus 1000000007 n  n //for loops n //------------------- n #define for0(i, N) for (i = 0; i < (int) N; i++) n #define for1(i, N) for (i = 1; i <= (int) N; i++) n #define forr0(i, N) for (i = (int) N - 1; ~i; i--) n #define forr1(i, N) for (i = (int) N; i; i--) n #define foru(i, l, r, x) for (i = l; i <= r; i += x) n //to declare a for loop, n //first declare the primitive on a separate line n // n // int i; n // for0(i, N){ n //      code n // } n  n  n //primitives & string n //------------------- n using ll = long long; n using str = std::string; n  n  n //pairs n //----- n using ii = std::pair<int, int>; n using llll = std::pair<ll, ll>; n #define mp make_pair n #define fi first n #define se second n  n using mii = std::map<int, int>; n using mllll = std::map<ll, ll>; n  n  n //vectors n //------------- n #define pb push_back n using vi = std::vector<int>; n using vvi = std::vector<vi>; n using vii = std::vector<ii>; n using vll = std::vector<ll>; n using vvll = std::vector<vll>; n using vb = std::vector<bool>; n  n  n //those grid problems (thanks BenCh) n //---------------------------------- n const int dx[] = {1, 0, -1, 0},  dy[] = {0, 1, 0, -1}; n  n  n //division rounded up and rounded down n //-------- n ll cdiv(ll a, ll b) { return a/b+((a^b)>0&&a%b); } n ll fdiv(ll a, ll b) { return a/b-((a^b)<0&&a%b); } n  n  n using namespace std; n  n //ACTUAL CODE n //------------------------------------------------------------------------------------------------ n //                                                                                | n //                                                                                | n  n  n  n  n void solve(){ n  n     int N; cin >> N; n     vector<pair<int, int>> coords; n     char arr[N][N]; n     for (int i = 0; i < N; i++) { n         for (int j = 0; j < N; j++) { n             cin >> arr[i][j]; n             if (arr[i][j] ==  * ) { n                 coords.pb({i, j}); n             } n         } n     } n  n     if (coords[0].first == coords[1].first) { n         if (coords[1].first == N - 1) { n             arr[N - 2][coords[1].second] =  * ; n             arr[N - 2][coords[0].second] =  * ; n         } n         else { n             arr[coords[1].first + 1][coords[1].second] =  * ; n             arr[coords[0].first + 1][coords[0].second] =  * ; n         } n     } n     else if (coords[0].second == coords[1].second) { n             if (coords[0].second == N - 1) { n                 arr[coords[0].first][N - 2] =  * ; n                 arr[coords[1].first][N - 2] =  * ; n             } n             else { n                 arr[coords[0].first][coords[0].second + 1] =  * ; n                 arr[coords[1].first][coords[1].second + 1] =  * ; n             } n         } n     else { n         arr[coords[0].first][coords[1].second] =  * ; n         arr[coords[1].first][coords[0].second] =  * ; n     } n  n     for (int i = 0; i < N; i++) { n         for (int j = 0; j < N; j++) { n             cout << arr[i][j]; n         } n         cout <<   n ; n     } n  n } n  n int main() n { n     std::ios::sync_with_stdio(0); n     std::cin.tie(0); std::cout.tie(0); std::cout << std::fixed; n  n     int t; std::cin >> t; n     while(t--) solve(); n  n     return 0; n } n  n  n  n  n  n  n //                                                                                | n //                                                                                | n //------------------------------------------------------------------------------------------------ n  n  n  n  n //  NOTES: n //~~~~~~~~~~~~~~~~~~~~~~~~ n // debug: n // - check int overflow, out of bounds n // - negative indexing? n //  special cases (n = 1, n = 0?) n //  do something instead of nothing n //  *stay organized* n // - write down IDEAS n // - DON T GET CAUGHT ON ONE APPROACH n // n // n // n // most of all, maintain absolute concentration - take a break if you must n // n // n


#include <bits/stdc++.h> n using namespace std; n const long long int mod = 1000000000 + 7; n const long long int N = 10000000 + 6; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int q; n   cin >> q; n   for (int i = 0; i < q; ++i) { n     long long n; n     cin >> n; n     vector<int> vals; n     int pos2 = -1; n     while (n > 0) { n       vals.push_back(n % 3); n       if (vals.back() == 2) pos2 = int(vals.size()) - 1; n       n /= 3; n     } n     vals.push_back(0); n     if (pos2 != -1) { n       int pos0 = find(vals.begin() + pos2, vals.end(), 0) - vals.begin(); n       vals[pos0] = 1; n       for (int i = pos0 - 1; i >= 0; --i) { n         vals[i] = 0; n       } n     } n     long long ans = 0; n     long long pw = 1; n     for (int i = 0; i < int(vals.size()); ++i) { n       ans += pw * vals[i]; n       pw *= 3; n     } n     if (vals.back() == 1) ans = pw / 3; n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n const bool MULTI_TESTCASES = 0; n struct Query { n   int l, r, id; n }; n void solve() { n   int n, k; n   cin >> n >> k; n   vector<int> t(n + 1); n   for (int i = 1; i < n + 1; ++i) { n     cin >> t[i]; n     if (t[i] == 2) t[i] = -1; n   } n   vector<ll> a(n + 1); n   for (int i = 1; i < n + 1; ++i) { n     cin >> a[i]; n     a[i] *= t[i]; n     a[i] += a[i - 1]; n   } n   map<ll, int> comp; n   for (int i = 0; i < n + 1; ++i) { n     comp[a[i]] = comp[a[i] - k] = comp[a[i] + k] = 0; n   } n   int z = 0; n   for (auto &kv : comp) kv.second = z++; n   vector<int> a1(n + 1), a2(n + 1), a3(n + 1); n   for (int i = 0; i < n + 1; ++i) { n     a1[i] = comp[a[i]]; n     a2[i] = comp[a[i] - k]; n     a3[i] = comp[a[i] + k]; n   } n   int q; n   cin >> q; n   vector<Query> qs(q); n   for (int i = 0; i < q; ++i) { n     cin >> qs[i].l >> qs[i].r; n     --qs[i].l; n     qs[i].id = i; n   } n   int sqrtn = sqrt(n + 1); n   sort(qs.begin(), qs.end(), [&](const Query &q1, const Query &q2) { n     return make_pair(q1.l / sqrtn, q1.r) < make_pair(q2.l / sqrtn, q2.r); n   }); n   int l = 0, r = -1; n   ll cur = 0; n   vector<ll> ans(q), oc(z); n   for (auto &query : qs) { n     while (l > query.l) { n       --l; n       cur += oc[a3[l]]; n       oc[a1[l]]++; n     } n     while (l < query.l) { n       oc[a1[l]]--; n       cur -= oc[a3[l]]; n       ++l; n     } n     while (r < query.r) { n       ++r; n       cur += oc[a2[r]]; n       oc[a1[r]]++; n     } n     while (r > query.r) { n       oc[a1[r]]--; n       cur -= oc[a2[r]]; n       --r; n     } n     ans[query.id] = cur; n   } n   for (auto x : ans) { n     cout << x <<   n ; n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int testcases = 1; n   if (MULTI_TESTCASES) cin >> testcases; n   while (testcases--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[102]; n int main() { n   int n, i, l, r, j; n   char name[100009]; n   cin >> n; n   scanf( %s , name); n   l = 0; n   r = 9; n   for (i = 0; i < n; i++) { n     if (name[i] ==  L ) n       for (j = 0; j < 10; j++) n         if (a[j] == 0) { n           a[j] = 1; n           break; n         } n     if (name[i] ==  R ) n       for (j = 9; j >= 0; j--) n         if (a[j] == 0) { n           a[j] = 1; n           break; n         } n     if (name[i] >=  0  && name[i] <=  9 ) a[name[i] -  0 ] = 0; n   } n   for (i = 0; i < 10; i++) printf( %d , a[i]); n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma optimize(  , off) n #pragma GCC optimize( Ofast,no-stack-protector,unroll-loops ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) n #pragma comment(linker,  /STACK:36777216 ) n const int mod = 1000000007; n using namespace std; n template <class T> n void smin(T& a, T val) { n   if (a > val) a = val; n } n template <class T> n void smax(T& a, T val) { n   if (a < val) a = val; n } n template <typename T> n inline std::ostream& operator<<(std::ostream& os, const std::vector<T>& v) { n   bool first = true; n   os <<  [ ; n   for (auto i : v) { n     if (!first) os <<  ,  ; n     os << i; n     first = false; n   } n   return os <<  ] ; n } n const int N = 1e3 + 10; n int n, cnt; n std::vector<pair<int, int> > v; n bool used[N]; n void dfs(int x) { n   used[x] = 1; n   for (int i = 0; i < n; ++i) { n     if (!used[i]) { n       if (v[x].first == v[i].first or v[x].second == v[i].second) { n         dfs(i); n       } n     } n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cin >> n; n   for (int i = 0; i < n; ++i) { n     int x, y; n     cin >> x >> y; n     v.push_back({x, y}); n   } n   for (int i = 0; i < n; ++i) { n     if (!used[i]) { n       dfs(i); n       cnt++; n     } n   } n   cout << max(cnt - 1, 0); n   return 0; n } n


#include <bits/stdc++.h> n const int N = 100000 + 1; n struct Point { n   int x, y; n   Point(int x = 0, int y = 0) : x(x), y(y) {} n   Point &operator-=(const Point &o) { n     x -= o.x; n     y -= o.y; n     return *this; n   } n   int quadrant() const { n     if (y != 0) { n       return y > 0; n     } n     return x < 0; n   } n }; n long long det(const Point &a, const Point &b) { n   return (long long)a.x * b.y - (long long)a.y * b.x; n } n bool operator<(const Point &a, const Point &b) { n   if (a.quadrant() == b.quadrant()) { n     return det(a, b) > 0; n   } n   return a.quadrant() < b.quadrant(); n } n Point operator-(Point a, const Point &b) { return a -= b; } n std::istream &operator>>(std::istream &in, Point &p) { n   return in >> p.x >> p.y; n } n int n, m; n Point points[N]; n int edge_count, first_edge[N], to[N << 1], flow[N << 1], next_edge[N << 1]; n void add_edge(int u, int v) { n   to[edge_count] = v; n   flow[edge_count] = 0; n   next_edge[edge_count] = first_edge[u]; n   first_edge[u] = edge_count++; n } n bool visit[N]; n int construct(int u) { n   int size = 1; n   visit[u] = true; n   for (int iter = first_edge[u]; iter != -1; iter = next_edge[iter]) { n     int v = to[iter]; n     if (!visit[v]) { n       int ret = construct(v); n       flow[iter] = -ret; n       flow[iter ^ 1] = -flow[iter]; n       size += ret; n     } n   } n   return size; n } n int cycle[N]; n std::map<std::pair<int, int>, int> edges; n std::vector<int> sums[N]; n int main() { n   std::cin >> n >> m; n   edge_count = 0; n   memset(first_edge, -1, sizeof(first_edge)); n   for (int i = 0; i < m; ++i) { n     int a, b; n     scanf( %d%d , &a, &b); n     a--, b--; n     add_edge(a, b); n     add_edge(b, a); n   } n   for (int i = 0; i < n; ++i) { n     std::cin >> points[i]; n   } n   int bottom_id = 0; n   for (int i = 0; i < n; ++i) { n     if (points[i].x < points[bottom_id].x) { n       bottom_id = i; n     } n   } n   points[n++] = points[bottom_id] - Point(1, 0); n   add_edge(bottom_id, n - 1); n   add_edge(n - 1, bottom_id); n   memset(visit, 0, sizeof(visit)); n   construct(n - 1); n   for (int u = 0; u < n; ++u) { n     std::vector<std::pair<Point, int> > order; n     for (int iter = first_edge[u]; iter != -1; iter = next_edge[iter]) { n       int v = to[iter]; n       order.push_back(std::make_pair(points[v] - points[u], iter)); n     } n     std::sort(order.begin(), order.end()); n     sums[u].push_back(0); n     for (int i = 0; i < (int)order.size(); ++i) { n       int iter = order[i].second; n       edges[std::make_pair(u, to[iter])] = i + 1; n       sums[u].push_back(sums[u].back() + flow[iter]); n     } n   } n   int q; n   std::cin >> q; n   while (q--) { n     int m; n     scanf( %d , &m); n     for (int i = 0; i < m; ++i) { n       scanf( %d , cycle + i); n       cycle[i]--; n     } n     long long area = 0; n     for (int i = 0; i < m; ++i) { n       area += det(points[cycle[i]], points[cycle[(i + 1) % m]]); n     } n     if (area < 0) { n       std::reverse(cycle, cycle + m); n     } n     int answer = 0; n     for (int i = 0; i < m; ++i) { n       int u = cycle[i]; n       int from = edges[std::make_pair(u, cycle[(i + m - 1) % m])]; n       int to = edges[std::make_pair(u, cycle[(i + 1) % m])]; n       if (from <= to) { n         answer += sums[u][to - 1]; n         answer -= sums[u][from]; n       } else { n         answer += sums[u][(int)sums[u].size() - 1]; n         answer -= sums[u][from]; n         answer += sums[u][to - 1]; n       } n     } n     printf( %d n , answer); n   } n   return 0; n } n


#include <bits/stdc++.h> n const double eps = 1e-9; n const double PI = acos(-1); n const long long MOD = 1e9 + 7; n using namespace std; n vector<string> powers; n pair<string, string> solution(string s) { n   string order; n   reverse(s.begin(), s.end()); n   map<char, int> mp; n   for (char c : s) { n     if (!mp[c]) { n       order.push_back(c); n     } n     mp[c]++; n   } n   int m = order.length(); n   int original_size = 0; n   for (int i = 0; i < m; i++) { n     original_size += mp[order[i]] / (m - i); n   } n   reverse(order.begin(), order.end()); n   return {string(s.rbegin(), s.rbegin() + original_size), order}; n } n string check(pair<string, string> p) { n   string result = p.first; n   for (auto c : p.second) { n     string tmp; n     for (auto d : p.first) { n       if (d != c) { n         tmp.push_back(d); n         result.push_back(d); n       } n     } n     p.first = tmp; n   } n   return result; n } n void solve() { n   string s; n   cin >> s; n   auto it = solution(s); n   auto res = check(it); n   if (res == s) { n     cout << it.first <<     << it.second << endl; n   } else { n     cout << -1 << endl; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   for (long long i = 1; i <= 2e18; i *= 2) { n     powers.push_back(to_string(i)); n   } n   long long t; n   cin >> t; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   string s; n   cin >> s; n   map<string, int> gram; n   for (int i = 0; i < n - 1; i++) ++gram[s.substr(i, 2)]; n   int cnt = 0; n   string ans =   ; n   for (auto e : gram) n     if (e.second > cnt) cnt = e.second, ans = e.first; n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = (int)1e4 + 10; n const int M = (int)1e9 + 7; n int n, k; n int mina, maxa; n void track() { n   for (int i = (1); i <= (n); i++) { n     for (int j = (1); j <= (n); j++) { n       if (i == j) n         cout << k; n       else n         cout << 0; n       if (j != n) cout <<    ; n     } n     cout << endl; n   } n   cout << endl; n } n int main() { n   cin >> n >> k; n   track(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 300005, N = 333333; n const int D = 1e9 + 7, inv2 = 5e8 + 4; n int n, Q, a[MAXN], mul[N + 233], inv[N + 233]; n int val[MAXN << 1]; n int id[MAXN], w[MAXN]; n int tim, len; n struct node { n   int v, id; n   node() {} n   node(int vv, int ii) { v = vv, id = ii; } n } s[MAXN << 1]; n bool operator<(const node &aa, const node &bb) { n   if (aa.v != bb.v) return aa.v < bb.v; n   return aa.id < bb.id; n } n int v0[MAXN << 4], v1[MAXN << 4], sum[MAXN << 4], cnt[MAXN << 4]; n void up(int p) { n   cnt[p] = cnt[p << 1] + cnt[p << 1 | 1]; n   sum[p] = (sum[p << 1] + sum[p << 1 | 1] + n             1ll * v0[p << 1] * v1[p << 1 | 1] % D * inv[cnt[p << 1]]) % n            D; n   v0[p] = (v0[p << 1] + 1ll * v0[p << 1 | 1] * mul[cnt[p << 1]]) % D; n   v1[p] = (v1[p << 1] + 1ll * v1[p << 1 | 1] * inv[cnt[p << 1]]) % D; n } n void insert(int p, int l, int r, int x) { n   if (l == r) { n     cnt[p] = 1, sum[p] = 0; n     v0[p] = val[x]; n     v1[p] = 1ll * val[x] * inv[1] % D; n     return; n   } n   int mid = (l + r) >> 1; n   if (x <= mid) n     insert(p << 1, l, mid, x); n   else n     insert(p << 1 | 1, mid + 1, r, x); n   up(p); n } n void remove(int p, int l, int r, int x) { n   if (l == r) return (void)(cnt[p] = sum[p] = v0[p] = v1[p] = 0); n   int mid = (l + r) >> 1; n   if (x <= mid) n     remove(p << 1, l, mid, x); n   else n     remove(p << 1 | 1, mid + 1, r, x); n   up(p); n } n int main() { n   mul[0] = inv[0] = 1; n   for (int i = 1; i <= N; i++) mul[i] = mul[i - 1] * 2 % D; n   for (int i = 1; i <= N; i++) inv[i] = 1ll * inv[i - 1] * inv2 % D; n   scanf( %d , &n); n   tim = -n; n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]), s[++len] = node(a[i], ++tim); n   scanf( %d , &Q); n   for (int i = 1; i <= Q; i++) { n     scanf( %d%d , &id[i], &w[i]); n     s[++len] = node(w[i], ++tim); n   } n   sort(s + 1, s + len + 1); n   for (int i = 1; i <= len; i++) { n     val[i] = s[i].v; n     if (s[i].id <= 0) n       a[s[i].id + n] = i; n     else n       w[s[i].id] = i; n   } n   for (int i = 1; i <= n; i++) insert(1, 1, len, a[i]); n   printf( %d n , sum[1]); n   for (int i = 1; i <= Q; i++) { n     remove(1, 1, len, a[id[i]]); n     insert(1, 1, len, w[i]); n     a[id[i]] = w[i]; n     printf( %d n , sum[1]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5; n vector<int> g[N]; n int mn[N]; n int mx[N]; n int ans; n void dfs(int u, int p) { n   bool leaf = true; n   mn[u] = 1e9; n   mx[u] = -1e9; n   for (int v : g[u]) n     if (v != p) { n       dfs(v, u); n       leaf = false; n       mn[u] = min(mn[u], mn[v] + 1); n       mx[u] = max(mx[u], mx[v] + 1); n     } n   if (leaf) { n     mn[u] = 0; n     mx[u] = 0; n   } n } n void dfs2(int u, int p) { n   const int m = g[u].size(); n   bool ok = true; n   vector<int> hh; n   for (int v : g[u]) { n     if (mx[v] != mn[v]) ok = false; n     hh.push_back(mx[v] + 1); n   } n   if (ok) { n     sort(hh.begin(), hh.end()); n     hh.erase(unique(hh.begin(), hh.end()), hh.end()); n     if (hh.size() <= 2) { n       int len = 0; n       for (int e : hh) len += e; n       while (len % 2 == 0) len /= 2; n       ans = min(ans, len); n     } n   } n   vector<int> maxl(m); n   vector<int> maxr(m); n   vector<int> minl(m); n   vector<int> minr(m); n   maxl[0] = -1e9; n   minl[0] = 1e9; n   maxr[m - 1] = -1e9; n   minr[m - 1] = 1e9; n   for (int i = 1; i < m; i++) { n     maxl[i] = max(maxl[i - 1], mx[g[u][i - 1]] + 1); n     minl[i] = min(minl[i - 1], mn[g[u][i - 1]] + 1); n   } n   for (int i = m - 2; i >= 0; i--) { n     maxr[i] = max(maxr[i + 1], mx[g[u][i + 1]] + 1); n     minr[i] = min(minr[i + 1], mn[g[u][i + 1]] + 1); n   } n   for (int i = 0; i < m; i++) { n     int v = g[u][i]; n     if (v == p) continue; n     mn[u] = min(minl[i], minr[i]); n     mx[u] = max(maxl[i], maxr[i]); n     dfs2(v, u); n   } n } n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n - 1; i++) { n     int u, v; n     scanf( %d %d , &u, &v); n     u--; n     v--; n     g[u].push_back(v); n     g[v].push_back(u); n   } n   if (n == 2) { n     cout << 1 << endl; n     return 0; n   } n   for (int i = 0; i < n; i++) { n     if (g[i].size() >= 2) { n       ans = 1e9; n       dfs(i, -1); n       dfs2(i, -1); n       if (ans >= 1e8) ans = -1; n       cout << ans << endl; n       return 0; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n bool found = false; n int b, A[10000], orig_a; n int main() { n   int a, b[3], i, j, n, x, y, z, array[100000]; n   cin >> n; n   for (i = 0; i < 3; i++) { n     if (i <= 1) { n       cout <<  ?   << 1 <<     << i + 2 << endl; n     } else { n       cout <<  ?   << 2 <<     << 3 << endl; n     } n     fflush(stdout); n     cin >> b[i]; n   } n   z = -(b[0] - b[1] - b[2]) / 2; n   y = b[2] - z; n   x = b[0] - y; n   array[0] = x; n   array[1] = y; n   array[2] = z; n   for (i = 4; i <= n; i++) { n     cout <<  ?   << 1 <<     << i << endl; n     fflush(stdout); n     cin >> a; n     array[i - 1] = a - x; n   } n   cout <<  ! ; n   for (i = 0; i < n; i++) { n     cout <<     << array[i]; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int max_n = 1111111, inf = 1000000007; n int n, k, a[max_n], pw[max_n], used[max_n]; n int main() { n   cin >> n >> k; n   if (k == 0) { n     long long ans = 1; n     for (int i = 1; i < n; ++i) { n       ans = (ans * n) % inf; n     } n     cout << ans << endl; n     return 0; n   } n   if (k == 1) { n     long long ans = 1; n     for (int i = 0; i < n; ++i) { n       ans = (ans * n) % inf; n     } n     cout << ans << endl; n     return 0; n   } n   pw[0] = 1; n   for (int i = 1; i < n; ++i) { n     pw[i] = (1LL * pw[i - 1] * k) % n; n   } n   for (int i = 0; i < n; ++i) { n     a[i] = (1LL * i * k) % n; n   } n   long long ans = 1; n   for (int i = 0; i < n; ++i) { n     if (used[i] == 0) { n       int cnt = 1, v = i, last; n       while (used[v] == 0) { n         used[v] = cnt; n         ++cnt; n         last = v; n         v = a[v]; n       } n       cnt = used[last] - used[v] + 1; n       if (pw[cnt] == 1) { n         ans = (ans * n) % inf; n       } n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   map<char, int> mp; n   for (int i = 0; i < s.length(); i++) { n     mp[s[i]]++; n   } n   int mn; n   set<int> st; n   set<int>::iterator it; n   st.insert(mp[ B ]); n   st.insert(mp[ u ] / 2); n   st.insert(mp[ l ]); n   st.insert(mp[ b ]); n   st.insert(mp[ a ] / 2); n   st.insert(mp[ s ]); n   st.insert(mp[ r ]); n   it = st.begin(); n   cout << *it; n } n


#include <bits/stdc++.h> n using namespace std; n char buf[11]; n const int MAXC = 20; n double C[MAXC][MAXC + 1]; n void count(int &sum, int &q) { n   char buf[11]; n   cin >> buf; n   sum = 0; n   q = 0; n   for (char *p = buf; *p; ++p) { n     switch (*p) { n       case  + : n         ++sum; n         break; n       case  - : n         --sum; n         break; n       case  ? : n         ++q; n     } n   } n } n void init_C() { n   C[0][0] = 1.0; n   for (int i = 1; i < MAXC; ++i) { n     C[i][0] = C[i][i] = 1.0; n     for (int j = 1; j < i; ++j) C[i][j] = C[i - 1][j - 1] + C[i - 1][j]; n   } n } n int main() { n   init_C(); n   int sum1, sum2, quest; n   count(sum1, quest); n   count(sum2, quest); n   int diff = sum1 - sum2; n   int t = (quest + diff) / 2; n   if (abs(diff) > quest || (quest + diff) % 2) { n     cout <<  0.0 n ; n   } else { n     cout << setprecision(12) << C[quest][t] / (1 << quest) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Point { n   long double x, y; n   Point(){}; n   long double dist(Point b) { n     return sqrt((b.x - x) * (b.x - x) + (b.y - y) * (b.y - y)); n   } n }; n int main() { n   Point a, b, c; n   cin >> a.x >> a.y >> b.x >> b.y >> c.x >> c.y; n   long double k = a.dist(b); n   long double l = c.dist(b); n   if (k == l && k + l != a.dist(c)) n     cout <<  YES ; n   else n     cout <<  NO ; n } n


#include <bits/stdc++.h> n int main() { n   int days, time, count, free; n   int x = 0; n   scanf( %d , &days); n   scanf( %d , &time); n   int busyTime[days]; n   for (x = 0; x < days; x++) { n     scanf( %d , &busyTime[x]); n   } n   count = 0; n   while (time > 0) { n     free = 86400 - busyTime[count]; n     time -= free; n     count++; n   } n   printf( %d , count); n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 2000000000; n const int N = 1e5 + 5; n const int TN = 1 << 18; n const double PI = acos(-1); n int n, m, k; n long long tree[TN], lazy[TN] = {0}; n void probagate(int l, int r, int p) { n   tree[p] |= lazy[p]; n   if (l != r) { n     lazy[p << 1] |= lazy[p]; n     lazy[(p << 1) | 1] |= lazy[p]; n   } n   lazy[p] = 0; n } n void update(int val, int ql, int qr, int l = 0, int r = n - 1, int p = 1) { n   probagate(l, r, p); n   if (ql > r || qr < l) return; n   if (ql <= l && r <= qr) { n     lazy[p] |= val; n     probagate(l, r, p); n     return; n   } n   int m = (l + r) >> 1; n   update(val, ql, qr, l, m, (p << 1)); n   update(val, ql, qr, m + 1, r, (p << 1) | 1); n   tree[p] = tree[(p << 1)] & tree[(p << 1) | 1]; n } n int get(int ql, int qr, int l = 0, int r = n - 1, int p = 1) { n   probagate(l, r, p); n   if (ql > r || qr < l) return (1LL << 31) - 1; n   if (ql <= l && r <= qr) { n     return tree[p]; n   } n   int m = (l + r) >> 1; n   return get(ql, qr, l, m, (p << 1)) & get(ql, qr, m + 1, r, (p << 1) | 1); n } n void print(int l = 0, int r = n - 1, int p = 1) { n   probagate(l, r, p); n   if (l == r) { n     printf( %d  , tree[p]); n     return; n   } n   int m = (l + r) >> 1; n   print(l, m, (p << 1)); n   print(m + 1, r, (p << 1) | 1); n } n int main() { n   scanf( %d%d , &n, &m); n   vector<pair<pair<int, int>, int>> v; n   for (int i = 0; i < m; i++) { n     int a, b, c; n     scanf( %d%d%d , &a, &b, &c); n     a--; n     b--; n     v.push_back({{a, b}, c}); n     update(c, a, b); n   } n   for (int i = 0; i < m; i++) { n     int a = v[i].first.first; n     int b = v[i].first.second; n     int c = v[i].second; n     if (get(a, b) != c) { n       return puts( NO ); n     } n   } n   puts( YES ); n   print(); n } n


#include <bits/stdc++.h> n using namespace std; n struct point { n   int i; n   int j; n }; n int main() { n   vector<char> s; n   int n, m; n   char c; n   char a[102][102]; n   point first; n   first.i = -1; n   point last; n   memset(a, 0, sizeof(a)); n   scanf( %d %d %c , &n, &m, &c); n   getchar(); n   for (int i = 0; i < n + 1; i++) { n     a[i][0] = c; n     a[i][m + 1] = c; n   } n   for (int i = 0; i < m + 1; i++) { n     a[0][i] = c; n     a[n + 1][i] = c; n   } n   for (int i = 1; i < n + 1; i++) { n     for (int j = 1; j < m + 1; j++) { n       scanf( %c , &c); n       a[i][j] = c; n       if (c == a[0][0]) { n         if (first.i < 0) { n           first.i = i; n           first.j = j; n         } n         last.i = i; n         last.j = j; n       } n     } n     getchar(); n   } n   int can; n   for (int i = first.i; i < last.i + 1; i++) { n     for (int j = first.j; j < last.j + 1; j++) { n       can = count(s.begin(), s.end(), a[i][j]); n       if (a[i - 1][j] !=  .  && count(s.begin(), s.end(), a[i - 1][j]) == 0) n         s.push_back(a[i - 1][j]); n       if (a[i][j - 1] !=  .  && count(s.begin(), s.end(), a[i][j - 1]) == 0) n         s.push_back(a[i][j - 1]); n       if (a[i][j + 1] !=  .  && count(s.begin(), s.end(), a[i][j + 1]) == 0) n         s.push_back(a[i][j + 1]); n       if (a[i + 1][j] !=  .  && count(s.begin(), s.end(), a[i + 1][j]) == 0) n         s.push_back(a[i + 1][j]); n     } n   } n   if (s.size()) n     printf( %d n , s.size() - 1); n   else n     printf( %d n , s.size()); n   return 0; n } n


#include <bits/stdc++.h> n #pragma optimize( O3 ) n using namespace std; n const long long MOD = 1e9 + 7; n const long long INF = 1e9 + 7; n const int base = 2e5 + 1; n const int MAX = 1e5 + 2; n const double EPS = 1e-9; n const double PI = acos(-1.); n const int MAXN = 1e6 + 47; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n int main() { n   int n; n   cin >> n; n   int cnt = 0; n   vector<int> res; n   set<int> was, is; n   for (long long i = (0); i < (n); ++i) { n     int a; n     cin >> a; n     if (a < 0) { n       if (is.count(abs(a))) { n         is.erase(abs(a)); n         cnt--; n       } else { n         cout << -1; n         return 0; n       } n     } else { n       if (was.count(a) || is.count(a)) { n         cout << -1; n         return 0; n       } else { n         was.insert(a); n         is.insert(a); n         cnt++; n       } n     } n     if (cnt == 0) { n       res.push_back(i + 1); n       was.clear(); n     } n   } n   if (cnt != 0) { n     cout << -1; n     return 0; n   } n   cout << (int)res.size() <<   n ; n   int last = 0; n   for (long long i = (0); i < ((int)res.size()); ++i) { n     cout << res[i] - last <<    ; n     last = res[i]; n   } n   cerr <<   nTime elapsed:   << clock() / (double)CLOCKS_PER_SEC <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   int s = 0; n   int a[5000]; n   cin >> n; n   int i; n   for (i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   for (i = 1; i <= n; i++) { n     if (a[i] == a[a[a[a[i]]]]) s = s + 1; n   } n   if (s != 0) { 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   ios_base::sync_with_stdio(false); n   int n, k; n   cin >> n >> k; n   vector<pair<int, int>> a(n * 2); n   vector<pair<int, int>> ans; n   for (int i = 0; i < n; i++) { n     cin >> a[i].first >> a[n + i].first; n     a[n + i].second = 1; n   } n   sort(a.begin(), a.end()); n   int z = 0; n   for (int i = 0; i < n * 2; i++) n     if (a[i].second == 0) { n       z++; n       if (z == k) ans.push_back({a[i].first, 0}); n     } else { n       if (z == k) ans.back().second = a[i].first; n       z--; n     } n   cout << ans.size() <<   n ; n   for (int i = 0; i < ans.size(); i++) n     cout << ans[i].first <<     << ans[i].second <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n using namespace std; n using ll = long long; n using ld = long double; n using pi = pair<int, int>; n using pl = pair<ll, ll>; n using vi = vector<int>; n using vl = vector<ll>; n using vpi = vector<pi>; n using si = set<int>; n using sl = set<ll>; n using qi = queue<int>; n using ql = queue<ll>; n template <class T> n bool uin(T& a, T b) { n   return a > b ? (a = b, true) : false; n } n template <class T> n bool uax(T& a, T b) { n   return a < b ? (a = b, true) : false; n } n template <class T> n bool uin(T& u, T& v, T a, T b) { n   return v - u > b - a ? (u = a, v = b, true) : false; n } n template <class T> n bool uax(T& u, T& v, T a, T b) { n   return v - u < b - a ? (u = a, v = b, true) : false; n } n namespace input { n template <class T> n void re(complex<T>& x); n template <class T1, class T2> n void re(pair<T1, T2>& p); n template <class T> n void re(vector<T>& a); n template <class T, size_t SZ> n void re(array<T, SZ>& a); n template <class T> n void re(T& x) { n   cin >> x; n } n void re(double& x) { n   string t; n   re(t); n   x = stod(t); n } n void re(ld& x) { n   string t; n   re(t); n   x = stold(t); n } n template <class T, class... Ts> n void re(T& t, Ts&... ts) { n   re(t); n   re(ts...); n } n template <class T> n void re(complex<T>& x) { n   T a, b; n   re(a, b); n   x = cd(a, b); n } n template <class T1, class T2> n void re(pair<T1, T2>& p) { n   re(p.f, p.s); n } n template <class T> n void re(vector<T>& a) { n   for (int i = 0; i < (((int)(a).size())); ++i) re(a[i]); n } n template <class T, size_t SZ> n void re(array<T, SZ>& a) { n   for (int i = 0; i < (SZ); ++i) re(a[i]); n } n }  // namespace input n using namespace input; n namespace output { n void pr(int x) { cout << x; } n void pr(long x) { cout << x; } n void pr(ll x) { cout << x; } n void pr(unsigned x) { cout << x; } n void pr(unsigned long x) { cout << x; } n void pr(unsigned long long x) { cout << x; } n void pr(float x) { cout << x; } n void pr(double x) { cout << x; } n void pr(ld x) { cout << x; } n void pr(char x) { cout << x; } n void pr(const char* x) { cout << x; } n void pr(const string& x) { cout << x; } n void pr(bool x) { pr(x ?  true  :  false ); } n template <class T> n void pr(const complex<T>& x) { n   cout << x; n } n template <class T1, class T2> n void pr(const pair<T1, T2>& x); n template <class T> n void pr(const T& x); n template <class T, class... Ts> n void pr(const T& t, const Ts&... ts) { n   pr(t); n   pr(ts...); n } n template <class T1, class T2> n void pr(const pair<T1, T2>& x) { n   pr( { , x.f,  ,  , x.s,  } ); n } n template <class T> n void pr(const T& x) { n   pr( { ); n   bool fst = 1; n   for (const auto& a : x) pr(!fst ?  ,   :   , a), fst = 0; n   pr( } ); n } n void ps() { pr(  n ); } n template <class T, class... Ts> n void ps(const T& t, const Ts&... ts) { n   pr(t); n   if (sizeof...(ts)) pr(   ); n   ps(ts...); n } n void pc() { pr( ] n ); } n template <class T, class... Ts> n void pc(const T& t, const Ts&... ts) { n   pr(t); n   if (sizeof...(ts)) pr( ,  ); n   pc(ts...); n } n }  // namespace output n using namespace output; n inline int bs(bool (*valid)(int), int l, int r, bool order) { n   l--, r++; n   if (!order) swap(l, r); n   while (abs(r - l) > 1) { n     int mid = (l + r) >> 1; n     if (valid(mid)) n       r = mid; n     else n       l = mid; n   } n   valid(r); n   return r; n } n struct dsu { n   vector<int> p; n   dsu(int n) { p.resize(n + 1); } n   inline int get(int x) { return p[x] ? p[x] = get(p[x]) : x; } n   inline bool mrg(int x, int y) { n     return get(x) == get(y) ? false : p[get(x)] = get(y); n   } n }; n const int M = 1e9 + 7; n const ll lnf = 1e18 + 3; n const int inf = 1e9 + 3; n const int nax = 1e6 + 5; n typedef decay<decltype(M)>::type T; n struct mi { n   T val; n   explicit operator T() const { return val; } n   mi() { val = 0; } n   mi(const ll& v) { n     val = (-M <= v && v <= M) ? v : v % M; n     if (val < 0) val += M; n   } n   friend void pr(const mi& a) { pr(a.val); } n   friend void re(mi& a) { n     ll x; n     re(x); n     a = mi(x); n   } n   friend bool operator==(const mi& a, const mi& b) { return a.val == b.val; } n   friend bool operator!=(const mi& a, const mi& b) { return !(a == b); } n   friend bool operator<(const mi& a, const mi& b) { return a.val < b.val; } n   mi operator-() const { return mi(-val); } n   mi& operator+=(const mi& m) { n     if ((val += m.val) >= M) val -= M; n     return *this; n   } n   mi& operator-=(const mi& m) { n     if ((val -= m.val) < 0) val += M; n     return *this; n   } n   mi& operator*=(const mi& m) { n     val = (ll)val * m.val % M; n     return *this; n   } n   friend mi pow(mi a, ll p) { n     mi ans = 1; n     assert(p >= 0); n     for (; p; p /= 2, a *= a) n       if (p & 1) ans *= a; n     return ans; n   } n   friend mi inv(const mi& a) { n     assert(a != 0); n     return pow(a, M - 2); n   } n   mi& operator/=(const mi& m) { return (*this) *= inv(m); } n   friend mi operator+(mi a, const mi& b) { return a += b; } n   friend mi operator-(mi a, const mi& b) { return a -= b; } n   friend mi operator*(mi a, const mi& b) { return a *= b; } n   friend mi operator/(mi a, const mi& b) { return a /= b; } n }; n vi invs, fac, ifac; n void genFac(int SZ) { n   invs.resize(SZ), fac.resize(SZ), ifac.resize(SZ); n   invs[1] = fac[0] = ifac[0] = 1; n   for (int i = (2); i <= (SZ - 1); ++i) n     invs[i] = M - (ll)M / i * invs[M % i] % M; n   for (int i = (1); i <= (SZ - 1); ++i) { n     fac[i] = (ll)fac[i - 1] * i % M; n     ifac[i] = (ll)ifac[i - 1] * invs[i] % M; n   } n } n ll comb(int a, int b) { n   if (a < b || b < 0) return 0; n   return (ll)fac[a] * ifac[b] % M * ifac[a - b] % M; n } n ll p[nax], s, c; n int t, n, q[nax]; n int main() { n   ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); n   re(n); n   vl a(n); n   re(a); n   for (int i = 0; i < (n); ++i) { n     s = a[i], c = 1; n     while (t && p[t] * c >= s * q[t]) s += p[t], c += q[t--]; n     p[++t] = s, q[t] = c; n   } n   cout << setprecision(10); n   for (int i = (1); i <= (t); ++i) n     for (int j = 0; j < (q[i]); ++j) cout << (double)p[i] / q[i] <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n map<string, int> mp; n int main() { n   int n, m; n   string s; n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     cin >> s; n     mp[s] = 1; n   } n   int con = 0; n   for (int i = 0; i < m; i++) { n     cin >> s; n     if (mp[s] == 1) { n       con++; n     } n   } n   m -= con; n   n -= con; n   if (con & 1) { n     n++; n   } n   if (n > m) { 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   long long t, a, b, c; n   cin >> t; n   while (t--) { n     cin >> a >> b >> c; n     if (a == c) { n       cout <<  -1   << b <<   n ; n     } else if (a > c) { n       cout <<  -1 1 n ; n     } else { n       double x = (double)c / b; n       double y = (double)a; n       if (x < y) n         cout <<  1   << b <<   n ; n       else n         cout <<  1 -1 n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<vector<int>> adj, CC; n const int MX = 1e5 + 69; n int n, m, q, vis[MX], dis[MX], pa[MX], compsz[MX], D[MX]; n int parent(int x) { n   if (x == pa[x]) return x; n   return pa[x] = parent(pa[x]); n } n bool cmp(int a, int b) { n   if (dis[a] != dis[b]) return dis[a] < dis[b]; n   return a < b; n } n long long* cum[MX]; n int* compel[MX]; n int bfs(int x, bool ok) { n   int p = parent(x); n   queue<int> q; n   q.push(x); n   vis[x] = 0; n   vector<int> v; n   while (!q.empty()) { n     int nxt = q.front(); n     v.push_back(nxt); n     q.pop(); n     for (auto u : adj[nxt]) { n       if (vis[u] == -1) { n         vis[u] = vis[nxt] + 1; n         dis[u] = max(dis[u], vis[u]); n         q.push(u); n         D[p] = max(D[p], dis[u]); n       } n     } n   } n   int mx = -69, opt = x; n   while (!v.empty() && ok == 1) { n     int nxt = v.back(); n     v.pop_back(); n     if (mx < vis[nxt]) { n       mx = vis[nxt]; n       opt = nxt; n     } n     vis[nxt] = -1; n   } n   if (ok) return opt; n   sort(v.begin(), v.end(), cmp); n   int sz = v.size(), j = 0; n   long long sum = 0; n   p = parent(x); n   compel[p] = new int[D[p] + 2]; n   cum[p] = new long long[D[p] + 2]; n   for (int i = 0; i <= D[p] + 1; i++) cum[p][i] = compel[p][i] = 0; n   for (auto u : v) { n     j++; n     sum += dis[u]; n     cum[p][dis[u] + 1] = sum; n     compel[p][dis[u] + 1] = j; n   } n   compsz[p] = v.size(); n   CC[p] = v; n   v.clear(); n   return opt; n } n long double nig[MX]; n void join(int u, int v) { n   u = parent(u); n   v = parent(v); n   if (u == v) return; n   pa[u] = v; n } n map<int, long double> quer[MX]; n int DD; n long double solve(int u, int v) { n   if (compsz[u] > compsz[v]) swap(u, v); n   if (quer[u].count(v)) return quer[u][v]; n   DD = max(D[u], D[v]); n   long double ret = 0; n   for (int i = 0; i < compsz[u]; i++) { n     int nxt = CC[u][i]; n     int l = min(D[v], max(-1, DD - dis[nxt] - 2)); n     long double r = compsz[v] - compel[v][l + 1]; n     ret += r * (dis[nxt] + 1) + cum[v][D[v] + 1] - cum[v][l + 1] + n            (long double)compel[v][l + 1] * DD; n   } n   return quer[u][v] = ret / ((long double)compsz[u] * compsz[v]); n } n int main() { n   cin >> n >> m >> q; n   for (int i = 1; i <= n; i++) pa[i] = i; n   memset(vis, -1, sizeof vis); n   adj.resize(n + 69); n   CC.resize(n + 69); n   for (int i = 1; i <= m; i++) { n     int u, v; n     scanf( %d%d , &u, &v); n     adj[u].push_back(v); n     adj[v].push_back(u); n     join(u, v); n   } n   for (int i = 1; i <= n; i++) { n     if (i != parent(i)) continue; n     int p = parent(i); n     int u = bfs(i, 1); n     u = bfs(u, 1); n     bfs(u, 0); n   } n   cout << fixed; n   cout.precision(10); n   for (int i = 1; i <= q; i++) { n     int u, v; n     scanf( %d%d , &u, &v); n     u = parent(u); n     v = parent(v); n     if (u == v) n       cout << -1 << endl; n     else n       cout << solve(u, v) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   double x1, y1, x2, y2, x0, y0; n   while (scanf( %lf%lf%lf%lf%lf%lf , &x0, &y0, &x1, &y1, &x2, &y2) != EOF) { n     double area = x0 * y1 + x2 * y0 + x1 * y2 - x2 * y1 - x0 * y2 - x1 * y0; n     if (area == 0) { n       printf( TOWARDS n ); n     } else if (area > 0) { n       printf( LEFT n ); n     } else { n       printf( RIGHT n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 200 * 1000 + 1; n struct Seg { n   int s, e, Id; n   friend bool operator<(Seg a, Seg b) { return a.s < b.s; } n   friend bool operator>(Seg a, Seg b) { n     return (a.e != b.e ? a.e > b.e : a.Id > b.Id); n   } n } a[MAX + 10]; n set<Seg, greater<Seg> > s; n bool mrk[MAX + 10]; n int cnt = 0; n int n, m; n int main() { n   ios::sync_with_stdio(false); n   cin >> n >> m; n   for (int i = 0; i < m; i++) n     cin >> a[i].s >> a[i].e, a[i].Id = i, a[i].e += a[i].s; n   sort(a, a + m); n   int p = 0; n   int next = 0; n   for (int i = 1; i <= n; i++) { n     while (p < m && a[p].s == i) s.insert(a[p++]); n     if (next <= i && !s.empty() && s.begin()->e > i) { n       mrk[s.begin()->Id] = true; n       next = s.begin()->e; n       s.erase(s.begin()); n       cnt++; n     } n   } n   cout << m - cnt << endl; n   for (int i = 0; i < m; i++) n     if (!mrk[i]) cout << i + 1 <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, s; n const int N = 1e5 + 10; n int v[N]; n int ans; n bool check(int x, int &res) { n   int k[n]; n   for (int i = 0; i < n; i++) { n     k[i] = v[i] + (i + 1) * x; n   } n   sort(k, k + n); n   for (int i = 0; i < x; i++) n     if (res <= s) res += k[i]; n   return res <= s; n } n int main() { n   cin >> n >> s; n   for (int i = 0; i < n; i++) { n     cin >> v[i]; n   } n   int hi = n + 1; n   int lo = 0; n   for (int i = 0; i < 100; i++) { n     int mid = (hi + lo) >> 1; n     int valor = 0; n     if (check(mid, valor)) { n       ans = valor; n       lo = mid; n     } else n       hi = mid; n   } n   cout << lo <<     << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string sol; n const int kBufferSize = 10000; n int BufferInd = kBufferSize; n char Buffer[kBufferSize]; n void cit(int &n) { n   ((++BufferInd >= kBufferSize) ? (cin.read(Buffer, kBufferSize), BufferInd = 0) n                                 : (0)); n   for (; not((((Buffer[BufferInd]) >=  0  and (Buffer[BufferInd]) <=  9 ) or n               (Buffer[BufferInd]) ==  - ) n                  ? (1) n                  : (0)); n        ((++BufferInd >= kBufferSize) n             ? (cin.read(Buffer, kBufferSize), BufferInd = 0) n             : (0))) n     ; n   bool sign = false; n   if ((Buffer[BufferInd]) ==  - ) { n     ((++BufferInd >= kBufferSize) n          ? (cin.read(Buffer, kBufferSize), BufferInd = 0) n          : (0)); n     sign = true; n   } n   n = 0; n   for (; ((((Buffer[BufferInd]) >=  0  and (Buffer[BufferInd]) <=  9 ) or n            (Buffer[BufferInd]) ==  - ) n               ? (1) n               : (0)); n        ((++BufferInd >= kBufferSize) n             ? (cin.read(Buffer, kBufferSize), BufferInd = 0) n             : (0))) { n     n *= 10; n     n += (Buffer[BufferInd]) -  0 ; n   } n   if (sign) n *= -1; n } n int main() { n   std::ios_base::sync_with_stdio(false); n   int n, sum = 0; n   cin >> n; n   for (int i = 1; i <= n; ++i) n     for (int j = 1; j <= n; ++j) { n       int x; n       cin >> x; n       if (i == j) sum ^= x; n     } n   int q; n   cin >> q; n   while (q--) { n     int t; n     cin >> t; n     if (t == 1 or t == 2) { n       int x; n       cin >> x; n       sum ^= 1; n     } else { n       sol +=  0  + sum; n     } n   } n   cout << sol <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool func(pair<int, int> &a, pair<int, int> &b) { return a.first < b.first; } n int main() { n   int n, q; n   vector<pair<int, int>> arr; n   int input1, input2; n   int i, j; n   int level[5001] = { n       0, n   }; n   priority_queue<int, vector<int>, greater<int>> exit; n   int curlev = 1; n   int cur, prev = 0; n   int acc1[5001], acc2[5001]; n   pair<int, int> interv1, interv2, temp; n   int over1, over2; n   int last = 0, sum; n   int cand; n   int lmax = 0; n   scanf( %d %d , &n, &q); n   for (i = 0; i < q; i++) { n     scanf( %d %d , &input1, &input2); n     arr.push_back(pair<int, int>(input1, input2)); n     for (j = input1; j <= input2; j++) { n       level[j]++; n     } n   } n   sort(arr.begin(), arr.end(), func); n   sum = n; n   for (i = 0; i < q; i++) { n     if (arr[i].first > prev + 1) { n       sum -= arr[i].first - (prev + 1); n     } n     if (arr[i].second > prev) { n       prev = arr[i].second; n     } n   } n   sum -= n - prev; n   acc1[0] = acc2[0] = 0; n   for (i = 1; i <= n; i++) { n     acc1[i] = acc1[i - 1] + (level[i] == 1); n     acc2[i] = acc2[i - 1] + (level[i] == 2); n   } n   for (i = 0; i < q - 1; i++) { n     for (j = i + 1; j < q; j++) { n       interv1 = arr[i]; n       interv2 = arr[j]; n       over1 = 1; n       over2 = 0; n       if (interv2.first <= interv1.second) { n         over1 = interv2.first; n         if (interv2.second < interv1.second) { n           over2 = interv2.second; n         } else { n           over2 = interv1.second; n         } n       } n       cand = sum - n              ((acc1[interv1.second] - acc1[interv1.first - 1]) + n               (acc1[interv2.second] - acc1[interv2.first - 1])) - n              (acc2[over2] - acc2[over1 - 1]); n       if (cand > lmax) { n         lmax = cand; n       } n     } n   } n   printf( %d n , lmax); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using lli = long long int; n int main() { n   int n; n   cin >> n; n   if (n == 2) { n     cout << 1 << endl << 1 <<     << 1 << endl; n     return 0; n   } else if (n == 3) { n     cout <<  0 n2 1 2 n ; n   } else if (n == 4) { n     cout <<  0 n2 1 4 n ; n   } else { n     vector<int> v; n     vector<int> c; n     lli sum_v = 0, sum_c = 0; n     for (lli i = n; i >= 1; i--) { n       if (abs(sum_v + i - sum_c) < abs(sum_c + i - sum_v)) { n         sum_v += i; n         v.push_back(i); n       } else { n         sum_c += i; n         c.push_back(i); n       } n     } n     cout << abs(sum_v - sum_c) << endl; n     cout << c.size() <<    ; n     int in = c.size(); n     for (lli i = 0; i < (in); i++) { n       cout << c[i]; n       if (i == in - 1) n         cout << endl; n       else n         cout <<    ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long int cnt; n long long int v[100005], lft[100005], rgt[100005], rt[100005]; n std::map<long long int, bool> M; n void check(long long int i, long long int left, long long int right) { n   if (v[i] <= left && v[i] >= right) { n     M[v[i]] = true; n   } n   if (lft[i] != -1) { n     long long int left1 = min(left, v[i]); n     check(lft[i], left1, right); n   } n   if (rgt[i] != -1) { n     long long int right1 = max(right, v[i]); n     check(rgt[i], left, right1); n   } n } n int main() { n   ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); n   ; n   long long int n, root; n   cin >> n; n   for (long long int i = 1; i <= n; i++) { n     cin >> v[i] >> lft[i] >> rgt[i]; n   } n   cnt = 0; n   for (long long int i = 1; i <= n; i++) { n     if (lft[i] != -1) rt[lft[i]] = 1; n     if (rgt[i] != -1) rt[rgt[i]] = 1; n   } n   for (long long int i = 1; i <= n; i++) { n     if (rt[i] == 0) { n       root = i; n       break; n     } n   } n   long long int left = INT_MAX; n   long long int right = INT_MIN; n   check(root, left, right); n   for (long long int i = 1; i <= n; i++) cnt += !M[v[i]]; n   cout << cnt << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename Arg1> n void __f(const char* name, Arg1&& arg1) { n   cout << name <<   :   << arg1 << std::endl; n } n template <typename Arg1, typename... Args> n void __f(const char* names, Arg1&& arg1, Args&&... args) { n   const char* comma = strchr(names + 1,  , ); n   cout.write(names, comma - names) <<   :   << arg1 <<   |  ; n   __f(comma + 1, args...); n } n const int MAXN = 100005; n const int SQRTN = 1003; n const int LOGN = 22; n const double PI = acos(-1); n const int INF = 1e9; n const int MOD = 1000000007; n const int FMOD = 998244353; n const double eps = 1e-8; n void solvethetestcase(); n signed main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t = 1; n   for (int testcase = 1; testcase < t + 1; testcase++) { n     solvethetestcase(); n   } n } n int n, m; n vector<pair<int, int> > adj[MAXN]; n bitset<MAXN> vis; n double ans[MAXN]; n bool flag = 1; n int par[MAXN], dep[MAXN]; n int pw[MAXN]; n int sm[MAXN]; n map<int, int> freq; n vector<int> cc; n void dfs(int u, double val) { n   ans[u] = val; n   for (auto v : adj[u]) { n     if (par[v.first] == u and v.first != u) { n       dfs(v.first, v.second - val); n     } n   } n } n void bfs(int root) { n   dep[root] = 0; n   par[root] = root; n   sm[root] = 0; n   queue<int> q; n   q.push(root); n   while (q.size()) { n     int u = q.front(); n     q.pop(); n     vis[u] = 1; n     cc.push_back(u); n     for (auto v : adj[u]) { n       if (dep[v.first] == MAXN) { n         par[v.first] = u; n         pw[v.first] = v.second; n         dep[v.first] = 1 + dep[u]; n         if (dep[u] % 2 == 0) n           sm[v.first] = sm[u] + v.second; n         else n           sm[v.first] = sm[u] - v.second; n         q.push(v.first); n       } n     } n   } n   double minoddshit = INF, maxoddshit = -INF; n   freq.clear(); n   for (auto u : cc) { n     freq[sm[u]]++; n     for (auto v : adj[u]) { n       if (v.first != u and (dep[v.first] + 1 == dep[u] or par[v.first] == u)) n         continue; n       if (dep[v.first] == dep[u]) { n         double val; n         if (dep[u] % 2 == 0) { n           val = (double)(sm[v.first] - sm[u] + v.second) / (double)2 + n                 (double)sm[u]; n         } else { n           val = (double)(sm[v.first] - sm[u] - v.second) / (double)2 + n                 (double)sm[u]; n         } n         minoddshit = min(minoddshit, val); n         maxoddshit = max(maxoddshit, val); n         if (minoddshit + eps < maxoddshit) { n           flag = 0; n           return; n         } n       } else { n         if (dep[u] % 2 == 1) { n           if (sm[u] - v.second == sm[v.first]) continue; n           flag = 0; n           return; n         } else { n           if (sm[u] + v.second == sm[v.first]) continue; n           flag = 0; n           return; n         } n       } n     } n   } n   if (minoddshit != INF) { n     dfs(root, minoddshit); n     return; n   } n   int neg = 0, zer = 0, pos = 0, cur = 0; n   for (auto x : freq) { n     if (x.first == 0) n       zer += x.second; n     else if (x.first > 0) n       pos += x.second; n     else n       neg += x.second; n   } n   while (pos + zer < neg) { n     pos += zer; n     cur--; n     zer = freq[cur]; n     neg -= zer; n   } n   while (neg + zer < pos) { n     neg += zer; n     cur++; n     zer = freq[cur]; n     pos -= zer; n   } n   dfs(root, cur); n } n void solvethetestcase() { n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     int u, v, w; n     cin >> u >> v >> w; n     adj[u].push_back({v, w}); n     adj[v].push_back({u, w}); n   } n   for (int i = 1; i < n + 1; i++) { n     sort(adj[i].begin(), adj[i].end()); n     for (int j = 0; j + 1 < adj[i].size(); j++) { n       if (adj[i][j].first == adj[i][j + 1].first and n           adj[i][j].second != adj[i][j + 1].second) { n         cout <<  NO n ; n         return; n       } n     } n   } n   for (int i = 1; i < n + 1; i++) dep[i] = MAXN; n   for (int i = 1; i < n + 1; i++) { n     if (!vis[i]) { n       bfs(i); n       cc.clear(); n       if (!flag) break; n     } n   } n   if (!flag) { n     cout <<  NO n ; n   } else { n     cout <<  YES n ; n     for (int i = 1; i < n + 1; i++) cout << ans[i] <<    ; n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n template <typename T> n T pow(T a, T b, long long m) { n   T ans = 1; n   while (b > 0) { n     if (b % 2 == 1) ans = (ans * a) % m; n     b /= 2; n     a = (a * a) % m; n   } n   return ans % m; n } n using namespace std; n const long long N = 2e5 + 77; n string s; n void sol() { n   long long n; n   cin >> n; n   vector<long long> A(30, 0); n   for (long long(i) = (0); (i) < (n); (i)++) { n     cin >> s; n     for (long long(j) = (0); (j) < ((long long)s.size()); (j)++) n       A[s[j] -  a ]++; n   } n   for (long long(i) = (0); (i) < ((long long)A.size()); (i)++) n     if (A[i] % n != 0) { n       cout <<  NO  <<   n ; n       return; n     } n   cout <<  YES  <<   n ; n } n int32_t main() { n   long long t; n   cin >> t; n   while (t--) sol(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100050; n int item[MAXN]; n int main() { n   int dcnt; n   while (scanf( %d , &dcnt) == 1) { n     int discount = MAXN; n     for (int i = 0; i < dcnt; i++) { n       int tmp; n       scanf( %d , &tmp); n       discount = min(discount, tmp); n     } n     int icnt; n     scanf( %d , &icnt); n     for (int i = 1; i <= icnt; i++) scanf( %d , &item[i]); n     sort(item + 1, item + 1 + icnt, greater<int>()); n     long long cost = 0; n     long long dealed = 0, cur = 0; n     while (dealed < icnt) { n       if (cur == discount) { n         cur = 0; n         dealed += 2; n       } n       cost += item[++dealed]; n       cur++; n     } n     printf( %lld n , cost); n   } n } n


#include <bits/stdc++.h> n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   int min_row = n, max_row = 0, min_col = m, max_col = 0; n   char S[105][105]; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       scanf(  %c , &S[i][j]); n       if (S[i][j] ==  * ) { n         min_row = std::min(min_row, i), max_row = std::max(max_row, i), n         min_col = std::min(min_col, j), max_col = std::max(max_col, j); n       } n     } n   } n   for (int i = min_row; i <= max_row; i++, putchar(  n )) { n     for (int j = min_col; j <= max_col; j++) { n       printf( %c , S[i][j]); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const char dig[10][8] = { 1110111 ,  0010010 ,  1011101 ,  1011011 ,  0111010 , n                           1101011 ,  1101111 ,  1010010 ,  1111111 ,  1111011 }; n int n, m; n char s[2022][8]; n int det[2022][10]; n bool f[2022][2022]; n int calc(int id1, int id2) { n   int res = 0; n   for (int i = 0; i < 7; i++) { n     if (s[id1][i] ==  0  && dig[id2][i] ==  1 ) res++; n     if (s[id1][i] ==  1  && dig[id2][i] ==  0 ) return -1; n   } n   return res; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) scanf( %s , s[i]); n   for (int i = 1; i <= n; i++) n     for (int j = 0; j < 10; j++) det[i][j] = calc(i, j); n   memset(f, false, sizeof f); n   f[n + 1][0] = true; n   for (int i = n; i > 0; i--) n     for (int j = 0; j <= m; j++) { n       if (!f[i + 1][j]) continue; n       for (int k = 0; k < 10; k++) n         if (det[i][k] >= 0) f[i][j + det[i][k]] = true; n     } n   if (!f[1][m]) n     puts( -1 ); n   else { n     int tot = 0; n     for (int i = 1; i <= n; i++) n       for (int j = 9; j >= 0; j--) { n         int x = det[i][j]; n         if (x >= 0 && tot + x <= m && f[i + 1][m - tot - x]) { n           printf( %d , j); n           tot += x; n           break; n         } n       } n     puts(  ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 10; n long long n, sum; n int a[maxn], ans[maxn]; n int main() { n   scanf( %I64d%I64d , &n, &sum); n   long long tot = 0; n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     tot += a[i]; n   } n   for (int i = 1; i <= n; i++) { n     if (a[i] > sum - n + 1) ans[i] += a[i] - (sum - n + 1); n     if (sum - 1 > tot - a[i]) ans[i] += sum - 1 - (tot - a[i]); n   } n   for (int i = 1; i <= n; i++) printf( %d%c , ans[i], i == n ?   n  :    ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MX = 300005; n long long a[MX], b[MX]; n int32_t main() { n   long long n, p, k; n   cin >> n >> p >> k; n   for (long long i = 0; i < n; i++) { n     cin >> a[i]; n     b[i] = n         (((a[i] * a[i]) % p) * ((a[i] * a[i]) % p) - (k * a[i]) + (k * p)) % p; n   } n   cout << endl; n   sort(b, b + n); n   long long j = 0, sum = 0; n   for (long long i = 0; i < n; i = j) { n     while (b[i] == b[j] && j < n) { n       j++; n     } n     long long num = j - i; n     sum += (num * (num - 1)) / 2; n   } n   cout << sum; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long t, h, g, n; n   scanf( %lld , &t); n   while (t--) { n     scanf( %lld %lld , &h, &g); n     n = 1ll << h; n     pair<long long, long long> p[n - 1]; n     for (long long i = 1; i < n; i++) n       scanf( %lld , &p[i - 1].first), p[i - 1].second = i; n     sort(p, p + n - 1); n     bool f[n * 2], ans[n]; n     for (long long i = 1; i < n; i++) { n       if (i < (1ll << g)) n         f[i] = true; n       else n         f[i] = false; n       ans[i] = false; n     } n     long long s = 0; n     for (auto e : p) { n       long long c = e.second; n       while (c > 0) { n         if (f[c] && !f[c * 2] && !f[c * 2 + 1]) { n           ans[e.second] = true, f[c] = false, s += e.first; n           break; n         } n         c /= 2; n       } n     } n     printf( %lld n , s); n     for (long long i = n - 1; i > 0; i--) n       if (!ans[i]) printf( %lld  , i); n     printf(  n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const double pi = acos(-1.0); n const int inf = 0x3f3f3f3f; n const double eps = 1e-15; n const long long INF = (1LL << 60); n const int N = 200000 + 100; n int xis[N << 1]; n bool vis[N << 1]; n struct QUE { n   bool flag; n   int x, y; n } que[N]; n struct SegTree { n   int l, r; n   int val; n } tree[2][N << 2]; n int use[N << 2]; n int k; n void pushup(int p) { n   tree[k][p].val = min(tree[k][p << 1].val, tree[k][p << 1 | 1].val); n } n void build(int p, int l, int r) { n   tree[k][p].l = l; n   tree[k][p].r = r; n   if (l == r) { n     tree[k][p].val = inf; n     return; n   } n   int mid = (l + r) >> 1; n   build(p << 1, l, mid); n   build(p << 1 | 1, mid + 1, r); n   pushup(p); n } n void update(int p, int pos, int val) { n   if (tree[k][p].l == tree[k][p].r) { n     tree[k][p].val = val; n     return; n   } n   int mid = (tree[k][p].l + tree[k][p].r) >> 1; n   if (pos <= mid) { n     update(p << 1, pos, val); n   } else { n     update(p << 1 | 1, pos, val); n   } n   pushup(p); n } n int tar, val, pos; n int query(int p, int l, int r) { n   if (l <= tree[k][p].l && tree[k][p].r <= r) { n     return tree[k][p].val; n   } n   int mid = (tree[k][p].l + tree[k][p].r) >> 1; n   if (r <= mid) { n     return query(p << 1, l, r); n   } else if (l > mid) { n     return query(p << 1 | 1, l, r); n   } else { n     return min(query(p << 1, l, mid), query(p << 1 | 1, mid + 1, r)); n   } n } n void work1(int l, int r) { n   int L = l, R = r, mid; n   while (L <= R) { n     mid = (L + R) >> 1; n     int v = query(1, mid, r); n     if (v <= tar) { n       L = mid + 1; n       pos = mid; n       val = v; n     } else { n       R = mid - 1; n     } n   } n } n void work2(int l, int r) { n   int L = l, R = r, mid; n   while (L <= R) { n     mid = (L + R) >> 1; n     int v = query(1, l, mid); n     if (v <= tar) { n       R = mid - 1; n       pos = mid; n       val = v; n     } else { n       L = mid + 1; n     } n   } n } n char op[4]; n int ret; n int main() { n   int n, q; n   while (~scanf( %d%d , &n, &q)) { n     ret = 0; n     memset(vis, 0, sizeof(vis)); n     for (int i = 1; i <= q; ++i) { n       scanf( %d%d%s , &que[i].x, &que[i].y, op); n       if (op[0] ==  U ) { n         que[i].flag = 0; n       } else { n         que[i].flag = 1; n       } n       swap(que[i].x, que[i].y); n       xis[++ret] = que[i].x; n     } n     sort(xis + 1, xis + 1 + ret); n     ret = unique(xis + 1, xis + 1 + ret) - xis - 1; n     k = 0; n     build(1, 1, ret); n     k = 1; n     build(1, 1, ret); n     for (int i = 1; i <= q; ++i) { n       int x = que[i].x; n       int y = que[i].y; n       int ans = 0; n       int indx = lower_bound(xis + 1, xis + 1 + ret, x) - xis; n       if (vis[indx]) { n         printf( 0 n ); n         continue; n       } n       val = inf; n       vis[indx] = 1; n       if (que[i].flag == 0) { n         tar = y; n         if (indx == 1) { n           ans = x; n         } else { n           k = 0; n           work1(1, indx - 1); n           if (val <= tar) { n             ans = x - xis[pos]; n           } else { n             ans = x; n           } n         } n         k = 1; n         update(1, indx, x - ans + 1); n       } else { n         tar = x; n         if (indx == ret) { n           ans = y; n         } else { n           k = 1; n           work2(indx + 1, ret); n           if (val <= tar) { n             ans = y - (n + 1 - xis[pos]); n           } else { n             ans = y; n           } n         } n         k = 0; n         update(1, indx, y - ans + 1); n       } n       printf( %d n , ans); n     } n   } n   return 0; n } n


#include <cstdio> n #include <algorithm> n #define ll long long int n using namespace std; n const int MAX = 1e7+10; n int t, c; n int ans[MAX]; n int prime[MAX], pcount; n bool vis[MAX]; n ll d[MAX], f[MAX]; n void eular() { n     for (int i = 1; i <= MAX; i++) ans[i] = MAX; n     ans[1] = 1; n     for (int i = 2; i < MAX; i++) { n         if (!vis[i]) { n             vis[i] = 1; n             prime[pcount++] = i; n             d[i] = f[i] = i + 1; n             if (ans[i + 1] > i) ans[i + 1] = i; n         } n         for (int j = 0; j < pcount && (ll)prime[j] * i < MAX; j++) { n             vis[prime[j] * i] = 1; n             if (i % prime[j] == 0) { n                 f[prime[j] * i] = f[i] * prime[j] + 1; n                 d[prime[j] * i] = d[i] / f[i] * f[prime[j] * i]; n             } else { n                 f[prime[j] * i] = f[prime[j]]; n                 d[prime[j] * i] = d[i] * f[prime[j] * i]; n             } n             if (d[prime[j] * i] < MAX && ans[d[prime[j] * i]] > prime[j] * i) ans[d[prime[j] * i]] = prime[j] * i; n             if (i % prime[j] == 0) break; n         } n     } n } n int main() { n     eular(); n     scanf( %d , &t); n     while(t--) { n         scanf( %d , &c); n         if (ans[c] != MAX) printf( %d n , ans[c]); n         else printf( -1 n ); n     } n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n const int N = 1000010; n int n, arr[N], arr2[N], seg[4 * N], lazy[4 * N]; n inline void fix(int s, int e, int idx) { n   seg[idx] += lazy[idx]; n   if (s != e) { n     lazy[(idx << 1)] += lazy[idx]; n     lazy[(idx << 1) + 1] += lazy[idx]; n   } n   lazy[idx] = 0; n } n int build(int s, int e, int idx) { n   if (s == e) return seg[idx] = arr2[s]; n   return seg[idx] = max(build(s, ((s + e) >> 1), (idx << 1)), n                         build(((s + e) >> 1) + 1, e, (idx << 1) + 1)); n } n int l, r, val; n inline int update(int s, int e, int idx) { n   fix(s, e, idx); n   if (s > r || e < l) return seg[idx]; n   if (s >= l && e <= r) { n     lazy[idx] += val; n     fix(s, e, idx); n     return seg[idx]; n   } n   return seg[idx] = max(update(s, ((s + e) >> 1), (idx << 1)), n                         update(((s + e) >> 1) + 1, e, (idx << 1) + 1)); n } n int s, e, idx, mid; n inline int Lower_bound(int val) { n   s = 0, e = n, idx = 1; n   while (true) { n     fix(s, e, idx); n     if (s == e) return s; n     mid = ((s + e) >> 1); n     fix(s, mid, (idx << 1)); n     fix(mid + 1, e, (idx << 1) + 1); n     if (seg[(idx << 1)] >= val) { n       idx = (idx << 1); n       e = mid; n     } else { n       idx = (idx << 1) + 1; n       s = mid + 1; n     } n   } n   assert(false); n } n int Idx; n inline int get() { n   s = 0, e = n, idx = 1; n   while (true) { n     fix(s, e, idx); n     if (s == e) return seg[idx]; n     mid = ((s + e) >> 1); n     if (Idx <= mid) { n       idx = (idx << 1); n       e = mid; n     } else { n       idx = (idx << 1) + 1; n       s = mid + 1; n     } n   } n   assert(false); n } n int tmp1, tmp2, tmp3, tmp4, tmp5; n int tmp7 = 0, tmp8 = 0; n void check2(int cur) { n   for (int i = 1; i <= 4 * (n + 1); i++) seg[i] = lazy[i] = 0; n   arr[n] = cur; n   for (int i = 0; i <= n; i++) { n     arr2[i] = arr[i]; n   } n   sort(arr2, arr2 + n + 1); n   build(0, n, 1); n   tmp7 = 0; n   for (int i = n; i >= 0; i--) { n     Idx = i; n     tmp1 = get(); n     if (tmp1 <= 0) { n       tmp7 -= tmp1; n       continue; n     } n     if (i - tmp1 < 0) { n       l = 0, r = i - 1, val = -1; n       update(0, n, 1); n       tmp7 -= (i - tmp1); n       continue; n     } n     Idx = i - tmp1; n     tmp2 = get(); n     tmp3 = Lower_bound(tmp2); n     Idx = i - 1; n     tmp4 = get(); n     if (tmp2 == tmp4) { n       l = tmp3; n       r = tmp3 + tmp1 - 1; n       val = -1; n       update(0, n, 1); n     } else { n       tmp5 = Lower_bound(tmp2 + 1); n       l = tmp5; n       r = i - 1; n       val = -1; n       update(0, n, 1); n       l = tmp3; n       r = l + (tmp5 - (i - tmp1)) - 1; n       val - 1; n       update(0, n, 1); n     } n   } n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d , &arr[i]); n   } n   check2(0); n   tmp8 = tmp7; n   check2(n); n   tmp7 = n - tmp7; n   if (tmp7 < tmp8) { n     puts( -1 ); n     return 0; n   } n   for (int i = tmp8; i <= tmp7; i += 2) { n     printf( %d  , i); n   } n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MX = 6; n int n, Arr[6]; n int solve(int rem, int mask) { n   if (mask == ((1 << MX) - 1)) return 0; n   int Ans = 50; n   for (int i = 0; i < MX; i++) { n     if (!(mask & (1 << i))) { n       if (rem < Arr[i]) n         Ans = min(Ans, 1 + solve(n - Arr[i], mask | (1 << i))); n       else n         Ans = min(Ans, solve(rem - Arr[i], mask | (1 << i))); n     } n   } n   return Ans; n } n int main() { n   scanf( %d %d %d , &n, &Arr[0], &Arr[2]); n   Arr[1] = Arr[3] = Arr[4] = Arr[0]; n   Arr[5] = Arr[2]; n   printf( %d n , solve(0, 0)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int in() { n   int x, y; n   y = scanf( %d , &x); n   return x; n } n const int MAXN = 2e6 + 10; n int n, p[MAXN], sec[MAXN], pt[MAXN]; n string s; n int nxt[MAXN], prv[MAXN]; n int d[MAXN][2]; n bool ok(int mid, int sz) { n   memset(d, 0, sizeof(d)); n   d[0][0] = d[0][1] = 1; n   for (int i = 1; i <= n; i++) { n     if (s[i - 1] ==  . ) n       d[i][0] = d[i - 1][0]; n     else if (s[i - 1] ==  P ) n       d[i][0] = d[i - 1][1]; n     else { n       int xx = prv[i - 1]; n       if (~xx && xx + mid >= i - 1) { n         d[i][0] |= d[xx][0]; n         int temp = prv[xx]; n         if (~temp && temp + mid >= i - 1) { n           int t = max(0, xx - mid); n           int temp2 = 0; n           if (t && nxt[t - 1] < temp) temp2 = 1; n           d[i][0] |= d[t][temp2]; n         } n       } n     } n     int xx = nxt[i - 1]; n     if (xx < n) { n       int temp = max(0, xx - mid); n       temp = min(temp, i); n       int temp2 = 0; n       if (temp && nxt[temp - 1] < xx) temp2 = 1; n       d[i][1] |= d[temp][temp2]; n     } n   } n   return d[n][0]; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> s; n   int sz = 0, c = 0; n   for (int i = 0; i < n; i++) n     if (s[i] ==  P ) n       sec[sz++] = i; n     else if (s[i] ==  * ) n       c++; n   int mn = 1e9, mx = -1; n   for (int i = 0; i < n; i++) { n     p[i + 1] = p[i]; n     if (s[i] ==  * ) { n       p[i + 1]++; n       mn = min(mn, i); n       mx = max(mx, i); n     } n   } n   int last = n; n   for (int i = n - 1; ~i; i--) { n     nxt[i] = last; n     if (s[i] ==  P ) last = i; n   } n   last = -1; n   for (int i = 0; i < n; i++) { n     prv[i] = last; n     if (s[i] ==  P ) last = i; n   } n   memcpy(pt, p, sizeof(p)); n   if (sz == 1) { n     if (mn > sec[0]) n       cout << c <<     << mx - sec[0] <<   n ; n     else if (mx < sec[0]) n       cout << c <<     << sec[0] - mn <<   n ; n     else { n       if (p[sec[0]] > c - p[sec[0]]) n         cout << p[sec[0]] <<      << sec[0] - mn <<   n ; n       else if (p[sec[0]] < c - p[sec[0]]) n         cout << c - p[sec[0]] <<     << mx - sec[0] <<   n ; n       else n         cout << p[sec[0]] <<     << min(sec[0] - mn, mx - sec[0]) <<   n ; n     } n   } else { n     int lo = 0, hi = n - 1; n     while (hi - lo > 1) { n       int mid = hi + lo >> 1; n       if (ok(mid, sz)) n         hi = mid; n       else n         lo = mid; n     } n     cout << c <<     << hi <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = 1e15; n int n, B, c0[5010], c1[5010]; n vector<int> e[5010]; n vector<long long> f[5010], g[5010]; n void combine(vector<long long>& a, const vector<long long>& b) { n   int la = a.size(), lb = b.size(), lc = la + lb - 1; n   vector<long long> c(lc); n   for (int i = 0; i < lc; ++i) c[i] = inf; n   for (int i = 0; i < la; ++i) n     for (int j = 0; j < lb; ++j) c[i + j] = min(c[i + j], a[i] + b[j]); n   a = c; n } n void dfs(int x) { n   vector<long long> a, b; n   a.push_back(0); n   b.push_back(0); n   for (int v : e[x]) { n     dfs(v); n     combine(a, f[v]); n     combine(b, g[v]); n   } n   f[x].push_back(0); n   for (int i = 0; i < a.size(); ++i) { n     f[x].push_back(a[i] + c0[x]); n   } n   g[x] = b; n   g[x].push_back(inf); n   for (int i = 0; i < b.size(); ++i) { n     g[x][i + 1] = min(g[x][i + 1], b[i] + c1[x]); n   } n   for (int i = 0; i < b.size(); ++i) { n     f[x][i] = min(f[x][i], g[x][i]); n   } n } n int main() { n   scanf( %d%d , &n, &B); n   for (int i = 1; i <= n; ++i) { n     scanf( %d%d , &c1[i], &c0[i]); n     c0[i] = c1[i] - c0[i]; n     int fa; n     if (i != 1) scanf( %d , &fa), e[fa].push_back(i); n   } n   dfs(1); n   int ans = 0; n   for (int i = 0; i < f[1].size(); ++i) n     if (f[1][i] <= B) ans = i; n   for (int i = ans + 1; i <= (int)g[1].size() - 1; ++i) n     if (g[1][i] <= B) ans = i; n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct keep { n   int pos; n   int apples; n }; n keep inp[1000]; n bool cmp(keep x, keep y) { n   if (x.pos < y.pos) return 1; n   return 0; n } n int main() { n   int n; n   cin >> n; n   int l = 0, r = 0; n   int sum = 0; n   for (int i = 0; i < n; i++) { n     cin >> inp[i].pos >> inp[i].apples; n     inp[i].pos < 0 ? l++ : r++; n     sum += inp[i].apples; n   } n   sort(inp, inp + n, cmp); n   if (l == 0) { n     cout << inp[0].apples; n     return 0; n   } n   if (r == 0) { n     cout << inp[n - 1].apples; n     return 0; n   } n   if (l - r == 1 || r - l == 1 || l == r) { n     cout << sum << endl; n     return 0; n   } n   if (l > r) { n     int ans = 0; n     int c = 0; n     int i; n     for (i = n - 1; inp[i].pos > 0; i--) { n       ans += inp[i].apples; n       c++; n     } n     int cc = 1; n     for (; cc <= c + 1; i--) { n       ans += inp[i].apples; n       cc++; n     } n     cout << ans; n     return 0; n   } n   if (r > l) { n     int ans = 0; n     int c = 0; n     int i; n     for (i = 0; inp[i].pos < 0; i++) { n       ans += inp[i].apples; n       c++; n     } n     int cc = 1; n     for (; cc <= c + 1; i++) { n       ans += inp[i].apples; n       cc++; n     } n     cout << ans; n     return 0; n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   long int n, k, i, *a, *b, t, max, j; n   scanf( %ld , &t); n   while (t--) { n     scanf( %ld , &n); n     a = (long int *)malloc(n * sizeof(long int)); n     b = (long int *)malloc(n * sizeof(long int)); n     for (i = 0; i < n; i++) { n       scanf( %ld , &a[i]); n       b[a[i] - 1] = i; n     } n     max = n - 1; n     if (b[0] != 0) k = b[0] - 1; n     for (j = 0, i = 0; j < n - 1; j++) { n       if (b[i] == max) { n         max = k; n         k = b[i + 1] - 1; n         i++; n       } else { n         if (b[i + 1] != b[i] + 1) n           break; n         else n           i++; n       } n     } n     if (j == n - 1) 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 const int maxn = 300010; n const int INF = 1e9 + 7; n struct node { n   int x, y; n } p[maxn]; n int s[maxn]; n int dis(int i, int j) { return abs(p[i].x - p[j].x) + abs(p[i].y - p[j].y); } n int main() { n   int n, top, i, num, ans, maxx, maxy, minx, miny, tmp, j, pos1, pos2, k; n   scanf( %d , &n); n   maxx = -INF; n   maxy = -INF; n   minx = INF; n   miny = INF; n   for (i = 1; i <= n; i++) { n     scanf( %d%d , &p[i].x, &p[i].y); n     maxx = max(maxx, p[i].x); n     maxy = max(maxy, p[i].y); n     minx = min(minx, p[i].x); n     miny = min(miny, p[i].y); n   } n   top = 0; n   for (i = 1; i <= n; i++) { n     if (p[i].x == minx || p[i].x == maxx || p[i].y == miny || p[i].y == maxy) n       s[++top] = i; n   } n   ans = 0; n   num = unique(s + 1, s + top + 1) - s - 1; n   for (i = 1; i <= num; i++) { n     for (j = i + 1; j <= num; j++) { n       pos1 = s[i]; n       pos2 = s[j]; n       for (k = 1; k <= n; k++) { n         if (k == pos1 || k == pos2) continue; n         tmp = dis(pos1, pos2) + dis(pos1, k) + dis(k, pos2); n         ans = max(ans, tmp); n       } n     } n   } n   for (i = 3; i <= n; i++) { n     if (i == 3) { n       printf( %d  , ans); n       continue; n     } n     printf( %d  , 2 * (maxx - minx + maxy - miny)); n   } n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n, k; n   cin >> n >> k; n   vector<int> arr(k); n   for (int i = 0; i < k; i++) { n     arr[i] = i + 1; n   } n   vector<string> allPos; n   do { n     string s =   ; n     for (int x : arr) { n       s += (x +  0 ); n     } n     allPos.emplace_back(s); n   } while (next_permutation((arr).begin(), (arr).end())); n   vector<string> v(n), tempV(n); n   for (int i = 0; i < n; i++) { n     cin >> v[i]; n     tempV[i] = v[i]; n   } n   int ans = INT_MAX; n   for (string current : allPos) { n     vector<int> temp(n); n     int index = 0; n     for (char ch : current) { n       for (int i = 0; i < n; i++) { n         tempV[i][index] = v[i][ch -  0  - 1]; n       } n       index++; n     } n     for (int i = 0; i < n; i++) { n       temp[i] = stoi(tempV[i]); n     } n     ans = min(ans, *max_element((temp).begin(), (temp).end()) - n                        *min_element((temp).begin(), (temp).end())); n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int q; n char s[5050]; n bool f[5000][5000]; n int g[5000][5000]; n int main() { n   scanf( %s , s); n   int n = strlen(s); n   for (int i = 0; i < n; i++) { n     for (int j = i; j >= 0; j--) { n       if (i - j == 0) n         f[j][i] = 1; n       else if (i - j == 1 && s[i] == s[j]) n         f[j][i] = 1; n       else if (f[j + 1][i - 1] && s[i] == s[j]) n         f[j][i] = 1; n     } n   } n   for (int i = 0; i < n; i++) { n     for (int j = i; j >= 0; j--) { n       if (i - j == 0) n         g[j][i] = 1; n       else { n         g[j][i] = g[j + 1][i] + g[j][i - 1]; n         if (i - j != 1) g[j][i] -= g[j + 1][i - 1]; n         if (f[j][i]) g[j][i]++; n       } n     } n   } n   scanf( %d , &q); n   for (int i = 1; i <= q; i++) { n     int l, r; n     scanf( %d%d , &l, &r); n     printf( %d n , g[l - 1][r - 1]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1000000007; n long long dp[200100][2]; n long long ans[200100]; n int main(int argc, char const *argv[]) { n   int T, k; n   cin >> T >> k; n   dp[k][1] = 1; n   dp[1][0] = 1; n   for (int i = 2; i <= 100000; i++) { n     if (i - k <= 0) { n       dp[i][0] += dp[i - 1][0] + dp[i - 1][1]; n       dp[i][0] %= mod; n     } else { n       dp[i][0] += dp[i - 1][0] + dp[i - 1][1]; n       dp[i][0] %= mod; n       dp[i][1] += dp[i - k][0] + dp[i - k][1]; n       dp[i][1] %= mod; n     } n   } n   int a, b; n   for (int i = 1; i <= 100000; i++) { n     ans[i] = ans[i - 1] + dp[i][1] + dp[i][0]; n     ans[i] %= mod; n   } n   while (T--) { n     scanf( %d%d , &a, &b); n     cout << (mod + ans[b] - ans[a - 1]) % mod << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, m, fl = 0, a[200005], c[200005] = {0}, ans, i, j; n   cin >> n >> m; n   for (i = 0; i < n; i++) { n     cin >> a[i]; n   } n   sort(a, a + n); n   for (i = 0; i < n; i++) { n     a[i] = a[i] % m; n     c[a[i]]++; n     if (c[a[i]] > 1) fl = 1; n   } n   if (fl == 1) cout <<  0  << endl; n   if (fl == 0) { n     ans = 1; n     for (i = 0; i < n; i++) { n       for (j = i + 1; j < n; j++) { n         ans = (ans % m * ((a[j] - a[i] + m) % m)) % m; n       } n     } n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 4e5 + 5; n int vis[MAX]; n vector<int> adj[MAX]; n vector<vector<int> > trip; n void dfs(int x) { n   vis[x] = 1; n   for (auto u : adj[x]) { n     if (!vis[u]) { n       for (auto v1 : adj[x]) { n         for (auto v2 : adj[u]) { n           if (!vis[v1] && !vis[v2] && v1 == v2) { n             vector<int> triplet; n             triplet.push_back(x); n             triplet.push_back(u); n             triplet.push_back(v1); n             trip.push_back(triplet); n           } n         } n       } n     } n   } n } n int main() { n   int n, m, x, y, minm = 1e9 + 9; n   cin >> n >> m; n   memset(vis, 0, sizeof(vis)); n   for (int i = 0; i < m; i++) { n     cin >> x >> y; n     adj[x].push_back(y); n     adj[y].push_back(x); n   } n   for (int i = 0; i < n; i++) { n     if (!vis[i]) dfs(i); n   } n   for (auto v : trip) { n     minm = min(minm, (int)(adj[v[0]].size() + adj[v[1]].size() + n                            adj[v[2]].size() - 6)); n   } n   if (!trip.empty()) n     cout << minm << endl; n   else n     cout << -1 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 5; n int v1[10], v2[10]; n int main() { n   memset(v1, 0, sizeof(v1)); n   memset(v2, 0, sizeof(v2)); n   int n; n   scanf( %d , &n); n   for (int i = 0, a; i < n; i++) { n     scanf( %d , &a); n     v1[a]++; n   } n   for (int i = 0, a; i < n; i++) { n     scanf( %d , &a); n     v2[a]++; n   } n   int ans = 0; n   bool flag = true; n   for (int i = 1; i <= 5 && flag; i++) { n     if ((v1[i] + v2[i]) & 1) { n       flag = false; n     } n     ans += abs(v1[i] - v2[i]) / 2; n   } n   if (flag) n     printf( %d n , ans / 2); n   else { n     printf( -1 n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n const long long MAXN = 80000 + 5; n std::vector<long long> G[MAXN]; n long long n; n long long sz[MAXN], fa[MAXN]; n inline void dfs1(long long v, long long faa = 0) { n   sz[v] = 1; n   fa[v] = faa; n   for (auto x : G[v]) { n     if (x == faa) continue; n     dfs1(x, v); n     sz[v] += sz[x]; n   } n } n long long ans = 0ll; n inline long long C(long long x) { return 1ll * x * (x - 1) / 2; } n signed main() { n   scanf( %lld , &n); n   for (register long long i = 1; i <= n - 1; ++i) { n     long long u, v; n     scanf( %lld%lld , &u, &v); n     G[u].push_back(v); n     G[v].push_back(u); n   } n   dfs1(1); n   ans = C(n) * C(n); n   for (register long long i = 1; i <= n; ++i) { n     long long num2 = C(sz[i]), num1 = C(n); n     for (auto x : G[i]) { n       if (x == fa[i]) { n         num1 -= C(n - sz[i]); n         continue; n       } n       num2 -= C(sz[x]); n       num1 -= C(sz[x]); n     } n     ans -= (2ll * num1 * num2 - num2 * num2); n   } n   std::cout << ans << std::endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n int a[N], n; n int dp[N]; n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n; n   for (int i = 1; i <= n; ++i) { n     cin >> a[i]; n   } n   for (int i = 1; i <= n; ++i) { n     dp[i] = dp[i - 1] + 20; n     for (int j = i - 1; j > 0; j--) { n       if (a[i] - a[j] < 90) n         dp[i] = min(dp[i], dp[j - 1] + 50); n       else if (a[i] - a[j] < 1440) n         dp[i] = min(dp[i], dp[j - 1] + 120); n       else n         break; n     } n     cout << dp[i] - dp[i - 1] << endl; n   } n   return 0; n } n