#include <bits/stdc++.h> n using namespace std; n const int mod = 1000003; n long double pi = 3.1415926536; n const int mx = 1000003; n long long gcd(long long x, long long y) { n   if (y == 0) return x; n   return gcd(y, x % y); n } n long long bp(long long a, long long b) { n   long long ans = 1; n   while (b > 0) { n     if (b & 1) ans = (ans * a) % mod; n     a = (a * a) % mod; n     b >>= 1; n   } n   return ans; n } n long long p(long long n) { n   for (long long i = 2; i * i <= n; i++) { n     if (n % i == 0) { n       return n / i; n     } n   } n   return 1; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long n, k; n   cin >> n >> k; n   long long a[n + 1], ans = 0; n   for (long long i = 1; i <= n; i++) cin >> a[i]; n   while (1) { n     bool t = 0; n     for (long long i = n; i >= 1;) { n       if (a[i] < k) { n         long long x = a[i], j = i; n         while (x == a[i] && i >= 1) { n           x = a[i]; n           i--; n         } n         a[j]++; n         t = 1; n       } else { n         i--; n       } n     } n     if (t) ans++; n     if (a[1] >= k) break; n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { n   if (b == 0) return a; n   return gcd(b, a - (a / b * b)); n } n long long mypow(long long a, long long b) { n   long long p = 1; n   if (b == 0) return 1; n   for (long long i = 0; i < b; i++) p *= a; n   return p; n } n long long zero(long long a) { n   long long za = 0; n   long long temp = a; n   long long tens = 0; n   while (temp > 0) { n     if (temp % 10 != 0) { n       za += ((temp % 10) * (mypow(10, tens))); n       tens++; n     } n     temp /= 10; n   } n   return za; n } n int main() { n   string st; n   while (getline(cin, st)) { n     int i = st.length() - 2; n     while (st[i] ==    ) i--; n     if (st[i] < 97) st[i] += 32; n     if (st[i] ==  a  || st[i] ==  e  || st[i] ==  i  || st[i] ==  o  || n         st[i] ==  u  || st[i] ==  y ) n       cout <<  YES  << endl; n     else n       cout <<  NO  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = (1 << 30) - 1; n const long long linf = (1ll << 62) - 1; n const int N = 1e6 + 100; n int n, m; n int u[N], v[N]; n set<int> g[N]; n bool used[N]; n int ts[N], tsLen = 0; n void topSort(int v) { n   if (used[v]) { n     return; n   } n   used[v] = true; n   for (int to : g[v]) { n     topSort(to); n   } n   ts[tsLen++] = v; n } n inline bool check(int m) { n   for (int i = 0; i < n; i++) { n     g[i].clear(); n   } n   for (int i = 0; i < m; i++) { n     g[u[i]].insert(v[i]); n   } n   fill_n(used, n, false); n   tsLen = 0; n   for (int i = 0; i < n; i++) { n     if (!used[i]) { n       topSort(i); n     } n   } n   reverse(ts, ts + tsLen); n   for (int i = 0; i < tsLen - 1; i++) { n     if (g[ts[i]].count(ts[i + 1]) == 0) { n       return false; n     } n   } n   return true; n } n int main() { n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     scanf( %d%d , &u[i], &v[i]); n     u[i]--, v[i]--; n   } n   int l = 0, r = m; n   while (r - l > 1) { n     int x = (l + r) / 2; n     if (check(x)) { n       r = x; n     } else { n       l = x; n     } n   } n   if (!check(r)) { n     puts( -1 ); n   } else { n     printf( %d n , r); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using pi = pair<long long, long long>; n const int N = 1e5 + 5; n const long long p1 = 31; n const long long p2 = 41; n const long long mod1 = 1e9 + 7; n const long long mod2 = 1e9 + 9; n long long power1[N]; n long long power2[N]; n long long prefix1[3][N]; n long long prefix2[3][N]; n pi suff[3][N]; n string s[3]; n int arr[3] = {0, 1, 2}; n bool yes[3][3]; n long long mod_exp(long long x, long long y, long long mod) { n   long long res = 1; n   x %= mod; n   while (y > 0) { n     if (y & 1LL) { n       res = (res * x) % mod; n     } n     y >>= 1LL; n     x = (x * x) % mod; n   } n   return res; n } n ll inv(ll x, ll mod) { return mod_exp(x, mod - 2, mod); } n ll add(ll a, ll b, ll mod) { return (a + b) % mod; } n ll mul(ll a, ll b, ll mod) { return (a * b) % mod; } n ll sub(ll a, ll b, ll mod) { return (a - b + mod) % mod; } n pi see(int l, int r, int idx) { n   pi p; n   if (l > 0) { n     p.first = mul(sub(prefix1[idx][r], prefix1[idx][l - 1], mod1), n                   inv(power1[l], mod1), mod1); n     p.second = mul(sub(prefix2[idx][r], prefix2[idx][l - 1], mod2), n                    inv(power2[l], mod2), mod2); n   } else { n     p.first = prefix1[idx][r]; n     p.second = prefix2[idx][r]; n   } n   return p; n } n inline void compute(string &str, int idx) { n   prefix1[idx][0] = str[0]; n   prefix2[idx][0] = str[0]; n   int n = str.size(); n   for (int i = 1; i < n; ++i) { n     prefix1[idx][i] = n         add(prefix1[idx][i - 1], mul(str[i], power1[i], mod1), mod1); n     prefix2[idx][i] = n         add(prefix2[idx][i - 1], mul(str[i], power2[i], mod2), mod2); n   } n   for (int i = n - 1; i >= 0; --i) { n     suff[idx][i] = see(i, n - 1, idx); n   } n   sort(suff[idx], suff[idx] + n); n } n bool check(int idx1, int idx2) { n   if (s[idx2].size() < s[idx1].size()) { n     return 0; n   } n   int i = 0; n   int j = s[idx1].size() - 1; n   int k = s[idx1].size() - 1; n   while (j < s[idx2].size()) { n     if (see(i, j, idx2) == pi(prefix1[idx1][k], prefix2[idx1][k])) { n       return 1; n     } n     ++i; n     ++j; n   } n   return 0; n } n inline int solve() { n   int ret = s[arr[0]].size(); n   for (int i = 1; i < 3; ++i) { n     if (yes[arr[i]][arr[i - 1]]) { n       continue; n     } n     if (i == 2 && yes[arr[i]][arr[i - 2]]) { n       break; n     } n     int sub = 0; n     for (int j = s[arr[i]].size() - 1; j >= 0; --j) { n       if (binary_search(suff[arr[i - 1]], n                         suff[arr[i - 1]] + s[arr[i - 1]].size(), n                         pi(prefix1[arr[i]][j], prefix2[arr[i]][j]))) { n         sub = j + 1; n         break; n       } n     } n     ret += s[arr[i]].size() - sub; n   } n   return ret; n } n int main() { n   ios_base::sync_with_stdio(0); n   power1[0] = 1; n   power2[0] = 1; n   for (int i = 1; i < N; ++i) { n     power1[i] = mul(power1[i - 1], p1, mod1); n     power2[i] = mul(power2[i - 1], p2, mod2); n   } n   for (int i = 0; i < 3; ++i) { n     cin >> s[i]; n     compute(s[i], i); n   } n   for (int i = 0; i < 3; ++i) { n     for (int j = 0; j < 3; ++j) { n       if (i == j) { n         continue; n       } n       yes[i][j] = check(i, j); n     } n   } n   int res = 1 << 29; n   do { n     res = min(res, solve()); n   } while (next_permutation(arr, arr + 3)); n   cout << res <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ld = long double; n using uint = unsigned int; n using ull = unsigned long long; n using pii = pair<int, int>; n int f[50], s[50], p[50]; n ld dp[51][5000]; n int main() { n   cin.tie(nullptr); n   cout.tie(nullptr); n   ios_base::sync_with_stdio(false); n   int n, r; n   cin >> n >> r; n   for (int i = 0; i < n; i++) cin >> f[i] >> s[i] >> p[i]; n   ld l = 0, h = 1e9; n   int rep = 80; n   while (rep--) { n     ld m = (l + h) / 2; n     for (int i = n - 1; i >= 0; i--) { n       fill(dp[i + 1] + min(5000, r + 1), dp[i + 1] + 5000, m); n       for (int j = 0; j <= r; j++) { n         ld ff = (dp[i + 1][j + f[i]] + f[i]) * p[i]; n         ld ss = (dp[i + 1][j + s[i]] + s[i]) * (100 - p[i]); n         dp[i][j] = min(m, (ff + ss) / 100); n       } n     } n     if (dp[0][0] < m) { n       h = m; n     } else { n       l = m; n     } n   } n   cout << fixed << setprecision(20) << l; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n void run() { n   int n; n   cin >> n; n   vector<long long> a(n); n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   if (n == 1) { n     cout << 1 <<     << 1 <<   n  << -a[0] <<   n ; n     cout << 1 <<     << 1 <<   n  << 0 <<   n ; n     cout << 1 <<     << 1 <<   n  << 0 <<   n ; n     return; n   } n   cout << 1 <<     << n - 1 <<   n ; n   for (int i = 0; i < n - 1; i++) { n     int x = a[i] % n; n     a[i] += 1ll * x * (n - 1); n     if (i) cout <<    ; n     cout << 1ll * x * (n - 1); n   } n   cout <<   n ; n   cout << 2 <<     << n <<   n ; n   for (int i = 1; i < n; i++) { n     int x = a[i] % n; n     a[i] += 1ll * x * (n - 1); n     if (i > 1) cout <<    ; n     cout << 1ll * x * (n - 1); n   } n   cout <<   n ; n   cout << 1 <<     << n <<   n ; n   for (int i = 0; i < n; i++) { n     if (i) cout <<    ; n     cout << -a[i]; n   } n   cout <<   n ; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cout << fixed << setprecision(20); n   run(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n const int M = 1e5 + 5; n long long mod_exp(long long cur, long long power) { n   long long res = 1; n   while (power > 0) { n     if (power % 2 == 0) { n       cur = (cur * cur) % 1000000007; n     } else { n       res = (res * cur) % 1000000007; n       cur = (cur * cur) % 1000000007; n     } n     power /= 2; n   } n   return res; n } n int main() { n   ios_base::sync_with_stdio(false); n   int i, j, n, m; n   cin >> n >> m; n   long long dp1[N], dp0[N]; n   dp1[0] = dp0[0] = 1; n   dp1[1] = dp0[1] = 2; n   for (i = 2; i < N; i++) { n     dp1[i] = (dp0[i - 1] + dp0[i - 2]) % 1000000007; n     dp0[i] = (dp1[i - 1] + dp1[i - 2]) % 1000000007; n   } n   long long foo = (dp1[m - 1] + dp0[m - 1]) % 1000000007; n   foo = (foo - 2 + 1000000007) % 1000000007; n   foo = (foo + dp0[n - 1] + dp1[n - 1]) % 1000000007; n   cout << foo << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n  n #define ll long long int n  n void solve(){ n    ll n,i,j; n    cin >> n; n    ll arr[n]; n    ll hash[65] = {0}; n    vector<string> vec; n    for(i=0;i<n;i++){ n        cin >> arr[i]; n        ll m = arr[i]; n        ll id = 0; n        string s =   ; n        while(m > 0){ n            if(m & 1){ n                hash[id]++; n                s +=  1 ; n            } n            else n            s +=  0 ; n            id++; n            m/=2; n        } n        vec.push_back(s); n    } n    ll ans[65];ll ps= 0; n    ll bin[65]={0}; n    for(i=0;i<65;i++){ n        ll a = 1; n        ll p = i; n        while(p > 0){ n            p--; n            a *= 2; n             a %= 1000000007; n        } n        bin[i]=a; n        a *= hash[i]; n         a %= 1000000007; n         ans[i]=a; n         ps += ans[i]; n          ps %= 1000000007; n    } n    /*for(i=0;i<5;i++){ n        cout << ans[i] <<   fggg   << bin[i] <<   n ; n    }*/ n    ll s = 0; n  //  cout << ps <<   n ; n    for(i=0;i<n;i++){ n        ll a = 0;ll b =0; n        string ss = vec[i]; n        for(j=0;j<ss.length();j++){ n           if(ss[j] ==  1 ){ n               a += ans[j]; n                a %= 1000000007; n                b += (bin[j]*(n-hash[j])); n                 b %= 1000000007; n           } n        } n        b += ps; n         b %= 1000000007; n         ll ls = (a*b)%1000000007; n         s += ls; n          s %= 1000000007; n         // cout << a <<   and or   << b <<   n ; n    } n    cout << s <<   n ; n } n  n int main() { n     ios_base :: sync_with_stdio(0); n     cin.tie(NULL); n     int t; n     cin >> t; n     while(t > 0){ n         t--; n         solve(); n     } n }


#include <bits/stdc++.h> n long long gcd(long long a, long long b) { n   while (a && b) { n     if (a > b) n       a %= b; n     else n       b %= a; n   } n   return a + b; n } n long long lcm(long long a, long long b) { return (((a) / gcd(a, b))) * b; } n int phi(int n) { n   int result = n; n   for (int i = 2; i * i <= n; i++) { n     if (n % i == 0) { n       while (n % i == 0) n /= i; n       result -= result / i; n     } n   } n   if (n > 1) result -= result / n; n   return result; n } n long long power(long long a, long long b, long long c) { n   long long r = 1; n   while (b) { n     if (b & 1) r *= a; n     a *= a; n     a %= c; n     r %= c; n     b /= 2; n   } n   return r; n } n void nxi(int& n) { n   bool min = 0; n   char c; n   n = 0; n   while ((c = getc(stdin)) && c <= 32) n     ; n   if (c ==  - ) n     min = 1; n   else n     n = c - 48; n   while ((c = getc(stdin)) && c > 32) n = (n << 3) + (n << 1) + c - 48; n   if (min) n = -n; n } n void prl(int n) { n   if (n == 0) { n     puts( 0 ); n     return; n   } n   static int s[10]; n   int top = 0; n   while (n > 0) s[top++] = n % 10, n /= 10; n   while (top--) putchar(s[top] + 48); n } n using namespace std; n int n, m; n pair<int, int> mem[101010]; n map<long long, vector<long long> > sv; n map<long long, long long> t; n void inc(int pos, long long val) { n   for (long long i = pos; i < 1000000007; i = (i | (i + 1))) { n     t[i] += val, t[i] %= 1000000007; n   } n } n long long get(int r) { n   long long s = 0; n   for (long long i = r; i >= 0; i = (i & (i + 1)) - 1) { n     if (t.count(i)) { n       s += t[i]; n       s %= 1000000007; n     } n   } n   return s; n } n long long get(int l, int r) { n   long long x = (long long)get(r) - (l > 0 ? 1ll * get(l - 1) : 0); n   x = (x + 1000000007) % 1000000007; n   x = (x + 1000000007) % 1000000007; n   return x; n } n int main() { n   ios::sync_with_stdio(0); n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     cin >> mem[i].first >> mem[i].second; n     int x = mem[i].second; n     sv[x].push_back(mem[i].first); n   } n   inc(0, 1); n   for (map<long long, vector<long long> >::iterator it = sv.begin(); n        it != sv.end(); it++) { n     long long s = 0; n     int x = it->first; n     for (int i = 0; i < it->second.size(); i++) { n       s += get(it->second[i], x - 1); n       s %= 1000000007; n       s = (s + 1000000007) % 1000000007; n       s = (s + 1000000007) % 1000000007; n     } n     inc(x, s); n   } n   cout << get(n, n) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } n long long powmod(long long a, long long b, long long MOD) { n   if (a == 0ll) { n     return 0ll; n   } n   a %= MOD; n   long long ans = 1; n   while (b) { n     if (b & 1) { n       ans = ans * a % MOD; n     } n     a = a * a % MOD; n     b >>= 1; n   } n   return ans; n } n long long poww(long long a, long long b) { n   if (a == 0ll) { n     return 0ll; n   } n   long long ans = 1; n   while (b) { n     if (b & 1) { n       ans = ans * a; n     } n     a = a * a; n     b >>= 1; n   } n   return ans; n } n void Pv(const vector<int> &V) { n   int Len = int(V.size()); n   for (int i = 0; i < Len; ++i) { n     printf( %d , V[i]); n     if (i != Len - 1) { n       printf(   ); n     } else { n       printf(  n ); n     } n   } n } n void Pvl(const vector<long long> &V) { n   int Len = int(V.size()); n   for (int i = 0; i < Len; ++i) { n     printf( %lld , V[i]); n     if (i != Len - 1) { n       printf(   ); n     } else { n       printf(  n ); n     } n   } n } n inline long long readll() { n   long long tmp = 0, fh = 1; n   char c = getchar(); n   while (c <  0  || c >  9 ) { n     if (c ==  - ) fh = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) tmp = tmp * 10 + c - 48, c = getchar(); n   return tmp * fh; n } n inline int readint() { n   int tmp = 0, fh = 1; n   char c = getchar(); n   while (c <  0  || c >  9 ) { n     if (c ==  - ) fh = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) tmp = tmp * 10 + c - 48, c = getchar(); n   return tmp * fh; n } n void pvarr_int(int *arr, int n, int strat = 1) { n   if (strat == 0) { n     n--; n   } n   for (int i = strat; i <= n; i++) { n     printf( %d%c , arr[i], i == n ?   n  :    ); n   } n } n void pvarr_LL(long long *arr, int n, int strat = 1) { n   if (strat == 0) { n     n--; n   } n   for (int i = strat; i <= n; i++) { n     printf( %lld%c , arr[i], i == n ?   n  :    ); n   } n } n const int maxn = 1000010; n const int inf = 0x3f3f3f3f; n int n, m; n long long a[maxn], b[maxn]; n struct node { n   int f, t; n   long long val; n   node() {} n   node(int ff, int tt, long long vv) { n     f = ff; n     t = tt; n     val = vv; n   } n   bool operator<(const node &bb) const { return val > bb.val; } n }; n std::vector<node> v; n int far[maxn]; n void dsu_init(int n) { n   for (int i = 0; i <= n; i++) { n     far[i] = i; n   } n } n int findpar(int x) { n   if (x == far[x]) { n     return x; n   } else { n     return far[x] = findpar(far[x]); n   } n } n void mg(int x, int y) { n   x = findpar(x); n   y = findpar(y); n   if (x == y) return; n   far[x] = y; n } n long long maxst() { n   dsu_init(n + m); n   sort((v).begin(), (v).end()); n   long long res = 0ll; n   for (auto e : v) { n     int x = e.f; n     int y = e.t; n     x = findpar(x); n     y = findpar(y); n     if (x != y) { n       res += e.val; n       far[x] = y; n     } n   } n   return res; n } n int main() { n   m = readint(); n   n = readint(); n   for (int i = 1; i <= m; i++) { n     a[i] = readll(); n   } n   for (int i = 1; i <= n; i++) { n     b[i] = readll(); n   } n   long long ans = 0ll; n   for (int i = 1; i <= m; i++) { n     int k = readint(); n     while (k--) { n       int x = readint(); n       ans += a[i] + b[x]; n       v.push_back(node(i, m + x, a[i] + b[x])); n       v.push_back(node(m + x, i, a[i] + b[x])); n     } n   } n   printf( %lld n , ans - maxst()); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int T, ans; n int dfs(vector<int> G[], int node, bool visit[], int k, bool first) { n   if (!visit[node]) { n     visit[node] = true; n     int counti = 0; n     for (auto it : G[node]) { n       if (!visit[it]) counti += dfs(G, it, visit, k, false); n     } n     ans += counti / k; n     if (counti != G[node].size() - 1) n       return 0; n     else if (G[node].size() % k != 1) n       return 0; n     else { n       return 1; n     } n   } n   return 0; n } n void solve() { n   int n, m, i, root, k; n   ans = 0; n   cin >> n >> k; n   vector<int> G[n]; n   bool all = true; n   bool visit[n]; n   for (i = 0; i < n - 1; i++) { n     int a, b; n     cin >> a >> b; n     a--, b--; n     G[a].push_back(b); n     G[b].push_back(a); n   } n   for (i = 0; i < n; i++) visit[i] = false; n   for (i = 0; i < n; i++) { n     if (G[i].size() % k != 1) { n       all = false; n       root = i; n       break; n     } n   } n   if (all) n     cout << (n - 1) / k <<   n ; n   else if (k == 1) { n     cout << n - 1 <<   n ; n   } else { n     dfs(G, root, visit, k, true); n     cout << ans <<   n ; n   } n } n int main() { n   cin >> T; n   while (T--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T, class L> n inline bool smax(T &x, L y) { n   return x < y ? (x = y, 1) : 0; n } n template <class T, class L> n inline bool smin(T &x, L y) { n   return y < x ? (x = y, 1) : 0; n } n const int maxn = 2e2 + 17, maxm = maxn * maxn; n int from[maxm], to[maxm], n, m; n bool bl[maxm], mark[maxn]; n vector<int> g[maxn], vec; n void see(int v) { n   if (mark[v]) return; n   mark[v] = 1; n   vec.push_back(v); n   for (auto i : g[v]) see(from[i] ^ to[i] ^ v); n } n void dfs(int v) { n   for (int i = g[v].size() - 1; i >= 0; i = (int)g[v].size() - 1) { n     int e = g[v][i], u = from[e] ^ to[e] ^ v; n     if (v != from[e]) swap(from[e], to[e]); n     g[v].pop_back(); n     if (bl[e]) continue; n     bl[e] = 1; n     dfs(u); n   } n } n void solve() { n   cin >> n >> m; n   int cm = m, ans = 0; n   for (int i = 0; i < m; i++) { n     cin >> from[i] >> to[i], from[i]--, to[i]--; n     g[from[i]].push_back(i), g[to[i]].push_back(i); n   } n   for (int i = 0; i < n; i++) ans += g[i].size() % 2 == 0; n   for (int i = 0; i < n; i++) n     if (!mark[i]) { n       vec.clear(); n       see(i); n       int last = -1; n       for (auto v : vec) n         if (g[v].size() & 1) n           if (last != -1) n             last = v; n           else { n             from[m] = v, to[m] = last; n             g[v].push_back(m), g[last].push_back(m); n             m++, last = -1; n           } n       dfs(i); n     } n   cout << ans <<   n ; n   for (int i = 0; i < cm; i++) cout << from[i] + 1 <<     << to[i] + 1 <<   n ; n } n int main() { n   ios::sync_with_stdio(0), cin.tie(0); n   int t; n   cin >> t; n   while (t--) { n     memset(mark, 0, sizeof mark); n     memset(bl, 0, sizeof bl); n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int comp[212345], ptr; n int idx[212345]; n map<int, int> place; n int h, m, n; n int merge(int x, int y) { n   if (x == -1) { n     return y; n   } n   return x; n } n struct segtree { n   int n; n   vector<int> tree; n   segtree(vector<int> base) { n     n = base.size(); n     tree.resize(2 * n); n     for (int i = 0; i < n; ++i) { n       tree[i + n] = base[i]; n     } n     for (int i = n - 1; i > 0; --i) { n       tree[i] = merge(tree[i + i], tree[i + i + 1]); n     } n   } n   int qry(int l) { n     int x = qry(l, n); n     if (x == -1) { n       return qry(0, l); n     } n     return x; n   } n   int qry(int l, int r) { n     int resl = -1, resr = -1; n     for (l += n, r += n; l < r; l /= 2, r /= 2) { n       if (l & 1) resl = merge(resl, tree[l++]); n       if (r & 1) resr = merge(tree[--r], resr); n     } n     return merge(resl, resr); n   } n   void upd(int pos, int val) { n     pos += n; n     tree[pos] = val; n     for (pos /= 2; pos > 0; pos /= 2) { n       tree[pos] = merge(tree[pos + pos], tree[pos + pos + 1]); n     } n   } n }; n vector<int> base[212345]; n void dfs(int x) { n   comp[x] = ptr; n   idx[x] = base[ptr].size(); n   base[ptr].push_back(x); n   int nxt = (x + m) % h; n   if (comp[nxt] == -1) { n     dfs(nxt); n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> h >> m >> n; n   long long ans = 0; n   memset(comp, -1, sizeof comp); n   for (int i = 0; i < h; ++i) { n     if (comp[i] == -1) { n       dfs(i); n       ++ptr; n     } n   } n   vector<segtree> segts; n   for (int i = 0; i < ptr; ++i) { n     segts.push_back(segtree(base[i])); n   } n   while (n--) { n     char a; n     int id, hash; n     cin >> a >> id; n     if (a ==  + ) { n       cin >> hash; n       int pos = segts[comp[hash]].qry(idx[hash]); n       int dummy = idx[pos] - idx[hash]; n       if (dummy < 0) { n         dummy += base[comp[hash]].size(); n       } n       ans += dummy; n       segts[comp[pos]].upd(idx[pos], -1); n       place[id] = pos; n     } else { n       int pos = place[id]; n       segts[comp[pos]].upd(idx[pos], pos); n     } n   } n   cout << ans <<   n ; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n bool func(vector<pair<int, int> > vd, vector<pair<int, int> > vu) { n   for (int i = 0; i < vd.size(); i++) { n     if ((vd[i].first < vu[i].first && vd[i].second < vu[i].second) || n         (vd[i].first > vu[i].first && vd[i].second > vu[i].second)) { n       return true; n     } n   } n   return false; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int t, i, j, k, n, m, n       a = 1, b = 1, c, p, prev, q, r, width, o, rc, height, mx = -999999999, n       mn = 99999999, ct = 0, ans = 0, flag = 1, sum = 0, pos1, pos2; n   cin >> n >> m >> k; n   std::vector<pair<long long int, long long int> > row(n), col(m); n   for (i = 1; i <= k; i++) { n     cin >> o >> rc >> a; n     if (o == 1) { n       row[rc - 1] = {a, i}; n     } else { n       col[rc - 1] = {a, i}; n     } n   } n   for (i = 0; i < n; i++) { n     for (j = 0; j < m; j++) { n       if (col[j].second > row[i].second) n         cout << col[j].first <<    ; n       else n         cout << row[i].first <<    ; n     } n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n priority_queue<pair<long long, int> > q; n vector<pair<long long, pair<int, int> > > E; n vector<pair<int, int> > e[120000]; n int n, m, x, y, k, z, fa[120000], i, be[120000], flag[120000]; n long long dis[120000], ans; n int get(int x) { return fa[x] == x ? x : (fa[x] = get(fa[x])); } n int main() { n   scanf( %d%d , &n, &m); n   for (i = 1; i <= m; ++i) { n     scanf( %d%d%d , &x, &y, &k); n     e[x].push_back(make_pair(y, k)); n     e[y].push_back(make_pair(x, k)); n   } n   memset(dis, 3, sizeof(dis)); n   scanf( %d , &k); n   for (i = 1; i <= k; ++i) { n     scanf( %d , &x); n     dis[x] = 0; n     fa[x] = x; n     q.push(make_pair(0, x)); n     be[x] = x; n   } n   while (!q.empty()) { n     x = q.top().second; n     q.pop(); n     if (flag[x]) continue; n     flag[x] = 1; n     for (i = 0; i < e[x].size(); ++i) { n       y = e[x][i].first; n       z = e[x][i].second; n       if (be[y]) n         E.push_back(make_pair(dis[x] + dis[y] + z, make_pair(be[x], be[y]))); n       if (dis[x] + z < dis[y]) { n         dis[y] = dis[x] + z; n         be[y] = be[x]; n         q.push(make_pair(-dis[y], y)); n       } n     } n   } n   sort(E.begin(), E.end()); n   for (i = 0; i < E.size(); ++i) { n     x = E[i].second.first; n     y = E[i].second.second; n     if (get(x) != get(y)) { n       ans += E[i].first; n       fa[get(x)] = get(y); n     } n   } n   printf( %I64d n , ans + dis[1]); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n int dp[N], a[N], n; n int main() { n   cin >> n; n   for (int i = 0; i < n; ++i) scanf( %d , &a[i]); n   if (n == 1) { n     cout << 1; n     return 0; n   } n   for (int i = 0; i < n; ++i) { n     if (a[i] == 1) continue; n     int d = 2, mx = 0, x = a[i]; n     while (d * d <= x) n       if (x % d == 0) { n         mx = max(mx, dp[d] + 1); n         x /= d; n       } else n         ++d; n     mx = max(mx, dp[x] + 1); n     d = 2; n     x = a[i]; n     while (d * d <= x) n       if (x % d == 0) { n         dp[d] = max(dp[d], mx); n         x /= d; n       } else n         ++d; n     dp[x] = max(dp[x], mx); n   } n   int mx = 0; n   for (int i = 1; i < N; ++i) mx = max(mx, dp[i]); n   cout << mx; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int oo = 1 << 30; n const double PI = M_PI; n const double EPS = 1e-15; n double w, h, u; n double cross(const complex<double> &a, const complex<double> &b) { n   return imag(conj(a) * b); n } n complex<double> rotate_by(const complex<double> &p, n                           const complex<double> &about, double radians) { n   return (p - about) * exp(complex<double>(0, radians)) + about; n } n complex<double> intersect(const complex<double> &a, const complex<double> &b, n                           const complex<double> &p, const complex<double> &q) { n   double d1 = cross(p - a, b - a); n   double d2 = cross(q - a, b - a); n   return (d1 * q - d2 * p) / (d1 - d2); n } n int main() { n   cin.sync_with_stdio(false); n   cin >> w >> h >> u; n   if (w < h) swap(w, h); n   if (u > 90) u = 180 - u; n   u = u * M_PI / 180.; n   cout.precision(10); n   if (u == 0) { n     cout << fixed << w * h << endl; n     return 0; n   } n   if (u >= (M_PI - 2. * atan(w / (double)h))) { n     cout << fixed << h * h / sin(u) << endl; n   } else { n     double answer = w * h; n     complex<double> a1 = n         rotate_by(complex<double>(+w / 2., -h / 2), complex<double>(0, 0), u); n     complex<double> a2 = n         rotate_by(complex<double>(+w / 2., +h / 2), complex<double>(0, 0), u); n     complex<double> a3 = n         rotate_by(complex<double>(-w / 2., +h / 2), complex<double>(0, 0), u); n     complex<double> b1 = intersect(a1, a2, complex<double>(-w / 2., h / 2.), n                                    complex<double>(w / 2., h / 2.)); n     complex<double> b2 = intersect(a2, a3, complex<double>(-w / 2., h / 2.), n                                    complex<double>(w / 2., h / 2.)); n     { n       double side1 = b1.real() - b2.real(); n       double side2 = side1 * cos(u); n       double area = side1 * side2 * sin(u); n       answer = answer - area; n     } n     { n       double side1 = w / 2. - b1.real(); n       double side2 = side1 * tan(M_PI / 2. - u); n       double area = side1 * side2; n       answer = answer - area; n     } n     cout << fixed << answer << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void lemon() { n   double R1, R2; n   int k; n   scanf( %lf%lf%d , &R1, &R2, &k); n   k++; n   double xl = 1 / (R1 * 2), xr = 1 / (R2 * 2); n   double xc = (xl + xr) / 2, rad = abs(xc - xl); n   double yc = rad * (k - 1) * 2; n   double len = sqrt(xc * xc + yc * yc); n   double final = (1 / (len - rad) - 1 / (len + rad)) / 2; n   printf( %.16lf n , final); n } n int main() { n   ios::sync_with_stdio(true); n   int tcase; n   scanf( %d , &tcase); n   while (tcase--) lemon(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int pp = (1e9) + 7; n long long P[1000010]; n long long ans, now; n int N, K; n long long quick(int x, int y) { n   long long s = 1, t = x; n   for (; y; y >>= 1) { n     if (y & 1) s = s * t % pp; n     t = t * t % pp; n   } n   return s; n } n long long get0() { n   long long t = 1; n   for (int i = N - K - 1; i < N; i++) t = t * i % pp; n   for (int i = 1; i <= K + 1; i++) t = t * quick(pp - i, pp - 2) % pp; n   return t; n } n int main() { n   scanf( %d%d , &N, &K); n   if (!K) { n     printf( %d n , N); n     return 0; n   } n   for (int i = 1; i <= K + 2; i++) P[i] = (P[i - 1] + quick(i, K)) % pp; n   if (N <= K + 2) { n     printf( %I64d n , P[N]); n     return 0; n   } n   ans = 0; n   now = get0(); n   for (int i = 1; i <= K + 1; i++) { n     now = now * quick(i, pp - 2) % pp * (N - i + 1) % pp * n           quick(N - i, pp - 2) % pp * (pp - (K - i + 2)) % pp; n     ans = (ans + P[i] * now) % pp; n   } n   printf( %I64d , ans); n } n


#include <bits/stdc++.h> n using namespace std; n long long G[333][333]; n long long min(long long a, long long b, long long c) { n   return min(a, min(b, c)); n } n int main() { n   int N; n   cin >> N; n   for (int i = 1; i <= N; i++) n     for (int j = 1; j <= N; j++) cin >> G[i][j]; n   int M; n   cin >> M; n   while (M--) { n     long long u, v, x; n     cin >> u >> v >> x; n     for (int i = 1; i <= N; i++) n       for (int j = 1; j <= N; j++) { n         G[i][j] = min(G[i][j], G[i][u] + G[v][j] + x, G[i][v] + G[u][j] + x); n       } n     long long sum = 0; n     for (int i = 1; i <= N; i++) n       for (int j = i; j <= N; j++) sum += G[i][j]; n     cout << sum <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 500005; n const int mod = 998244353; n const long long int inf = 10e17; n const long double pi = 3.14159265359; n mt19937_64 rang( n     chrono::high_resolution_clock::now().time_since_epoch().count()); n int rng(int lim) { n   uniform_int_distribution<int> uid(0, lim - 1); n   return uid(rang); n } n void fastIO(void); n void decimal(int); n int modmulti(int, int); n int modadd(int, int); n int modpower(int, int); n int gcd(int, int); n int modinv(int); n vector<int> parent, depth, heavy, head, pos, tin, tout; n int cur_pos; n int lazy1[4 * N], lazy2[8 * N]; n int timer, l; n vector<vector<int>> up; n int dfs(int v, vector<vector<int>> const& g) { n   int size = 1; n   tin[v] = ++timer; n   up[v][0] = parent[v]; n   for (int i = 1; i < l + 1; i++) { n     up[v][i] = up[up[v][i - 1]][i - 1]; n   } n   int max_c_size = 0; n   for (int c : g[v]) { n     if (c != parent[v]) { n       parent[c] = v; n       depth[c] = depth[v] + 1; n       int c_size = dfs(c, g); n       if (c_size > max_c_size) { n         max_c_size = c_size; n         heavy[v] = c; n       } n       size += c_size; n     } n   } n   tout[v] = ++timer; n   return size; n } n bool is_ancestor(int u, int v) { n   return tin[u] <= tin[v] && tout[u] >= tout[v]; n } n int lca(int u, int v) { n   if (is_ancestor(u, v)) return u; n   if (is_ancestor(v, u)) return v; n   for (int i = l; i >= 0; --i) { n     if (!is_ancestor(up[u][i], v)) u = up[u][i]; n   } n   return up[u][0]; n } n void decompose(int v, int h, vector<vector<int>> const& g) { n   head[v] = h; n   pos[v] = cur_pos++; n   if (heavy[v] != -1) { n     decompose(heavy[v], h, g); n   } n   for (int c : g[v]) { n     if (c != parent[v] && c != heavy[v]) decompose(c, c, g); n   } n } n void init(vector<vector<int>> const& g) { n   int n = g.size(); n   parent = vector<int>(n); n   depth = vector<int>(n); n   heavy = vector<int>(n, -1); n   head = vector<int>(n); n   pos = vector<int>(n); n   tin = vector<int>(n); n   tout = vector<int>(n); n   timer = 0; n   l = ceil(log2(n)); n   up.assign(n, vector<int>(l + 1)); n   cur_pos = 0; n   dfs(0, g); n   decompose(0, 0, g); n } n void update1(int node, int l, int r, int st, int ed, int val) { n   if (l == st && r == ed) { n     lazy1[node] = val; n     return; n   } n   int m = (l + r) / 2; n   if (ed <= m) { n     update1(2 * node + 1, l, m, st, ed, val); n     return; n   } n   if (st > m) { n     update1(2 * node + 2, m + 1, r, st, ed, val); n     return; n   } n   update1(2 * node + 1, l, m, st, m, val); n   update1(2 * node + 2, m + 1, r, m + 1, ed, val); n   return; n } n void update2(int node, int l, int r, int st, int ed, int val) { n   if (l == st && r == ed) { n     lazy2[node] = val; n     return; n   } n   int m = (l + r) / 2; n   if (ed <= m) { n     update2(2 * node + 1, l, m, st, ed, val); n     return; n   } n   if (st > m) { n     update2(2 * node + 2, m + 1, r, st, ed, val); n     return; n   } n   update2(2 * node + 1, l, m, st, m, val); n   update2(2 * node + 2, m + 1, r, m + 1, ed, val); n   return; n } n void update(int b, int val, int n) { n   int a = 0; n   for (; head[a] != head[b]; b = parent[head[b]]) { n     if (depth[head[a]] > depth[head[b]]) swap(a, b); n     update1(0, 0, n - 1, pos[head[b]], pos[b], val); n   } n   update1(0, 0, n - 1, pos[a], pos[b], val); n   return; n } n int query1(int node, int l, int r, int pos) { n   if (l == r) { n     return lazy1[node]; n   } n   lazy1[2 * node + 1] = max(lazy1[node], lazy1[2 * node + 1]); n   lazy1[2 * node + 2] = max(lazy1[node], lazy1[2 * node + 2]); n   int m = (l + r) / 2; n   if (pos <= m) return query1(2 * node + 1, l, m, pos); n   return query1(2 * node + 2, m + 1, r, pos); n } n int query2(int node, int l, int r, int pos) { n   if (l == r) { n     return lazy2[node]; n   } n   lazy2[2 * node + 1] = max(lazy2[node], lazy2[2 * node + 1]); n   lazy2[2 * node + 2] = max(lazy2[node], lazy2[2 * node + 2]); n   int m = (l + r) / 2; n   if (pos <= m) return query2(2 * node + 1, l, m, pos); n   return query2(2 * node + 2, m + 1, r, pos); n } n int main() { n   fastIO(); n   int n; n   cin >> n; n   vector<vector<int>> g(n); 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   } n   init(g); n   int m; n   cin >> m; n   int cnt = 0; n   for (int m1 = 1; m1 < m + 1; m1++) { n     int a, b; n     cin >> a >> b; n     b--; n     if (a == 1) { n       update2(0, 1, 2 * n, tin[b], tout[b], m1); n     } else if (a == 2) { n       update(b, m1, n); n     } else { n       cnt++; n       int q1, q2; n       q1 = query1(0, 0, n - 1, pos[b]); n       q2 = query2(0, 1, 2 * n, tin[b]); n       if (q1 >= q2) n         cout << 0 <<   n ; n       else n         cout << 1 <<   n ; n     } n   } n } n void fastIO(void) { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n } n void decimal(int n) { n   cout << fixed; n   cout << setprecision(n); n } n int modmulti(int a, int b) { return ((a % mod) * 1ll * (b % mod)) % mod; } n int modadd(int a, int b) { n   a = a % mod; n   a = (a + mod) % mod; n   b = b % mod; n   b = (b + mod) % mod; n   return (a + b) % mod; n } n int modpower(int a, int n) { n   if (n == 0) return 1; n   if (n == 1) return a % mod; n   int b = modpower(a, n / 2); n   b = modmulti(b, b); n   if (n % 2 == 0) return b; n   return modmulti(a, b); n } n int gcd(int a, int b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n int modinv(int a) { return modpower(a, mod - 2); } n


#include <bits/stdc++.h> n using namespace std; n set<int> m; n using namespace std; n bool check(int a) { n   m.erase(m.begin(), m.end()); n   int i = 0; n   while (a > 0) { n     int re = a % 10; n     m.insert(re); n     a /= 10; n     i++; n   } n   if (m.size() == i) { n     return true; n   } else n     return false; n } n int main() { n   int l, r, flag = 0; n   long long int ans = 0; n   cin >> l >> r; n   for (int i = l; i <= r; i++) { n     if (check(i) == true) { n       ans = i; n       flag = 1; n       break; n     } n   } n   if (flag == 1) { n     cout << ans << endl; n   } else n     cout <<  -1  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100005; n int n, l; n int cnt[N]; n long long sum[N]; n int main() { n   cin >> n >> l; n   for (int i = 1; i < n; i++) { n     cin >> cnt[i]; n     sum[i] = sum[i - 1] + cnt[i]; n   } n   long long ans = 2e18; n   for (int i = l; i < n; i++) { n     ans = min(ans, sum[i] - sum[i - l]); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using pii = pair<int, int>; n using pll = pair<ll, ll>; n int n, k; n string s; n int main(void) { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int t; n   cin >> t; n   while (t--) { n     cin >> n >> k; n     vector<int> alpha(26, 0); n     vector<int> cpy(26, 0); n     vector<int> cnt(26, 0); n     cin >> s; n     int ans = 0; n     for (char c : s) { n       alpha[c -  a ]++; n       cpy[c -  a ]++; n     } n     for (int i = 1; i <= min(n, k); i++) n       if (k % i == 0) n         for (int l = 1; l <= n; l++) { n           int tmp = 0; n           for (int a = 0; a < 26; a++) tmp += alpha[a] / l; n           if (tmp >= i) ans = max(ans, l * i); n         } n     cout << ans <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   map<char, int> cnt; n   if (s.size() > 2) n     for (int i = 1; i <= s.size() - 2; i++) { n       cnt.clear(); n       cnt[s[i - 1]]++; n       cnt[s[i]]++; n       cnt[s[i + 1]]++; n       if (cnt[ A ] == 1 && cnt[ B ] == 1 && cnt[ C ] == 1) { n         cout <<  Yes n ; n         return 0; n       } n     } n   cout <<  No n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char s[1000001], s1[1000001], s2[1000001]; n int n, ci, dian = -1, en, e2; n int main() { n   scanf( %s , s); n   n = strlen(s); n   if (n == 1 && s[0] ==  0 ) { n     puts( 0 ); n     return 0; n   } n   for (int i = 0; i < n; ++i) n     if (s[i] ==  . ) dian = i; n   if (dian == -1) s[dian = n++] =  . ; n   for (int i = 0; i < n; ++i) n     if (s[i] !=  0  && s[i] !=  . ) { n       if (dian > i) n         ci = dian - i - 1; n       else n         ci = dian - i; n       for (int j = 0; j <= i; ++j) n         if (s[j] !=  . ) s1[en++] = s[j]; n       for (int j = i + 1; j < n; ++j) n         if (s[j] !=  . ) s2[e2++] = s[j]; n       break; n     } n   for (int i = 0; i < en; ++i) n     if (s1[i] !=  0 ) { n       for (int j = i; j < en; ++j) putchar(s1[j]); n       break; n     } n   if (e2 != 0) { n     bool flag = 0; n     for (int i = e2 - 1; i >= 0; --i) n       if (s2[i] !=  0 ) { n         flag = 1; n         break; n       } n     if (flag) putchar( . ); n     for (int i = e2 - 1; i >= 0; --i) n       if (s2[i] !=  0 ) { n         for (int j = 0; j <= i; ++j) putchar(s2[j]); n         break; n       } n   } n   if (ci != 0) printf( E%d n , ci); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool same(int l1, int r1, int l2, int r2) { n   if (r1 < l2 || r2 < l1) { n     return 0; n   } n   return 1; n } n signed main() { n   int q; n   cin >> q; n   while (q--) { n     int n; n     cin >> n; n     int l = 1e9 + 5, r = 0; n     for (int i = 0; i < n; i++) { n       int l3, r3; n       cin >> l3 >> r3; n       r = max(r, l3); n       l = min(l, r3); n     } n     cout << max(0, r - l) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int chkpower2(int x) { return (x && !(x & x - 1)); } n void solve() { n   long long int n; n   cin >> n; n   long long int a[n], sum = 0; n   for (int i1 = 0; i1 < n; i1++) { n     cin >> a[i1]; n     sum += a[i1]; n   } n   sort(a, a + n); n   if (sum % (n - 1)) { n     sum = sum / (n - 1) + 1; n   } else { n     sum = sum / (n - 1); n   } n   cout << max(a[n - 1], sum); n   return; n } n int main() { n   int t; n   t = 1; n   while (t--) { n     solve(); n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool vowel(char a) { n   a = toupper(a); n   if ((a ==  A ) || (a ==  E ) || (a ==  I ) || (a ==  O ) || (a ==  U )) n     return 1; n   return 0; n } n long long int n, m, q, a, b, c, sum = 0, cnt = 0; n long long int arr[500055]; n map<long long int, bool> mp; n string str; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n   } n   bool one = 1, two = 1, tr = 1; n   for (int i = 0; i < n; i++) { n     if (arr[i] > arr[i - 1]) { n       if (!tr || !two) { n         cout <<  NO  << endl; n         return 0; n       } n     } n     if (arr[i] == arr[i - 1]) { n       two = 0; n       if (tr) n         continue; n       else { n         cout <<  NO  << endl; n         return 0; n       } n     } n     if (arr[i] < arr[i - 1]) { n       tr = 0; n     } n   } n   cout <<  YES  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long n, m; n   cin >> n >> m; n   ; n   int a[n + 1]; n   memset(a, 0, sizeof(a)); n   for (long long i = 0; i < m; i++) { n     long long t; n     cin >> t; n     ; n     for (long long j = t; j < n + 1; j++) { n       if (a[j] == 0) a[j] = t; n     } n   } n   for (long long i = 1; i < n + 1; i++) { n     cout << a[i] <<    ; n   } n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N, K; n vector<int> tabs; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> N >> K; n   tabs.resize(N); n   for (int i = 0; i < N; i++) cin >> tabs[i]; n   int most = 0; n   for (int b = 0; b < N; b++) { n     int sum = 0; n     for (int i = 0; i < N; i++) n       if (i % K != b % K) sum += tabs[i]; n     most = max(most, abs(sum)); n   } n   cout << most <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void setmax(T& x, T y) { n   x = max(x, y); n } n template <typename T> n void setmin(T& x, T y) { n   x = min(x, y); n } n const double PI = acos(-1.0); n const long long INF = 1e9 + 47; n const long long LINF = INF * INF; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n const int N = 1e7 + 7; n int lp[N]; n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   iota(lp, lp + N, 0); n   for (int i = (2); i < (N); ++i) n     if (lp[i] == i) n       for (int j = i + i; j < N; j += i) setmin(lp[j], i); n   int n; n   cin >> n; n   vector<pair<int, int> > ans(n, make_pair(-1, -1)); n   for (int i = (0); i < (n); ++i) { n     int x; n     cin >> x; n     int p = lp[x]; n     int t = x; n     while (t % p == 0) t /= p; n     if (t > 1) { n       ans[i] = {t, p}; n     } n   } n   for (int i = (0); i < (n); ++i) { n     cout << ans[i].first <<    ; n   } n   cout << endl; n   for (int i = (0); i < (n); ++i) { n     cout << ans[i].second <<    ; n   } n   cout << endl; n   cerr <<  Time elapsed:   << clock() / (double)CLOCKS_PER_SEC << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 1 << 30; n const int md = 1e9 + 7; n const int N = 1e5 + 10; n int n, m; n int T; n int k; n long long p; n long long q[N]; n long long ar[N]; n int now = -1; n set<long long> se[N]; n inline long long up(long long x, long long y) { n   long long re = x / y - 1; n   while (re * y < x) re++; n   return re; n } n inline long long down(long long x, long long y) { n   long long re = x / y + 1; n   while (re * y > x) re--; n   return re; n } n bool dfs(int x) { n   if (x >= N - 5) return 0; n   if (se[x].count(q[x])) return 0; n   se[x].insert(q[x]); n   if (0 <= q[x] && q[x] < k) { n     now = x; n     ar[0] = q[0] * k + p; n     for (int i = 0; i <= x - 1; i++) ar[i + 1] = q[i] + q[i + 1] * k; n     ar[x + 1] = q[x]; n     return 1; n   } n   for (q[x + 1] = up(-q[x], k); q[x + 1] <= 1 + down(-q[x], k); q[x + 1]++) { n     if (dfs(x + 1)) return 1; n   } n   return 0; n } n void solve() { n   long long l = up(-p, k); n   long long r = 1 + down(-p, k); n   for (q[0] = l; q[0] <= r; q[0]++) n     if (dfs(0)) break; n   if (now >= 0) { n     now++; n     while (ar[now] == 0) now--; n     printf( %d n , now + 1); n     for (int i = 0; i <= now; i++) n       printf( %I64d%c , ar[i], i == now ? 10 :    ); n   } else { n     puts( -1 ); n   } n } n int main() { n   scanf( %I64d%d , &p, &k); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5005; n int n, m, w, o[N], K[N], P[N], C[N]; n int vis[N]; n vector<int> adj[N]; n void bfs(int src) { n   for (int i = 0; i <= n; ++i) vis[i] = -1; n   queue<int> q, qw; n   q.push(src); n   qw.push(0); n   vis[src] = 0; n   while (!q.empty()) { n     int u = q.front(); n     q.pop(); n     int w = qw.front(); n     qw.pop(); n     for (auto v : adj[u]) { n       if (vis[v] == -1) { n         vis[v] = w + 1; n         q.push(v); n         qw.push(w + 1); n       } n     } n   } n } n int q; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < m; ++i) { n     int u, v; n     scanf( %d%d , &u, &v); n     adj[u].push_back(v); n     adj[v].push_back(u); n   } n   scanf( %d , &w); n   for (int i = 0; i < w; ++i) { n     int c, k, p; n     scanf( %d%d%d , &c, &k, &p); n     o[i] = i; n     K[i] = k; n     P[i] = p; n     C[i] = c; n   } n   sort(o, o + w, [](int i, int j) { return P[i] < P[j]; }); n   scanf( %d , &q); n   while (q--) { n     int g, r, a; n     scanf( %d%d%d , &g, &r, &a); n     int L = 0, R = n; n     bfs(g); n     while (L < R) { n       int M = (L + R) / 2; n       long long totalCost = 0; n       int shovels = 0; n       for (int i = 0; i < w; ++i) { n         int id = o[i]; n         if (vis[C[id]] != -1 && vis[C[id]] <= M) { n           int need = min(r - shovels, K[id]); n           totalCost += 1ll * need * P[id]; n           shovels += need; n           if (shovels >= r) break; n         } n       } n       if (totalCost > a || shovels < r) { n         L = M + 1; n       } else { n         R = M; n       } n     } n     if (L == n) { n       L = -1; n     } n     printf( %d n , L); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n; n void solve_1() { n   if (n < 6) { n     printf( -1 n ); n     return; n   } n   puts( 1 2 ); n   puts( 2 3 ); n   puts( 2 4 ); n   puts( 4 5 ); n   puts( 4 6 ); n   for (int i = 7; i <= n; i++) { n     printf( 2 %d n , i); n   } n } n void solve_2() { n   for (int i = 2; i <= n; i++) { n     printf( 1 %d n , i); n   } n } n int main() { n   scanf( %d , &n); n   solve_1(); n   solve_2(); n } n


#include <bits/stdc++.h> n #define debug(x) cerr << #x <<   =   << x << endl n using namespace std; n typedef long long LL; n const int N = 400005; n int a[N], b[N], n; n vector<int> vec; n  n int main() n { n     int T; scanf( %d , &T); n     while(T--) { n         vec.clear(); n         scanf( %d , &n); n         for(int i = 1; i <= n; i++) scanf( %d , &a[i]); n  n         //1 n         for(int i = 1; i <= n; i++) b[i] = a[i]; n         int pos = -1; n         for(int i = 2; i <= n; i++) { n             b[i] = b[i] - b[i - 1]; n             b[i - 1] -= b[i - 1]; n             if(b[i] < 0) { n                 pos = i; n                 break; n             } n         } n         if(b[n]) { n             if(pos == -1) pos = n; n         } n         if(pos == -1) { n             puts( YES ); n             continue; n         } n         if(pos > 0) { n             vec.push_back(pos); n             for(int id = 1; id <= 10; id++) { n                 vec.push_back(pos - id); n                 vec.push_back(pos + id); n             } n         } n  n         //2 n         pos = -1; n         for(int i = 1; i <= n; i++) b[i] = a[i]; n         for(int i = n - 1; i >= 1; i--) { n             b[i] = b[i] - b[i + 1]; n             b[i + 1] -= b[i + 1]; n             if(b[i] < 0) { n                 pos = i; n                 break; n             } n         } n         if(b[1]) { n             if(pos == -1) pos = 1; n         } n         if(pos > 0) { n             vec.push_back(pos); n             for(int id = 1; id <= 10; id++) { n                 vec.push_back(pos - id); n                 vec.push_back(pos + id); n             } n         } n  n  n         //3 n         for(int i = 1; i <= n; i++) b[i] = a[i]; n         pos = -1; n         bool ok = false; n         for(int i = 2; i <= n; i++) { n             b[i] = b[i] - b[i - 1]; n             if(b[i] < 0) { n                 if(!ok) pos = i, ok = true; n             } n         } n         b[1] = 0; n         if(b[n]) { n             if(pos == -1) pos = n; n         } n         int tmp = b[n]; n //        if(tmp % 2) { n //            puts( NO ); n //            continue; n //        } n         for(int i = n; i > 0; i -= 2) { n             if(i <= pos) { n                 if(a[i] - a[i + 1] == tmp / 2 || a[i] - a[i - 1] == tmp / 2) { n                     vec.push_back(i); n                     break; n                 } n             } n         } n  n  n         int sz = vec.size(); n         bool flag = true; n         for(int i = 0; i < sz; i++) { n             int now = vec[i]; n             if(now < 1 || now > n) continue; n             if(now > 1) { n                 for(int j = 1; j <= n; j++) b[j] = a[j]; n                 flag = true; n                 swap(b[now], b[now - 1]); n                 for(int j = 2; j <= n; j++) { n                     b[j] = b[j] - b[j - 1]; n                     b[j - 1] -= b[j - 1]; n                     if(b[j] < 0) { n                         flag = false; n                         break; n                     } n                 } n                 if(b[n]) flag = false; n                 if(flag) break; n             } n             if(now < n) { n                    for(int j = 1; j <= n; j++) b[j] = a[j]; n                    flag = true; n                    swap(b[now], b[now + 1]); n                    for(int j = 2; j <= n; j++) { n                     b[j] = b[j] - b[j - 1]; n                     b[j - 1] -= b[j - 1]; n                     if(b[j] < 0) { n                         flag = false; n                         break; n                     } n                 } n                 if(b[n]) flag = false; n                 if(flag) break; n             } n         } n         if(!sz) flag = false; n         if(flag) puts( YES ); n         else puts( NO ); n     } n     return 0; n } n  n


#include <bits/stdc++.h> n using namespace std; n struct debugger { n   template <typename T> n   debugger& operator,(const T& v) { n     cerr << v <<   t ; n     return *this; n   } n } dbg; n template <class T> n void setmax(T& a, T b) { n   if (a < b) a = b; n } n template <class T> n void setmin(T& a, T b) { n   if (b < a) a = b; n } n template <class T> n T Abs(T x) { n   return x > 0 ? x : -x; n } n template <class T> n inline T sqr(T x) { n   return x * x; n } n template <class T> n inline T Mod(T n, T m) { n   return (n % m + m) % m; n } n template <class T> n string toString(T n) { n   ostringstream oss; n   oss << n; n   oss.flush(); n   return oss.str(); n } n int toInt(string s) { n   int r = 0; n   istringstream sin(s); n   sin >> r; n   return r; n } n bool isVowel(char ch) { n   ch = tolower(ch); n   if (ch ==  a  || ch ==  e  || ch ==  i  || ch ==  o  || ch ==  u ) n     return true; n   return false; n } n bool isUpper(char c) { return c >=  A  && c <=  Z ; } n bool isLower(char c) { return c >=  a  && c <=  z ; } n long long Pow(long long B, long long P) { n   long long R = 1; n   while (P > 0) { n     if (P % 2 == 1) R = (R * B); n     P /= 2; n     B = (B * B); n   } n   return R; n } n int BigMod(long long B, long long P, long long M) { n   long long R = 1; n   while (P > 0) { n     if (P % 2 == 1) { n       R = (R * B) % M; n     } n     P /= 2; n     B = (B * B) % M; n   } n   return (int)R; n } n void binprint(long long mask, long long n) { n   long long i; n   string s =   ; n   do { n     s += (mask % 2 +  0 ); n     mask /= 2; n   } while (mask); n   reverse(s.begin(), s.end()); n   s = string(max(n - (int)s.size(), 0LL),  0 ) + s; n   for (i = (int)s.size() - n; i < (int)s.size(); i++) printf( %c , s[i]); n   printf(  n ); n } n void ASCII_Chart() { n   int i, j, k; n   printf( ASCII Chart:(30-129) n ); n   for (i = 30; i < 50; i++) { n     for (j = 0; j < 5; j++) { n       k = i + j * 20; n       printf( %3d--->  %c     , k, k); n     } n     printf(  n ); n   } n } n struct edges { n   int u, v, ind; n   double w; n   edges(int _u = 0, int _v = 0, double _w = 0, int _ind = 0) { n     u = _u; n     v = _v; n     w = _w; n     ind = _ind; n   } n }; n vector<edges> e, e1, e2; n int N, M, p[5050], cnt, K; n bool comp(edges p, edges q) { return p.w < q.w; } n void precalc() { n   e1.clear(); n   e2.clear(); n   for (int i = 0; i < M; i++) { n     if (e[i].u == 0 || e[i].v == 0) n       e1.push_back(e[i]); n     else n       e2.push_back(e[i]); n   } n   sort(e1.begin(), e1.end(), comp); n   sort(e2.begin(), e2.end(), comp); n } n void merge(vector<edges> e3) { n   e.clear(); n   int i = 0, j = 0; n   while (i < e2.size() && j < e3.size()) { n     if (e2[i].w < e3[j].w) n       e.push_back(e2[i++]); n     else n       e.push_back(e3[j++]); n   } n   for (; i < e2.size(); i++) e.push_back(e2[i]); n   for (; j < e3.size(); j++) e.push_back(e3[j]); n   return; n } n int find_parent(int u) { n   if (u != p[u]) p[u] = find_parent(p[u]); n   return p[u]; n } n void Union(int a, int b) { p[b] = a; } n int calc_k(double x) { n   int ret, i; n   vector<edges> e3; n   for (i = 0; i < e1.size(); i++) { n     e3.push_back(e1[i]); n     e3[i].w += x; n   } n   merge(e3); n   for (i = 0; i < N; i++) p[i] = i; n   cnt = ret = 0; n   for (i = 0; i < M; i++) { n     int a = find_parent(e[i].u); n     int b = find_parent(e[i].v); n     if (a != b) { n       e[i].ind = -e[i].ind; n       Union(a, b); n       if (e[i].u == 0 || e[i].v == 0) ret++; n       cnt++; n     } n   } n   return ret; n } n bool solve() { n   double low = -10001000, high = 10001000; n   for (int i = 0; i < e1.size(); i++) e1[i].w += (i * 1e-7); n   while (fabs(low - high) > 1e-7) { n     double mid = (low + high) / 2; n     if (calc_k(mid) > K) n       low = mid; n     else n       high = mid; n   } n   if (calc_k(low) == K) return true; n   return (calc_k(high) == K); n } n int main() { n   int i, j, test, Case = 1, u, v, w; n   test = 1; n   while (test--) { n     scanf( %d %d %d , &N, &M, &K); n     e.clear(); n     for (i = 0; i < M; i++) { n       scanf( %d %d %d , &u, &v, &w); n       e.push_back(edges(u - 1, v - 1, w, i + 1)); n     } n     if (K == 0) { n       if (N <= 1) n         printf( 0 n ); n       else n         printf( -1 n ); n       continue; n     } n     precalc(); n     calc_k(0); n     if (cnt != N - 1 || e1.size() < K) { n       printf( -1.. n ); n       continue; n     } n     double total = 0; n     int ind; n     if (solve()) { n       printf( %d n , N - 1); n       for (i = 0; i < M; i++) n         if (e[i].ind <= 0) { n           printf( %d  , -e[i].ind); n         } n       printf(  n ); n     } else n       printf( -1__- n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int head[2 * 200005], nex[2 * 200005], from[2 * 200005], to[2 * 200005], cnt, n; n int sn[200005], num[200005]; n long long d[200005], ans; n void add(int u, int v) { n   nex[++cnt] = head[u]; n   from[cnt] = u; n   to[cnt] = v; n   head[u] = cnt; n   nex[++cnt] = head[v]; n   from[cnt] = v; n   to[cnt] = u; n   head[v] = cnt; n } n void dfs(int x, int p) { n   sn[x] = 1; n   for (int i = head[x]; i; i = nex[i]) { n     int u = to[i]; n     if (u == p) continue; n     dfs(u, x); n     for (int j = head[u]; j; j = nex[j]) { n       int v = to[j]; n       if (v == x) continue; n       d[x] += d[v] + sn[v]; n     } n     d[x]++; n     sn[x] += sn[u]; n   } n } n void dfs2(int x, int p) { n   ans += d[x]; n   for (int i = head[x]; i; i = nex[i]) { n     int u = to[i]; n     if (u == p) continue; n     for (int j = head[u]; j; j = nex[j]) { n       int v = to[j]; n       if (v == x) continue; n       d[v] = d[x] - sn[v] + n - sn[u]; n       dfs2(v, u); n     } n     if (p == 0) { n       d[u]++; n       dfs2(u, x); n     } n   } n } n int main() { n   cin >> n; n   int u, v; n   for (int i = 1; i < n; i++) { n     scanf( %d%d , &u, &v); n     add(u, v); n     num[u]++; n     num[v]++; n   } n   int root; n   for (int i = 1; i <= n; i++) n     if (num[i] == 1) { n       root = i; n       break; n     } n   dfs(root, 0); n   dfs2(root, 0); n   cout << ans / 2 << endl; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n #pragma comment(linker,  /STACK:100000000 ) n const double pi = acos(-1); n const int MOD = 1e9 + 7; n const int INF = 1e9 + 7; n const int MAXN = 5e3 + 5; n const double eps = 1e-9; n using namespace std; n bool vis[MAXN]; n int a[MAXN * 20], L[MAXN], R[MAXN], mx[MAXN]; n double dp[MAXN][MAXN], P[MAXN], tmp2[MAXN]; n set<int> ind; n int main() { n   int n, q; n   scanf( %d , &(n)), scanf( %d , &(q)); n   for (int i = 0; i < n; i++) ind.insert(i); n   for (int i = 0; i < (n); i++) scanf( %d , &(a[i])); n   for (int i = 0; i < q; i++) { n     scanf( %d %d %lf , L + i, R + i, P + i); n     L[i]--, R[i]--; n     for (int j = 0; j < i; j++) n       if (R[j] - L[j] > R[i] - L[i]) n         swap(L[i], L[j]), swap(R[i], R[j]), swap(P[i], P[j]); n   } n   L[q] = 0, R[q] = n - 1, P[q] = 0, q++; n   for (int i = 0; i < q; i++) { n     vector<int> tmp; n     for (int j = 0; j < i; j++) n       if (L[i] <= L[j] && R[j] <= R[i] && !vis[j]) { n         vis[j] = 1; n         tmp.push_back(j); n         mx[i] = max(mx[i], mx[j]); n       } n     while (1) { n       set<int>::iterator it = ind.lower_bound(L[i]); n       if (it != ind.end() && *it <= R[i]) n         mx[i] = max(mx[i], a[*it]), ind.erase(it); n       else n         break; n     } n     for (int j = 0; j <= q; j++) tmp2[j] = 1; n     for (int x = 0; x < tmp.size(); x++) { n       int j = tmp[x]; n       for (int k = 0; k <= q; k++) n         if (mx[i] + k - mx[j] <= q) tmp2[k] *= dp[j][mx[i] + k - mx[j]]; n     } n     for (int j = q; j > 0; j--) tmp2[j] -= tmp2[j - 1]; n     dp[i][0] = (1 - P[i]) * tmp2[0]; n     for (int j = 1; j <= q; j++) n       dp[i][j] = dp[i][j - 1] + P[i] * tmp2[j - 1] + (1 - P[i]) * tmp2[j]; n   } n   double ans = dp[q - 1][0] * mx[q - 1]; n   for (int i = 1; i <= q; i++) n     ans += (dp[q - 1][i] - dp[q - 1][i - 1]) * (mx[q - 1] + i); n   printf( %.15f n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int A[200005], B[200005], N; n int O[200005][5]; n void ucitaj() { n   scanf( %d , &N); n   if (N == 5) { n     printf( 1 5 3 4 2 n ); n     exit(0); n   } n   int i, j; n   for (i = 1; i <= 2 * N; i++) { n     scanf( %d%d , A + i, B + i); n     if (O[A[i]][1] == 0) n       O[A[i]][1] = B[i]; n     else if (O[A[i]][2] == 0) n       O[A[i]][2] = B[i]; n     else if (O[A[i]][3] == 0) n       O[A[i]][3] = B[i]; n     else n       O[A[i]][4] = B[i]; n     if (O[B[i]][1] == 0) n       O[B[i]][1] = A[i]; n     else if (O[B[i]][2] == 0) n       O[B[i]][2] = A[i]; n     else if (O[B[i]][3] == 0) n       O[B[i]][3] = A[i]; n     else n       O[B[i]][4] = A[i]; n   } n } n bool Sus6[10][10]; n bool susedi(int a, int b) { n   if (Sus6[a][b]) return false; n   return O[a][1] == b || O[a][2] == b || O[a][3] == b || O[a][4] == b; n } n bool susedi6(int a, int b) { n   return O[a][1] == b || O[a][2] == b || O[a][3] == b || O[a][4] == b; n } n void sus6() { n   int i, j, k; n   for (i = 1; i <= 6; i++) { n     for (j = i + 1; j <= 6; j++) { n       for (k = i + 1; k <= 6; k++) { n         if (susedi6(i, j) && susedi6(j, k) && susedi6(i, k)) { n           Sus6[i][j] = true; n           Sus6[i][k] = true; n           Sus6[k][j] = true; n           Sus6[k][i] = true; n           Sus6[j][k] = true; n           Sus6[j][i] = true; n         } n       } n     } n   } n } n void probaj(int *A) { n   int i, j; n   if (!susedi6(A[1], A[2])) return; n   if (!susedi6(A[2], A[3])) return; n   if (!susedi6(A[3], A[4])) return; n   if (!susedi6(A[4], A[5])) return; n   if (!susedi6(A[5], A[6])) return; n   if (!susedi6(A[6], A[1])) return; n   if (!susedi6(A[1], A[3])) return; n   if (!susedi6(A[2], A[4])) return; n   if (!susedi6(A[3], A[5])) return; n   if (!susedi6(A[4], A[6])) return; n   if (!susedi6(A[5], A[1])) return; n   if (!susedi6(A[6], A[2])) return; n   if (susedi6(A[1], A[4])) return; n   if (susedi6(A[2], A[5])) return; n   if (susedi6(A[3], A[6])) return; n   for (i = 1; i <= 6; i++) printf( %d n , A[i]); n   exit(0); n } n int S1[200005], S2[200005]; n void ubaci() { n   int i, j, k, l, u, v; n   if (N == 6) { n     sus6(); n     for (i = 1; i <= N; i++) A[i] = i; n     for (i = 1; i <= 720; i++) { n       probaj(A); n       next_permutation(A + 1, A + 7); n     } n     printf( -1 n ); n     exit(0); n   } n   for (i = 1; i <= 2 * N; i++) { n     u = A[i]; n     v = B[i]; n     k = 0; n     if (O[u][1] == O[v][1]) k++; n     if (O[u][2] == O[v][1]) k++; n     if (O[u][3] == O[v][1]) k++; n     if (O[u][4] == O[v][1]) k++; n     if (O[u][1] == O[v][2]) k++; n     if (O[u][2] == O[v][2]) k++; n     if (O[u][3] == O[v][2]) k++; n     if (O[u][4] == O[v][2]) k++; n     if (O[u][1] == O[v][3]) k++; n     if (O[u][2] == O[v][3]) k++; n     if (O[u][3] == O[v][3]) k++; n     if (O[u][4] == O[v][3]) k++; n     if (O[u][1] == O[v][4]) k++; n     if (O[u][2] == O[v][4]) k++; n     if (O[u][3] == O[v][4]) k++; n     if (O[u][4] == O[v][4]) k++; n     if (k == 2) { n       if (S1[u] == 0) n         S1[u] = v; n       else n         S2[u] = v; n       if (S1[v] == 0) n         S1[v] = u; n       else n         S2[v] = u; n     } n   } n } n bool V[200005]; n int Red[200005], RRR; n void obidji() { n   int i, j, k; n   k = 1; n   j = 1; n   V[1] = true; n   Red[++RRR] = 1; n   while (k < N) { n     if (!V[S1[j]]) { n       j = S1[j]; n       V[j] = true; n     } else if (!V[S2[j]]) { n       j = S2[j]; n       V[j] = true; n     } else { n       printf( -1 n ); n       exit(0); n     } n     Red[++RRR] = j; n     k++; n   } n   for (i = 1; i <= N; i++) printf( %d%c , Red[i], i == N ?   n  :    ); n } n int main() { n   ucitaj(); n   ubaci(); n   obidji(); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   int n; n   cin >> n; n   vector<pair<int, int>> a(n); n   for (int i = 0; i < n; i++) { n     cin >> a[i].first; n   } n   for (int i = 0; i < n; i++) { n     cin >> a[i].second; n   } n   sort(a.begin(), a.end()); n   a.push_back(make_pair(static_cast<int>(2e9), 0)); n   n++; n   multiset<int> s; n   int recent = 0; n   long long ans = 0LL, sum = 0LL; n   for (int i = 0; i < n; i++) { n     while (a[i].first > recent && s.size()) { n       recent++; n       auto it = prev(s.end()); n       sum -= *it; n       ans += sum; n       s.erase(it); n     } n     recent = a[i].first; n     sum += a[i].second; n     s.insert(a[i].second); n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int t; n   scanf( %d , &t); n   while (t--) { n     int n; n     scanf( %d , &n); n     int k; n     int a[n]; n     for (k = 0; k < n; k++) scanf( %d , &a[k]); n     int p = n - 2; n     int i, j; n     for (i = 0; i < p; i++) { n       for (j = i + 2; j < n; j++) { n         if (a[i] == a[j]) { n           printf( YES n ); n           break; n         } n       } n       if (a[i] == a[j]) { n         break; n       } n     } n     if (a[i] == a[j]) continue; n     printf( NO n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, performance; n   long long cost = 0; n   long long k; n   cin >> n >> k; n   int x[n], c[n]; n   for (int& e : x) cin >> e; n   cin >> performance; n   for (int& e : c) cin >> e; n   priority_queue<int, vector<int>, greater<int>> rem; n   int i = 0; n   for (int e : x) { n     rem.push(c[i++]); n     while (k < e) { n       if (rem.empty()) { n         cout <<  -1 n ; n         return 0; n       } n       k += performance, cost += rem.top(), rem.pop(); n     } n   } n   cout << cost <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct dame { n   int b, i, r; n   dame() {} n   dame(int b_, int i_, int r_) : b(b_), i(i_), r(r_) {} n }; n bool greater_compare(dame A, dame B) { n   return A.b > B.b || A.b == B.b && A.i < B.i || n          A.b == B.b && A.i == B.i && A.r < B.r; n } n bool by_i(dame a, dame b) { return a.i < b.i; } n int size = 2; n vector<int> tree; n void update(int pos, int value) { n   pos += size; n   if (value <= tree[pos]) return; n   tree[pos] = value; n   pos /= 2; n   while (pos > 0) { n     tree[pos] = max(tree[pos * 2], tree[pos * 2 + 1]); n     pos /= 2; n   } n } n int getmax(int l, int r, int i = 1, int a = 0, int b = size - 1) { n   if (l > r) return 0; n   if (l == a && r == b) return tree[i]; n   int center = (a + b) / 2; n   if (r <= center) return getmax(l, r, i * 2, a, center); n   if (l > center) return getmax(l, r, i * 2 + 1, center + 1, b); n   return max(getmax(l, center, i * 2, a, center), n              getmax(center + 1, r, i * 2 + 1, center + 1, b)); n } n int main() { n   int n, answer = 0; n   scanf( %d , &n); n   while (size < n) size *= 2; n   vector<dame> dames(n); n   tree = vector<int>(size * 2, 0); n   for (int i = 0; i < n; i++) scanf( %d , &dames[i].b); n   for (int i = 0; i < n; i++) scanf( %d , &dames[i].i); n   for (int i = 0; i < n; i++) scanf( %d , &dames[i].r); n   sort(dames.begin(), dames.end(), by_i); n   int compress_val = -1, prev = -1; n   for (int i = 0; i < n; i++) { n     if (dames[i].i != prev) compress_val++; n     prev = dames[i].i; n     dames[i].i = compress_val; n   } n   sort(dames.begin(), dames.end(), greater_compare); n   for (int i = 0; i < n; i++) { n     int max_r = getmax(dames[i].i + 1, n - 1); n     if (max_r > dames[i].r) answer++; n     update(dames[i].i, dames[i].r); n   } n   printf( %d n , answer); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cout <<  INTERCAL ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long double PI = 3.1415926535897932384626433832795; n template <typename S, typename T> n ostream& operator<<(ostream& out, pair<S, T> const& p) { n   out <<  (  << p.first <<  ,   << p.second <<  ) ; n   return out; n } n template <typename T> n ostream& operator<<(ostream& out, vector<T> const& v) { n   int l = v.size(); n   for (int i = 0; i < l - 1; i++) out << v[i] <<    ; n   if (l > 0) out << v[l - 1]; n   return out; n } n void tr() { cout << endl; } n template <typename S, typename... Strings> n void tr(S x, const Strings&... rest) { n   cout << x <<    ; n   tr(rest...); n } n long long fast(long long a, long long b, long long MOD) { n   long long ret = 1; n   while (b) { n     if ((b & 1)) ret = (ret * a) % MOD; n     a = (a * a) % MOD; n     b >>= 1; n   } n   return ret; n } n const long long MOD = 1e6 + 3; n int main() { n   long long n, k; n   cin >> n >> k; n   if (n <= 59 and (1ll << n) < k) { n     cout << 1 <<     << 1 << endl; n     return 0; n   } n   long long a, b; n   b = fast(2, n, MOD); n   b = fast(b, k - 1, MOD); n   a = 1; n   long long x = (k - 1) / MOD; n   long long y = (k - 1) % MOD; n   long long t = fast(2, n, MOD); n   for (int i = 1; i <= MOD; i++) { n     a *= (t - i + MOD) % MOD; n     a %= MOD; n   } n   a = fast(a, x, MOD); n   for (int i = 1; i <= y; i++) { n     a *= (t - i + MOD) % MOD; n     a %= MOD; n   } n   long long tot = 0, tmp = k - 1, pw = 2; n   while (pw <= tmp) { n     tot += tmp / pw; n     pw <<= 1; n   } n   a = a * fast(fast(2, tot, MOD), MOD - 2, MOD) % MOD; n   b = b * fast(fast(2, tot, MOD), MOD - 2, MOD) % MOD; n   a = (b - a + MOD) % MOD; n   cout << a <<     << b << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n  n /*DEBUGGER*/ n #define deb(x) cout << #x <<  =  << x << endl n #define deb2(x, y) cout << #x <<  =  << x <<  ,  << #y <<  =  << y << endl n  n typedef long long ll; n typedef pair<int, int> pii; n typedef pair<ll, ll> pll; n typedef pair<string, string> pss; n typedef vector<int> vi; n typedef vector<vi> vvi; n typedef vector<pii> vii; n typedef vector<ll> vll; n typedef vector<vll> vvll; n  n #define fl(i, a, n) for (int i = a; i < n; i++) n #define fll(i, a, n) for (ll i = a; i < n; i++) n #define tr(it, a) for (auto it = a.begin(); it != a.end(); it++) n #define all(c) c.begin(), c.end() n #define sz(a) int((a).size()) n #define pb push_back n #define mp make_pair n #define F first n #define S second n #define getUnique(c)  {sort(all(c));c.erase(unique(all(c)), c.end())} n #define MOD 1000000007 n #define PI 3.14159265358979323846 n #define INF 2000000000000000000 n  n /*TEMPLATES*/ n template <typename A> n ostream &operator<<(ostream &cout, vector<A> const &v) n { n     cout <<  [ ; n     for (int i = 0; i < sz(v); i++) n     { n         if (i) n             cout <<  ,  ; n         cout << v[i]; n     } n     return cout <<  ] ; n } n template <typename A, typename B> n ostream &operator<<(ostream &cout, pair<A, B> const &p) { return cout <<  (  << p.F <<  ,   << p.S <<  ) ; } n template <typename A, typename B> n istream &operator>>(istream &cin, pair<A, B> &p) n { n     cin >> p.F; n     return cin >> p.S; n } n  n /*NUMBER THEORY*/ n namespace number_theory n { n     ll gcd(ll x, ll y) n     { n         if (x == 0) n             return y; n         if (y == 0) n             return x; n         return gcd(y, x % y); n     } n  n     bool isPrime(ll n) n     { n         if (n <= 1) n             return false; n         if (n <= 3) n             return true; n  n         if (n % 2 == 0 || n % 3 == 0) n             return false; n  n         for (ll i = 5; i * i <= n; i += 6) n             if (n % i == 0 || n % (i + 2) == 0) n                 return false; n  n         return true; n     } n  n     bool prime[15000105]; n     void sieve(int n) n     { n         for (ll i = 0; i <= n; i++) n             prime[i] = 1; n         for (ll p = 2; p * p <= n; p++) n         { n             if (prime[p] == true) n             { n                 for (ll i = p * p; i <= n; i += p) n                     prime[i] = false; n             } n         } n         prime[1] = prime[0] = 0; n     } n     vll primelist; n     bool __primes_generated__ = 0; n  n     void genprimes(int n) n     { n         __primes_generated__ = 1; n         sieve(n + 1); n         for (ll i = 2; i <= n; i++) n             if (prime[i]) n                 primelist.push_back(i); n     } n  n     vll factors(ll n) n     { n         if (!__primes_generated__) n         { n             exit(1); n         } n         vll facs; n  n         for (ll i = 0; primelist[i] * primelist[i] <= n && i < primelist.size(); i++) n         { n             if (n % primelist[i] == 0) n             { n                 while (n % primelist[i] == 0) n                 { n                     n /= primelist[i]; n                     facs.pb(primelist[i]); n                 } n             } n         } n         if (n > 1) n         { n             facs.pb(n); n         } n         sort(all(facs)); n         return facs; n     } n }  n  n using namespace number_theory; n  n void kkvanonymous() n { n     ios_base::sync_with_stdio(0); n     cin.tie(0); n     cout.tie(0); n     genprimes(1.5e6); n  n     //#ifdef ONLINE_JUDGE n     //    freopen( input.txt ,  r , stdin); n     //    freopen( output.txt ,  w , stdout); n     //#endif n } n bool sortbysec(const pair<int,int> &a,  n               const pair<int,int> &b)  n {  n     return (a.second < b.second);  n } n  n void solve(){ n     ll a,b,n; n     cin>>a>>b>>n; n     vll p(n); n     vll h(n); n   n     ll m=-1; n     fll(i,0,n){ n         cin>>p[i]; n         m=max(m,p[i]); n     } n     fll(i,0,n){ n         cin>>h[i]; n     } n     fll(i,0,n){ n         ll t=ceil(double(h[i])/a); n         b-=p[i]*t; n     } n     if(b+m>0) cout<< YES n ; n     else cout<< NO n ; n  n } n  n int main() n { n #ifdef kkvanonymous_time n     auto begin = chrono::high_resolution_clock::now(); n #endif n     kkvanonymous(); n  n     int t = 1; n     cin>>t; n     while (t--) n         solve(); n  n #ifdef kkvanonymous_time n     auto end = chrono::high_resolution_clock::now(); n     cout << setprecision(4) << fixed; n     cout <<   nExecution time:   << chrono::duration_cast<std::chrono::duration<double>>(end - begin).count() <<   seconds  << endl; n #endif n     return 0; n }


#include <bits/stdc++.h> n long long int trie[10000007][2] = {0}; n long long int bin[40] = {0}, x = 0; n long long int a[100005] = {0}, prefix[100005] = {0}, suffix[100005] = {0}; n void tobinary(long long int x) { n   int cnt = 0; n   memset(bin, 0, sizeof(bin)); n   while (x > 0) { n     bin[cnt++] = (x % 2); n     x /= 2; n   } n } n void update(long long int node, long long int idx) { n   if (idx < 0) return; n   if (bin[idx] == 0) { n     if (trie[node][0] == -1) { n       trie[node][0] = ++x; n     } n     update(trie[node][0], idx - 1); n   } else { n     if (trie[node][1] == -1) { n       trie[node][1] = ++x; n     } n     update(trie[node][1], idx - 1); n   } n } n long long int query(long long int node, long long int idx) { n   if (idx < 0) return 0LL; n   long long int ret = 0; n   if (bin[idx] == 0) { n     if (trie[node][1] != -1) { n       ret += ((1LL) << idx); n       ret += query(trie[node][1], idx - 1); n     } else { n       ret += query(trie[node][0], idx - 1); n     } n   } else { n     if (trie[node][0] != -1) { n       ret += (1LL << idx); n       ret += query(trie[node][0], idx - 1); n     } else { n       ret += query(trie[node][1], idx - 1); n     } n   } n   return ret; n } n int main() { n   memset(trie, -1, sizeof(trie)); n   long long int n, i, ans = 0; n   scanf( %I64d , &n); n   for (i = 1; i <= n; i++) { n     scanf( %I64d , &a[i]); n     prefix[i] = a[i] ^ prefix[i - 1]; n   } n   for (i = n; i >= 1; i--) { n     suffix[i] = suffix[i + 1] ^ a[i]; n   } n   for (i = 0; i <= n; i++) { n     tobinary(prefix[i]); n     update(0, 39); n     tobinary(suffix[i + 1]); n     ans = (ans > query(0, 39) ? ans : query(0, 39)); n   } n   printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast,no-stack-protector,unroll-loops,fast-math ) n #pragma GCC target(   n      sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native ) n static int a[100001], n, m, l, r, x, cnt; n static bool type; n inline static int read() { n   register int x = 0; n   static char ch = getchar(); n   while (ch <  0  || ch >  9 ) ch = getchar(); n   while (ch >=  0  && ch <=  9 ) n     x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar(); n   return x; n } n signed main(void) { n   n = read(), m = read(); n   for (register int i = 1; i <= n; ++i) a[i] = read(); n   for (; m--;) { n     type = (read() == 1); n     l = read(), r = read(), x = read(); n     if (type) n       for (register int i = l; i <= r; ++i) a[i] -= (a[i] > x) ? x : 0; n     else { n       cnt = 0; n       for (register int i = l; i <= r; ++i) cnt += (a[i] == x); n       printf( %d n , cnt); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n; n vector<pair<int, int> > v; n long long dp[2][5001][51][51]; n const long long oo = 1e16; n int main() { n   cin >> n; n   v.resize(n); n   for (auto &x : v) cin >> x.first; n   int total = 0; n   for (auto &x : v) { n     cin >> x.second; n     total += x.second; n   } n   vector<int> bef(n, 1); n   sort(v.rbegin(), v.rend()); n   for (int i = 1; i < n; ++i) n     if (v[i - 1].first == v[i].first) bef[i] += bef[i - 1]; n   bef.push_back(1); n   v.push_back(make_pair(-1, -1)); n   for (int i = n; i >= 0; --i) { n     for (int rem = total; rem >= 0; --rem) { n       for (int g = 0; g <= i; ++g) { n         int me = bef[i]; n         for (int cg = 0; cg < me; ++cg) { n           long long ret = oo; n           if (i == n) { n             if (rem == 0) ret = 0; n           } else { n             const bool ch = v[i + 1].first != v[i].first; n             if (g > 0) { n               if (ch) n                 ret = dp[1 & ~i][rem][g - 1 + cg][0]; n               else n                 ret = dp[1 & ~i][rem][g - 1][cg]; n             } n             if (rem >= v[i].second) { n               if (ch) n                 ret = n                     min(ret, v[i].first + n                                  dp[1 & ~i][rem - v[i].second][g + cg + 1][0]); n               else n                 ret = min( n                     ret, v[i].first + dp[1 & ~i][rem - v[i].second][g][cg + 1]); n             } n           } n           dp[i & 1][rem][g][cg] = ret; n         } n       } n     } n   } n   pair<long long, long long> ans; n   long long res = -1; n   for (int i = 1; i <= total; ++i) { n     long long cur = dp[0][i][0][0]; n     if (cur == oo) continue; n     if (res == -1) { n       ans.first = cur; n       ans.second = i; n       res = 0; n     } else { n       if (ans.first * i <= cur * ans.second) continue; n       ans.first = cur; n       ans.second = i; n     } n   } n   res = ans.first * 10000; n   res = (res + ans.second - 1) / ans.second; n   int add = res % 10 > 0; n   res = res / 10 + add; n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     string s; n     cin >> s; n     int o = 0, t = 0, to = 0; n     int n = s.length(); n     for (int i = 0; i < n; i++) { n       if (s.substr(i, 3) ==  one ) { n         o++; n         i++; n       } else if (s.substr(i, 5) ==  twone ) { n         to++; n         i += 3; n       } else if (s.substr(i, 3) ==  two ) { n         t++; n         i++; n       } n     } n     cout << to + o + t << endl; n     for (int i = 0; i < n; i++) { n       if (s.substr(i, 3) ==  one ) { n         cout << i + 2 <<    ; n         i++; n       } else if (s.substr(i, 5) ==  twone ) { n         cout << i + 3 <<    ; n         i += 3; n       } else if (s.substr(i, 3) ==  two ) { n         cout << i + 2 <<    ; n         i++; n       } n     } n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n char t[200002]; n struct node { n   int nt[5][5]; n }; n node st[4 * 200000 + 2]; n node merge(node a, node b) { n   if (a.nt[0][0] == -1) return b; n   if (b.nt[0][0] == -1) return a; n   node ans; n   for (int i = 0; i <= 4; i++) n     for (int j = 0; j <= 4; j++) ans.nt[i][j] = -1e9; n   for (int i = 0; i <= 4; i++) { n     for (int j = i; j <= 4; j++) { n       ans.nt[i][j] = max(ans.nt[i][j], a.nt[i][j]); n       ans.nt[i][j] = max(ans.nt[i][j], b.nt[i][j]); n       for (int k = i; k <= j; k++) { n         ans.nt[i][j] = max(ans.nt[i][j], a.nt[i][k] + b.nt[k][j]); n       } n     } n   } n   return ans; n } n void build(int n, int s, int e) { n   if (s == e) { n     for (int i = 0; i <= 4; i++) n       for (int j = 0; j <= 4; j++) st[n].nt[i][j] = -1e9; n     for (int i = 0; i <= 4; i++) st[n].nt[i][i] = 1; n     if (t[s] ==  2 ) { n       st[n].nt[0][1] = 1; n       st[n].nt[0][0] = -1e9; n     } else if (t[s] ==  0 ) { n       st[n].nt[1][2] = 1; n       st[n].nt[1][1] = -1e9; n     } else if (t[s] ==  1 ) { n       st[n].nt[2][3] = 1; n       st[n].nt[2][2] = -1e9; n     } else if (t[s] ==  7 ) { n       st[n].nt[3][4] = 1; n       st[n].nt[3][3] = -1e9; n     } else if (t[s] ==  6 ) { n       st[n].nt[3][3] = -1e9; n       st[n].nt[4][4] = -1e9; n     } n   } else { n     int mid = (s + e) / 2; n     build(n + n, s, mid); n     build(n + n + 1, mid + 1, e); n     st[n] = merge(st[n + n], st[n + n + 1]); n   } n } n node query(int n, int s, int e, int l, int r) { n   if (s > r || l > e) { n     node ans; n     ans.nt[0][0] = -1; n     return ans; n   } n   if (l <= s && e <= r) return st[n]; n   int mid = (s + e) / 2; n   return merge(query(n + n, s, mid, l, r), query(n + n + 1, mid + 1, e, l, r)); n } n int main() { n   int n, q; n   scanf( %d%d , &n, &q); n   scanf( %s , t + 1); n   build(1, 1, n); n   for (int i = 1; i <= q; i++) { n     int l, r; n     scanf( %d%d , &l, &r); n     node ans = query(1, 1, n, l, r); n     if (ans.nt[0][4] < 0) n       printf( -1 n ); n     else n       printf( %d n , r - l + 1 - ans.nt[0][4]); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n, k, m, t; n   cin >> n >> k >> m >> t; n   while (t--) { n     int x, y; n     cin >> x >> y; n     if (x == 1) { n       if (y <= k) k++; n       n++; n     } else { n       if (y < k) n         k -= y, n -= y; n       else n         n = y; n     } n     cout << n <<     << k <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 1e9 + 7; n int dx[] = {1, -1, 0, 0, 1, 1, -1, -1}; n int dy[] = {0, 0, 1, -1, 1, -1, 1, -1}; n int expo(int x, int y) { n   int res = 1; n   x = x % M; n   while (y > 0) { n     if (y & 1) res = (1ll * res * x) % M; n     y = y >> 1; n     x = (1ll * x * x) % M; n   } n   return res; n } n const int N = 1e3 + 5; n int n, m, a[1000005]; n int dp[N][N]; n int rec(int s, int mod) { n   if (mod == 0) return 1; n   if (s >= n) return 0; n   if (dp[s][mod] != -1) return dp[s][mod]; n   dp[s][mod] = 0; n   dp[s][mod] |= rec(s + 1, (mod - a[s] + m) % m); n   dp[s][mod] |= rec(s + 1, mod); n   return dp[s][mod]; n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   int t = 1; n   while (t--) { n     cin >> n >> m; n     for (int i = 0; i < n; i++) { n       cin >> a[i]; n       a[i] %= m; n     } n     if (n > m) return cout <<  YES , 0; n     memset(dp, -1, sizeof dp); n     int chk = rec(0, m); n     if (chk) n       cout <<  YES ; n     else n       cout <<  NO ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n constexpr int MOD = 998244353; n int main() { n   int R, C; n   cin >> R >> C; n   int X = 1; n   while (R--) X = (2 * X) % MOD; n   while (C--) X = (2 * X) % MOD; n   cout << X << endl; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename Arg1> n void __f(const char* name, Arg1&& arg1) { n   cerr << name <<   :   << arg1 << std::endl; n } n template <typename Arg1, typename... Args> n void __f(const char* names, Arg1&& arg1, Args&&... args) { n   const char* comma = strchr(names + 1,  , ); n   cerr.write(names, comma - names) <<   :   << arg1 <<   |  ; n   __f(comma + 1, args...); n } n int a[1003][1003]; n int main() { n   int n; n   long long int cnt = 0; n   cin >> n; n   for (int i = 0; i < n; i++) { n     int x, y; n     cin >> x >> y; n     a[x][y] = 1; n   } n   long long int p, q, fl; n   for (int i = 0; i <= 1000; i++) { n     p = 0; n     q = i; n     fl = 0; n     while (p <= 1000 and q <= 1000) { n       if (a[p][q]) fl++; n       p++; n       q++; n     } n     cnt += (fl * (fl - 1)) / 2; n     if (i) { n       p = i; n       q = 0; n       fl = 0; n       while (p <= 1000 and q <= 1000) { n         if (a[p][q]) fl++; n         p++; n         q++; n       } n       cnt += (fl * (fl - 1)) / 2; n     } n     p = 0; n     q = i; n     fl = 0; n     while (p <= 1000 and q >= 0) { n       if (a[p][q]) fl++; n       p++; n       q--; n     } n     cnt += (fl * (fl - 1)) / 2; n     if (i) { n       p = i; n       q = 1000; n       fl = 0; n       while (p <= 1000 and q >= 0) { n         if (a[p][q]) fl++; n         p++; n         q--; n       } n       cnt += (fl * (fl - 1)) / 2; n     } n   } n   cout << cnt << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n, m; n   cin >> n >> m; n   vector<vector<pair<int, int>>> g(n); n   vector<int> goes(m, -1); n   vector<int> offset(m); n   vector<int> from(m); n   vector<int> to(m); n   for (int i = 0; i < m; ++i) { n     int v, u; n     cin >> v >> u; n     from[i] = --v; n     to[i] = --u; n     g[v].emplace_back(u, i); n     g[u].emplace_back(v, i); n   } n   vector<int> pr(n, -1); n   vector<int> dfn(n, -1); n   vector<int> low(n, -1); n   vector<int> val(n, -1); n   int tt = 0; n   function<void(int)> tarjan = [&](int v) { n     dfn[v] = low[v] = tt++; n     for (auto e : g[v]) { n       int u = e.first; n       int w = e.second; n       if (u != pr[v]) { n         if (dfn[u] == -1) { n           pr[u] = v; n           val[u] = w; n           tarjan(u); n           low[v] = min(low[v], low[u]); n         } else { n           low[v] = min(low[v], dfn[u]); n         } n       } n     } n     for (auto e : g[v]) { n       int u = e.first; n       int w = e.second; n       if (pr[u] != v && dfn[v] < dfn[u]) { n         vector<int> cycle; n         cycle.push_back(w); n         for (int t = u; t != v; t = pr[t]) { n           cycle.push_back(val[t]); n         } n         rotate(cycle.begin(), min_element(cycle.begin(), cycle.end()), n                cycle.end()); n         int p = max_element(cycle.begin(), cycle.end()) - cycle.begin(); n         bool valid = true; n         for (int i = 0; i < p - 1; ++i) { n           if (cycle[i] > cycle[i + 1]) { n             valid = false; n             break; n           } n         } n         for (int i = p; i < (int)cycle.size() - 1; ++i) { n           if (cycle[i] < cycle[i + 1]) { n             valid = false; n             break; n           } n         } n         if (valid) { n           goes[cycle[p]] = cycle[0]; n         } n       } n     } n   }; n   tarjan(0); n   vector<int> dp(n, 1); n   for (int i = m - 1; ~i; --i) { n     int v = from[i]; n     int u = to[i]; n     int w = dp[v] + dp[u] - offset[i]; n     dp[v] = dp[u] = w; n     if (goes[i] != -1) { n       offset[goes[i]] = w; n     } n   } n   for (int i = 0; i < n; ++i) { n     if (i) { n       cout <<    ; n     } n     cout << dp[i] - 1; n   } n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = 3 * 1e18; n long long mi[5], ma[5], low[5], high[5]; n long long resx, resy, resz; n long long div2(long long x, long long y) { n   if (x > 0 && y > 0 || x < 0 && y < 0) { n     long long f = 0; n     if (x % 2 && y % 2) f = 1; n     if (x > 0 && y > 0) n       return x / 2 + y / 2 + f; n     else n       return x / 2 + y / 2 - f; n   } else n     return (x + y) / 2; n } n void update(long long s, long long d) { n   resy = div2(s, d); n   resz = div2(s, -d); n } n bool check(long long x) { n   for (int i = 1; i <= 4; i++) { n     low[i] = ma[i] - x; n     high[i] = mi[i] + x; n     if (low[i] > high[i]) return false; n   } n   long long x1 = div2(low[1], -high[2]); n   long long y1 = div2(high[1], -low[2]); n   long long x2 = div2(low[3], -high[4]); n   long long y2 = div2(high[3], -low[4]); n   long long x3 = max(x1, x2); n   long long y3 = min(y1, y2); n   if (x3 > y3) return false; n   for (long long i = x3; i <= y3; i++) { n     resx = i; n     long long s1 = max(low[1] - i, low[2] + i); n     long long s2 = min(high[1] - i, high[2] + i); n     long long d1 = max(low[3] - i, low[4] + i); n     long long d2 = min(high[3] - i, high[4] + i); n     if (s1 > s2 || d1 > d2) continue; n     if (s1 < s2) { n       if ((s1 & 1) == (d1 & 1)) n         update(s1, d1); n       else n         update(s1 + 1, d1); n       return true; n     } else if (d1 < d2) { n       if ((s1 & 1) == (d1 & 1)) n         update(s1, d1); n       else n         update(s1, d1 + 1); n       return true; n     } else { n       if ((s1 & 1) == (d1 & 1)) { n         update(s1, d1); n         return true; n       } n     } n   } n   return false; n } n int main() { n   int T; n   cin >> T; n   while (T--) { n     int n; n     scanf( %d , &n); n     for (int i = 1; i <= 4; i++) { n       mi[i] = inf; n       ma[i] = -inf; n     } n     for (int i = 0; i < n; i++) { n       long long x, y, z; n       scanf( %I64d%I64d%I64d , &x, &y, &z); n       mi[1] = min(mi[1], x + y + z); n       ma[1] = max(ma[1], x + y + z); n       mi[2] = min(mi[2], -x + y + z); n       ma[2] = max(ma[2], -x + y + z); n       mi[3] = min(mi[3], x + y - z); n       ma[3] = max(ma[3], x + y - z); n       mi[4] = min(mi[4], -x + y - z); n       ma[4] = max(ma[4], -x + y - z); n     } n     long long L = -1, R = inf; n     while (R - L > 1) { n       long long M = (L + R) >> 1; n       if (check(M)) n         R = M; n       else n         L = M; n     } n     check(R); n     printf( %I64d %I64d %I64d n , resx, resy, resz); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = (int)1e9 + 7; n const int FFTMOD = 119 << 23 | 1; n const int INF = (int)1e9 + 23111992; n const long long LINF = (long long)1e18 + 23111992; n const long double PI = acos((long double)-1); n const long double EPS = 1e-9; n inline long long gcd(long long a, long long b) { n   long long r; n   while (b) { n     r = a % b; n     a = b; n     b = r; n   } n   return a; n } n inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } n inline long long fpow(long long n, long long k, int p = MOD) { n   long long r = 1; n   for (; k; k >>= 1) { n     if (k & 1) r = r * n % p; n     n = n * n % p; n   } n   return r; n } n template <class T> n inline int chkmin(T& a, const T& val) { n   return val < a ? a = val, 1 : 0; n } n template <class T> n inline int chkmax(T& a, const T& val) { n   return a < val ? a = val, 1 : 0; n } n inline unsigned long long isqrt(unsigned long long k) { n   unsigned long long r = sqrt(k) + 1; n   while (r * r > k) r--; n   return r; n } n inline long long icbrt(long long k) { n   long long r = cbrt(k) + 1; n   while (r * r * r > k) r--; n   return r; n } n inline void addmod(int& a, int val, int p = MOD) { n   if ((a = (a + val)) >= p) a -= p; n } n inline void submod(int& a, int val, int p = MOD) { n   if ((a = (a - val)) < 0) a += p; n } n inline int mult(int a, int b, int p = MOD) { return (long long)a * b % p; } n inline int inv(int a, int p = MOD) { return fpow(a, p - 2, p); } n inline int sign(long double x) { return x < -EPS ? -1 : x > +EPS; } n inline int sign(long double x, long double y) { return sign(x - y); } n mt19937 mt(chrono::high_resolution_clock::now().time_since_epoch().count()); n inline int myrand() { return abs((int)mt()); } n struct KMP { n   string s; n   int n; n   vector<int> link; n   void build(char* str) { n     s = str, n = (int)s.size(); n     link.resize(n); n     int cur = link[0] = -1; n     for (int i = 1; i < n; i++) { n       while (cur >= 0 && s[i] != s[cur + 1]) cur = link[cur]; n       if (s[i] == s[cur + 1]) cur++; n       link[i] = cur; n     } n   } n   vector<int> query(char* t) { n     vector<int> res; n     int k = strlen(t); n     int cur = -1; n     for (int i = 0; i < k; i++) { n       while (cur >= 0 && t[i] != s[cur + 1]) cur = link[cur]; n       if (t[i] == s[cur + 1]) cur++; n       if (cur == n - 1) { n         res.push_back(i - n + 1); n         cur = link[cur]; n       } n     } n     return res; n   } n } kmp; n const int maxn = 1e5 + 5; n const int logn = 20; n int pr, base; n int pr2, base2; n int n, q; n vector<pair<int, char> > adj[maxn]; n int u[maxn]; n int v[maxn]; n string s[maxn]; n int lev[maxn]; n int par[logn][maxn]; n pair<int, int> g[maxn]; n pair<int, int> gi[maxn]; n vector<pair<int, int> > vals; n int res[maxn]; n vector<pair<int, pair<int, int> > > que[maxn]; n char from[maxn]; n void dfs(int u, int dad) { n   for (int i = (1); i < (logn); ++i) par[i][u] = par[i - 1][par[i - 1][u]]; n   for (auto e : adj[u]) { n     int v = e.first; n     char c = e.second; n     if (v != dad) { n       lev[v] = lev[u] + 1; n       par[0][v] = u; n       from[v] = c; n       dfs(v, u); n     } n   } n } n int lca(int u, int v) { n   if (lev[u] < lev[v]) swap(u, v); n   for (int i = (logn)-1; i >= (0); --i) { n     if (lev[par[i][u]] >= lev[v]) { n       u = par[i][u]; n     } n   } n   if (u == v) return u; n   for (int i = (logn)-1; i >= (0); --i) { n     if (par[i][u] != par[i][v]) { n       u = par[i][u]; n       v = par[i][v]; n     } n   } n   return par[0][u]; n } n int jump(int u, int k) { n   for (int i = (logn)-1; i >= (0); --i) { n     if ((1 << i) <= k) { n       k -= 1 << i; n       u = par[i][u]; n     } n   } n   return u; n } n int cnt[maxn * 2]; n void upd_dfs(int u, int dad) { n   int w = u, rt = 0; n   pair<int, int> hs = make_pair(0, 0); n   for (int i = (0); i < (min(100, lev[u])); ++i) { n     char c = from[w]; n     hs.first = mult(hs.first, base, pr); n     addmod(hs.first, c, pr); n     hs.second = mult(hs.second, base2, pr2); n     addmod(hs.second, c, pr2); n     int k = lower_bound((vals).begin(), (vals).end(), hs) - vals.begin(); n     if (k < int((vals).size()) && vals[k] == hs) { n       cnt[k]++; n     } n     w = par[0][w]; n   } n   for (auto it : que[u]) { n     int ix = it.first; n     int u = it.second.first; n     int s = it.second.second; n     res[ix] += cnt[u] * s; n   } n   for (auto e : adj[u]) { n     int v = e.first; n     if (v != dad) { n       upd_dfs(v, u); n     } n   } n   w = u, rt = 0; n   hs = make_pair(0, 0); n   for (int i = (0); i < (min(100, lev[u])); ++i) { n     char c = from[w]; n     hs.first = mult(hs.first, base, pr); n     addmod(hs.first, c, pr); n     hs.second = mult(hs.second, base2, pr2); n     addmod(hs.second, c, pr2); n     int k = lower_bound((vals).begin(), (vals).end(), hs) - vals.begin(); n     if (k < int((vals).size()) && vals[k] == hs) { n       cnt[k]--; n     } n     w = par[0][w]; n   } n } n int ispr(int k) { n   for (int i = 2; i * i <= k; i++) { n     if (k % i == 0) { n       return 0; n     } n   } n   return 1; n } n void chemthan() { n   pr = INF + myrand() % (INF / 100); n   while (!ispr(pr)) pr++; n   base = 1234 + myrand() % 1234; n   pr2 = INF + myrand() % (INF / 100); n   while (!ispr(pr2)) pr2++; n   base2 = 1234 + myrand() % 1234; n   cin >> n; n   for (int i = (0); i < (n - 1); ++i) { n     int u, v; n     char c; n     cin >> u >> v >> c; n     u--, v--; n     adj[u].push_back(make_pair(v, c)); n     adj[v].push_back(make_pair(u, c)); n   } n   cin >> q; n   for (int i = (0); i < (q); ++i) { n     cin >> u[i] >> v[i] >> s[i], u[i]--, v[i]--; n     reverse((s[i]).begin(), (s[i]).end()); n     for (char c : s[i]) { n       g[i].first = mult(g[i].first, base, pr); n       addmod(g[i].first, c, pr); n       g[i].second = mult(g[i].second, base2, pr2); n       addmod(g[i].second, c, pr2); n     } n     reverse((s[i]).begin(), (s[i]).end()); n     for (char c : s[i]) { n       gi[i].first = mult(gi[i].first, base, pr); n       addmod(gi[i].first, c, pr); n       gi[i].second = mult(gi[i].second, base2, pr2); n       addmod(gi[i].second, c, pr2); n     } n     vals.push_back(g[i]), vals.push_back(gi[i]); n   } n   sort((vals).begin(), (vals).end()), n       (vals).erase(unique((vals).begin(), (vals).end()), (vals).end()); n   static int h[maxn]; n   static int hi[maxn]; n   for (int i = (0); i < (q); ++i) n     h[i] = lower_bound((vals).begin(), (vals).end(), g[i]) - vals.begin(); n   for (int i = (0); i < (q); ++i) n     hi[i] = lower_bound((vals).begin(), (vals).end(), gi[i]) - vals.begin(); n   dfs(0, -1); n   for (int i = (0); i < (q); ++i) { n     if (u[i] == v[i]) { n       continue; n     } n     int w = lca(u[i], v[i]); n     int iter = 0; n     for (int t : {u[i], v[i]}) { n       if (lev[t] - lev[w] >= int((s[i]).size())) { n         int k = lev[t] - (lev[w] + int((s[i]).size()) - 1); n         int nt = jump(t, k); n         if (!iter) { n           que[t].push_back(make_pair(i, make_pair(hi[i], 1))); n           que[nt].push_back(make_pair(i, make_pair(hi[i], -1))); n         } else { n           que[t].push_back(make_pair(i, make_pair(h[i], 1))); n           que[nt].push_back(make_pair(i, make_pair(h[i], -1))); n         } n       } n       iter++; n     } n     if (w != u[i] && w != v[i]) { n       int du = lev[u[i]] - lev[w]; n       int dv = lev[v[i]] - lev[w]; n       int mu = min(du, int((s[i]).size()) - 1); n       int mv = min(dv, int((s[i]).size()) - 1); n       int su = jump(u[i], du - mu); n       int sv = jump(v[i], dv - mv); n       string tu; n       while (su != w) { n         char c = from[su]; n         tu += c; n         su = par[0][su]; n       } n       string tv; n       while (sv != w) { n         char c = from[sv]; n         tv += c; n         sv = par[0][sv]; n       } n       reverse((tv).begin(), (tv).end()); n       string tt = tu + tv; n       kmp.build((char*)s[i].data()); n       res[i] += int((kmp.query((char*)tt.data())).size()); n     } n   } n   upd_dfs(0, -1); n   for (int i = (0); i < (q); ++i) cout << res[i] <<   n ; n } n int main(int argc, char* argv[]) { n   ios_base::sync_with_stdio(0), cin.tie(0); n   if (argc > 1) { n     assert(freopen(argv[1],  r , stdin)); n   } n   if (argc > 2) { n     assert(freopen(argv[2],  wb , stdout)); n   } n   chemthan(); n   cerr <<   nTime elapsed:   << 1000 * clock() / CLOCKS_PER_SEC <<  ms n ; n   return 0; n } n