#include <bits/stdc++.h> n using namespace std; n using LL = long long; n using ULL = unsigned long long; n using VI = vector<int>; n using VL = vector<LL>; n using PII = pair<int, int>; n using PLL = pair<LL, LL>; n void dout() { cerr << endl; } n template <typename Head, typename... Tail> n void dout(Head H, Tail... T) { n   cerr << H <<    ; n   dout(T...); n } n const int MAX = 1 << 10; n int a[MAX][MAX]; n set<int> s[MAX]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n, m, k; n   cin >> n >> m >> k; 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] != 2) s[j].insert(i); n     } n   while (k--) { n     int c; n     cin >> c; n     c--; n     int r = 0; n     while (true) { n       auto it = s[c].lower_bound(r); n       if (it == s[c].end()) break; n       int nr = *it; n       int& na = a[nr][c]; n       s[c].erase(it); n       if (na == 1) { n         c++; n       } else { n         assert(na == 3); n         c--; n       } n       na = 2; n       r = nr; n     } n     cout << c + 1 <<    ; n   } n   cout <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int t, n, i, min1, min2, max1, max2, a[10000], b[10000]; n   cin >> n; n   t = n; n   while (n--) { n     cin >> a[i] >> b[i]; n     i++; n   } n   max1 = a[0]; n   max2 = b[0]; n   min1 = a[0]; n   min2 = b[0]; n   for (i = 0; i < t; i++) { n     if (a[i] > max1) max1 = a[i]; n     if (b[i] > max2) max2 = b[i]; n     if (a[i] <= min1) min1 = a[i]; n     if (b[i] <= min2) min2 = b[i]; n   } n   max1 = max1 - min1; n   max2 = max2 - min2; n   if (max1 >= max2) n     cout << max1 * max1; n   else n     cout << max2 * max2; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x7f7f7f7f; n const double EPS = 1e-8; n int n, m; n int ans[707][707][3]; n bool used[707]; n int start; n void DFS(int v, int len = 0) { n   used[v] = true; n   for (int i = 0; i < n; i++) { n     if (ans[v][i][0] && !used[v]) { n       DFS(i, len + 1); n     } n   } n   used[v] = false; n } n int main() { n   scanf( %d %d , &n, &m); n   for (int i = 0; i < m; i++) { n     int a, b; n     scanf( %d %d , &a, &b); n     --a; n     --b; n     ans[a][b][0] = ans[b][a][0] = 1; n   } n   for (int i = 0; i < n; i++) { n     used[i] = true; n     for (int u = 0; u < n; u++) { n       if (ans[i][u][0]) { n         used[u] = true; n         for (int t = 0; t < n; t++) { n           if (ans[u][t][0] && !used[t]) ++ans[i][t][1]; n         } n         used[u] = false; n       } n     } n     used[i] = false; n   } n   long long sum = 0; n   for (int i = 0; i < n; i++) { n     used[i] = true; n     for (int u = 0; u < n; u++) { n       if (ans[i][u][0]) { n         used[u] = true; n         for (int t = 0; t < n; t++) { n           if (!used[t]) { n             ans[i][t][2] += ans[u][t][1]; n             if (ans[i][t][0]) --ans[i][t][2]; n           } n         } n         used[u] = false; n       } n     } n     used[i] = false; n   } n   for (int i = 0; i < n; i++) { n     used[i] = true; n     for (int u = 0; u < n; u++) { n       if (ans[i][u][0]) { n         used[u] = true; n         for (int t = 0; t < n; t++) { n           if (!used[t] && ans[i][t][0]) { n             sum += ans[u][t][2]; n             int minus = 0; n             if (ans[u][t][0]) minus = 1; n             sum -= (long long)(max(0, ans[i][u][1] - minus)) + n                    (max(0, ans[i][t][1] - minus)); n           } n         } n         used[u] = false; n       } n     } n     used[i] = false; n   } n   cout << sum / 5 / 2 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n pair<long long, long long> sn[100005], sp[100005]; n bool myfunction(pair<long long, long long> i, pair<long long, long long> j) { n   return (i.first > j.first); n } n int main() { n   int n, p, a, first = 0, second = 0; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d%d , &p, &a); n     if (p < 0) { n       first++; n       sn[first] = make_pair(p, a); n     } else { n       second++; n       sp[second] = make_pair(p, a); n     } n   } n   sort(sn, sn + first + 1, myfunction); n   sort(sp, sp + second + 1); n   for (int i = 1; i <= first; i++) sn[i].second += sn[i - 1].second; n   for (int i = 1; i <= second; i++) sp[i].second += sp[i - 1].second; n   sn[first + 1] = sn[first]; n   sp[second + 1] = sp[second]; n   int m = min(first, second); n   long long ans = sp[m + 1].second + sn[m].second; n   ans = max(ans, sp[m].second + sn[m + 1].second); n   printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1000010; n const long long inf = (1ll << 58ll); n int k, n, q; n int ten[10] = {1, 10, 100, 1000, 10000, 100000}; n long long F[7]; n long long dp[maxn], g[7][maxn]; n long long read() { n   long long s = 0, f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     s = s * 10 + ch -  0 ; n     ch = getchar(); n   } n   return s * f; n } n int main() { n   k = read(); n   for (int i = 0; i < 6; ++i) F[i] = read(); n   fill(dp, dp + 1 + 999999, -inf); n   dp[0] = 0; n   for (int d = 1; d <= 6; ++d) { n     long long left = 1ll * 3 * (k - 1); n     long long group = 1ll; n     while (left > 0) { n       group = min(group, left); n       long long val = 1ll * group * F[d - 1]; n       long long wei = 1ll * 3 * group * ten[d - 1]; n       for (int i = 999999; i >= wei; --i) { n         dp[i] = max(dp[i], dp[i - wei] + val); n       } n       left -= group; n       group *= 2; n     } n   } n   for (int i = 1; i <= 6; ++i) fill(g[i], g[i] + 1000000, -inf); n   for (int i = 0; i <= 999999; ++i) g[0][i] = dp[i]; n   for (int d = 1; d <= 6; ++d) { n     for (int j = 0; j <= 9; ++j) { n       long long val, wei; n       if (j == 3 || j == 6 || j == 9) n         val = 1ll * (j / 3) * F[d - 1]; n       else n         val = 0; n       wei = 1ll * j * ten[d - 1]; n       for (int i = 999999; i >= wei; i--) n         g[d][i] = max(g[d][i], g[d - 1][i - wei] + val); n     } n   } n   q = read(); n   for (int i = 1; i <= q; ++i) { n     n = read(); n     printf( %lld n , g[6][n]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, c[205], num = 1e9; n vector<int> a, ans; n void dfs(int n, vector<int> a, int d, int t) { n   if (n == 0) { n     if (d < num) { n       ans.clear(); n       num = d; n       for (int i = 0; i < d; i++) ans.push_back(c[i]); n     } n     return; n   } n   int m = a.size(); n   bool f = 1; n   for (int i = 0; i < m; i++) n     if (a[i] % 2 != 0) { n       f = 0; n       break; n     } n   if (f) { n     for (int i = 0; i < m; i++) a[i] /= 2; n     return dfs(n / 2, a, d, t + 1); n   } n   if (m == 2 && a[0] == 0 && a[1] == 1 || m == 1 && a[0] == 1) { n     c[d] = 1 << t; n     return dfs(0, a, d + 1, t + 1); n   } n   if (m == 2 && a[0] == -1 && a[1] == 0 || m == 1 && a[0] == -1) { n     c[d] = -(1 << t); n     return dfs(0, a, d + 1, t + 1); n   } n   vector<int> b; n   b.clear(); n   int lst = -1e9; n   for (int i = 0; i < m; i++) { n     int u = a[i]; n     if (u % 2 == 0) n       u /= 2; n     else n       u = (u - 1) / 2; n     if (u != lst) { n       b.push_back(u); n       lst = u; n     } n   } n   c[d] = 1 << t; n   dfs((n + 1) / 2, b, d + 1, t + 1); n   b.clear(); n   lst = -1e9; n   for (int i = 0; i < m; i++) { n     int u = a[i]; n     if (u % 2 == 0) n       u /= 2; n     else n       u = (u + 1) / 2; n     if (u != lst) { n       b.push_back(u); n       lst = u; n     } n   } n   c[d] = -(1 << t); n   dfs((n + 1) / 2, b, d + 1, t + 1); n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     int x; n     scanf( %d , &x); n     a.push_back(x); n   } n   sort(a.begin(), a.end()); n   dfs(100000, a, 0, 0); n   printf( %d n , ans.size()); n   for (int i = 0; i < ans.size(); i++) printf( %d  , ans[i]); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, ans; n int main() { n   scanf( %I64d%I64d , &n, &m); n   if (n > m) swap(n, m); n   if (n == 2) { n     if (m <= 2) n       ans = 0; n     else { n       ans = n * m; n       if (m == 3) n         ans -= 2; n       else if (m == 7) n         ans -= 2; n     } n   } else if (n == 1) { n     ans = n * m; n     long long t = ans % 6; n     if (t <= 3) n       ans -= t; n     else if (t == 4) n       ans -= 2; n     else if (t == 5) n       --ans; n   } else { n     ans = (n * m) - ((n * m) & 1); n   } n   printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t, n, a, b, c, d; n   cin >> t; n   while (t--) { n     cin >> n >> a >> b >> c >> d; n     int rice_left = a - b; n     int rice_right = a + b; n     int packeg_left = c - d; n     int packeg_right = c + d; n     rice_left *= n; n     rice_right *= n; n     if (rice_left > packeg_right || rice_right < packeg_left) n       cout <<  No  << endl; n     else n       cout <<  Yes  << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <class T1, class T2> n ostream& operator<<(ostream& os, const pair<T1, T2>& p) { n   os <<  {  << p.first <<  ,   << p.second <<  } ; n   return os; n } n template <class T> n ostream& operator<<(ostream& os, const vector<T>& p) { n   os <<  [ ; n   int i = 0; n   for (typename vector<T>::const_iterator it = p.begin(); it != p.end(); ++it) { n     os << (i == 0 ?    :  ,  ) << *it; n     i++; n   } n   os <<  ] ; n   return os; n } n template <class T> n ostream& operator<<(ostream& os, const set<T>& p) { n   os <<  [ ; n   int i = 0; n   for (typename set<T>::const_iterator it = p.begin(); it != p.end(); ++it) { n     os << (i == 0 ?    :  ,  ) << *it; n     i++; n   } n   os <<  ] ; n   return os; n } n template <class K, class V> n ostream& operator<<(ostream& os, const map<K, V>& p) { n   os <<  [ ; n   int i = 0; n   for (typename map<K, V>::const_iterator it = p.begin(); it != p.end(); ++it) { n     os << (i == 0 ?    :  ,  ) << *it; n     i++; n   } n   os <<  ] ; n   return os; n } n template <class T> n string toString(const T& o) { n   stringstream ss; n   ss << o; n   string ret; n   getline(ss, ret); n   return ret; n } n template <class T> n vector<T> subv(const vector<T>& v, int from, int to) { n   return vector<T>(v.begin() + min(max(0, from), (int)v.size()), n                    v.begin() + min(max(0, to), (int)v.size())); n } n pair<int, int> operator+(const pair<int, int>& a, const pair<int, int>& b) { n   return make_pair(a.first + b.first, a.second + b.second); n } n pair<int, int> operator*(const int& a, const pair<int, int>& b) { n   return make_pair(a * b.first, a * b.second); n } n vector<int> sex, parent, used[2], path, bestw[2]; n vector<int> was; n vector<int> points; n vector<vector<int> > adj; n vector<pair<int, int> > w[2], o[2]; n int k, mark, n; n void dfs(int v, int color) { n   if (was[v] == color) { n     points.push_back(v); n     return; n   } n   if (was[v]) return; n   was[v] = color; n   for (int i = 0; i < adj[v].size(); ++i) dfs(adj[v][i], color); n } n void go(int v) { n   if (used[k][v]) return; n   used[k][v] = 1; n   w[k][v] = o[k][v] = make_pair(0, 0); n   for (int i = 0; i < adj[v].size(); ++i) { n     int to = adj[v][i]; n     go(to); n     if (mark != to) o[k][v] = o[k][v] + max(w[k][to], o[k][to]); n   } n   for (int i = 0; i < adj[v].size(); ++i) { n     int to = adj[v][i]; n     if (to == mark) continue; n     pair<int, int> val = o[k][v] + (-1) * max(w[k][to], o[k][to]) + o[k][to] + n                          make_pair(1, sex[v] != sex[to]); n     if (w[k][v] < val) { n       bestw[k][v] = to; n       w[k][v] = val; n     } n   } n } n void make_path(int v, bool with) { n   int to = -1; n   if (with) { n     to = bestw[k][v]; n     path[to] = 1; n   } n   for (int i = 0; i < adj[v].size(); ++i) { n     int to2 = adj[v][i]; n     if (to2 != mark) make_path(to2, (to2 != to) && (w[k][to2] > o[k][to2])); n   } n } n int main() { n   cin >> n; n   adj.resize(n); n   sex.resize(n); n   path.resize(n, 0); n   parent.resize(n); n   was.resize(n, 0); n   for (int i = 0; i < 2; ++i) { n     bestw[i].resize(n, -1); n     w[i].resize(n); n     o[i].resize(n); n     used[i].resize(n, 0); n   } n   for (int i = 0; i < n; ++i) { n     int f, s; n     cin >> f >> s; n     f--, s--; n     parent[i] = f; n     sex[i] = s; n     adj[f].push_back(i); n   } n   for (int i = 0; i < n; ++i) n     if (!was[i]) dfs(i, i + 1); n   for (int i = 0; i < points.size(); ++i) { n     int v1 = points[i]; n     int v2 = parent[v1]; n     k = 0; n     mark = v1; n     go(v1); n     k = 1; n     mark = v2; n     go(v2); n     int v; n     if (max(o[0][v1], w[0][v1]) > max(o[1][v2], w[1][v2])) { n       k = 0; n       v = v1; n       mark = v1; n     } else { n       k = 1; n       v = v2; n       mark = v2; n     } n     make_path(v, w[k][v] > o[k][v]); n   } n   int all = 0, diff = 0; n   for (int i = 0; i < n; ++i) n     if (path[i]) { n       all++; n       diff += (sex[i] != sex[parent[i]]); n     } n   cout << all <<     << diff << endl; n   for (int i = 0; i < n; ++i) n     if (path[i]) cout << i + 1 <<     << parent[i] + 1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, a[20000], i, j, s = 1; n   cin >> n; n   for (i = 0; i < n; i++) { n     cin >> a[i]; n   } n   for (i = 0; i < n; i++) { n     if (a[i] % 2) { n       printf( %d n , (s + a[i]) / 2); n       s *= -1; n     } else n       printf( %d n , a[i] / 2); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1e18 + 1; n const int INF_INT = 2e9 + 100; n const long long POLTOS = 50; n const long long SOTKA = 100; n const long long P = 1e9 + 7; n const long double eps = 1e-9; n const long long base = 1000 * 1000 * 1000; n const long long M = 3050; n const long long MAXN = 1e6 + 1; n const long long MAXN1 = 2 * 1e2; n const long long N = 2e5 + 1000; n const long double pi = acos(-1); n const long long mod = 1e9 + 7; n const int S_M = 20; n const long long LEN = 96; n const long long DMOD = 500000004; n const long long PR = 32000; n int main() { n   int n, k; n   cin >> n >> k; n   vector<pair<int, int>> a(n); n   for (auto &i : a) { n     cin >> i.first; n   } n   for (auto &i : a) { n     cin >> i.second; n   } n   sort(a.begin(), a.end(), n        [&](pair<int, int> a, pair<int, int> b) { return a.second > b.second; }); n   vector<int> used(k + 1, false); n   multiset<int> b; n   for (int i = 0; i < n; ++i) { n     if (used[a[i].first]) { n       b.insert(a[i].second); n     } else { n       used[a[i].first] = 1; n     } n   } n   long long ans = 0; n   for (int i = 1; i <= k; i++) n     if (!used[i]) { n       ans += *b.begin(); n       b.erase(b.begin()); n     } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n inline long long pow(long long a, long long b, long long mod) { n   long long ans = 1; n   a %= mod; n   for (; b; b >>= 1, a = a * a % mod) n     if (b & 1) ans = ans * a % mod; n   return ans; n } n struct Node { n   int L, R; n   mutable long long val; n   Node(int L, int R = -1, long long val = 0) : L(L), R(R), val(val) {} n   inline bool operator<(const Node& b) const { return L < b.L; } n }; n std::set<Node> Chtholly; n inline std::set<Node>::iterator spilt(int pos) { n   std::set<Node>::iterator iter = Chtholly.lower_bound(Node(pos)); n   if (iter != Chtholly.end() && iter->L == pos) return iter; n   --iter; n   int L = iter->L, R = iter->R; n   long long V = iter->val; n   Chtholly.erase(iter); n   Chtholly.insert(Node(L, pos - 1, V)); n   return Chtholly.insert(Node(pos, R, V)).first; n } n inline void Add(int L, int R, long long val = 1) { n   std::set<Node>::iterator end = spilt(R + 1), iter = spilt(L); n   for (; iter != end; ++iter) iter->val += val; n } n inline void assign(int L, int R, long long val = 0) { n   std::set<Node>::iterator terR = spilt(R + 1), terL = spilt(L); n   Chtholly.erase(terL, terR); n   Chtholly.insert(Node(L, R, val)); n } n inline long long Rank(int L, int R, int k) { n   std::vector<std::pair<long long, int> > s; n   std::set<Node>::iterator end = spilt(R + 1), iter = spilt(L); n   for (; iter != end; ++iter) { n     s.push_back(std::pair<long long, int>(iter->val, iter->R - iter->L + 1)); n   } n   std::sort(s.begin(), s.end()); n   for (auto iter : s) { n     k -= iter.second; n     if (k <= 0) return iter.first; n   } n   return -1ll; n } n inline long long Sum(int L, int R, long long k, long long mod) { n   std::set<Node>::iterator end = spilt(R + 1), iter = spilt(L); n   long long ans = 0; n   for (; iter != end; ++iter) { n     ans = (ans + 1ll * (iter->R - iter->L + 1) * pow(iter->val, k, mod) % mod) % n           mod; n   } n   return ans; n } n long long n, m, seed, vmax; n const int MOD = 1e9 + 7; n inline long long rnd() { n   long long ret = seed; n   seed = (seed * 7 + 13) % MOD; n   return ret; n } n signed main() { n   int i; n   scanf( %lld%lld%lld%lld , &n, &m, &seed, &vmax); n   for (i = 1; i <= n; ++i) Chtholly.insert(Node(i, i, rnd() % vmax + 1)); n   Chtholly.insert(Node(n + 1, n + 1, 0)); n   for (i = 1; i <= m; ++i) { n     int op = rnd() % 4 + 1, L = rnd() % n + 1, R = rnd() % n + 1, x, y; n     if (L > R) L ^= R ^= L ^= R; n     x = rnd() % (op == 3 ? R - L + 1 : vmax) + 1; n     if (op == 4) y = rnd() % vmax + 1; n     if (op == 1) { n       Add(L, R, x); n     } n     if (op == 2) { n       assign(L, R, x); n     } n     if (op == 3) { n       printf( %lld n , Rank(L, R, x)); n     } n     if (op == 4) { n       printf( %lld n , Sum(L, R, x, y)); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long a, b, c; n int vis[12]; n bool ok(long long mid, int x) { n   long long m = mid % 7; n   int day = x; n   long long res[5]; n   res[1] = mid / 7; n   res[2] = mid / 7; n   res[3] = mid / 7; n   res[1] *= 3; n   res[2] *= 2; n   res[3] *= 2; n   while (m--) { n     res[vis[day]]++; n     day++; n     day %= 7; n   } n   return res[1] <= a && res[2] <= b && res[3] <= c; n } n long long ans(int x) { n   long long st = 0, en = 1e12; n   while (st < en) { n     long long mid = (st + en) / 2; n     if (ok(mid, x)) n       st = mid + 1; n     else n       en = mid; n   } n   return st; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin >> a >> b >> c; n   vis[0] = 2; n   vis[1] = 1; n   vis[2] = 1; n   vis[3] = 2; n   vis[4] = 3; n   vis[5] = 1; n   vis[6] = 3; n   long long mx = 0; n   for (int i = 0; i < 7; i++) { n     mx = max(mx, ans(i)); n   } n   cout << --mx <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 2000000000; n using namespace std; n template <typename A, typename B> n string to_string(pair<A, B> p); n template <typename A, typename B, typename C> n string to_string(tuple<A, B, C> p); n template <typename A, typename B, typename C, typename D> n string to_string(tuple<A, B, C, D> p); n string to_string(const string& s) { return     + s +    ; } n string to_string(const char* s) { return to_string((string)s); } n string to_string(bool b) { return (b ?  true  :  false ); } n string to_string(vector<bool> v) { n   bool first = true; n   string res =  { ; n   for (int i = 0; i < static_cast<int>(v.size()); i++) { n     if (!first) { n       res +=  ,  ; n     } n     first = false; n     res += to_string(v[i]); n   } n   res +=  } ; n   return res; n } n template <size_t N> n string to_string(bitset<N> v) { n   string res =   ; n   for (size_t i = 0; i < N; i++) { n     res += static_cast<char>( 0  + v[i]); n   } n   return res; n } n template <typename A> n string to_string(A v) { n   bool first = true; n   string res =  { ; n   for (const auto& x : v) { n     if (!first) { n       res +=  ,  ; n     } n     first = false; n     res += to_string(x); n   } n   res +=  } ; n   return res; n } n template <typename A, typename B> n string to_string(pair<A, B> p) { n   return  (  + to_string(p.first) +  ,   + to_string(p.second) +  ) ; n } n template <typename A, typename B, typename C> n string to_string(tuple<A, B, C> p) { n   return  (  + to_string(get<0>(p)) +  ,   + to_string(get<1>(p)) +  ,   + n          to_string(get<2>(p)) +  ) ; n } n template <typename A, typename B, typename C, typename D> n string to_string(tuple<A, B, C, D> p) { n   return  (  + to_string(get<0>(p)) +  ,   + to_string(get<1>(p)) +  ,   + n          to_string(get<2>(p)) +  ,   + to_string(get<3>(p)) +  ) ; n } n void debug_out() { cerr << endl; } n template <typename Head, typename... Tail> n void debug_out(Head H, Tail... T) { n   cerr <<     << to_string(H); n   debug_out(T...); n } n long long n; n vector<int> adj[200005]; n vector<bool> visited; n vector<int> ans; n void dfs(int v) { n   visited[v] = true; n   for (int u : adj[v]) { n     if (!visited[u]) dfs(u); n   } n   ans.push_back(v); n } n void topological_sort() { n   visited.assign(n, false); n   ans.clear(); n   for (int i = 0; i < n; ++i) { n     if (!visited[i]) dfs(i); n   } n   reverse(ans.begin(), ans.end()); n } n long long p2(long long x) { return x * x; } n long long dist2(pair<long long, long long> p, pair<long long, long long> q) { n   return p2(p.first - q.first) + p2(p.second - q.second); n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   vector<long long> t(n); n   vector<double> p(n); n   vector<pair<long long, long long> > x(n); n   for (int i = (0); i < (int)(n); i++) { n     long long ti, xi, yi; n     double pi; n     cin >> xi >> yi >> ti >> pi; n     t[i] = ti, p[i] = pi; n     x[i] = {xi, yi}; n   } n   for (int i = (0); i < (int)(n); i++) { n     for (int j = (0); j < (int)(n); j++) { n       if (t[i] < t[j] && p2(t[i] - t[j]) >= dist2(x[j], x[i])) n         adj[i].push_back(j); n     } n   } n   vector<double> dp(n, 0); n   for (int i = (0); i < (int)(n); i++) dp[i] = p[i]; n   topological_sort(); n   for (int iii = (0); iii < (int)(n); iii++) { n     long long i = ans[iii]; n     for (auto j : adj[i]) dp[j] = max(dp[j], dp[i] + p[j]); n   } n   for (int i = (0); i < (int)(n); i++) 42; n   42; n   42; n   double out = dp[0]; n   for (int i = (0); i < (int)(n); i++) out = max(out, dp[i]); n   cout << fixed << setprecision(10) << out << endl; n   return 0; n } n


#include <bits/stdc++.h> n const double eps = 1e-7, PI = 3.1415926; n const int N = 2e5 + 10; n using namespace std; n int n, q, m, k, x, y, a[N], mx = -1, mn = 1e9, sum, t; n char c[N]; n string s, s1, s2; n map<pair<int, char>, int> mp; n vector<int> vec; n int main() { n   cin >> q; n   while (q--) { n     cin >> n; n     cin >> s1 >> s2; n     y = 0; n     mp.clear(); n     sum = 0; n     for (int i = 0; i < 20; i++) { n       char v =  a  + i; n       mn = 1e9; n       int hg = 20; n       while (hg--) { n         mn = 1e9; n         for (int j = 0; j < n; j++) { n           if (s1[j] == v && s2[j] != s1[j]) { n             mn = min(mn, s2[j] - s1[j]); n           } n         } n         for (int j = 0; j < n; j++) { n           if (s1[j] == v && s2[j] != s1[j]) { n             s1[j] += mn; n           } n         } n         if (mn < 0) y = 1; n         if (mn == 1e9) n           break; n         else n           sum++; n       } n     } n     if (!y) n       cout << sum << endl; n     else n       cout << -1 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1005; n const int INF = 0x3f3f3f3f; n int a[MAXN]; n int book[MAXN * 2]; n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= m; i++) { n     int tmp; n     scanf( %d , &tmp); n     a[tmp] = 1; n   } n   queue<int> Q; n   memset(book, 0x3f, sizeof(book)); n   for (int i = 0; i <= 1000; i++) { n     if (a[i]) { n       book[i - n + MAXN] = 1; n       Q.push(i - n + MAXN); n     } n   } n   while (!Q.empty()) { n     if (book[MAXN] != INF) { n       return 0 * printf( %d n , book[MAXN]); n     } n     int now = Q.front(); n     Q.pop(); n     for (int i = 0; i <= 1000; i++) { n       if (!a[i]) continue; n       int co = i - n; n       int nxt = now + co; n       if (nxt < 0 || nxt > 2005) continue; n       if (book[nxt] > book[now] + 1) { n         Q.push(nxt); n         book[nxt] = book[now] + 1; n       } n     } n   } n   printf( -1 n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct ring { n   int inside, outside, height; n } rings[200000]; n long long all, tot; n bool cmp(ring a, ring b) { n   if (a.outside != b.outside) return a.outside < b.outside; n   return a.inside < b.inside; n } n int main() { n   int n; n   while (cin >> n) { n     all = 0; n     tot = 0; n     for (int i = 0; i < n; i++) { n       scanf( %d%d%d , &rings[i].inside, &rings[i].outside, &rings[i].height); n     } n     sort(rings, rings + n, cmp); n     stack<ring> keys; n     for (int i = n - 1; i >= 0; i--) { n       while (1) { n         if (keys.empty()) break; n         ring key = keys.top(); n         if (key.outside >= rings[i].outside && key.inside < rings[i].outside) n           break; n         all -= key.height; n         keys.pop(); n       } n       keys.push(rings[i]); n       all += rings[i].height; n       tot = max(tot, all); n     } n     cout << tot << endl; n   } n } n


#include <bits/stdc++.h> n const int max_N = 1000005; n const int max_P = 100; n const long double eps = 1e-9; n int n, m, cnt; n long double R, C, T[max_N], l_pos[max_N], r_pos[max_N]; n long double coef[max_N][3]; n std::vector<std::pair<long double, int> > Vec; n int read() { n   char c = getchar(); n   int ans = 0; n   bool flag = true; n   while (!isdigit(c)) flag &= (c !=  - ), c = getchar(); n   while (isdigit(c)) ans = ans * 10 + c -  0 , c = getchar(); n   return flag ? ans : -ans; n } n void Write(int x) { n   if (x < 10) n     putchar(x +  0 ); n   else n     Write(x / 10), putchar(x % 10 +  0 ); n } n int min(int x, int y) { return x < y ? x : y; } n int max(int x, int y) { return x > y ? x : y; } n long double min(long double x, long double y) { return x < y ? x : y; } n long double max(long double x, long double y) { return x > y ? x : y; } n long double sqr(long double x) { return x * x; } n struct vec { n   long double x, y; n   vec(long double x0 = 0, long double y0 = 0) : x(x0), y(y0) {} n   vec operator+(const vec b) const { return vec(x + b.x, y + b.y); } n   vec operator-(const vec b) const { return vec(x - b.x, y - b.y); } n   vec operator*(const long double b) const { return vec(x * b, y * b); } n   vec operator/(const long double b) const { return vec(x / b, y / b); } n   long double norm() { return sqrtl(sqr(x) + sqr(y)); } n   long double norm2() { return sqr(x) + sqr(y); } n } P[max_N]; n long double mod(long double x, long double y) { n   x -= (long long)(x / y) * y; n   if (x < 0) x += y; n   return x; n } n vec walk(vec L, vec R, long double t) { n   return L + (R - L) * (t / (R - L).norm()); n } n std::pair<long double, long double> calc(long double Y1, long double Y2, n                                          long double X1, long double X2) { n   std::pair<long double, long double> res; n   res.first = (Y2 - Y1) / (X2 - X1); n   return res.second = Y1 - X1 * res.first, res; n } n std::pair<long double, long double> solve_equation(long double A, long double B, n                                                    long double C) { n   long double delta = sqr(B) - 4 * A * C; n   delta = sqrtl(delta); n   return std::make_pair((-B - delta) / (2 * A), (-B + delta) / (2 * A)); n } n long double calc(int id, long double key) { n   return coef[id][0] * sqr(key) + coef[id][1] * key + coef[id][2]; n } n void prepare() { n   int p1 = 1, p2 = 1; n   long double t = 0; n   while (p2 < 2 * n && T[p2] < R) p2++; n   while (p1 <= n && p2 < 2 * n) { n     long double nxt = min(T[p1], T[p2] - R); n     if (nxt > t + eps) { n       ++cnt, l_pos[cnt] = t, r_pos[cnt] = nxt; n       vec X1 = walk(P[p1], P[p1 + 1], t - T[p1 - 1]), n           X2 = walk(P[p1], P[p1 + 1], nxt - T[p1 - 1]); n       vec Y1 = walk(P[p2], P[p2 + 1], t + R - T[p2 - 1]), n           Y2 = walk(P[p2], P[p2 + 1], nxt + R - T[p2 - 1]); n       std::pair<long double, long double> X = n           calc(Y1.x - X1.x, Y2.x - X2.x, t, nxt); n       std::pair<long double, long double> Y = n           calc(Y1.y - X1.y, Y2.y - X2.y, t, nxt); n       coef[cnt][0] = sqr(X.first) + sqr(Y.first); n       coef[cnt][1] = 2 * (X.first * X.second + Y.first * Y.second); n       coef[cnt][2] = sqr(X.second) + sqr(Y.second); n     } n     t = nxt; n     while (p1 <= n && T[p1] < t + eps) p1++; n     while (p2 < 2 * n && T[p2] - R < t + eps) p2++; n   } n } n void Delete(long double l, long double r) { n   if (r - l < eps) return; n   if (r - l > R - eps) n     l = -eps, r = R + eps; n   else n     l = mod(l, R), r = mod(r, R); n   if (l < r) n     Vec.push_back(std::make_pair(l - eps, -1)), n         Vec.push_back(std::make_pair(r + eps, 1)); n   if (l > r) n     Vec.push_back(std::make_pair(-eps, -1)), n         Vec.push_back(std::make_pair(r + eps, 1)), n         Vec.push_back(std::make_pair(l - eps, -1)), n         Vec.push_back(std::make_pair(R + eps, 1)); n } n bool check(long double x) { n   Vec.clear(); n   Vec.reserve(4 * cnt); n   for (int i = 1; i <= cnt; coef[i][2] += x, i++) { n     coef[i][2] -= x; n     if (sqr(coef[i][1]) - 4 * coef[i][0] * coef[i][2] < 0) n       Delete(l_pos[i], r_pos[i]); n     else { n       std::pair<long double, long double> tmp = n           solve_equation(coef[i][0], coef[i][1], coef[i][2]); n       if (calc(i, l_pos[i]) > 0 && calc(i, r_pos[i]) > 0) { n         if (tmp.first > l_pos[i] && tmp.second < r_pos[i]) n           Delete(l_pos[i], tmp.first), Delete(tmp.second, r_pos[i]); n         else n           Delete(l_pos[i], r_pos[i]); n       } else if (calc(i, l_pos[i]) > 0 && calc(i, r_pos[i]) < 0) n         Delete(l_pos[i], tmp.first); n       else if (calc(i, l_pos[i]) < 0 && calc(i, r_pos[i]) > 0) n         Delete(tmp.second, r_pos[i]); n     } n   } n   int sum = 0; n   std::sort(Vec.begin(), Vec.end()); n   if (Vec.empty() || Vec[0].first > 0 || Vec.back().first < R) return true; n   for (int i = 0; i + 1 < Vec.size(); i++) n     if ((sum += Vec[i].second) >= 0) return true; n   return false; n } n int main() { n   n = read(), m = read(); n   for (int i = 1; i <= n; i++) n     scanf( %Lf%Lf , &P[i].x, &P[i].y), P[i + n] = P[i]; n   for (int i = 1; i < 2 * n; i++) T[i] = T[i - 1] + (P[i + 1] - P[i]).norm(); n   C = T[n], R = C / m; n   prepare(); n   long double ml = 0, mr = R; n   for (int _ = 1; _ <= max_P; _++) { n     long double mid = (ml + mr) / 2; n     if (check(sqr(mid))) n       mr = mid; n     else n       ml = mid; n   } n   if ((ml + mr) / 2 < 0.5) n     printf( 0 ); n   else n     printf( %.10Lf , (ml + mr) / 2); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e4 + 10; n int dp[maxn][40]; n long long pw[maxn][40]; n long long n; n int dfs(int a, int b) { n   if (dp[a][b]) return dp[a][b]; n   if (pw[a][b] >= n) return dp[a][b] = 1; n   if (pw[a][2] >= n) { n     if ((n - a) % 2 == 0) n       return dp[a][b] = 1; n     else n       return dp[a][b] = 2; n   } n   if (dfs(a + 1, b) == 2 || dfs(a, b + 1) == 2) return dp[a][b] = 1; n   return dp[a][b] = 2; n } n long long a, b; n int main() { n   scanf( %lld%lld%lld , &a, &b, &n); n   for (int i = 1; i < maxn; i++) { n     pw[i][0] = 1; n     for (int j = 1; j <= 35; j++) { n       pw[i][j] = pw[i][j - 1] * i; n       if (pw[i][j] >= n) { n         for (int k = j; k <= 35; k++) pw[i][k] = n; n         break; n       } n     } n   } n   dfs(a, b); n   if (a == 1) { n     for (int i = b; i <= 36; i++) { n       if (i > 35) { n         printf( Missing n ); n         break; n       } n       if (dp[2][i] == 2) { n         if ((i - b) % 2 == 0) n           printf( Masha n ); n         else n           printf( Stas n ); n         break; n       } n     } n   } else { n     if (dp[a][b] == 1) n       printf( Masha n ); n     else n       printf( Stas n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int mod(long long int x) { n   return (((x) % 1000000007) + 1000000007) % 1000000007; n } n long long int mod1(long long int x, long long int m) { n   return (((x) % m) + m) % m; n } n long long int solve() { n   string s; n   cin >> s; n   long long int d = count(s.begin(), s.end(),  D ); n   long long int l = count(s.begin(), s.end(),  L ); n   long long int u = count(s.begin(), s.end(),  U ); n   long long int r = count(s.begin(), s.end(),  R ); n   if (s.size() % 2 == 0) n     cout << (abs(d - u) + abs(l - r)) / 2; n   else n     cout <<  -1 ; n   return 0; n } n signed main() { n   ios::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int t; n   t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MAXn = 1e5 + 100; n const long long INF = 1e9 + 100; n long long l, r = -INF, n, k, a[MAXn], p, ans, dp[MAXn], anss; n signed main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cin >> n >> k; n   cin >> a[1]; n   for (long long i = 2; i < n + 1; ++i) { n     cin >> a[i]; n     r = max(r, abs(a[i] - a[i - 1])); n   } n   while (l <= r) { n     long long p = (l + r) / 2; n     long long ans = -1; n     for (long long i = 1; i <= n; ++i) { n       dp[i] = 1; n       for (long long j = 1; j <= i - 1; ++j) { n         if (abs(a[i] - a[j]) <= p * abs(i - j)) { n           dp[i] = max(dp[i], dp[j] + 1); n         } n       } n       ans = max(ans, dp[i]); n     } n     if (ans < n - k) n       l = p + 1; n     else n       r = p - 1, anss = p; n   } n   return cout << anss << endl, 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n const ll N = 1e6 + 233; n struct node { n   ll x, y; n   node(ll x = 0, ll y = 0) : x(x), y(y) {} n }; n ll dist(node a, node b) { n   ll L = a.x - b.x; n   ll R = a.y - b.y; n   return L * L + R * R; n } n vector<node> v; n vector<ll> ans; n ll cnt[3][3]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) { n     ll x, y; n     scanf( %lld%lld , &x, &y); n     v.emplace_back(x, y + N); n   } n   for (;;) { n     for (int i = 0; i < 2; ++i) n       for (int j = 0; j < 2; ++j) cnt[i][j] = 0; n     for (int i = 0; i < n; ++i) ++cnt[v[i].x & 1][v[i].y & 1]; n     if (cnt[0][0] + cnt[1][1] && cnt[1][0] + cnt[0][1]) { n       for (int i = 0; i < n; ++i) n         if ((v[i].x + v[i].y) & 1) ans.push_back(i + 1); n       break; n     } else if (cnt[0][0] + cnt[0][1] && cnt[1][0] + cnt[1][1]) { n       for (int i = 0; i < n; ++i) n         if (v[i].x & 1) ans.push_back(i + 1); n       break; n     } else { n       int x, y; n       for (int i = 0; i < 2; ++i) n         for (int j = 0; j < 2; ++j) n           if (cnt[i][j]) { n             x = i; n             y = j; n           } n       for (int i = 0; i < n; ++i) { n         v[i].x = (v[i].x - x) >> 1; n         v[i].y = (v[i].y - y) >> 1; n       } n     } n   } n   printf( %d n , ans.size()); n   for (auto i : ans) printf( %d  , i); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> v[200005]; n long long dp[200005][3]; n long long f[200005]; n long long g[200005]; n const int mod = 998244353; n void dfs(int p, int u) { n   for (auto x : v[u]) { n     if (x != p) { n       dfs(u, x); n     } n   } n   int l = v[u].size() - (p ? 1 : 0); n   if (l == 0) { n     dp[u][0] = 0; n     dp[u][1] = 1; n     dp[u][2] = 1; n     return; n   } n   int k = 0; n   while (k < v[u].size() && v[u][k] != p) k++; n   int cnt = 1; n   f[0] = 1; n   for (int i = 0; i < v[u].size(); i++) { n     int tmp = v[u][i]; n     if (tmp == p) continue; n     f[cnt] = f[cnt - 1] * (dp[tmp][0] + dp[tmp][1]) % mod; n     cnt++; n   } n   cnt = l; n   g[l + 1] = 1; n   for (int i = v[u].size() - 1; i >= 0; i--) { n     int tmp = v[u][i]; n     if (tmp == p) continue; n     g[cnt] = g[cnt + 1] * (dp[tmp][0] + dp[tmp][2]) % mod; n     cnt--; n   } n   for (int i = 1; i <= k; i++) { n     dp[u][0] = n         (dp[u][0] + f[i - 1] * g[i + 1] % mod * dp[v[u][i - 1]][2]) % mod; n   } n   dp[u][1] = f[k] * g[k + 1] % mod; n   for (int i = k + 1; i <= l; i++) { n     dp[u][2] = (dp[u][2] + f[i - 1] * g[i + 1] % mod * dp[v[u][i]][2]) % mod; n   } n   dp[u][2] = (dp[u][2] + f[l]) % mod; n } n void work() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i < n; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     v[x].push_back(y); n     v[y].push_back(x); n   } n   dfs(0, 1); n   printf( %lld n , (dp[1][0] + dp[1][1]) % mod); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int T; n   T = 1; n   while (T--) { n     work(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n inline long long read() { n   long long s = 0, w = 1; n   char ch = getchar(); n   for (; !isdigit(ch); ch = getchar()) n     if (ch ==  - ) w = -1; n   for (; isdigit(ch); ch = getchar()) s = (s << 1) + (s << 3) + (ch ^ 48); n   return s * w; n } n inline void print(long long x, int op = 10) { n   if (!x) { n     putchar( 0 ); n     if (op) putchar(op); n     return; n   } n   char F[40]; n   long long tmp = x > 0 ? x : -x; n   if (x < 0) putchar( - ); n   int cnt = 0; n   while (tmp > 0) { n     F[cnt++] = tmp % 10 +  0 ; n     tmp /= 10; n   } n   while (cnt > 0) putchar(F[--cnt]); n   if (op) putchar(op); n } n inline long long gcd(long long x, long long y) { return y ? gcd(y, x % y) : x; } n long long qpow(long long a, long long b) { n   long long ans = 1; n   while (b) { n     if (b & 1) ans *= a; n     b >>= 1; n     a *= a; n   } n   return ans; n } n long long qpow(long long a, long long b, long long mod) { n   long long ans = 1; n   while (b) { n     if (b & 1) (ans *= a) %= mod; n     b >>= 1; n     (a *= a) %= mod; n   } n   return ans % mod; n } n const int dir[][2] = {{0, 1}, {1, 0},  {0, -1}, {-1, 0}, n                       {1, 1}, {1, -1}, {-1, 1}, {-1, -1}}; n const int MOD = 1e9 + 7; n const int INF = 0x3f3f3f3f; n const int N = 1e6 + 7; n long long n, m; n struct Node { n   long long x, y; n   int id; n   bool operator<(const Node& opt) const { return x - y < opt.x - opt.y; } n } p[N]; n long long pre[N], suf[N]; n long long ans[N]; n int pos[N]; n void solve() { n   n = read(), m = read(); n   for (int i = 1; i <= n; ++i) { n     p[i].x = read(), p[i].y = read(); n     p[i].id = i; n   } n   sort(p + 1, p + 1 + n); n   for (int i = 1; i <= n; ++i) pos[p[i].id] = i; n   for (int i = 1; i <= n; ++i) pre[i] = pre[i - 1] + p[i].x; n   for (int i = n; i >= 1; --i) suf[i] = suf[i + 1] + p[i].y; n   for (int i = 1; i <= n; ++i) { n     int id = p[i].id; n     long long tmp = n         p[i].x * (n - i) + p[i].y * (i - 1) + pre[i - 1] + suf[i + 1]; n     ans[id] = tmp; n   } n   while (m--) { n     int u = read(), v = read(); n     long long tmp = min(p[pos[u]].x + p[pos[v]].y, p[pos[u]].y + p[pos[v]].x); n     ans[u] -= tmp; n     ans[v] -= tmp; n   } n   for (int i = 1; i <= n; ++i) printf( %lld%c , ans[i],    n [i == n]); n } n int main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[102], b[102]; n pair<int, int> dp[102][2 * 10002]; n bool vis[102][2 * 10002]; n pair<int, int> solve(int i, int diff) { n   if (i == n + 1) n     return diff >= 10000 ? pair<int, int>(0, 0) : pair<int, int>(1e9, 1e9); n   if (vis[i][diff]) return dp[i][diff]; n   vis[i][diff] = true; n   pair<int, int> take = solve(i + 1, diff - a[i]); n   take.second += a[i]; n   pair<int, int> leave = solve(i + 1, diff + b[i] - a[i]); n   ++leave.first; n   return dp[i][diff] = min(take, leave); n } n int main() { n   cin >> n; n   for (int i = (1); i <= (int)(n); ++i) scanf( %d , &a[i]); n   for (int i = (1); i <= (int)(n); ++i) scanf( %d , &b[i]); n   pair<int, int> ans = solve(1, 10000); n   cout << ans.first <<     << ans.second; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   long count[] = {0, 0, 0, 0}; n   long n; n   cin >> n; n   while (n--) { n     long x; n     cin >> x; n     count[x] += 1; n   } n   cout << min(min(count[1] + count[2], count[1] + count[3]), n               count[2] + count[3]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool isPrime(long long int n) { n   for (long long int i = 2; i <= sqrt(n); i++) n     if (n % i == 0) return 0; n   return 1; n } n long long knap_0_1(long long wt[], long long val[], long long n, long long w) { n   long long kp[n + 1][w + 1]; n   for (long long i = 0; i <= n; i++) { n     for (long long j = 0; j <= w; j++) { n       if (i == 0 || j == 0) n         kp[i][j] = 0; n       else if (wt[i - 1] <= j) n         kp[i][j] = max(val[i - 1] + kp[i - 1][j - wt[i - 1]], kp[i - 1][j]); n       else n         kp[i][j] = kp[i - 1][j]; n     } n   } n   return kp[n][w]; n } n long long compute_xor_1_n(long long n) { n   switch (n & 3) { n     case 0: n       return n; n     case 1: n       return 1; n     case 2: n       return n + 1; n     case 3: n       return 0; n   } n } n bool isPowerOfTwo(long long x) { return x && (!(x & (x - 1))); } n void bfs(vector<long long> g[], long long vis[], long long i, n          vector<long long> &v) { n   queue<long long> q; n   q.push(i); n   vis[i] = 1; n   while (!q.empty()) { n     long long x = q.front(); n     q.pop(); n     v.push_back(x); n     for (long long j = 0; j < g[x].size(); j++) { n       if (vis[g[x][j]] == 0) { n         vis[g[x][j]] = 1; n         q.push(g[x][j]); n       } n     } n   } n } n long long binary_Expo(long long x, long long n) { n   long long result = 1; n   while (n > 0) { n     if (n % 2 == 1) result = result * x; n     x = x * x; n     n = n / 2; n   } n   return result; n } n long long mod_Expo(long long x, long long n, long long M) { n   long long result = 1; n   while (n > 0) { n     if (n % 2 == 1) result = (result * x) % M; n     x = (x * x) % M; n     n = n / 2; n   } n   return result; n } n long long GCD(long long A, long long B) { n   if (B == 0) n     return A; n   else n     return GCD(B, A % B); n } n long long bs(long long a[], long long l, long long r, long long x, n              long long n) { n   while (r >= l) { n     long long m = l + (r - l) / 2; n   } n   return -1; n } n void SieveOfEratosthenes(int n) { n   bool prime[n + 1]; n   memset(prime, true, sizeof(prime)); n   for (int p = 2; p * p <= n; p++) { n     if (prime[p] == true) { n       for (int i = p * p; i <= n; i += p) prime[i] = false; n     } n   } n   for (int p = 2; p <= n; p++) n     if (prime[p]) cout << p <<    ; n   cout << endl; n } n long long s1[8] = {1, 1, 1, -1, -1, -1, 0, 0}; n long long f1[8] = {-1, 0, 1, -1, 0, 1, -1, 1}; n void dfs(long long a1, long long a2, long long b1, long long b2, long long c1, n          long long c2, long long &first, long long n, vector<long long> vis[]) { n   vis[b1][b2] = 1; n   if (a1 == b1 || a2 == b2 || (abs(a1 - b1) == abs(a2 - b2))) return; n   if (b1 == c1 && b2 == c2) { n     first = 1; n     return; n   } n   for (long long i = 0; i < 8; i++) { n     if (b1 + f1[i] <= 0 || b2 + s1[i] <= 0 || b1 + f1[i] > n || n         b2 + s1[i] > n || vis[b1 + f1[i]][b2 + s1[i]] == 1) n       continue; n     dfs(a1, a2, b1 + f1[i], b2 + s1[i], c1, c2, first, n, vis); n     if (first == 1) return; n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   long long n; n   cin >> n; n   long long a1, a2, b1, b2, c1, c2; n   cin >> a1 >> a2; n   cin >> b1 >> b2; n   cin >> c1 >> c2; n   long long first = 0; n   vector<long long> vis[n + 1]; n   for (long long i = 0; i < n + 1; i++) n     for (long long j = 0; j < n + 1; j++) vis[i].push_back(0); n   dfs(a1, a2, b1, b2, c1, c2, first, n, vis); n   if (first) n     cout <<  YES  << endl; n   else n     cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, t[200100], ll, rr, sum; n double inv, res, f[200100], g[200100]; n inline double calc(long long l, long long r) { n   while (ll > l) { n     --ll; n     inv += 1.0 / t[ll]; n     sum += t[ll]; n     res += inv * t[ll]; n   } n   while (rr < r) { n     ++rr; n     inv += 1.0 / t[rr]; n     sum += t[rr]; n     res += 1.0 * sum / t[rr]; n   } n   while (ll < l) { n     res -= inv * t[ll]; n     sum -= t[ll]; n     inv -= 1.0 / t[ll]; n     ll++; n   } n   while (rr > r) { n     res -= 1.0 * sum / t[rr]; n     sum -= t[rr]; n     inv -= 1.0 / t[rr]; n     rr--; n   } n   return res; n } n inline void func(long long l, long long r, long long L, long long R) { n   if (l > r || L > R) return; n   register long long mid = (l + r) >> 1; n   register long long mp; n   register double mn = 9e18; n   for (register long long i = L; i <= R; i++) n     if (f[i] + calc(i + 1, mid) < mn) mn = f[i] + calc(i + 1, mid), mp = i; n   g[mid] = mn; n   func(l, mid - 1, L, mp), func(mid + 1, r, mp, R); n } n inline void read(long long &x) { n   x = 0; n   register char c = getchar(); n   while (c >  9  || c <  0 ) c = getchar(); n   while (c >=  0  && c <=  9 ) n     x = (x << 3) + (x << 1) + (c ^ 48), c = getchar(); n } n signed main() { n   read(n), read(m); n   for (register long long i = 1; i <= n; i++) n     read(t[i]), inv += 1.0 / t[i], sum += t[i], res += 1.0 * sum / t[i], n         f[i] = res; n   ll = 1, rr = n; n   while (--m) func(1, n, 0, n - 1), memcpy(f, g, sizeof(g)); n   printf( %lf n , f[n]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, x, y, z; n pair<int, int> a[200005]; n map<int, int> m, o; n map<pair<int, int>, int> p; n long long q; n int main() { n   scanf( %d , &n); n   ; n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i].first); n     ; n     scanf( %d , &a[i].second); n     ; n     m[a[i].first]++; n     o[a[i].second]++; n     p[make_pair(a[i].first, a[i].second)]++; n   } n   for (int i = 1; i <= n; i++) { n     q += m[a[i].first] + o[a[i].second] - 1 - n          p[make_pair(a[i].first, a[i].second)]; n   } n   cout << q / 2 << endl; n   return 0; n } n


#include <bits/stdc++.h> n const long long max9 = 10 + 1e9, max6 = 10 + 1e6, max12 = 10 + 1e12, n                 max15 = 10 + 1e15; n const long long min6 = -1 * max6, min9 = -1 * max9, min12 = -1 * max12, n                 min15 = -1 * max15; n const long long R = 7 + 1e9, NN = 10 + 1e5; n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n, k; n   cin >> n >> k; n   bool a[n * k + 1]; n   memset(a, false, sizeof(a)); n   int t[k]; n   for (int i = (0); i < (k); ++i) cin >> t[i]; n   for (int i = (0); i < (k); ++i) { n     a[t[i] - 1] = true; n   } n   for (int i = (0); i < (k); ++i) { n     cout << t[i] <<    ; n     int o = 1; n     for (int j = (0); j < (n * k + 1); ++j) { n       if (!a[j]) { n         cout << j + 1 <<    ; n         o++; n         a[j] = true; n       } n       if (o == n) { n         cout << endl; n         break; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int OO = int(1e6); n bool dcmp(double a, double b) { return fabs(a - b) < 1e-8; } n int Dcmp(long double a, long double b) { n   return fabs(a - b) <= 1e-8 ? 0 : a < b ? -1 : 1; n } n int gcd(int a, int b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n void INPUT() { n   cout << fixed << setprecision(13); n   ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); n } n int main() { n   INPUT(); n   int n, m; n   cin >> n >> m; n   long long ans = 0; n   string answers[n]; n   int grades[m]; n   for (int i = 0; i < (int)(n); ++i) cin >> answers[i]; n   for (int i = 0; i < (int)(m); ++i) cin >> grades[i]; n   int Most[30]; n   for (int i = 0; i < m; i++) { n     memset(Most, 0, sizeof(Most)); n     for (int j = 0; j < n; ++j) { n       char let = answers[j][i]; n       Most[let - 65]++; n     } n     int mx = 0; n     for (int k = 0; k < 30; k++) { n       mx = max(mx, Most[k]); n     } n     ans += grades[i] * mx; n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long f(long long x) { return x * (x - 1) / 2; } n vector<int> g[303]; n int m1[303][303]; n int m2[303][303]; n int main() { n   int n, m, k; n   cin >> n >> m >> k; n   vector<int> a(k); n   vector<int> c(n + 1, 0); n   for (int i = 0; i < k; i++) { n     cin >> a[i]; n     c[a[i]] = 1; n   } n   vector<pair<int, int>> r; n   if (k == n) { n     cout << -1; n     return 0; n   } n   int V, X; n   for (int x = 1; x <= n; x++) { n     if (!c[x]) n       V = x; n     else n       X = x; n   } n   m--; n   r.push_back({X, V}); n   for (int x = 1; x <= n; x++) { n     if (x == V || x == X) continue; n     if (m > 0) { n       r.push_back({x, V}); n       m--; n     } n   } n   for (int x = 1; x <= n; x++) { n     if (x == V || x == X) continue; n     for (int y = x + 1; y <= n; y++) { n       if (y == V || y == X) continue; n       if (m > 0) { n         m--; n         r.push_back({x, y}); n       } n     } n   } n   for (int x = 1; x <= n; x++) { n     if (X == x || V == x || c[x]) continue; n     if (m > 0) { n       m--; n       r.push_back({X, x}); n       ; n     } n   } n   if (m) { n     cout << -1; n     return 0; n   } n   for (int i = 0; i < r.size(); i++) { n     cout << r[i].first <<     << r[i].second << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   vector<long> v; n   long n, i, j, k; n   cin >> n; n   for (i = 0; i < n; i++) { n     long long a; n     cin >> a; n     v.push_back(a); n   } n   if (v[0] != v[n - 1]) { n     cout << n - 1; n   } else { n     i = n - 1; n     while (v[i] == v[0]) { n       i--; n     } n     j = 1; n     while (v[j] == v[0]) { n       j++; n     } n     long t = max(i, n - 1 - i); n     long p = max(j, n - 1 - j); n     cout << max(t, p); n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> adj[100001]; n long long vis[100001]; n long long level[100001]; n long long nodes[100001]; n void bfs(long long s) { n   queue<long long> q; n   q.push(s); n   vis[s] = 1; n   while (!q.empty()) { n     long long f = q.front(); n     q.pop(); n     for (auto it = adj[f].begin(); it != adj[f].end(); ++it) { n       if (vis[*it] == 0) { n         q.push(*it); n         level[*it] = level[f] + 1; n         nodes[level[*it]]++; n         vis[*it] = 1; n       } n     } n   } n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long n; n   cin >> n; n   for (long long i = 2; i <= n; i++) { n     long long tmp; n     cin >> tmp; n     adj[tmp].push_back(i); n   } n   nodes[0] = 1; n   bfs(1); n   long long mx = 0, cnt = 0, ans = 0; n   for (long long i = 1; i <= n; i++) { n     mx = max(mx, level[i]); n   } n   for (long long i = 0; i <= mx; i++) { n     ans += (nodes[i] % 2); n     cnt = 0; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int32_t main() { n   long long t; n   cin >> t; n   while (t--) { n     long long a, b, c; n     cin >> a >> b >> c; n     if (a < c) n       cout << 1; n     else n       cout << -1; n     cout <<    ; n     if (c < a * b) n       cout << b; n     else n       cout <<  -1 ; n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int dp[2100][2100] = {0}; n int a[2100]; n bool vis[2100][2100]; n int main(int argc, const char* argv[]) { n   string s, p; n   cin >> s >> p; n   for (int i = 0; i < s.size(); ++i) { n     int tmp = 0; n     int l = 0; n     while (i + tmp < s.size() && l < p.size()) { n       if (s[i + tmp] == p[l]) { n         ++l; n       } n       ++tmp; n     } n     if (l == p.size()) { n       a[i] = tmp; n     } else n       a[i] = -1; n   } n   for (int i = 0; i < s.size(); ++i) { n     for (int j = 0; j <= i; ++j) { n       dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]); n       dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j]); n       if (a[i] != -1 && i + a[i] <= s.size() && n           j + a[i] - p.size() + 1 <= i + a[i] + 1) { n         dp[i + a[i]][j + a[i] - p.size()] = n             max(dp[i + a[i]][j + a[i] - p.size()], dp[i][j] + 1); n       } n     } n   } n   for (int i = 0; i <= s.size(); ++i) { n     cout << dp[s.size()][i] <<    ; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int md = 998244353, MX = 2020; n int n, it, cc, j, z, r, u[2][MX], c[2][MX], v[2][MX], st[MX], vst[MX], pw2[MX]; n char s[MX]; n bool ok; n void dfs(int i, int j, int cur, int val) { n   u[i][j] = it; n   c[i][j] = cur; n   v[i][j] = val; n   if (i == 0) { n     if (u[0][n - 1 - j] != it) n       dfs(0, n - 1 - j, cur, val); n     else if (v[0][n - 1 - j] != val) n       ok = false; n     if (s[j] ==  0 ) { n       if (j < it) { n         if (u[1][j] != it) n           dfs(1, j, cur, val); n         else if (v[1][j] != val) n           ok = false; n       } else if (st[cur] != it) { n         st[cur] = it; n         vst[cur] = val; n       } else if (vst[cur] != val) n         ok = false; n     } else if (s[j] ==  1 ) { n       if (j < it) { n         if (u[1][j] != it) n           dfs(1, j, cur, 1 - val); n         else if (v[1][j] != 1 - val) n           ok = false; n       } else if (st[cur] != it) { n         st[cur] = it; n         vst[cur] = val ^ 1; n       } else if (vst[cur] != (val ^ 1)) n         ok = false; n     } n   } else { n     if (u[1][it - 1 - j] != it) n       dfs(1, it - 1 - j, cur, val); n     else if (v[1][it - 1 - j] != val) n       ok = false; n     if (s[j] ==  0 ) { n       if (u[0][j] != it) n         dfs(0, j, cur, val); n       else if (v[0][j] != val) n         ok = false; n     } else if (s[j] ==  1 ) { n       if (u[0][j] != it) n         dfs(0, j, cur, 1 - val); n       else if (v[0][j] != 1 - val) n         ok = false; n     } n   } n } n int main() { n   scanf( %s , s); n   n = strlen(s); n   reverse(s, s + n); n   for (pw2[0] = j = 1; j < MX; j++) pw2[j] = (pw2[j - 1] * 2) % md; n   for (it = 1; it < n; it++) { n     ok = true; n     cc = 1; n     dfs(0, n - 1, cc, 1); n     if (st[cc] == it && vst[cc] != 0) ok = false; n     st[cc] = it; n     if (u[1][it - 1] == it) { n       if (v[0][n - 1] != v[1][it - 1]) ok = false; n     } else { n       ++cc; n       dfs(1, it - 1, cc, 1); n       if (st[cc] == it && vst[cc] != 0) ok = false; n       st[cc] = it; n     } n     for (j = 0; j < n - 1; j++) n       if (ok && u[0][j] != it) { n         ++cc; n         dfs(0, j, cc, 1); n       } n     for (j = 0; j < it - 1; j++) n       if (ok && u[1][j] != it) { n         ++cc; n         dfs(1, j, cc, 1); n       } n     if (ok) { n       for (z = 0, j = 1; j <= cc; j++) n         if (st[j] != it) z++; n       r = (r + pw2[z]) % md; n     } n   } n   printf( %d n , r); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int d(int n) { n   if (!n) return 1; n   int d = 0; n   while (n) { n     d++; n     n /= 10; n   } n   return d; n } n int n, k, m; n set<int> st; n set<int>::iterator it; n void prnt() { n   it = st.begin(); n   for (int i = 0; i < m; i++) { n     printf( %08d n , *it); n     it++; n   } n   exit(0); n } n void doit(int a, int b) { n   if (!b) { n     int diff = abs(abs(a) - k); n     if (diff >= 10000) return; n     if (d(diff * 10000 + n) <= 8) st.insert(diff * 10000 + n); n     if (d(n * 10000 + diff) <= 8) st.insert(n * 10000 + diff); n     if (st.size() >= m) prnt(); n     return; n   } n   doit(a + b % 10, b / 10); n   doit(a - b % 10, b / 10); n   doit(a * (b % 10), b / 10); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> k >> m; n   for (int i = 0; true; i++) { n     n = i; n     doit(i % 10, i / 10); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e6; n int N; n long long A[MAXN + 10], B[MAXN + 10], S; n vector<long long> prime; n int main() { n   long long i, j; n   scanf( %d , &N); n   for (i = 1; i <= N; i++) scanf( %d , &A[i]), S += A[i]; n   for (i = 2; i * i <= S; i++) { n     if (S % i == 0) { n       prime.push_back(i); n       while (S % i == 0) S /= i; n     } n   } n   if (S != 1) prime.push_back(S); n   long long ans = 1e18; n   for (auto now : prime) { n     long long aaa = 0; n     for (i = 1; i <= N; i++) B[i] = A[i]; n     vector<long long> V; n     long long S = 0; n     for (i = 1; i <= N; i++) B[i] %= now; n     for (i = 1; i <= N; i++) { n       if (S + B[i] >= now) n         V.push_back(now - S), S += now - S; n       else n         V.push_back(B[i]), S += B[i]; n       B[i] -= V.back(); n       if (S == now) { n         long long t = 0; n         for (j = 0; j < V.size(); j++) { n           t += V[j]; n           if (t >= (now + 1) / 2) break; n         } n         long long pos = j; n         for (j = 0; j < V.size(); j++) aaa += abs(pos - j) * V[j]; n         V.clear(); n         V.push_back(B[i]); n         S = B[i]; n       } n     } n     ans = min(ans, aaa); n   } n   if (ans == 1e18) ans = -1; n   printf( %lld , ans); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 300010; n struct Segment { n   int l, r; n   int id; n   bool operator<(const Segment &o) const { return l < o.l; } n } a[maxn]; n int main() { n   int n, k; n   cin >> n >> k; n   for (int i = 0; i < n; i++) { n     scanf( %d %d , &a[i].l, &a[i].r); n     a[i].id = i + 1; n   } n   sort(a, a + n); n   priority_queue<int, vector<int>, greater<int> > que; n   int ans = -1; n   int L, R; n   for (int i = 0; i < n; i++) { n     que.push(a[i].r); n     while (que.size() && que.top() == a[i].l - 1) { n       que.pop(); n     } n     while (que.size() > k) { n       que.pop(); n     } n     if (que.size() == k) { n       int tmp = que.top() - a[i].l + 1; n       if (tmp > ans) { n         ans = tmp; n         L = a[i].l; n         R = que.top(); n       } n     } n   } n   if (ans == -1) { n     cout << 0 << endl; n     for (int i = 1; i <= k; i++) { n       printf( %d  , i); n     } n   } else { n     cout << ans << endl; n     int cnt = 0; n     for (int i = 0; i < n; i++) { n       if (a[i].l <= L && a[i].r >= R) { n         printf( %d  , a[i].id); n         cnt++; n         if (cnt == k) break; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int mod = 1e9 + 7; n long long int inf = 1e9 + 7; n void solve() { n   long long int n, m; n   cin >> n >> m; n   vector<vector<long long int> > v(n, vector<long long int>(m)); n   long long int i, j; n   for (i = 0; i < n; i++) { n     for (j = 0; j < m; j++) { n       cin >> v[i][j]; n     } n   } n   vector<long long int> ans(n, -1); n   for (i = 0; i < n; i++) { n     ans[i] = i; n   } n   for (i = 0; i < m; i++) { n     long long int mx = 0; n     for (j = 1; j < n; j++) { n       if (v[j][i] >= v[j - 1][i]) { n         ans[j] = min(ans[j], mx); n       } else { n         mx = j; n         ans[j] = min(ans[j], mx); n       } n     } n   } n   long long int q; n   cin >> q; n   while (q--) { n     long long int l, r; n     cin >> l >> r; n     l--; n     r--; n     if (l >= ans[r]) { n       cout <<  Yes  << endl; n     } else { n       cout <<  No  << endl; n     } n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int t = 1; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 300005; n const int mod = 998244353; n int n; n long long a[N], b[N], c[N], k, ans = 2e15; n inline long long qpow(long long a, long long b, long long m) { n   long long ans = 1ll; n   for (; b; b >>= 1, a = a * a % m) n     if (b & 1) ans = ans * a % m; n   return ans; n } n inline int read() { n   int xx = 0, ff = 1; n   char ch = getchar(); n   while (ch >  9  || ch <  0 ) { n     if (ch ==  - ) ff = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     xx = xx * 10 + ch -  0 ; n     ch = getchar(); n   } n   return xx * ff; n } n inline int gcd(int a, int b) { n   while (b ^= a ^= b ^= a %= b) n     ; n   return a; n } n inline int lcm(int a, int b) { return a / gcd(a, b) * b; } n inline void pre() { n   for (int i = 1; i <= n; i++) b[i] = b[i - 1] + a[i]; n } n inline void suf() { n   for (int i = n; i >= 1; i--) c[i] = c[i + 1] + a[i]; n } n inline long long minLL(long long x, long long y) { return x < y ? x : y; } n int main() { n   cin >> n >> k; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   sort(a + 1, a + 1 + n); n   pre(); n   suf(); n   for (int l = 1, r; l <= n; l = r + 1) { n     for (r = l; a[r + 1] == a[l]; r++) n       ; n     if (r - l + 1 >= k) return cout << 0, 0; n     long long left = 1ll * (a[l] - 1) * (l - 1) - b[l - 1], n               right = c[r + 1] - 1ll * (a[r] + 1) * (n - r), n               mid = k - r + l - 1; n     if (r >= k && left + mid < ans) ans = left + mid; n     if (n - l + 1 >= k) ans = min(ans, right + mid); n     ans = min(ans, left + right + mid); n   } n   return cout << ans << endl, 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   int a, b; n   node(int a = 0, int b = 1e9) : a(a), b(b) {} n   int get(int x) { return a * x + b; } n }; n struct Eins { n   vector<node> st; n   int n; n   Eins(int n) : n(n) { st.resize(n << 2 | 1); } n   void modify(int id, int l, int r, node v) { n     int mid = l + r >> 1; n     if (st[id].get(mid) > v.get(mid)) { n       swap(st[id], v); n     } n     if (l == r) return; n     if (st[id].get(l) > v.get(l)) modify(id << 1, l, mid, v); n     if (st[id].get(r) > v.get(r)) modify(id << 1 | 1, mid + 1, r, v); n   } n   int get(int id, int l, int r, int x) { n     int ans = st[id].get(x); n     if (l == r) return ans; n     int mid = l + r >> 1; n     if (x <= mid) return min(ans, get(id << 1, l, mid, x)); n     return min(ans, get(id << 1 | 1, mid + 1, r, x)); n   } n }; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n, m; n   cin >> n >> m; n   vector<string> s(n + 1); n   vector<Eins> Tree(m + 1, Eins(n + 1)); n   auto sqr = [&](int x) { return x * x; }; n   for (int i = 0; i <= n; i++) { n     cin >> s[i]; n     int last = -1; n     for (int j = 0; j <= m; j++) { n       if (s[i][j] ==  1 ) { n         if (last == -1) { n           for (int p = 0; p <= j; p++) { n             Tree[p].modify(1, 0, n, node(-2 * i, sqr(i) + sqr(p - j))); n           } n         } else { n           for (int p = last + 1; p <= j; p++) { n             Tree[p].modify( n                 1, 0, n, node(-2 * i, sqr(i) + min(sqr(p - j), sqr(last - p)))); n           } n         } n         last = j; n       } else if (j == m && last != -1) { n         for (int p = last + 1; p <= m; p++) { n           Tree[p].modify(1, 0, n, node(-2 * i, sqr(i) + sqr(p - last))); n         } n       } n     } n   } n   long long ans = 0; n   for (int i = 0; i <= n; i++) { n     for (int j = 0; j <= m; j++) { n       ans += Tree[j].get(1, 0, n, i) + sqr(i); n     } n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100010, maxs = 200010; n const int inf = 2000000000; n int n, m, ecnt; n pair<int, int> edges[maxs]; n bool dat[maxn]; n int c[maxn]; n int pos[maxn], last[maxn], nowt; n vector<pair<int, int> > ga[maxn]; n vector<int> weight[maxn]; n void insertunit(int* c, int pos, int x) { n   for (; pos <= n; pos += pos & (-pos)) c[pos] += x; n } n int querysum(int* c, int pos) { n   int ans = 0; n   for (; pos; pos -= pos & (-pos)) ans += c[pos]; n   return ans; n } n int lcaf[maxn][20], depth[maxn]; n void dfs(int start = 1) { n   pos[start] = ++nowt; n   for (int i = 1; i < 20; i++) lcaf[start][i] = lcaf[lcaf[start][i - 1]][i - 1]; n   for (vector<pair<int, int> >::iterator it = ga[start].begin(); n        it != ga[start].end(); it++) { n     if (it->first != lcaf[start][0]) { n       lcaf[it->first][0] = start; n       depth[it->first] = depth[start] + 1; n       dfs(it->first); n     } n   } n   last[start] = nowt; n } n int lca(int x, int y) { n   if (depth[x] > depth[y]) swap(x, y); n   for (int i = 19; i >= 0; i--) n     if (lcaf[y][i] && depth[lcaf[y][i]] >= depth[x]) y = lcaf[y][i]; n   if (x == y) return x; n   for (int i = 19; i >= 0; i--) n     if (lcaf[x][i] != lcaf[y][i]) x = lcaf[x][i], y = lcaf[y][i]; n   return lcaf[x][0]; n } n void insertpath(int x, int y) { n   int u = lca(x, y); n   insertunit(c, pos[x], 1); n   insertunit(c, pos[y], 1); n   insertunit(c, pos[u], -1); n   if (depth[u]) insertunit(c, pos[lcaf[u][0]], -1); n } n int querynode(int x) { return querysum(c, last[x]) - querysum(c, pos[x] - 1); } n pair<int, int> f[maxs]; n bool vis[maxs]; n pair<int, int> getf(int en) { n   if (ga[edges[en].first].size() == 1) n     if (dat[edges[en].first]) n       return make_pair(0, edges[en].first); n     else n       return make_pair(-inf, -1); n   if (vis[en]) return f[en]; n   vis[en] = true; n   int st = edges[en].first; n   int ed = edges[en].second; n   int nowmin = -inf, cnt = 0, nowlca = -1; n   if (dat[st]) { n     nowmin = 0; n     cnt = 1; n     nowlca = st; n   } n   int p = 0; n   for (vector<pair<int, int> >::iterator it = ga[st].begin(); n        it != ga[st].end(); it++, p++) n     if (it->first != ed) { n       pair<int, int> t = getf(it->second); n       if (t.second == -1) continue; n       t.first += weight[st][p]; n       if (t.first > nowmin) { n         nowmin = t.first; n         nowlca = t.second; n         cnt = 1; n       } else if (t.first == nowmin) n         cnt++; n     } n   if (cnt > 1) n     f[en] = make_pair(nowmin, st); n   else if (cnt == 1) n     f[en] = make_pair(nowmin, nowlca); n   else n     f[en] = make_pair(-inf, -1); n   return f[en]; n } n void processpath() { n   for (int i = 1; i <= n; i++) { n     if (dat[i]) { n       int nowmin = -inf, cnt = 0, nowlca = -1, p = 0; n       for (vector<pair<int, int> >::iterator it = ga[i].begin(); n            it != ga[i].end(); it++, p++) { n         pair<int, int> t = getf(it->second); n         if (t.second == -1) continue; n         t.first += weight[i][p]; n         if (t.first > nowmin) { n           nowmin = t.first; n           nowlca = t.second; n           cnt = 1; n         } else if (t.first == nowmin) { n           cnt++; n         } n       } n       if (cnt == 1) insertpath(i, nowlca); n     } n   } n } n pair<int, int> getans() { n   int nowmax = 0, cnt = 0; n   for (int i = 1; i <= n; i++) { n     if (dat[i]) continue; n     int t = querynode(i); n     if (t > nowmax) { n       nowmax = t; n       cnt = 1; n     } else if (t == nowmax) n       cnt++; n   } n   return make_pair(nowmax, cnt); n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < m; i++) { n     int x; n     scanf( %d , &x); n     dat[x] = true; n   } n   for (int i = 1; i < n; i++) { n     int x, y, w; n     scanf( %d%d%d , &x, &y, &w); n     edges[ecnt++] = make_pair(x, y); n     edges[ecnt++] = make_pair(y, x); n     ga[x].push_back(make_pair(y, ecnt - 1)); n     ga[y].push_back(make_pair(x, ecnt - 2)); n     weight[x].push_back(w); n     weight[y].push_back(w); n   } n   dfs(); n   processpath(); n   pair<int, int> t = getans(); n   printf( %d %d n , t.first, t.second); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n, i; n   int a[100000]; n   double sum = 0; n   int sum1 = 0, sum2 = 0; n   scanf( %d , &n); n   for (i = 0; i < n; i++) { n     scanf( %d , &a[i]); n     sum += a[i]; n   } n   sum = sum / n; n   for (i = 0; i < n; i++) { n     if (sum - a[i] > 0) n       sum1 += fabs(sum - a[i]); n     else n       sum2 += fabs(sum - a[i]); n   } n   if (sum2 > sum1) sum1 = sum2; n   printf( %d n , sum1); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 3007; n const int w = 31; n int n, m, q, d[maxn][maxn >> 4], o[maxn][maxn >> 4], sm[maxn]; n set<pair<int, int> > s; n set<pair<int, int> > valid; n vector<int> val[maxn]; n int gd(int n) { n   int ans = 0; n   while (n) { n     ans += n & 1; n     n >>= 1; n   } n   return ans; n } n int get_ans(int l, int r) { n   int ans = ((1 << r - l + 1) - 1) << l; n   return ans; n } n int get_sum(int u) { n   int ans = 0; n   for (int i = 0; i <= m / w; ++i) { n     ans += o[u][i]; n   } n   return ans; n } n bool ck(int i, int j) { n   for (int k = 0; k <= m / w; ++k) { n     if ((d[i][k] & d[j][k]) != d[i][k]) return 1; n   } n   return 0; n } n int get_dig(int i, int j) { n   for (int k = 0; k <= m / w; ++k) { n     if ((d[i][k] & d[j][k]) != d[i][k]) { n       int t1 = d[i][k], t2 = d[j][k]; n       for (int u = 0; u < w; ++u) { n         if ((t1 & 1) && !(t2 & 1)) { n           return u + k * w + 1; n         } n         t1 >>= 1, t2 >>= 1; n       } n     } n   } n } n int main() { n   scanf( %d%d%d , &n, &m, &q); n   for (int i = 1; i <= n; ++i) { n     s.insert({0, i}); n   } n   while (q--) { n     int i, l, r; n     scanf( %d%d%d , &i, &l, &r); n     l--, r--; n     int lb = l / w, rb = r / w; n     if (lb < rb) { n       d[i][lb] = d[i][lb] ^ get_ans(l % w, w - 1); n       o[i][lb] = gd(d[i][lb]); n       d[i][rb] = d[i][rb] ^ get_ans(0, r % w); n       o[i][rb] = gd(d[i][rb]); n       for (int j = lb + 1; j < rb; ++j) { n         d[i][j] = d[i][j] ^ get_ans(0, w - 1); n         o[i][j] = w - o[i][j]; n       } n     } else { n       d[i][lb] = d[i][lb] ^ get_ans(l % w, r % w); n       o[i][lb] = gd(d[i][lb]); n     } n     auto p = s.find({sm[i], i}); n     int u = prev(p)->second, v = next(p)->second; n     if (u && v && ck(u, v)) { n       val[u].push_back(v); n       val[v].push_back(u); n       valid.insert({u, v}); n       valid.insert({v, u}); n     } n     s.erase(p); n     for (int j = 0; j < val[i].size(); ++j) { n       valid.erase({i, val[i][j]}); n       valid.erase({val[i][j], i}); n     } n     val[i].clear(); n     sm[i] = get_sum(i); n     s.insert({sm[i], i}); n     p = s.find({sm[i], i}); n     u = prev(p)->second, v = next(p)->second; n     if (u && ck(u, i)) { n       val[i].push_back(u); n       val[u].push_back(i); n       valid.insert({i, u}); n       valid.insert({u, i}); n     } n     if (v && ck(i, v)) { n       val[i].push_back(v); n       val[v].push_back(i); n       valid.insert({i, v}); n       valid.insert({v, i}); n     } n     if (valid.empty()) { n       cout << -1 << endl; n     } else { n       auto p = valid.begin(); n       int u = next(p)->first, v = next(p)->second; n       int au = get_dig(u, v), av = get_dig(v, u); n       if (u > v) swap(u, v); n       if (au > av) swap(au, av); n       printf( %d %d %d %d n , u, au, v, av); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long dx[8] = {-1, 1, 0, 0, 1, -1, 1, -1}; n long long dy[8] = {0, 0, -1, 1, 1, -1, -1, 1}; n void solve() { n   long long n, m; n   cin >> n >> m; n   char a[n][m]; n   for (long long i = 0; i < n; i++) { n     for (long long j = 0; j < m; j++) cin >> a[i][j]; n   } n   bool f = 1; n   for (long long i = 0; i < n; i++) { n     for (long long j = 0; j < m; j++) { n       if (a[i][j] ==  . ) { n         for (long long k = 0; k < 8; k++) { n           long long x = i + dx[k]; n           long long y = j + dy[k]; n           if (x < 0 || x >= n || y < 0 || y >= m) continue; n           if (a[x][y] ==  * ) { n             cout <<  NO ; n             return; n           } n         } n       } else if (a[i][j] >=  1  && a[i][j] <=  8 ) { n         long long cnt = 0; n         for (long long k = 0; k < 8; k++) { n           long long x = i + dx[k]; n           long long y = j + dy[k]; n           if (x < 0 || x >= n || y < 0 || y >= m) continue; n           if (a[x][y] ==  * ) cnt++; n         } n         if (cnt != a[i][j] -  0 ) { n           cout <<  NO ; n           return; n         } n       } n     } n   } n   cout <<  YES ; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long t = 1; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n map<int, int> moebius(int n) { n   map<int, int> res; n   vector<int> primes; n   for (int i = 2; i * i <= n; i++) { n     if (n % i == 0) { n       primes.push_back(i); n       while (n % i == 0) n /= i; n     } n   } n   if (n != 1) primes.push_back(n); n   int m = primes.size(); n   for (int i = 0; i < (1 << m); i++) { n     int mu = 1; n     int d = 1; n     for (int j = 0; j < m; j++) { n       if (i >> j & 1) { n         mu *= -1; n         d *= primes[j]; n       } n     } n     res[d] = mu; n   } n   return res; n } n int digCnt(long long n) { n   int cnt = 0; n   while (n) { n     cnt++; n     n /= 2; n   } n   return cnt; n } n int isPrime(int a) { n   if (a <= 1) return false; n   for (int i = 2; i * i <= a; i++) n     if (a % i == 0) return false; n   return true; n } n const int mu[] = {0, 1,  -1, -1, 0, -1, 1,  -1, 0, 0,  1,  -1, 0, -1, 1,  1, n                   0, -1, 0,  -1, 0, 1,  1,  -1, 0, 0,  1,  0,  0, -1, -1, -1, n                   0, 1,  1,  1,  0, -1, 1,  1,  0, -1, -1, -1, 0, 0,  1,  -1, n                   0, 0,  0,  1,  0, -1, 0,  1,  0, 1,  1,  -1, 0, -1, 1,  0, n                   0, 1,  -1, -1, 0, 1,  -1, -1, 0, -1, 1,  0,  0, 1}; n long long getNum(long long n) { n   long long res = 0; n   if (n <= 0) return res; n   int digs = digCnt(n); n   vector<int> v; n   long long tmp = n; n   while (tmp) { n     v.push_back(tmp % 2); n     tmp /= 2; n   } n   reverse(v.begin(), v.end()); n   for (int i = 2; i < digs; i++) { n     map<int, int> m = moebius(i); n     for (int k = 1; k < i; k++) n       if (i % k == 0) { n         res -= (1LL << (k - 1)) * (long long)m[i / k]; n       } n   } n   map<int, int> m = moebius(digs); n   for (int k = 1; k < digs; k++) { n     if (digs % k == 0) { n       long long cand = 0; n       for (int i = 0; i < k; i++) { n         if (i != 0 && v[i]) { n           cand += (1LL << (k - i - 1)); n         } n       } n       long long num = 0; n       for (int i = 0; i < k; i++) { n         if (v[i]) { n           for (int j = i; j < digs; j += k) num |= (1LL << (digs - 1 - j)); n         } n       } n       if (num <= n) cand++; n       res -= m[digs / k] * cand; n     } n   } n   return res; n } n int main() { n   long long l, r; n   while (cin >> l >> r) { n     cout << getNum(r) - getNum(l - 1) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, a, b; n   scanf( %d%d%d , &n, &a, &b); n   printf( %d n , ((a - 1) + b + 100 * n) % n + 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string grid[60]; n int main() { n   cin.tie(0); n   ios::sync_with_stdio(0); n   int t; n   cin >> t; n   int r, c; n   while (t--) { n     cin >> r >> c; n     for (int i = 0; i < r; i++) cin >> grid[i]; n     bool quina = false, borda = false, meio = false, all = true, tdb1 = true, n          tdb2 = true, tdb3 = true, tdb4 = true, tdm1 = false, tdm2 = false; n     for (int i = 0; i < r; i++) { n       bool at = true; n       for (int j = 0; j < c; j++) { n         if (grid[i][j] ==  A ) { n           if ((i == 0 && j == 0) || (i == 0 && j == c - 1) || n               (i == r - 1 && j == 0) || (i == r - 1 && j == c - 1)) n             quina = true; n           else if (i == 0 || j == c - 1 || i == r - 1 || j == 0) n             borda = true; n           else n             meio = true; n         } else { n           all = false; n           at = false; n         } n       } n       tdm1 = tdm1 || at; n     } n     for (int i = 0; i < c; i++) { n       bool at = true; n       for (int j = 0; j < r; j++) { n         if (grid[j][i] ==  P ) { n           at = false; n         } n       } n       tdm2 = tdm2 || at; n     } n     for (int i = 0; i < r; i++) { n       if (grid[i][0] ==  P ) tdb1 = false; n     } n     for (int i = 0; i < r; i++) { n       if (grid[i][c - 1] ==  P ) tdb2 = false; n     } n     for (int i = 0; i < c; i++) { n       if (grid[0][i] ==  P ) tdb3 = false; n     } n     for (int i = 0; i < c; i++) { n       if (grid[r - 1][i] ==  P ) tdb4 = false; n     } n     if (all) n       cout << 0 << endl; n     else if (tdb1 || tdb2 || tdb3 || tdb4) n       cout << 1 << endl; n     else if (quina || tdm1 || tdm2) n       cout << 2 << endl; n     else if (borda) n       cout << 3 << endl; n     else if (meio) n       cout << 4 << endl; n     else n       cout <<  MORTAL  << endl; n   } n } n


#include <bits/stdc++.h> n using std::cin; n using std::cout; n using std::endl; n using std::string; n using std::vector; n void print(std::list<int> &a) { n   for (std::list<int>::iterator it = a.begin(); it != a.end(); ++it) { n     cout << *it <<    ; n   } n   cout << endl; n } n void print(vector<int> &a) { n   for (int i = 0; i < a.size(); ++i) { n     cout << a[i] <<    ; n   } n   cout << endl; n } n int main() { n   int tr; n   cin >> tr; n   while (tr--) { n     int n; n     cin >> n; n     std::list<int> deli; n     deli.push_back(n); n     for (int k = 2; k * k <= n; ++k) { n       if (n % k == 0) { n         deli.push_back(k); n         if (n / k != k) { n           deli.push_back(n / k); n         } n       } n     } n     vector<int> pr; n     vector<int> p; n     vector<int> alpha(100); n     int z = n; n     for (int i = 2; i * i <= n; ++i) { n       while (z % i == 0) { n         pr.push_back(i); n         z /= i; n       } n     } n     if (z > 1) { n       pr.push_back(z); n     } n     p.push_back(pr[0]); n     for (int i = 0; i < 100; ++i) { n       alpha[i] = 0; n     } n     alpha[0] = 1; n     int cur_ind = 0; n     for (int i = 1; i < pr.size(); ++i) { n       if (pr[i] == pr[i - 1]) { n         ++alpha[cur_ind]; n       } else { n         p.push_back(pr[i]); n         ++cur_ind; n         ++alpha[cur_ind]; n       } n     } n     if (p.size() == 1) { n       for (std::list<int>::iterator it = deli.begin(); it != deli.end(); it++) { n         cout << *it <<    ; n       } n       cout << endl; n       cout << 0 << endl; n     } else { n       if (p.size() == 2) { n         if (alpha[0] * alpha[1] == 1) { n           cout << p[0] <<     << p[1] <<     << p[0] * p[1] << endl; n           cout << 1 << endl; n         } else { n           cout << p[0] <<    ; n           int s = p[0]; n           for (int j = 1; j < alpha[0]; ++j) { n             s *= p[0]; n             cout << s <<    ; n           } n           cout << p[1] * p[0] <<    ; n           cout << p[1] <<    ; n           s = p[1]; n           for (int j = 1; j < alpha[1]; ++j) { n             s *= p[1]; n             cout << s <<    ; n           } n           int proiz = p[0] * p[1]; n           for (std::list<int>::iterator it = deli.begin(); it != deli.end(); n                it++) { n             if ((*it % proiz == 0) && (*it != proiz)) { n               cout << *it <<    ; n             } n           } n           cout << endl; n           cout << 0 << endl; n         } n       } else { n         int last = p.size() - 1; n         int lastproiz = p[0] * p[last]; n         for (int i = 0; i + 1 < p.size(); ++i) { n           cout << lastproiz <<     << p[i] <<    ; n           int proiz = p[i] * p[i + 1]; n           for (std::list<int>::iterator it = deli.begin(); it != deli.end();) { n             if (((*it % p[i] == 0) && (*it != proiz)) && n                 ((*it != lastproiz) && (*it != p[i]))) { n               cout << *it <<    ; n               it = deli.erase(it); n             } else { n               ++it; n             } n           } n           lastproiz = proiz; n         } n         cout << lastproiz <<    ; n         cout << p[last] <<    ; n         int proiz = p[last] * p[0]; n         for (std::list<int>::iterator it = deli.begin(); it != deli.end(); n              it++) { n           if (((*it % p[last] == 0) && (*it != proiz)) && n               ((*it != lastproiz) && (*it != p[last]))) { n             cout << *it <<    ; n           } n         } n         cout << endl << 0 << endl; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int s, x1, x2, t1, t2, p, d; n   cin >> s >> x1 >> x2 >> t1 >> t2 >> p >> d; n   int cnt = 0, initial_position = p; n   while (initial_position != x1) { n     if (initial_position == s) d = -1; n     if (initial_position == 0) d = 1; n     initial_position += d; n     cnt++; n   } n   while (initial_position != x2) { n     if (initial_position == s) d = -1; n     if (initial_position == 0) d = 1; n     initial_position += d; n     cnt++; n   } n   int ans = min(abs(x1 - x2) * t2, cnt * t1); n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool even(long long int x) { return (x % 2 == 0); } n bool odd(long long int x) { return (x % 2 != 0); } n vector<int> sieve(int n) { n   bool prime[n + 1]; n   memset(prime, true, sizeof(prime)); n   for (int p = 2; p * p <= n; p++) { n     if (prime[p] == true) { n       for (int i = p * p; i <= n; i += p) prime[i] = false; n     } n   } n   vector<int> v; n   for (int p = 2; p <= n; p++) n     if (prime[p]) v.push_back(p); n   return v; n } n long long gcd(long long int a, long long int b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n bool isprime(long long n) { n   if (n == 1) return false; n   if (n == 2 || n == 3) return true; n   if (n % 2 == 0 || n % 3 == 0) return false; n   for (long long i = 5; i * i <= n; i = i + 6) { n     if (n % i == 0 || n % (i + 2) == 0) return false; n   } n   return true; n } n long long lcm(int a, int b) { return (a / gcd(a, b)) * b; } n bool compare(pair<int, int> p1, pair<int, int> p2) { n   if (p1.first == p2.first) n     return p1.second <= p2.second; n   else n     return p1.first > p2.first; n } n vector<int> presum(vector<long long int> v) { n   vector<int> res(v.size()); n   res[0] = v[0]; n   for (int i = 1; i < v.size(); i++) { n     res[i] = res[i - 1] + v[i]; n   } n   return res; n } n void precision(int a) { cout << setprecision(a) << fixed; } n int main() { n   int t; n   cin >> t; n   while (t--) { n     int f = 0; n     int n; n     cin >> n; n     string s; n     cin >> s; n     int l = 0, r = 0; n     for (int i = 0; i < n; i++) { n       int a = 0, b = 0; n       for (int j = i; j < n; j++) { n         if (s[j] ==  a ) a++; n         if (s[j] ==  b ) b++; n         if (a == b && (a != 0 && b != 0)) { n           l = i; n           r = j; n           f = 1; n           break; n         } n       } n       if (f == 1) break; n     } n     if (f == 1) n       cout << l + 1 <<     << r + 1 << endl; n     else n       cout <<  -1 -1  << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int mod = 1e9 + 7; n int main() { n   int r, g, h, t; n   cin >> r >> g; n   t = r + g; n   h = sqrt(t * 2); n   if ((h * (h + 1)) / 2 > t) { n     h--; n   } n   int dp[2][r + 1]; n   for (int j = 0; j <= r; j++) { n     dp[0][j] = 0; n     dp[1][j] = 0; n   } n   dp[0][0] = 1; n   int x = 0; n   for (int i = 1; i <= h; i++) { n     x += i; n     for (int j = 0; j <= r; j++) { n       if (x - j <= g) { n         dp[1][j] += dp[0][j]; n       } n       if (j - i >= 0) { n         dp[1][j] += dp[0][j - i]; n       } n       dp[1][j] = dp[1][j] % mod; n     } n     for (int j = 0; j <= r; j++) { n       dp[0][j] = dp[1][j]; n       dp[1][j] = 0; n     } n   } n   int out = 0; n   for (int i = 0; i <= r; i++) { n     out = (out + dp[0][i]) % mod; n   } n   cout << out << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<pair<int, int>, int> mmp; n int n, m, L[1000010], R[1000010], V[1000010], Head[1000010], Next[1000010], n     Go[1000010], Val[1000010], Cnt = 0, tmp[1000010], Dep[1000010], fa[200010], n                                Fa[200010][20], Len[200010][20]; n int Ans[1000010]; n int qwq = 1e9; n bool used[1000010]; n int getf(int first) { n   return fa[first] == first ? first : fa[first] = getf(fa[first]); n } n bool cmp(int first, int second) { return V[first] < V[second]; } n struct hp { n   int Rt[5000010], L[5000010], R[5000010], Val[5000010], Dis[5000010], Cnt; n   hp() { Cnt = 0; } n   int newnode(int first) { n     Val[++Cnt] = first, L[Cnt] = R[Cnt] = 0; n     return Cnt; n   } n   int merge(int first, int second) { n     if (!first || !second) return first + second; n     if (Val[first] > Val[second]) swap(first, second); n     R[first] = merge(R[first], second); n     if (Dis[L[first]] < Dis[R[first]]) swap(L[first], R[first]); n     Dis[first] = Dis[R[first]] + 1; n     return first; n   } n   void delrt(int first) { Rt[first] = merge(L[Rt[first]], R[Rt[first]]); } n   void add(int first, int v) { n     Rt[first * 2] = merge(Rt[first * 2], newnode(v)); n     while (Rt[first * 2] && Rt[first * 2 + 1] && n            Val[Rt[first * 2]] == Val[Rt[first * 2 + 1]]) n       delrt(first * 2), delrt(first * 2 + 1); n   } n   void del(int first, int v) { n     Rt[first * 2 + 1] = merge(Rt[first * 2 + 1], newnode(v)); n     while (Rt[first * 2] && Rt[first * 2 + 1] && n            Val[Rt[first * 2]] == Val[Rt[first * 2 + 1]]) n       delrt(first * 2), delrt(first * 2 + 1); n   } n   int ask(int first) { return Val[Rt[first * 2]]; } n   void mm(int first, int second) { n     Rt[first * 2] = merge(Rt[first * 2], Rt[second * 2]); n     Rt[first * 2 + 1] = merge(Rt[first * 2 + 1], Rt[second * 2 + 1]); n     while (Rt[first * 2] && Rt[first * 2 + 1] && n            Val[Rt[first * 2]] == Val[Rt[first * 2 + 1]]) n       delrt(first * 2), delrt(first * 2 + 1); n   } n } pvp; n void addedge(int first, int second, int z) { n   Go[++Cnt] = second; n   Next[Cnt] = Head[first]; n   Head[first] = Cnt; n   Val[Cnt] = z; n } n void DFS(int first, int f, int fv, int d) { n   Fa[first][0] = f, Len[first][0] = fv, Dep[first] = d; n   for (int T = Head[first]; T; T = Next[T]) n     if (Go[T] != f) DFS(Go[T], first, Val[T], d + 1); n } n int Solve(int first, int second) { n   if (Dep[first] < Dep[second]) swap(first, second); n   int mx = 0, det = Dep[first] - Dep[second]; n   for (int i = 19; i >= 0; i--) n     if (det >> i & 1) mx = max(mx, Len[first][i]), first = Fa[first][i]; n   if (first == second) return mx; n   for (int i = 19; i >= 0; i--) n     if (Fa[first][i] != Fa[second][i]) n       mx = max(mx, max(Len[first][i], Len[second][i])), first = Fa[first][i], n       second = Fa[second][i]; n   return max(mx, max(Len[first][0], Len[second][0])); n } n int getlca(int first, int second) { n   if (Dep[first] < Dep[second]) swap(first, second); n   int mx = 0, det = Dep[first] - Dep[second]; n   for (int i = 19; i >= 0; i--) n     if (det >> i & 1) mx = max(mx, Len[first][i]), first = Fa[first][i]; n   if (first == second) return first; n   for (int i = 19; i >= 0; i--) n     if (Fa[first][i] != Fa[second][i]) n       mx = max(mx, max(Len[first][i], Len[second][i])), first = Fa[first][i], n       second = Fa[second][i]; n   return Fa[first][0]; n } n void Do(int first, int f) { n   for (int T = Head[first]; T; T = Next[T]) n     if (Go[T] != f) { n       Do(Go[T], first); n       pvp.mm(first, Go[T]); n     } n   if (f && pvp.Rt[first * 2]) n     Ans[mmp[pair<int, int>(first, f)]] = pvp.ask(first); n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= m; i++) { n     scanf( %d%d%d , &L[i], &R[i], &V[i]); n     tmp[i] = i, mmp[pair<int, int>(L[i], R[i])] = i, n     mmp[pair<int, int>(R[i], L[i])] = i; n   } n   for (int i = 1; i <= n; i++) fa[i] = i; n   sort(tmp + 1, tmp + m + 1, cmp); n   for (int i = 1; i <= m; i++) { n     int a = L[tmp[i]], b = R[tmp[i]], c = V[tmp[i]]; n     if (getf(a) != getf(b)) n       addedge(a, b, c), addedge(b, a, c), fa[getf(a)] = getf(b), n                                           used[tmp[i]] = 1; n   } n   DFS(1, 0, 1e9, 1); n   for (int i = 0; i < 19; i++) n     for (int j = 1; j <= n; j++) n       Fa[j][i + 1] = Fa[Fa[j][i]][i], n                 Len[j][i + 1] = max(Len[j][i], Len[Fa[j][i]][i]); n   for (int i = 1; i <= m; i++) n     if (!used[i]) { n       int a = L[i], b = R[i], c = V[i]; n       int g = getlca(a, b); n       pvp.add(a, c), pvp.add(b, c), pvp.del(g, c), pvp.del(g, c); n     } n   for (int i = 1; i <= n; i++) Ans[i] = 1e9; n   Do(1, 0); n   for (int i = 1; i <= m; i++) n     if (!used[i]) n       printf( %d n , Solve(L[i], R[i])); n     else n       printf( %d n , Ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long read() { n   long long f = 1, x = 0; n   char s = getchar(); n   while (s <  0  || s >  9 ) { n     if (s ==  - ) f = -f; n     s = getchar(); n   } n   while (s >=  0  && s <=  9 ) { n     x = x * 10 + (s -  0 ); n     s = getchar(); n   } n   return f * x; n } n int n, m, i, j, s, o, k; n int U[200005], V[200005]; n vector<int> g[200005]; n int fa[200005], ds[200005], siz[200005]; n int findd(int x) { n   if (x == fa[x]) return 0; n   return findd(fa[x]) ^ ds[x]; n } n int findf(int x) { return x == fa[x] ? x : findf(fa[x]); } n vector<int> st; n bool unionSet(int a, int b) { n   int u = findf(a), v = findf(b); n   if (siz[u] > siz[v]) swap(u, v), swap(a, b); n   int d1 = findd(a), d2 = findd(b); n   if (u == v) { n     return d1 ^ d2; n   } n   if (d1 == d2) n     ds[u] = 1; n   else n     ds[u] = 0; n   siz[v] += siz[u]; n   fa[u] = v; n   st.push_back(u); n   return 1; n } n void POP() { n   int u = st.back(); n   st.pop_back(); n   int v = fa[u]; n   ds[u] = 0; n   siz[v] -= siz[u]; n   fa[u] = u; n   return; n } n int maxl[200005]; n int li[200005], ri[200005]; n void solve(int l, int r, int l2, int r2) { n   if (l > r || l2 > r2) return; n   if (l2 == r2) { n     for (int i = l; i <= r; i++) maxl[i] = l2; n     return; n   } n   int mid = (l + r) >> 1, le = (int)st.size(), rr = max(l2, mid - 1); n   int flag = 0; n   for (int i = l; i <= mid; i++) n     if (i) flag |= 1 ^ unionSet(U[i], V[i]); n   int le2 = (int)st.size(); n   if (flag) rr = r2; n   for (int i = min(r2, m); i > mid && i >= l2 && i > rr; i--) { n     flag |= 1 ^ unionSet(U[i], V[i]); n     if (flag) { n       rr = i; n       break; n     } n   } n   maxl[mid] = rr; n   while ((int)st.size() > le2) POP(); n   solve(mid + 1, r, rr, r2); n   while ((int)st.size() > le) POP(); n   for (int i = min(r2, m); i >= rr; i--) unionSet(U[i], V[i]); n   solve(l, mid - 1, l2, rr); n   while ((int)st.size() > le) POP(); n   return; n } n int main() { n   n = read(); n   m = read(); n   int Q = read(); n   for (int i = 1; i <= m; i++) { n     s = read(); n     o = read(); n     g[s].push_back(o); n     g[o].push_back(s); n     U[i] = s; n     V[i] = o; n   } n   int rr = 0; n   for (int i = 1; i <= Q; i++) n     li[i] = read(), ri[i] = read(), rr = max(li[i], rr); n   for (int i = 1; i <= n; i++) fa[i] = i, ds[i] = 0, siz[i] = 1; n   solve(0, m, 0, m + 1); n   for (int i = 1; i <= Q; i++) { n     s = li[i]; n     o = ri[i]; n     if (maxl[s - 1] > o) n       printf( YES n ); n     else n       printf( NO n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MaxN = 123456, Mod = 1000000000; n long long a[MaxN], f[MaxN]; n int main(void) { n   int i, j, n, m; n   f[0] = f[1] = 1; n   for (i = 2; i < MaxN; i++) f[i] = (f[i - 1] + f[i - 2]) % Mod; n   while (scanf( %d %d , &n, &m) != EOF) { n     int i, t, l, r; n     for (i = 0; i < n; i++) scanf( %I64d , &a[i]); n     for (j = 0; j < m; j++) { n       scanf( %d %d %d , &t, &l, &r); n       if (t == 1) n         a[l - 1] = r; n       else { n         long long s = 0; n         for (i = 0; i <= r - l; i++) s = (s + a[i + l - 1] * f[i]) % Mod; n         printf( %d n , (int)s); n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, x, y; n long int v[1001]; n long int ans; n int main() { n   cin >> n >> m; n   for (int i = 1; i <= n; i++) { n     scanf( %ld , &v[i]); n   } n   for (int i = 0; i < m; i++) { n     scanf( %d%d , &x, &y); n     if (v[x] > v[y]) n       ans += v[y]; n     else n       ans += v[x]; n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n; n   cin >> n; n   cout << n / 2520 << endl; n } n


// O(q*log(q)) with pretty big constant factor n #include<bits/stdc++.h> n using namespace std; n  n struct query { n  vector<int> nodes; n }; n // for a given path n // active will find intersections not at it s lca  n // cnts with find intersections at it s lca n  n const int maxn = 3e5 + 1; n int n, m; n vector<int> adj[maxn]; n // for changing # active n vector<pair<int,int>> updates[maxn]; n // intersections of 2 paths with common lca = this node + for active n vector<query> queries[maxn]; n // counts of paths with lca at current point n int tot_cnt = 0; n map<long long, int> cnts; n // intersections of 2 paths one at it s lca and the other not at it s lca n int active[maxn]; n long long ans = 0; n  n // for lca n int t = 0; n int ancestor[maxn][20], tin[maxn], tout[maxn]; n  n void dfs_lca(int c, int p) { n  tin[c] = t++; n  n  ancestor[c][0] = p; n  for (int i = 1; i < 20; i++) { n   ancestor[c][i] = ancestor[ancestor[c][i - 1]][i - 1]; n  } n  n  // messes with bin search n  if (!(c==1 and p == 1)) { n   adj[c].erase(find(adj[c].begin(), adj[c].end(), p)); n  } n  for (auto n : adj[c]) { n   dfs_lca(n, c); n  } n  n  tout[c] = t++; n } n  n // p is ancestor of a n bool is_ancestor(int a, int p) { n  // lol <= is very important or else you say the lca of a and a is parent of a n  return tin[p] <= tin[a] and tout[a] <= tout[p]; n } n  n int find_lca(int a, int b) { n  if (is_ancestor(b, a)) { n   return a; n  } n  else if (is_ancestor(a, b)) { n   return b; n  } n  else { n   for (int i = 19; i >= 0; i--) { n    if (!is_ancestor(b, ancestor[a][i])) { n     a = ancestor[a][i]; n    } n   } n  n   return ancestor[a][0]; n  } n } n  n // find which subtree rooted at a child of c this node is in n int find_sub(int node, int c) { n  int l = 0, r = adj[c].size() - 1; n  while (l != r) { n   int m = (l + r + 1) / 2; n  n   if (tin[adj[c][m]] <= tin[node]) { n    l = m; n   } n   else { n    r = m - 1; n   } n  } n  n  return adj[c][l]; n } n  n // for computing the intersections of line path with any other path n void dfs(int c, int p) { n  for (auto n : adj[c]) { n   if (n != p) { n    dfs(n, c);  n    active[c] += active[n]; n   } n  } n  n  for (auto upd : updates[c]) { n   if (upd.first == -1) { n    active[c]++; n    updates[upd.second].push_back({ c, upd.second }); n   } n   else { n    active[c]--; n    active[find_sub(upd.first, c)]--; n   } n  } n  n  // loop through queries and update cnts as you iterate so that every intersection is counted only one time n  for (auto& q : queries[c]) { n   // find which subtrees rooted at children of c each node in the query is in n   // use this for PIE and complementary counting later n   vector<long long> subtrees; n   for (auto& node : q.nodes) { n    if (node != c) { n     subtrees.push_back(find_sub(node,c)); n    } n   } n  n   // update ans with compl counting n   ans += tot_cnt + active[c]; n   if (subtrees.size() == 1) { n    ans += - cnts[subtrees[0]]; n    ans += - active[subtrees[0]]; n   } n   else if (subtrees.size() == 2) { n    // make unique n    if (subtrees[0] > subtrees[1]) { n     swap(subtrees[0], subtrees[1]); n    } n    ans += - cnts[subtrees[0]] - cnts[subtrees[1]] + cnts[subtrees[0] * maxn + subtrees[1]]; n    ans += - active[subtrees[0]] - active[subtrees[1]]; n   } n  n   // update cnts n   for (auto subtree : subtrees) { n    cnts[subtree]++; n   } n   if (subtrees.size() == 2) { n    cnts[subtrees[0] * maxn + subtrees[1]]++; n   } n   tot_cnt++; n  } n  n  tot_cnt = 0; n  cnts.clear(); n } n  n  n int main() { n  ios_base::sync_with_stdio(0); cin.tie(0); n  n  cin >> n; n  n  for (int i = 0; i < n-1; i++) { n   int a, b; cin >> a >> b; n   adj[a].push_back(b); n   adj[b].push_back(a); n  } n  n  dfs_lca(1, 1); n  n  cin >> m; n  n  for (int i = 0; i < m; i++) { n   int a, b, lca; cin >> a >> b; n   lca = find_lca(a, b); n  n   queries[lca].push_back({ {a,b} }); n   if (a != lca) { n    updates[a].push_back({ -1, lca }); n   } n   if (b != lca) { n    updates[b].push_back({ -1, lca }); n   } n  } n  n  dfs(1, 1); n  n  cout << ans; n }


#include <bits/stdc++.h> n using namespace std; n string itos(int x) { n   stringstream ss; n   ss << x; n   return ss.str(); n } n int n, k, a[400010], nxt[400010], pt[400010], res; n struct Comp { n   bool operator()(int l, int r) const { return pt[l] > pt[r]; } n }; n set<int, Comp> s1; n int main() { n   ios_base::sync_with_stdio(false); n   cin >> n >> k; n   for (int i = 0; i < n; i++) cin >> a[i]; n   iota(pt, pt + 400010, n); n   for (int i = n - 1; i >= 0; i--) { n     nxt[i] = pt[a[i]]; n     pt[a[i]] = i; n   } n   for (int i = 0; i < n; i++) { n     if (s1.count(a[i])) { n       s1.erase(a[i]); n       pt[a[i]] = nxt[i]; n       s1.insert(a[i]); n     } else n       pt[a[i]] = nxt[i]; n     if (s1.count(a[i])) continue; n     if (s1.size() == k) s1.erase(s1.begin()); n     s1.insert(a[i]); n     res++; n   } n   cout << res << endl; 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(0); n   int n; n   cin >> n; n   vector<long long> p(n); n   vector<int> a(n), b(n); n   for (int i = 0; i < n; ++i) cin >> p[i]; n   for (int i = 0; i < n; ++i) cin >> a[i]; n   for (int i = 0; i < n; ++i) cin >> b[i]; n   vector<pair<long long, int>> one, two, three; n   for (int i = 0; i < n; ++i) { n     if (a[i] == 1 || b[i] == 1) one.push_back({p[i], i}); n     if (a[i] == 2 || b[i] == 2) two.push_back({p[i], i}); n     if (a[i] == 3 || b[i] == 3) three.push_back({p[i], i}); n   } n   sort(one.begin(), one.end()); n   sort(two.begin(), two.end()); n   sort(three.begin(), three.end()); n   vector<bool> vis(n, false); n   int p1 = 0, p2 = 0, p3 = 0; n   int si1 = one.size(), si2 = two.size(), si3 = three.size(); n   int m, c; n   cin >> m; n   for (int i = 0; i < m; ++i) { n     cin >> c; n     if (c == 1) { n       bool fl = false; n       for (int start = p1; start < si1; start++) { n         if (vis[one[start].second] == false) { n           cout << one[start].first <<    ; n           vis[one[start].second] = true; n           fl = true; n           p1 = start + 1; n           break; n         } n       } n       if (!fl) cout <<  -1  ; n     } else if (c == 2) { n       bool fl = false; n       for (int start = p2; start < si2; start++) { n         if (vis[two[start].second] == false) { n           cout << two[start].first <<    ; n           vis[two[start].second] = true; n           fl = true; n           p2 = start + 1; n           break; n         } n       } n       if (!fl) cout <<  -1  ; n     } else { n       bool fl = false; n       for (int start = p3; start < si3; start++) { n         if (vis[three[start].second] == false) { n           cout << three[start].first <<    ; n           vis[three[start].second] = true; n           fl = true; n           p3 = start + 1; n           break; n         } n       } n       if (!fl) cout <<  -1  ; n     } n   } n } n