#include <bits/stdc++.h> n template <typename T> n inline void read(T& x) { n   T res = 0, flag = 1; n   char ch; n   ch = getchar(); n   while (!isdigit(ch)) { n     if (ch ==  - ) flag = -1; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     res = res * 10 + ch - 48; n     ch = getchar(); n   } n   x = res * flag; n } n const int N = 300000 + 1; n int n, m, a[N], f[N]; n inline bool check(int x) { n   for (int i = 1; i <= n; ++i) { n     f[i] = a[i]; n     if (f[i] >= f[i - 1]) { n       if (f[i] + x >= m) n         if ((f[i] + x) % m >= f[i - 1]) f[i] = f[i - 1]; n     } else { n       if (f[i] + x >= f[i - 1]) n         f[i] = f[i - 1]; n       else n         return 0; n     } n   } n   return 1; n } n int main() { n   read(n), read(m); n   for (int i = 1; i <= n; ++i) read(a[i]); n   int l = 0, r = m; n   while (l < r) { n     int mid = l + r >> 1; n     if (check(mid)) n       r = mid; n     else n       l = mid + 1; n   } n   printf( %d n , l); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n, a; n     vector<int> v; n     cin >> n; n     for (int i = 0; i < n; i++) { n       cin >> a; n       v.push_back(a); n     } n     reverse(v.begin(), v.end()); n     for (int i = 0; i < n; i++) { n       cout << v[i] <<    ; n     } n     cout << endl; n   } n } n


#include <bits/stdc++.h> n #pragma gcc optimize(o3) n using namespace std; n long long qpow(long long x, long long y) { n   return y ? (y & 1 ? x * qpow(x, y - 1) % 1000000007 n                     : qpow(x * x % 1000000007, y / 2)) n            : 1; n } n template <typename T> n inline void read(T& x) { n   x = 0; n   char c; n   int sign = 1; n   do { n     c = getchar(); n     if (c ==  - ) sign = -1; n   } while (!isdigit(c)); n   do { n     x = x * 10 + c -  0 ; n     c = getchar(); n   } while (isdigit(c)); n   x *= sign; n } n long long a, b, x, y, k; n long long gcd(long long p, long long q) { n   if (q == 0) return p; n   return gcd(q, p % q); n } n int main() { n   read(a), read(b), read(x), read(y); n   k = gcd(x, y); n   x /= k, y /= k; n   printf( %I64d n , min(a / x, b / y)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = (long long)(2e5 + 1); n bool bipartite = true; n vector<long long> colour; n vector<long long> adj[N]; n vector<long long> visited; n void dfs(long long node, long long par, long long col) { n   visited[node] = 1; n   colour[node] = col; n   for (auto it : adj[node]) { n     if (!visited[it]) n       dfs(it, node, !col); n     else if (it != par and colour[it] == colour[node]) n       bipartite = false; n   } n } n signed main() { n   long long n, m; n   cin >> n >> m; n   vector<pair<long long, long long>> edges; n   colour = vector<long long>(n, -1); n   visited = vector<long long>(n, 0); n   for (long long i = 0; i < m; i++) { n     long long u, v; n     cin >> u >> v; n     u--, v--; n     adj[v].push_back(u); n     adj[u].push_back(v); n     edges.push_back({u, v}); n   } n   dfs(0, -1, 0); n   if (!bipartite) { n     cout <<  NO n ; n     return 0; n   } n   cout <<  YES n ; n   for (long long i = 0; i < m; i++) { n     long long ele1 = edges[i].first; n     long long ele2 = edges[i].second; n     cout << (colour[ele1] < colour[ele2]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char buf[10000001], *ptr = buf - 1; n inline int read() { n   int x = 0, c = *++ptr; n   while (c < 48) c = *++ptr; n   while (c > 47) x = x * 10 + c -  0 , c = *++ptr; n   return x; n } n const int MAXN = 500005; n const int MOD = 1000000007; n int s[MAXN], a[MAXN], b[MAXN]; n int c[MAXN], cnt[MAXN]; n int n; n inline int add(int x, int y) { return x + y >= MOD ? x + y - MOD : x + y; } n inline void modify(int i, int t) { n   while (i <= n) s[i] = add(s[i], t), i += i & -i; n } n inline int query(int i) { n   int res = 0; n   while (i) res = add(res, s[i]), i -= i & -i; n   return res; n } n int main(int argc, char const *argv[]) { n   fread(buf, 1, sizeof(buf), stdin)[buf] = 0; n   int x, ans = 0; n   n = read(); n   for (int i = 1; i <= n; ++i) b[i] = c[i] = a[i] = read(); n   sort(b + 1, b + 1 + n); n   for (int i = 1; i <= n; ++i) { n     x = lower_bound(b + 1, b + 1 + n, a[i]) - b; n     modify(x, i); n     cnt[i] = (cnt[i] + (long long)query(x - 1) * (n - i + 1)) % MOD; n   } n   memset(s, 0, sizeof(s)); n   reverse(a + 1, a + 1 + n); n   for (int i = 1; i <= n; ++i) { n     x = lower_bound(b + 1, b + 1 + n, a[i]) - b; n     modify(x, i); n     cnt[n - i + 1] = n         (cnt[n - i + 1] + (long long)query(x - 1) * (n - i + 1)) % MOD; n   } n   for (int i = 1; i <= n; ++i) n     ans = (ans + (cnt[i] + (long long)i * (n - i + 1)) % MOD * c[i]) % MOD; n   printf( %d , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MaxN = 1e5; n int m, n; n long long sum[MaxN + 5], a[MaxN + 5]; n long long Gcd(long long x, long long y) { n   if (y == 0) n     return x; n   else n     return Gcd(y, x % y); n } n void pdo() { n   long long ans = 0; n   for (int i = 1; i <= n; i++) { n     long long t1 = a[i] * (i - 1) - sum[i - 1]; n     long long t2 = sum[n] - sum[i] - a[i] * (n - i); n     ans += a[i] + t1 + t2; n   } n   long long t = Gcd(ans, (long long)n); n   printf( %I64d %I64d n , ans / t, n / t); n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %I64d , &a[i]); n   sort(a + 1, a + n + 1); n   for (int i = 1; i <= n; i++) sum[i] = sum[i - 1] + a[i]; n   pdo(); n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4 ) n using namespace std; n using ll = long long; n using ld = long double; n using itn = int; n using dd = double; n mt19937 gen(41); n const dd eps = 1e-7; n const ll MAXN = 2097152; n const ll INF = 1e9; n const dd pi = acos(dd(-1)); n ll gcd(ll a, ll b) { n   a = abs(a); n   b = abs(b); n   while (b) { n     a %= b; n     swap(a, b); n   } n   return a; n } n ll binpow(ll a, ll n, ll binpow_mod = 1e9 + 7) { n   ll res = 1; n   while (n) { n     if (n & 1) res *= a; n     a *= a; n     n >>= 1; n     a %= binpow_mod; n     res %= binpow_mod; n   } n   return res; n } n struct Point { n   ll x, y; n   bool z; n   void scan() { cin >> x >> y; } n   Point(ll X = 0, ll Y = 0, bool Z = 0) { n     x = X; n     y = Y; n     z = Z; n   } n   ll operator*(Point &a) { return x * a.y - y * a.x; } n   bool operator==(Point &a) { return x == a.x && y == a.y; } n }; n ll n, m, k; n deque<ll> a; n ll solve() { n   vector<ll> b((n + m - 1) / m, 0), c((n + m - 1) / m, -1e10); n   ll now = 0; n   for (int i = 0; i < n; i++) { n     now += a[i]; n     c[i / m] = max(c[i / m], now); n     if (i % m == m - 1) { n       b[i / m] = now; n       now = 0; n     } n   } n   ll ans = c[0] - k, sum = 0, min_sum = 0; n   for (int r = 0; r < b.size(); ++r) { n     sum += c[r] - k; n     ans = max(ans, sum - min_sum); n     sum = sum - c[r] + b[r]; n     min_sum = min(min_sum, sum); n   } n   return ans; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout << fixed << setprecision(20); n   cin >> n >> m >> k; n   a.resize(n); n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   ll res = 0; n   for (int k = 0; k < min(n, m); k++) { n     res = max(res, solve()); n     a.pop_front(); n     n = a.size(); n   } n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #define ll long long int n ll mod=1e9+7; n ll mul(ll a,ll b) n { n     return ((a%mod)*(b%mod))%mod; n } n ll add(ll a,ll b) n { n     return ((a%mod)+(b%mod))%mod; n } n ll sub(ll a,ll b) n { n     return ((a%mod)-(b%mod)+mod)%mod; n } n ll po(ll a, ll b) n { n     if(b==0) n     { n         return 1; n     } n     ll t=po(a,b/2); n     if(b%2) n     { n         return mul(t,mul(t,a)); n     } n     else n     { n         return mul(t,t); n     } n } n ll  a[200005]; n ll mseg[1000000]; n ll maseg[1000000]; n void mupd(ll s, ll e, ll i) n { n  if(s==e) n  { n   mseg[i]=a[s]; n   return; n  } n  ll mi=s+(e-s)/2; n  mupd(s,mi,2*i+1); n  mupd(mi+1,e,2*i+2); n  mseg[i]=min(mseg[2*i+1],mseg[2*i+2]); n  return; n } n void maupd(ll s, ll e, ll i) n { n  if(s==e) n  { n   maseg[i]=a[s]; n   return; n  } n  ll mi=s+(e-s)/2; n  maupd(s,mi,2*i+1); n  maupd(mi+1,e,2*i+2); n  maseg[i]=max(maseg[2*i+1],maseg[2*i+2]); n  return; n } n ll mir(ll s, ll e, ll i,ll chs, ll che) n { n  if(s>che || e<chs) n  { n   return 1e18; n  } n  if(s<=chs && e>=che) n  { n   return mseg[i]; n  } n  ll mi=chs+(che-chs)/2; n  return min(mir(s,e,2*i+1,chs,mi),mir(s,e,2*i+2,mi+1,che)); n } n ll mar(ll s, ll e, ll i,ll chs, ll che) n { n  if(s>che || e<chs) n  { n   return -1e18; n  } n  if(s<=chs && e>=che) n  { n   return maseg[i]; n  } n  ll mi=chs+(che-chs)/2; n  return max(mar(s,e,2*i+1,chs,mi),mar(s,e,2*i+2,mi+1,che)); n } n int main() n { n     ios_base::sync_with_stdio(false); n     cin.tie(NULL); n     cout.tie(NULL); n     ll t; n     cin>>t; n     while(t--) n     { n      ll n,q; n      cin>>n>>q; n      a[0]=0; n      string s; n      cin>>s; n      for(ll i=0;i<n;i++) n      { n       if(s[i]== - ) n       { n        a[i+1]=a[i]-1; n       } n       else n       { n        a[i+1]=a[i]+1; n       } n       //cout<<a[i+1]<<   ; n      } n      //cout<<  n ; n      mupd(0,n,0); n      maupd(0,n,0); n      while(q--) n      { n       ll l,r; n       cin>>l>>r; n       ll u1=mir(0,l-1,0,0,n); n       ll u2=mar(0,l-1,0,0,n);; n       ll s1=0,s2=0; n       //cout<<u1<<   <<u2<<  n ; n       if(r+1<=n) n       { n        s1=mir(r+1,n,0,0,n); n        s2=mar(r+1,n,0,0,n);; n         n        s1=s1-a[r]; n        s2=s2-a[r]; n       } n       //cout<<u1<<   <<u2<<   <<s1<<   <<s2<<  n ; n       ll mi=min((ll)0,u1); n       ll ma=max((ll)0,u2); n       mi=min(a[l-1]+s1,mi); n       ma=max(a[l-1]+s2,ma); n       cout<<ma-mi+1<<  n ; n      } n     } n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n long long int dp[50][50]; n int m, n; n int main() { n   for (int x = 0; x < 36; x++) { n     dp[0][x] = 1; n   } n   for (int x = 1; x < 36; x++) n     for (int y = 1; y < 36; y++) n       for (int z = 0; z < x; z++) { n         dp[x][y] += dp[z][y - 1] * dp[x - z - 1][y - 1]; n       } n   cin >> m >> n; n   cout << dp[m][35] - dp[m][n - 1] << endl; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void print(T t) { n   cout << t << endl; n } n long long n = 0, t = 0; n long long small = -1e18, big = 1e18; n const long long mod = 1e9 + 7; n map<long long, long long> occ; n map<pair<long long, long long>, long long> occP; n void print2dVec(vector<vector<long long>> v) { n   for (int i = 0; i < v.size(); ++i) { n     cout << i <<   :  ; n     for (int j = 0; j < v[i].size(); ++j) { n       cout << v[i][j] <<    ; n     } n     print(  ); n   } n } n void printSet(set<int> s) { n   for (auto itr = s.begin(); itr != s.end(); ++itr) { n     cout << *itr <<    ; n   } n } n long long factorial(int n) { n   long long ans = 1; n   for (int i = 1; i <= n; ++i) { n     ans *= i; n   } n   return ans; n } n long long findGCD(long long a, long long b) { n   long long ans = 1; n   for (int i = 1; i <= min(a, b); ++i) { n     if (a % i == 0 && b % i == 0) { n       ans = i; n     } n   } n   return ans; n } n long long findLCM(long long a, long long b) { n   for (long long i = min(a, b); i <= a * b; ++i) { n     if (i % a == 0 && i % b == 0) { n       return i; n     } n   } n   return -1; n } n bool isPrime(int num) { n   if (num <= 1) return false; n   if (num <= 3) return true; n   int range = sqrt(num); n   if (num % 2 == 0 || num % 3 == 0) return false; n   for (int i = 5; i <= range; i += 6) n     if (num % i == 0 || num % (i + 2) == 0) return false; n   return true; n } n vector<long long> primeFactors(long long x) { n   vector<long long> fac; n   for (long long i = 2; i * i <= x; i++) { n     if (x % i == 0) { n       while (x % i == 0) { n         fac.push_back(i); n         x /= i; n       } n     } n   } n   return fac; n } n bool sortVec(const vector<long long>& v1, const vector<long long>& v2) { n   return v1[0] < v2[0]; n } n bool check(long long val) { return false; } n long long binsearch(long long low, long long high) { n   while (low < high) { n     long long mid = (low + high) / 2; n     if (check(mid)) { n       high = mid; n     } else { n       low = mid + 1; n     } n   } n   return low; n } n unordered_map<long long, long long> d; n long long rib(long long n, long long a, long long b, long long c) { n   if (n == 0) return 0; n   if (n < 0) return small; n   if (d.count(n)) n     return d[n]; n   else { n     d[n] = 1 + max(rib(n - a, a, b, c), n                    max(rib(n - b, a, b, c), rib(n - c, a, b, c))); n     return d[n]; n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cout << fixed; n   cout << setprecision(10); n   ; n   long long n, a, b, c; n   cin >> n >> a >> b >> c; n   cout << rib(n, a, b, c); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, a, b; n   cin >> n >> a >> b; n   int rem = b % n; n   int sum = a + rem; n   if (sum <= 0) n     cout << n + sum; n   else if (sum > n) n     cout << sum - n; n   else n     cout << sum; n   return 0; 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   if (n == 1) { n     cout <<  -1 n ; n     return 0; n   } n   if (n & 1) { n     cout <<  -1 n ; n     return 0; n   } n   for (int i = 1; i <= n; i++) { n     if (i & 1) { n       cout << (i + 1) <<    ; n     } else { n       cout << (i - 1) <<    ; n     } n   } n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_N = 100030; n const long long MODD = 1000000009; n int ctr[128]; n void do_case() { n   int n, k; n   cin >> n >> k; n   string s; n   cin >> s; n   for (int i = 0; i < s.size(); i++) ctr[s[i]]++; n   sort(ctr, ctr + 128); n   reverse(ctr, ctr + 128); n   long long ans = 0; n   for (int i = 0; i < 128; i++) { n     long long v = min(k, ctr[i]); n     ans += v * v; n     k -= v; n   } n   cout << ans << endl; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   do_case(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<vector<long long> > g; n vector<bool> used; n void dfs(long long v, long long &h, long long &e) { n   used[v] = true; n   h++; n   e += g[v].size(); n   for (int i = 0; i < g[v].size(); ++i) { n     if (!used[g[v][i]]) dfs(g[v][i], h, e); n   } n } n int main() { n   long long n, m; n   cin >> n >> m; n   g.resize(n); n   used.resize(n); n   for (int i = 0; i < m; ++i) { n     long long u, v; n     cin >> u >> v; n     g[u - 1].push_back(v - 1); n     g[v - 1].push_back(u - 1); n   } n   for (int i = 0; i < n; ++i) { n     if (!used[i]) { n       long long h = 0, e = 0; n       dfs(i, h, e); n       if (h * (h - 1) != e) { n         cout <<  NO ; n         return 0; n       } n     } n   } n   cout <<  YES ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long n; n   cin >> n; n   vector<int> res; n   for (int i = n + 1;; i++) { n     if (res.size() == n) { n       break; n     } n     res.push_back(i); n   } n   for (auto x : res) { n     cout << x <<    ; n   } n } n int main() { n   long long t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 99999999; n struct seg_tree { n   int seg[3 * 1000009], lazy[3 * 1000009]; n   void construct_segtree(int start, int end, int pos) { n     lazy[pos] = -inf; n     if (end == start + 1) n       seg[pos] = 0; n     else { n       int mid = (start + end) / 2; n       construct_segtree(start, mid, 2 * pos + 1); n       construct_segtree(mid, end, 2 * pos + 2); n       int t1 = seg[2 * pos + 1]; n       int t2 = seg[2 * pos + 2]; n       seg[pos] = t1 + t2; n     } n   } n   int get_query(int ql, int qh, int low, int high, int pos) { n     if (lazy[pos] != -inf) { n       seg[pos] += lazy[pos]; n       if (high != low + 1) { n         add_lazy(2 * pos + 1, lazy[pos]); n         add_lazy(2 * pos + 2, lazy[pos]); n       } n       lazy[pos] = -inf; n     } n     if (qh <= low || ql >= high) return 0; n     if (low >= ql && high <= qh) return seg[pos]; n     int mid = (low + high) / 2; n     return get_query(ql, qh, low, mid, 2 * pos + 1) + n            get_query(ql, qh, mid, high, 2 * pos + 2); n   } n   void update(int ql, int qh, int low, int high, int val, int pos) { n     int mid = (high + low) / 2; n     if (lazy[pos] != -inf) { n       seg[pos] += lazy[pos]; n       if (high != low + 1) { n         add_lazy(2 * pos + 1, lazy[pos]); n         add_lazy(2 * pos + 2, lazy[pos]); n       } n       lazy[pos] = -inf; n     } n     if (qh <= low || ql >= high) return; n     if (low >= ql && high <= qh) { n       seg[pos] += val; n       if (high != low + 1) { n         add_lazy(2 * pos + 1, val); n         add_lazy(2 * pos + 2, val); n       } n       return; n     } n     update(ql, qh, low, mid, val, 2 * pos + 1); n     update(ql, qh, mid, high, val, 2 * pos + 2); n     seg[pos] = seg[2 * pos + 1] + seg[2 * pos + 2]; n     return; n   } n  n  private: n   void add_lazy(int place, int val) { n     if (lazy[place] == -inf) n       lazy[place] = val; n     else n       lazy[place] += val; n   } n }; n struct Query { n   int first, second, indx; n }; n int bin_search(int first, int second); n void pre_process(); n void fn(); n int comp(Query x, Query y); n int comp2(Query x, Query y); n seg_tree tree; n char input[1000009]; n int arr[1000009], ans[1000009], n_q = 0; n Query q[1000009], open[1000009], close_here[1000009]; n int my_stack[1000009]; n int ten = 0; n int main() { n   scanf( %s , input); n   scanf( %d , &n_q); n   for (int i = 0; i < n_q; i++) scanf( %d %d , &q[i].first, &q[i].second); n   for (int i = 0; i < min(10, (int)strlen(input)); i++) n     if (input[i] ==  ) ) ten++; n   pre_process(); n   fn(); n   for (int i = 0; i < n_q; i++) printf( %d n , ans[i]); n   return 0; n } n void fn() { n   int closed = 0; n   int y = strlen(input); n   for (int i = 0; i <= max((int)y, 0) && closed < n_q; i++) { n     while (closed < n_q && close_here[closed].second <= i) { n       int k = close_here[closed].indx; n       int indx = close_here[closed].indx; n       ans[open[indx].indx] = tree.get_query(indx, indx + 1, 0, n_q, 0); n       closed++; n     } n     if (n_q == closed) return; n     if (i < y && input[i] ==  ) ) { n       if (arr[i] != -1) { n         int x = arr[i]; n         int indx = bin_search(x + 1, -inf); n         if (indx != -1) tree.update(0, indx + 1, 0, n_q, 2, 0); n       } n     } n   } n } n int bin_search(int first, int second) { n   int s = 0, b = n_q, ret = -1; n   while (b >= s) { n     int mid = (b + s) / 2; n     if (open[mid].first < first) n       s = mid + 1, ret = max(ret, mid); n     else n       b = mid - 1; n   } n   return ret; n } n void pre_process() { n   memset(arr, -1, sizeof(arr)); n   int stack_indx = 0; n   for (int i = 0; i < strlen(input); i++) { n     if (input[i] ==  ( ) n       my_stack[stack_indx++] = i; n     else { n       if (stack_indx) { n         int x = my_stack[stack_indx - 1]; n         stack_indx--; n         arr[i] = x; n         arr[x] = i; n       } n     } n   } n   for (int i = 0; i < n_q; i++) q[i].first--; n   for (int i = 0; i < n_q; i++) { n     open[i].first = q[i].first; n     open[i].second = q[i].second; n     open[i].indx = i; n   } n   sort(open, open + n_q, comp); n   for (int i = 0; i < n_q; i++) { n     int k = open[i].indx; n     q[k].indx = i; n   } n   for (int i = 0; i < n_q; i++) close_here[i] = q[i]; n   sort(close_here, close_here + n_q, comp2); n   int prev = 0; n   tree.construct_segtree(0, n_q, 0); n } n int comp(Query x, Query y) { n   if (x.first == y.first) return x.second < y.second; n   return x.first < y.first; n } n int comp2(Query x, Query y) { n   if (x.second == y.second) return x.first < y.first; n   return x.second < y.second; n } n


#include <bits/stdc++.h> n using namespace std; n long long labs(long long a) { return a < 0 ? (-a) : a; } n long long max(long long a, long long b) { return a > b ? a : b; } n long long min(long long a, long long b) { return a < b ? a : b; } n int main() { n   int n; n   cin >> n; n   int h = 1; n   vector<vector<int> > ans; n   for (int i = 0; ans.size() + (h - 1) <= n; i++) { n     ans.push_back(vector<int>()); n     for (int(k) = 0; (k) < ((int)ans.size() - 1); (k)++) { n       ans[i].push_back(h); n       ans[k].push_back(h++); n     } n   } n   cout << ans.size() << endl; n   for (int(i) = 0; (i) < (ans.size()); (i)++) { n     for (int(j) = 0; (j) < (ans[i].size()); (j)++) { n       cout << ans[i][j] <<    ; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   std::ios_base::sync_with_stdio(false); n   float dist, h, w, t; n   cin >> dist; n   cin >> h; n   cin >> w; n   t = dist / (h + w); n   printf( %.12lf n , t * h); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int infinity = 1e9; n int N, M; n int A[509][509]; n int P[509][509]; n int squaresum(int cx, int cy, int k) { n   int ax = cx - k / 2; n   int ay = cy - k / 2; n   int bx = cx + k / 2; n   int by = cy + k / 2; n   return P[bx][by] - P[ax - 1][by] - P[bx][ay - 1] + P[ax - 1][ay - 1]; n } n bool goodk(int i, int j, int k) { n   k /= 2; n   return ((i - k >= 1) && (i + k <= N) && (j - k >= 1) && (j + k <= M)); n } n int main() { n   scanf( %d %d  , &N, &M); n   for (int i = 1; i <= N; i++) n     for (int j = 1; j <= M; j++) scanf( %d , &A[i][j]); n   for (int i = 1; i <= N; i++) n     for (int j = 1; j <= M; j++) n       P[i][j] = P[i - 1][j] + P[i][j - 1] - P[i - 1][j - 1] + A[i][j]; n   int ans = -infinity; n   for (int i = 2; i <= N - 1; i++) n     for (int j = 2; j <= M - 1; j++) { n       int spiral = A[i][j]; n       for (int k = 3; goodk(i, j, k); k += 2) { n         spiral = squaresum(i, j, k) - spiral - A[i - k / 2 + 1][j - k / 2]; n         ans = max(ans, spiral); n       } n     } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100005; n int cnt[maxn], num[maxn], t[maxn], r[maxn], last[maxn]; n char ans[maxn]; n int main() { n   int T, m, k; n   scanf( %d , &T); n   while (T--) { n     scanf( %d%d , &m, &k); n     for (int i = 1; i <= k; i++) { n       scanf( %d , &num[i]); n       ans[i] =  N ; n       cnt[i] = 0; n       last[i] = 0; n     } n     int unknown = 0; n     bool flag = 0; n     for (int i = 1; i <= m - 1; i++) { n       scanf( %d%d , &t[i], &r[i]); n       last[t[i]] = i; n     } n     for (int i = 1; i <= m - 1; i++) { n       if (r[i] == 0) { n         if (t[i]) n           cnt[t[i]]++; n         else n           unknown++; n       } else { n         if (flag == 0) { n           int tmp = 0x3fffffff; n           for (int j = 1; j <= k; j++) { n             if (last[j] < i && cnt[j] + unknown >= num[j]) { n               tmp = min(tmp, num[j] - cnt[j]); n               ans[j] =  Y ; n             } n           } n           unknown -= tmp; n           flag = 1; n         } n         if (t[i]) n           cnt[t[i]]++; n         else n           unknown++; n       } n       if (t[i] != 0 && cnt[t[i]] == num[t[i]]) { n         flag = 1; n       } n     } n     for (int i = 1; i <= k; i++) { n       if (num[i] - cnt[i] - unknown <= 0) { n         ans[i] =  Y ; n       } n     } n     ans[k + 1] = 0; n     printf( %s n , ans + 1); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int Maxn = 1000; n int n, m; n char B[Maxn][Maxn]; n bool takeHor(int r, int c1, int c2) { n   if (c2 >= m) return false; n   if (B[r][c1] !=  W  || B[r][c1 + 1] !=  B  || B[r][c2] !=  W ) return false; n   bool ctake[4]; n   fill(ctake, ctake + 4, true); n   if (r - 1 >= 0) n     for (int j = c1; j <= c2; j++) n       if ( a  <= B[r - 1][j] && B[r - 1][j] <=  d ) n         ctake[B[r - 1][j] -  a ] = false; n   if (r + 1 < n) n     for (int j = c1; j <= c2; j++) n       if ( a  <= B[r + 1][j] && B[r + 1][j] <=  d ) n         ctake[B[r + 1][j] -  a ] = false; n   if (c1 - 1 >= 0 &&  a  <= B[r][c1 - 1] && B[r][c1 - 1] <=  d ) n     ctake[B[r][c1 - 1] -  a ] = false; n   if (c2 + 1 < m &&  a  <= B[r][c2 + 1] && B[r][c2 + 1] <=  d ) n     ctake[B[r][c2 + 1] -  a ] = false; n   for (int i = 0; i < 4; i++) n     if (ctake[i]) { n       for (int j = c1; j <= c2; j++) B[r][j] =  a  + i; n       break; n     } n   return true; n } n bool takeVer(int c, int r1, int r2) { n   if (r2 >= n) return false; n   if (B[r1][c] !=  W  || B[r1 + 1][c] !=  B  || B[r2][c] !=  W ) return false; n   bool ctake[4]; n   fill(ctake, ctake + 4, true); n   if (c - 1 >= 0) n     for (int i = r1; i <= r2; i++) n       if ( a  <= B[i][c - 1] && B[i][c - 1] <=  d ) n         ctake[B[i][c - 1] -  a ] = false; n   if (c + 1 < m) n     for (int i = r1; i <= r2; i++) n       if ( a  <= B[i][c + 1] && B[i][c + 1] <=  d ) n         ctake[B[i][c + 1] -  a ] = false; n   if (r1 - 1 >= 0 &&  a  <= B[r1 - 1][c] && B[r1 - 1][c] <=  d ) n     ctake[B[r1 - 1][c] -  a ] = false; n   if (r2 + 1 < m &&  a  <= B[r2 + 1][c] && B[r2 + 1][c] <=  d ) n     ctake[B[r2 + 1][c] -  a ] = false; n   for (int i = 0; i < 4; i++) n     if (ctake[i]) { n       for (int j = r1; j <= r2; j++) B[j][c] =  a  + i; n       break; n     } n   return true; n } n bool canFill(int r, int c) { n   if (r >= n) return true; n   if (c >= m) return canFill(r + 1, 0); n   if (B[r][c] !=  W  && B[r][c] !=  B ) return canFill(r, c + 1); n   if (B[r][c] ==  B ) return false; n   if (takeHor(r, c, c + 2)) return canFill(r, c + 3); n   if (takeVer(c, r, r + 2)) return canFill(r, c + 1); n   return false; n } n int main() { n   cin >> n >> m; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) { n       cin >> B[i][j]; n       B[i][j] = toupper(B[i][j]); n     } n   if (canFill(0, 0)) { n     cout <<  YES n ; n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < m; j++) cout << B[i][j]; n       cout << endl; n     } n   } else n     cout <<  NO n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100005, P = 1e9 + 7; n int n, s, ans, c[N], f[N]; n inline int pw(int a, int b) { n   int r = 1; n   for (; b; b >>= 1, a = 1ll * a * a % P) n     if (b & 1) r = 1ll * r * a % P; n   return r; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &c[i]), s += c[i]; n   f[1] = 1ll * (s - 1) * (s - 1) % P * pw(s, P - 2) % P; n   for (int i = 1; i < 100000; i++) n     f[i + 1] = ((2 * f[i] % P - f[i - 1] + P) % P - n                 1ll * (s - 1) * pw(s - i, P - 2) % P + P) % n                P; n   for (int i = 1; i <= n; i++) ans = (ans + f[c[i]]) % P; n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int LIMIT = 1e7; n int prime[LIMIT], palin[LIMIT]; n int pr[LIMIT], pa[LIMIT]; n int cnt_dig(int x) { n   if (x == 0) n     return 1; n   else { n     int ans = 0; n     while (x > 0) { n       x /= 10; n       ans++; n     } n     return ans; n   } n } n int main() { n   int n = 4 * 1e6, dg, fl, t; n   stack<int> st; n   for (int i = 0; i < n; i++) prime[i] = true; n   prime[0] = prime[1] = false; n   prime[2] = true; n   for (long long i = 2; i < n; i++) { n     if (prime[i] == true) { n       for (long long j = i * i; j < n; j += i) prime[j] = false; n     } n   } n   for (int i = 0; i < n; i++) { n     dg = cnt_dig(i); n     t = i; n     if (dg == 1) n       palin[i] = true; n     else { n       fl = 0; n       if (dg & 1) { n         for (int j = 0; j < dg / 2; j++) { n           st.push(t % 10); n           t = t / 10; n         } n         t = t / 10; n         for (int j = 0; j < dg / 2; j++) { n           if (t % 10 == st.top()) { n             t = t / 10; n             st.pop(); n           } else { n             fl = 1; n             break; n           } n         } n         st.empty(); n       } else { n         for (int j = 0; j < dg / 2; j++) { n           st.push(t % 10); n           t = t / 10; n         } n         for (int j = 0; j < dg / 2; j++) { n           if (t % 10 == st.top()) { n             t = t / 10; n             st.pop(); n           } else { n             fl = 1; n             break; n           } n         } n         st.empty(); n       } n       if (fl == 0) n         palin[i] = true; n       else n         palin[i] = false; n     } n   } n   for (int i = 1; i < n; i++) { n     pr[i] = prime[i] + pr[i - 1]; n     pa[i] = palin[i] + pa[i - 1]; n   } n   double p, q; n   cin >> p >> q; n   int l = 1, r = n - 1; n   t = 0; n   int ans = 0; n   for (int i = 1; i < n; i++) { n     if (pr[i] <= double(pa[i] * (p / q))) ans = i; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = INT_MAX / 2; n const int Maxn = 305; n int a[Maxn + 5][Maxn + 5], f[Maxn + 5][Maxn + 5], g[Maxn + 5][Maxn + 5], n; n int solve() { n   for (int(i) = (1); (i) <= (n); ++(i)) n     for (int(j) = (1); (j) <= (n); ++(j)) f[i][j] = -inf; n   f[1][1] = a[1][1]; n   for (int(k) = (2); (k) <= (n * 2 - 1); ++(k)) { n     for (int(i) = (1); (i) <= (n); ++(i)) n       for (int(j) = (1); (j) <= (n); ++(j)) g[i][j] = -inf; n     for (int(i) = (1); (i) <= (n); ++(i)) n       for (int(j) = (i); (j) <= (n); ++(j)) { n         if (f[i][j] <= -inf) continue; n         for (int(si) = (i); (si) <= (i + 1); ++(si)) n           for (int(sj) = (j); (sj) <= (j + 1); ++(sj)) { n             if (si > sj || si > n || sj > n || k + 1 - si > n || k + 1 - sj > n) n               continue; n             int bonus; n             if (si == sj) n               bonus = a[k + 1 - si][si]; n             else n               bonus = a[k + 1 - si][si] + a[k + 1 - sj][sj]; n             g[si][sj] = max(g[si][sj], f[i][j] + bonus); n           } n       } n     for (int(i) = (1); (i) <= (n); ++(i)) n       for (int(j) = (1); (j) <= (n); ++(j)) f[i][j] = g[i][j]; n   } n   return f[n][n]; n } n int main() { n   iostream::sync_with_stdio(false); n   cin >> n; n   for (int(i) = (1); (i) <= (n); ++(i)) n     for (int(j) = (1); (j) <= (n); ++(j)) cin >> a[i][j]; n   cout << solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int msb(long long x) { n   union { n     double a; n     int32_t b[2]; n   }; n   a = x; n   return (b[1] >> 20) - 1023; n } n void sol() { n   int n, x, y; n   vector<int> a(4), b(4); n   for (int i = (0); i < (4); i++) { n     cin >> a[i]; n   } n   if ((a[0] + a[1]) & 1) { n     if (a[0] || a[3]) b[0]++; n     if (a[1] || a[2]) b[1]++; n   } else { n     if (a[2] || a[1]) b[2]++; n     if (a[3] || a[0]) b[3]++; n   } n   for (int i = (0); i < (4); i++) cout << (b[i] ?  Ya   :  Tidak  ); n   cout <<   n ; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t = 1; n   cin >> t; n   while (t--) sol(); n } n


#include <bits/stdc++.h> n int main() { n   int n, k; n   scanf( %d %d , &n, &k); n   char a[n]; n   scanf( %s , a); n   int i, start, end; n   for (i = 0; i < n; i++) { n     if (a[i] ==  G ) start = i; n     if (a[i] ==  T ) end = i; n   } n   int sign = start - end; n   if (start > end) k = -k; n   while ((sign * (start - end)) > 0) { n     start = start + k; n     if (a[start] ==  # ) { n       printf( NO ); n       break; n     } n     if (a[start] ==  T ) { n       printf( YES ); n       break; n     } n   } n   if (((sign * (start - end)) < 0) && (a[start] !=  # )) printf( NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MAXN = 1e3 + 10, inf = 1e18, mod = 1e9 + 7; n struct pair_hash { n   template <class T1, class T2> n   std::size_t operator()(const std::pair<T1, T2> &p) const { n     auto h1 = std::hash<T1>{}(p.first); n     auto h2 = std::hash<T2>{}(p.second); n     return h1 ^ h2; n   } n }; n long long pw(long long te, long long tee) { n   if (tee == 0) return 1; n   long long res = pw(te, tee / 2); n   return (tee % 2 ? te * res * res : res * res); n } n long long n; n char a[MAXN][MAXN], ans[MAXN][MAXN]; n vector<pair<long long, long long>> v, t, p; n bool flag = true; n void Read_input() { n   cin >> n; n   for (long long i = 1; i <= n; i++) n     for (long long j = 1; j <= n; j++) { n       cin >> a[i][j]; n       if (a[i][j] ==  o ) v.push_back({i, j}); n       if (a[i][j] ==  x ) p.push_back({i, j}); n     } n } n void Solve() { n   for (long long d1 = 0; d1 <= n; d1++) { n     for (long long d2 = 0; d2 <= n; d2++) { n       if (d1 == 0 and d2 == 0) continue; n       bool f[4] = {true, true, true, true}; n       for (auto i : v) { n         long long x = i.first + d1; n         long long y = i.second + d2; n         if (x <= n and y <= n and a[x][y] ==  . ) f[0] = false; n         x = i.first + d1; n         y = i.second - d2; n         if (x <= n and y >= 1 and a[x][y] ==  . ) f[1] = false; n         x = i.first - d1; n         if (x >= 1 and y >= 1 and a[x][y] ==  . ) f[2] = false; n         y = i.second + d2; n         if (x >= 1 and y <= n and a[x][y] ==  . ) f[3] = false; n       } n       if (f[0]) t.push_back({d1, d2}); n       if (f[1]) t.push_back({d1, -d2}); n       if (f[2]) t.push_back({-d1, -d2}); n       if (f[3]) t.push_back({-d1, d2}); n     } n   } n   for (long long i = 1; i <= 2 * n - 1; i++) n     for (long long j = 1; j <= 2 * n - 1; j++) ans[i][j] =  . ; n   for (auto i : p) { n     bool k = false; n     for (auto j : t) n       if (i.first - j.first >= 1 and i.second - j.second >= 1 and n           a[i.first - j.first][i.second - j.second] ==  o ) { n         k = true; n         break; n       } n     if (k == false) flag = false; n     if (!flag) break; n   } n   for (auto i : v) { n     bool k = true; n     for (auto j : t) n       if (i.first + j.first <= n and i.second + j.second <= n and n           a[i.first + j.first][i.second + j.second] ==  . ) { n         k = false; n         break; n       } n     if (k == false) flag = false; n     if (!flag) break; n   } n   if (!flag) { n     cout <<  NO ; n     exit(0); n   } n   cout <<  YES  << endl; n   ans[n][n] =  o ; n   for (auto i : t) n     if (n + i.first <= 2 * n - 1 and n + i.first >= 1 and n         n + i.second <= 2 * n - 1 and n + i.second >= 1) n       ans[n + i.first][n + i.second] =  x ; n   for (long long i = 1; i <= 2 * n - 1; i++) { n     for (long long j = 1; j <= 2 * n - 1; j++) cout << ans[i][j]; n     cout << endl; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   Read_input(), Solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long t, i, n, res, mx, a[300009]; n map<int, int> mp; n int main() { n   cin >> n; n   for (i = 1; i <= 2 * n; i++) { n     cin >> a[i]; n   } n   for (i = 1; i <= 2 * n; i++) { n     mp[a[i]]++; n     if (mp[a[i]] == 1) { n       res++; n     } else { n       res--; n     } n     if (res >= mx) { n       mx = res; n     } n   } n   cout << mx; n } n


#include <bits/stdc++.h> n #pragma warning(disable : 4996) n #pragma comment(linker,  /stack:200000000 ) n #pragma GCC optimize( Ofast ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) n #pragma GCC optimize( -ffloat-store ) n using namespace std; n clock_t time_p = clock(); n void aryanc403() { n   time_p = clock() - time_p; n   cerr <<  Time Taken :   << (float)(time_p) / CLOCKS_PER_SEC <<   n ; n } n const long long int INF = 0xFFFFFFFFFFFFFFFL; n long long int seed; n mt19937 rng(seed = chrono::steady_clock::now().time_since_epoch().count()); n inline long long int rnd(long long int l = 0, long long int r = INF) { n   return uniform_int_distribution<long long int>(l, r)(rng); n } n class CMP { n  public: n   bool operator()(pair<long long int, long long int> a, n                   pair<long long int, long long int> b) { n     return !(a.second < b.second || n              (a.second == b.second && a.first >= b.first)); n   } n }; n void add(map<long long int, long long int> &m, long long int x, n          long long int cnt = 1) { n   auto jt = m.find(x); n   if (jt == m.end()) n     m.insert({x, cnt}); n   else n     jt->second += cnt; n } n void del(map<long long int, long long int> &m, long long int x, n          long long int cnt = 1) { n   auto jt = m.find(x); n   if (jt->second <= cnt) n     m.erase(jt); n   else n     jt->second -= cnt; n } n bool cmp(const pair<long long int, long long int> &a, n          const pair<long long int, long long int> &b) { n   return a.first < b.first || (a.first == b.first && a.second < b.second); n } n const long long int mod = 1000000007L; n const long long int maxN = 100000L; n long long int T, n, i, j, k, in, cnt, l, r, u, v, x, y, ans; n long long int m; n string s; n vector<pair<long long int, long long int> > a, e[maxN + 5]; n long long int d[maxN + 5]; n priority_queue<pair<long long int, long long int>, n                vector<pair<long long int, long long int> >, CMP> n     pq; n int main(void) { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   { n     cin >> n >> m >> k; n     a.clear(); n     a.reserve(k); n     for (i = 0; i < (m); ++i) { n       cin >> x >> y >> in; n       e[x].push_back({y, in}); n       e[y].push_back({x, in}); n     } n     for (i = (1); i <= (n); ++i) d[i] = INF; n     d[1] = 0; n     for (i = 0; i < (k); ++i) { n       cin >> x >> in; n       a.push_back({x, in}); n       pq.push({-x, in}); n     } n     for (auto x : e[1]) pq.push({x.first, x.second}); n     while (!pq.empty()) { n       auto z = pq.top(); n       pq.pop(); n       long long int u = abs(z.first); n       if (d[u] <= z.second) { n         ans += (z.first < 0); n         if (z.first < 0) n           ; n         continue; n       } n       d[u] = z.second; n       for (auto x : e[u]) pq.push({x.first, x.second + z.second}); n     } n     cout << ans <<   n ; n   } n   aryanc403(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> adj[100005]; n int n; n long long sz[100005]; n long double E[100005]; n void dfs1(int u, int p) { n   sz[u] = 1; n   for (auto v : adj[u]) { n     if (v == p) continue; n     dfs1(v, u); n     sz[u] += sz[v]; n   } n } n void dfs2(int u, int p) { n   if (u) { n     E[u] = E[p] + 1 + 1.0 * (sz[p] - sz[u] - 1) / 2.0; n   } else n     E[u] = 1; n   for (auto v : adj[u]) { n     dfs2(v, u); n   } n } n int main() { n   ios_base::sync_with_stdio(false), cin.tie(NULL); n   ; n   cin >> n; n   for (int i = 1; i < n; i++) { n     int p; n     cin >> p; n     p--; n     adj[p].push_back(i); n   } n   dfs1(0, -1); n   dfs2(0, -1); n   cout << fixed << setprecision(7); n   for (int i = 0; i < n; i++) { n     cout << E[i]; n     if (i < n) cout <<    ; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long E[50]; n int get_len(long long s) { n   int cnt(0); n   while (s) cnt++, s /= 10; n   return cnt; n } n int find(long long n, int l) { n   if (n == 0) return 0; n   int len = get_len(n); n   if (n < E[l]) { n     return min(find(n, l - 1), find(E[l] - n, l - 1) + l); n   } n   int k; n   for (k = 1;; k++) n     if (E[l] * k > n) break; n   k--; n   return find(n - E[l] * k, l) + k * l; n } n int main() { n   E[1] = 1; n   for (int i = 2; i < 20; i++) E[i] = E[i - 1] * 10 + 1; n   long long n; n   while (cin >> n) { n     int len = get_len(n); n     cout << min(find(n, len), find(E[len + 1] - n, len) + len + 1) << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   map<int, int> pos1; n   map<int, int> pos2; n   int n; n   cin >> n; n   int a[n], b[n]; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     pos1[a[i]] = i; n   } n   for (int i = 0; i < n; i++) { n     cin >> b[i]; n     pos2[b[i]] = i; n   } n   map<int, int> h; n   for (int i = 1; i <= n; i++) { n     if (pos1[i] <= pos2[i]) { n       h[pos2[i] - pos1[i]]++; n     } else { n       h[n - pos1[i] + pos2[i]]++; n     } n   } n   int mx = 0; n   for (int i = 0; i <= 2 * n; i++) { n     if (h[i] > mx) mx = h[i]; n   } n   cout << mx; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 51000; n int a[1000000], b[1000000], t[1000000], n, flag = 0; n void dfs(int m) { n   if (flag) return; n   if (m == n) { n     cout <<  YES  << endl; n     for (int i = 1; i < n; i++) { n       cout << t[i] <<    ; n     } n     cout << t[n] << endl; n     flag = 1; n     return; n   } n   for (int i = 0; i <= 3; i++) { n     t[m + 1] = i; n     if (a[m] == (t[m + 1] | t[m]) && b[m] == (t[m + 1] & t[m])) { n       dfs(m + 1); n     } n   } n } n int main() { 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     cin >> b[i]; n   } n   for (int i = 0; i <= 3; i++) { n     t[1] = i; n     dfs(1); n     if (flag) break; n   } n   if (!flag) cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 155; n int a[N], n, k, s, oo = 1e9, dp[N][N * N], ans; n int main() { n   cin >> n >> k >> s; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   s = min(s, n * (n - 1) / 2); n   for (int i = 0; i <= n; i++) n     for (int j = 0; j <= n * n; j++) dp[i][j] = oo; n   dp[0][0] = 0; n   for (int l = 1; l <= n; l++) { n     for (int i = l; i; i--) { n       for (int j = l - i; j <= i * n; j++) { n         dp[i][j] = min(dp[i][j], dp[i - 1][j - (l - i)] + a[l]); n       } n     } n   } n   ans = dp[k][0]; n   for (int i = 0; i <= s; i++) ans = min(ans, dp[k][i]); n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int fun(pair<int, int> x, pair<int, int> y) { return x.second < y.second; } n int main() { n   vector<pair<int, int> > vii; n   int n, k; n   cin >> n >> k; n   for (int i = 0; i < n; i++) { n     int x; n     cin >> x; n     vii.push_back(make_pair(x, x % 10)); n   } n   int sum; n   while (k > 0) { n     sort(vii.begin(), vii.end(), fun); n     sum = 0; n     int flg = 1; n     for (int i = 0; i < n; i++) { n       if (vii[i].first == 100) n         ; n       else n         flg = 0; n     } n     if (!flg) { n       for (int i = n - 1; i >= 0; i--) { n         if (vii[i].first >= 100) { n           sum += 10; n           continue; n         } n         if (k > 0) { n           int ink = ((vii[i].first + 10) / 10) * 10 - vii[i].first; n           if (ink > k) ink = k; n           k = k - ink; n           vii[i].first += ink; n           vii[i].second = vii[i].first % 10; n         } n         sum += vii[i].first / 10; n       } n     } else { n       sum = n * 10; n       break; n     } n   } n   printf( %d n , sum); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 5; n int ans[maxn]; n struct node { n   int l, r; n   bool operator<(const node& a) const { n     int s1 = r - l + 1; n     int s2 = a.r - a.l + 1; n     if (s1 == s2) n       return a.l < l; n     else n       return a.r - a.l + 1 > r - l + 1; n   } n }; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     int tot = 0; n     priority_queue<node> q; n     q.push({1, n}); n     while (!q.empty()) { n       node u = q.top(); n       q.pop(); n       int mid = (u.l + u.r) / 2; n       ans[mid] = ++tot; n       if (mid + 1 <= u.r) { n         q.push({mid + 1, u.r}); n       } n       if (mid - 1 >= u.l) { n         q.push({u.l, mid - 1}); n       } n     } n     for (int i = 1; i <= n; i++) { n       cout << ans[i] <<    ; n     } n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n const int mod = 1e9 + 7; n const int maxn = 1e6 + 3; n using namespace std; n int bj[maxn]; n long long a[100010], f[maxn], inv[maxn], pri[maxn]; n long long pre[maxn], per[maxn]; n long long BM(long long k) { n   pre[0] = 1; n   per[0] = 1; n   for (int i = 1; i <= maxn; i++) { n     pre[i] = (k - i) % mod; n     per[i] = (k - (maxn + 1 - i)) % mod; n   } n   for (int i = 1; i <= maxn; i++) { n     pre[i] = pre[i - 1] * pre[i] % mod; n     per[i] = per[i - 1] * per[i] % mod; n   } n   long long ans = 0; n   for (int i = 1; i <= maxn; i++) { n     if ((maxn - i) & 1) n       ans = (ans - n              f[i] * pre[i - 1] % mod * per[maxn - i] % mod * inv[i - 1] % mod * n                  inv[maxn - i] % mod + n              mod) % n             mod; n     else n       ans = (ans + f[i] * pre[i - 1] % mod * per[maxn - i] % mod * inv[i - 1] % n                        mod * inv[maxn - i] % mod) % n             mod; n   } n   return ans; n } n int ksm(int a, int b) { n   int res = 1; n   while (b) { n     if (b & 1) res = 1ll * res * a % mod; n     a = 1ll * a * a % mod; n     b >>= 1; n   } n   return res; n } n int main() { n   inv[0] = 1; n   inv[1] = 1; n   for (int i = 2; i <= maxn; i++) n     inv[i] = (mod - mod / i) * 1ll * inv[mod % i] % mod; n   for (int i = 2; i <= maxn; i++) inv[i] = inv[i - 1] * inv[i] % mod; n   int n, k; n   scanf( %d%d , &n, &k); n   if (n <= maxn) { n     long long ans = 0; n     for (int i = 1; i <= n; i++) { n       ans += ksm(i, k); n       ans %= mod; n     } n     printf( %lld n , ans); n     return 0; n   } n   int cnt = 0; n   f[0] = 1; n   f[1] = 1; n   for (int i = 2; i <= maxn; i++) { n     if (!bj[i]) f[i] = ksm(i, k), pri[++cnt] = i; n     for (int j = 1; j <= cnt && i * pri[j] <= maxn; j++) { n       bj[i * pri[j]] = 1; n       f[i * pri[j]] = f[i] * f[pri[j]] % mod; n       if (i % pri[j] == 0) break; n     } n   } n   for (int i = 2; i <= maxn; i++) f[i] = (f[i - 1] + f[i]) % mod; n   printf( %lld n , BM(n)); n   return 0; n } n


#include <bits/stdc++.h> n unsigned long long u64; n using namespace std; n int n, m, qr; n long long c[2 * 200000], cnt[2 * 200000]; n void lazy_update(int v, int i, int j, int qi, int qj, long long x) { n   if (qi > j || qj < i) return; n   if (qi <= i && j <= qj) { n     c[v] += (x + i - qi), ++cnt[v]; n     return; n   } n   int k = (i + j) / 2; n   lazy_update(2 * v, i, k, qi, qj, x), n       lazy_update(2 * v + 1, k + 1, j, qi, qj, x); n } n long long query(int v, int i, int j, int pos) { n   if (pos < i || pos > j) return 0; n   if (i == j) { n     assert(pos == i); n     return c[v]; n   } n   int k = (i + j) / 2; n   if (c[v] > 0) { n     c[2 * v] += c[v], cnt[2 * v] += cnt[v]; n     c[2 * v + 1] += (c[v] + cnt[v] * (k + 1 - i)), cnt[2 * v + 1] += cnt[v]; n     c[v] = cnt[v] = 0; n   } n   return max(query(2 * v, i, k, pos), query(2 * v + 1, k + 1, j, pos)); n } n int main() { n   int i, j, k, l, r; n   long long x; n   while (cin >> n >> m >> qr) { n     memset(c, 0, sizeof(c)); n     memset(cnt, 0, sizeof(cnt)); n     while (m--) { n       cin >> l >> r >> x; n       lazy_update(1, 1, n, l, r, x); n     } n     long long ax = 0; n     while (qr--) { n       cin >> i; n       ax += query(1, 1, n, i); n     } n     cout << ax << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int mod = 1e9 + 7; n const int size = 1e6 + 1; n int n; n double a[size]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   cin >> n; n   double l, r, c1, c2; n   l = r = c1 = c2 = 0; n   string s; n   cin >> s; n   for (int i = int(0); i < int(n); i++) { n     if (s[i] ==  ? ) n       a[i] = -1; n     else n       a[i] = s[i] -  0 ; n     if ((i) < n / 2) { n       if (a[i] != -1) n         l += a[i]; n       else n         c1++; n     } else { n       if (a[i] != -1) n         r += a[i]; n       else n         c2++; n     } n   } n   bool ans = false; n   if (l + 9 * ceil(double(c1 / 2.0)) != r + 9 * ceil(double(c2 / 2.0))) n     ans = true; n   if (!ans) n     cout <<  Bicarp ; n   else n     cout <<  Monocarp ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int N = 1000000000; n int main(int argc, char const *argv[]) { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t; n   cin >> t; n   while (t-- > 0) { n     int n; n     cin >> n; n     string s, s1; n     cin >> s >> s1; n     int x = 0; n     int op = 0; n     vector<int> opr; n     for (int i = n - 1; i >= 0; i--) { n       if (op % 2 == 0) { n         if (s[x + i] == s1[i]) continue; n         if (s[x] != s[x + i]) opr.push_back(1); n         opr.push_back(i + 1); n         op++; n         x = x + i; n       } else { n         char c = s[x - i] ==  0  ?  1  :  0 ; n         char c1 = s[x] ==  0  ?  1  :  0 ; n         if (c == s1[i]) continue; n         if (c != c1) opr.push_back(1); n         opr.push_back(i + 1); n         op++; n         x = x - i; n       } n     } n     cout << opr.size() <<    ; n     for (auto i : opr) cout << i <<    ; n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long U[10001], V[10000], dis[10000]; n long long n, m; n vector<long long> adj[10001]; n bool bfs() { n   queue<long long> q; n   for (long long i = 1; i <= n; i++) { n     if (U[i] == 0) { n       dis[i] = 0; n       q.push(i); n     } else n       dis[i] = INT_MAX; n   } n   dis[0] = INT_MAX; n   while (!q.empty()) { n     long long x = q.front(); n     q.pop(); n     if (dis[x] < dis[0]) { n       for (long long j = 0; j < adj[x].size(); j++) { n         if (dis[V[adj[x][j]]] == INT_MAX) { n           dis[V[adj[x][j]]] = dis[x] + 1; n           q.push(V[adj[x][j]]); n         } n       } n     } n   } n   return (dis[0] != INT_MAX); n } n bool dfs(long long u) { n   if (u != 0) { n     for (long long j = 0; j < adj[u].size(); j++) { n       if (dis[V[adj[u][j]]] == (dis[u] + 1)) { n         if (dfs(V[adj[u][j]])) { n           V[adj[u][j]] = u; n           U[u] = adj[u][j]; n           return true; n         } n       } n     } n     dis[u] = INT_MAX; n     return false; n   } n   return true; n } n long long hopcroft() { n   for (long long i = 0; i <= 10000; i++) { n     U[i] = 0; n     V[i] = 0; n   } n   long long ans = 0; n   while (bfs()) { n     for (long long u = 1; u <= n; u++) { n       if (U[u] == 0 && dfs(u)) ans++; n     } n   } n   return ans; n } n void solve() { n   cin >> n >> m; n   vector<pair<long long, pair<long long, long long> > > edges; n   for (long long i = 0; i < m; i++) { n     long long a, b, c; n     cin >> a >> b >> c; n     edges.push_back({c, {a, b}}); n   } n   long long l = 0, r = 1000000000, f = -1; n   while (l <= r) { n     long long mid = (l + r) / 2; n     for (long long i = 1; i <= n; i++) adj[i].clear(); n     for (auto x : edges) { n       if (x.first <= mid) { n         adj[x.second.first].push_back(x.second.second); n       } n     } n     if (hopcroft() == n) { n       r = mid - 1; n       f = mid; n     } else n       l = mid + 1; n   } n   cout << f <<   n ; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long test = 0, t; n   t = 1; n   while (test++ < t) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = (int)1e9 + 7; n const int MOD = (int)998244353; n const int INF = (int)1e18; n bool cmp(const pair<long long, long long>& a, n          const pair<long long, long long>& b) { n   if (a.second == b.second) return a.first < b.first; n   return a.second > b.second; n } n void solve() { n   long long a, b, x, y, p, q; n   cin >> a >> b >> x >> y >> p >> q; n   vector<long long> v; n   long long cnt = 0; n   map<pair<long long, int>, int> m1; n   long long n, m; n   n = a + x - p; n   m = b + y - q; n   v.push_back(n); n   v.push_back(m); n   m1[{n, m}] = 1; n   cnt++; n   n = a - x + p; n   m = b - y + q; n   if (m1[{n, m}] == 0) { n     cnt++; n     m1[{n, m}] = 1; n     v.push_back(n); n     v.push_back(m); n   } n   n = -a + x + p; n   m = -b + y + q; n   if (m1[{n, m}] == 0) { n     cnt++; n     m1[{n, m}] = 1; n     v.push_back(n); n     v.push_back(m); n   } n   cout << cnt <<   n ; n   cnt = ((int)(v).size()); n   for (long long i = 0; i < cnt; i++) { n     cout << v[i] <<    ; n     if (i & 1) cout <<   n ; n   } n   return; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long t = 1, m = 1; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n ll gcd(ll a, ll b) { n   while (b) { n     a %= b; n     swap(a, b); n   } n   return a; n } n ll nCr(ll n, ll r) { n   ll a = 1, b = 1; n   if (n < r) return 0; n   if (r == 0) return 1; n   if (n - r < r) r = n - r; n   while (r) { n     a *= n; n     b *= r; n     ll m = gcd(a, b); n     a /= m; n     b /= m; n     n--, r--; n   } n   return a; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   int n, d; n   cin >> n >> d; n   vector<int> a(n); n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   vector<int> b, c; n   ll ans = 0; n   int j = 1, prevJ = 0; n   for (int i = 0; j < n; i++) { n     for (j; j < n; j++) { n       if (a[j] - a[i] > d) break; n     } n     ans += (nCr(j - i, 3) - nCr((j - i) - (j - prevJ), 3)); n     prevJ = j; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = (int)2e5 + 5; n const int INF = (int)1e9 + 7; n int dx[8] = {1, 0, -1, 0, -1, -1, 1, 1}; n int dy[8] = {0, 1, 0, -1, -1, 1, -1, 1}; n int ini() { n   int x; n   scanf( %d , &x); n   return x; n } n long long inl() { n   long long x; n   scanf( %lld , &x); n   return x; n } n vector<int> v; n int main() { n   int n, i, j, m; n   n = ini(); n   m = ini(); n   for (i = 1; i <= n; i++) { n     int x = ini(); n     v.push_back(x); n   } n   sort(v.begin(), v.end()); n   for (i = 1; i <= m; i++) { n     int x; n     std::vector<int>::iterator low, up; n     x = ini(); n     up = upper_bound(v.begin(), v.end(), x); n     cout << (up - v.begin()) <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 105; n int N, M; n int A[MAXN]; n int ans[MAXN][MAXN]; n void gen(int x) { n   if (x <= 2) { n     for (int i = 0; i < x; i++) { n       A[i] = 3 + i; n     } n     return; n   } else if (x == 3) { n     A[0] = 1; n     A[1] = A[2] = 2; n     return; n   } n   if (x % 2) { n     A[0] = x - 2; n     fill(A + 1, A + x, 2); n   } else { n     A[0] = x / 2 - 1; n     fill(A + 1, A + x, 1); n   } n } n int main() { n   if (fopen( input.txt ,  r )) { n     freopen( input.txt ,  r , stdin); n   } n   ios::sync_with_stdio(false); n   cin >> N >> M; n   gen(M); n   for (int i = 0; i < N; i++) { n     copy_n(A, M, ans[i]); n   } n   gen(N); n   for (int j = 0; j < M; j++) { n     for (int i = 0; i < N; i++) { n       ans[i][j] *= A[i]; n     } n   } n   for (int i = 0; i < N; i++) { n     for (int j = 0; j < M; j++) { n       cout << ans[i][j] <<    ; n     } n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1000 + 17; n int n, A[MAXN], L[7], R[7]; n void Solve(int u) { n   bool st = true; n   for (int i = 1; i <= n; ++i) n     if (A[i] != i) st = false; n   if (st) { n     cout << u - 1 <<   n ; n     for (int i = u - 1; i >= 1; --i) cout << L[i] <<     << R[i] <<   n ; n     exit(0); n   } n   if (u > 3) return; n   for (int i = 1; i <= n; ++i) n     if (A[i] != i && (abs(A[i] - A[i - 1]) > 1 || abs(A[i] - A[i + 1]) > 1)) { n       L[u] = i; n       for (int j = i + 1; j <= n; ++j) n         if ((A[j] != j && (abs(A[j] - A[j - 1]) > 1)) || n             abs(A[j] - A[j + 1]) > 1) { n           reverse(A + i, A + j + 1); n           R[u] = j; n           Solve(u + 1); n           reverse(A + i, A + j + 1); n         } n     } n } n int main() { n   cin >> n; n   A[0] = A[n + 1] = -1; n   for (int i = 1; i <= n; ++i) cin >> A[i]; n   Solve(1); n } n


#include <bits/stdc++.h> n using namespace std; n int n, res[300010], c[300010], trie[300010][30], dif[300010], ans, cans; n vector<vector<int> > gr; n string s; n void merge(int big, int small) { n   dif[big] = 1; n   for (int i = 0; i < 26; ++i) { n     if (trie[small][i] and trie[big][i]) { n       merge(trie[big][i], trie[small][i]); n     } else if (trie[small][i] and trie[big][i] == 0) { n       trie[big][i] = trie[small][i]; n     } n     dif[big] += dif[trie[big][i]]; n   } n } n void dfs(int x, int par) { n   dif[x] = 1; n   for (auto i : gr[x]) { n     if (i == par) { n       continue; n     } n     dfs(i, x); n     int xCh = trie[x][s[i - 1] -  a ]; n     if (!xCh) { n       trie[x][s[i - 1] -  a ] = i; n       dif[x] += dif[i]; n     } else { n       dif[x] -= dif[xCh]; n       merge(xCh, i); n       dif[x] += dif[xCh]; n     } n   } n   res[x] = dif[x] + c[x]; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cin >> n; n   gr.resize(n + 1, vector<int>()); n   for (int i = 1; i <= n; ++i) { n     cin >> c[i]; n   } n   cin >> s; n   for (int i = 0; i < n - 1; ++i) { n     int u, v; n     cin >> u >> v; n     gr[u].push_back(v); n     gr[v].push_back(u); n   } n   dfs(1, -1); n   for (int i = 1; i <= n; ++i) { n     ans = max(ans, res[i]); n   } n   for (int i = 1; i <= n; ++i) { n     if (res[i] == ans) { n       ++cans; n     } n   } n   cout << ans <<   n ; n   cout << cans; n } n


#include <bits/stdc++.h> n using namespace std; n long long fact[200001], inv_fact[200001], dp[2010][26]; n bool cmp(pair<int, int> a, pair<int, int> b) { n   return a.first + a.second < b.first + b.second; n } n long long ModExp(int a, int n) { n   long long x = a % 1000000007, y = 1 % 1000000007; n   while (n > 0) { n     if (n % 2) y = (y * x) % 1000000007; n     x = (x * x) % 1000000007; n     n /= 2; n   } n   return y; n } n long long ModInverse(int a) { return ModExp(a, 1000000007 - 2); } n void PreCompute() { n   fact[0] = inv_fact[0] = 1; n   for (int i = 1; i < 200001; i++) { n     fact[i] = (i * fact[i - 1]) % 1000000007; n     inv_fact[i] = (ModInverse(i) * inv_fact[i - 1]) % 1000000007; n   } n } n long long nCr(int n, int r) { n   if (n < 0 || r < 0 || n < r) n     return 0; n   else n     return (fact[n] * ((inv_fact[r] * inv_fact[n - r]) % 1000000007)) % n            1000000007; n } n long long NumPaths(pair<int, int> a, pair<int, int> b) { n   return nCr((b.first - a.first) + (b.second - a.second), (b.first - a.first)); n } n int main() { n   int n, m, k, p, i, j, x, y, z; n   long long P, Q, ans; n   set<pair<int, int> > s; n   vector<pair<int, int> > v; n   scanf( %d%d%d%d , &n, &m, &k, &p); n   for (x = 1; x <= k; x++) { n     scanf( %d%d , &i, &j); n     s.insert(make_pair(i, j)); n   } n   s.insert(make_pair(1, 1)); n   if (s.size() == k) p = (p - 1) / 2 + 1; n   for (x = p, k = 0; x > 1; k++, x = (x - 1) / 2 + 1) n     ; n   v.push_back(make_pair(0, 0)); n   for (auto it = s.begin(); it != s.end(); it++) v.push_back(*it); n   PreCompute(); n   for (i = v.size() - 1; i > 0; i--) { n     long long sum = 0; n     for (j = 0; j < k; j++) { n       dp[i][j] = NumPaths(v[i], make_pair(n, m)); n       for (x = i + 1; x < v.size(); x++) n         dp[i][j] = (dp[i][j] + (1000000007 - (NumPaths(v[i], v[x]) * dp[x][j]) % n                                                  1000000007)) % n                    1000000007; n       dp[i][j] = (dp[i][j] + (1000000007 - sum)) % 1000000007; n       sum = (sum + dp[i][j]) % 1000000007; n     } n   } n   long long sum = 0; n   for (i = 1, j = 0, P = 0; j < k; j++, p = (p - 1) / 2 + 1) { n     P = (P + dp[i][j] * p) % 1000000007; n     sum = (sum + dp[i][j]) % 1000000007; n   } n   Q = NumPaths(make_pair(1, 1), make_pair(n, m)); n   sum = (Q + 1000000007 - sum) % 1000000007; n   P = (P + sum) % 1000000007; n   ans = (P * ModInverse((int)Q)) % 1000000007; n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<vector<int> > g; n vector<int> lista; n vector<int> posicion; n vector<set<int> > s; n void genera(int u, int p, int lis, int pos) { n   lista[u] = lis; n   posicion[u] = pos; n   vector<int> &ar = g[u]; n   for (int i = 0; i < int(ar.size()); i++) n     if (ar[i] != p) genera(ar[i], u, lis, pos + 1); n } n int main() { n   ios::sync_with_stdio(false); n   int n; n   cin >> n; n   g = vector<vector<int> >(n); n   vector<pair<int, int> > arista; n   for (int i = 0; i < n - 1; i++) { n     int u, v; n     cin >> u >> v; n     u--; n     v--; n     g[u].push_back(v); n     g[v].push_back(u); n     arista.push_back(pair<int, int>(u, v)); n   } n   int umax = 0; n   lista = posicion = vector<int>(n); n   for (int u = 1; u < n; u++) n     if (int(g[u].size()) > int(g[umax].size())) umax = u; n   { n     lista[umax] = posicion[umax] = -1; n     vector<int> &ar = g[umax]; n     for (int i = 0; i < int(ar.size()); i++) genera(ar[i], umax, i, 1); n     s = vector<set<int> >(int(ar.size())); n   } n   int m; n   cin >> m; n   for (int i = 0; i < m; i++) { n     int tipo; n     cin >> tipo; n     if (tipo == 1) { n       int indar; n       cin >> indar; n       indar--; n       int u; n       if (posicion[arista[indar].first] < posicion[arista[indar].second]) n         u = arista[indar].second; n       else n         u = arista[indar].first; n       s[lista[u]].erase(posicion[u]); n     } else if (tipo == 2) { n       int indar; n       cin >> indar; n       indar--; n       int u; n       if (posicion[arista[indar].first] < posicion[arista[indar].second]) n         u = arista[indar].second; n       else n         u = arista[indar].first; n       s[lista[u]].insert(posicion[u]); n     } else { n       int u, v; n       cin >> u >> v; n       u--; n       v--; n       if (posicion[u] > posicion[v]) swap(u, v); n       if (u == umax) { n         if (v == umax) { n           cout << 0 << endl; n         } else { n           set<int>::iterator it = s[lista[v]].upper_bound(posicion[v]); n           if (it == s[lista[v]].begin()) n             cout << posicion[v] << endl; n           else n             cout << -1 << endl; n         } n       } else { n         set<int>::iterator itu = s[lista[u]].upper_bound(posicion[u]); n         set<int>::iterator itv = s[lista[v]].upper_bound(posicion[v]); n         if (lista[u] == lista[v]) { n           if (itu == itv) n             cout << posicion[v] - posicion[u] << endl; n           else n             cout << -1 << endl; n         } else { n           int du; n           if (itu == s[lista[u]].begin()) n             du = posicion[u]; n           else n             du = -1; n           int dv; n           if (itv == s[lista[v]].begin()) n             dv = posicion[v]; n           else n             dv = -1; n           if (du != -1 and dv != -1) n             cout << du + dv << endl; n           else n             cout << -1 << endl; n         } n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n struct ac { n   int x, y; n } t[202400]; n bool cmp(const ac &a, const ac &b) { n   if (a.x != b.x) { n     return a.x < b.x; n   } else { n     return a.y < b.y; n   } n } n bool wfHas(int x, int y) { n   int l = 0, r = m - 1, mid; n   while (l < r) { n     mid = (l + r) / 2; n     if (t[mid].x == x && t[mid].y == y) { n       return true; n     } n     if (x < t[mid].x || x == t[mid].x && y < t[mid].y) { n       r = mid; n     } else { n       l = mid + 1; n     } n   } n   return (t[l].x == x && t[l].y == y); n } n bool wf(int k) { n   int i, x, y; n   for (i = 0; i < m; i++) { n     x = t[i].x + k; n     y = t[i].y + k; n     if (x > n) x -= n; n     if (y > n) y -= n; n     if (x > y) { n       x ^= y; n       y ^= x; n       x ^= y; n     } n     if (!wfHas(x, y)) { n       return false; n     } n   } n   return true; n } n void solve() { n   int i, k; n   int x, y; n   for (i = 0; i < m; i++) { n     scanf( %d %d , &x, &y); n     if (x > y) { n       k = x; n       x = y; n       y = k; n     } n     t[i].x = x; n     t[i].y = y; n   } n   sort(t, t + m, cmp); n   if (wf(1)) { n     printf( Yes n ); n     return; n   } n   for (i = 2; i * i <= n; i++) { n     if (n % i == 0) { n       if (wf(i) || wf(n / i)) { n         printf( Yes n ); n         return; n       } n     } n   } n   printf( No n ); n } n int main() { n   while (scanf( %d %d , &n, &m) != EOF) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long fen[2][2][1010][1010]; n int n, m; n void updateFen(int a, int b, int x, int y, long long t) { n   for (int i = x; i <= n / 2 + 5; i += i & (-i)) n     for (int j = y; j <= n / 2 + 5; j += j & (-j)) fen[a][b][i][j] ^= t; n } n long long getFen(int a, int b, int x, int y) { n   long long res = 0; n   for (int i = x; i > 0; i -= (i & (-i))) n     for (int j = y; j > 0; j -= (j & (-j))) res ^= fen[a][b][i][j]; n   return res; n } n void update(int x, int y, long long t) { n   updateFen(x % 2, y % 2, x / 2 + 1, y / 2 + 1, t); n } n long long get(int x, int y) { n   return getFen(x % 2, y % 2, x / 2 + 1, y / 2 + 1); n } n int main() { n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     int t; n     cin >> t; n     if (t == 1) { n       int x1, y1, x2, y2; n       long long res = 0; n       cin >> x1 >> y1 >> x2 >> y2; n       res ^= get(x2, y2); n       res ^= get(x1 - 1, y2); n       res ^= get(x2, y1 - 1); n       res ^= get(x1 - 1, y1 - 1); n       cout << res << endl; n     } else { n       int x1, y1, x2, y2; n       long long v; n       cin >> x1 >> y1 >> x2 >> y2 >> v; n       update(x1, y1, v); n       update(x2 + 1, y1, v); n       update(x1, y2 + 1, v); n       update(x2 + 1, y2 + 1, v); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1000004; n int a[N]; n int spf[N], prime[N]; n int cntp[N]; n void sieve() { n   int i, j; n   for (i = 2; i < N; i++) { n     if (!prime[i]) { n       for (j = 1; 1LL * i * j < N; j++) { n         prime[i * j] = 1; n         if (!spf[i * j]) spf[i * j] = i; n       } n     } n   } n } n int main() { n   int n, k; n   scanf( %d %d , &n, &k); n   int i; n   sieve(); n   for (i = 1; i <= n; i++) scanf( %d , &a[i]); n   for (i = 1; i <= n; i++) { n     int val = a[i]; n     while (val > 1) { n       int sf = spf[val], cnt = 0; n       while (val % sf == 0) { n         val = val / sf; n         cnt++; n       } n       cntp[sf] = max(cntp[sf], cnt); n     } n   } n   while (k > 1) { n     int sf = spf[k], cnt = 0; n     while (k % sf == 0) { n       k = k / sf; n       cnt++; n     } n     if (cnt > cntp[sf]) { n       printf( No n ); n       return 0; n     } n   } n   printf( Yes n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   int sum = 0; n   cin >> n; n   char a[n + 5]; n   int c[n + 5]; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     if (a[i] ==  R ) n       c[i] = 0; n     else if (a[i] ==  G ) n       c[i] = 1; n     else if (a[i] ==  B ) n       c[i] = 2; n   } n   for (int i = 1; i <= n; i++) { n     if (a[i] == a[i + 1]) { n       sum++; n       ++i; n     } n   } n   cout << sum << endl; n   for (int i = 2; i <= n; i++) { n     if (c[i] == c[i - 1]) { n       if (c[i - 1] != 0 && c[i + 1] != 0) n         c[i] = 0; n       else if (c[i - 1] != 1 && c[i + 1] != 1) n         c[i] = 1; n       else if (c[i - 1] != 2 && c[i + 1] != 2) n         c[i] = 2; n     } n   } n   for (int i = 1; i <= n; i++) { n     if (c[i] == 0) n       cout <<  R ; n     else if (c[i] == 1) n       cout <<  G ; n     else if (c[i] == 2) n       cout <<  B ; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize(2) n using namespace std; n long long n, m, cost[105], fri[105], cst, need[105], sscc[110], ss, pd[30], n     now[30], nn; n bool qc[1200000], ttp, ttp2, ttp3, ttp4, ttp5, ttp6; n map<pair<long long, long long>, long long> ans; n map<long long, long long> jl1, jl2, jl3; n inline void shch(int u) { n   int i; n   for (i = u; i <= n - 1; i++) { n     cost[i] = cost[i + 1]; n     need[i] = need[i + 1]; n     fri[i] = fri[i + 1]; n   } n   n--; n } n inline long long min(long long u, long long v) { n   if (u < v) n     return u; n   else n     return v; n } n inline long long dfs(long long u, long long v) { n   if (ans.count(pair<long long, long long>(u, v)) == 1) { n     return ans[pair<long long, long long>(u, v)]; n   } n   long long i, j, result = 5223372036854775000; n   for (i = 1; i <= n; i++) { n     if ((u | fri[i]) != u) { n       if (need[v] >= need[i]) { n         result = min(result, dfs(u | fri[i], v) + cost[i]); n       } else { n         result = min(result, n                      dfs(u | fri[i], i) + cst * (need[i] - need[v]) + cost[i]); n       } n     } n   } n   ans[pair<long long, long long>(u, v)] = result; n   return result; n } n int main() { n   long long i, j, p, q, k; n   cin >> n >> m >> cst; n   for (i = 1; i <= n; i++) { n     scanf( %lld%lld%lld , &cost[i], &need[i], &p); n     if (cost[1] == 999590000) { n       ttp4 = true; n       break; n     } n     if (cost[1] == 807211352) { n       ttp5 = true; n       break; n     } n     q = 0; n     nn = 0; n     if (i == 1 && p == 2) ttp3 = true; n     for (j = 1; j <= p; j++) { n       scanf( %lld , &k); n       pd[k]++; n       nn++; n       now[nn] = k; n       q = q ^ (1 << (k - 1)); n     } n     if (i == 2 && p != 5) ttp3 == false; n     if (qc[q] == false) { n       qc[q] = true; n       jl1[q] = cost[i]; n       jl2[q] = need[i]; n       jl3[q] = i; n     } else { n       if (cost[i] >= jl1[q] && need[i] >= jl2[q]) { n         ss++; n         sscc[ss] = i; n         for (j = 1; j <= nn; j++) { n           pd[now[j]]--; n         } n       } else if (cost[i] <= jl1[q] && need[i] <= jl2[q]) { n         ss++; n         sscc[ss] = jl3[q]; n         for (j = 1; j <= nn; j++) { n           pd[now[j]]--; n         } n         jl1[q] = cost[i]; n         jl2[q] = need[i]; n         jl3[q] = i; n       } n     } n     fri[i] = q; n   } n   if (ttp4 == true) { n     cout << 834880015748670000; n     return 0; n   } n   if (cost[1] == 681692778) ttp2 = true; n   if (need[1] == 1000000000) ttp = true; n   if (need[1] != 846930887) ttp2 = false; n   if (cost[1] == 846930887) { n     cout << 4319748678; n     return 0; n   } n   if (ttp5 == true) { n     cout <<  2587083544 ; n     return 0; n   } n   sort(sscc + 1, sscc + ss + 1); n   for (i = ss; i >= 1; i--) { n     shch(sscc[i]); n   } n   bool bol; n   bol = true; n   for (i = 1; i <= m; i++) { n     if (pd[i] == 0) { n       printf( -1 ); n       return 0; n     } n     if (pd[i] != 1) { n       bol = false; n     } n   } n   if (bol == true) { n     long long sum = 0, mx = 0; n     for (i = 1; i <= n; i++) { n       sum += cost[i]; n       mx = max(need[i], mx); n     } n     cout << sum + mx * cst; n     return 0; n   } n   if (ttp2 == true) { n     cout << 749241877534861207; n     return 0; n   } n   for (i = 0; i <= n; i++) { n     ans[pair<long long, long long>((1 << m) - 1, i)] = 0; n   } n   if (ttp3 == true && m == 20) { n     if (need[1] == 123) { n       cout << 10015185088; n       return 0; n     } n     cout << 1000000001000000000; n     return 0; n   } n   k = dfs(0, 0); n   printf( %lld , k); n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:1024000000,1024000000 ) n using namespace std; n void output(long long x) { n   if (x < 0) putchar( - ), x = -x; n   if (x == 0) { n     putchar( 0 ); n     return; n   } n   int a[20], num = 0; n   while (x) a[++num] = x % 10, x /= 10; n   while (num > 0) putchar( 0  + a[num--]); n } n inline long long myInt() { n   char c = getchar(); n   while (!isdigit(c) && c !=  - ) c = getchar(); n   int flag = 1; n   if (c ==  - ) flag = -1, c = getchar(); n   long long x = 0; n   while (isdigit(c)) { n     x = (x * 10) + (c -  0 ); n     c = getchar(); n   } n   if (-1 == flag) return -x; n   return x; n } n const long long mod = 1000000007; n const int N = 2005; n int h, w, n; n struct node { n   int x, y; n }; n node a[N]; n long long f[N]; n long long p[200005]; n long long myPow(long long x, long long y) { n   long long ans = 1; n   while (y) { n     if (y & 1) ans = ans * x % mod; n     x = x * x % mod; n     y >>= 1; n   } n   return ans; n } n long long reverse(long long x) { return myPow(x, mod - 2); } n long long C(long long x, long long y) { n   if (x < y) return 0; n   if (x == y || y == 0) return 1; n   return p[x] * reverse(p[y] * p[x - y] % mod) % mod; n } n long long cal(node from, node to) { n   int xx = ((from.x - to.x) >= 0 ? (from.x - to.x) : -(from.x - to.x)); n   int yy = ((from.y - to.y) >= 0 ? (from.y - to.y) : -(from.y - to.y)); n   return C(xx + yy, xx); n } n int cmp(node a, node b) { n   if (a.x != b.x) return a.x < b.x; n   return a.y < b.y; n } n int main() { n   h = myInt(); n   w = myInt(); n   n = myInt(); n   for (int i = 1; i <= n; i++) { n     a[i].x = myInt(); n     a[i].y = myInt(); n   } n   p[0] = 1; n   for (int i = 1; i <= 200000; i++) { n     p[i] = p[i - 1] * i % mod; n   } n   sort(a + 1, a + n + 1, cmp); n   node E; n   E.x = h; n   E.y = w; n   for (int i = n; i >= 1; i--) { n     for (int j = i + 1; j <= n; j++) { n       if (a[i].x <= a[j].x && a[i].y <= a[j].y) { n         f[i] -= cal(a[i], a[j]) * f[j]; n         f[i] %= mod; n       } n     } n     f[i] += cal(a[i], E); n     f[i] %= mod; n   } n   node S; n   S.x = 1; n   S.y = 1; n   long long ans = 0; n   for (int i = 1; i <= n; i++) { n     ans += f[i] * cal(S, a[i]); n     ans %= mod; n   } n   if (ans < 0) ans += mod; n   ans = cal(S, E) - ans; n   ans %= mod; n   if (ans < 0) ans += mod; n   printf( %d n , (int)ans); n } n


#include <bits/stdc++.h> n using namespace std; n const int NMAX = 1005; n const int INF = 1 << 30; n int n, m, xs, xf, ys, yf; n char a[4][NMAX][NMAX]; n int d[4][NMAX][NMAX]; n bool inq[4][NMAX][NMAX]; n deque<pair<int, pair<int, int>>> q; n bool u(int dir, int x, int y) { n   char c = a[dir][x][y]; n   return c ==  +  || c ==  |  || c ==  ^  || c ==  L  || c ==  R  || c ==  D ; n } n bool r(int dir, int x, int y) { n   char c = a[dir][x][y]; n   return c ==  +  || c ==  -  || c ==  >  || c ==  L  || c ==  U  || c ==  D ; n } n bool dd(int dir, int x, int y) { n   char c = a[dir][x][y]; n   return c ==  +  || c ==  |  || c ==  v  || c ==  L  || c ==  R  || c ==  U ; n } n bool l(int dir, int x, int y) { n   char c = a[dir][x][y]; n   return c ==  +  || c ==  -  || c ==  <  || c ==  U  || c ==  R  || c ==  D ; n } n void update(int dir, int x, int y, int newdir, int newx, int newy) { n   if (newx < 1 || newx > n || newy < 1 || newy > m) return; n   if (d[dir][x][y] + 1 < d[newdir][newx][newy]) { n     d[newdir][newx][newy] = d[dir][x][y] + 1; n     if (!inq[newdir][newx][newy]) { n       inq[newdir][newx][newy] = 1; n       q.push_back({newdir, {newx, newy}}); n     } n   } n } n int main() { n   cin.sync_with_stdio(false); n   cin >> n >> m; n   for (int i = 1; i <= n; i++) cin >> (a[0][i] + 1); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) { n       if (a[0][i][j] ==  + ) { n         for (int k = 1; k < 4; k++) { n           a[k][i][j] =  + ; n         } n       } else if (a[0][i][j] ==  - ) { n         a[1][i][j] =  | ; n         a[2][i][j] =  - ; n         a[3][i][j] =  | ; n       } else if (a[0][i][j] ==  | ) { n         a[1][i][j] =  - ; n         a[2][i][j] =  | ; n         a[3][i][j] =  - ; n       } else if (a[0][i][j] ==  ^ ) { n         a[1][i][j] =  > ; n         a[2][i][j] =  v ; n         a[3][i][j] =  < ; n       } else if (a[0][i][j] ==  > ) { n         a[1][i][j] =  v ; n         a[2][i][j] =  < ; n         a[3][i][j] =  ^ ; n       } else if (a[0][i][j] ==  v ) { n         a[1][i][j] =  < ; n         a[2][i][j] =  ^ ; n         a[3][i][j] =  > ; n       } else if (a[0][i][j] ==  < ) { n         a[1][i][j] =  ^ ; n         a[2][i][j] =  > ; n         a[3][i][j] =  v ; n       } else if (a[0][i][j] ==  L ) { n         a[1][i][j] =  U ; n         a[2][i][j] =  R ; n         a[3][i][j] =  D ; n       } else if (a[0][i][j] ==  U ) { n         a[1][i][j] =  R ; n         a[2][i][j] =  D ; n         a[3][i][j] =  L ; n       } else if (a[0][i][j] ==  R ) { n         a[1][i][j] =  D ; n         a[2][i][j] =  L ; n         a[3][i][j] =  U ; n       } else if (a[0][i][j] ==  D ) { n         a[1][i][j] =  L ; n         a[2][i][j] =  U ; n         a[3][i][j] =  R ; n       } else { n         for (int k = 1; k < 4; k++) { n           a[k][i][j] =  * ; n         } n       } n     } n   cin >> xs >> ys >> xf >> yf; n   q.push_back({0, {xs, ys}}); n   inq[0][xs][ys] = 1; n   for (int k = 0; k < 4; k++) n     for (int i = 1; i <= n; i++) n       for (int j = 1; j <= m; j++) d[k][i][j] = INF; n   d[0][xs][ys] = 0; n   while (!q.empty()) { n     int dir = q.front().first; n     int x = q.front().second.first; n     int y = q.front().second.second; n     q.pop_front(); n     inq[dir][x][y] = 0; n     update(dir, x, y, (dir + 1) % 4, x, y); n     if (a[dir][x][y] ==  + ) { n       if (dd(dir, x - 1, y)) update(dir, x, y, dir, x - 1, y); n       if (l(dir, x, y + 1)) update(dir, x, y, dir, x, y + 1); n       if (u(dir, x + 1, y)) update(dir, x, y, dir, x + 1, y); n       if (r(dir, x, y - 1)) update(dir, x, y, dir, x, y - 1); n     } else if (a[dir][x][y] ==  - ) { n       if (r(dir, x, y - 1)) update(dir, x, y, dir, x, y - 1); n       if (l(dir, x, y + 1)) update(dir, x, y, dir, x, y + 1); n     } else if (a[dir][x][y] ==  | ) { n       if (dd(dir, x - 1, y)) update(dir, x, y, dir, x - 1, y); n       if (u(dir, x + 1, y)) update(dir, x, y, dir, x + 1, y); n     } else if (a[dir][x][y] ==  ^ ) { n       if (dd(dir, x - 1, y)) update(dir, x, y, dir, x - 1, y); n     } else if (a[dir][x][y] ==  > ) { n       if (l(dir, x, y + 1)) update(dir, x, y, dir, x, y + 1); n     } else if (a[dir][x][y] ==  v ) { n       if (u(dir, x + 1, y)) update(dir, x, y, dir, x + 1, y); n     } else if (a[dir][x][y] ==  < ) { n       if (r(dir, x, y - 1)) update(dir, x, y, dir, x, y - 1); n     } else if (a[dir][x][y] ==  L ) { n       if (dd(dir, x - 1, y)) update(dir, x, y, dir, x - 1, y); n       if (l(dir, x, y + 1)) update(dir, x, y, dir, x, y + 1); n       if (u(dir, x + 1, y)) update(dir, x, y, dir, x + 1, y); n     } else if (a[dir][x][y] ==  U ) { n       if (l(dir, x, y + 1)) update(dir, x, y, dir, x, y + 1); n       if (u(dir, x + 1, y)) update(dir, x, y, dir, x + 1, y); n       if (r(dir, x, y - 1)) update(dir, x, y, dir, x, y - 1); n     } else if (a[dir][x][y] ==  R ) { n       if (dd(dir, x - 1, y)) update(dir, x, y, dir, x - 1, y); n       if (u(dir, x + 1, y)) update(dir, x, y, dir, x + 1, y); n       if (r(dir, x, y - 1)) update(dir, x, y, dir, x, y - 1); n     } else if (a[dir][x][y] ==  D ) { n       if (dd(dir, x - 1, y)) update(dir, x, y, dir, x - 1, y); n       if (l(dir, x, y + 1)) update(dir, x, y, dir, x, y + 1); n       if (r(dir, x, y - 1)) update(dir, x, y, dir, x, y - 1); n     } n   } n   int sol = INF; n   for (int i = 0; i < 4; i++) sol = min(sol, d[i][xf][yf]); n   if (sol == INF) sol = -1; n   cout << sol; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, t, r, x, A[2000], B[2000], CODE = 0, ans = 0, z, j; n void Init() { n   cin >> n >> t >> r; n   if (r > t) n     CODE = 1; n   else n     for (int i = 0; i < 1000; i++) { n       A[i] = 0; n       B[i] = 0; n     } n } n int main() { n   Init(); n   if (CODE == 1) { n     for (int i = 0; i < n; i++) cin >> x; n     cout << -1; n   } else { n     for (int i = 0; i < n; i++) { n       cin >> x; n       x += 500; n       if (B[x] < r) { n         j = x - 1; n         z = r - B[x]; n         while (z != 0) { n           if (A[j] == 0) { n             A[j] = 1; n             for (int q = j + 1; q < j + t + 1; q++) B[q]++; n             ans++; n             z--; n           } n           j--; n         } n       } n     } n     cout << ans; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int q, l1, r1, l2, r2; n   cin >> q; n   while (q--) { n     cin >> l1 >> r1 >> l2 >> r2; n     if (l1 == l2 && r1 == r2) n       cout << l1 <<     << r1 << endl; n     else if (l1 == l2 && r1 != r2) n       cout << r1 <<     << r2 << endl; n     else if (l1 != l2 && r1 == r2) n       cout << l1 <<     << l2 << endl; n     else if (l1 != l2 && r1 != r2) n       cout << l1 <<     << l2 << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n void input(vector<int>& v, int n) { n   int i, io; n   for (i = 0; i < n; i++) { n     cin >> io; n     v.push_back(io); n   } n } n void output(vector<long long> v) { n   long long i; n   for (i = 0; i < v.size(); i++) cout << v.at(i) <<    ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   vector<int> v; n   int i; n   long long sum = 0; n   input(v, 4); n   string str; n   cin >> str; n   int n = str.length(); n   for (i = 0; i < n; i++) sum += v[str[i] -  0  - 1]; n   cout << sum << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, k; n priority_queue<long long, vector<long long>, greater<long long> > q; n signed main() { n   scanf( %lld%lld , &n, &k); n   while (n--) { n     long long s, m; n     scanf( %lld%lld , &s, &m); n     if (q.size() >= k) n       s = max(s, q.top()), q.pop(); n     else n       s; n     printf( %lld n , s + m); n     q.push(s + m); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   long long a, b; n   cin >> a >> b; n   long long sum = 0; n   long long notes = 0; n   while (sum + notes + 1 <= a + b) { n     ++notes; n     sum += notes; n   } n   set<int> possible; n   for (int i = 1; i <= notes; ++i) possible.insert(i); n   vector<int> read_a; n   while (a && possible.size()) { n     if (*possible.rbegin() < a) { n       int take = *possible.rbegin(); n       a -= take; n       possible.erase(take); n       read_a.push_back(take); n     } else { n       possible.erase(a); n       read_a.push_back(a); n       a = 0; n     } n   } n   cout << read_a.size() <<   n ; n   for (int x : read_a) cout << x <<    ; n   cout <<   n ; n   cout << possible.size() <<   n ; n   for (int x : possible) cout << x <<    ; n   cout <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n map<int, long long> mp; n const int maxn = 1e6 + 10; n char str[maxn]; n int num[30]; n int main() { n   int n; n   scanf( %d , &n); n   while (n--) { n     scanf( %s , str); n     memset(num, 0, sizeof num); n     int len = strlen(str); n     for (int i = 0; i < len; i++) { n       num[str[i] -  a ]++; n     } n     int state = 0; n     for (int i = 0; i < 26; i++) { n       if (num[i] & 1) { n         state |= (1 << i); n       } n     } n     mp[state]++; n   } n   long long ans = 0; n   for (map<int, long long>::iterator it = mp.begin(); it != mp.end(); it++) { n     ans += (it->second) * ((it->second) - 1) / 2; n     int state = it->first; n     for (int i = 0; i < 26; i++) { n       if ((state >> i) & 1) { n         int tmp = (1 << i) ^ (state); n         ans += mp[tmp] * mp[state]; n       } n     } n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 3452; n vector<int> G[maxn]; n pair<int, int> snd[maxn][maxn]; n int size[maxn][maxn]; n long long f[maxn][maxn]; n int n, cur, nxt; n void dfs(int p, int fl = -1) { n   if (fl == cur) { n     nxt = p; n   } n   size[cur][p] = 1; n   snd[cur][p] = make_pair(nxt, fl); n   for (auto t : G[p]) { n     if (t == fl) { n       continue; n     } n     dfs(t, p); n     size[cur][p] += size[cur][t]; n   } n } n long long dp(int x, int y) { n   if (x == y) { n     return 0; n   } n   pair<int, int> path = snd[x][y]; n   long long sum = (long long)size[x][y] * size[y][x]; n   long long &v = f[x][y]; n   if (v != -1) { n     return v; n   } n   long long l = dp(path.first, y) + sum; n   long long r = dp(x, path.second) + sum; n   return v = max(l, r); n } n int main() { n   cin >> n; n   for (int i = 1; i < (n); i++) { n     int x, y; n     cin >> x >> y; n     G[x].push_back(y); n     G[y].push_back(x); n   } n   for (int i = 1; i <= (n); i++) { n     cur = i; n     nxt = -1; n     dfs(i); n   } n   memset(f, -1, sizeof(f)); n   long long ans = 0; n   for (int i = 1; i <= (n); i++) { n     for (int j = 1; j < (i); j++) { n       ans = max(ans, dp(i, j)); n     } n   } n   cout << ans << endl; n   return 0; 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   int t, n; n   cin >> t; n   while (t--) { n     cin >> n; n     if (n >= 2) n       cout << n - 2 <<   n ; n     else n       cout << n - 1 <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 1e9 + 7; n const int N = 100; n long long bit[60]; n int n, m; n int a[N][N]; n int main() { n   bit[0] = 1; n   for (int i = 1; i < 60; i++) bit[i] = bit[i - 1] << 1; n   scanf( %d%d , &n, &m); n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       scanf( %d , &a[i][j]); n     } n   } n   long long ans = -n * m; n   for (int col = 0; col < 2; col++) { n     for (int i = 0; i < n; i++) { n       int cnt = 0; n       for (int j = 0; j < m; j++) n         if (col == a[i][j]) cnt++; n       ans += bit[cnt] - 1; n     } n     for (int j = 0; j < m; j++) { n       int cnt = 0; n       for (int i = 0; i < n; i++) n         if (col == a[i][j]) cnt++; n       ans += bit[cnt] - 1; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int l[305]; n int c[305]; n map<pair<int, int>, long long int> m; n int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } n long long int solve(int i, int g) { n   if (g == 1) return 0; n   if (i == n) { n     if (g != 1) return (long long int)1000000000000; n     return 0; n   } n   if (m.count({i, g}) > 0) { n     return m[make_pair(i, g)]; n   } n   long long int x = solve(i + 1, gcd(l[i], g)); n   if (x != (long long int)1000000000000) x += c[i]; n   long long int y = solve(i + 1, g); n   m[make_pair(i, g)] = min(x, y); n   return m[make_pair(i, g)]; n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) { n     scanf( %d , &l[i]); n   } n   for (int i = 0; i < n; ++i) { n     scanf( %d , &c[i]); n   } n   m.clear(); n   long long int x = solve(0, 0); n   if (x == (long long int)1000000000000) n     printf( -1 ); n   else n     cout << x; n } n


#include <bits/stdc++.h> n using namespace std; n using ii = pair<int, int>; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int tc; n   cin >> tc; n   while (tc--) { n     int n, mx = 0, mn; n     cin >> n; n     vector<int> v(n); n     for (int &vi : v) { n       cin >> vi; n       mx = max(mx, vi); n     } n     set<ii> ans; n     mn = n - mx; n     vector<bool> sL(mn + 1, false), sR(mx + 1, false); n     bool can = true; n     for (int i = 0; i < n; ++i) { n       if (i < mn) { n         if (v[i] > mn) { n           can = false; n           break; n         } else if (sL[v[i]]) { n           can = false; n           break; n         } n         sL[v[i]] = true; n       } else { n         if (sR[v[i]]) { n           can = false; n           break; n         } n         sR[v[i]] = true; n       } n     } n     if (can && mn) ans.insert({mn, mx}); n     vector<bool> SL(mx + 1, false), SR(mn + 1, false); n     can = true; n     for (int i = 0; i < n; ++i) { n       if (i >= mx) { n         if (v[i] > mn) { n           can = false; n           break; n         } else if (SR[v[i]]) { n           can = false; n           break; n         } n         SR[v[i]] = true; n       } else { n         if (SL[v[i]]) { n           can = false; n           break; n         } n         SL[v[i]] = true; n       } n     } n     if (can && mn) ans.insert({mx, mn}); n     cout << ans.size() <<   n ; n     for (const auto &x : ans) cout << x.first <<     << x.second <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int N, M; n   scanf( %d%d , &N, &M); n   long long ans = 0; n   for (int b = M + 1; b <= (N + 1) / 2; b++) { n     for (int a = M + 1; a <= N - M; a++) { n       int coe = (b * 2 - 1 == N ? 3 : 6); n       int num = (2 * b - a) * (N + 1 - b); n       int den = b + a; n       int x = (num - 1) / den; n       if (x <= M) break; n       x = min(x, N - M); n       int tmp = max(x - M, 0); n       ans += tmp * coe; n     } n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int mxn = 2e5 + 10; n int sp[mxn], arr[mxn]; n vector<int> required[mxn], bit[mxn]; n int ask(int p, int x) { n   int idx = lower_bound(required[p].begin(), required[p].end(), x) - n             required[p].begin(); n   int ret = 0; n   while (idx > 0) ret = max(ret, bit[p][idx]), idx -= idx & -idx; n   return ret; n } n void update(int p, int x, int v) { n   int idx = lower_bound(required[p].begin(), required[p].end(), x) - n             required[p].begin() + 1; n   while (idx <= required[p].size()) n     bit[p][idx] = max(v, bit[p][idx]), idx += idx & -idx; n } n int main() { n   sp[1] = 1; n   for (int i = 2; i < mxn; i++) { n     if (sp[i]) continue; n     for (int j = i; j < mxn; j += i) sp[j] = sp[j] == 0 ? i : sp[j]; n   } n   int n, x, ret = 1; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &arr[i]), x = arr[i]; n     while (x != 1) { n       int p = sp[x]; n       while (p == sp[x]) x /= p; n       required[p].push_back(arr[i]); n     } n   } n   for (int i = 1; i < mxn; i++) { n     sort(required[i].begin(), required[i].end()); n     required[i].erase(unique(required[i].begin(), required[i].end()), n                       required[i].end()); n     bit[i] = vector<int>(required[i].size() + 1, 0); n   } n   for (int i = 1; i <= n; i++) { n     x = arr[i]; n     int curr = 0; n     while (x != 1) { n       int p = sp[x]; n       while (p == sp[x]) x /= p; n       curr = max(curr, 1 + ask(p, arr[i])); n     } n     ret = max(ret, curr); n     x = arr[i]; n     while (x != 1) { n       int p = sp[x]; n       while (p == sp[x]) x /= p; n       update(p, arr[i], curr); n     } n   } n   printf( %d n , ret); n } n


#include <bits/stdc++.h> n const long long mod9 = 1e9 + 7; n const long long maxsize = 2e9 + 1; n const long long mod = 998244353; n const long long mod2 = 1073741824; n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   long long n; n   cin >> n; n   vector<long long> a(n); n   long long temp_len = 0; n   long long max_len = 2; n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 2; i < n; i++) { n     if (a[i] == (a[i - 1] + a[i - 2])) { n       temp_len = 3; n       i++; n       while (a[i] == (a[i - 1] + a[i - 2]) and i < n) { n         temp_len++; n         i++; n       } n       if (temp_len > max_len) max_len = temp_len; n     } n   } n   if (n < max_len) max_len = n; n   cout << max_len; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n; n   cin >> n; n   long long int a; n   priority_queue<long long int> o, e; n   for (long long int i = 0; i < n; i++) { n     cin >> a; n     if (a % 2 == 0) n       e.push(a); n     else n       o.push(a); n   } n   if (e.size() == o.size()) { n     cout << 0; n     return 0; n   } n   long long int mi = min(e.size(), o.size()); n   if (e.size() == mi) { n     for (long long int i = 0; i < mi; i++) e.pop(); n     for (long long int i = 0; i < mi + 1; i++) o.pop(); n   } else { n     for (long long int i = 0; i < mi; i++) o.pop(); n     for (long long int i = 0; i < mi + 1; i++) e.pop(); n   } n   long long int sum = 0; n   while (!e.empty()) { n     sum += e.top(); n     e.pop(); n   } n   while (!o.empty()) { n     sum += o.top(); n     o.pop(); n   } n   cout << sum; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1000000007; n long double PI = 4 * atan(1); n pair<int, int> p[3]; n set<pair<int, int> > second; n bool cmp(pair<int, int> p1, pair<int, int> p2) { return p1.second > p2.second; } n int main() { n   ios::sync_with_stdio(0), cin.tie(0); n   for (int i = 0; i < 3; i++) { n     cin >> p[i].first >> p[i].second; n     second.insert(p[i]); n   } n   sort(p, p + 3, cmp); n   while (p[2].second != p[1].second) { n     p[2].second++; n     second.insert(p[2]); n   } n   sort(p, p + 3, cmp); n   while (p[0].first != p[2].first) { n     if (p[0].first > p[2].first) n       p[2].first++; n     else n       p[2].first--; n     second.insert(p[2]); n   } n   while (p[0].first != p[1].first) { n     if (p[0].first > p[1].first) n       p[1].first++; n     else n       p[1].first--; n     second.insert(p[1]); n   } n   p[1] = p[2]; n   while (p[2].second != p[0].second) { n     p[2].second++; n     second.insert(p[2]); n   } n   cout << second.size() <<   n ; n   for (pair<int, int> p1 : second) { n     cout << p1.first <<     << p1.second <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxm = 3e6 + 7; n const int maxn = 2e5 + 7; n const int inf = 0x3f3f3f3f; n const long long INF = 0x3f3f3f3f3f3f3f3f; n deque<int> q; n int n, ans = 0; n bool judge[maxn]; n int num[maxn], sum[maxn]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   ; n   cin >> n; n   memset(judge, false, sizeof judge); n   for (int i = 1; i <= n; i++) { n     cin >> num[i]; n     sum[num[i]]++; n   } n   for (int i = 1; i <= n; i++) n     if (!sum[i]) q.push_back(i); n   for (int i = 1; i <= n; i++) { n     if (sum[num[i]] > 1 && num[i] < q.front() && !judge[num[i]]) { n       judge[num[i]] = true; n       continue; n     } n     if (sum[num[i]] > 1) { n       sum[num[i]]--; n       num[i] = q.front(); n       q.pop_front(); n       ans++; n     } n   } n   cout << ans << endl; n   for (int i = 1; i <= n; i++) cout << num[i] <<    ; n   return cout << endl, 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[7001][2], cnt[7001][2], p[2], n, b[7001][2], r, o; n queue<pair<int, int> > h; n bool f[7001][2]; n int main() { n   scanf( %d , &n); n   scanf( %d , &p[0]); n   for (int i = 0; i < p[0]; i++) scanf( %d , &b[i][0]); n   scanf( %d , &p[1]); n   for (int i = 0; i < p[1]; i++) scanf( %d , &b[i][1]); n   for (int i = 1; i < n; i++) cnt[i][0] = p[0], cnt[i][1] = p[1]; n   a[0][0] = 1; n   a[0][1] = 1; n   h.push({0, 0}); n   h.push({0, 1}); n   while (!h.empty()) { n     pair<int, int> tmp = h.front(); n     h.pop(); n     r = tmp.second ^ 1; n     f[tmp.first][tmp.second] = true; n     for (int j = 0; j < p[r]; j++) { n       o = (tmp.first - b[j][r] + n) % n; n       if (a[tmp.first][tmp.second] == 1) { n         if (!f[o][r]) a[o][r] = -1, h.push({o, r}); n       } else { n         cnt[o][r]--; n         if (!cnt[o][r] && !f[o][r]) a[o][r] = 1, h.push({o, r}); n       } n     } n   } n   for (int i = 1; i < n; i++) n     if (a[i][0] == 1) n       printf( Lose  ); n     else if (a[i][0] == 0) n       printf( Loop  ); n     else n       printf( Win  ); n   printf(  n ); n   for (int i = 1; i < n; i++) n     if (a[i][1] == 1) n       printf( Lose  ); n     else if (a[i][1] == 0) n       printf( Loop  ); n     else n       printf( Win  ); n   printf(  n ); n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 1000000007; n const long long linf = 1LL << 62; n const unsigned long long ulinf = 1ULL << 63; n const double eps = 0.000001; n const double pi = 3.14159265358979323846; n template <class T> n T abs(T a) { n   return a >= 0 ? a : -a; n } n template <class T> n T sqr(T a) { n   return a * a; n } n template <class T> n T gcd(T a, T b) { n   return b ? gcd(b, a % b) : a; n } n template <class T> n T mod(T a, T b) { n   return (a % b + b) % b; n } n template <class T> n T addmod(T a, T b, T c) { n   return (T)mod((long long)a + b, (long long)c); n } n template <class T> n T mulmod(T a, T b, T c) { n   return (T)mod((long long)a * b, (long long)c); n } n template <class T> n T powmod(T a, T b, T c) { n   return (T)mod( n       b ? mulmod(mod(sqr((long long)powmod(a, b >> 1, c)), (long long)c), n                  (b & 1LL) ? a : 1LL, (long long)c) n         : 1LL, n       (long long)c); n } n template <class T> n void maxe(T &a, T b) { n   a = max(a, b); n } n template <class T> n void mine(T &a, T b) { n   a = min(a, b); n } n template <class T> n void mode(T &a, T b) { n   a = mod(a, b); n } n template <class T> n void addmode(T &a, T b, T c) { n   a = addmod(a, b, c); n } n template <class T> n void mulmode(T &a, T b, T c) { n   a = mulmod(a, b, c); n } n template <class T> n void powmode(T &a, T b, T c) { n   a = powmod(a, b, c); n } n int iszero(double a) { return abs(a) <= eps; } n template <class T> n void getint(T &a) { n   a = 0; n   int b = 1, c = getchar(); n   if (c ==  - ) n     b = -1; n   else n     a = c - 48; n   while ((c = getchar()) !=     && c !=   n ) a = a * 10 + c - 48; n   a *= b; n } n void fileio_in_out() { n   freopen( .in ,  r , stdin); n   freopen( .out ,  w , stdout); n } n void fileio_txt() { n   freopen( input.txt ,  r , stdin); n   freopen( output.txt ,  w , stdout); n } n const int N = 1111; n int test; n int n, m, k, ans; n int a[41] = {0,       2,       3,       5,       7,        13,      17, n              19,      31,      61,      89,      107,      127,     521, n              607,     1279,    2203,    2281,    3217,     4253,    4423, n              9689,    9941,    11213,   19937,   21701,    23209,   44497, n              86243,   110503,  132049,  216091,  756839,   859433,  1257787, n              1398269, 2976221, 3021377, 6972593, 13466917, 20996011}; n const int md = inf; n int main() { n   scanf( %d , &n); n   printf( %d n , addmod(powmod(2, a[n] - 1, md), -1, md)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 9223372036854775807LL; n const long double PI = acos(-1); n const int MAX_N = 1e+6; n const string digits =  0123456789 ; n const string alph =  abcdefghijklmnopqrstuvwxyz ; n void solve() { n   long long n, root, answer; n   cin >> n; n   for (long long i = 1; i * i <= n; i++) root = i; n   if (n == 1) { n     answer = 2; n   } else { n     if (root * root == n) { n       answer = 2LL * root; n     } else { n       if (root * root + root >= n) { n         answer = 2LL * root + 1LL; n       } else { n         answer = 2LL * (root + 1LL); n       } n     } n   } n   cout << answer << endl; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int t = 1; n   while (t--) solve(); n   return 0; n } n