#include <bits/stdc++.h> n int main() { n   int n, m, z, l, k, i = 1, j = 1, p = 0, q = 0, count = 0; n   scanf( %d%d%d , &n, &m, &z); n   int a[10000], b[10000]; n   while ((i * n) <= z) { n     a[p] = i * n; n     i++; n     p++; n   } n   while ((j * m) <= z) { n     b[q] = j * m; n     j++; n     q++; n   } n   for (k = 0; k < p; k++) { n     for (l = 0; l < q; l++) { n       if (a[k] == b[l]) count++; n     } n   } n   printf( %d n , count); n   return 0; n } n


#include <bits/stdc++.h> n int cmp(const void *a, const void *b) { return *(int *)a - *(int *)b; } n int main() { n   int a[55], b[55], m, i, j, map[55], n, x, y, s; n   while (~scanf( %d , &m)) { n     for (i = 0; i < m - 1; i++) { n       scanf( %d , &a[i]); n     } n     for (i = 0; i < m - 1; i++) { n       scanf( %d , &b[i]); n     } n     j = 0; n     for (i = 0; i < m; i++) { n       scanf( %d , &s); n       for (x = i; x < m - 1; x++) { n         s += b[x]; n       } n       for (y = 0; y < i; y++) { n         s += a[y]; n       } n       map[j++] = s; n     } n     qsort(map, j, sizeof(map[0]), cmp); n     printf( %d n , map[0] + map[1]); n   } n } n


#include <bits/stdc++.h> n using namespace std; n string to_string(string s) { return     + s +    ; } n template <typename T> n string to_string(vector<T>); n template <typename U, typename V> n string to_string(pair<U, V> p) { n   return  (  + to_string(p.first) +  ,   + to_string(p.second) +  ) ; n } n template <typename T> n string to_string(vector<T> v) { n   string r =  [ ; n   for (int i = 0; i < (int)v.size() - 1; i++) r += to_string(v[i]) +  ,  ; n   if (!v.empty()) r += to_string(v.back()); n   return r +  ] ; n } n int main() { n   int a, b, c, d, k, tc; n   scanf( %d , &tc); n   while (tc--) { n     scanf( %d%d%d%d%d , &a, &b, &c, &d, &k); n     int x = (a - 1) / c + 1; n     int y = (b - 1) / d + 1; n     if (x + y > k) n       printf( -1 n ); n     else n       printf( %d %d n , x, y); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, a; n vector<int> v1, v2; n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a; n     v1.push_back(a); n     v2.push_back(a); n   } n   sort(v2.begin(), v2.end()); n   a = 0; n   for (int i = 0; i < n; i++) { n     a += v1[i] != v2[i]; n   } n   (a < 3) ? cout <<  YES  << endl : cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 7, M = 20, MOD = 1000 * 1000 * 1000 + 7; n int sum(int a, int b) { n   a += b; n   if (a < 0) n     a += MOD; n   else if (a >= MOD) n     a -= MOD; n   return a; n } n void _sum(int &a, int b) { a = sum(a, b); } n int mul(int a, int b) { return 1LL * a * b % MOD; } n void _mul(int &a, int b) { a = mul(a, b); } n int power(int a, int b) { n   int res = 1; n   while (b) { n     if (b & 1) _mul(res, a); n     _mul(a, a); n     b >>= 1; n   } n   return res; n } n int inv(int x) { return power(x, MOD - 2); } n int n, f[1 << N], p[N][N], dp[N][1 << M], pro[N][1 << N]; n vector<int> vec[N]; n void input() { n   cin >> n; n   for (int i = 1; i <= n; i++) { n     for (int j = 0; j < n; j++) { n       cin >> p[i][j]; n       _mul(p[i][j], inv(100)); n     } n     for (int mask = 0; mask < (1 << n); mask++) { n       pro[i][mask] = 1; n       for (int j = 0; j < n; j++) n         if (mask & (1 << j)) n           _mul(pro[i][mask], p[i][j]); n         else n           _mul(pro[i][mask], sum(1, -p[i][j])); n     } n   } n } n void pre_pro() { n   for (int i = 0; i < (1 << n); i++) { n     f[i] = vec[__builtin_popcount(i)].size(); n     vec[__builtin_popcount(i)].push_back(i); n   } n } n void act(int i, int mask, int nei) { n   int nxt = 0; n   vector<int> a, b; n   for (int j = 0; j < vec[i].size(); j++) n     if (mask & (1 << j)) a.push_back(vec[i][j]); n   for (int j = 0; j < n; j++) n     if (nei & (1 << j)) b.push_back(j); n   for (int x : a) n     for (int y : b) n       if ((x & (1 << y)) == false) nxt |= (1 << f[x | (1 << y)]); n   _sum(dp[i + 1][nxt], mul(dp[i][mask], pro[i + 1][nei])); n } n void solve() { n   dp[0][1] = 1; n   for (int i = 0; i < n; i++) n     for (int mask = 0; mask < (1 << vec[i].size()); mask++) n       if (dp[i][mask]) n         for (int nei = 0; nei < (1 << n); nei++) act(i, mask, nei); n } n int main() { n   ios::sync_with_stdio(false), cin.tie(0); n   input(); n   pre_pro(); n   solve(); n   cout << dp[n][1]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e9 + 23; n const int MOD = 1e9 + 7; n const int N = 1000 + 10; n int brow[N], bcol[N]; n int main() { n   ios_base::sync_with_stdio(0); n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < (m); ++i) { n     int x, y; n     cin >> x >> y; n     brow[x] = true; n     bcol[y] = true; n   } n   int ans = 0; n   for (int i = (2); i < (n); ++i) { n     if (i == n - i + 1) n       ans += !brow[i] || !bcol[i]; n     else { n       ans += !brow[i]; n       ans += !bcol[i]; n     } n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int dp[100002]; n int f(int x) { n   if (dp[x] != -1) return dp[x]; n   bool nim[100] = {}; n   for (int i = 2; i * (i - 1) < 2 * x; i++) { n     if ((2 * x - i * (i - 1)) % (2 * i) == 0) { n       int tmp = 0, xx = (2 * x - i * (i - 1)) / (2 * i); n       for (int j = 0; j < i; j++) tmp ^= f(xx + j); n       nim[tmp] = 1; n       if (tmp == 0 && x == n) { n         printf( %d n , i); n         return 1; n       } n     } n   } n   int ret = 0; n   while (nim[ret]) ret++; n   return dp[x] = ret; n } n int main() { n   scanf( %d , &n); n   memset(dp, -1, sizeof dp); n   if (f(n)) n     ; n   else n     printf( -1 n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int test; n   scanf( %d , &(test)); n   for (int t = 1; t <= test; t++) { n     int n; n     scanf( %d , &(n)); n     int a[200], cnt = 0; n     int sum = 0; n     for (int i = 0; i < n; i++) { n       scanf( %d , &(a[i])); n       if (a[i] == 0) { n         a[i] = 1; n         cnt++; n       } n       sum = sum + a[i]; n     } n     if (sum != 0) n       cout << cnt << endl; n     else { n       cout << cnt + 1 << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MX = 500005; n const long long MOD = 1000000007; n void setIO(string name) { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   freopen((name +  .in ).c_str(),  r , stdin); n   freopen((name +  .out ).c_str(),  w , stdout); n } n int t; n int n; n void solve() { n   cin >> n; n   long long x = 0, even = 0, odd = 0; n   long long sum = 0, improv = 0; n   for (int i = 0; i < (n); i++) { n     int a; n     cin >> a; n     if (i % 2 == 0) { n       x -= a; n       improv = max(improv, x - even); n       even = min(x, even); n       sum += a; n     } n     if (i % 2 == 1) { n       x += a; n       improv = max(improv, x - odd); n       odd = min(x, odd); n     } n   } n   cout << sum + improv << endl; n } n int main() { n   cin >> t; n   for (int i = 0; i < (t); i++) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long n, sol = 0, modul = 1000000007; n vector<long long> parent, depth; n long long where(long long pos) { n   if (parent[pos] == -1) return pos; n   long long a = depth[pos]; n   long long b = parent[pos]; n   long long c = where(parent[pos]); n   parent[pos] = c; n   depth[pos] = (a + depth[b]) % modul; n   return c; n } n void uniona(long long a, long long x, long long b) { n   long long wa = where(a); n   if (wa == b) return; n   sol = (sol + x + depth[a]) % modul; n   depth[wa] = (x + depth[a]) % modul; n   parent[wa] = b; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cin >> n; n   parent.assign(n, -1); n   depth.assign(n, 0); n   for (long long i = 0; i < n; i++) { n     long long k; n     cin >> k; n     for (long long j = 0; j < k; j++) { n       long long a, b; n       cin >> a >> b; n       a--; n       uniona(a, b, i); n     } n   } n   cout << (sol + modul) % modul << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   long long ans = 0; n   for (int i = (0); i < (n); ++i) { n     long long t, T, x, cost; n     cin >> t >> T >> x >> cost; n     if (t >= T) { n       ans += cost + m * x; n       continue; n     } n     long long aux1 = cost; n     if (m > (T - t)) aux1 += m * x; n     long long aux2 = (long long)ceil((double)(m - (T - t)) / (T - t)) + 1; n     aux2 *= cost; n     ans += min(aux1, aux2); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1000000007; n const int N = 3e5 + 5; n const int INF = 1e9 + 7; n vector<int> graph[N]; n multiset<int, greater<int>> st; n int n, a, b, dat[N], ans, cur; n void rem(int val) { n   auto it = st.find(val); n   st.erase(it); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n   ans = INF; n   cin >> n; n   for (int i = (0); i < (n); i++) { n     cin >> dat[i]; n     st.insert(dat[i]); n   } n   for (int i = (0); i < (n - 1); i++) { n     cin >> a >> b; n     --a; n     --b; n     graph[a].push_back(b); n     graph[b].push_back(a); n   } n   for (int i = (0); i < (n); i++) { n     rem(dat[i]); n     for (auto it : graph[i]) rem(dat[it]); n     cur = -INF; n     cur = max(cur, dat[i]); n     if (!st.empty()) cur = max(cur, *st.begin() + 2); n     st.insert(dat[i]); n     for (auto it : graph[i]) { n       st.insert(dat[it]); n       cur = max(cur, dat[it] + 1); n     } n     ans = min(ans, cur); n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, k; n bool fort[100007]; n vector<int> v[100007]; n struct tuhla { n   int vertex; n   double val; n }; n double f[100007]; n bool used[100007]; n double bestval; n int id; n bool operator<(struct tuhla p1, struct tuhla p2) { return (p1.val > p2.val); } n priority_queue<struct tuhla> q; n void calc() { n   int i, j; n   for (i = 1; i <= n; i++) { n     if (fort[i] == true) { n       used[i] = true; n     } else { n       used[i] = false; n     } n   } n   for (i = 1; i <= n; i++) { n     if (fort[i] == true) { n       continue; n     } n     int sz = v[i].size(); n     double br = 0.00000; n     for (j = 0; j < sz; j++) { n       if (fort[v[i][j]] == false) { n         br += 1.000000; n       } n     } n     f[i] = (br / sz); n     struct tuhla u; n     u.vertex = i; n     u.val = f[i]; n     q.push(u); n   } n   bestval = -1.00000000; n   id = -1; n   int tot = 0; n   while (q.empty() == false) { n     struct tuhla cur = q.top(); n     q.pop(); n     if (fort[cur.vertex] == true || used[cur.vertex] == true) { n       continue; n     } n     if (cur.val > f[cur.vertex] + 0.0000001) { n       continue; n     } n     if (cur.val < f[cur.vertex] - 0.0000001) { n       continue; n     } n     if (bestval < cur.val) { n       bestval = cur.val; n       id = tot; n     } n     tot++; n     used[cur.vertex] = true; n     int sz = v[cur.vertex].size(); n     for (i = 0; i < sz; i++) { n       if (used[v[cur.vertex][i]] == false) { n         struct tuhla u; n         f[v[cur.vertex][i]] = n             f[v[cur.vertex][i]] - (1.00000 / (v[v[cur.vertex][i]].size())); n         u.vertex = v[cur.vertex][i]; n         u.val = f[v[cur.vertex][i]]; n         q.push(u); n       } n     } n   } n } n void recreate(int br) { n   int i, j; n   for (i = 1; i <= n; i++) { n     if (fort[i] == true) { n       used[i] = true; n     } else { n       used[i] = false; n     } n   } n   for (i = 1; i <= n; i++) { n     int sz = v[i].size(); n     double br = 0.00000; n     for (j = 0; j < sz; j++) { n       if (fort[v[i][j]] == false) { n         br += 1.000000; n       } n     } n     f[i] = (br / sz); n     struct tuhla u; n     u.vertex = i; n     u.val = f[i]; n     q.push(u); n   } n   int tot = 0; n   while (q.empty() == false) { n     struct tuhla cur = q.top(); n     q.pop(); n     if (fort[cur.vertex] == true || used[cur.vertex] == true) { n       continue; n     } n     if (cur.val > f[cur.vertex] + 0.0000001) { n       continue; n     } n     if (cur.val < f[cur.vertex] - 0.0000001) { n       continue; n     } n     if (br == 0) { n       break; n     } n     br--; n     used[cur.vertex] = true; n     int sz = v[cur.vertex].size(); n     for (i = 0; i < sz; i++) { n       if (used[v[cur.vertex][i]] == false) { n         struct tuhla u; n         f[v[cur.vertex][i]] = n             f[v[cur.vertex][i]] - (1.00000 / (v[v[cur.vertex][i]].size())); n         u.vertex = v[cur.vertex][i]; n         u.val = f[v[cur.vertex][i]]; n         q.push(u); n       } n     } n   } n } n void input() { n   scanf( %d%d%d , &n, &m, &k); n   int i; n   for (i = 1; i <= k; i++) { n     int x; n     scanf( %d , &x); n     fort[x] = true; n   } n   for (i = 1; i <= m; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     v[x].push_back(y); n     v[y].push_back(x); n   } n } n void solve() { n   calc(); n   recreate(id); n   vector<int> ret; n   int i; n   for (i = 1; i <= n; i++) { n     if (used[i] == false) { n       ret.push_back(i); n     } n   } n   int sz = ret.size(); n   printf( %d n , sz); n   for (i = 0; i < sz; i++) { n     printf( %d , ret[i]); n     if (i == sz - 1) { n       printf(  n ); n     } else { n       printf(   ); n     } n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   input(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int k; n   cin >> k; n   int x; n   cin >> x; n   int* a; n   a = new int[n + 1]; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   int index; n   index = n; n   while (k > 0) { n     a[index] = x; n     index--; n     k--; n   } n   int sum; n   sum = 0; n   for (int i = 1; i <= n; i++) { n     sum = sum + a[i]; n   } n   cout << sum; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int n; n   cin >> n; n   int a[n]; n   for (int i = 0; i < n; i++) cin >> a[i]; n   vector<int> V; n   int px = 0; n   while (px < n) { n     if (a[px] == 1) { n       if (px < n && a[px + 1] == 1) { n         px += 2; n         continue; n       } n       V.push_back(px); n     } n     px++; n   } n   px = 0; n   cout << (n - V.size()) <<   n ; n   for (int i = 0; i < n; i++) { n     while (px < (V.size()) && V[px] < i) px++; n     if (px < (V.size()) && V[px] == i) continue; n     cout << a[i] <<    ; n   } n   cout <<   n ; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   while (t) { n     t--; n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   long long a[4]; n   for (long long i = 0; i < 4; i++) cin >> a[i]; n   sort(a, a + 4); n   if ((a[3] + a[0] == a[2] + a[1]) || (a[3] == a[0] + a[1] + a[2])) n     cout <<  YES  << endl; n   else n     cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string str[51], temp; n int main() { n   int t, f, a; n   int cnt = 0, ans = 0x3f3f3f3f; n   scanf( %d , &t); n   getchar(); n   for (int i = 0; i < t; i++) { n     cin >> str[i]; n   } n   for (int i = 0; i < t; i++) { n     cnt = 0; n     for (int j = 0; j < t; j++) { n       temp = str[j] + str[j]; n       if (temp.find(str[i]) == string::npos) { n         cout << -1 << endl; n         return 0; n       } n       cnt += temp.find(str[i]); n     } n     ans = min(ans, cnt); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int Maxn = 2e5 + 5; n vector<int> adj[Maxn]; n int sz[Maxn]; n int n; n long long ret, ans; n void dfs_init(int u, int f) { n   sz[u] = 1; n   for (int i = 0; i < adj[u].size(); i++) { n     int v = adj[u][i]; n     if (v == f) continue; n     dfs_init(v, u); n     sz[u] += sz[v]; n   } n   ret += sz[u]; n } n void dfs(int u, int f, long long ret) { n   ans = max(ans, ret); n   for (int i = 0; i < adj[u].size(); i++) { n     int v = adj[u][i]; n     if (v == f) continue; n     dfs(v, u, ret + n - 2 * sz[v]); n   } n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i < n; 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   ans = ret = 0; n   dfs_init(1, 0); n   dfs(1, 0, ret); n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 7; n long long int sum[N << 2][11], sum2[11], a[N]; n int lazy[N << 2][11], vis[11]; n void pushdown(int rt) { n   for (int i = 0; i < 10; i++) n     vis[i] = lazy[rt << 1][i], sum2[i] = sum[rt << 1][i]; n   for (int i = 0; i < 10; i++) n     if (lazy[rt][i] != i) { n       for (int j = 0; j < 10; j++) n         if (lazy[rt << 1][j] == i) vis[j] = lazy[rt][i]; n       sum2[lazy[rt][i]] += sum[rt << 1][i]; n       sum2[i] -= sum[rt << 1][i]; n     } n   for (int i = 0; i < 10; i++) n     sum[rt << 1][i] = sum2[i], lazy[rt << 1][i] = vis[i]; n   for (int i = 0; i < 10; i++) n     vis[i] = lazy[rt << 1 | 1][i], sum2[i] = sum[rt << 1 | 1][i]; n   for (int i = 0; i < 10; i++) n     if (lazy[rt][i] != i) { n       for (int j = 0; j < 10; j++) n         if (lazy[rt << 1 | 1][j] == i) vis[j] = lazy[rt][i]; n       sum2[lazy[rt][i]] += sum[rt << 1 | 1][i]; n       sum2[i] -= sum[rt << 1 | 1][i]; n     } n   for (int i = 0; i < 10; i++) n     sum[rt << 1 | 1][i] = sum2[i], lazy[rt << 1 | 1][i] = vis[i]; n   for (int i = 0; i < 10; i++) lazy[rt][i] = i; n } n void pushup(int rt) { n   for (int i = 0; i < 10; i++) n     sum[rt][i] = sum[rt << 1][i] + sum[rt << 1 | 1][i]; n } n void build(int rt, int l, int r) { n   for (int i = 0; i < 10; i++) sum[rt][i] = 0; n   for (int i = 0; i < 10; i++) lazy[rt][i] = i; n   if (l == r) { n     for (long long int t = 1; a[l]; a[l] /= 10, t *= 10) n       sum[rt][a[l] % 10] += t; n     return; n   } n   int m = r + l >> 1; n   build(rt << 1, l, m); n   build(rt << 1 | 1, m + 1, r); n   pushup(rt); n } n void update(int rt, int l, int r, int L, int R, int x, int y) { n   if (L <= l && r <= R) { n     for (int i = 0; i < 10; i++) n       if (lazy[rt][i] == x) { n         sum[rt][y] += sum[rt][x]; n         sum[rt][x] = 0; n         lazy[rt][i] = y; n       } n     return; n   } n   pushdown(rt); n   int m = r + l >> 1; n   if (L <= m) update(rt << 1, l, m, L, R, x, y); n   if (R > m) update(rt << 1 | 1, m + 1, r, L, R, x, y); n   pushup(rt); n } n long long int query(int rt, int l, int r, int L, int R) { n   if (L <= l && r <= R) { n     long long int ans = 0; n     for (long long int i = 1; i < 10; i++) ans += sum[rt][i] * i; n     return ans; n   } n   pushdown(rt); n   int m = r + l >> 1; n   long long int ans = 0; n   if (L <= m) ans += query(rt << 1, l, m, L, R); n   if (R > m) ans += query(rt << 1 | 1, m + 1, r, L, R); n   pushup(rt); n   return ans; n } n int n, m; n int main() { n   while (~scanf( %d%d , &n, &m)) { n     for (int i = 1; i <= n; i++) scanf( %lld , &a[i]); n     build(1, 1, n); n     int op, l, r, x, y; n     for (int i = 1; i <= m; i++) { n       scanf( %d%d%d , &op, &l, &r); n       if (1 == op) { n         scanf( %d%d , &x, &y); n         if (x == y) continue; n         update(1, 1, n, l, r, x, y); n       } else n         printf( %lld n , query(1, 1, n, l, r)); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long labs(long long a) { return a < 0 ? (-a) : a; } n vector<pair<long long, long long> > v; n vector<long long> c; n vector<bool> was; n int main() { n   int n, k; n   cin >> n >> k; n   long long b; n   cin >> b; n   v.resize(n); n   c.resize(n); n   for (int(i) = 0; (i) < (n); (i)++) { n     cin >> c[i]; n     v[i].first = c[i]; n     v[i].second = i; n   } n   v.pop_back(); n   sort((v).begin(), (v).end()); n   reverse((v).begin(), (v).end()); n   was.assign(n, false); n   long long sum = 0; n   for (int(i) = 0; (i) < (k - 1); (i)++) { n     was[v[i].second] = true; n     sum += v[i].first; n   } n   int res = n - 1; n   for (int(i) = 0; (i) < (n); (i)++) { n     if (was[i]) { n       if (sum + v[k - 1].first > b) { n         res = min(i, res); n       } n     } else { n       if (sum + c[i] > b) { n         res = min(i, res); n       } n     } n   } n   cout << res + 1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200005; n int a[N]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   sort(a + 1, a + n + 1); n   int lim = n / 2 - 1, res = 1e9 + 7; n   for (int i = 0; i <= lim; i++) { n     res = min(res, a[n - (lim - i)] - a[i + 1]); n   } n   printf( %d n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   ; n   long long x, y; n   cin >> x >> y; n   long long n; n   cin >> n; n   long long ans; n   switch (n % 6) { n     case 1: n       ans = x; n       break; n     case 2: n       ans = y; n       break; n     case 3: n       ans = y - x; n       break; n     case 4: n       ans = -x; n       break; n     case 5: n       ans = -y; n       break; n     case 0: n       ans = x - y; n       break; n   } n   cout << (ans + 2 * mod) % mod; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 300000; n const long long INF = (1 << 30) - 1; n int n; n long long a[N + 9], sum[N + 9]; n void into() { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) { n     scanf( %lld , &a[i]); n     sum[i] = a[i] + sum[i - 1]; n   } n } n int ans; n void Get_ans(int l, int r) { n   if (l <= 0 || r > n) return; n   for (; 2333;) { n     if ((a[l] ^ a[r]) == sum[r - 1] - sum[l]) ++ans; n     int le = 1, ri = n, len = n + 1; n     for (int mid; le <= ri;) { n       mid = le + ri >> 1; n       int nl = l - mid, nr = r + mid; n       if (nl <= 0 || nr > n) { n         len = mid; n         ri = mid - 1; n         continue; n       } n       sum[nr] - sum[nl - 1] - sum[r - 1] + sum[l] >= sum[r - 1] - sum[l] n           ? (len = mid, ri = mid - 1) n           : le = mid + 1; n     } n     l -= len; n     r += len; n     if (l <= 0 || r > n) return; n     if (sum[r - 1] - sum[l] > INF) return; n   } n } n void Get_ans() { n   for (int i = 1; i <= n; ++i) Get_ans(i - 1, i + 1); n   for (int i = 1; i < n; ++i) Get_ans(i - 1, i + 2); n } n void work() { Get_ans(); } n void outo() { printf( %d n , ans); } n int main() { n   int T = 1; n   for (; T--;) { n     into(); n     work(); n     outo(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[100000]; n vector<int> p; n long long fact[100001]; n long long inv[100001]; n long long finv[100001]; n void init(int n) { n   int i; n   fact[0] = 1; n   for (i = 1; i <= n; i++) fact[i] = fact[i - 1] * i % 1000000007; n   inv[0] = 1; n   inv[1] = 1; n   for (i = 2; i <= n; i++) n     inv[i] = 1000000007 - (1000000007 / i) * inv[1000000007 % i] % 1000000007; n   finv[0] = 1; n   for (i = 1; i <= n; i++) finv[i] = finv[i - 1] * inv[i] % 1000000007; n } n long long C(int n, int k) { n   return fact[n] * (finv[k] * finv[n - k] % 1000000007) % 1000000007; n } n long long pow(int x, int p) { n   long long ans = 1; n   for (int i = 0; i < p; i++) ans = ans * x % 1000000007; n   return ans; n } n int next(int s, int t, int m) { n   if (s < 0 || s >= m) return t; n   if (t < 0 || t >= m) return s; n   return a[p[s]] < a[p[t]] ? s : t; n } n int pos(int i, int m, int n) { n   if (i < 0) return -1; n   if (i >= m) return n; n   return p[i]; n } n long long solve(int cnt, int l, int r, int n) { n   int s = cnt - 1, t = cnt + 1; n   if (l <= pos(s, p.size(), n) || r >= pos(t, p.size(), n)) return 0; n   long long ans = 1; n   while (!(s == -1 && t == p.size())) { n     int nxt = next(s, t, p.size()); n     int w = r - l + 1; n     if (nxt == t) { n       int d = p[nxt] - r - 1; n       if (d > a[p[nxt]] - w - 1) return 0; n       d = a[p[nxt]] - w - 1 - d; n       l -= d; n       if (l <= pos(s, p.size(), n)) return 0; n       r = p[nxt]; n       ans = ans * C(a[p[nxt]] - w - 1, d) % 1000000007; n       t++; n     } n     if (nxt == s) { n       int d = l - p[nxt] - 1; n       if (d > a[p[nxt]] - w - 1) return 0; n       d = a[p[nxt]] - w - 1 - d; n       r += d; n       if (r >= pos(t, p.size(), n)) return 0; n       l = p[nxt]; n       ans = ans * C(a[p[nxt]] - w - 1, d) % 1000000007; n       s--; n     } n   } n   ans = ans * C(n - (r - l + 1), l) % 1000000007; n   return ans; n } n int main() { n   int n, i; n   scanf( %d , &n); n   init(n); n   for (i = 0; i < n; i++) { n     scanf( %d , &a[i]); n     if (a[i] > 0) p.push_back(i); n   } n   if (p.size() == 0) { n     printf( %I64d n , pow(2, n - 1)); n     return 0; n   } n   if (p.size() == 1) { n     if (a[p[0]] == 1) n       printf( %I64d n , C(n - 1, p[0])); n     else { n       int t = a[p[0]] - 1; n       long long vl = (p[0] >= t ? C(n - t - 1, p[0] - t) : 0); n       long long vr = (n - p[0] - 1 >= t ? C(n - t - 1, p[0]) : 0); n       printf( %I64d n , pow(2, t - 1) * (vl + vr) % 1000000007); n     } n     return 0; n   } n   int cnt = 0; n   while (cnt + 1 < p.size() && a[p[cnt]] > a[p[cnt + 1]]) cnt++; n   for (i = cnt; i + 1 < p.size(); i++) n     if (a[p[i]] > a[p[i + 1]]) { n       puts( 0 ); n       return 0; n     } n   int l, r; n   long long ans; n   if (a[p[cnt]] == 1) { n     l = p[cnt]; n     r = p[cnt]; n     ans = solve(cnt, l, r, n); n   } else { n     l = p[cnt] - (a[p[cnt]] - 1); n     r = p[cnt]; n     ans = solve(cnt, l, r, n); n     l = p[cnt]; n     r = p[cnt] + (a[p[cnt]] - 1); n     ans = (ans + solve(cnt, l, r, n)) % 1000000007; n   } n   printf( %I64d n , ans * pow(2, r - l - 1) % 1000000007); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> X[105], Y[105]; n char s[105]; n int main() { n   int n, i, j; n   scanf( %d , &n); n   for (i = 0; i < n; i++) { n     scanf( %s , s); n     for (j = 0; j < n; j++) { n       if (s[j] ==  . ) { n         X[i].push_back(j); n         Y[j].push_back(i); n       } n     } n   } n   for (i = 0; i < n; i++) { n     if (X[i].size() == 0) break; n   } n   if (i == n) { n     for (i = 0; i < n; i++) { n       printf( %d %d n , i + 1, X[i][0] + 1); n     } n     return 0; n   } n   for (i = 0; i < n; i++) { n     if (Y[i].size() == 0) break; n   } n   if (i < n) { n     printf( -1 n ); n     return 0; n   } n   for (i = 0; i < n; i++) { n     printf( %d %d n , Y[i][0] + 1, i + 1); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n bool umin(T& a, T b) { n   if (a > b) { n     a = b; n     return 1; n   } n   return 0; n } n template <class T> n bool umax(T& a, T b) { n   if (a < b) { n     a = b; n     return 1; n   } n   return 0; n } n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n; n     string s; n     cin >> n >> s; n     int a = n, b = n; n     for (int i = 0; i < n; i++) n       if (s[i] ==  > ) { n         a = i; n         break; n       } n     for (int i = n - 1; i >= 0; i--) n       if (s[i] ==  < ) { n         b = n - i - 1; n         break; n       } n     printf( %d n , min(a, b)); n   } 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     long long p1, p2, p3; n     cin >> p1 >> p2 >> p3; n     cout << (p1 + p2 + p3) / 2 << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 5; n const int MOD = 1e9 + 7; n const long long M = 1e18; n long long qpow(long long a, long long b, long long m) { n   long long ans = 1; n   while (b) { n     if (b & 1) ans = (ans * a) % m; n     b /= 2; n     a = (a * a) % m; n   } n   return ans; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   int t = 1; n   while (t--) { n     int n, q; n     cin >> n >> q; n     int a[n + 1]; n     map<int, int> mn, mx, ct; n     for (int i = 1; i <= n; i++) { n       cin >> a[i]; n       if (mn[a[i]] == 0) mn[a[i]] = i; n       mx[a[i]] = i; n       ct[a[i]]++; n     } n     int l = 1, r = 1; n     int ans = 0; n     for (int i = 1; i <= n; i++) { n       r = max(r, mx[a[i]]); n       if (i == r) { n         int ans1 = N; n         for (int j = l; j <= r; j++) { n           ans1 = min(ans1, (r - l + 1) - ct[a[j]]); n         } n         ans += ans1; n         l = r + 1; n         r++; n       } n     } n     cout << ans; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n; n bool f, a[(int)1e6 + 9]; n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 1; i < (n == 3 ? 2 : (n >> 1)); i++) { n     if (n % i == 0) { n       for (int j = 0; j < i; j++) { n         if (!a[j]) continue; n         int k = j + i; n         f = 1; n         while (k != j) { n           if (!a[k]) { n             f = 0; n             break; n           } n           k = (k + i) % n; n         } n         if (f) break; n       } n       if (f) break; n     } n   } n   if (f) n     printf( YES ); n   else n     printf( NO ); n   return 0; n } n


#include<bits/stdc++.h> n #define int long long n #define float double n #define itn int n #define f(j,n) for(int i=j;i<n;i++) n #define fastio ios_base::sync_with_stdio(false);cin.tie(NULL) n #define endl   n  n #define Endl   n  n #define ednl   n  n #define Ednl   n  n #define pb push_back n #define mp make_pair n #define mod 1000000000000000000 n #define mset(arr, i) memset(arr, i, sizeof(arr)) n using namespace std; n   n typedef vector<pair<int, int>> vpi; n typedef vector<vector<int>> vvi; n typedef pair<int, int> pii; n typedef vector<int> vi; n typedef map<int,int> mii; n typedef priority_queue<int> pqi; n typedef priority_queue<int, vector<int>, greater<int>> pqim; n   n long long binpow(long long a, long long b) { a %= mod; long long res = 1; while (b > 0){if (b & 1)res = res * a % mod;a = a * a % mod;b >>= 1;}return res;} n  n int LCSubStr(string X, string  Y, int m, int n) n {  n     int LCSuff[m + 1][n + 1]; n     int result = 0;  n     for (int i = 0; i <= m; i++) n     { n         for (int j = 0; j <= n; j++) n         { n             if (i == 0 || j == 0) n                 LCSuff[i][j] = 0; n   n             else if (X[i - 1] == Y[j - 1]) { n                 LCSuff[i][j] = LCSuff[i - 1][j - 1] + 1; n                 result = max(result, LCSuff[i][j]); n             } n             else n                 LCSuff[i][j] = 0; n         } n     } n     return result; n } n  n int32_t main() n { n     int t1; n     fastio; n     cin>>t1;  n     //t1=1; n     while(t1--) n     { n         string a, b; n         cin>>a>>b; n         int n1=a.length(), n2 = b.length(); n         int ans = LCSubStr(a, b, n1, n2); n         cout<<n1+n2-2*ans<<endl; n     } n      n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n; n   cin >> n; n   int a[100000]; n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n   } n   sort(a, a + n); n   cout << min(a[n - 1] - a[1], a[n - 2] - a[0]) <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n signed main(signed argc, const char* argv[]) { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   int cnt = 0; n   for (int i = 0; cnt < n; ++i) { n     cout << 3 <<     << i << endl; n     ++cnt; n     if (cnt == n) break; n     if (i % 2) { n       ++cnt; n       cout << 0 <<     << i << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int mn = 410, mm = 101000, inf = 1 << 30; n bool vis[mn]; n int X[mm], Y[mm], du[mn]; n int n, m, K, t, S, T, ans[mm], answer; n int dis[mm], pre[mm], Q[mm]; n struct network { n   int tt, la[mn], next[mm], y[mm], f[mm], co[mm]; n   void clear() { n     tt = 1; n     memset(la, 0, (T + 2) * 4); n   } n   void add(int i, int j, int u, int v) { n     ++tt, next[tt] = la[i], la[i] = tt, y[tt] = j, f[tt] = u, co[tt] = v; n     ++tt, next[tt] = la[j], la[j] = tt, y[tt] = i, f[tt] = 0, co[tt] = -v; n   } n } e; n int spfa() { n   memset(vis, 0, T + 2); n   memset(pre, 0, (T + 2) * 4); n   fill(dis + 1, dis + 1 + T, inf); n   dis[S] = 0; n   int st = 1, ed = 2; n   Q[1] = S, vis[S] = 1; n   while (st != ed) { n     int r = Q[st]; n     for (int p = e.la[r], i; p; p = e.next[p]) { n       if (!e.f[p]) continue; n       i = e.y[p]; n       if (dis[r] + e.co[p] < dis[i]) { n         dis[i] = dis[r] + e.co[p], pre[i] = p; n         if (!vis[i]) { n           Q[ed] = i, vis[i] = 1; n           ed = (ed + 1) % (T + 2); n         } n       } n     } n     st = (st + 1) % (T + 2); n     vis[r] = 0; n   } n   return dis[T]; n } n void repair() { n   for (int i = T, p; i != S; i = e.y[p ^ 1]) { n     p = pre[i]; n     --e.f[p], ++e.f[p ^ 1]; n   } n } n int main() { n   scanf( %d%d%d%d , &n, &m, &K, &t); n   for (int i = 1; i <= K; ++i) { n     scanf( %d%d , X + i, Y + i); n     ++du[X[i]], ++du[Y[i] + n]; n   } n   for (int i = 1; i <= n + m; ++i) answer += (du[i] % t) != 0; n   S = n + m + 1, T = S + 1; n   while (t) { n     e.clear(); n     for (int i = 1; i <= K; ++i) { n       if (ans[i]) continue; n       e.add(X[i], Y[i] + n, 1, 0); n     } n     for (int i = 1; i <= n; ++i) { n       e.add(S, i, du[i] / t, 0); n       if (du[i] % t != 0) e.add(S, i, 1, 1); n     } n     for (int i = n + 1; i <= n + m; ++i) { n       e.add(i, T, du[i] / t, 0); n       if (du[i] % t != 0) e.add(i, T, 1, 1); n     } n     while (spfa() < inf) repair(); n     int tot = 0; n     for (int i = 1; i <= K; ++i) { n       if (ans[i]) continue; n       ++tot; n       if (e.f[tot * 2] == 0) { n         ans[i] = t; n         --du[X[i]], --du[Y[i] + n]; n       } n     } n     --t; n   } n   printf( %d n , answer); n   for (int i = 1; i <= K - 1; ++i) printf( %d  , ans[i]); n   printf( %d n , ans[K]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool debug = 0; n int n, m, k; n int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; n string direc =  RDLU ; n long long ln, lk, lm; n char s[105][105]; n int cal(int x) { return x * (x - 1) / 2; } n void fmain() { n   scanf( %d , &n); n   for (int(i) = 1; (i) <= (int)(n); (i)++) scanf( %s , s[i] + 1); n   for (int(i) = 1; (i) <= (int)(n); (i)++) { n     m = 0; n     for (int(j) = 1; (j) <= (int)(n); (j)++) n       if (s[i][j] ==  C ) m++; n     k += cal(m); n   } n   for (int(j) = 1; (j) <= (int)(n); (j)++) { n     m = 0; n     for (int(i) = 1; (i) <= (int)(n); (i)++) n       if (s[i][j] ==  C ) m++; n     k += cal(m); n   } n   printf( %d n , k); n } n int main() { n   fmain(); n   return 0; n } n


#include <algorithm> n #include <climits> n #include <cmath> n #include <cstdio> n #include <cstring> n #include <fstream> n #include <functional> n #include <iomanip> n #include <iostream> n #include <map> n #include <queue> n #include <set> n #include <stack> n #include <string> n #include <vector> n #define pb push_back n #define IOS ios::sync_with_stdio(0), cin.tie(0), cout.tie(0) n #define mem(a) memset(a, 0, sizeof(a)) n using namespace std; n typedef long long ll; n typedef unsigned long long ull; n typedef long double ld; n typedef double db; n typedef pair<ll, ll> P; n const int maxn = 2e5 + 10; n int n, m; n vector<int> vec[maxn]; n int dis[maxn]; n bool vis[maxn]; n int dp[maxn]; n void bfs() n { n     queue<P> q; n     dis[1] = 0; n     q.push(P{1, 0}); n     while (q.size()) n     { n         P p = q.front(); n         q.pop(); n         int po = p.first; n         int d = p.second; n         if (vis[po]) n             continue; n         vis[po] = 1; n         dis[po] = d; n         for (auto v : vec[po]) n         { n             q.push(P(v, d + 1)); n         } n     } n } n void dfs(int x) n { n     vis[x] = 1; n     dp[x] = dis[x]; n     for(auto u:vec[x]) n     { n         if(!vis[u]&& dis[x]<dis[u]) n             dfs(u); n         if(dis[x]<dis[u]) n             dp[x] = min(dp[x], dp[u]); n         else n             dp[x] = min(dp[x], dis[u]); n     } n } n void solve() n { n     cin >> n >> m; n     for (int i = 1; i <= n; i++) n         vec[i].clear(), dis[i] = 0, vis[i] = 0, dp[i] = 1e7; n     for (int i = 1; i <= m; i++) n     { n         int x, y; n         cin >> x >> y; n         vec[x].pb(y); n     } n     bfs(); n     for (int i = 1; i <= n; i++) n         vis[i] = 0; n     dfs(1); n     for (int i = 1; i <= n;i++) n         cout << dp[i] <<    ; n     cout << endl; n } n int main() n { n     IOS; n     int t; n     cin >> t; n     while (t--) n         solve(); n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     int ar[n], i, b; n     b = n; n     for (i = 0; i < n; i++) cin >> ar[i]; n     sort(ar, ar + n); n     for (i = 0; i < n; i++) { n       if (ar[i] < b) n         b--; n       else n         break; n     } n     cout << b <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string str, result =   , tmp; n   map<string, int> array; n   cin >> str; n   for (long long i = 0; i < 10; i++) cin >> tmp, array[tmp] = i; n   tmp =   ; n   for (long long i = 0; i < 80; i++) { n     tmp += str[i]; n     if ((i + 1) % 10 == 0) result += array[tmp] +  0 , tmp =   ; n   } n   cout << result << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void fast() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n } n int32_t main() { n   fast(); n   long long t; n   cin >> t; n   while (t--) { n     long long n, k; n     cin >> n >> k; n     priority_queue<long long> b; n     priority_queue<long long, vector<long long>, greater<long long>> a; n     long long d; n     long long sum = 0; n     for (long long i = 0; i < n; i++) { n       cin >> d; n       a.push(d); n       sum += d; n     } n     for (long long i = 0; i < n; i++) { n       cin >> d; n       b.push(d); n     } n     for (long long i = 0; i < k; i++) { n       if (a.top() < b.top()) { n         long long a1 = a.top(); n         long long b1 = b.top(); n         a.pop(); n         b.pop(); n         b.push(a1); n         a.push(b1); n         sum = sum - a1 + b1; n       } else n         break; n     } n     cout << sum << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int r, h; n int main() { n   scanf( %d%d , &r, &h); n   double tt = h + r * 1.0 / 2; n   double re = tt / r - (int)(tt / r); n   int ans; n   if (fabs(re) < 0.00001) { n     ans = (int)(tt / r) * 2; n   } else { n     ans = (int)(tt / r) * 2; n     re = h + r - (int)(tt / r) * r; n     double mm = re - sqrt(3.0) / 2 * r; n     if (mm > 0.00001) ans++; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t; n   cin >> t; n   long long int a, b, n; n   while (t--) { n     cin >> a >> b >> n; n     n %= 3; n     if (n == 0) n       cout << a << endl; n     else if (n == 1) n       cout << b << endl; n     else n       cout << (a ^ b) << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int q; n   cin >> q; n   while (q != 0) { n     int n; n     cin >> n; n     int one = 0; n     if (n == 1) n       cout <<  FastestFinger  << endl; n     else if (n == 2) n       cout <<  Ashishgup  << endl; n     else if (n % 2 != 0) n       cout <<  Ashishgup  << endl; n     else { n       int t = n; n       int count = 0, last = n; n       set<int> s; n       while (n != 1) { n         for (int i = 2; i <= sqrt(n); i++) { n           if ((n % i) == 0) { n             if (i % 2 != 0) { n               s.insert(i); n               count++; n             } n             n = (n / i); n           } n         } n         if (n == last) break; n         last = n; n       } n       if (n % 2 != 0) { n         s.insert(n); n         count++; n       } n       for (auto a : s) { n         t = (t / a); n       } n       if ((t == 2 && count != 1) || t == 1) n         cout <<  Ashishgup  << endl; n       else if (t == 2 && count == 1) n         cout <<  FastestFinger  << endl; n       else if (count != 0) n         cout <<  Ashishgup  << endl; n       else n         cout <<  FastestFinger  << endl; n     } n     q--; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int read() { n   int x = 0, f = 0; n   char ch = 0; n   while (!isdigit(ch)) f |= (ch ==  - ), ch = getchar(); n   while (isdigit(ch)) (x *= 10) += (ch ^ 48), ch = getchar(); n   return f ? -x : x; n } n void Ot(int x) { n   if (x < 0) putchar( - ), x = -x; n   if (x >= 10) Ot(x / 10); n   putchar(x % 10 + 48); n } n void Print(int x, char til =   n ) { n   Ot(x); n   putchar(til); n } n int Max(int x, int y) { return x > y ? x : y; } n int Min(int x, int y) { return x < y ? x : y; } n int Abs(int x) { return x < 0 ? -x : x; } n vector<int> G, P; n int n, ans; n char Getch() { n   char c = getchar(); n   while (c !=  .  && c !=  *  && c !=  P ) c = getchar(); n   return c; n } n bool check(int x) { n   int t = 1, pos = G[t - 1]; n   for (register int i = 0; i < P.size(); i++) { n     int way = P[i] - pos; n     if (way > 0 && way > x) return 0; n     while (1) { n       int len = P[i] - pos; n       if (1ll * way * len < 0) { n         if (Abs(len) < Abs(way)) n           len = Abs(len) * 2 + Abs(way); n         else n           len = Abs(len) + Abs(way) * 2; n       } else n         len = Abs(len); n       if (len <= x) n         t++, pos = G[t - 1]; n       else n         break; n       if (t > G.size()) return 1; n     } n   } n   return 0; n } n signed main() { n   n = read(); n   for (register int i = 1; i <= n; i++) { n     char ch = Getch(); n     if (ch ==  P ) n       P.push_back(i); n     else if (ch ==  * ) n       G.push_back(i); n   } n   if (!G.size()) return Print(0), 0; n   int L = 0, R = 2 * n; n   while (L <= R) { n     register int mid = (L + R) >> 1; n     if (check(mid)) n       ans = mid, R = mid - 1; n     else n       L = mid + 1; n   } n   Print(ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const int N = 3e5 + 10; n const long long mod = 998244353; n const double PI = acos(-1); n inline int ab(int x) { return x < 0 ? -x : x; } n inline int mm(int x, int p) { return x >= p ? x - p : x < 0 ? x + p : x; } n int main() { n   int n, ans = 0, now = 0, pre = INF; n   scanf( %d , &n); n   for (int i = 1, x; i <= n; ++i) { n     scanf( %d , &x); n     if (x > pre * 2) { n       ans = max(ans, now); n       pre = x; n       now = 1; n     } else { n       now++; n       pre = x; n     } n   } n   ans = max(ans, now); n   printf( %d , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> graph[(int)2e5 + 5]; n int visited[(int)2e5 + 5]; n vector<int64_t> values; n int64_t dfs(int node, int depth) { n   if (visited[node] == 1) return 0; n   visited[node] = 1; n   int64_t child = 0; n   for (int i = 0; i < graph[node].size(); i++) { n     if (visited[graph[node][i]] == 0) child += dfs(graph[node][i], depth + 1); n   } n   values.push_back(depth - (int64_t)child); n   return child + 1; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   ; n   int t = 1; n   while (t--) { n     int total_city, num_of_industry, u, v; n     cin >> total_city >> num_of_industry; n     for (int i = 1; i < total_city; i++) { n       cin >> u >> v; n       graph[u].push_back(v); n       graph[v].push_back(u); n     } n     dfs(1, 0); n     sort(values.begin(), values.end(), greater<int>()); n     int64_t sum = 0; n     for (int i = 0; i < num_of_industry; i++) { n       sum += values[i]; n     } n     cout << sum; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k, t; n   cin >> n >> k >> t; n   cout << (k >= t ? t : (n >= t ? k : k - (t - n))); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1024; n int dp[N]; n int n, k; n vector<int> ans; n int f(int i) { n   if (i >= n) return 0; n   if (i + k >= n) return 1e9; n   if (dp[i] != -1) return dp[i]; n   return dp[i] = f(i + 2 * k + 1) + 1; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> k; n   memset(dp, -1, sizeof dp); n   int r = 1e9, best = -1; n   for (int i = 0, ThxDem = k + 1; i < ThxDem; ++i) n     if (r > f(i + k + 1) + 1) { n       r = f(i + k + 1) + 1; n       best = i; n     } n   while (best < n) { n     ans.push_back(best + 1); n     best += 2 * k + 1; n   } n   assert(((int)(ans).size()) == r); n   cout << r <<   n ; n   for (int x : ans) cout << x <<    ; n   cout <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n bool cmp(int a, int b) { n   if (a > b) { n     return true; n   } else { n     return false; n   } n } n int main() { n   int n, k, le[26], cnt = 1; n   for (int i = 0; i < 26; i++) { n     le[i] = 0; n   } n   char c, ch; n   cin >> n >> k; n   getchar(); n   cin >> ch; n   if (n == k & n == 1) { n     cout << 1; n     return 0; n   } n   for (int i = 0; i < n - 1; i++) { n     cin >> c; n     if (c == ch) { n       cnt++; n     } n     if (c != ch || i == n - 2) { n       le[ch -  a ] += cnt / k; n       cnt = 1; n       ch = c; n     } n   } n   sort(le, le + 26, cmp); n   cout << le[0]; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f; n const int maxn = 2e5 + 5; n struct node { n   int u, v, ne; n } e[maxn << 1]; n int n; n int head[maxn], len; n int ve[maxn], b[maxn]; n void add(int u, int v) { n   e[len].u = u; n   e[len].v = v; n   e[len].ne = head[u]; n   head[u] = len++; n } n void dfs(int x, int p, int k) { n   ve[x] = 1; n   b[x] = k; n   for (int i = head[x]; ~i; i = e[i].ne) { n     int v = e[i].v; n     if (v == p) continue; n     dfs(v, x, k ^ 1); n     ve[x] += ve[v]; n   } n   return; n } n int main() { n   memset(head, -1, sizeof(head)); n   cin >> n; n   for (int i = 1, x, y; i < n; i++) { n     scanf( %d %d , &x, &y); n     add(x, y); n     add(y, x); n   } n   dfs(1, 0, 0); n   long long ans = 0, sum = 0; n   for (int i = 1; i <= n; i++) { n     ans += 1ll * ve[i] * (n - ve[i]); n     sum += b[i]; n   } n   ans += sum * (n - sum); n   cout << ans / 2 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long read() { n   long long x = 0; n   char ch = getchar(); n   bool f = 0; n   for (; !isdigit(ch); ch = getchar()) n     if (ch ==  - ) f = 1; n   for (; isdigit(ch); ch = getchar()) x = x * 10 + ch -  0 ; n   return f ? -x : x; n } n void write(long long x) { n   if (x < 0) putchar( - ), x = -x; n   if (x >= 10) write(x / 10); n   putchar(x % 10 +  0 ); n } n void writeln(long long x) { n   write(x); n   puts(  ); n } n void writep(long long x) { n   write(x); n   putchar(   ); n } n const int N = 1000005; n int n, cnt; n struct info { n   long long x, y, z; n   int id; n } a[N], b[N]; n bool cmp(info a, info b) { n   if (a.x != b.x) return a.x < b.x; n   if (a.y != b.y) return a.y < b.y; n   return a.z < b.z; n } n void solve(bool d) { n   cnt = 0; n   sort(a + 1, a + n + 1, cmp); n   for (int i = 1, j = 1; i <= n; i = j + 1) { n     j = i; n     if (d) { n       while (j + 1 <= n && a[j + 1].x == a[j].x && a[j + 1].y == a[j].y) j++; n     } else n       while (j + 1 <= n && a[j + 1].x == a[j].x) j++; n     for (int k = i; k <= j; k += 2) { n       if (k + 1 > j) break; n       writep(a[k].id); n       writeln(a[k + 1].id); n     } n     if ((j - i + 1) % 2) b[++cnt] = a[j]; n   } n   if (cnt == 0) exit(0); n   n = cnt; n   for (int i = (int)(1); i <= (int)(n); i++) a[i] = b[i]; n } n int main() { n   n = read(); n   for (int i = (int)(1); i <= (int)(n); i++) n     a[i].x = read(), a[i].y = read(), a[i].z = read(), a[i].id = i; n   solve(1); n   solve(0); n   for (int i = 1; i <= n; i += 2) writep(a[i].id), writeln(a[i + 1].id); n } n


#include <bits/stdc++.h> n using namespace std; n using ll = int_fast64_t; n constexpr int mod = 1e9 + 7; n constexpr int inf = (1 << 30) - 1; n constexpr ll infll = (1LL << 61) - 1; n #pragma GCC target( avx2 ) n #pragma GCC optimize( O3 ) n #pragma GCC optimize( unroll-loops ) n int main() { n   ios::sync_with_stdio(false), cin.tie(nullptr); n   int N; n   cin >> N; n   map<pair<int, int>, int> mp; n   vector<pair<int, int>> v(N); n   for (int i = 0; i < N; i++) { n     cin >> v[i].first >> v[i].second; n     if (mp[v[i]] != 0) { n       cout << mp[v[i]] <<     << i + 1 <<   n ; n       return 0; n     } n     mp[v[i]] = i + 1; n     v[i].second *= -1; n   } n   sort(v.begin(), v.end()); n   set<int> st; n   int ans1 = -1, ans2 = -1; n   for (int i = 0; i < N; i++) { n     v[i].second *= -1; n     st.erase(st.begin(), st.lower_bound(v[i].first)); n     if (!st.empty() && st.lower_bound(v[i].second) != st.end()) { n       ans1 = i; n     } n     st.insert(v[i].second); n   } n   if (ans1 == -1) { n     cout << -1 <<     << -1 <<   n ; n     return 0; n   } n   for (int i = 0; i < N; i++) { n     if (i == ans1) continue; n     if (v[i].first <= v[ans1].first && v[ans1].second <= v[i].second) ans2 = i; n   } n   cout << mp[v[ans1]] <<     << mp[v[ans2]] <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3010; n long long a[N], b[N], n, m; n long long sa = 0, sb = 0, bestSa, bestSb; n pair<int, int> ansOne, ansTwo; n vector<long long> va, vb; n void add(long long *arr, int size, vector<long long> &v) { n   for (int i = 0; i < size; ++i) n     for (int j = i + 1; j < size; ++j) v.push_back(arr[i] + arr[j]); n } n long long one() { n   long long ba = sa; n   long long bb = sb; n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < m; ++j) { n       long long nsa = sa - a[i] + b[j], nsb = sb - b[j] + a[i]; n       if (llabs(nsa - nsb) < llabs(ba - bb)) { n         ba = nsa; n         bb = nsb; n         ansOne = pair<int, int>(i + 1, j + 1); n       } n     } n   } n   return llabs(ba - bb); n } n long long two() { n   long long diff = llabs(sa - sb); n   for (int i = 0; i < (int)va.size(); ++i) { n     long long x = va[i]; n     long long y = x + ((sb - sa) / 2); n     int at = lower_bound(vb.begin(), vb.end(), y) - vb.begin(); n     for (int j = at - 1; j <= at; ++j) { n       if (j >= 0 and j < (int)vb.size()) { n         long long nsa = sa - x + vb[j]; n         long long nsb = sb - vb[j] + x; n         if (llabs(nsa - nsb) < diff) { n           diff = llabs(nsa - nsb); n           ansTwo = pair<int, int>(x, vb[j]); n         } n       } n     } n   } n   return diff; n } n pair<int, int> find(long long e, long long *arr, int size) { n   for (int i = 0; i < size; ++i) n     for (int j = i + 1; j < size; ++j) n       if (arr[i] + arr[j] == e) return pair<int, int>(i + 1, j + 1); n } n void solve() { n   long long d0 = llabs(sa - sb), d1 = one(), d2 = d1; n   if (n > 1 and m > 1) { n     d2 = two(); n   } n   long long best = min(d0, min(d1, d2)); n   cout << best <<   n ; n   if (best == d0) { n     cout <<  0 n ; n   } else if (best == d1) { n     cout <<  1 n ; n     cout << ansOne.first <<     << ansOne.second <<   n ; n   } else { n     vector<pair<int, int> > pans; n     pans.push_back(find(ansTwo.first, a, n)); n     pans.push_back(find(ansTwo.second, b, m)); n     cout <<  2 n ; n     cout << pans[0].first <<     << pans[1].first <<   n ; n     cout << pans[0].second <<     << pans[1].second <<   n ; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin >> n; n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n     sa += a[i]; n   } n   cin >> m; n   for (int i = 0; i < m; ++i) { n     cin >> b[i]; n     sb += b[i]; n   } n   add(a, n, va); n   add(b, m, vb); n   sort(vb.begin(), vb.end()); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = (int)1e9; n const int mod = (int)1e9 + 7; n const int MAXN = (int)3e5 + 5; n vector<int> primes[1000005]; n int x, p, k; n vector<pair<int, int> > v(1 << 7); n int calc(int a) { n   int cnt = 1; n   int num = 0; n   int tmp = a; n   a = 0; n   for (int i = 1; i < (1 << primes[p].size()); i++) { n     if (cnt * 2 == i) { n       cnt *= 2; n       num++; n     } n     v[i] = {v[i - cnt].first * primes[p][num], v[i - cnt].second + 1}; n     if (v[i].second % 2) { n       a += tmp / v[i].first; n     } else { n       a -= tmp / v[i].first; n     } n   } n   return tmp - a; n } n void solve() { n   cin >> x >> p >> k; n   k += calc(x); n   int l = x + 1, r = 6e6 + 5e5 + 3e4 + 4e3 + 5e2; n   while (l < r) { n     int m = (l + r) / 2; n     if (calc(m) >= k) { n       r = m; n     } else { n       l = m + 1; n     } n   } n   cout << l <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   for (int i = 2; i < 1000005; i++) { n     if (!primes[i].size()) { n       for (int j = i; j < 1000005; j += i) { n         primes[j].push_back(i); n       } n     } n   } n   v[0] = {1, 0}; n   int t = 1; n   cin >> t; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n; n   cin >> n; n   int t[n]; n   for (int i = 0; i < n; i++) { n     cin >> t[i]; n   } n   sort(t, t + n); n   bool b = true; n   int j = 0; n   if (t[0] > 1) { n     cout << 1; n   } else { n     while ((b == true) && (j < n)) { n       if (t[j + 1] == t[j] + 1) { n         j++; n       } else { n         b = false; n       } n     } n     cout << t[j] + 1; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 2e6 + 500; n const long double eps = (1e-12); n const int logn = 21; n const double Pi = acos(-1.0); n const long long pp = 998244353; n int lowbit(int x) { return x & (-x); } n const long long INF = 1e11 + 7; n long long dp[3][205][4]; n long long a[MAXN]; n long long pre[MAXN]; n long long lst[MAXN]; n signed main() { n   long long n; n   scanf( %lld , &n); n   for (long long i = 1; i <= n; i++) { n     scanf( %lld , &a[i]); n   } n   dp[0][1][2] = 1; n   long long now = 0; n   for (long long i = 1; i <= n; i++) { n     now ^= 1; n     for (long long j = 1; j <= 200; j++) { n       pre[j] = lst[j] = 0; n       for (long long k = 0; k <= 2; k++) dp[now][j][k] = 0; n     } n     for (long long k = 0; k <= 2; k++) pre[1] += dp[now ^ 1][1][k]; n     for (long long j = 2; j <= 200; j++) { n       for (long long k = 0; k <= 2; k++) pre[j] += dp[now ^ 1][j][k]; n       pre[j] %= pp; n       pre[j] += pre[j - 1]; n       pre[j] %= pp; n     } n     for (long long k = 1; k <= 2; k++) lst[200] += dp[now ^ 1][200][k]; n     for (long long j = 199; j > 0; j--) { n       for (long long k = 1; k <= 2; k++) lst[j] += dp[now ^ 1][j][k]; n       lst[j] %= pp; n       lst[j] += lst[j + 1]; n       lst[j] %= pp; n     } n     for (long long j = 1; j <= 200; j++) { n       dp[now][j][0] += pre[j - 1]; n       dp[now][j][0] %= pp; n       dp[now][j][1] += n           dp[now ^ 1][j][0] + dp[now ^ 1][j][1] + dp[now ^ 1][j][2]; n       dp[now][j][1] %= pp; n       dp[now][j][2] += lst[j + 1]; n       dp[now][j][2] %= pp; n     } n     if (a[i] != -1) { n       for (long long j = 1; j <= 200; j++) { n         if (j == a[i]) continue; n         for (long long k = 0; k <= 2; k++) { n           dp[now][j][k] = 0; n         } n       } n     } n   } n   long long ans = 0; n   for (long long j = 1; j <= 200; j++) { n     for (long long k = 1; k <= 2; k++) { n       ans += dp[now][j][k]; n       ans %= pp; n     } n   } n   printf( %lld , ans); n   return 0; n } n


#include <bits/stdc++.h> n int min(int a, int b) { return a < b ? a : b; } n int next[1 + (200 * (200 - 1) / 2)], jj[1 + (200 * (200 - 1) / 2)]; n int link(int l, int j) { n   static int l_ = 1; n   next[l_] = l; n   jj[l_] = j; n   return l_++; n } n int ao[200], cc[200], dd[200], qq[3][200]; n int solve(int n, int c_) { n   static int dd_[200], head[3], cnt[3]; n   int i, j, l, k, t, c; n   memcpy(dd_, dd, n * sizeof *dd); n   memset(head, 0, sizeof head); n   memset(cnt, 0, sizeof cnt); n   for (i = 0; i < n; i++) n     if (dd_[i] == 0) { n       c = cc[i]; n       qq[c][head[c] + cnt[c]++] = i; n     } n   k = 0, t = 0; n   while (k < n) { n     while (cnt[c_]) { n       i = qq[c_][cnt[c_]--, head[c_]++]; n       k++, t++; n       for (l = ao[i]; l; l = next[l]) { n         j = jj[l]; n         if (--dd_[j] == 0) { n           c = cc[j]; n           qq[c][head[c] + cnt[c]++] = j; n         } n       } n     } n     c_ = (c_ + 1) % 3; n     t++; n   } n   return t - 1; n } n int main() { n   int n, i, j, k; n   scanf( %d , &n); n   for (i = 0; i < n; i++) scanf( %d , &cc[i]), cc[i]--; n   for (i = 0; i < n; i++) { n     scanf( %d , &k); n     while (k--) { n       scanf( %d , &j), j--; n       ao[j] = link(ao[j], i); n       dd[i]++; n     } n   } n   printf( %d n , min(solve(n, 0), min(solve(n, 1), solve(n, 2)))); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 7; n struct node { n   int l, r, id; n } p[2][2][maxn]; n bool cmp1(node A, node B) { return A.l < B.l; } n bool cmp2(node A, node B) { return A.r < B.r; } n int n; n int sz[2][maxn]; n int rid1[maxn], rid2[maxn]; n int main() { n   srand(time(NULL)); n   memset(sz, 0, sizeof(sz)); n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < 2; j++) { n       scanf( %d%d , &p[j][0][i].l, &p[j][0][i].r); n       p[j][0][i].id = i; n       p[j][1][i] = p[j][0][i]; n     } n   } n   for (int i = 0; i < 10000000; i++) { n     int l = rand() % n; n     int r = rand() % n; n     int L11 = p[0][0][l].l, L12 = p[0][0][l].r; n     int L21 = p[1][0][l].l, L22 = p[1][0][l].r; n     int R11 = p[0][0][r].l, R12 = p[0][0][r].r; n     int R21 = p[1][0][r].l, R22 = p[1][0][r].r; n     int flag1 = 0, flag2 = 0; n     if (max(L11, R11) <= min(L12, R12)) { n       flag1 = 1; n     } else { n       flag1 = 0; n     } n     if (max(L21, R21) <= min(L22, R22)) { n       flag2 = 1; n     } else { n       flag2 = 0; n     } n     if (flag1 != flag2) { n       cout <<  NO  << endl; n       return 0; n     } n   } n   for (int i = 0; i < 2; i++) { n     sort(p[i][0], p[i][0] + n, cmp1); n     sort(p[i][1], p[i][1] + n, cmp2); n   } n   for (int j = 0; j < 2; j++) { n     for (int i = 0; i < n; i++) { n       int l = 0, r = n - 1, ans = n; n       while (l <= r) { n         int mid = (l + r) / 2; n         if (p[j][0][mid].l > p[j][0][i].r) { n           r = mid - 1; n           ans = mid; n         } else { n           l = mid + 1; n         } n       } n       sz[j][p[j][0][i].id] += n - ans; n       l = 0, r = n - 1, ans = -1; n       while (l <= r) { n         int mid = (l + r) / 2; n         if (p[j][1][mid].r < p[j][0][i].l) { n           l = mid + 1; n           ans = mid; n         } else { n           r = mid - 1; n         } n       } n       sz[j][p[j][0][i].id] += ans + 1; n     } n   } n   for (int i = 0; i < n; i++) { n     if (sz[0][i] != sz[1][i]) { n       cout <<  NO  << endl; n       return 0; n     } n   } n   cout <<  YES  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100000; n const long long INF = 1000000000000000; n int a[N]; n int main() { n   int t; n   cin >> t; n   for (int q = 0; q < t; q++) { n     int n, k; n     cin >> n >> k; n     for (int j = 0; j < n; j++) { n       cin >> a[j]; n     } n     long long answer = -INF; n     for (long long j = 1; j < n; j++) { n       long long i = j - 1; n       while (i >= 0 and (i + 1) * (j + 1) - k * a[j] > answer) { n         long long result = (i + 1) * (j + 1) - k * (a[i] | a[j]); n         answer = max(answer, result); n         i--; n       } n     } n     cout << answer << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long pw[20][120], p[120], V, B, fA, fB, fC, ans; n int num[120], n, T, i, j; n void dfs_B(int l, long long x) { n   if ((double)x * x > V) return; n   if (l == n + 1) { n     B = max(B, x); n     return; n   } n   for (int i = 0; i <= num[l]; ++i) dfs_B(l + 1, x * pw[l][i]); n } n void dfs_A(int l, long long A) { n   if ((double)A * A * A > V) return; n   if (l == n + 1) { n     long long x = (int)sqrt(V / A); n     if (2 * A * x + V / A >= ans) return; n     V /= A; n     B = 0; n     dfs_B(1, 1); n     if (A * B + A * (V / B) + V < ans) { n       ans = A * B + A * (V / B) + V; n       fA = A; n       fB = B; n       fC = V / B; n     } n     V *= A; n     return; n   } n   for (int i = num[l]; i >= 0; --i) { n     num[l] -= i; n     dfs_A(l + 1, A * pw[l][i]); n     num[l] += i; n   } n } n int main() { n   for (scanf( %d , &T); T--;) { n     scanf( %d , &n); n     V = 1; n     for (i = 1; i <= n; ++i) { n       scanf( %I64d%d , &p[i], &num[i]); n       pw[i][0] = 1; n       for (j = 1; j <= num[i]; ++j) pw[i][j] = pw[i][j - 1] * p[i]; n       V *= pw[i][num[i]]; n     } n     ans = 1ll << 62; n     dfs_A(1, 1); n     printf( %I64d %I64d %I64d %I64d n , ans * 2, fA, fB, fC); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     long long n, g, b; n     cin >> n >> g >> b; n     long long sure_fire = (n + 1) / 2; n     long long sum = 0; n     if (g >= b) { n       sum = n; n     } else { n       long long a1 = (sure_fire + g - 1) / g; n       sum = (a1 - 1) * (g + b); n       long long a2 = n - sure_fire - (a1 - 1) * b; n       if (a2 < 0) a2 = 0; n       sure_fire -= (a1 - 1) * g; n       sum += sure_fire + a2; n     } n     cout << sum << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline bool smax(long long &a, long long b) { n   if (a < b) { n     a = b; n     return true; n   } else n     return false; n } n const long long MAXN = 1e5 + 100; n long long dp[MAXN][4], par[MAXN][4], choose[MAXN][4], id[MAXN]; n pair<long long, long long> p[MAXN], s; n set<long long> sz; n map<long long, long long> cost, toid; n bool byP(long long x, long long y) { n   return greater<pair<long long, long long> >()(p[x], p[y]); n } n pair<long long, long long> ans[MAXN]; n long long ansN; n void get(long long i, long long j) { n   if (i) get(i - 1, par[i][j]); n   if (choose[i][j] != -1) n     ans[ansN++] = pair<long long, long long>(i - 1, choose[i][j]); n } n int main() { n   ios::sync_with_stdio(false); n   cout.tie(0); n   cin.tie(0); n   memset(dp, -63, sizeof dp); n   memset(choose, -1, sizeof choose); n   long long m; n   cin >> m; n   for (long long i = 0; i < m; i++) { n     cin >> s.second >> s.first; n     sz.insert(s.first); n     cost[s.first] = s.second; n     toid[s.first] = i; n   } n   long long n; n   cin >> n; n   for (long long i = 0; i < n; i++) cin >> p[i].second >> p[i].first, id[i] = i; n   p[n++] = pair<long long, long long>(-1, -1); n   id[n - 1] = n - 1; n   sort(id, id + n, byP); n   sort(p, p + n, greater<pair<long long, long long> >()); n   long long tmp = 0; n   if (sz.count(p[0].first)) tmp += 1; n   if (sz.count(p[0].first + 1)) tmp += 2; n   dp[0][tmp] = 0; n   cerr << bitset<2>(tmp) << endl; n   cost[-1] = 0; n   for (long long i = 0; i < n - 1; i++) n     for (long long j = 0; j < 4; j++) n       if (dp[i][j] >= 0) { n         long long cur = p[i].first; n         vector<long long> t; n         t.push_back(-1); n         if (j & 1) t.push_back(cur); n         if (j >> 1 & 1) t.push_back(cur + 1); n         for (long long k = 0; k < t.size(); k++) n           if (p[i].second >= cost[t[k]]) { n             long long newmask = 0, next = p[i + 1].first; n             if (sz.count(next) && t[k] != next) newmask += 1; n             if (sz.count(next + 1) && t[k] != next + 1) newmask += 2; n             if (next == cur) newmask &= j; n             if (next == cur - 1 && j % 2 == 0) newmask -= newmask / 2 * 2; n             if (smax(dp[i + 1][newmask], dp[i][j] + cost[t[k]])) { n               par[i + 1][newmask] = j; n               choose[i + 1][newmask] = t[k]; n             } n           } n       } n   get(n - 1, 0); n   cout << dp[n - 1][0] <<   n  << ansN <<   n ; n   for (long long i = 0; i < ansN; i++) n     cout << id[ans[i].first] + 1 <<     << toid[ans[i].second] + 1 <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3010, Mo = 1000000007; n int tt, n, D; n int f[N][N], rev[N * 2], head[N], to[N], nxt[N]; n inline int gi() { n   int x = 0, o = 1; n   char ch = getchar(); n   while (ch !=  -  && (ch <  0  || ch >  9 )) ch = getchar(); n   if (ch ==  - ) o = -1, ch = getchar(); n   while (ch >=  0  && ch <=  9 ) x = x * 10 + ch -  0 , ch = getchar(); n   return x * o; n } n inline void dfs(int x) { n   for (int i = 1; i <= n; i++) f[x][i] = 1; n   for (int i = head[x]; i; i = nxt[i]) { n     int y = to[i]; n     dfs(y); n     for (int j = 1; j <= n; j++) f[x][j] = 1LL * f[x][j] * f[y][j] % Mo; n   } n   for (int i = 2; i <= n; i++) f[x][i] = (f[x][i - 1] + f[x][i]) % Mo; n } n inline int Pow(int x, int y) { n   int ret = 1; n   while (y) { n     if (y & 1) ret = 1LL * ret * x % Mo; n     x = 1LL * x * x % Mo, y >>= 1; n   } n   return ret; n } n int main() { n   cin >> n >> D; n   for (int i = 2, x; i <= n; i++) n     to[++tt] = i, nxt[tt] = head[x = gi()], head[x] = tt; n   dfs(1); n   if (D <= n) return cout << f[1][D], 0; n   int ans = 0; n   for (int i = -n; i <= n; i++) rev[i + n] = Pow(i, Mo - 2); n   for (int i = 0; i <= n; i++) { n     int sum = f[1][i]; n     for (int j = 0; j <= n; j++) n       if (i != j) sum = 1LL * sum * (D - j) % Mo * rev[i - j + n] % Mo; n     (ans += sum) %= Mo; n   } n   cout << (ans + Mo) % Mo; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long int> g[300005]; n long long int n, m, flag = 1; n map<pair<long long int, long long int>, long long int> ans; n long long int visit[300005]; n long long int color[300005]; n void dfs1(long long int src, long long int col) { n   visit[src] = 1; n   color[src] = col; n   for (long long int i = 0; i < g[src].size(); i++) { n     if (visit[g[src][i]] == 0) { n       long long int x; n       if (col == 0) n         x = 1; n       else n         x = 0; n       dfs1(g[src][i], x); n     } n   } n } n void dfs2(long long int src) { n   visit[src] = 1; n   for (long long int i = 0; i < g[src].size(); i++) { n     if (color[g[src][i]] == color[src]) { n       flag = 0; n       break; n     } n     if (color[src] == 0) { n       ans[{src, g[src][i]}] = 1; n     } else { n       ans[{src, g[src][i]}] = 0; n     } n     if (visit[g[src][i]] == 0) { n       dfs2(g[src][i]); n     } n   } n } n int32_t main() { n   cin >> n >> m; n   pair<long long int, long long int> arr[m]; n   for (long long int i = 0; i < m; i++) { n     long long int a, b; n     cin >> a >> b; n     a--; n     b--; n     arr[i].first = a; n     arr[i].second = b; n     g[a].push_back(b); n     g[b].push_back(a); n   } n   dfs1(0, 0); n   for (long long int i = 0; i < n; i++) visit[i] = false; n   dfs2(0); n   if (flag == 0) n     cout <<  NO  << endl; n   else { n     cout <<  YES  << endl; n     for (long long int i = 0; i < m; i++) { n       cout << ans[{arr[i].first, arr[i].second}]; n     } n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long MOD = 998244353; n double eps = 1e-12; n void solve() { n   long long n, x; n   cin >> n >> x; n   int a; n   map<int, int> mr; n   map<int, int> mc; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < x; j++) { n       cin >> a; n       if (a) { n         mr[i + 1]++; n         mc[j + 1]++; n       } n     } n   } n   int k1 = n - mr.size(), k2 = x - mc.size(); n   int l = min(k1, k2); n   if (l & 1) n     cout <<  Ashish  << endl; n   else n     cout <<  Vivek  << endl; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int n, m; n   cin >> n >> m; n   long long s = 0, x = 1; n   long long a[n]; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     s = s + a[i]; n   } n   s = s + m; n   sort(a, a + n); n   int y = max((s / n + min(x, s % n)), a[n - 1]); n   cout << y; n   cout <<     << a[n - 1] + m << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 3e5 + 1; n int N, Q, nums[MAXN], ans[MAXN], BIT[MAXN + 5]; n vector<pair<int, int> > queries[MAXN]; n void upd(int i) { n   i++; n   while (i > 0) { n     BIT[i]++; n     i -= i & -i; n   } n } n int get(int i) { n   i++; n   int ret = 0; n   while (i < N + 5) { n     ret += BIT[i]; n     i += i & -i; n   } n   return ret; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> N >> Q; n   for (int i = 1; i <= N; i++) { n     cin >> nums[i]; n     nums[i] = i - nums[i]; n   } n   for (int i = 0; i < Q; i++) { n     int x, y; n     cin >> x >> y; n     if (x + y >= N) { n       ans[i] = 0; n       continue; n     } n     queries[N - y].push_back(make_pair(1 + x, i)); n   } n   for (int i = 1; i <= N; i++) { n     if (nums[i] >= 0 && get(1) >= nums[i]) { n       int lo = 1; n       int hi = i; n       if (nums[i] == 0) lo = i; n       while (lo < hi - 1) { n         int m = (lo + hi) / 2; n         if (get(m) >= nums[i]) { n           lo = m; n         } else { n           hi = m; n         } n       } n       upd(lo); n     } n     for (auto it : queries[i]) { n       ans[it.second] = get(it.first); n     } n   } n   for (int i = 0; i < Q; i++) { n     cout << ans[i] <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n double start_moment = 0; n double get_runtime() { return 1.0 * clock() / CLOCKS_PER_SEC; } n void reset_timer() { start_moment = get_runtime(); } n double timer_time() { return get_runtime() - start_moment; } n void runtime() { cout << fixed << setprecision(5) << get_runtime() << endl; } n template <class T> n void read(vector<T> &a, int n) { n   T x; n   a.clear(); n   for (int i = 0; i < n; i++) { n     cin >> x; n     a.push_back(x); n   } n } n template <class T> n void write(vector<T> &a) { n   for (T x : a) cout << x <<    ; n   cout <<   n ; n } n const int N = 2003; n const int INF = 1000000000; n int T, len; n string second, t; n int cnt1[26], cnt2[26]; n int have_s[26][N]; n int have_t[26][N]; n int get(int *have, int x) { n   if (x < 0) return 0; n   return have[x]; n } n int get(int *have, int l, int r) { return get(have, r) - get(have, l - 1); } n int dp[N][N]; n int go(int x, int y) { n   if (dp[x][y] != INF) return dp[x][y]; n   if (second[x - 1] == t[y - 1]) dp[x][y] = min(dp[x][y], go(x - 1, y - 1)); n   dp[x][y] = min(dp[x][y], go(x - 1, y) + 1); n   if (get(have_s[t[y - 1] -  a ], x, len) > get(have_t[t[y - 1] -  a ], y, len)) n     dp[x][y] = min(dp[x][y], go(x, y - 1)); n   return dp[x][y]; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin >> T; n   while (T--) { n     memset(cnt1, 0, sizeof(cnt1)); n     memset(cnt2, 0, sizeof(cnt2)); n     cin >> len; n     cin >> second >> t; n     for (char c : second) cnt1[c -  a ]++; n     for (char c : t) cnt2[c -  a ]++; n     bool bad = 0; n     for (int i = 0; i < 26; i++) n       if (cnt1[i] != cnt2[i]) bad = 1; n     if (bad) { n       cout << -1 <<   n ; n       continue; n       ; n     } n     for (int i = 0; i <= len; i++) n       for (int j = 0; j <= len; j++) dp[i][j] = INF; n     memset(have_s, 0, sizeof(have_s)); n     memset(have_t, 0, sizeof(have_t)); n     for (char c =  a ; c <=  z ; c++) { n       for (int i = 0; i < len; i++) { n         have_s[c -  a ][i] = (c == second[i]); n         have_t[c -  a ][i] = (c == t[i]); n       } n       for (int i = 1; i < len; i++) { n         have_s[c -  a ][i] += have_s[c -  a ][i - 1]; n         have_t[c -  a ][i] += have_t[c -  a ][i - 1]; n       } n     } n     for (int i = 0; i <= len; i++) dp[0][i] = 0; n     cout << go(len, len) <<   n ; n   } n } n


#include <bits/stdc++.h> n #pragma warning(disable : 4996) n using namespace std; n const int mod = 1e9 + 7; n const int maxn = 1e5 + 5; n int m, q; n int val[maxn], sum1[maxn], sum2[maxn], las[maxn], pre[maxn], fl[maxn], n     res[maxn]; n struct no { n   int le, ri, id; n } qu[maxn]; n bool cmp(no n1, no n2) { return n1.ri < n2.ri; } n void solve() { n   int i; n   scanf( %d , &m); n   for (i = 1; i <= m; i++) { n     scanf( %d , &val[i]); n     pre[i] = las[val[i]]; n     las[val[i]] = i; n     if (i - pre[i] == pre[i] - pre[pre[i]]) { n       fl[i] = fl[pre[i]]; n     } else { n       fl[i] = pre[pre[i]]; n     } n   } n   scanf( %d , &q); n   for (i = 1; i <= q; i++) { n     scanf( %d%d , &qu[i].le, &qu[i].ri); n     qu[i].id = i; n   } n   sort(qu + 1, qu + q + 1, cmp); n   int j, k = 1; n   for (i = 1; i <= m; i++) { n     if (i == 10) { n       i++; n       i--; n     } n     for (j = i; j; j -= (j & (-j))) { n       sum1[j]++; n     } n     for (j = pre[i]; j; j -= (j & (-j))) { n       sum1[j]--; n     } n     for (j = fl[i]; j; j -= (j & (-j))) { n       sum2[j]++; n     } n     for (j = fl[pre[i]]; j; j -= (j & (-j))) { n       sum2[j]--; n     } n     while (qu[k].ri == i && k <= q) { n       int su1 = 0, su2 = 0; n       for (j = qu[k].le; j <= i; j += (j & (-j))) { n         su1 += sum1[j]; n         su2 += sum2[j]; n       } n       res[qu[k].id] = su1 + (su1 == su2); n       k++; n     } n   } n   for (i = 1; i <= q; i++) { n     printf( %d n , res[i]); n   } n } n int main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n #define MOD 998244353 n using namespace std; n  n #define rep(i, a, b) for(int i = a; i < (b); ++i) n #define rrep(i, a, b) for(int i = (a-1); i >= (b); --i) n #define trav(a, x) for(auto& a : x) n #define all(x) begin(x), end(x) n #define sz(x) (int)(x).size() n typedef long long ll; n typedef pair<int, int> pii; n typedef vector<int> vi; n  n void solve() { n  int n; n  cin >> n; n  vector<vector<int>> a(2*n, vector<int>(n)); n  vector<vector<set<int>>> S(n, vector<set<int>>(n)); n  rep(i,0,2*n) { n   rep(j,0,n) { n    cin >> a[i][j]; n    --a[i][j]; n    S[j][a[i][j]].insert(i); n   } n  } n  ll ans = 1; n  vector<bool> used(2*n, false); n  vector<int> side(2*n); n  int I = 0; n  while (true) { n   bool any = false; n   int r; n   int i = I; n   while (!any) { n    rep(j,0,n) { n     if (sz(S[i][j]) == 1) { n      any = true; n      r = *S[i][j].begin(); n      break; n     } n    } n    if (any) n     break; n    i = (i+1)%n; n    if (i == I) n     break; n   } n   I = i; n   if (!any) { n    rep(i,0,2*n) n     if (!used[i]) { n      r = i; n      any = true; n     } n    if (!any) n     break; n    ans = (ans*2) % MOD; n   } n   used[r] = true; n   side[r] = 0; n   set<int> toRemove; n   rep(l,0,n) { n    for (int k : S[l][a[r][l]]) { n     toRemove.insert(k); n    } n   } n   for (int k : toRemove) { n    used[k] = true; n    side[k] = k == r; n    rep(l,0,n) n     S[l][a[k][l]].erase(k); n   } n  } n  cout << ans << endl; n  rep(i,0,2*n) { n   if (side[i] == 1) { n    cout << i+1 <<    ; n   } n  } n  cout << endl; n } n  n int main() { n  cin.sync_with_stdio(0); cin.tie(0); n  cin.exceptions(cin.failbit); n  int t; n  cin >> t; n  while (t--) n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n long long int gcd(long long int a, long long int b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n int main() { n   vector<long long int> v1; n   vector<long long int> v2; n   long long int n, mo = 0, x, y, f; n   scanf( %lld , &n); n   for (long long int i = 0; i < n; i++) { n     scanf( %lld , &f); n     v1.push_back(f); n   } n   for (long long int i = 0; i < n; ++i) { n     v2.push_back(v1[i]); n     if (i + 1 == n) break; n     x = v1[i], y = v1[i + 1]; n     if (x < y) swap(x, y); n     if (gcd(x, y) != 1) { n       mo++; n       v2.push_back(1); n     } n   } n   printf( %lld n , mo); n   for (long long int i = 0; i < v2.size(); ++i) printf( %lld  , v2[i]); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int d, n, m; n const int M = 2e5; n pair<int, int> gas[M + 7]; n bool possible = true; n int pendingD, running; n int currentGas; n long long res = 0; n struct cmp { n   bool operator()(int c1, int c2) { n     if (gas[c1].second != gas[c2].second) n       return gas[c1].second > gas[c2].second; n     else n       return gas[c1].first < gas[c2].first; n   } n }; n priority_queue<int, vector<int>, cmp> heap; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> d >> n >> m; n   int r1, r2; n   for (int i = 1; i <= m; i++) { n     cin >> r1 >> r2; n     gas[i] = {r1, r2}; n   } n   sort(gas + 1, gas + m + 1); n   gas[0] = {0, 0}; n   gas[m + 1] = {d, 0}; n   for (int i = 1; i <= m + 1; i++) n     if (gas[i].first - gas[i - 1].first > n) { n       possible = false; n       break; n     } n   if (!possible) { n     cout << -1; n     exit(0); n   } n   pendingD = 0; n   running = 1; n   currentGas = 0; n   while (pendingD <= m) { n     for (int i = running; i <= m + 1; i++) n       if (gas[i].first - gas[pendingD].first > n) { n         running = i; n         break; n       } else { n         heap.push(i); n         if (gas[i].second < gas[pendingD].second) { n           running = i + 1; n           break; n         } n       } n     while (heap.top() <= pendingD) heap.pop(); n     if (gas[heap.top()].second <= gas[pendingD].second) { n       res += 1LL * gas[pendingD].second * n              (gas[heap.top()].first - gas[pendingD].first - currentGas); n       currentGas = 0; n     } else { n       res += 1LL * gas[pendingD].second * (n - currentGas); n       currentGas = n - (gas[heap.top()].first - gas[pendingD].first); n     } n     pendingD = heap.top(); n     heap.pop(); n     running = max(running, pendingD + 1); n   } n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, sum = 0; n   int num[1000]; n   int x, y; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> num[i]; n     sum += num[i]; n   } n   cin >> x >> y; n   int cur = 0; n   for (int i = 1; i <= n; i++) { n     if (cur >= x && cur <= y && (sum - cur) >= x && (sum - cur) <= y) { n       cout << i << endl; n       return 0; n     } n     cur += num[i]; n   } n   cout << 0 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int a, b; n   scanf( %d%d , &a, &b); n   int c[3] = {0, 0, 0}; n   for (int i = 1; i <= 6; ++i) { n     int d1 = abs(i - a), d2 = abs(i - b); n     if (d1 < d2) c[0]++; n     if (d1 == d2) c[1]++; n     if (d1 > d2) c[2]++; n   } n   for (int i = 0; i < 3; ++i) { n     printf( %d  , c[i]); n   } n } n int main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T, typename U> n inline void smin(T &a, const U &b) { n   if (a > b) a = b; n } n template <typename T, typename U> n inline void smax(T &a, const U &b) { n   if (a < b) a = b; n } n template <class T> n inline void gn(T &first) { n   char c, sg = 0; n   while (c = getchar(), (c >  9  || c <  0 ) && c !=  - ) n     ; n   for ((c ==  -  ? sg = 1, c = getchar() : 0), first = 0; c >=  0  && c <=  9 ; n        c = getchar()) n     first = (first << 1) + (first << 3) + c -  0 ; n   if (sg) first = -first; n } n template <class T1, class T2> n inline void gn(T1 &x1, T2 &x2) { n   gn(x1), gn(x2); n } n int power(int a, int b, int m, int ans = 1) { n   for (; b; b >>= 1, a = 1LL * a * a % m) n     if (b & 1) ans = 1LL * ans * a % m; n   return ans; n } n const int mod = 1000000007; n int pt[100100], p2[100100]; n int sum[100100], dp[100100], mn[100100], mx[100100]; n char s[100100], t[100100]; n int calc(char *s, int n) { n   int ans = 0, val = 0, pre = 0; n   for (int i = 0; i < n; i++) { n     int v = (val + 4LL * pt[n - i - 1]) % mod; n     if (s[i] ==  7 ) { n       ans = (ans + (long long)v * v % mod * (p2[n - i - 1] - 1) + n              (long long)v * n                  (2LL * sum[n - i - 1] - mn[n - i - 1] - mx[n - i - 1]) + n              dp[n - i - 1]) % n             mod; n       ans = (ans + mod) % mod; n       if (pre) n         ans = (ans + (long long)(pre + mx[n - i]) * (v + mn[n - i - 1])) % mod; n     } n     if (s[i] ==  7 ) n       pre = (val + 4LL * pt[n - i - 1]) % mod; n     else if (pre) n       pre = (pre + 7LL * pt[n - i - 1]) % mod; n     val = (val + (long long)(s[i] -  0 ) * pt[n - i - 1]) % mod; n   } n   ans = (ans + (long long)val * pre) % mod; n   return ans; n } n int main() { n   scanf( %s %s , s, t); n   int n = strlen(s); n   for (int i = pt[0] = p2[0] = 1; i <= n; i++) n     pt[i] = 10LL * pt[i - 1] % mod, p2[i] = p2[i - 1] * 2 % mod; n   for (int i = 1; i <= n; i++) { n     dp[i] = n         ((long long)pt[i - 1] * pt[i - 1] % mod * (4 * 4 + 7 * 7) % mod * n              (p2[i - 1] - 1) + n          11LL * pt[i - 1] % mod * (2LL * sum[i - 1] - mn[i - 1] - mx[i - 1]) + n          2LL * dp[i - 1] + n          (4LL * pt[i - 1] + mx[i - 1]) % mod * n              (7LL * pt[i - 1] % mod + mn[i - 1])) % n         mod; n     dp[i] = (dp[i] + mod) % mod; n     mn[i] = (10LL * mn[i - 1] + 4) % mod; n     mx[i] = (10LL * mx[i - 1] + 7) % mod; n     sum[i] = (2LL * sum[i - 1] + 11LL * pt[i - 1] % mod * p2[i - 1]) % mod; n   } n   cout << (calc(t, n) - calc(s, n) + mod) % mod << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   int p[n]; n   for (int i = 0; i < n; i++) cin >> p[i]; n   sort(p, p + n); n   map<string, int> mm; n   string x; n   for (int i = 0; i < m; ++i) { n     cin >> x; n     if (mm.find(x) == mm.end()) n       mm.insert(make_pair(x, 1)); n     else n       mm[x]++; n   } n   vector<int> v; n   for (auto i = mm.begin(); i != mm.end(); i++) { n     v.push_back(i->second); n   } n   sort(v.begin(), v.end()); n   reverse(v.begin(), v.end()); n   long long int sum = 0; n   for (int i = 0; i < v.size(); i++) { n     sum += v[i] * p[i]; n   } n   cout << sum <<    ; n   sum = 0; n   for (int i = 0; i < v.size(); i++) { n     sum += v[i] * p[n - 1 - i]; n   } n   cout << sum << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100010; n vector<vector<int> > adj(maxn), adj2(maxn); n vector<pair<int, int> > vs; n bool vis[maxn]; n void dfs(int pv, int v) { n   vis[v] = 1; n   for (int i = 0; i < adj[v].size(); i++) { n     int u = adj[v][i]; n     if (vis[u]) continue; n     vs.push_back(make_pair(v, u)); n     adj2[pv].push_back(vs.size() - 1); n     dfs(vs.size() - 1, u); n   } n } n int main() { n   int n, a, b; n   cin >> n; n   for (int i = 1; i <= n - 1; i++) { n     cin >> a >> b; n     adj[a].push_back(b); n     adj[b].push_back(a); n   } n   memset(vis, 0, maxn); n   vs.push_back(make_pair(-1, -1)); n   int s; n   for (int i = 1; i <= n; i++) n     if (adj[i].size() == 1) { n       s = i; n       break; n     } n   vis[s] = 1; n   for (int i = 0; i < adj[s].size(); i++) { n     int u = adj[s][i]; n     vs.push_back(make_pair(s, u)); n     dfs(vs.size() - 1, u); n   } n   cout << n - 1 << endl; n   for (int i = 1; i <= n - 1; i++) { n     cout <<  2   << vs[i].first <<     << vs[i].second << endl; n   } n   for (int i = 1; i <= n - 1; i++) { n     for (int j = 0; j < adj2[i].size(); j++) n       cout << i <<     << adj2[i][j] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long mod = 1000000007; n long long z[3000019], arr[3000019], pre[3000019]; n void fnc(string s) { n   memset(z, 0, sizeof(z)); n   long long l = 0, r = 0; n   long long n = s.size(); n   for (long long i = 1; i <= n; i++) { n     if (i <= r) z[i] = min(r - i + 1, z[i - l]); n     while (i + z[i] <= n && s[z[i]] == s[i + z[i]]) z[i]++; n     if (i + z[i] - 1 > r) { n       l = i; n       r = i + z[i] - 1; n     } n   } n   z[0] = n; n   return; n } n signed main() { n   std::ios::sync_with_stdio(false); n   long long n, m, q, i, l, r; n   string s, t, str; n   cin >> n >> m >> q; n   cin >> s >> t; n   str = t +  $  + s; n   fnc(str); n   for (i = m + 1; i <= n + m; i++) arr[i - m] = (z[i] == m); n   for (i = 1; i <= n; i++) pre[i] = arr[i] + pre[i - 1]; n   while (q--) { n     cin >> l >> r; n     if (r - l + 1 < m) { n       cout << 0 << endl; n       continue; n     } n     cout << pre[r - m + 1] - pre[l - 1] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int tc, cs = 1, n, k, a, b, c, d; n   while (cin >> n >> k) { n     cin >> a >> b >> c >> d; n     if (k < n + 1 || n == 4) { n       printf( %d n , -1); n     } else { n       printf( %d %d  , a, c); n       for (int i = 1; i < n + 1; i++) { n         if (i != a && i != b && i != c && i != d) { n           printf( %d  , i); n         } n       } n       printf( %d %d n , d, b); n       printf( %d %d  , c, a); n       for (int i = 1; i < n + 1; i++) { n         if (i != a && i != b && i != c && i != d) { n           printf( %d  , i); n         } n       } n       printf( %d %d n , b, d); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void File() { n   freopen( output.txt ,  w , stdout); n   freopen( input.txt ,  r , stdin); n } n void fast() { n   ios::sync_with_stdio(NULL); n   cout.tie(NULL); n   cin.tie(NULL); n } n const double pi = 2 * acos(0.0); n const int nn = 5e2 + 5; n const int oo = 0x3f3f3f3f; n int main() { n   fast(); n   int n, ans = 0; n   pair<int, int> arr[nn]; n   cin >> n; n   for (int i = 0; i < n; i++) cin >> arr[i].first >> arr[i].second; n   for (int i = 0; i < n; i++) { n     int cnt[4] = {}; n     for (int j = 0; j < n; j++) { n       if (i == j) continue; n       if (arr[i].first > arr[j].first && arr[i].second == arr[j].second) n         cnt[0]++; n       else if (arr[i].first < arr[j].first && arr[i].second == arr[j].second) n         cnt[1]++; n       else if (arr[i].first == arr[j].first && arr[i].second > arr[j].second) n         cnt[2]++; n       else if (arr[i].first == arr[j].first && arr[i].second < arr[j].second) n         cnt[3]++; n       if (cnt[0] && cnt[1] && cnt[2] && cnt[3]) { n         ans++; n         break; n       } n     } n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n   n using namespace std; n using ll=long long; n using pii=pair<int,int>; n  n int np[17][200000]; n int pre[(1<<17)+1]; n  n int main() { n  int n,k; n  cin >> n >> k; n     string s; n     cin >> s; n  int l=0,r=n/k; n     while (l<r) { n         int m=(l+r+1)/2; n         for (int j=0;j<k;j++) { n             int cnt=0, nextpos=n+1; n             for (int i=n-1;i>=0;i--) { n                 if (s[i]== ?  || s[i]== a +j) cnt++; n                 else cnt=0; n                 if (cnt>=m) nextpos=i; n                 np[j][i]=nextpos; n             } n         } n         /*cerr << m << endl; n         for (int j=0;j<k;j++) { n             cerr << (char)( a +j) <<  :  ; n             for (int i=0;i<n;i++) cerr << np[j][i] <<    ; n             cerr << endl; n         }*/ n         pre[0]=0; n         for (int i=1;i<(1<<k);i++) { n             pre[i]=n+1; n             /*for (int j=0;j<k;j++) if (i&(1<<j)) { n                 cerr << (char)( a +j) <<  , ; n             } n             cerr <<   t  << i << endl;*/ n             for (int j=0;j<k;j++) if (i&(1<<j)) { n                 int x=(i&(~(1<<j))); n                 //cerr << (char)( a +j) <<    << x <<     << pre[x] <<     << np[j][pre[x]] << endl; n                 if (pre[x]<n) pre[i]=min(pre[i],np[j][pre[x]]+m); n                 //cerr << (char)( a +j) <<     << pre[i] << endl; n             } n         } n         /*for (int i=0;i<(1<<k);i++) { n             for (int j=0;j<k;j++) if (i&(1<<j)) { n                 cerr << (char)( a +j) <<  , ; n             } n             cerr <<   t  << pre[i] << endl; n         }*/ n         if (pre[(1<<k)-1]<=n) l=m; n         else r=m-1; n     } n     printf( %d n ,l); n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n const int MAX = 1005; n char ch[MAX]; n int ans[MAX]; n int main() { n   int n; n   scanf( %d%s , &n, ch); n   ans[0] = 1; n   for (int i = 1; i < n; i++) { n     if (ch[i - 1] ==  = ) n       ans[i] = ans[i - 1]; n     else if (ch[i - 1] ==  L ) { n       ans[i] = 1; n       for (int j = i - 1; j >= 0; j--) { n         if (ch[j] ==  L  && ans[j] <= ans[j + 1]) n           ans[j] = ans[j + 1] + 1; n         else if (ch[j] ==  =  && ans[j] != ans[j + 1]) n           ans[j] = ans[j + 1]; n         else n           break; n       } n     } else n       ans[i] = ans[i - 1] + 1; n   } n   for (int i = 0; i < n; i++) { n     if (i == 0) n       printf( %d , ans[i]); n     else n       printf(  %d , ans[i]); n   } n   printf(  n ); n } n


#include <bits/stdc++.h> n using namespace std; n void printAll(int n, ...) { n   if (1) { n     va_list ap; n     va_start(ap, n); n     for (int i = 1; i <= n; i++) cout << va_arg(ap, long long int) <<    ; n     cout << endl; n     va_end(ap); n   } n } n template <typename T> n string toString(T n) { n   stringstream ss; n   ss << n; n   string s; n   ss >> s; n   return s; n } n void init() { n   cout << fixed << setprecision(0); n   ios_base::sync_with_stdio(false); n   cin.tie(0); n } n bool isInteger(double n) { n   if (n - (long long int)n == 0) n     return true; n   else n     return false; n } n int main() { n   init(); n   long long int i, j, k; n   int n, w; n   cin >> n >> w; n   int x; n   int low = 0; n   int high = w; n   int lowsum = 0; n   int highsum = w; n   for (int i = 0; i < n; i++) { n     cin >> x; n     if (lowsum + x < 0) { n       low -= (lowsum + x); n       lowsum = 0; n     } else n       lowsum += x; n     if (highsum + x > w) { n       high = high + (w - highsum - x); n       highsum = w; n     } else n       highsum += x; n   } n   high < low ? cout << 0 << endl : cout << high - low + 1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dp[100000]; n int main() { n   int n, a, b, c; n   cin >> n >> a >> b >> c; n   dp[a] = dp[b] = dp[c] = 1; n   int x = min(a, min(b, c)); n   for (int i = x; i <= n; i++) { n     if (i - a >= 0 && dp[i - a] > 0) dp[i] = max(dp[i], dp[i - a] + 1); n     if (i - b >= 0 && dp[i - b] > 0) dp[i] = max(dp[i], dp[i - b] + 1); n     if (i - c >= 0 && dp[i - c] > 0) dp[i] = max(dp[i], dp[i - c] + 1); n   } n   cout << dp[n]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int L[100007], R[100007]; n int n; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d%d , L + i, R + i); n   } n   sort(L + 1, L + n + 1); n   sort(R + 1, R + n + 1); n   long long ans = 0; n   for (int i = 1; i <= n; i++) { n     ans += max(L[i], R[i]); n   } n   printf( %I64d n , ans + n); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int ball[100005]; n int main() { n   int n, m; n   cin >> n >> m; n   if (m % 2) { n     int basket = m / 2; n     while (n--) { n       cout << basket + 1 <<   n ; n       basket = m - basket - 1; n       if (basket <= m / 2) basket--; n       if (basket < 0) basket = m / 2; n     } n   } else { n     int basket = m / 2 - 1; n     while (n--) { n       cout << basket + 1 <<   n ; n       basket = m - basket - 1; n       if (basket < m / 2) basket--; n       if (basket < 0) basket = m / 2 - 1; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1e18; n const int N = 1000 + 10; n int n, l, k; n double dp[202][202][404]; n double pro[300]; n int a[N]; n double solve(int i, int sofar, int cap) { n   if (i == n + 1) { n     return sofar >= l && cap >= 0; n   } n   double &ret = dp[i][sofar][cap + 200]; n   if (ret == ret) return ret; n   int toref = min(200, cap + a[i]); n   toref = max(toref, -200); n   double c1 = solve(i + 1, sofar + 1, toref) * pro[i]; n   double c2 = solve(i + 1, sofar, cap) * (1 - pro[i]); n   return ret = c1 + c2; n } n int main() { n   cin >> n >> l >> k; n   for (int i = 1; i <= n; ++i) { n     cin >> pro[i]; n     pro[i] /= 100.0; n   } n   for (int i = 1; i <= n; ++i) cin >> a[i]; n   memset(dp, -1, sizeof(dp)); n   cout << fixed << setprecision(10) << solve(1, 0, k) << endl; n } n


#include <bits/stdc++.h> n using namespace std; n using namespace std; n const int N = 3030; n const int INF = (int)2e9; n int n, m; n int len[N]; n int f[N][N]; n vector<int> g[N]; n vector<int> gr[N]; n vector<pair<int, int> > have[N]; n vector<pair<int, int> > gave[N]; n queue<int> q; n void bfs(int v) { n   for (int i = 1; i <= n; i++) len[i] = INF; n   len[v] = 0; n   q.push(v); n   while (!q.empty()) { n     int ver = q.front(); n     q.pop(); n     for (int i = 0; i < (int)g[ver].size(); i++) { n       int to = g[ver][i]; n       if (len[to] > len[ver] + 1) { n         len[to] = len[ver] + 1; n         q.push(to); n       } n     } n   } n   for (int i = 1; i <= n; i++) { n     f[v][i] = len[i]; n     if (len[i] != INF && i != v) { n       gave[i].push_back(make_pair(len[i], v)); n     } n   } n   for (int i = 1; i <= n; i++) len[i] = INF; n   len[v] = 0; n   q.push(v); n   while (!q.empty()) { n     int ver = q.front(); n     q.pop(); n     for (int i = 0; i < (int)gr[ver].size(); i++) { n       int to = gr[ver][i]; n       if (len[to] > len[ver] + 1) { n         len[to] = len[ver] + 1; n         q.push(to); n       } n     } n   } n   for (int i = 1; i <= n; i++) { n     if (len[i] != INF && i != v) { n       have[i].push_back(make_pair(len[i], v)); n     } n   } n } n int main() { n   scanf( %d%d , &n, &m); n   int u, v; n   for (int i = 1; i <= m; i++) { n     scanf( %d%d , &u, &v); n     g[u].push_back(v); n     gr[v].push_back(u); n   } n   for (int i = 1; i <= n; i++) { n     bfs(i); n   } n   for (int i = 1; i <= n; i++) { n     sort(have[i].begin(), have[i].end()); n     reverse(have[i].begin(), have[i].end()); n     sort(gave[i].begin(), gave[i].end()); n     reverse(gave[i].begin(), gave[i].end()); n   } n   int res = 0; n   int a, b, c, d, sz, ptr, cost; n   for (int i = 1; i <= n; i++) { n     sz = min(3, (int)gave[i].size()); n     for (int idx = 0; idx < sz; idx++) { n       for (int j = 1; j <= n; j++) { n         if (j == i) continue; n         if (f[i][j] == INF) continue; n         ptr = 0; n         while (true) { n           if (ptr >= (int)have[j].size()) break; n           if (have[j][ptr].second == i || n               have[j][ptr].second == gave[i][idx].second || n               j == gave[i][idx].second) { n             ++ptr; n             continue; n           } else { n             cost = gave[i][idx].first + f[i][j] + have[j][ptr].first; n             if (cost > res) { n               res = cost; n               a = gave[i][idx].second; n               b = i; n               c = j; n               d = have[j][ptr].second; n             } n             break; n           } n         } n       } n     } n   } n   printf( %d %d %d %d n , a, b, c, d); n   return 0; n } n