#include <bits/stdc++.h> n using namespace std; n const long long mod = 1000000007; n const double PI = acos(-1.0); n const double epsilon = PI / 180.0; n long long powmod(long long a, long long b) { n   long long res = 1; n   a %= mod; n   assert(b >= 0); n   for (; b; b >>= 1) { n     if (b & 1) res = res * a % mod; n     a = a * a % mod; n   } n   return res; n } n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n const int N = 3e3 + 10, M = 1e6 + 10; n int n; n int a[N]; n long long num[N][N], f[N][N]; n int main() { n   int _; n   cin >> _; n   while (_--) { n     memset(num, 0, sizeof num); n     memset(f, 0, sizeof f); n     cin >> n; n     for (int i = 1; i < n + 1; i++) cin >> a[i]; n     for (int i = 1; i < n + 1; i++) { n       for (int j = n + 1 - 1; j >= i + 1; j--) { n         num[i][j] = num[i][j + 1]; n         if (a[i] == a[j]) num[i][j]++; n       } n     } n     for (int k = 1; k < n + 1; k++) { n       for (int i = 1; i < k + 1; i++) { n         f[i][k] = f[i - 1][k] + num[i][k + 1]; n       } n     } n     long long ans = 0; n     for (int i = 1; i < n + 1; i++) { n       for (int j = i + 1; j < n + 1; j++) { n         if (a[i] != a[j]) continue; n         ans += (f[j - 1][j] - f[i][j]); n       } n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using namespace chrono; n const int infinity = (int)1e9 + 42; n const int64_t llInfinity = (int64_t)1e18 + 256; n const int module = (int)1e9 + 7; n const long double eps = 1e-8; n mt19937_64 randGen(system_clock().now().time_since_epoch().count()); n inline void raiseError(string errorCode) { n   cerr <<  Error :   << errorCode << endl; n   exit(42); n } n signed main() { n   ios_base::sync_with_stdio(false); n   int n, m; n   cin >> n >> m; n   vector<vector<int> > g(n); n   for (int i = 0; i < m; i++) { n     int a, b; n     cin >> a >> b; n     a--; n     b--; n     g[a].push_back(b); n     g[b].push_back(a); n   } n   vector<int> d(n, INT_MAX); n   vector<int> p(n, -1); n   queue<int> q; n   q.push(0); n   d[0] = 0; n   while (!q.empty()) { n     int v = q.front(); n     q.pop(); n     for (int to : g[v]) { n       if (d[to] == INT_MAX) { n         d[to] = d[v] + 1; n         p[to] = v; n         q.push(to); n       } n     } n   } n   int ans1 = d[n - 1]; n   int idx2 = -1; n   for (int i = 0; i < n; i++) { n     if (d[i] == 2) { n       idx2 = i; n       break; n     } n   } n   if (ans1 > 4 && idx2 >= 0) { n     cout << 4 << endl; n     cout << 1 <<     << p[idx2] + 1 <<     << idx2 + 1 <<     << 1 <<     << n n          << endl; n     return 0; n   } n   if (ans1 > 5) { n     vector<char> used(n, false); n     vector<vector<int> > komps; n     vector<int> kompId(n, -1); n     used[0] = true; n     function<void(int)> dfs = [&](int v) { n       if (used[v]) { n         return; n       } n       used[v] = true; n       kompId[v] = (int)komps.size() - 1; n       komps.back().push_back(v); n       for (int to : g[v]) { n         dfs(to); n       } n     }; n     for (int i : g[0]) { n       if (!used[i]) { n         komps.emplace_back(); n         dfs(i); n       } n     } n     for (int i = 0; i < n; i++) { n       sort(g[i].begin(), g[i].end()); n     } n     for (auto &komp : komps) { n       sort(komp.begin(), komp.end()); n       bool klika = true; n       int ngsz = (int)komp.size(); n       int x = -1, y = -1; n       for (int i : komp) { n         if ((int)g[i].size() == ngsz) { n           continue; n         } n         for (int j : komp) { n           if (i == j) { n             continue; n           } n           if (!binary_search(g[i].begin(), g[i].end(), j)) { n             x = i; n             y = j; n             break; n           } n         } n         if (x < 0 || y < 0) { n           for (;;) n             ; n         } n         klika = false; n         break; n       } n       if (klika) { n         continue; n       } n       int z = -1; n       vector<int> xReach; n       for (int i = 1; i < n; i++) { n         if (i == x || i == y) { n           continue; n         } n         bool findX = binary_search(g[i].begin(), g[i].end(), x); n         bool findY = binary_search(g[i].begin(), g[i].end(), y); n         if (findX && findY) { n           z = i; n           break; n         } else if (findX) { n           xReach.push_back(i); n         } n       } n       if (z < 0) { n         z = -1; n         for (int i : xReach) { n           for (int j : g[i]) { n             if (j == x || j == 0 || n                 binary_search(xReach.begin(), xReach.end(), j)) { n               continue; n             } n             y = j; n             z = i; n             break; n           } n           if (z >= 0) { n             break; n           } n         } n         assert(z >= 0); n       } n       cout << 5 << endl; n       cout << 1 <<     << x + 1 <<     << z + 1 <<     << y + 1 <<     << x + 1 n            <<     << n << endl; n       return 0; n     } n   } n   if (ans1 == INT_MAX) { n     cout << -1 << endl; n     return 0; n   } n   cout << ans1 << endl; n   vector<int> res; n   int kur = n - 1; n   while (kur >= 0) { n     res.push_back(kur + 1); n     kur = p[kur]; n   } n   reverse(res.begin(), res.end()); n   for (int i : res) { n     cout << i <<    ; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long cum[(int)1e6], input[(int)1e6], k, ans; n char c; n int sz, t; n map<long long, vector<int>> pos; n int main() { n   cin >> sz >> k; n   for (int i = 0; i < sz; i++) { n     scanf( %c%d , &c, &t); n     input[i] = t; n     if (i == 0) { n       cum[0] = t; n     } else { n       cum[i] = cum[i - 1] + input[i]; n     } n     pos[cum[i]].push_back(i); n   } n   for (long long target = 1; abs(target) <= 1e16; target *= k) { n     for (int i = 0; i < sz; i++) { n       if (i == 0) { n         ans += pos[target].size(); n       } else { n         ans += pos[target + cum[i - 1]].end() - n                lower_bound(pos[target + cum[i - 1]].begin(), n                            pos[target + cum[i - 1]].end(), i); n       } n     } n     if ((k == 1) || (k == -1 && target == k)) { n       cout << ans << endl; n       return 0; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n const double pi = acos(-1); n const int MOD = 1e9 + 7; n const long long INF = 1e18 + 7; n const int MAXN = 1e3 + 5; n const double eps = 1e-9; n using namespace std; n long long a[MAXN], dist[MAXN]; n pair<long long, long long> loc[MAXN]; n int par[MAXN]; n long long cost(int x, int y, long long d) { n   return d * (abs(loc[x].first - loc[y].first) + n               abs(loc[x].second - loc[y].second)); n } n int main() { n   long long n, d; n   scanf( %I64d , &(n)), scanf( %I64d , &(d)); n   for (int i = 1; i + 1 < n; i++) scanf( %I64d , &(a[i])); n   for (int i = 0; i < n; i++) n     scanf( %I64d , &(loc[i].first)), scanf( %I64d , &(loc[i].second)), n         dist[i] = INF; n   dist[0] = 0; n   priority_queue<pair<long long, long long>, n                  vector<pair<long long, long long> >, n                  greater<pair<long long, long long> > > n       pq; n   pq.push({0, 0}); n   while (!pq.empty()) { n     pair<long long, long long> cur = pq.top(); n     pq.pop(); n     for (int i = 0; i < n; i++) { n       if (i == cur.second) continue; n       long long c = cost(i, cur.second, d); n       if (dist[i] > dist[cur.second] + c - a[i]) { n         dist[i] = dist[cur.second] + c - a[i]; n         pq.push({dist[i], i}); n         par[i] = cur.second; n       } n     } n   } n   printf( %I64d n , (dist[n - 1])); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f, oo = inf; n inline long long read() { n   register long long x = 0, f = 1; n   register char c = getchar(); n   for (; !isdigit(c); c = getchar()) n     if (c ==  - ) f = -1; n   for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48); n   return x * f; n } n void write(long long x) { n   if (x < 0) x = -x, putchar( - ); 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 const long long maxn = 200005; n long long n, a[maxn], k; n bool check(long long m) { n   long double sum = 0, s; n   long long p, q; n   for (register long long i = (0); i < (n); ++i) { n     if (a[i] == 0) continue; n     q = n - 1 - i + m - 1; n     p = m - 1; n     p = min(p, q - p); n     s = a[i]; n     while (p) { n       s = s * q / p; n       p--, q--; n       if (s >= k) return true; n     } n     sum += s; n     if (sum >= k) return true; n   } n   return false; n } n long long solve(long long l, long long r) { n   long long mid, ans; n   while (l <= r) { n     mid = (l + r) >> 1; n     if (check(mid)) { n       ans = mid; n       r = mid - 1; n     } else n       l = mid + 1; n   } n   return ans; n } n signed main() { n   n = read(), k = read(); n   for (register long long i = (0); i < (n); ++i) { n     a[i] = read(); n     if (a[i] >= k) { n       puts( 0 ); n       return 0; n     } n   } n   writeln(solve(1, k)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int sum[100010], ans[100010]; n int data[100010]; n bool sign[100010]; n int main() { n   int n, m, c; n   while (scanf(  %d %d %d , &n, &m, &c) == 3) { n     for (int i = 1; i <= n; i++) { n       scanf(  %d , &ans[i]); n     } n     for (int i = 1; i <= m; i++) { n       scanf(  %d , &sum[i]); n       sum[i] += sum[i - 1]; n     } n     memset(sign, 0, sizeof(sign)); n     for (int i = 1; i <= m && i <= n; i++) { n       if (i + m - 1 <= n) { n         ans[i] += sum[i]; n         sign[i] = 1; n       } else { n         ans[i] += sum[i] - sum[i + m - 1 - n]; n         sign[i] = 1; n       } n     } n     for (int i = n; n - i + 1 <= m && i >= 1 && !sign[i]; i--) { n       if (i - m + 1 >= 1) { n         ans[i] += sum[m] - sum[m + i - n - 1]; n         sign[i] = 1; n       } else { n         ans[i] += sum[m] - sum[m + i - n - 1] - (sum[m] - sum[i]); n         sign[i] = 1; n       } n     } n     for (int i = 1; i <= n; i++) { n       if (sign[i]) continue; n       ans[i] += sum[m]; n     } n     for (int i = 1; i < n; i++) { n       printf( %d  , ans[i] % c); n     } n     printf( %d n , ans[n] % c); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> v[1000005]; n int n, ar[100005]; n int main() { n   cin >> n; n   set<int> st; n   for (int i = 0; i < n; i++) { n     cin >> ar[i]; n     st.insert(ar[i]); n     v[ar[i]].push_back(i + 1); n   } n   vector<int> fr(st.begin(), st.end()); n   int mx = 0, l = 1, r = n, mnd = INT_MAX; n   for (int i = 0; i < fr.size(); i++) { n     int x = fr[i]; n     if (v[fr[i]].size() >= mx) { n       if (mx < v[fr[i]].size() || v[x][v[x].size() - 1] - v[x][0] < mnd) { n         l = v[x][0]; n         r = v[x][v[x].size() - 1]; n         mnd = v[x][v[x].size() - 1] - v[x][0]; n         mx = v[x].size(); n       } n     } n   } n   cout << l <<     << r << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100005; n struct Edge { n   int to, next; n } e[N << 1]; n int in[N], head[N], sz; n int del[N], leg[N]; n int cnt[N]; n int res, n; n void addedge(int u, int v) { n   e[sz].to = v; n   e[sz].next = head[u]; n   head[u] = sz++; n } n void init() { n   memset(head, -1, sizeof(head)); n   memset(in, 0, sizeof(in)); n   memset(del, 0, sizeof(del)); n   memset(cnt, 0, sizeof(cnt)); n   sz = 0; n   res = 1; n } n void dfs1(int u, int f = 0) { n   int f1 = 0, f2 = 0; n   for (int i = head[u]; i != -1; i = e[i].next) { n     int v = e[i].to; n     if (v == f) continue; n     f1 = 1; n     dfs1(v, u); n     if (del[v]) f2 = 1; n   } n   if (!f1 || (f2 && in[u] < 3)) del[u] = 1; n } n void calc(int u) { n   leg[u] = 0; n   for (int i = head[u]; i != -1; i = e[i].next) { n     int v = e[i].to; n     if (del[v]) leg[u]++; n   } n   leg[u] = min(leg[u], 2); n } n bool check(int u) { n   int cnt = 0; n   for (int i = head[u]; i != -1; i = e[i].next) { n     int v = e[i].to; n     if (!del[v] && in[v] - leg[v] > 1) ++cnt; n   } n   return cnt > 2; n } n void solve() { n   init(); n   int x, y; n   for (int i = 1; i < n; i++) { n     scanf( %d%d , &x, &y); n     addedge(x, y); n     addedge(y, x); n     in[x]++; n     in[y]++; n   } n   int s = 0; n   for (int i = 1; i <= n; i++) n     if (in[s] < in[i]) s = i; n   if (in[s] <= 2) { n     printf( Yes n ); n     return; n   } n   dfs1(s); n   for (int i = 1; i <= n; i++) calc(i); n   int flag = 0; n   for (int i = 1; i <= n; i++) flag |= check(i); n   printf(!flag ?  Yes n  :  No n ); n } n int main() { n   while (~scanf( %d , &n)) solve(); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int t; n   scanf( %d , &t); n   int a[t], b[t], c[t]; n   for (int i = 0; i < t; i++) scanf( %d %d %d , &a[i], &b[i], &c[i]); n   for (int i = 0; i < t; i++) { n     if (a[i] == b[i] && b[i] == c[i]) { n       printf( YES n ); n       printf( %d %d %d n , a[i], a[i], a[i]); n     } else if (a[i] == b[i] && a[i] > c[i]) { n       printf( YES n ); n       printf( %d %d %d n , a[i], c[i], c[i]); n     } else if (b[i] == c[i] && b[i] > a[i]) { n       printf( YES n ); n       printf( %d %d %d n , b[i], a[i], a[i]); n     } else if (a[i] == c[i] && a[i] > b[i]) { n       printf( YES n ); n       printf( %d %d %d n , a[i], b[i], b[i]); n     } else { n       printf( NO n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct query { n   long long int l; n   long long int r; n   long long int i; n }; n query Q[200001]; n long long int arr[200001]; n long long int f[1000001]; n long long int ans[200001]; n long long int cnt; n bool compare(query a, query b) { n   if ((a.l / 448) != (b.l / 448)) return a.l / 448 < b.l / 448; n   return a.r < b.r; n } n void add(long long int a) { n   long long int x = f[arr[a]] * f[arr[a]] * arr[a]; n   cnt -= x; n   f[arr[a]]++; n   x = f[arr[a]] * f[arr[a]] * arr[a]; n   cnt += x; n } n void remove(long long int a) { n   long long int x = f[arr[a]] * f[arr[a]] * arr[a]; n   cnt -= x; n   f[arr[a]]--; n   x = f[arr[a]] * f[arr[a]] * arr[a]; n   cnt += x; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long int n, i, q; n   cin >> n >> q; n   for (i = 0; i < n; i++) cin >> arr[i]; n   for (i = 0; i < q; i++) { n     cin >> Q[i].l >> Q[i].r; n     Q[i].i = i; n     Q[i].l--; n     Q[i].r--; n   } n   sort(Q, Q + q, compare); n   long long int cl = 0, cr = -1; n   for (i = 0; i < q; i++) { n     long long int L = Q[i].l; n     long long int R = Q[i].r; n     while (cl > L) { n       cl--; n       add(cl); n     } n     while (cr < R) { n       cr++; n       add(cr); n     } n     while (cl < L) { n       remove(cl); n       cl++; n     } n     while (cr > R) { n       remove(cr); n       cr--; n     } n     ans[Q[i].i] = cnt; n   } n   for (i = 0; i < q; i++) { n     cout << ans[i] <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n = 0, a = 0; n   cin >> n; n   int* arr = new int[n]; n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n   } n   sort(arr, arr + n); n   for (int j = 0; j < n; j++) { n     cout << arr[j] <<    ; n   } n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const int inf = (int)1e9; n const long long mod = (long long)1e9 + 7; n const double eps = (double)1e-9; n const double pi = acos(-1.0); n const int dx[] = {0, 0, 1, 0, -1}; n const int dy[] = {0, 1, 0, -1, 0}; n const int N = 100500; n int n, ans, a[N], dp[N]; n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   ans = inf; n   for (int i = 1; i <= n; i++) { n     dp[a[i]] = dp[a[i] - 1] + 1; n     ans = min(ans, n - dp[a[i]]); n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long double PI = 3.14159265359; n long double DEL = 1e-10; n int mod = 1000000007; n long long fpow(long long x, long long n) { n   long long res = 1; n   while (n) { n     if (n & 1) res = res * x % mod; n     x = x * x % mod; n     n >>= 1; n   } n   return res; n } n long long gcd(long long a, long long b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n void sieve(long long n) { n   bool prime[1000006]; n   memset(prime, true, sizeof(prime)); n   long long rootn = (long long)sqrt(n); n   for (long long p = 2; p <= rootn; p++) n     if (prime[p] == true) n       for (long long i = p * p; i <= n; i += p) prime[i] = false; n   prime[1] = 0; n } n const long long N = 300050; n long long cnt, sum, mid, mx, mn, ans, a[N], b[N]; n long long n, m, d, i, j, k, l, p, q, r, t, w, x, y, z; n bool f, f1, f2; n string s; n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> k; n   cout << (n / k + 1) * k; n } n


#include <bits/stdc++.h> n using namespace std; n int main(int argc, char const *argv[]) { n   int n; n   cin >> n; n   int a[n]; n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n   } n   int cnt = 0; n   std::vector<int> v; n   for (int i = n - 1; i > 0; --i) { n     if (a[i] != 10000) { n       for (int j = i - 1; j >= 0; --j) { n         if (a[j] == a[i]) { n           a[j] = 10000; n         } n       } n       v.push_back(a[i]); n     } n   } n   if (a[0] != 10000) { n     v.push_back(a[0]); n   } n   cout << v.size() << endl; n   for (int i = v.size() - 1; i >= 0; --i) { n     cout << v[i] <<    ; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long n; n   cin >> n; n   vector<pair<long long, long long>> a(n); n   vector<long long> ord(n); n   for (long long i = 0; i < n; i++) { n     cin >> a[i].first; n     a[i].second = i; n   } n   sort(a.begin(), a.end()); n   for (long long i = 0; i < n; i++) { n     ord[a[i].second] = i; n   } n   for (long long i = 0; i < n; i++) { n     cout << (a[(ord[i] + 1) % n].first) <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(void) { n   string second[5] = { Danil ,  Olya ,  Slava ,  Ann ,  Nikita }; n   int sum = 0; n   string t; n   cin >> t; n   for (int i = 0; i < 5; i++) { n     int pos = t.find(second[i], 0); n     while (pos != string::npos) { n       sum++; n       pos = t.find(second[i], pos + 1); n     } n   } n   if (sum == 1) n     cout <<  YES  << endl; n   else n     cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[102] = {0}; n int main() { n   int n, m, i, ans = 0, x, l, r, num, mid; n   cin >> n >> m; n   for (i = 0; i < m; i++) { n     cin >> x; n     a[x]++; n   } n   sort(a + 1, a + 101); n   l = 1; n   r = 10000; n   while (l <= r) { n     num = 0; n     mid = (l + r) >> 1; n     for (i = 100; i > 0; i--) { n       if (a[i] < mid) break; n       num += a[i] / mid; n     } n     if (num >= n) { n       ans = mid; n       l = mid + 1; n     } else n       r = mid - 1; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mxn = 110; n int i, j, k; n int x, y, z, xx, xy, xz, yx, yy, yz; n int m, n, p; n char a[mxn]; n int main() { n   scanf( %d , &n); n   for (i = 1; i <= n; i++) { n     scanf( %s , a); n     m = strlen(a); n     if (a[0] != a[m - 1]) { n       a[0] = a[m - 1]; n     } n     printf( %s n , a); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   scanf( %d , &n); n   int k = 0; n   int sum = 0; n   for (int i = 2; i <= n - 1; i++) { n     int z = n; n     while (z >= i) { n       sum += z % i; n       z = z / i; n     } n     sum += z; n     k++; n   } n   for (int i = 2; i <= n - 1; i++) { n     while (k % i == 0 && sum % i == 0) { n       sum = sum / i; n       k = k / i; n     } n   } n   cout << sum <<  /  << k << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n queue<int> q; n vector<int> v[200001]; n set<pair<int, int> > se; n int mdis, node; n void dfs(int x, int l, int p) { n   if (l > mdis) { n     mdis = l; n     node = x; n   } n   for (int y : v[x]) { n     if (y == p) continue; n     dfs(y, l + 1, x); n   } n } n int main() { n   int p, n, k, i, j, x, y, a, b, c; n   scanf( %d %d , &n, &k); n   p = k + 1; n   for (i = 1; i <= k; ++i) { n     se.insert(make_pair(p, i)); n     q.push(i); n   } n   for (i = k + 2; i <= n; ++i) { n     x = q.front(); n     q.pop(); n     se.erase(make_pair(p, x)); n     se.insert(make_pair(p, i)); n     se.insert(make_pair(i, x)); n     q.push(i); n   } n   for (auto it : se) { n     x = it.first; n     y = it.second; n     v[x].push_back(y); n     v[y].push_back(x); n   } n   dfs(p, 0, 0); n   mdis = 0; n   dfs(node, 0, 0); n   printf( %d n , mdis); n   for (auto it : se) { n     printf( %d %d n , it.first, it.second); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t, m, n, z, s, q, p; n   cin >> t; n   while (t--) { n     cin >> n >> m; n     int a[n][m], z = 200, s = 0, p = 0; n     for (int i = 0; i < n; ++i) { n       for (int j = 0; j < m; ++j) { n         cin >> a[i][j]; n         if (a[i][j] < 0) { n           ++p; n         } n         z = min(z, abs(a[i][j])); n         s = s + abs(a[i][j]); n       } n     } n     if (p % 2 == 1) { n       s = s - 2 * z; n       cout << s << endl; n     } else n       cout << s << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long int> x; n vector<vector<int> > adjList; n vector<bool> visited; n long long int ans = 0; n long long int add(long long int a, long long int b) { n   return (a + b) % 1000000007; n } n long long int mul(long long int a, long long int b) { n   return (a * b) % 1000000007; n } n long long int gcd(long long int a, long long int b) { n   if (b == 0) n     return a; n   else n     return gcd(b, a % b); n } n void dfs(int node, unordered_map<long long int, long long int> mp) { n   long long int gc; n   unordered_map<long long int, long long int> mp2; n   for (auto elem : mp) { n     gc = gcd(elem.first, x[node]); n     ans = add(ans, mul(gc, elem.second)); n     mp2[gc] += elem.second; n   } n   ans = add(x[node], ans); n   mp2[x[node]] += 1LL; n   for (auto elem : adjList[node]) { n     if (!visited[elem]) { n       visited[elem] = true; n       dfs(elem, mp2); n     } n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   int n; n   cin >> n; n   x.resize(n + 1, 0); n   visited.resize(n + 1, false); n   adjList.resize(n + 1); n   for (register int i = 1; i <= n; i++) cin >> x[i]; n   int a, b; n   for (int i = 1; i < n; i++) { n     cin >> a >> b; n     adjList[a].push_back(b); n     adjList[b].push_back(a); n   } n   unordered_map<long long int, long long int> mp; n   visited[1] = true; n   dfs(1, mp); n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3e5 + 9; n const int mod = 1e9 + 7; n int32_t main() { n   int t; n   cin >> t; n   while (t--) { n     int n, m; n     cin >> n >> m; n     if (n % m == 0) n       cout <<  YES n ; n     else n       cout <<  NO n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long read() { n   long long k = 1, x = 0; n   char c = getchar(); n   while (c <  0  || c >  9 ) { n     if (c ==  - ) k = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     x = (x << 3) + (x << 1) + (c - 48); n     c = getchar(); n   } n   return x * k; n } n long long n, m, f[2001][2001], s[2001][2001], res[300000], p = 998244353; n long long a[1000604], k, ans, now; n int main() { n   n = read(); n   k = read(); n   for (int i = 1; i <= n; i++) { n     a[i] = read(); n   } n   ans = 0; n   sort(a + 1, a + 1 + n); n   for (int v = 1; v * (k - 1) <= a[n]; v++) { n     now = 0; n     f[0][0] = 1; n     for (int i = 1; i <= n; i++) { n       while (a[i] - a[now + 1] >= v) now++; n       f[i][0] = f[i - 1][0]; n       for (int j = 1; j <= k; j++) { n         f[i][j] = (f[i - 1][j] + f[now][j - 1]) % p; n       } n     } n     ans = (ans + f[n][k]) % p; n   } n   printf( %lld n , ans % p); n } n


#include <bits/stdc++.h> n using namespace std; n int A[1000010]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) cin >> A[i]; n   string s; n   cin >> s; n   int la = -1e9, ha = 1e9, lb = -1e9, hb = 1e9; n   multiset<int> fg; n   for (int i = 0; i < 4; i++) fg.insert(A[i]); n   for (int i = 4; i < n; i++) { n     fg.insert(A[i]); n     if (s[i] ==  0 ) { n       if (s[i - 1] ==  0 ) { n         if (s[i - 1] ==  0  && s[i - 2] ==  0  && s[i - 3] ==  0  && n             s[i - 4] ==  0 ) { n           ha = min(la, *fg.rbegin()); n         } n       } else { n         hb = min(hb, *fg.begin() - 1); n       } n     } else if (s[i] ==  1 ) { n       if (s[i - 1] ==  1 ) { n         if (s[i - 1] ==  1  && s[i - 2] ==  1  && s[i - 3] ==  1  && n             s[i - 4] ==  1 ) { n           lb = max(lb, *fg.begin()); n         } n       } else { n         la = max(la, (*fg.rbegin()) + 1); n       } n     } n     fg.erase(fg.find(A[i - 4])); n   } n   cout << la <<     << hb <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n; n   vector<long long> v; n   cin >> n; n   long long a = 1; n   while (a * (a + 1) / 2 <= n) v.push_back(a * (a + 1) / 2), a++; n   for (long long i = 1; i * (i + 1) / 2 <= n; i++) { n     int cur = n - i * (i + 1) / 2; n     if (binary_search(v.begin(), v.end(), cur)) { n       puts( YES ); n       return 0; n     } n   } n   puts( NO ); 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 int n; n     cin >> n; n     long long int ans = pow(2, n); n     for (int i = 1; i <= n / 2 - 1; i++) { n       ans += pow(2, i); n     } n     for (int i = n / 2; i <= n - 1; i++) { n       ans -= pow(2, i); n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool primes[1000007]; n void sieve(int n) { n   for (long long i = 2; i <= n; i++) { n     if (primes[i]) continue; n     for (long long j = i * i; j <= n; j += i) { n       primes[j] = 1; n     } n   } n } n int main() { n   int n; n   cin >> n; n   sieve(1e6); n   while (n--) { n     long long tmp; n     scanf( %I64d , &tmp); n     long long limit = sqrt(tmp); n     if (tmp == 1) n       printf( NO n ); n     else if (limit * limit == tmp && tmp % 2 && !primes[limit]) n       printf( YES n ); n     else if (tmp == 4) n       printf( YES n ); n     else n       printf( NO n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int dis[200009]; n int con[200009]; n bool cmp(int a, int b) { return a < b; } n int main() { n   int i, p, j, n, t, y; n   int x; n   long long int nail; n   scanf( %d , &n); n   for (i = 0; i <= n - 1; i++) scanf( %d , &dis[i]); n   sort(dis, dis + n, cmp); n   nail = 99999999999; n   j = 0; n   y = 0; n   for (i = 1; i <= n - 1; i++) { n     x = dis[i] - dis[i - 1]; n     con[i] = x; n     if (x < nail) nail = x; n   } n   for (i = 1; i <= n - 1; i++) n     if (nail == con[i]) y++; n   printf( %lld %d n , nail, y); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   long long mas[n]; n   long long sum = 0; n   for (int i = 0; i < n; i++) { n     cin >> mas[i]; n     sum += mas[i]; n   } n   sort(mas, mas + n); n   int k; n   cin >> k; n   int c; n   while (k > 0) { n     cin >> c; n     cout << sum - mas[n - c] << endl; n     k--; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1 << 29; n int killed[100000], val[100000]; n int main() { n   ios::sync_with_stdio(0); n   int n, ans = 0; n   vector<int> S(1, 0); n   cin >> n; n   killed[0] = -1; n   for (int i = 0; i < n; i++) cin >> val[i]; n   for (int i = 1; i < n; i++) { n     while (S.size() > 0 && val[S.back()] < val[i]) { n       killed[i] = max(killed[i], killed[S.back()] + 1); n       S.pop_back(); n     } n     if (S.empty()) killed[i] = -1; n     ans = max(ans, killed[i] + 1); n     S.push_back(i); n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma comment(linker,  /STACK:400000000 ) n int n, t; n int H; n string s; n int ok(int rest, int last) { n   int time = 0; n   for (int i = 0; i < (last + 1); i++) { n     time++; n     if (time > t) return 0; n     if (time + (last - i) * 2 <= t) return 1; n     if (s[i] ==  S ) rest++; n     if (s[i] ==  H ) rest--; n     if (rest < 0) { n       int from = i; n       while (rest < 0) { n         i++; n         if (s[i] ==  S ) rest++; n         if (s[i] ==  H ) rest--; n       } n       time += (i - from) * 3; n     } n   } n   return time <= t; n } n int main() { n   cin >> n >> t >> s; n   for (int i = 0; i < (n); i++) H += s[i] ==  H ; n   int lo = -1, hi = H + 1; n   while (lo + 1 < hi) { n     int mi = (lo + hi) / 2; n     int rest = mi; n     int last = 0; n     for (int i = 0; i < (n); i++) { n       if (s[i] ==  S ) { n         if (rest < 0) last = i; n         rest++; n       } n       if (s[i] ==  H ) { n         last = i; n         rest--; n       } n     } n     if (rest < 0) n       lo = mi; n     else { n       if (ok(mi, last)) n         hi = mi; n       else n         lo = mi; n     } n   } n   if (hi == H + 1) n     cout << -1 << endl; n   else n     cout << hi << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int N = 2e6, OO = 1e17, M = 1e9 + 7; n pair<long long, long long> p[N]; n long long dp[N]; n int main() { n   ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   long long n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> p[i].first >> p[i].second; n   } n   sort(p, p + n); n   for (int i = 0; i < n; i++) { n     int t = lower_bound(p, p + n, n                         make_pair(p[i].first - p[i].second, (long long)0)) - n             p - 1; n     if (t < 0) n       dp[i] = 1; n     else n       dp[i] = dp[t] + 1; n   } n   cout << n - *max_element(dp, dp + n); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int sum = 0; n vector<vector<int>> mat; n vector<string> mat2; n void f1(vector<string>& ans, int f, string s) { n   if (f == 0) return; n   string s1 = to_string(f) +     + s; n   ans.push_back(s1); n   sum += f * s.length(); n   vector<int> temp(2, 0); n   temp[0] = f; n   temp[1] = s.length(); n   mat.push_back(temp); n   mat2.push_back(s); n } n string frs(char c, int fre) { n   string ans =   ; n   for (int i = 0; i < fre; i++) { n     ans = ans + c; n   } n   return ans; n } n int main() { n   int n, m, k; n   cin >> n >> m >> k; n   vector<string> ans; n   string s; n   int f = (m - 1) / 4; n   f1(ans, f,  RRRR ); n   if ((m - 1) % 4) f1(ans, 1, frs( R , (m - 1) % 4)); n   f = (m - 1) / 4; n   f1(ans, f,  LLLL ); n   if ((m - 1) % 4) f1(ans, 1, frs( L , (m - 1) % 4)); n   for (int i = 0; i < n - 1; i++) { n     f1(ans, 1,  D ); n     f1(ans, m - 1,  RUD ); n     f = (m - 1) / 4; n     f1(ans, f,  LLLL ); n     if ((m - 1) % 4) f1(ans, 1, frs( L , (m - 1) % 4)); n   } n   f = (n - 1) / 4; n   f1(ans, f,  UUUU ); n   if ((n - 1) % 4) f1(ans, 1, frs( U , (n - 1) % 4)); n   int N = ans.size(); n   if (sum < k) { n     cout <<  NO ; n   } else { n     cout <<  YES  << endl; n     vector<string> v; n     int cur = 0; n     for (int i = 0; i < N; i++) { n       if (cur == k) break; n       int l = mat[i][0] * mat[i][1]; n       if (cur + l <= k) { n         v.push_back(ans[i]); n         cur += l; n       } else { n         int len = k - cur; n         f = len / mat[i][1]; n         string s; n         if (f) { n           s = to_string(f) +     + mat2[i]; n           v.push_back(s); n         } n         if (len % (mat[i][1])) { n           s =  1   + mat2[i].substr(0, len % (mat[i][1])); n           v.push_back(s); n         } n         break; n       } n     } n     N = v.size(); n     cout << N << endl; n     for (int j = 0; j < N; j++) { n       cout << v[j] << endl; n     } n   } n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n  n int MOD; n struct modint { n  n private: n  n     int v; n  n     static int minv(int a, int m) { n         a %= m; n         assert(a); n         return a == 1 ? 1 : int(m - ll(minv(m, a)) * ll(m) / a); n     } n  n public: n  n     modint() : v(0) {} n     modint(ll v_) : v(int(v_ % MOD)) { if (v < 0) v += MOD; } n     explicit operator int() const { return v; } n     friend std::ostream& operator << (std::ostream& out, const modint& n) { return out << int(n); } n     friend std::istream& operator >> (std::istream& in, modint& n) { ll v_; in >> v_; n = modint(v_); return in; } n  n     friend bool operator == (const modint& a, const modint& b) { return a.v == b.v; } n     friend bool operator != (const modint& a, const modint& b) { return a.v != b.v; } n  n     modint inv() const { n         modint res; n         res.v = minv(v, MOD); n         return res; n     } n     friend modint inv(const modint& m) { return m.inv(); } n     modint neg() const { n         modint res; n         res.v = v ? MOD-v : 0; n         return res; n     } n     friend modint neg(const modint& m) { return m.neg(); } n  n     modint operator- () const { n         return neg(); n     } n     modint operator+ () const { n         return modint(*this); n     } n  n     modint& operator ++ () { n         v ++; n         if (v == MOD) v = 0; n         return *this; n     } n     modint& operator -- () { n         if (v == 0) v = MOD; n         v --; n         return *this; n     } n     modint& operator += (const modint& o) { n         v += o.v; n         if (v >= MOD) v -= MOD; n         return *this; n     } n     modint& operator -= (const modint& o) { n         v -= o.v; n         if (v < 0) v += MOD; n         return *this; n     } n     modint& operator *= (const modint& o) { n         v = int(ll(v) * ll(o.v) % MOD); n         return *this; n     } n     modint& operator /= (const modint& o) { n         return *this *= o.inv(); n     } n  n     friend modint operator ++ (modint& a, int) { modint r = a; ++a; return r; } n     friend modint operator -- (modint& a, int) { modint r = a; --a; return r; } n     friend modint operator + (const modint& a, const modint& b) { return modint(a) += b; } n     friend modint operator - (const modint& a, const modint& b) { return modint(a) -= b; } n     friend modint operator * (const modint& a, const modint& b) { return modint(a) *= b; } n     friend modint operator / (const modint& a, const modint& b) { return modint(a) /= b; } n }; n namespace IO { n     template<class T> n     void _R(T &x) { cin >> x; } n     void _R(int &x) { scanf( %d , &x); } n     void _R(ll &x) { scanf( %lld , &x); } n     void _R(double &x) { scanf( %lf , &x); } n     void _R(char &x) { x = getchar(); } n     void _R(char *x) { scanf( %s , x); } n     void R() {} n     template<class T, class... U> n     void R(T &head, U &... tail) { _R(head), R(tail...); } n     template<class T> n     void _W(const T &x) { cout << x; } n     void _W(const int &x) { printf( %d , x); } n     void _W(const ll &x) { printf( %lld , x); } n     void _W(const double &x) { printf( %.16f , x); } n     void _W(const char &x) { putchar(x); } n     void _W(const char *x) { printf( %s , x); } n     template<class T, class U> n     void _W(const pair<T, U> &x) { _W(x.first), putchar(   ), _W(x.second); } n     template<class T> n     void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(   ); } n     void W() {} n     template<class T, class... U> n     void W(const T &head, const U &... tail) { _W(head), putchar(sizeof...(tail) ?     :   n ), W(tail...); } n } n using namespace IO; n template <typename T> T pow(T a, long long b) { n     assert(b >= 0); n     T r = 1; while (b) { if (b & 1) r *= a; b >>= 1; a *= a; } return r; n } n  n const int maxn = 2e5+50; n const int maxp = maxn*32+5; n const int K = 20; n const int M = 1 << K; n int ls[maxp], rs[maxp], tot; n // 0, 1, cnt, XOR n int t0[maxp], t1[maxp], p[maxp], txor[maxp]; n  n inline void pushup(int x) { n     p[x] = p[ls[x]] + p[rs[x]]; n     t0[x] = t0[ls[x]] | t0[rs[x]]; n     t1[x] = t1[ls[x]] | t1[rs[x]]; n } n inline void XOR(int x, int t, int dep = K) { n     if (!x) return; n     txor[x] ^= t; n     if (t >> (dep - 1) & 1) swap(ls[x], rs[x]); n     int a = (t0[x] & (~t)) | (t1[x] & t), b = (t1[x] & (~t)) | (t0[x] & t); n     t0[x] = a, t1[x] = b; n } n inline void pushdown(int x, int dep) { n     if (txor[x]) { n         XOR(ls[x], txor[x], dep - 1), XOR(rs[x], txor[x], dep - 1); n         txor[x] = 0; n     } n } n inline void insert(int &x, int s, int dep = K) { n     if (!x) x = ++tot; n     if (!dep) { n         t1[x] = s, t0[x] = s ^ (M - 1); n         p[x] = 1; n         return; n     } n     insert((s >> (dep - 1) & 1) ? rs[x] : ls[x], s, dep - 1); n     pushup(x); n } n inline void split(int &x, int &y, int l, int r, int le, int re, int dep = K) { n     if (le <= l && r <= re) { n         y = x; x = 0; n         return; n     } n     int mid = l + r >> 1; pushdown(x, dep); n     y = ++tot; n     if (le <= mid) split(ls[x], ls[y], l, mid, le, re, dep - 1); n     if (re > mid) split(rs[x], rs[y], mid + 1, r, le, re, dep - 1); n     pushup(x), pushup(y); n } n inline void merge(int &x, int y, int dep = K) { n     if (!x || !y) { n         x = x + y; n         return; n     } n     if (!dep) return; n     pushdown(x, dep), pushdown(y, dep); n     merge(ls[x], ls[y], dep - 1), merge(rs[x], rs[y], dep - 1); n     pushup(x); n } n inline void OR(int x, int s, int dep = K) { n     if (!x) return; n     if (!(s & t0[x] & t1[x])) { n         XOR(x, s & t0[x], dep); n         return; n     } n     pushdown(x, dep); n     if (s >> (dep - 1) & 1) { n         XOR(ls[x], 1 << (dep - 1), dep - 1); n         merge(rs[x], ls[x], dep - 1); n         ls[x] = 0; n     } n     OR(ls[x], s, dep - 1), OR(rs[x], s, dep - 1); n     pushup(x); n } n  n int main() { n     int n, q, x = 0; n     R(n, q); n     for (int i = 1; i <= n; ++i) { n         int v; R(v); insert(x, v); n     } n     for (; q; --q) { n         int oth; n         int t, l, r, v; R(t, l, r); n         split(x, oth, 0, M - 1, l, r); n         if (t == 1) R(v),XOR(oth, M - 1),OR(oth, v ^ (M - 1)),XOR(oth, M - 1); n         else if (t == 2) R(v),OR(oth, v); n         else if (t == 3) R(v), XOR(oth, v); n         else W(p[oth]); n         merge(x, oth); n     } n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n void solve(string s) { n   int n = s.size(); n   s =  #  + s; n   vector<int> pi(n + 1); n   pi[0] = 0, pi[1] = 0; n   for (int i = 2; i <= n; i++) { n     int j = pi[i - 1]; n     while (j > 0 && s[i] != s[j + 1]) j = pi[j]; n     if (s[i] == s[j + 1]) j++; n     pi[i] = j; n   } n   int dp[n + 1]; n   for (int i = 0; i <= n; i++) dp[i] = 1; n   for (int i = n; i >= 1; i--) dp[pi[i]] += dp[i]; n   vector<pair<int, int> > res; n   res.push_back(pair<int, int>(n, 1)); n   int j = pi[n]; n   if (j == 0) { n     cout << 1 << endl; n     cout << n <<   1  << endl; n     return; n   } n   res.push_back(pair<int, int>(j, dp[j])); n   while (j > 0) { n     j = pi[j]; n     if (j == 0) break; n     res.push_back(pair<int, int>(j, dp[j])); n   } n   cout << res.size() << endl; n   for (int i = res.size() - 1; i >= 0; i--) n     cout << res[i].first <<     << res[i].second << endl; n } n int main() { n   string s; n   cin >> s; n   solve(s); n } n


#include <bits/stdc++.h> n using namespace std; n int n; n string txt, ans; n int main() { n   ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   cin >> n >> txt; n   for (int i = 0; i < n - (2 + (n & 1)); i++) { n     if ((i & 1) == 0 && i) ans +=  - ; n     ans += txt[i]; n   } n   if (n > 3) ans +=  - ; n   for (int i = n - (2 + (n & 1)); i < n; i++) ans += txt[i]; n   return cout << ans << endl, 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 50015; n const int INF = 0x3f3f3f3f; n struct edge { n   int to, capacity, rev; n   long long cost; n   edge() {} n   edge(int to, int _capacity, long long _cost, int _rev) n       : to(to), capacity(_capacity), cost(_cost), rev(_rev) {} n }; n struct Min_Cost_Max_Flow { n   int V, PreV[maxn + 5], PreE[maxn + 5]; n   long long dis[maxn + 5], H[maxn + 5]; n   vector<edge> G[maxn + 5]; n   void Init(int n) { n     V = n; n     for (int i = 0; i <= V; ++i) G[i].clear(); n   } n   void Add_Edge(int from, int to, int cap, long long cost) { n     G[from].emplace_back(edge(to, cap, cost, G[to].size())); n     G[to].emplace_back(edge(from, 0, -cost, G[from].size() - 1)); n   } n   long long Max_cost_max_flow(int s, int t, int f, int& flow) { n     long long res = 0; n     fill(H, H + 1 + V, 0); n     while (f) { n       priority_queue<pair<int, int>> q; n       fill(dis, dis + 1 + V, -INF); n       dis[s] = 0; n       q.push(pair<int, int>(0, s)); n       while (!q.empty()) { n         pair<int, int> now = q.top(); n         q.pop(); n         int v = now.second; n         if (dis[v] > now.first) continue; n         for (int i = 0; i < G[v].size(); ++i) { n           edge& e = G[v][i]; n           if (e.capacity > 0 && dis[e.to] < dis[v] + e.cost + H[v] - H[e.to]) { n             dis[e.to] = dis[v] + e.cost + H[v] - H[e.to]; n             PreV[e.to] = v; n             PreE[e.to] = i; n             q.push(pair<int, int>(dis[e.to], e.to)); n           } n         } n       } n       if (dis[t] == -INF) break; n       for (int i = 0; i <= V; ++i) H[i] += dis[i]; n       int d = f; n       for (int v = t; v != s; v = PreV[v]) n         d = min(d, G[PreV[v]][PreE[v]].capacity); n       f -= d; n       flow += d; n       if (H[t] < 0) return res; n       res += d * H[t]; n       for (int v = t; v != s; v = PreV[v]) { n         edge& e = G[PreV[v]][PreE[v]]; n         e.capacity -= d; n         G[v][e.rev].capacity += d; n       } n     } n     return res; n   } n } fuck; n int main() { n   int n; n   cin >> n; n   fuck.Init(n + n + 2); n   int a, b; n   int s = 0, t = n + n + 1; n   long long k; n   for (int i = 1; i <= n; i++) { n     scanf( %d%d%lld , &a, &b, &k); n     for (int j = 1; j <= n; j++) { n       fuck.Add_Edge(i, n + j, 1, (a - 1LL * min(1LL * j - 1, k) * b)); n     } n     fuck.Add_Edge(s, i, 1, 0); n     fuck.Add_Edge(i + n, t, 1, 0); n   } n   int tmp; n   long long ans = fuck.Max_cost_max_flow(s, t, INF, tmp); n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize(2) n using namespace std; n const int INF = 1e9 + 7; n const int N = 2e5 + 7; n const int M = 5e6 + 7; n int a[N], f[N], ans[N]; n int get(int x) { n   int k = ceil(sqrt(abs(x) * 1.00)); n   for (int i = 2; i <= k; i++) n     while (x % (i * i) == 0) x /= i * i; n   return x; n } n signed main() { n   ios::sync_with_stdio(false); n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     a[i] = get(a[i]); n   } n   f[1] = -1; n   for (int i = 2; i <= n; i++) { n     for (int j = i - 1; j >= 1; --j) n       if (a[i] == a[j]) { n         f[i] = j; n         break; n       } n   } n   for (int i = 1; i <= n; i++) { n     int num = 0; n     for (int j = i; j <= n; j++) { n       if (f[j] < i && a[j] != 0) num++; n       if (!num) n         ans[1]++; n       else n         ans[num]++; n     } n   } n   for (int i = 1; i <= n; i++) cout << ans[i] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int dp[100000 + 10]; n vector<int> ma[100000 + 10]; n int a[100000 + 10]; n long long int dp1[100000 + 10]; n long long int gcd(long long int x, long long int y) { n   return y == 0 ? x : gcd(y, x % y); n } n long long int lcm(long long int x, long long int y) { n   long long int tmp = gcd(x, y); n   return x / tmp * y; n } n void tree_dp(int fa, int n) { n   if (ma[n].size() == 1) { n     dp1[n] = 1; n     dp[n] = (long long int)a[n]; n     return; n   } n   int i; n   long long int tmp = 1; n   long long int mins = LLONG_MAX; n   for (i = 0; i < ma[n].size(); i++) { n     if (ma[n][i] == fa) continue; n     tree_dp(n, ma[n][i]); n     tmp = lcm(tmp, dp1[ma[n][i]]); n     if (tmp == 0) tmp = 1; n     mins = min(mins, dp[ma[n][i]]); n   } n   dp[n] = (long long int)a[n] + mins / tmp * tmp * (ma[n].size() - 1); n   dp1[n] = tmp * (ma[n].size() - 1); n } n int main() { n   int n; n   cin >> n; n   int i; n   long long int all = 0; n   for (i = 1; i <= n; i++) { n     cin >> a[i]; n     ma[i].clear(); n     all += a[i]; n   } n   ma[1].push_back(1); n   for (i = 0; i < n - 1; i++) { n     int x, y; n     cin >> x >> y; n     ma[x].push_back(y); n     ma[y].push_back(x); n   } n   tree_dp(1, 1); n   cout << all - dp[1] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = 1e18; n const long long mod = 1e9 + 7; n inline long long bigmod(long long B, long long P) { n   long long R = 1; n   while (P > 0) { n     if (P & 1) { n       R = (R * B) % mod; n     } n     P >>= 1; n     B = (B * B) % mod; n   } n   return R; n } n const long long mxn = 500; n long long Mod(long long a) { n   if (a < 0) n     return (a + mod) % mod; n   else n     return a % mod; n } n long long Mul(long long a, long long b) { return Mod(Mod(a) * Mod(b)); } n long long Add(long long a, long long b) { return Mod(Mod(a) + Mod(b)); } n vector<long long> adj[mxn]; n long long vis[mxn], pred[mxn], dist[mxn]; n vector<long long> adj2[mxn]; n long long vis2[mxn], pred2[mxn], dist2[mxn]; n long long n; n bool bfs(long long root) { n   queue<long long> q; n   q.push(root); n   vis[root] = 1; n   while (!q.empty()) { n     long long u = q.front(); n     q.pop(); n     for (long long v : adj[u]) { n       if (!vis[v]) { n         vis[v] = 1; n         dist[v] = dist[u] + 1; n         q.push(v); n         if (v == n) return true; n       } n     } n   } n   return false; n } n bool bfs2(long long root) { n   queue<long long> q; n   q.push(root); n   vis2[root] = 1; n   while (!q.empty()) { n     long long u = q.front(); n     q.pop(); n     for (long long v : adj2[u]) { n       if (!vis2[v]) { n         vis2[v] = 1; n         dist2[v] = dist2[u] + 1; n         q.push(v); n         if (v == n) return true; n       } n     } n   } n   return false; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   long long tt = 1; n   for (long long tc = 1; tc <= tt; tc++) { n     long long m, f = 0; n     cin >> n >> m; n     map<pair<long long, long long>, bool> trainase; n     for (long long i = 0; i < m; i++) { n       long long u, v; n       cin >> u >> v; n       adj[u].push_back(v); n       adj[v].push_back(u); n       trainase[{u, v}] = 1; n       trainase[{v, u}] = 1; n       if (u == n || v == n) f = 1; n     } n     if (!f) n       cout << -1 <<   n ; n     else { n       for (long long i = 1; i <= n; i++) { n         for (long long j = i + 1; j <= n; j++) { n           if (!trainase[{i, j}]) { n             adj2[i].push_back(j); n             adj2[j].push_back(i); n           } n         } n       } n       if (!bfs2(1) || !bfs(1)) { n         cout << -1 <<   n ; n         return 0; n       } n       bfs(1); n       bfs2(1); n       cout << max(dist2[n], dist[n]) <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const long long INFF = 0x3f3f3f3f3f3f3f3fll; n const long long M = 1e9 + 7; n const long long maxn = 2e5 + 7; n const double eps = 0.00000001; n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n template <typename T> n inline T abs(T a) { n   return a > 0 ? a : -a; n } n void exgcd(long long a, long long b, long long &d, long long &x, long long &y) { n   if (!b) { n     d = a; n     x = 1; n     y = 0; n   } else { n     exgcd(b, a % b, d, y, x); n     y -= a / b * x; n   } n } n int n, m; n int i, j; n bool used[maxn]; n vector<int> v[maxn]; n int num[maxn], fa[maxn], mx; n int pp = 1; n void print(int mx) { n   if (fa[mx]) print(fa[mx]); n   long long a, b, t; n   for (i = 0; i < v[mx].size(); i++) { n     exgcd(pp, m, t, a, b); n     printf( %I64d  , (v[mx][i] / t * a % m + m) % m); n     pp = v[mx][i]; n   } n } n int main() { n   scanf( %d%d , &n, &m); n   for (i = 0; i < n; i++) scanf( %d , &j), used[j] = 1; n   for (i = 1; i < m; i++) n     if (!used[i]) v[gcd(i, m)].push_back(i); n   for (i = 1; i < m; i++) { n     if ((num[i] += v[i].size()) > num[mx]) mx = i; n     for (j = i << 1; j < m; j += i) n       if (num[j] < num[i]) num[j] = num[i], fa[j] = i; n   } n   printf( %d n , num[mx] + !used[0]); n   print(mx); n   if (!used[0]) n     puts( 0 ); n   else n     puts(  ); n } n


#include <bits/stdc++.h> n using namespace std; n long long K, N; n string A, B; n bool valid[1001]; n long long dp[100002][2]; n int main() { n   ios::sync_with_stdio(0); n   cin >> A >> B >> K; n   N = A.size(); n   dp[0][0] = 1, dp[0][1] = 0; n   for (int i = 1; i <= K; ++i) { n     dp[i][0] = (N - 1) * dp[i - 1][1]; n     dp[i][1] = dp[i - 1][0] + (N - 2) * dp[i - 1][1]; n     dp[i][0] %= 1000000007; n     dp[i][1] %= 1000000007; n   } n   long long ans = 0; n   for (int i = 0; i < N; ++i) { n     valid[i] = true; n     for (int j = 0; j < N; ++j) n       if (A[j % N] != B[(i + j) % N]) valid[i] = false; n     if (valid[i]) { n       if (i == 0) n         ans = (ans + dp[K][0]) % 1000000007; n       else n         ans = (ans + dp[K][1]) % 1000000007; n     } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n int n, m; n using namespace std; n vector<int> used(5005, 0); n vector<int> used2(5005, 0); n int jlo1 = 3; n vector<int> a; n long long res = 0; n vector<vector<int>> graph(5005, a); n vector<int> b; n vector<vector<int>> graph1(5005, b); n int q = 0; n bool flag = false; n void dfs(int p) { n   { n     q++; n     used[p] = 1; n     for (int i = 0; i < graph[p].size(); i++) { n       if (!used[graph[p][i]]) dfs(graph[p][i]); n     } n   } n } n void dfs2(int p) { n   { n     q++; n     used2[p] = 1; n     for (int i = 0; i < graph[p].size(); i++) { n       if (!used2[graph[p][i]]) dfs2(graph[p][i]); n     } n   } n } n int main() { n   int k, t, t1; n   cin >> n >> m >> k; n   for (int i = 0; i < m; i++) { n     cin >> t >> t1; n     graph[t].push_back(t1); n   } n   dfs(k); n   vector<pair<int, int>> p_q; n   for (int i = 1; i <= n; i++) { n     if (!used[i]) { n       used2.assign(5005, 0); n       q = 0; n       dfs2(i); n       p_q.push_back(make_pair(q, i)); n     } n   } n   sort(p_q.rbegin(), p_q.rend()); n   long long res = 0; n   for (int i = 0; i < p_q.size(); i++) { n     if (!used[p_q[i].second]) { n       res++; n       dfs(p_q[i].second); n     } n   } n   cout << res <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100010; n vector<int> vec[N], fenw[N]; n int n, k, x[N], r[N], f[N], id[N], rev[N]; n void update(int who, int p, int v) { n   while (p < fenw[who].size()) fenw[who][p] += v, p += p & -p; n } n int pref(int who, int p) { n   int ret = 0; n   while (p) ret += fenw[who][p], p -= p & -p; n   return ret; n } n inline int get(int who, int l, int r) { n   if (l > r) return 0; n   return pref(who, r) - (l ? pref(who, l - 1) : 0); n } n int main() { n   cin >> n >> k; n   for (int i = 1; i <= n; ++i) { n     scanf( %d %d %d , x + i, r + i, f + i); n     id[i] = i, vec[f[i]].emplace_back(i); n   } n   for (int i = 0; i < N; ++i) { n     if (vec[i].empty()) continue; n     vec[i].emplace_back(0); n     sort(vec[i].begin(), vec[i].end(), n          [](int p, int q) { return x[p] < x[q]; }); n     fenw[i].resize(vec[i].size(), 0); n     for (int j = 0; j < vec[i].size(); ++j) { n       rev[vec[i][j]] = j, vec[i][j] = x[vec[i][j]]; n     } n   } n   sort(id + 1, id + n + 1, [](int i, int j) { return r[i] > r[j]; }); n   long long ans = 0; n   for (int it = 1; it <= n; ++it) { n     int i = id[it]; n     for (int oth = max(0, f[i] - k); oth <= f[i] + k; ++oth) { n       if (vec[oth].empty()) continue; n       int lt = lower_bound(vec[oth].begin(), vec[oth].end(), x[i] - r[i]) - n                vec[oth].begin(); n       int rt = upper_bound(vec[oth].begin(), vec[oth].end(), x[i] - 1) - n                vec[oth].begin() - 1; n       ans += get(oth, lt, rt); n       lt = lower_bound(vec[oth].begin(), vec[oth].end(), x[i] + 1) - n            vec[oth].begin(); n       rt = upper_bound(vec[oth].begin(), vec[oth].end(), x[i] + r[i]) - n            vec[oth].begin() - 1; n       ans += get(oth, lt, rt); n     } n     update(f[i], rev[i], 1); n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T1, class T2> n void pr(const pair<T1, T2>& x); n template <class T, size_t SZ> n void pr(const array<T, SZ>& x); n template <class T> n void pr(const vector<T>& x); n template <class T> n void pr(const set<T>& x); n template <class T1, class T2> n void pr(const map<T1, T2>& x); n template <class T> n void pr(const T& x) { n   cout << x; n } n template <class Arg, class... Args> n void pr(const Arg& first, const Args&... rest) { n   pr(first); n   pr(rest...); n } n template <class T1, class T2> n void pr(const pair<T1, T2>& x) { n   pr( { , x.first,  ,  , x.second,  } ); n } n template <class T> n void prContain(const T& x) { n   pr( { ); n   bool fst = 1; n   for (auto& a : x) pr(!fst ?  ,   :   , a), fst = 0; n   pr( } ); n } n template <class T, size_t SZ> n void pr(const array<T, SZ>& x) { n   prContain(x); n } n template <class T> n void pr(const vector<T>& x) { n   prContain(x); n } n template <class T> n void pr(const set<T>& x) { n   prContain(x); n } n template <class T1, class T2> n void pr(const map<T1, T2>& x) { n   prContain(x); n } n void ps() { pr(  n ); } n template <class Arg, class... Args> n void ps(const Arg& first, const Args&... rest) { n   pr(first,    ); n   ps(rest...); n } n template <typename T> n int remin(T& a, const T& b) { n   if (b < a) { n     a = b; n     return true; n   } n   return false; n } n template <typename T> n int remax(T& a, const T& b) { n   if (b > a) { n     a = b; n     return true; n   } n   return false; n } n int n; n int G[5005][5005]; n short int nxt[5005][5005]; n vector<unsigned short int> H[5005]; n void dfs_parent(int from, vector<int>& p, int parent = -1) { n   for (auto to : H[from]) n     if (to != parent) { n       p[to] = from; n       dfs_parent(to, p, from); n     } n } n void dfs(int u, int v, int w, const vector<int>& p) { n   while (u != v) { n     int n = p[v]; n     remax(G[v][n], w); n     remax(G[n][v], w); n     v = n; n   } n } n void check(int u, int v, int w, const vector<int>& p) { n   int exp = 1E9; n   while (u != v) { n     int n = p[v]; n     if (G[v][n] == -1) { n       G[v][n] = 1000000; n       G[n][v] = 1000000; n     } n     remin(exp, G[v][n]); n     v = n; n   } n   if (exp != w) { n     printf( -1 n ); n     exit(0); n   } n } n int main(int argc, const char** argv) { n   scanf( %d , &n); n   vector<pair<int, int>> edges; n   for (int i = 0; i < n - 1; ++i) { n     int u, v; n     scanf( %d%d , &u, &v); n     --u, --v; n     G[u][v] = -1; n     G[v][u] = -1; n     edges.emplace_back(u, v); n     H[u].push_back(v); n     H[v].push_back(u); n   } n   vector<vector<int>> p(n, vector<int>(n, -1)); n   for (int u = 0; u < n; ++u) { n     dfs_parent(u, p[u]); n   } n   int q; n   scanf( %d , &q); n   vector<pair<pair<int, int>, int>> qu(q); n   for (int qq = 0; qq < q; ++qq) { n     int u, v, w; n     scanf( %d%d%d , &u, &v, &w); n     --u, --v; n     qu[qq] = make_pair(pair<int, int>(u, v), w); n     dfs(u, v, w, p[u]); n   } n   for (auto [c, w] : qu) { n     check(c.first, c.second, w, p[c.first]); n   } n   vector<int> ans(n - 1); n   for (int i = 0; i < n - 1; ++i) { n     int u = edges[i].first; n     int v = edges[i].second; n     int mn = min(u, v); n     int mx = max(u, v); n     printf( %d  , G[mn][mx] == -1 ? 1000000 : G[mn][mx]); n   } n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long n; n   cin >> n; n   vector<long long> a(n); n   for (long long i = 0; i < n; i++) cin >> a[i]; n   long long mx = 0; n   vector<long long> ans(n); n   for (long long i = 0; i < n; i++) { n     long long cnt = a[i], mn = a[i]; n     vector<long long> tmp(n); n     tmp[i] = a[i]; n     for (long long j = i + 1; j < n; j++) { n       if (a[j] >= mn) { n         tmp[j] = mn; n         cnt += mn; n       } else { n         tmp[j] = a[j]; n         mn = a[j]; n         cnt += mn; n       } n     } n     mn = a[i]; n     for (long long j = i - 1; j >= 0; j--) { n       if (a[j] >= mn) { n         tmp[j] = mn; n         cnt += mn; n       } else { n         tmp[j] = a[j]; n         mn = a[j]; n         cnt += mn; n       } n     } n     if (cnt > mx) { n       mx = cnt; n       for (long long j = 0; j < n; j++) ans[j] = tmp[j]; n     } n   } n   for (long long i = 0; i < n; i++) cout << ans[i] <<    ; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   long long t; n   t = 1; n   while (t--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n int n; n vector<pair<int, int> > v; n vector<int> tail; n int getIdx(int l, int r, int key) { n   while (l < r) { n     int mid = (l + r) / 2; n     if (tail[mid] < key) n       r = mid; n     else n       l = mid + 1; n   } n   return r; n } n int LIS() { n   int length = 1; n   tail[0] = v[0].second; n   for (int i = 1; i < n; i++) { n     if (v[i].second > tail[0]) n       tail[0] = v[i].second; n     else if (v[i].second < tail[length - 1]) n       tail[length++] = v[i].second; n     else { n       tail[getIdx(0, length - 1, v[i].second)] = v[i].second; n     } n   } n   return length; n } n int main() { n   int x, y; n   scanf( %d , &n); n   v = vector<pair<int, int> >(n); n   for (int i = 0; i < n; i++) { n     scanf( %d , &x); n     x--; n     v[x].first = i; n   } n   for (int i = 0; i < n; i++) { n     scanf( %d , &y); n     y--; n     v[y].second = i; n   } n   sort(v.begin(), v.end()); n   tail = vector<int>(n, 0); n   cout << LIS(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void fast(); n int main() { n   fast(); n   int n, cookies[105] = {0}; n   cin >> n; n   int sum = 0; n   for (int i = 0; i < n; ++i) { n     cin >> cookies[i]; n     sum += cookies[i]; n   } n   int cnt = 0; n   for (int i = 0; i < n; ++i) { n     int cook = sum; n     cook -= cookies[i]; n     if (cook % 2 == 0) ++cnt; n   } n   cout << cnt; n   return 0; n } n void fast() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n } n


#include <bits/stdc++.h> n using namespace std; n struct st { n   long long n, v; n   bool operator<(const st &other) const { return v < other.v; } n }; n template <typename T> n string ts(T val) { n   std::ostringstream oss; n   oss << val; n   return oss.str(); n } n int main() { n   ios_base::sync_with_stdio(0); n   long long n, a, b, a1, b1, a2, b2, k1, k2, k3; n   cin >> n >> a >> b >> a1 >> b1 >> a2 >> b2; n   k1 = a; n   k2 = a1; n   k3 = a2; n   while (k1 + k2 + k3 < n && k1 < b) k1++; n   while (k1 + k2 + k3 < n && k2 < b1) k2++; n   while (k1 + k2 + k3 < n && k3 < b2) k3++; n   cout << k1 <<     << k2 <<     << k3; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n const int K = 1048576; n int n; n long long t[2][2 * K]; n vector<int> g[N]; n int T[N]; n long long h[N], x[N], dp[N]; n long long total; n void build() { n   for (int i = 0; i < 2 * K; i++) t[0][i] = t[1][i] = 0; n } n void upd(int id, long long X, int type) { n   id += K - 1; n   while (id > 0) { n     t[type][id] += X; n     id /= 2; n   } n } n long long get(int L, int R, int type) { n   if (R == 0) n     return 0; n   else if (type == 0 && R == K) n     return 2e18; n   L += K - 1; n   R += K - 1; n   long long res = 0; n   while (L <= R) { n     if (L % 2 == 1) res += t[type][L++]; n     if (R % 2 == 0) res += t[type][R--]; n     L /= 2; n     R /= 2; n   } n   return res; n } n void dfs(int v) { n   upd(T[v], x[v] * T[v], 0); n   upd(T[v], x[v], 1); n   int l = 1, r = K; n   while (l + 1 < r) { n     int mid = (l + r) / 2; n     if (h[v] + h[v] + get(1, mid - 1, 0) <= total) n       l = mid; n     else n       r = mid; n   } n   dp[v] = get(1, l - 1, 1); n   long long residue = total - get(1, l - 1, 0) - h[v] - h[v]; n   dp[v] += min(t[0][l + K - 1], residue) / l; n   for (int u : g[v]) { n     h[u] += h[v]; n     dfs(u); n   } n   if (v == 1) { n     for (int u : g[v]) dp[v] = max(dp[v], dp[u]); n   } else { n     long long max1 = 0, max2 = 0; n     for (int u : g[v]) { n       if (dp[u] > max1) { n         max2 = max1; n         max1 = dp[u]; n       } else if (dp[u] > max2) { n         max2 = dp[u]; n       } n     } n     dp[v] = max(dp[v], max2); n   } n   upd(T[v], -x[v] * T[v], 0); n   upd(T[v], -x[v], 1); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> total; n   for (int i = 1; i <= n; i++) cin >> x[i]; n   for (int i = 1; i <= n; i++) cin >> T[i]; n   for (int i = 2; i <= n; i++) { n     int parent; n     cin >> parent >> h[i]; n     g[parent].push_back(i); n   } n   build(); n   h[1] = 0; n   dfs(1); n   cout << dp[1] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int z[1000005]; n string s; n void z_function() { n   int n = (int)s.length(); n   for (int i = 0; i < n; i++) z[i] = 0; n   for (int i = 1, l = 0, r = 0; 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) l = i, r = i + z[i] - 1; n   } n } n long long power(long long a, long long b, long long m) { n   long long x = 1; n   a %= m; n   while (b) { n     if ((1 & b)) x = x * a % m; n     a = a * a % m; n     b >>= 1; n   } n   return x; n } n int y[1000005]; n int main() { n   int n, m; n   scanf( %d , &n); n   scanf( %d , &m); n   cin >> s; n   z_function(); n   int p = s.length(); n   for (int i = 1; i <= m; i++) { n     scanf( %d , &y[i]); n   } n   y[m + 1] = n + 1; n   int pos = 0; n   pos += y[1] - 1; n   for (int i = 2; i <= m + 1; i++) { n     if (y[i] < y[i - 1] + p) { n       if (z[y[i] - y[i - 1]] < p - y[i] + y[i - 1]) { n         printf( 0 ); n         return 0; n       } n     } else n       pos += y[i] - y[i - 1] - p; n   } n   printf( %lld n , power(26, pos, 1000000007)); n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using lll = __int128_t; n const int N = 105; n int n; n vector<int> v[N]; n void del(int x, int y) { n   v[x].erase(find(v[x].begin(), v[x].end(), y)); n   v[y].erase(find(v[y].begin(), v[y].end(), x)); n } n int f[N], rk[N]; n lll dfs(int pos, int fa) { n   f[pos] = fa; n   vector<lll> vv; n   for (auto &i : v[pos]) n     if (i != fa) vv.push_back(dfs(i, pos)); n   if (vv.empty()) return 0; n   vector<int> ee(vv.size()); n   auto chk = [&](lll x) { n     auto val = vv; n     int mxid = -1; n     lll mx; n     for (int i = N - 1; i >= 0; --i) n       if ((x >> i) & 1) { n         mx = -(lll(1) << N); n         for (int j = 0; j < int(vv.size()); j++) n           if (val[j] > mx) mx = val[j], mxid = j; n         ee[mxid] = i; n         val[mxid] -= lll(1) << i; n       } n     for (int j = 0; j < int(vv.size()); j++) n       if (val[j] >= 0) return 0; n     return 1; n   }; n   lll tt = (lll(1) << N) - 1; n   for (int i = N - 1; i >= 0; --i) n     if (chk(tt - (lll(1) << i))) tt -= lll(1) << i; n   assert(chk(tt)); n   int idx = 0; n   for (auto &i : v[pos]) n     if (i != fa) rk[i] = ee[idx++]; n   lll ret = 0; n   for (int i = 0; i < int(vv.size()); i++) n     ret |= vv[i] & (~((lll(1) << ee[i]) - 1)) | lll(1) << ee[i]; n   return ret; n } n int xx; n inline void upd(int x, int y) { n   if (f[x] == y) swap(x, y); n   if (!xx || rk[y] > rk[xx]) xx = y; n } n void gete(int pos, int fa) { n   for (auto &i : v[pos]) n     if (i != fa) gete(i, pos), upd(pos, i); n } n int main() { n   ios::sync_with_stdio(false); n   cin >> n; n   int t1, t2; n   for (int i = 1; i < n; i++) n     cin >> t1 >> t2, v[t1].push_back(t2), v[t2].push_back(t1); n   dfs(1, 0); n   int x = 1; n   while (!v[x].empty()) { n     xx = 0; n     gete(x, 0); n     del(xx, f[xx]); n     cout <<  ?   << xx <<     << f[xx] << endl; n     cin >> x; n   } n   cout <<  !   << x << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long x, n; n   cin >> x >> n; n   int pp = n % 4; n   long long z; n   if (pp == 0) n     z = 0; n   else if (pp == 1) n     z = -n; n   else if (pp == 2) n     z = 1; n   else n     z = n + 1; n   long long dre = x % 2 == 0 ? 1 : -1; n   x += dre * z; n   cout << x << endl; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin.tie(NULL); n   long long t = 1; n   cin >> t; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = (int)(1e5 + 123); n vector<int> gg[MAXN], gb[MAXN]; n bool was[MAXN], been[MAXN]; n int cnt = 0; n void dfs(int v) { n   cnt++; n   was[v] = been[v] = true; n   for (auto to : gg[v]) { n     if (was[to]) continue; n     dfs(to); n   } n } n int main() { n   int n; n   cin >> n; n   int good; n   cin >> good; n   for (int i = 1; i <= good; i++) { n     int a, b; n     cin >> a >> b; n     gg[a].push_back(b); n     gg[b].push_back(a); n   } n   int bad; n   cin >> bad; n   for (int i = 1; i <= bad; i++) { n     int a, b; n     cin >> a >> b; n     gb[a].push_back(b); n     gb[b].push_back(a); n   } n   int ans = 0; n   for (int i = 1; i <= n; i++) { n     if (was[i]) continue; n     cnt = 0; n     dfs(i); n     bool no = false; n     for (int j = 1; j <= n; j++) { n       if (been[j]) { n         for (auto to : gb[j]) n           if (been[to]) { n             no = true; n             break; n           } n       } n       if (no) break; n     } n     if (!no) ans = max(ans, cnt); n     for (int j = 1; j <= n; j++) been[j] = false; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   int n; n   cin >> n; n   string s; n   cin >> s; n   char arr[n + 10]; n   if (n % 2 == 1) { n     int temp = n / 2; n     int j = 0; n     int k = 1; n     for (int i = 0; i < (int)s.size(); i++) { n       if (i % 2 == 0) { n         arr[temp + j++] = s[i]; n       } else { n         arr[temp - k++] = s[i]; n       } n     } n     string ans(arr, arr + n); n     cout << ans << endl; n   } else { n     int temp = n / 2 - 1; n     int j = 0; n     int k = 1; n     for (int i = 0; i < (int)s.size(); i++) { n       if (i % 2 == 0) { n         arr[temp - j++] = s[i]; n       } else { n         arr[temp + k++] = s[i]; n       } n     } n     string ans(arr, arr + n); n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 7; n int n, a[N], pre[N], suf[N]; n char ans[N]; n struct Fenwick { n   int n, c[N]; n   void init(int _n) { n     n = _n; n     memset(c, 0, sizeof(c[0]) * (n + 1)); n   } n   void upd(int i, int v) { n     while (i <= n) { n       c[i] = max(c[i], v); n       i += i & -i; n     } n   } n   int qry(int i) { n     int r = 0; n     while (i > 0) { n       r = max(r, c[i]); n       i -= i & -i; n     } n     return r; n   } n } fw; n void solve(int f[]) { n   fw.init(*max_element(a, a + n)); n   for (int i = (0); i < (n); ++i) { n     f[i] = fw.qry(a[i] - 1) + 1; n     fw.upd(a[i], f[i]); n   } n } n map<pair<int, int>, int> R; n int main() { n   scanf( %d , &n); n   for (int i = (0); i < (n); ++i) scanf( %d , a + i); n   solve(pre); n   reverse(a, a + n); n   int L = *max_element(a, a + n); n   for (int i = (0); i < (n); ++i) a[i] = L - a[i] + 1; n   solve(suf); n   reverse(suf, suf + n); n   L = *max_element(pre, pre + n); n   for (int i = (0); i < (n); ++i) n     if (pre[i] + suf[i] - 1 == L) { n       ++R[make_pair(pre[i], suf[i])]; n     } else { n       ans[i] =  1 ; n     } n   for (int i = (0); i < (n); ++i) n     if (pre[i] + suf[i] - 1 == L) n       ans[i] =  2  + (R[make_pair(pre[i], suf[i])] == 1); n   ans[n] =   0 ; n   puts(ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct po { n   int x, id, st; n } a[444444]; n int n, m, i, j, x, y, t, k, ans[111111], id; n bool cmp(po a, po b) { n   if (a.x == b.x) { n     return a.st < b.st; n   } n   return a.x < b.x; n } n int main() { n   cin >> n >> m; n   for (i = 1; i <= n; i++) { n     scanf( %d%d , &x, &y); n     a[++t].x = k + 1, a[t].id = i, a[t].st = 0; n     k += x * y; n   } n   for (i = 1; i <= m; i++) { n     scanf( %d , &x); n     a[++t].x = x, a[t].id = i, a[t].st = 1; n   } n   sort(a + 1, a + t + 1, &cmp); n   for (i = 1; i <= t; i++) { n     if (!a[i].st) { n       id = a[i].id; n     } else { n       ans[a[i].id] = id; n     } n   } n   for (i = 1; i <= m; i++) { n     printf( %d n , ans[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> fac; n void solve() { n   long long int n, s, c = 0; n   cin >> n; n   vector<long long int> a, o; n   for (long long int i = 0; i < n; i++) { n     cin >> s; n     a.push_back(s); n   } n   for (long long int i = 0; i < n; i++) { n     cin >> s; n     o.push_back(s); n   } n   int a1 = *min_element(a.begin(), a.end()); n   int o1 = *min_element(o.begin(), o.end()); n   for (long long int i = 0; i < n; i++) { n     long long int e = a[i] - a1; n     long long int f = o[i] - o1; n     c += max(e, f); n   } n   cout << c << endl; n } n int main() { n   int t; n   cin >> t; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n; n   cin >> n; n   vector<vector<int> > adj(n, vector<int>(n, 0)); n   set<int> s; n   int x; n   for (int i = 2; i < n + 1; i++) { n     cin >> x; n     adj[x - 1][i - 1] = 1; n     s.insert(x - 1); n   } n   stack<int> st; n   int i = 0; n   int j; n   int count = 0; n   while (i < n) { n     if (s.find(i) == s.end()) { n       i++; n       continue; n     } n     for (j = 0; j < n; j++) { n       if (adj[i][j] == 1 && (s.find(j) == s.end())) { n         count++; n       } n     } n     if (count < 3) { n       cout <<  No  << endl; n       return 0; n     } n     count = 0; n     i++; n   } n   cout <<  Yes  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long b, d, s, res = 0; n   cin >> b; n   cin >> d; n   cin >> s; n   long long val = max(b, max(d, s)) - 1; n   if (val > b) res += val - b; n   if (val > d) res += val - d; n   if (val > s) res += val - s; n   cout << res; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   bool flag; n   vector<int> op; n }; n map<string, node> def; n map<char, int> op; n void del_space(string tmp) { n   int x = tmp.find_first_not_of(   ); n   tmp.erase(0, x); n   x = tmp.find_last_not_of(   ); n   tmp.erase(x + 1, tmp.length()); n   string s =   ; n   for (int i = 0; i < tmp.length(); i++) { n     if (tmp[i] !=    ) { n       s += tmp[i]; n     } else { n       if (tmp[i + 1] !=    ) s += tmp[i]; n     } n   } n   x = s.find( define ) + 7; n   int y = x; n   while (s[y] !=    ) y++; n   string s1, s2; n   s1.append(s.begin() + x, s.begin() + y); n   for (int i = y + 1; i < s.length(); i++) { n     if (s[i] !=    ) s2 += s[i]; n   } n   node tmp_node; n   tmp_node.flag = 0; n   int cnt = 0; n   for (int i = 0; i < s2.length(); i++) { n     if (s2[i] ==  ( ) cnt++; n     if (s2[i] ==  ) ) cnt--; n     if (op[s2[i]] && cnt == 0) { n       tmp_node.op.push_back(op[s2[i]]); n     } n   } n   for (int i = 0; i < s2.length(); i++) { n     if (s2[i] ==  *  || s2[i] ==  / ) { n       string ss1, ss2; n       bool bracket = 0; n       if (s2[i + 1] ==  ( ) bracket = 1; n       for (int j = i + 1; s2[j] !=  )  && !op[s2[j]] && j < s2.length(); j++) { n         ss1 += s2[j]; n       } n       map<string, node>::iterator it = def.find(ss1); n       if (it != def.end()) { n         if (def[ss1].flag == 1) { n           tmp_node.flag = 1; n         } n         if (bracket == 0) { n           for (int k = 0; k < def[ss1].op.size(); k++) { n             if (def[ss1].op[k] == 1 || def[ss1].op[k] == 2) { n               tmp_node.flag = 1; n             } n           } n         } n       } n       bracket = 0; n       if (s2[i - 1] ==  ) ) bracket = 1; n       for (int j = i - 1; s2[j] !=  (  && !op[s2[j]] && j >= 0; j--) { n         ss2 += s2[j]; n       } n       reverse(ss2.begin(), ss2.end()); n       it = def.find(ss2); n       if (it != def.end()) { n         if (def[ss2].flag == 1) { n           tmp_node.flag = 1; n         } n         if (bracket == 0) { n           for (int k = 0; k < def[ss2].op.size(); k++) { n             if (def[ss2].op[k] == 1 || def[ss2].op[k] == 2) { n               tmp_node.flag = 1; n             } n           } n         } n       } n     } n     if (s2[i] ==  - ) { n       string ss1; n       bool bracket = 0; n       if (s2[i + 1] ==  ( ) bracket = 1; n       for (int j = i + 1; s2[j] !=  )  && !op[s2[j]] && j < s2.length(); j++) { n         ss1 += s2[j]; n       } n       map<string, node>::iterator it = def.find(ss1); n       if (it != def.end()) { n         if (def[ss1].flag == 1) { n           tmp_node.flag = 1; n         } n         if (bracket == 0) { n           for (int k = 0; k < def[ss1].op.size(); k++) { n             if (def[ss1].op[k] == 1 || def[ss1].op[k] == 2) { n               tmp_node.flag = 1; n             } n           } n         } n       } n     } n     if (s2[i] ==  / ) { n       string ss1; n       bool bracket = 0; n       if (s2[i + 1] ==  ( ) bracket = 1; n       for (int j = i + 1; s2[j] !=  )  && !op[s2[j]] && j < s2.length(); j++) { n         ss1 += s2[j]; n       } n       map<string, node>::iterator it = def.find(ss1); n       if (it != def.end()) { n         if (def[ss1].flag == 1 || (bracket == 0 && !def[ss1].op.empty())) { n           tmp_node.flag = 1; n         } n       } n     } n   } n   def[s1] = tmp_node; n } n int main() { n   op[ + ] = 1; n   op[ - ] = 2; n   op[ * ] = 3; n   op[ / ] = 4; n   int n, flag = 1; n   cin >> n; n   getchar(); n   while (n--) { n     string tmp; n     getline(cin, tmp); n     del_space(tmp); n   } n   string tmp, exp; n   getline(cin, tmp); n   int x = tmp.find_first_not_of(   ); n   tmp.erase(0, x); n   x = tmp.find_last_not_of(   ); n   tmp.erase(x + 1, tmp.length()); n   for (int i = 0; i < tmp.length(); i++) { n     if (tmp[i] !=    ) { n       exp += tmp[i]; n     } n   } n   for (map<string, node>::iterator it = def.begin(); it != def.end(); ++it) { n     if (exp.find(it->first) != string::npos && it->second.flag == 1) { n       cout <<  Suspicious  << endl; n       return 0; n     } n   } n   for (int i = 0; i < exp.length(); i++) { n     if (exp[i] ==  *  || exp[i] ==  / ) { n       string s1, s2; n       bool bracket = 0; n       if (exp[i + 1] ==  ( ) bracket = 1; n       for (int j = i + 1; exp[j] !=  )  && !op[exp[j]] && j < exp.length(); n            j++) { n         s1 += exp[j]; n       } n       map<string, node>::iterator it = def.find(s1); n       if (it != def.end()) { n         if (def[s1].flag == 1) { n           cout <<  Suspicious  << endl; n           return 0; n         } n         if (bracket == 0) { n           for (int k = 0; k < def[s1].op.size(); k++) { n             if (def[s1].op[k] == 1 || def[s1].op[k] == 2) { n               cout <<  Suspicious  << endl; n               return 0; n             } n           } n         } n       } n       bracket = 0; n       if (exp[i - 1] ==  ) ) bracket = 1; n       for (int j = i - 1; exp[j] !=  (  && !op[exp[j]] && j >= 0; j--) { n         s2 += exp[j]; n       } n       reverse(s2.begin(), s2.end()); n       it = def.find(s2); n       if (it != def.end()) { n         if (def[s2].flag == 1) { n           cout <<  Suspicious  << endl; n           return 0; n         } n         if (bracket == 0) { n           for (int k = 0; k < def[s2].op.size(); k++) { n             if (def[s2].op[k] == 1 || def[s2].op[k] == 2) { n               cout <<  Suspicious  << endl; n               return 0; n             } n           } n         } n       } n     } n     if (exp[i] ==  - ) { n       string s1; n       bool bracket = 0; n       if (exp[i + 1] ==  ( ) bracket = 1; n       for (int j = i + 1; exp[j] !=  )  && !op[exp[j]] && j < exp.length(); n            j++) { n         s1 += exp[j]; n       } n       map<string, node>::iterator it = def.find(s1); n       if (it != def.end()) { n         if (def[s1].flag == 1) { n           cout <<  Suspicious  << endl; n           return 0; n         } n         if (bracket == 0) { n           for (int k = 0; k < def[s1].op.size(); k++) { n             if (def[s1].op[k] == 1 || def[s1].op[k] == 2) { n               cout <<  Suspicious  << endl; n               return 0; n             } n           } n         } n       } n     } n     if (exp[i] ==  / ) { n       string s1; n       bool bracket = 0; n       if (exp[i + 1] ==  ( ) bracket = 1; n       for (int j = i + 1; exp[j] !=  )  && !op[exp[j]] && j < exp.length(); n            j++) { n         s1 += exp[j]; n       } n       map<string, node>::iterator it = def.find(s1); n       if (it != def.end()) { n         if (def[s1].flag == 1 || (bracket == 0 && !def[s1].op.empty())) { n           cout <<  Suspicious  << endl; n           return 0; n         } n       } n     } n   } n   cout <<  OK  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long ans = 0; n int n, m, x, y; n int main() { n   while (scanf( %d%d%d%d , &n, &x, &m, &y) == 4) { n     if (x > y) { n       n ^= m ^= n ^= m; n       x ^= y ^= x ^= y; n     } n     ans = n + 1; n     int l, r; n     for (int i = 1; i <= m; ++i) { n       l = y - i; n       r = y + i; n       if (l <= x - n || l >= x + n) n         ++ans; n       else { n         r = min(r - 1, n + x); n         if (l >= x) n           ans += ((r - l) << 1); n         else n           ans += ((r - x - (x - l)) << 1) + 1; n       } n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long paw[30]; n void pw() { n   paw[0] = 1; n   for (int i = 1; i < 20; i++) paw[i] = 10 * paw[i - 1]; n } n long long rev(long long a) { n   long long ret = 0; n   vector<int> v; n   while (a > 0) { n     v.push_back(a % 10); n     a /= 10; n   } n   for (int i = 0; i < v.size(); i++) ret += (v[i] * paw[v.size() - i - 1]); n   return ret; n } n int main() { n   long long a1, a2; n   cin >> a1 >> a2; n   pw(); n   cout << a1 + rev(a2) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int count = 0, k = 0, res = 0; n     string s; n     cin >> s; n     for (int i = 0; i < s.length(); i++) { n       count = count + (s[i] -  0 ); n     } n     for (int i = 0; i < s.length(); i++) { n       if (s[i] ==  1 ) { n         k = 1; n         count--; n       } n       if (s[i] ==  0  && k == 1 && count > 0) { n         res++; n       } n     } n     cout << res << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int xp, nb; n   cin >> xp >> nb; n   cout << min(xp, min(nb, (xp + nb) / 3)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1010; n int n, c[N][N]; n struct node { n   int v, pos; n } a[N]; n inline bool cmp(node x, node y) { return x.v > y.v; } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) scanf( %d , &a[i].v), a[i].pos = i; n   sort(a + 1, a + n + 1, cmp); n   for (int i = 1; i <= n; ++i) n     for (int j = 1, k = i; j <= a[i].v; ++j, k = k % (n + 1) + 1) n       c[k][a[i].pos] = 1; n   printf( %d n , n + 1); n   for (int i = 1; i <= n + 1; puts(  ), ++i) n     for (int j = 1; j <= n; ++j) printf( %d , c[i][j]); n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:33554432 ) n using namespace std; n int P[1 << 18]; n int B[1 << 18]; n int C[1 << 18]; n int S[1 << 18]; n long long R[1 << 18]; n vector<pair<int, int> > E[1 << 18]; n int gMark, gS; n int Parent(int a) { n   if (P[a] == a) return a; n   return P[a] = Parent(P[a]); n } n void dfs(int a, int p) { n   B[a] = gMark; n   int i; n   for (i = 0; i < int((E[a]).size()); i++) { n     int b = E[a][i].first; n     if (b == p) continue; n     dfs(b, a); n     S[a] += S[b]; n   } n   S[a] += C[a]; n } n void dfs2(int a, int p) { n   B[a] = gMark; n   int i; n   for (i = 0; i < int((E[a]).size()); i++) { n     int b = E[a][i].first; n     if (b == p) continue; n     dfs2(b, a); n     long long s = S[b]; n     R[E[a][i].second] = s * (gS - s); n   } n } n int main() { n   int n; n   scanf( %d , &n); n   int i, j; n   vector<pair<pair<int, int>, pair<int, int> > > v; n   for (i = 0; i < n - 1; i++) { n     int a, b, c; n     scanf( %d%d%d , &a, &b, &c); n     v.push_back(make_pair(pair<int, int>(c, i), pair<int, int>(a - 1, b - 1))); n   } n   sort((v).begin(), (v).end()); n   for (i = 0; i < n + n; i++) { n     P[i] = i; n     C[i] = 1; n     S[i] = 0; n   } n   memset(B, 0, sizeof(B)); n   gMark = 0; n   int prev = 0; n   int cnt = n; n   for (i = 1; i < n; i++) n     if (i == n - 1 || v[i].first.first != v[i - 1].first.first) { n       for (j = prev; j < i; j++) { n         int a = Parent(v[j].second.first); n         int b = Parent(v[j].second.second); n         E[a].push_back(pair<int, int>(b, v[j].first.second)); n         E[b].push_back(pair<int, int>(a, v[j].first.second)); n       } n       ++gMark; n       for (j = prev; j < i; j++) { n         int a = Parent(v[j].second.first); n         if (B[a] != gMark) dfs(a, -1); n       } n       ++gMark; n       for (j = prev; j < i; j++) { n         int a = Parent(v[j].second.first); n         if (B[a] != gMark) { n           gS = S[a]; n           dfs2(a, -1); n         } n       } n       for (j = prev; j < i; j++) { n         int a = Parent(v[j].second.first); n         int b = Parent(v[j].second.second); n         P[a] = P[b] = cnt; n         C[cnt] = C[a] + C[b]; n         ++cnt; n       } n       prev = i; n     } n   long long res = 0; n   for (i = 0; i < n - 1; i++) res = max(res, R[i]); n   vector<int> ret; n   for (i = 0; i < n - 1; i++) n     if (R[i] == res) ret.push_back(i + 1); n   printf( %lld %d n , res + res, int((ret).size())); n   for (i = 0; i < int((ret).size()); i++) n     printf( %d%c , ret[i], i == int((ret).size()) - 1 ?   n  :    ); n   return 0; n }; n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n using namespace std; n vector<long long> v; n void lucky(long long x) { n   queue<long long> q; n   q.push(x); n   while (x < 1e9) { n     x = q.front(); n     q.pop(); n     x *= 10; n     q.push(x + 4); n     q.push(x + 7); n     v.push_back(x + 4); n     v.push_back(x + 7); n   } n } n int next(long long x) { n   int str = 0, ed = v.size() - 1, ans; n   while (str <= ed) { n     int mid = str + ((ed - str) / 2); n     if (v[mid] >= x) { n       ans = mid; n       ed = mid - 1; n     } else n       str = mid + 1; n   } n   return ans; n } n int main() { n   long long x, y; n   cin >> x >> y; n   lucky(0); n   unsigned long long sum = 0; n   sort(v.begin(), v.end()); n   int z = next(x); n   if (y <= v[z]) n     return cout << (y - x + 1) * v[z], 0; n   else { n     sum += (v[z] - x + 1) * v[z]; n     int i; n     for (i = z + 1; v[i] <= y; i++) { n       sum += (v[i] - v[i - 1]) * v[i]; n     } n     sum += (y - v[i - 1]) * v[i]; n   } n   cout << sum; n } n


#include <bits/stdc++.h> n using namespace std; n void pairsort(long long a[], long long b[], long long n) { n   pair<long long, long long> pairt[n]; n   for (long long i = 0; i < n; i++) { n     pairt[i].first = a[i]; n     pairt[i].second = b[i]; n   } n   sort(pairt, pairt + n); n   for (long long i = 0; i < n; i++) { n     a[i] = pairt[i].first; n     b[i] = pairt[i].second; n   } n } n int32_t main() { n   long long n, k; n   cin >> n >> k; n   long long a[n]; n   for (long long i = 0; i < n; i++) { n     cin >> a[i]; n   } n   long long f = 0; n   long long c = 0; n   for (long long i = 0; i < n - 1; i++) { n     c += a[i] / k; n     a[i] = a[i] % k; n     if (a[i] != 0) { n       if (k - a[i] < a[i + 1]) n         a[i + 1] -= (k - a[i]); n       else n         a[i + 1] = 0; n       c += 1; n     } n     a[i] = 0; n   } n   c += a[n - 1] / k; n   if (a[n - 1] % k != 0) c += 1; n   cout << c; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1000LL * 1000 * 1000 + 7; n const int INF = 1000 * 1000 * 1000; n const long long base = 1e18; n const int MAXN = 600000; n const double EPS = 1e-9; n const double PI = acos(-1.); n long long binPow(long long a, long long x) { n   long long res = 1; n   while (x) { n     if (x & 1) { n       res *= a; n       res %= MOD; n     } n     a *= a; n     a %= MOD; n     x /= 2; n   } n   return res; n } n long long dpT[200000][2]; n long long n, m; n long long a[200000]; n long long b[200000]; n long long dp(int pos, int more) { n   if (pos == n) { n     return more; n   } n   if (dpT[pos][more] != -1) return dpT[pos][more]; n   if (more == 1) { n     long long ans = dp(pos + 1, more); n     if (a[pos] == 0) { n       ans *= m; n       ans %= MOD; n     } n     if (b[pos] == 0) { n       ans *= m; n       ans %= MOD; n     } n     return dpT[pos][more] = ans; n   } n   if (a[pos] && b[pos] && a[pos] < b[pos]) return dpT[pos][more] = 0; n   if (a[pos] && b[pos] && a[pos] > b[pos]) n     return dpT[pos][more] = dp(pos + 1, 1); n   if (a[pos] && b[pos] && a[pos] == b[pos]) { n     return dpT[pos][more] = dp(pos + 1, 0); n   } n   if (a[pos] == 0 && b[pos]) { n     long long res = 0; n     res += (dp(pos + 1, 1) * (m - b[pos])) % MOD; n     res %= MOD; n     res += dp(pos + 1, 0); n     res %= MOD; n     return dpT[pos][more] = res; n   } n   if (a[pos] && b[pos] == 0) { n     long long res = 0; n     res += (dp(pos + 1, 1) * (a[pos] - 1)) % MOD; n     res %= MOD; n     res += dp(pos + 1, 0); n     res %= MOD; n     return dpT[pos][more] = res; n   } n   long long res = 0; n   long long cur = (m * (m - 1) / 2) % MOD; n   res += (dp(pos + 1, 1) * (cur)) % MOD; n   res %= MOD; n   res += (dp(pos + 1, 0) * m) % MOD; n   res %= MOD; n   return dpT[pos][more] = res; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> m; n   int cnt = 0; n   for (int i = (0); i < (n); ++i) { n     cin >> a[i]; n     if (!a[i]) cnt++; n   } n   for (int i = (0); i < (n); ++i) { n     cin >> b[i]; n     if (!b[i]) cnt++; n   } n   memset(dpT, -1, sizeof dpT); n   long long div = binPow(m, cnt); n   long long y = binPow(div, MOD - 2); n   cout << (dp(0, 0) * y) % MOD; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int t, s, p; n int main() { n   scanf( %d %d %d , &t, &s, &p); n   int l = t; n   int x1 = 0, x2 = s; n   int co = 1; n   while (x1 < l) { n     x1 = x1 + p; n     x2 = x2 + p - 1; n     if (x1 >= l && x2 >= l) n       break; n     else if (x1 >= x2) { n       x1 = 0; n       co++; n     } n   } n   cout << co << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n typedef long long ll; n int main() n { n     ios_base::sync_with_stdio(false); n     cin.tie(0); n     int t; n     cin >> t; n     while (t--) { n         int u, v; cin >> u >> v; n         if (u > v) n             printf( NO n ); n         else if (u == v) n             printf( YES n ); n         else { n             int U = 0, V = 0, fail = 0; n             // (u) 1 0111 0111 -> 1 1010 0110 (v) n             // bit0: {0, 1, 2, 4, 5, 6, 8} -> bit1: {1, 2, 5, 7, 8} n             for (int i = 0; i < 30; i++) { n                 U += u >> i & 1; n                 V += v >> i & 1; n                 fail |= U < V; n             } n             printf(fail ?  NO n  :  YES n ); n         } n     } n     return 0; n } n /* n 3 12 n YES n 6 9 n NO 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   long m, n; n   cin >> m >> n; n   long ar[n], br[n]; n   for (int i = 0; i < n; i++) { n     cin >> ar[i] >> br[i]; n   } n   int tmp; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n - 1; j++) { n       if (br[j] < br[j + 1]) { n         tmp = br[j]; n         br[j] = br[j + 1]; n         br[j + 1] = tmp; n         tmp = ar[j]; n         ar[j] = ar[j + 1]; n         ar[j + 1] = tmp; n       } n     } n   } n   int cnt = 0, i = 0; n   long sum = 0; n   while (cnt != m && i < n) { n     while (ar[i] != 0) { n       sum += br[i]; n       ar[i]--; n       cnt++; n       if (cnt == m) break; n     } n     i++; n   } n   cout << sum; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int inp[200200]; n int sum[200200]; n char str[200200]; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) scanf( %d , &inp[i]); n   scanf( %s , str); n   int len = strlen(str); n   reverse(inp, inp + n); n   reverse(str, str + len); n   sum[n] = 0; n   for (int i = n - 1; ~i; --i) sum[i] = sum[i + 1] + inp[i]; n   int v = 0, ans = 0; n   for (int i = 0; i < len; ++i) { n     if (str[i] ==  1 ) { n       ans = max(ans, v + sum[i + 1]); n       v += inp[i]; n     } n     if (0) printf(  << i %2d, v %2d, ans %2d n , i, v, ans); n   } n   if (0) { n     for (int i = 0; i < n; ++i) printf(  %d n , inp[i]); n     puts(  ); n     for (int i = 0; i < n; ++i) printf(  %d n , sum[i]); n     puts(  ); n   } n   if (0) printf( < v %d n , v); n   printf( %d n , max(ans, v)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n pair<int, int> V[2001010]; n int cnt[2002]; n long long cum[2002], N; n string s; n void make(int l, int r) { n   while (l >= 0 && r < (int)s.size()) { n     if (s[l] == s[r]) { n       V[N++] = (make_pair(l, r)); n       cnt[l]++; n     } else n       break; n     l--, r++; n   } n } n int main() { n   N = 0; n   cin >> s; n   memset(cnt, 0, sizeof(cnt)); n   for (int i = 0; i < (int)s.size(); ++i) { n     make(i, i); n     make(i - 1, i); n   } n   cum[0] = 0; n   for (int i = 0; i < (int)s.size(); ++i) cum[i + 1] = cum[i] + cnt[i]; n   long long Sm = cum[s.size()]; n   long long c = 0; n   for (int i = 0; i < N; ++i) { n     c += (Sm - cum[V[i].second + 1]); n   } n   cout << c << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int dx[4] = {1, 0, -1, 0}; n int dy[4] = {0, 1, 0, -1}; n char E[1005][1005]; n int N, M; n struct Node { n   int step[3]; n } s[1005][1005]; n struct use { n   int x, y; n }; n void bfs(int x) { n   queue<use> que; n   use u, v; n   for (int i = 0; i < N; i++) { n     for (int j = 0; j < M; j++) { n       if (E[i][j] == x +  1 ) { n         u.x = i, u.y = j; n         s[i][j].step[x] = 0; n         que.push(u); n       } n     } n   } n   while (!que.empty()) { n     u = que.front(); n     que.pop(); n     for (int i = 0; i < 4; i++) { n       v = u; n       v.x += dx[i]; n       v.y += dy[i]; n       if (v.x >= 0 && v.x < N && v.y >= 0 && v.y < M && E[v.x][v.y] !=  # ) { n         int t; n         if (E[v.x][v.y] ==  . ) n           t = 1; n         else n           t = 0; n         if (s[v.x][v.y].step[x] == -1 || n             s[u.x][u.y].step[x] + t < s[v.x][v.y].step[x]) { n           s[v.x][v.y].step[x] = s[u.x][u.y].step[x] + t; n           que.push(v); n         } n       } n     } n   } n } n int main() { n   scanf( %d %d , &N, &M); n   for (int i = 0; i < N; i++) scanf( %s , E[i]); n   memset(s, -1, sizeof(s)); n   for (int i = 0; i < 3; i++) bfs(i); n   int ans = 1 << 30; n   for (int i = 0; i < N; i++) n     for (int j = 0; j < M; j++) { n       if (s[i][j].step[0] != -1 && s[i][j].step[1] != -1 && n           s[i][j].step[2] != -1) { n         int t; n         if (E[i][j] ==  . ) n           t = 1; n         else n           t = 0; n         ans = min(ans, n                   s[i][j].step[0] + s[i][j].step[1] + s[i][j].step[2] - t * 2); n       } n     } n   if (ans == 1 << 30) { n     puts( -1 ); n     exit(0); n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char a[200][200]; n int lef[200], righ[200]; n bool is[200]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n, m, bot = 0; n   cin >> n >> m; n   for (int i = 0; i < n + 1; i++) { n     lef[i] = m - 1; n     righ[i] = 0; n   } n   for (int i = 0; i < n; i++) { n     bool f = 0; n     for (int j = 0; j < m; j++) { n       cin >> a[i][j]; n       if (a[i][j] ==  W ) { n         lef[i] = min(j, lef[i]); n         righ[i] = max(j, righ[i]); n         f = 1; n         bot = i; n       } n     } n     is[i] = f; n   } n   int cur = 0, ans = 0; n   for (int i = 0; i <= bot; i++) { n     if (is[i + 1] == 0 && is[i] == 0) continue; n     if (i % 2 == 0) { n       ans += abs(cur - max(righ[i + 1], righ[i])); n       cur = max(righ[i + 1], righ[i]); n     } else { n       ans += abs(cur - min(lef[i + 1], lef[i])); n       cur = min(lef[i + 1], lef[i]); n     } n   } n   cout << ans + bot; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n bool cmin(T &a_, T b_) { n   return a_ > b_ ? (a_ = b_, true) : false; n } n template <class T> n bool cmax(T &a_, T b_) { n   return a_ < b_ ? (a_ = b_, true) : false; n } n template <class T> n void OUT(T s_) { n   cout << s_ <<   n ; n   exit(0); n } n const int maxn = 1.0001e6; n const long long mod = 998244353; n long long n; n long long gt[maxn]; n long long power(long long a, long long b) { n   if (!b) return 1LL; n   long long c = power(a, b >> 1); n   c *= c; n   c %= mod; n   if (b & 1) { n     c *= a; n     c %= mod; n   } n   return c; n } n long long f(long long a, long long b) { n   if (a == b) return 1; n   return (gt[a] * power((gt[b] * gt[a - b]) % mod, mod - 2)) % mod; n } n void Fan_chipu() { n   cin >> n; n   gt[0] = 1; n   for (int i = (int)(1); i <= (int)(maxn - 1); ++i) n     gt[i] = gt[i - 1] * i, gt[i] %= mod; n   long long ans = 0LL; n   for (int i = (int)(0); i <= (int)(n); ++i) { n     ans += (f(n, i) * (((gt[i] - i + mod) * gt[n - i]) % mod)) % mod; n     ans %= mod; n   } n   cout << ans % mod <<   n ; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.precision(10); n   cout << fixed; n   Fan_chipu(); n } n


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


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int mod = 1000000007, c = 1, n, k; n   cin >> n >> k; n   for (int i = 1; i <= n - k; i++) c = (c * (n - k)) % mod; n   for (int i = 1; i <= k - 1; i++) c = (c * k) % mod; n   cout << c; n } n


#include <bits/stdc++.h> n bool isVowel(char c) { n   return (c ==  a  || c ==  e  || c ==  i  || c ==  o  || c ==  u  || n           c ==  A  || c ==  E  || c ==  I  || c ==  O  || c ==  U  || n           c ==  y  || c ==  Y ); n }; n int main() { n   char word[100]; n   scanf( %s , word); n   int i; n   int length; n   length = strlen(word); n   for (i = 0; i < length; i++) { n     if (!isVowel(word[i])) { n       printf( .%c , tolower(word[i])); n     } n   } n   return 0; n } n