#include <bits/stdc++.h> n using namespace std; n const long long MX = 3009; n long long x[MX], par[MX], num[MX], ans = 0; n pair<long long, pair<long long, long long> > edg[MX]; n void MS(long long v) { n   par[v] = -1; n   num[v] = x[v]; n } n long long FS(long long v) { n   if (par[v] < 0) return v; n   return par[v] = FS(par[v]); n } n void US(long long a, long long b) { n   a = FS(a), b = FS(b); n   if (a == b) return; n   if (par[a] > par[b]) swap(a, b); n   par[a] += par[b]; n   par[b] = a; n   num[a] += num[b]; n } n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   long long n; n   cin >> n; n   for (long long i = 0; i < n - 1; i++) { n     long long u, v, w; n     cin >> u >> v >> w; n     edg[i] = {w, {u, v}}; n   } n   for (long long i = 1; i <= n; i++) cin >> x[i]; n   for (long long i = 1; i <= n; i++) MS(i); n   sort(edg, edg + n - 1); n   for (long long i = 0; i < n - 1; i++) { n     long long u = edg[i].second.first, v = edg[i].second.second, n               w = edg[i].first; n     long long maxx = 0, sum = 0, res = 1; n     for (long long j = 1; j <= n; j++) { n       if (par[j] < 0) { n         maxx = max(maxx, -par[j]); n         sum += num[j]; n       } n     } n     for (long long j = 1; j <= n; j++) { n       if (par[j] < 0) { n         if (sum - num[j] < -par[j]) res = 0; n       } n     } n     if (res) ans = w; n     US(u, v); n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const long long LINF = 0x3f3f3f3f3f3f3f3fll; n unordered_map<int, int> dp; n int msb(int x) { return __builtin_clz(1) - __builtin_clz(x); } n int solve(int mask) { n   if (mask <= 1) return 0; n   if (dp.count(mask)) return dp[mask]; n   int maxi = msb(mask); n   vector<int> mex; n   for (int i = 1; i <= maxi; i++) { n     int at = mask; n     for (int j = i; j <= maxi; j++) n       if (mask & (1 << j)) { n         at ^= (1 << j); n         if (i != j) at |= (1 << (j - i)); n       } n     mex.push_back(solve(at)); n   } n   sort(mex.begin(), mex.end()); n   vector<int>::iterator it = unique(mex.begin(), mex.end()); n   mex.resize(distance(mex.begin(), it)); n   int g = 0; n   int i = 0; n   for (auto k : mex) { n     if (g == k) n       g++; n     else n       break; n   } n   return dp[mask] = g; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n; n   cin >> n; n   map<int, vector<int>> mp; n   for (int i = 0; i < n; i++) { n     int a; n     cin >> a; n     for (int i = 2; i * i <= a; i++) n       if (a % i == 0) { n         int at = 0; n         while (a % i == 0) a /= i, at++; n         mp[i].push_back(at); n       } n     if (a != 1) mp[a].push_back(1); n   } n   int ans = 0; n   for (auto x : mp) { n     sort(x.second.begin(), x.second.end()); n     vector<int>::iterator it = unique(x.second.begin(), x.second.end()); n     x.second.resize(distance(x.second.begin(), it)); n     int at = 0; n     for (int k : x.second) { n       at += 1 << k; n     } n     ans ^= solve(at); n   } n   if (ans == 0) n     cout <<  Arpa  << endl; n   else n     cout <<  Mojtaba  << endl; n   exit(0); n } n


#include <bits/stdc++.h> n using namespace std; n long long int arr[200001]; n int cnt[200001]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long int n; n   bool flag; n   int T; n   cin >> T; n   while (T--) { n     cin >> n; n     for (int i = 0; i < n; i++) { n       cin >> arr[i]; n     } n     for (int i = 0; i < n; i++) { n       cnt[i] = 0; n     } n     for (int i = 0; i < n; i++) { n       arr[i] = (arr[i] + i + n * 2147483647) % n; n       cnt[arr[i]]++; n     } n     flag = 1; n     for (int i = 0; i < n; i++) { n       if (cnt[i] != 1) { n         flag = 0; n       } n     } n     if (flag == 1) { n       cout <<  YES  << endl; n     } else { n       cout <<  NO  << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int pos, neg, tmp, x, y, len; n int main() { n   scanf( %d%d , &n, &m); n   pos = 0; n   neg = 0; n   for (int i = 0; i < n; ++i) { n     scanf( %d , &tmp); n     if (tmp > 0) n       ++pos; n     else n       ++neg; n   } n   while (m--) { n     scanf( %d%d , &x, &y); n     len = y - x + 1; n     if (len & 1) n       printf( 0 n ); n     else { n       if (len / 2 <= min(pos, neg)) n         printf( 1 n ); n       else n         printf( 0 n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int mypow(long long int a, long long int b) { n   long long int res = 1; n   while (b) { n     if (b & 1) res = res * a; n     b >>= 1; n     if (b) { n       a = a * a; n     } n   } n   return res; n } n long long int modpow(long long int a, long long int b, n                      long long int m = 1000000007) { n   a %= m; n   long long int res = 1; n   while (b) { n     if (b & 1) res = res * a % m; n     b >>= 1; n     if (b) { n       a = a * a % m; n     } n   } n   return res; n } n long long int modinv(long long int a) { n   return modpow(a, 1000000007 - 2, 1000000007); n } n vector<long long int> power(11); n void computepower() { n   vector<int> p = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31}; n   for (int(i) = (0); (i) < (p.size()); ++(i)) { n     power[i] = modpow(2, p[i]) - 1; n   } n } n vector<long long int> powerof2(60); n void computepower2() { n   powerof2[0] = 1; n   for (int(i) = (1); (i) < (60); ++(i)) { n     powerof2[i] = powerof2[i - 1] * 2; n   } n } n vector<long long int> modinvr(100005); n void computemodinvr() { n   modinvr[0] = modinvr[1] = 1; n   for (int i = 2; i < 100005; i++) n     modinvr[i] = n         modinvr[1000000007 % i] * (1000000007 - 1000000007 / i) % 1000000007; n } n vector<long long int> modinvrfact(100005); n vector<long long int> factr(100005); n void computeodinvrfact() { n   factr[0] = 1; n   modinvrfact[0] = 1; n   for (int(i) = (1); (i) < (100005); ++(i)) { n     factr[i] = (factr[i - 1] * i) % 1000000007; n     modinvrfact[i] = modinv(factr[i]) % 1000000007; n   } n } n bool mycompr(pair<int, int> i1, pair<int, int> i2) { n   return i1.second > i2.second; n } n void unique_elem(vector<long long int> &v) { n   sort(v.begin(), v.end()); n   auto ip = unique(v.begin(), v.end()); n   v.resize(distance(v.begin(), ip)); n } n void unique_elem_with_count(vector<long long int> &a, n                             vector<long long int> &unique, n                             vector<long long int> &cnt_unique, n                             vector<long long int> &sum_unique, int n) { n   sort(a.begin(), a.end()); n   long long int j = 0; n   long long int sum = 0; n   while (j < n) { n     long long int cnt = 1; n     if (j != n - 1) { n       while (a[j + 1] == a[j]) { n         cnt++; n         j++; n         if (j == n - 1) { n           break; n         } n       } n     } n     sum += cnt; n     unique.push_back(a[j]); n     cnt_unique.push_back(cnt); n     sum_unique.push_back(sum); n     j++; n   } n } n void bfs(int root, int par, vector<bool> &vis, vector<long long int> &dist, n          vector<vector<int> > &adj) { n   queue<int> q; n   q.push(root); n   dist[root] = 0; n   vis[root] = true; n   while (!q.empty()) { n     int p = q.front(); n     q.pop(); n     for (int i = 0; i < adj[p].size(); i++) { n       if (vis[adj[p][i]] == false) { n         dist[adj[p][i]] = dist[p] + 1; n         q.push(adj[p][i]); n         vis[adj[p][i]] = true; n       } n     } n   } n } n void bfs_tree(int p, int par, vector<vector<long long int> > &adj, n               vector<long long int> &ways, vector<long long int> &dist) { n   for (int i = 0; i < adj[p].size(); i++) { n     if (adj[p][i] == par) { n       continue; n     } n     bfs_tree(adj[p][i], p, adj, ways, dist); n   } n } n void graph(vector<vector<long long int> > &adj, long long int m) { n   long long int x, y; n   for (int(i) = (0); (i) < (m); ++(i)) { n     cin >> x >> y; n     adj[x].push_back(y); n     adj[y].push_back(x); n   } n } n void decToBinary(int n, vector<int> &binaryNum) { n   binaryNum.resize(32, 0); n   int i = 0; n   while (n > 0) { n     binaryNum[i] = n % 2; n     n = n / 2; n     i++; n   } n } n long long int Binarytodec(string &st) { n   computepower2(); n   long long int len = st.size(); n   long long int num = 0; n   for (int(i) = (0); (i) < (st.size()); ++(i)) { n     if (st[i] ==  1 ) { n       num += powerof2[len - i - 1]; n     } n   } n   return num; n } n vector<int> spf(1000005); n void sieve() { n   spf[1] = 1; n   for (int i = 2; i < 1000005; i++) spf[i] = i; n   for (int i = 4; i < 1000005; i += 2) spf[i] = 2; n   for (int i = 3; i * i < 1000005; i++) { n     if (spf[i] == i) { n       for (int j = i * i; j < 1000005; j += i) { n         if (spf[j] == j) { n           spf[j] = i; n         } n       } n     } n   } n } n vector<int> getFactorization(int x) { n   vector<int> ret; n   map<int, int> fact; n   while (x != 1) { n     ret.push_back(spf[x]); n     if (fact.find(spf[x]) == fact.end()) { n       fact[spf[x]] = 1; n     } else { n       fact[spf[x]]++; n     } n     x = x / spf[x]; n   } n   for (auto it = fact.begin(); it != fact.end(); it++) { n     if (it->second & 1) { n       ret.push_back(it->first); n     } n   } n   return ret; n } n bool containsOnlyDigit(string str) { n   int l = str.length(); n   for (int i = 0; i < l; i++) { n     if (str.at(i) <  0  || str.at(i) >  9 ) return false; n   } n   return true; n } n void solve() { n   long long int n, m, l, q, k; n   cin >> n; n   vector<pair<long long int, long long int> > a(n); n   vector<long long int> f1(n); n   vector<long long int> f2(n), f3(n), f4(n); n   for (int(i) = (0); (i) < (n); ++(i)) { n     cin >> a[i].first >> a[i].second >> f1[i] >> f2[i] >> f3[i] >> f4[i]; n   } n   long long int x1 = -1e5; n   long long int y1 = -1e5; n   long long int x2 = 1e5; n   long long int y2 = 1e5; n   for (int(i) = (0); (i) < (n); ++(i)) { n     if (f1[i] == 0) { n       if (x2 < a[i].first) { n         cout << 0 <<   n ; n         return; n       } else { n         x1 = max(x1, a[i].first); n       } n     } n     if (f2[i] == 0) { n       if (y1 > a[i].second) { n         cout << 0 <<   n ; n         return; n       } else { n         y2 = min(y2, a[i].second); n       } n     } n     if (f3[i] == 0) { n       if (x1 > a[i].first) { n         cout << 0 <<   n ; n         return; n       } else { n         x2 = min(x2, a[i].first); n       } n     } n     if (f4[i] == 0) { n       if (y2 < a[i].second) { n         cout << 0 <<   n ; n         return; n       } else { n         y1 = max(y1, a[i].second); n       } n     } n   } n   cout << 1 <<     << x1 <<     << y1 <<   n ; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t = 1; n   cin >> t; n   int tt = 1; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n unordered_map<int, int> Task; n unordered_map<int, int> Idle; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int N, K; n   cin >> N >> K; n   for (int i = 1; i <= N; i++) { n     int val; n     cin >> val; n     Task[val]++; n     Idle[i] = val; n   } n   priority_queue<pair<int, int>, vector<pair<int, int> >, n                  greater<pair<int, int> > > n       Cost; n   for (int i = 1; i <= N; i++) { n     int val; n     cin >> val; n     if (Task[Idle[i]] == 1) continue; n     Cost.push(make_pair(val, i)); n   } n   long long int Ans = 0; n   for (int i = 1; i <= K; i++) { n     if (Task[i] == 0) { n       while (!Cost.empty()) { n         pair<int, int> u = Cost.top(); n         Cost.pop(); n         if (Task[Idle[u.second]] > 1) { n           Task[Idle[u.second]]--; n           Ans += u.first; n           Task[i]++; n           break; n         } n       } n     } n   } n   cout << Ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, x, y, u, v, a[1010], b[1010][1010]; n vector<int> vv; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < n; ++i) { n     scanf( %d%d , &x, &y); n     a[y]++; n     b[x][y]++; n   } n   for (int i = 0; i < m; ++i) { n     scanf( %d%d , &x, &y); n     if (a[y] && b[x][y]) { n       ++u; n       --a[y]; n       ++v; n       --b[x][y]; n     } else n       vv.push_back(y); n   } n   for (int i = 0; i < (int)vv.size(); ++i) n     if (a[vv[i]]) ++u, --a[vv[i]]; n   printf( %d %d , u, v); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double pi = 3.14159265358979323846; n void rw() {} n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   string s, r; n   long long n, c = 0; n   cin >> n >> s; n   for (int i = 0; i < n - 1; i++) { n     if (s[i] == s[i + 1]) n       c++, s[i] =  0 ; n     else n       i++; n   } n   cout << ((n - c) % 2 ? c + 1 : c); n   for (int i = 0; i < n; i++) n     if (s[i] !=  0 ) r += s[i]; n   if ((n - c) % 2) r.erase(r.size() - 1); n   cout <<   n  << r; n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /stack:200000000 ) n #pragma GCC optimize(2) n #pragma G++ optimize(2) n using namespace std; n constexpr long long N = 2e5 + 9; n constexpr long long M = 1e9 + 7; n constexpr long long INF = 0x3f3f3f3f3f3f3f3f; n constexpr long long INF2 = 0xcfcfcfcfcfcfcfcf; n long long n; n long long a[N], b[N]; n string ans; n long long p, r; n void print() { n   for (long long i = 1; i <= n; i++) cout << b[i] <<    ; n   cout << endl; n } n signed fail() { n   cout <<  IMPOSSIBLE  << endl; n   return 0; n } n bool rcheck() { n   for (long long i = 1; i <= n; i++) { n     if (a[i] != b[n + 1 - i]) return false; n   } n   r++; n   ans +=  R ; n   return true; n } n bool check() { n   if (rcheck()) return true; n   for (long long i = 1; i <= n; i++) { n     if (a[i] != b[i]) return false; n   } n   return true; n } n signed answer() { n   if (p <= 2e5) { n     cout <<  SMALL  << endl; n     cout << ans.size() << endl; n     reverse(ans.begin(), ans.end()); n     cout << ans << endl; n     return 0; n   } n   cout <<  BIG  << endl; n   cout << p << endl; n   return 0; n } n signed solve1() { n   if (check()) { n     return answer(); n   } n   return fail(); n } n inline bool inc() { n   for (long long i = 1; i < n; i++) { n     if (b[i + 1] <= b[i]) return false; n   } n   return true; n } n bool R() { n   if (inc()) return true; n   reverse(b + 1, b + 1 + n); n   r++; n   ans +=  R ; n   return inc(); n } n inline bool P() { n   for (long long i = n; i >= 1; i--) { n     b[i] = b[i] - b[i - 1]; n     if (b[i] < 1) return false; n   } n   p++; n   ans +=  P ; n   return true; n } n signed solve2() { n   bool rev = false; n   if (a[1] > a[2]) swap(a[1], a[2]), r++, rev = true; n   while (!check()) { n     if (b[1] == b[2]) return fail(); n     if (b[1] > b[2]) { n       swap(b[1], b[2]), r++; n       if (p <= 2e5) ans +=  R ; n       continue; n     } n     if (b[1] == b[2]) return fail(); n     if (a[1] == b[1]) { n       if ((b[2] - a[2]) % b[1] == 0 && b[2] > a[2]) { n         long long op = (b[2] - a[2]) / b[1]; n         p += op; n         if (p <= 2e5) { n           while (op--) ans +=  P ; n         } n         break; n       } else n         return fail(); n     } n     long long op = b[2] / b[1]; n     p += op; n     if (p <= 2e5) n       while (op--) ans +=  P ; n     b[2] = b[2] % b[1]; n     if (b[2] == 0) return fail(); n   } n   if (rev) ans +=  R ; n   return answer(); n } n signed solve() { n   while (!check()) { n     if (!R()) return fail(); n     if (!P()) return fail(); n   } n   answer(); n   return 0; n } n signed main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   ; n   cin >> n; n   for (long long i = 1; i <= n; i++) cin >> a[i]; n   for (long long i = 1; i <= n; i++) cin >> b[i]; n   if (n == 1) return solve1(); n   if (n == 2) return solve2(); n   return solve(); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 3e5 + 5; n const int INF = 0x7fffffff; n vector<int> G[maxn]; n long long weight[maxn]; n long long dp[maxn]; n int ans; n long long maxsum = -INF; n void dfs(int now, int fa) { n   dp[now] = weight[now]; n   for (int v : G[now]) { n     if (v != fa) { n       dfs(v, now); n       if (dp[v] > 0) dp[now] += dp[v]; n     } n   } n   maxsum = max(maxsum, dp[now]); n } n void dfs2(int now, int fa) { n   dp[now] = weight[now]; n   for (int v : G[now]) { n     if (v != fa) { n       dfs2(v, now); n       if (dp[v] > 0) dp[now] += dp[v]; n     } n   } n   if (dp[now] == maxsum) { n     ans++; n     dp[now] = 0; n   } n } n int main() { n   std::ios::sync_with_stdio(false); n   int n; n   cin >> n; n   for (int i = 1; i <= n; ++i) cin >> weight[i]; n   int from, to; n   for (int i = 2; i <= n; ++i) { n     cin >> from >> to; n     G[from].push_back(to); n     G[to].push_back(from); n   } n   dfs(1, 0); n   dfs2(1, 0); n   cout << maxsum * ans <<     << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   int i, j, k, n, l, n1, n2, m, m1, m2; n   cin >> s; n   l = s.size(); n   for (i = l - 1; i >= 0; i--) { n     n = s[i] - 48; n     m = n; n     if (m == 0) { n       cout <<  YES  << endl << 0 << endl; n       exit(0); n     } n     if (n % 2 == 0) { n       if (m % 8 == 0) { n         cout <<  YES  << endl << m << endl; n         exit(0); n       } n       if (i == 0) { n         break; n       } n       for (j = i - 1; j >= 0; j--) { n         n1 = (s[j] - 48) * 10; n         m1 = n1 + m; n         if (m1 % 8 == 0) { n           cout <<  YES  << endl << m1 << endl; n           exit(0); n         } n         if (j == 0) { n           break; n         } n         for (k = j - 1; k >= 0; k--) { n           n2 = (s[k] - 48) * 100; n           m2 = n2 + m1; n           if (m2 % 8 == 0) { n             cout <<  YES  << endl << m2 << endl; n             exit(0); n           } n         } n       } n     } n   } n   cout <<  NO  << endl; n } n


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


#include <bits/stdc++.h> n int product(long long a) { n   int maxi = 0, mini = 9, x, ans; n   long long temp = a; n   while (temp) { n     x = temp % 10; n     if (x < mini) mini = x; n     if (x > maxi) maxi = x; n     temp /= 10; n   } n   if (mini == 0) return 0; n   ans = mini * maxi; n   return ans; n } n using namespace std; n int main() { n   ios_base::sync_with_stdio(NULL); n   cin.tie(0); n   cout.tie(0); n   long long a, k, ans; n   int t; n   cin >> t; n   while (t--) { n     cin >> a >> k; n     k--; n     while (k--) { n       if (product(a) == 0) n         break; n       else { n         a += product(a); n       } n     } n     cout << a <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string rem(string second) { n   if (second[second.size() - 1] ==   r ) second.erase(second.size() - 1); n   return second; n } n vector<string> split(string second, char delim) { n   replace(second.begin(), second.end(), delim,    ); n   stringstream ss(second); n   string buf; n   vector<string> v; n   while (ss >> buf) v.push_back(buf); n   return v; n } n long long fpowm(long long x, unsigned long long y, n                 unsigned long long p = LLONG_MAX) { n   long long res = 1; n   x = x % p; n   if (x == 0) return 0; n   while (y > 0) { n     if (y & 1) res = (res * x) % p; n     y = y >> 1; n     x = (x * x) % p; n   } n   return res; n } n inline vector<int> ints(int x) { n   vector<int> A(x); n   for (int i = 0; i < (x); i++) cin >> A[i]; n   return A; n } n inline vector<long long> lls(int x) { n   vector<long long> A(x); n   for (int i = 0; i < (x); i++) cin >> A[i]; n   return A; n } n inline vector<string> strs(int x) { n   vector<string> A(x); n   for (int i = 0; i < (x); i++) cin >> A[i]; n   return A; n } n struct chsh { n   static uint64_t splitmix64(uint64_t x) { n     x += 0x9e3779b97f4a7c15; n     x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; n     x = (x ^ (x >> 27)) * 0x94d049bb133111eb; n     return x ^ (x >> 31); n   } n   size_t operator()(uint64_t x) const { n     static const uint64_t FIXED_RANDOM = n         chrono::steady_clock::now().time_since_epoch().count(); n     return splitmix64(x + FIXED_RANDOM); n   } n }; n const int MOD = 1000000007; n const double EPS = 1e-6; n const int MAXN = -1; n int tc() { n   int N; n   cin >> N; n   vector<int> A = ints(3); n   string B; n   cin >> B; n   vector<pair<int, char>> ans; n   int R[3]; n   std::memset(R, 0, sizeof R); n   for (int i = 0; i < (N); i++) { n     if (B[i] ==  R ) { n       if (A[1]) ans.push_back({i,  P }), A[1]--, R[1]++; n     } else if (B[i] ==  P ) { n       if (A[2]) ans.push_back({i,  S }), A[2]--, R[2]++; n     } else { n       if (A[0]) ans.push_back({i,  R }), A[0]--, R[0]++; n     } n   } n   if (2 * (int)ans.size() < N) return !(cout <<  NO n ); n   cout <<  YES n ; n   vector<char> rem; n   for (int i = 0; i < (3); i++) n     for (int j = 0; j < (A[i]); j++) { n       if (!i) n         rem.push_back( R ); n       else if (i == 1) n         rem.push_back( P ); n       else if (i == 2) n         rem.push_back( S ); n     } n   string ret(N,  ~ ); n   for (auto first : ans) { n     ret[first.first] = first.second; n   } n   for (int i = 0; i < (N); i++) { n     if (ret[i] ==  ~ ) { n       ret[i] = rem.front(); n       rem.erase(rem.begin()); n     } n   } n   cout << ret <<   n ; n   return 0; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int T; n   cin >> T; n   while (T--) tc(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, r; n   cin >> a >> b >> r; n   if (2 * r > min(a, b)) { n     cout <<  Second ; n   } else { n     cout <<  First ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, j, k, a, b; n pair<int, int> c[300005]; n map<pair<long long, long long>, long long> m; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a >> b; n     c[i] = make_pair(a, b); n     m[c[i]] = i; n     if (a > b) { n       k++; n     } n   } n   sort(c, c + n); n   j = n - k; n   if (k > j) { n     cout << k <<   n ; n     for (int i = 0; i < n; i++) { n       if (c[i].first > c[i].second) { n         cout << m[c[i]] + 1 <<    ; n       } n     } n   } else { n     cout << j <<   n ; n     for (int i = n - 1; i >= 0; i--) { n       if (c[i].first < c[i].second) { n         cout << m[c[i]] + 1 <<    ; n       } n     } n   } n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n; n   cin >> n; n   int ar[n]; n   int cnt = 0, cnt2 = 0; n   for (int i = 0; i < n; i++) { n     cin >> ar[i]; n     if (ar[i] > 0) n       cnt += ar[i]; n     else n       cnt2 += ar[i]; n   } n   cout << cnt - cnt2; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   int n, u, v, c, a, b, t; n   cin >> n; n   long long w[n], A, B, Su[n], SMax = 0; n   int g[n], S[n], Pa[n]; n   vector<pair<int, long long>> D[n]; n   vector<int> T, P; n   for (int i = 0; i < n; i++) { n     cin >> w[i]; n     g[i] = 1; n   } n   for (int i = 0; i < n - 1; i++) { n     cin >> u >> v >> c; n     u--; n     v--; n     D[u].push_back({v, c}); n     D[v].push_back({u, c}); n   } n   g[0] = 0; n   T.push_back(0); n   for (int i = 0; i < T.size(); i++) { n     t = 0; n     for (int y = 0; y < D[T[i]].size(); y++) { n       a = D[T[i]][y].first; n       if (g[a] == 1) { n         Pa[a] = T[i]; n         g[a] = 0; n         t = 1; n         T.push_back(a); n       } n     } n     if (t == 0) { n       P.push_back(T[i]); n     } n   } n   T.clear(); n   for (int i = 0; i < n; i++) { n     g[i] = 1; n     S[i] = D[i].size() - 1; n     if (i == 0) { n       S[i]++; n     } n   } n   for (int i = 0; i < P.size(); i++) { n     g[P[i]] = 0; n     a = P[i]; n     if (a != 0) { n       S[Pa[a]]--; n       if (S[Pa[a]] == 0) { n         P.push_back(Pa[a]); n       } n     } n     A = 0; n     B = 0; n     for (int y = 0; y < D[a].size(); y++) { n       b = D[a][y].first; n       c = D[a][y].second; n       if (b != Pa[a]) { n         if (Su[b] - c + w[b] > A) { n           B = A; n           A = Su[b] - c + w[b]; n         } else if (Su[b] - c + w[b] > B) { n           B = Su[b] - c + w[b]; n         } n       } n     } n     SMax = max(SMax, A + B + w[a]); n     Su[a] = A; n   } n   cout << SMax; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n long long n, k; n long long a[100010]; n signed main() { n   scanf( %lld%lld , &n, &k); n   long long cnt = 1; n   long long l = 1; n   long long ans = 0; n   for (long long i = 1; i <= n; i++) { n     scanf( %lld , &a[i]); n     cnt = cnt * a[i] % k; n     if (!cnt) { n       cnt = 1; n       long long j; n       for (j = i; j >= 1 && cnt * a[j] % k; j--) cnt = cnt * a[j] % k; n       ans += (n - i + 1) * (j - l + 1); n       l = j + 1; n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200001; n int n; n vector<pair<int, int> > a[N * 2]; n vector<int> odd; n int used[N * 2], cur[N * 2]; n char res[N]; n void dfs(int u) { n   while (cur[u] < a[u].size()) { n     int v = a[u][cur[u]].first; n     int pos = a[u][cur[u]].second; n     if (used[pos]) { n       cur[u]++; n       continue; n     } n     if (pos <= n) { n       if (u < N) { n         res[pos] =  b ; n       } else { n         res[pos] =  r ; n       } n     } n     used[pos] = 1; n     dfs(v); n     cur[u]++; n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin >> n; n   for (int i = 1; i <= n; i++) { n     int x, y; n     cin >> x >> y; n     a[x].push_back(make_pair(y + N, i)); n     a[y + N].push_back(make_pair(x, i)); n   } n   for (int i = 1; i < N * 2; i++) { n     if (a[i].size() % 2) { n       odd.push_back(i); n     } n     used[i] = cur[i] = 0; n   } n   int cnt = n; n   for (int i = 0; i < odd.size(); i += 2) { n     cnt++; n     a[odd[i]].push_back(make_pair(odd[i + 1], cnt)); n     a[odd[i + 1]].push_back(make_pair(odd[i], cnt)); n   } n   for (int i = 1; i < N * 2; i++) { n     dfs(i); n   } n   cout << (res + 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long linf = 1e18 + 5; n const int mod = (int)1e9 + 7; n const int logN = 17; n const int inf = 1e9; n const int N = 2e5 + 5; n int n, a[N], s; n vector<pair<long long, long long> > T[N << 2], g[N << 2]; n pair<long long, long long> line[N]; n long long pre[N]; n int P[N << 2]; n double intersect(pair<long long, long long> x, pair<long long, long long> y) { n   return (y.second - x.second) * 1.0 / (x.first - y.first); n } n void add(vector<pair<long long, long long> > &T, pair<long long, long long> x) { n   int s = T.size() - 1; n   while (s > 0 && intersect(T[s], T[s - 1]) >= intersect(T[s], x)) { n     T.pop_back(); n     s--; n   } n   T.push_back(x); n } n void init(int k, int bas, int son) { n   P[k] = 0; n   if (bas == son) { n     T[k].clear(); n     g[k].clear(); n     T[k].push_back(line[bas]); n     g[k].push_back(line[bas]); n     return; n   } n   init((k + k), bas, (bas + son >> 1)); n   init((k + k + 1), (bas + son >> 1) + 1, son); n   g[k].resize(g[(k + k)].size() + g[(k + k + 1)].size()); n   merge(g[(k + k)].begin(), g[(k + k)].end(), g[(k + k + 1)].begin(), n         g[(k + k + 1)].end(), g[k].begin()); n   for (__typeof(g[k].begin()) it = g[k].begin(); it != g[k].end(); it++) n     add(T[k], *it); n   P[k] = 0; n } n long long query(int k, int bas, int son, int x, int y, long long t) { n   if (bas > y || son < x) return -linf; n   if (x <= bas && son <= y) { n     while (P[k] < T[k].size() - 1 && intersect(T[k][P[k]], T[k][P[k] + 1]) <= t) n       P[k]++; n     return T[k][P[k]].first * t + T[k][P[k]].second; n   } n   return max(query((k + k), bas, (bas + son >> 1), x, y, t), n              query((k + k + 1), (bas + son >> 1) + 1, son, x, y, t)); n } n int main() { n   scanf( %d , &n); n   long long ans = 0; n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     pre[i] = pre[i - 1] + a[i]; n     ans += a[i] * (long long)i; n   } n   for (int i = 0; i <= n; i++) line[i] = make_pair(i, pre[n] - pre[i]); n   init(1, 0, n); n   long long res = -linf; n   vector<pair<int, int> > look; n   for (int i = 1; i <= n; i++) look.push_back(make_pair(a[i], i)); n   sort(look.begin(), look.end()); n   for (__typeof(look.begin()) it = look.begin(); it != look.end(); it++) { n     long long left = query(1, 0, n, 0, it->second, it->first); n     res = max(res, ans + left - (pre[n] - pre[it->second]) - n                        it->second * (long long)it->first); n     left = query(1, 0, n, it->second + 2, n, it->first); n     res = max(res, ans + left - (pre[n] - pre[it->second]) - n                        it->second * (long long)it->first); n   } n   cout << res <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n char a[22][22], b[22][22], c[22][22]; n bool chk() { n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n; j++) n       if (a[i][j] != b[i][j]) return false; n   return true; n } n void flip() { n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n - j - 1; j++) swap(a[i][j], a[i][n - j - 1]); n } n void rotate() { n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n; j++) c[j][n - i - 1] = a[i][j]; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n; j++) a[i][j] = c[i][j]; n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %s , a[i]); n   for (int i = 0; i < n; i++) scanf( %s , b[i]); n   bool f = false; n   for (int i = 0; i < 2 && !f; i++) { n     for (int j = 0; j < 4 && !f; j++) { n       if (chk()) f = true; n       rotate(); n     } n     flip(); n   } n   puts(f ?  Yes  :  No ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int getc() { n   char ch = getchar(); n   while (ch !=  0  && ch !=  1 ) { n     ch = getchar(); n   } n   return ch -  0 ; n } n int main() { n   int n; n   scanf( %d , &n); n   int cnt1 = 0, cnt2 = 0; n   for (int i = 0; i < n; ++i) { n     if (getc() == i % 2) n       cnt1++; n     else n       cnt2++; n   } n   printf( %d n , min(cnt1, cnt2)); n   ; n   ; n   ; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n struct Segtree { n   int n; n   T e; n   vector<T> dat; n   typedef function<T(T a, T b)> Func; n   Func f; n   Segtree() {} n   Segtree(int n_input, Func f_input, T e_input) { n     initialize(n_input, f_input, e_input); n   } n   void initialize(int n_input, Func f_input, T e_input) { n     f = f_input; n     e = e_input; n     n = 1; n     while (n < n_input) n <<= 1; n     dat.resize(2 * n - 1, e); n   } n   void update(int k, T a) { n     k += n - 1; n     dat[k] = a; n     while (k > 0) { n       k = (k - 1) / 2; n       dat[k] = f(dat[2 * k + 1], dat[2 * k + 2]); n     } n   } n   T get(int k) { return dat[k + n - 1]; } n   T between(int a, int b) { return query(a, b + 1, 0, 0, n); } n   T query(int a, int b, int k, int l, int r) { n     if (r <= a || b <= l) return e; n     if (a <= l && r <= b) return dat[k]; n     T vl = query(a, b, 2 * k + 1, l, (l + r) / 2); n     T vr = query(a, b, 2 * k + 2, (l + r) / 2, r); n     return f(vl, vr); n   } n }; n void chmin(int64_t& a, int64_t b) { a = min(a, b); } n int main() { n   int N, C, A[100000]; n   cin >> N >> C; n   for (int i = 0; i < N; i++) cin >> A[i]; n   Segtree<int> st( n       N, [](int a, int b) { return min(a, b); }, 1e9); n   Segtree<int64_t> sum( n       N, [](int64_t a, int64_t b) { return a + b; }, 0); n   for (int i = 0; i < N; i++) { n     st.update(i, A[i]); n     sum.update(i, A[i]); n   } n   vector<int64_t> dp(N + 1, 1e18); n   dp[0] = 0; n   for (int i = 0; i < N; i++) { n     chmin(dp[i + 1], dp[i] + A[i]); n     if (i + C <= N) n       chmin(dp[i + C], n             dp[i] + sum.between(i, i + C - 1) - st.between(i, i + C - 1)); n   } n   cout << dp[N] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dp[2][1 << 16]; n char art[7][1008]; n int n; n int c[5]; n vector<int> modi[1008]; n void upd(int &x, int y) { x = min(x, y); } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= 4; i++) scanf( %d , &c[i]); n   for (int i = 1; i <= 4; i++) scanf( %s , art[i] + 1); n   int now = 0; n   memset(dp[now], 0x3f, sizeof dp[now]); n   dp[now][0] = 0; n   for (int i = 1; i <= 4; i++) { n     for (int j = 1; j <= 4 - i + 1; j++) { n       int mas = (1 << 16) - 1; n       for (int x = 0; x < i; x++) n         for (int y = j - 1; y < j - 1 + i; y++) { n           mas ^= (1 << (x * 4 + y)); n         } n       modi[i].push_back(mas); n     } n   } n   for (int i = 1; i <= n + 4; i++) { n     now ^= 1; n     memset(dp[now], 0x3f, sizeof dp[now]); n     int nm = 0; n     for (int j = 1; j <= 4; j++) n       if (art[j][i] ==  * ) nm |= (1 << 11 + j); n     for (int j = (1 << 16) - 1; j >= 0; j--) n       if (dp[now ^ 1][j] < 1e9) { n         for (int k = 1; k <= 4; k++) n           for (int l = 0; l < modi[k].size(); l++) n             upd(dp[now ^ 1][j & modi[k][l]], dp[now ^ 1][j] + c[k]); n         if (j & 15) continue; n         upd(dp[now][(j >> 4) | nm], dp[now ^ 1][j]); n       } n   } n   printf( %d n , dp[now][0]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 106; n long long d, A[N], f[N]; n bool vs[N]; n int X[N], Y[N], n; n queue<int> q; n inline long long dis(int a, int b) { n   return abs(X[a] - X[b]) + abs(Y[a] - Y[b]); n } n bool judge(long long init) { n   memset(f, -1, sizeof f); n   while (!q.empty()) q.pop(); n   memset(vs, 0, sizeof vs); n   q.push(1), f[1] = init, vs[1] = 1; n   while (!q.empty()) { n     int u = q.front(); n     q.pop(), vs[u] = 0; n     for (int i = 1; i <= n; i++) { n       if (dis(i, u) * d > f[u] || i == u) continue; n       if (f[u] - dis(i, u) * d + A[i] > f[i]) { n         f[i] = f[u] - dis(i, u) * d + A[i]; n         if (!vs[i]) q.push(i), vs[i] = 1; n       } n     } n   } n   if (f[n] == -1) return false; n   return true; n } n int main() { n   scanf( %d%I64d , &n, &d); n   for (int i = 2; i < n; i++) scanf( %I64d , &A[i]); n   for (int i = 1; i <= n; i++) scanf( %d%d , &X[i], &Y[i]); n   long long L = 0, R = ((1LL) << 60); n   while (L < R) { n     long long mid = (L + R) >> 1; n     if (judge(mid)) n       R = mid; n     else n       L = mid + 1; n   } n   if (!judge(L)) L++; n   printf( %I64d n , L); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void RI() {} n template <typename... T> n void RI(int &head, T &...tail) { n   scanf( %d , &head); n   RI(tail...); n } n struct Maxflow { n   struct E { n     int to, cap, rev, rcap, ucap; n     E() {} n     E(int _to, int _cap, int _rev) n         : to(_to), cap(_cap), rev(_rev), rcap(_cap) {} n   }; n   int n; n   vector<vector<E> > e; n   vector<vector<E>::iterator> cur; n   vector<int> dis; n   void init(int _n) { n     n = _n; n     e.resize(n); n     cur.resize(n); n     dis.resize(n); n     for (int i = 0; i < int(n); i++) e[i].clear(); n   } n   void add(int a, int b, int c) { n     if (a == b) return; n     e[a].push_back(E(b, c, e[b].size())); n     e[b].push_back(E(a, 0, e[a].size() - 1)); n   } n   bool bfs() { n     fill(dis.begin(), dis.end(), -1); n     queue<int> q; n     dis[0] = 0; n     q.push(0); n     while (!q.empty() && dis[n - 1] == -1) { n       int p = q.front(); n       q.pop(); n       for (auto it = (e[p]).begin(); it != (e[p]).end(); it++) { n         if (it->cap == 0 || dis[it->to] != -1) continue; n         dis[it->to] = dis[p] + 1; n         q.push(it->to); n       } n     } n     return dis[n - 1] != -1; n   } n   long long go(int p, long long c) { n     if (p == n - 1) return c; n     long long ret = 0; n     for (; cur[p] != e[p].end(); cur[p]++) { n       if (cur[p]->cap == 0 || dis[cur[p]->to] != dis[p] + 1) continue; n       long long now = go(cur[p]->to, min((long long)cur[p]->cap, c - ret)); n       ret += now; n       cur[p]->cap -= now; n       e[cur[p]->to][cur[p]->rev].cap += now; n       if (ret == c) break; n     } n     if (ret == 0) dis[p] = -1; n     return ret; n   } n   long long maxflow() { n     long long ret = 0; n     while (bfs()) { n       for (int i = 0; i < int(n); i++) cur[i] = e[i].begin(); n       ret += go(0, 1LL << 60); n     } n     return ret; n   } n   bool _addPath(int wh, vector<vector<int> > &ans) { n     if (wh == n - 1) { n       ans.push_back(vector<int>()); n       ans.back().push_back(wh); n       return true; n     } n     for (auto it = (e[wh]).begin(); it != (e[wh]).end(); it++) n       if (it->ucap > 0 && _addPath(it->to, ans)) { n         it->ucap--; n         ans.back().push_back(wh); n         return true; n       } n     return false; n   } n   vector<vector<int> > getPath() { n     vector<vector<int> > ans; n     for (auto it = (e).begin(); it != (e).end(); it++) n       for (auto jt = (*it).begin(); jt != (*it).end(); jt++) n         jt->ucap = max(0, jt->rcap - jt->cap); n     while (_addPath(0, ans)) reverse(ans.back().begin(), ans.back().end()); n     return ans; n   } n } flow; n bool pt[51400]; n void predo() { n   for (int i = 2; i < 51400; i++) n     if (!pt[i]) n       for (int j = i + i; j < 51400; j += i) pt[j] = 1; n } n int n, a[210]; n vector<int> e[210], sol; n bool vis[210]; n void dfs(int p) { n   sol.push_back(p); n   vis[p] = 1; n   for (auto it = (e[p]).begin(); it != (e[p]).end(); it++) n     if (!vis[*it]) { n       dfs(*it); n       break; n     } n } n int main() { n   predo(); n   RI(n); n   for (int i = (1); i <= int(n); i++) RI(a[i]); n   int sink = n + 1; n   flow.init(sink + 1); n   for (int i = (1); i <= int(n); i++) { n     if (a[i] % 2 == 0) n       flow.add(0, i, 2); n     else n       flow.add(i, sink, 2); n   } n   for (int i = (1); i <= int(n); i++) n     if (a[i] % 2 == 0) n       for (int j = (1); j <= int(n); j++) n         if (!pt[a[i] + a[j]]) flow.add(i, j, 1); n   int mf = flow.maxflow(); n   if (mf != n) { n     puts( Impossible ); n     return 0; n   } n   auto ps = flow.getPath(); n   for (auto it = (ps).begin(); it != (ps).end(); it++) { n     assert(((int)(*it).size()) == 4); n     int x = it->at(1); n     int y = it->at(2); n     e[x].push_back(y); n     e[y].push_back(x); n   } n   for (int i = (1); i <= int(n); i++) assert(((int)(e[i]).size()) == 2); n   vector<vector<int> > sols; n   for (int i = (1); i <= int(n); i++) n     if (!vis[i]) { n       sol.clear(); n       dfs(i); n       assert(((int)(sol).size()) % 2 == 0 && ((int)(sol).size()) >= 4); n       sols.push_back(sol); n     } n   printf( %d n , ((int)(sols).size())); n   for (auto &s : sols) { n     printf( %d , ((int)(s).size())); n     for (auto it = (s).begin(); it != (s).end(); it++) printf(  %d , *it); n     puts(  ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int i, j, n, t, x; n   string s =   ; n   cin >> t; n   for (i = 0; i < t; i++) { n     s +=  o ; n   } n   int ara[t + 5]; n   ara[0] = 1; n   ara[1] = 1; n   s[0] =  O ; n   s[1] =  O ; n   for (i = 2; i <= t; i++) { n     ara[i] = ara[i - 1] + ara[i - 2]; n     if (ara[i] <= t) s[ara[i] - 1] =  O ; n     if (ara[i] > t) n       break; n     else n       continue; n   } n   cout << s <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n const int INF = 0x3f3f3f3f; n const int maxn = 1e6 + 50; n const int Mod = 1e9 + 7; n using namespace std; n int n, m, sx, sy, dx, dy; n int get(int sx, int sy, int ex, int ey) { n   int cx = abs(sx - ex), cy = abs(sy - ey); n   if (cx == 0 && cy == 0) return 0; n   if (cx % dx != 0 || cy % dy != 0) return INF; n   if (sx == ex) { n     if (!(sx - dx >= 1 || sx + dx <= n)) return INF; n   } else if (sy == ey) { n     if (!(sy - dy >= 1 || sy + dy <= m)) return INF; n   } n   int t1 = cx / dx, t2 = cy / dy; n   int cha = abs(t1 - t2); n   if (cha & 1) return INF; n   return max(t1, t2); n } n int main() { n   while (scanf( %d %d %d %d %d %d , &n, &m, &sx, &sy, &dx, &dy) != EOF) { n     int ans = INF; n     ans = min(get(sx, sy, 1, 1), ans); n     ans = min(get(sx, sy, 1, m), ans); n     ans = min(get(sx, sy, n, 1), ans); n     ans = min(get(sx, sy, n, m), ans); n     if (ans == INF) n       puts( Poor Inna and pony! ); n     else n       printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char op[] = { R ,  G ,  B ,  Y ,  W ,  1 ,  2 ,  3 ,  4 ,  5 }; n string s[555]; n int main() { n   int n, i, j, k; n   ios_base::sync_with_stdio(0), cin.tie(0); n   cin >> n; n   for (i = 1; i <= n; i++) { n     cin >> s[i]; n   } n   map<char, bool> mp; n   int mn = 100000; n   for (int msk = 0; msk < (1024); msk++) { n     mp.clear(); n     for (i = 0; i < 10; i++) { n       if (msk & (1 << i)) { n         mp[op[i]] = 1; n       } n     } n     bool f = 1; n     for (i = 1; i <= n; i++) { n       for (j = i + 1; j <= n; j++) { n         if (s[i] == s[j]) continue; n         if (s[i][0] == s[j][0]) { n           if (!mp[s[i][1]] && !mp[s[j][1]]) { n             f = 0; n             break; n           } n         } else if (s[i][1] == s[j][1]) { n           if (!mp[s[i][0]] && !mp[s[j][0]]) { n             f = 0; n             break; n           } n         } else { n           if (!mp[s[i][1]] && !mp[s[j][1]] && !mp[s[i][0]] && !mp[s[j][0]]) { n             f = 0; n             break; n           } n         } n       } n       if (!f) break; n     } n     if (f == 1) mn = min(mn, __builtin_popcount(msk)); n   } n   cout << mn << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   cin >> n >> k; n   vector<pair<long long, long long>> v(n); n   for (int i = 0; i < n; i++) cin >> v[i].second >> v[i].first; n   sort(v.rbegin(), v.rend()); n   long long ans = 0, sum = 0; n   priority_queue<long long, vector<long long>, greater<long long>> pq; n   for (int i = 0; i < n; i++) { n     pq.push(v[i].second); n     sum += v[i].second; n     if (pq.size() > k) { n       sum -= pq.top(); n       pq.pop(); n     } n     ans = max(ans, v[i].first * sum); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e2; n int i, a[N], n; n int main() { n   cin >> n; n   for (i = 0; i < n; i++) cin >> a[i]; n   sort(a, a + n); n   cout << a[n - 1] <<    ; n   for (i = 1; i < n - 1; i++) cout << a[i] <<    ; n   cout << a[0]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int TEST, CASE = 1; n int n, m, a, b, c, k, x, y, z, len, total = 0; n string s, temp, ans; n vector<string> v; n bool cmp(string &a, string &b) { n   if (a.size() < b.size()) n     return true; n   else n     return false; n } n int main(void) { n   cin >> n; n   for (int i = 0; i < n; i++) cin >> s, v.push_back(s); n   sort(v.begin(), v.end(), cmp); n   for (int i = 1; i < n; i++) { n     if (v[i].find(v[i - 1]) > v[i].size()) { n       cout <<  NO  << endl; n       return 0; n     } n   } n   cout <<  YES  << endl; n   for (int i = 0; i < n; i++) cout << v[i] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int Set(int N, int pos) { return N |= (1LL << pos); } n int Reset(int N, int pos) { return N &= ~(1LL << pos); } n bool Check(int N, int pos) { return N & (1LL << pos); } n int dx[] = {0, 0, 1, -1, 1, -1, 1, -1}, dy[] = {1, -1, 0, 0, 1, -1, -1, 1}; n inline void cn(long &n) { n   n = 0; n   long ch = getchar(); n   int sign = 1; n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) sign = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) n     n = (n << 3) + (n << 1) + ch -  0 , ch = getchar(); n   n = n * sign; n } n template <class T> n void cmin(T &a, T b) { n   if (b < a) a = b; n } n template <class T> n void cmax(T &a, T b) { n   if (b > a) a = b; n } n template <class T> n int len(const T &c) { n   return (int)c.size(); n } n template <class T> n int len(char c[]) { n   return (int)strlen(c); n } n long stoi(string s) { n   long n = 0; n   for (int i(0), _n(len(s)); i < _n; ++i) n = n * 10 + (s[i] - 48); n   return n; n } n int main() { n   ios_base::sync_with_stdio(false); n   long m, t, r, w, k, ww, res = 0, ls; n   cin >> m >> t >> r; n   vector<int> v, tem; n   int f = 1; n   for (int i(0), _n(m); i < _n; ++i) { n     cin >> w; n     ls = -100000000000000000LL; n     if (!f) continue; n     for (int j = len(v) - 1; j >= 0; j--) { n       if (v[j] >= w) n         tem.push_back(v[j]); n       else n         break; n     } n     if (len(tem) < r) { n       k = r - len(tem); n       ww = w; n       for (int j(0), _n(k); j < _n; ++j) { n         if (ww + t - 1 >= w) { n           res++; n           tem.push_back(ww + t - 1); n           ww -= 1; n         } else { n           f = 0; n           break; n         } n       } n     } n     v = tem; n     tem.clear(); n     sort(v.begin(), v.end()); n   } n   if (f == 0) res = -1; n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double pi = acos(-1); n double a, b, c[100005], mx; n int n; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %lf %lf , &a, &b); n     c[i] = (atan2(b, a) * 180) / pi; n   } n   sort(c, c + n); n   for (int i = 0; i < n - 1; i++) { n     mx = max(mx, c[i + 1] - c[i]); n   } n   mx = max(mx, 360 - c[n - 1] + c[0]); n   printf( %0.08lf n , 360 - mx); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, ans = 1e9; n pair<int, int> p[1005]; n void solve(int height) { n   int width = 0, remain = n / 2; n   vector<int> priority; n   for (int i = 1; i <= n; i++) n     if (p[i].second > height) n       if (p[i].first > height) n         return; n       else if (remain) { n         remain--; n         width += p[i].second; n       } else n         return; n     else if (p[i].first > height) n       width += p[i].first; n     else if (p[i].first > p[i].second) { n       width += p[i].second; n       priority.push_back(p[i].first - p[i].second); n     } else n       width += p[i].first; n   sort(priority.rbegin(), priority.rend()); n   for (int i = remain; i < priority.size(); i++) width += priority[i]; n   ans = min(ans, width * height); n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d %d , &p[i].first, &p[i].second); n   for (int i = 1; i <= 1000; i++) solve(i); n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   int x, num; n } s[100009]; n int mark[100009]; n int cmp(node a, node b) { return a.x < b.x; } n int main() { n   int n, m; n   while (scanf( %d %d , &n, &m) == 2) { n     int l, h; n     scanf( %d %d , &l, &h); n     for (int i = 0; i < n; i++) { n       int a, b; n       scanf( %d %d , &a, &b); n       s[i].x = a * l + b * h; n       s[i].num = i + 1; n     } n     sort(s, s + n, cmp); n     int ans = 0; n     int k = 0; n     for (int i = 0; i < n; i++) { n       m -= s[i].x; n       if (m < 0) break; n       ans++; n       mark[k++] = s[i].num; n     } n     printf( %d n , ans); n     for (int i = 0; i < ans; i++) { n       if (i == 0) n         printf( %d , mark[i]); n       else n         printf(  %d , mark[i]); n     } n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 2000 * 100 + 1000; n vector<long long> adj[N]; n long long a[N], h[N], t[N]; n bool mark[N]; n long long f(long long p) { n   mark[p] = true; n   long long ans = h[p] * a[p]; n   t[p] = a[p]; n   for (auto q : adj[p]) n     if (mark[q] == false) { n       h[q] = h[p] + 1; n       ans += f(q); n       t[p] += t[q]; n     } n   return ans; n } n long long ans = 0, sum = 0; n void calc(long long val, long long p) { n   mark[p] = true; n   ans = max(ans, val); n   for (auto q : adj[p]) n     if (mark[q] == false) { n       long long k = sum - 2 * t[q]; n       calc(val + k, q); n     } n } n int32_t main() { n   ios::sync_with_stdio(false); n   long long n; n   cin >> n; n   for (long long i = 0; i < n; i++) { n     cin >> a[i]; n     sum += a[i]; n   } n   for (long long i = 0; i < n - 1; i++) { n     long long u, v; n     cin >> u >> v; n     u--; n     v--; n     adj[u].push_back(v); n     adj[v].push_back(u); n   } n   long long val = f(0); n   for (long long i = 0; i < N; i++) mark[i] = false; n   calc(val, 0); n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   for (int i = 0; i < s.size(); i++) { n     if (s[i] ==  1 ) continue; n     if (s[i] ==  4  && s[i - 1] ==  1 ) continue; n     if (s[i] ==  4  && i > 1 && s[i - 1] ==  4  && s[i - 2] ==  1 ) continue; n     return cout <<  NO , 0; n   } n   cout <<  YES ; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5010; n int n, m, s; n vector<int> e[N]; n int low[N], dfn[N], idx; n bool vis[N]; n stack<int> st; n bool in_stack[N]; n int color[N], color_tot; n int indeg[N]; n int ans; n void AddEdge(int u, int v) { e[u].push_back(v); } n void Dfs(int u) { n   low[u] = dfn[u] = ++idx; n   in_stack[u] = true; n   st.push(u); n   for (int v : e[u]) { n     if (!dfn[v]) { n       Dfs(v); n       low[u] = min(low[u], low[v]); n     } else if (in_stack[v]) { n       low[u] = min(low[u], dfn[v]); n     } n   } n   if (low[u] == dfn[u]) { n     ++color_tot; n     int v; n     do { n       v = st.top(); n       st.pop(); n       in_stack[v] = false; n       color[v] = color_tot; n     } while (v != u); n   } n } n void Tarjan() { n   for (int i = 1; i <= n; ++i) n     if (!color[i]) Dfs(i); n } n int main() { n   scanf( %d%d%d , &n, &m, &s); n   for (int i = 1; i <= m; ++i) { n     int u, v; n     scanf( %d%d , &u, &v); n     AddEdge(u, v); n   } n   Tarjan(); n   for (int i = 1; i <= n; ++i) n     ; n   for (int i = 1; i <= n; ++i) n     for (int j : e[i]) n       if (color[i] != color[j]) ++indeg[color[j]]; n   for (int i = 1; i <= color_tot; ++i) n     ; n   for (int i = 1; i <= color_tot; ++i) n     if (indeg[i] == 0) ++ans; n   if (indeg[color[s]] == 0) --ans; n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, m, a; n   cin >> n >> m >> a; n   long long v = n / a; n   long long t = m / a; n   if (n % a != 0) v++; n   if (m % a != 0) t++; n   cout << v * t; n } n


#include <bits/stdc++.h> n using namespace std; n struct Edge { n   int v; n   char c; n } edge[220]; n vector<Edge> ans; n vector<int> Chain[10]; n int N; n bool flag[110]; n void dfs(int x) { n   for (int i = 0; i < Chain[x].size(); i++) { n     int v = edge[Chain[x][i]].v, id = Chain[x][i] >> 1; n     if (!flag[id]) { n       flag[id] = 1; n       dfs(v); n       ans.push_back((Edge){id, edge[Chain[x][i]].c}); n     } n   } n } n void print() { n   for (int i = N - 1; i >= 0; i--) printf( %d %c n , ans[i].v + 1, ans[i].c); n } n int main() { n   scanf( %d , &N); n   for (int i = 0; i < N; i++) { n     int a, b; n     scanf( %d%d , &a, &b); n     edge[2 * i] = (Edge){b,  + }; n     Chain[a].push_back(2 * i); n     edge[2 * i + 1] = (Edge){a,  - }; n     Chain[b].push_back(2 * i + 1); n   } n   int u = edge[0].v, sum = 0; n   for (int i = 0; i <= 6; i++) n     if (1 & Chain[i].size()) u = i, sum++; n   if (sum == 0 || sum == 2) { n     dfs(u); n     if (ans.size() == N) n       print(); n     else n       printf( No solution ); n   } else n     printf( No solution ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n bool umin(T& a, T b) { n   if (a > b) { n     a = b; n     return 1; n   } n   return 0; n } n template <class T> n bool umax(T& a, T b) { n   if (a < b) { n     a = b; n     return 1; n   } n   return 0; n } n const int N = 1e3 + 9; n double dp[N][N], prob[N]; n bool vis[N][N]; n long long sum[22], pw[22]; n double rec(int x, int y) { n   umax(y, 0); n   if (!x) { n     if (!y) return 1.0; n     return 0.0; n   } n   double& ret = dp[x][y]; n   if (vis[x][y]) return ret; n   vis[x][y] = 1; n   return ret = rec(x - 1, y - 1) * prob[x] + rec(x - 1, y) * (1 - prob[x]); n } n long long f(long long x) { n   if (!x) return 0; n   vector<int> v; n   while (x >= 1) { n     v.push_back(x % 10); n     x /= 10; n   } n   reverse(v.begin(), v.end()); n   if (v[0] > 1) return sum[v.size()]; n   long long res = sum[v.size() - 1]; n   for (int i = 1; i < int(v.size()); i++) { n     if (v[i] == 0) continue; n     res += (v[i]) * 1LL * pw[v.size() - i - 1]; n   } n   return res + 1; n } n int main() { n   pw[0] = 1; n   for (int i = 1; i <= 18; i++) { n     pw[i] = pw[i - 1] * 10; n     sum[i] = sum[i - 1] + pw[i - 1]; n   } n   int n, k; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     long long x, y; n     scanf( %lld%lld , &x, &y); n     long long res = f(y) - f(x - 1); n     prob[i] = double(res) / double(y - x + 1); n   } n   scanf( %d , &k); n   printf( %.12lf n , rec(n, (k * n + 99) / 100)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long spf[3000001]; n long long fac[3000001]; n void sieve() { n   spf[1] = 1; n   for (long long i = 2; i < 3000001; i++) spf[i] = i; n   for (long long i = 4; i < 3000001; i += 2) spf[i] = 2; n   for (long long i = 3; i * i < 3000001; i++) { n     if (spf[i] == i) { n       for (long long j = i * i; j < 3000001; j += i) n         if (spf[j] == j) spf[j] = i; n     } n   } n } n map<long long, long long> getfactor(long long a) { n   map<long long, long long> m; n   while (a > 1) { n     m[spf[a]]++; n     a /= spf[a]; n   } n   return m; n } n long long power(long long x, long long y, long long p) { n   long long res = 1; n   x = x % p; n   while (y > 0) { n     if (y & 1) res = (res * x) % p; n     y = y >> 1; n     x = (x * x) % p; n   } n   return res; n } n long long gcd(long long a, long long b) { n   if (a == 0) return b; n   return gcd(b % a, a); n } n long long inverse(long long a, long long p) { return power(a, p - 2, p); } n long long ncr(long long n, long long r, long long p) { n   if (r > n) return 0; n   if (r == 0) return 1; n   return (fac[n] * inverse(fac[r], p) % p * inverse(fac[n - r], p) % p) % p; n } n void solve() { n   long long n, k; n   cin >> n >> k; n   map<long long, long long> m; n   for (long long i = 1; i <= n; i++) { n     long long l, r; n     cin >> l >> r; n     m[l]++; n     m[r + 1]--; n   } n   long long pre = 0; n   for (auto it : m) { n     pre += m[it.first]; n     m[it.first] = pre; n   } n   long long prev = -1e18, val = 0; n   long long ans = 0; n   for (auto it : m) { n     if (prev == -1e18) { n       prev = it.first; n       val = it.second; n     } else { n       ans = (ans + n              (ncr(val, k, 1000000007) * (it.first - prev) % 1000000007) % n                  1000000007 + n              1000000007) % n             1000000007; n       prev = it.first; n       val = it.second; n     } n   } n   cout << ans <<   n ; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long test = 1; n   fac[0] = 1; n   for (long long i = 1; i <= 300000; i++) n     fac[i] = (fac[i - 1] * i) % 1000000007; n   while (test--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long int pow_mod(long long int a, long long int b) { n   long long int res = 1; n   while (b != 0) { n     if (b & 1) { n       res = (res * a) % 1000000007; n     } n     a = (a * a) % 1000000007; n     b /= 2; n   } n   return res; n } n long long int inv(long long int x) { return pow_mod(x, 1000000007 - 2); } n void solve() { n   long long int n; n   cin >> n; n   long long int x = 2 * n - 1; n   long long int y = n; n   long long int res1 = 1; n   for (long long int i = 0; i < y; i++) res1 = (res1 * (x - i)) % 1000000007; n   long long int res2 = 1; n   for (long long int i = 1; i <= y; i++) res2 = (res2 * i) % 1000000007; n   long long int ans = (res1 * inv(res2)) % 1000000007; n   ans *= 2; n   ans %= 1000000007; n   ans -= n; n   ans += 1000000007; n   ans %= 1000000007; n   cout << ans << endl; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int t; n   t = 1; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const unsigned long long INF = ~0ll; n unsigned long long prime[16] = {2,  3,  5,  7,  11, 13, 17, 19, n                                 23, 29, 31, 37, 41, 43, 47, 53}; n int n; n unsigned long long ans; n void dfs(int deepth, unsigned long long v, unsigned long long num) { n   if (num > n || deepth > 15) return; n   if (num == n && v < ans) { n     ans = v; n     return; n   } n   for (unsigned long long i = 1; i <= 63; i++) { n     if (ans / prime[deepth] < v) break; n     dfs(deepth + 1, v *= prime[deepth], num * (i + 1)); n   } n } n int main() { n   while (cin >> n) { n     ans = INF; n     dfs(0, 1, 1); n     cout << ans << endl; n   } n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const long long p = 1000000007LL; n long long power(long long x, long long n) { n   long long ret = 1LL; n   while (n > 0LL) { n     if (n & 1LL) ret = ret * x % p; n     x = x * x % p; n     n >>= 1LL; n   } n   return ret; n } n long long n, m; n long long dp; n int main() { n   cin >> n >> m; n   dp = 1; n   for (long long i = 1LL; i <= n; i++) n     dp = (dp * (m * 2LL - 1LL) + power(m, i - 1LL)) % p; n   cout << dp << endl; n } n


#include <bits/stdc++.h> n using namespace std; n typedef pair<int, int> pii; n typedef long long ll; n typedef unsigned long long ull; n template <typename T> n bool umax(T& a, const T& b) { n   return b <= a ? false : (a = b, true); n } n template <typename T> n bool umin(T& a, const T& b) { n   return b >= a ? false : (a = b, true); n } n template <typename T> n void V2A(T a[], const vector<T>& b) { n   for (int i = 0; i < b.size(); i++) a[i] = b[i]; n } n template <typename T> n void A2V(vector<T>& a, const T b[]) { n   for (int i = 0; i < a.size(); i++) a[i] = b[i]; n } n const double PI = acos(-1.0); n const int INF = 1e9 + 7; n const int maxn = 2e3 + 7; n bool plot[maxn][maxn]; n int r[maxn][maxn]; n int n, m, k; n int check(int row, int col) { n   if (col >= 0 && col < m) return true; n   return false; n } n int main() { n   cin >> n >> m >> k; n   for (int i = 0; i < k; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     x--; n     y--; n     if (!plot[x][y]) { n       printf( %d %d n , x + 1, y + 1); n       plot[x][y] = true; n       continue; n     } n     for (int d = 1; d <= 3; d++) { n       int Max = min(n, x + d + 1); n       for (int row = max(0, x - d); row < Max; row++) { n         int t = d - abs(x - row), col1 = y - t, col2 = y + t; n         if (check(row, col1)) umax(r[x][y], r[row][col1] - d); n         if (check(row, col2)) umax(r[x][y], r[row][col2] - d); n       } n     } n     for (int d = r[x][y] + 1;; d++) { n       int Max = min(n, x + d + 1); n       bool ok = false; n       for (int row = max(0, x - d); row < Max; row++) { n         int t = d - abs(x - row), col1 = y - t, col2 = y + t; n         if (check(row, col1) && !plot[row][col1]) { n           ok = true; n           printf( %d %d n , row + 1, col1 + 1); n           plot[row][col1] = true; n           break; n         } n         if (check(row, col2) && !plot[row][col2]) { n           ok = true; n           printf( %d %d n , row + 1, col2 + 1); n           plot[row][col2] = true; n           break; n         } n       } n       if (ok) { n         r[x][y] = d - 1; n         break; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int n, t; n   cin >> n >> t; n   for (int i = 1; i <= n; i++) { n     int val; n     cin >> val; n     t -= (86400 - val); n     if (t <= 0) { n       cout << i <<   n ; n       return; n     } n   } n } n int main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void desperate_optimization(int precision) { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cout.setf(ios::fixed); n   cout.setf(ios::showpoint); n   cout.precision(precision); n } n const int N = 2e5; n long long arrA[N + 5], arrB[N + 5]; n long long posA[N + 5], posB[N + 5]; n long long res[N + 5]; n long long bit[N + 5]; n int n; n int query(int x) { n   int tot = 0; n   for (; x; x -= (x & -x)) tot += bit[x]; n   return tot; n } n void update(int x) { n   for (; x <= n; x += (x & -x)) bit[x]++; n } n void hora(int tc) { n   cin >> n; n   for (int i = 0; i < n; i++) cin >> arrA[i]; n   for (int i = 0; i < n; i++) cin >> arrB[i]; n   for (int i = 0; i < n; i++) { n     int val = query(arrA[i] + 1); n     posA[i] = arrA[i] - val; n     update(arrA[i] + 1); n   } n   memset(bit, 0, sizeof bit); n   for (int i = 0; i < n; i++) { n     int val = query(arrB[i] + 1); n     posB[i] = arrB[i] - val; n     update(arrB[i] + 1); n   } n   long long cur = 0; n   long long sisa = 0; n   for (int i = n - 1; i >= 0; i--) { n     res[i] = posA[i] + posB[i] + sisa; n     sisa = res[i] / (cur + 1); n     res[i] %= (cur + 1); n     cur += 1; n   } n   memset(bit, 0, sizeof bit); n   for (int i = 0; i < n; i++) { n     int lo = 0; n     int hi = n; n     int ans = -1; n     while (lo <= hi) { n       int mid = (lo + hi) / 2; n       if (mid - query(mid + 1) >= res[i]) { n         ans = mid; n         hi = mid - 1; n       } else n         lo = mid + 1; n     } n     update(ans + 1); n     cout << ans <<    ; n   } n } n int main() { n   desperate_optimization(10); n   int ntc = 1; n   for (int tc = 1; tc <= ntc; tc++) hora(tc); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long maxm = -1e18; n long long minm = 1e18; n long long mod = 1e9 + 7; n long long mod1 = 1e9 + 9; n const long double Pi = 3.141592653; n long long int fact(long long int n) { n   if (n == 0) { n     return 1; n   } else if (n == 1) { n     return 1; n   } n   long long int res = 1; n   for (long long int i = 2; i <= n; i++) res = res * i; n   return res; n } n long long int nCr(long long int n, long long int r) { n   return fact(n) / (fact(r) * fact(n - r)); n } n bool isint(double n) { n   long long int j = n; n   double w = n - j; n   if (w > 0) { n     return false; n   } n   return true; n } n long long int count3(long long int x) { n   long long int ret = 0; n   while (x % 3 == 0) { n     ret++; n     x /= 3; n   } n   return ret; n } n long long int sqr(long long int a) { return a * a; } n int32_t main() { n   ios_base::sync_with_stdio(false), cin.tie(NULL); n   long long t = 1; n   cin >> t; n   while (t--) { n     long long int n; n     cin >> n; n     long long int ans = 1; n     if (n == 1) { n       cout << 1 <<   n ; n       continue; n     } n     for (long long int i = 1; i <= 2 * n - 1; i++) { n       ans = (ans * i); n       ans = (ans) % mod; n     } n     ans = (ans * n) % mod; n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 4e3 + 44; n const int F = 4000; n int n; n int s[M]; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &s[i]); n   printf( %d n , n + 1); n   int tmp, tmp1, tmp2, sum = 0; n   for (int i = n; i >= 1; i--) { n     tmp = (F + i - ((sum + s[i]) % F)) % F; n     sum += tmp; n     printf( %d %d %d n , 1, i, tmp); n   } n   printf( %d %d %d n , 2, n, F); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n static const int maxn = 2e5 + 5; n struct Node { n   bool ok; n   int val; n   int pos, rpos; n   Node(bool ok = 0, int val = 0, int pos = 0, int rpos = 0) n       : ok(ok), val(val), pos(pos), rpos(rpos) {} n   bool operator<(const Node &other) const { n     if (ok and other.ok) { n       if (val == other.val) return pos > other.pos; n       return val < other.val; n     } n     return ok > other.ok; n   } n }; n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   int n; n   cin >> n; n   vector<int> arr(n + 2); n   for (int i = 1; i <= n; i++) cin >> arr[i]; n   vector<int> rgt(n + 1); n   rgt[n] = -1; n   for (int i = n - 1; i >= 1; i--) rgt[i] = i + 1; n   multiset<Node> ms; n   arr[n + 1] = 1e9 + 9; n   for (int i = 1; i <= n; i++) { n     bool ok = arr[i] > arr[i + 1]; n     ms.insert(Node(ok, arr[i], i, i + 1)); n   } n   int min_time = 0; n   map<int, int> mp; n   while (ms.size()) { n     int cnt = 0; n     mp.clear(); n     while (ms.size()) { n       auto it = *ms.begin(); n       if (it.ok == false) break; n       ++cnt; n       ms.erase(it); n       rgt[it.pos] = rgt[it.rpos]; n       mp[rgt[it.rpos]] = it.pos; n     } n     if (cnt == 0) break; n     ++min_time; n     for (auto it : mp) { n       if (it.first == -1) continue; n       int pos = it.second; n       int rpos = it.first; n       bool ok = arr[pos] > arr[rpos]; n       ms.insert(Node(ok, arr[pos], pos, rpos)); n     } n   } n   cout << min_time <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n long long rdtsc() { n   long long tmp; n   asm( rdtsc  :  =A (tmp)); n   return tmp; n } n inline int myrand() { return abs((rand() << 15) ^ rand()); } n inline int rnd(int x) { return myrand() % x; } n const int INF = (int)1e9 + 1; n const long double EPS = 1e-9; n void precalc() {} n const int maxn = (int)1e5 + 10; n int a[2 * maxn]; n int n, w; n bool read() { n   if (scanf( %d%d , &n, &w) < 2) { n     return 0; n   } n   for (int i = 0; i < 2 * n; ++i) { n     scanf( %d , &a[i]); n   } n   return 1; n } n void solve() { n   sort(a, a + 2 * n); n   printf( %.18f n , n          min((double)w, min((double)a[n] / 2, (double)a[0]) * 3 * n)); n } n int main() { n   srand(rdtsc()); n   precalc(); n   while (1) { n     if (!read()) { n       break; n     } n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:100000000,100000000 ) n using namespace std; n const long long inf = 1e9 + 7; n const long long mod = 1e9 + 7; n const double eps = 1e-9; n const double PI = 2 * acos(0.0); n const double E = 2.71828; n int main(void) { n   long long n; n   cin >> n; n   double ans = 0, e = 0; n   for (int i = 0; i < n; i++) { n     double temp; n     cin >> temp; n     ans += temp * (2.0 * e + 1); n     e = temp * (e + 1); n   } n   cout << setprecision(8) << fixed << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string norm(string s) { n   string res = s; n   for (int i = 0; i < 2; i++) { n     for (int j = 0; j < 4; j++) { n       for (int k = 0; k < 4; k++) { n         res = min(s, res); n         swap(s[0], s[1]); n         swap(s[1], s[2]); n         swap(s[2], s[3]); n       } n       swap(s[0], s[4]); n       swap(s[4], s[2]); n       swap(s[2], s[5]); n     } n     swap(s[0], s[1]); n     swap(s[1], s[2]); n     swap(s[2], s[3]); n   } n   return res; n } n int main() { n   set<string> myS; n   string S; n   cin >> S; n   sort(S.begin(), S.end()); n   do { n     myS.insert(norm(S)); n   } while (next_permutation(S.begin(), S.end())); n   cout << myS.size() << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     vector<int> a(n + 1); n     for (int i = 1; i <= n; i++) cin >> a[i]; n     vector<vector<int> > dp(201); n     for (int i = 1; i <= n; i++) { n       dp[a[i]].push_back(i); n     } n     int maximum = 0; n     for (int i = 1; i <= n; i++) { n       int p = a[i]; n       int l = lower_bound(dp[p].begin(), dp[p].end(), i) - begin(dp[p]); n       int r = dp[p].size() - l - 1; n       if (l >= r) continue; n       int lval = dp[p][l]; n       int rval = dp[p][r]; n       int maxi = 0; n       for (int j = 1; j <= 200; j++) { n         if (j == p) { n           maxi = max(maxi, r - l - 1); n           continue; n         } n         int xx = upper_bound(dp[j].begin(), dp[j].end(), rval) - dp[j].begin(); n         int yy = upper_bound(dp[j].begin(), dp[j].end(), lval) - dp[j].begin(); n         maxi = max(maxi, xx - yy); n       } n       maximum = max(maximum, maxi + (2 * (l + 1))); n     } n     cout << max(1, maximum) << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int s, t, ds, dt; n struct edge { n   int u, v; n } E[400005]; n vector<int> vecr[200005]; n int pre[200005]; n int find(int x) { n   if (x != pre[x]) pre[x] = find(pre[x]); n   return pre[x]; n } n void Union(int x, int y) { pre[find(x)] = find(y); } n int cpts[200005]; n int tot; n int conts[200005]; n int contt[200005]; n map<int, int> to; n vector<int> dhas; n vector<int> ans; n void output(int pos) { n   int siz = vecr[pos].size(); n   for (int i = 0; i < siz; i++) { n     int u = E[vecr[pos][i]].u; n     int v = E[vecr[pos][i]].v; n     if (find(u) == find(v)) n       continue; n     else { n       Union(u, v); n       ans.push_back(vecr[pos][i]); n     } n   } n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) pre[i] = i; n   for (int i = 0; i < m; i++) scanf( %d%d , &E[i].u, &E[i].v); n   scanf( %d%d%d%d , &s, &t, &ds, &dt); n   for (int i = 0; i < m; i++) { n     if (E[i].u == s || E[i].u == t || E[i].v == s || E[i].v == t) n       continue; n     else if (find(E[i].u) != find(E[i].v)) { n       Union(E[i].u, E[i].v); n     } n   } n   for (int i = 1; i <= n; i++) n     if (i != s && i != t) cpts[tot++] = find(i); n   sort(cpts, cpts + tot); n   tot = unique(cpts, cpts + tot) - cpts; n   cpts[tot++] = s; n   cpts[tot++] = t; n   for (int i = 0; i < tot; i++) to[cpts[i]] = i; n   memset(conts, -1, sizeof(conts)); n   memset(contt, -1, sizeof(contt)); n   for (int i = 0; i < m; i++) { n     if (E[i].u == s) conts[to[find(E[i].v)]] = i; n     if (E[i].v == s) conts[to[find(E[i].u)]] = i; n     if (E[i].u == t) contt[to[find(E[i].v)]] = i; n     if (E[i].v == t) contt[to[find(E[i].u)]] = i; n     if (find(E[i].u) == find(E[i].v)) vecr[to[find(E[i].u)]].push_back(i); n   } n   for (int i = 1; i <= n; i++) pre[i] = i; n   for (int i = 0; i < tot - 2; i++) { n     output(i); n     if (conts[i] >= 0 && contt[i] >= 0) n       dhas.push_back(i); n     else if (conts[i] >= 0) { n       ans.push_back(conts[i]); n       ds--; n     } else if (contt[i] >= 0) { n       ans.push_back(contt[i]); n       dt--; n     } n   } n   if (dhas.size() == 0) { n     ds--; n     dt--; n     ans.push_back(conts[tot - 1]); n     if (ds < 0 || dt < 0) n       printf( No n ); n     else { n       printf( Yes n ); n       for (int i = 0; i < ans.size(); i++) { n         printf( %d %d n , E[ans[i]].u, E[ans[i]].v); n       } n     } n   } else { n     for (int i = 0; i < dhas.size(); i++) { n       if (i == 0) { n         ans.push_back(conts[dhas[i]]); n         ans.push_back(contt[dhas[i]]); n         ds--; n         dt--; n       } else { n         if (ds < dt) { n           ans.push_back(contt[dhas[i]]); n           dt--; n         } else { n           ans.push_back(conts[dhas[i]]); n           ds--; n         } n       } n     } n     if (ds < 0 || dt < 0) n       printf( No n ); n     else { n       printf( Yes n ); n       for (int i = 0; i < ans.size(); i++) { n         printf( %d %d n , E[ans[i]].u, E[ans[i]].v); n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double power(float x, int y) { n   float temp; n   if (y == 0) return 1; n   temp = power(x, y / 2); n   if (y % 2 == 0) n     return temp * temp; n   else { n     if (y > 0) n       return x * temp * temp; n     else n       return (temp * temp) / x; n   } n } n bool isPrime(int n) { n   if (n <= 1) return false; n   if (n <= 3) return true; n   if (n % 2 == 0 || n % 3 == 0) return false; n   for (int i = 5; i * i <= n; i = i + 6) n     if (n % i == 0 || n % (i + 2) == 0) return false; n   return true; n } n void solve() { n   long long int n; n   cin >> n; n   vector<long long int> a(n); n   for (long long int i = 0; i < n; i++) cin >> a[i]; n   ; n   long long int ans = a[0]; n   long long int t = a[0]; n   for (long long int i = 1; i < n; i++) { n     t += a[i]; n     ans = min(ans, t); n   } n   cout << abs(ans) << endl; n } n int main() { n   long long int t; n   cin >> t; n   while (t--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = acos(-1.0); n const double EPS = 1e-6; n int main() { n   { ; }; n   int n; n   scanf( %d , &n); n   vector<pair<int, int> > V(n); n   int totalW = 0; n   for (int i = 0; i < n; i++) { n     scanf( %d %d , &V[i].first, &V[i].second); n     totalW += V[i].first; n   } n   int res = ((1 << 30) - 1); n   for (int maxH = 1; maxH <= 1000; maxH++) { n     bool testOK = true; n     int coda = n / 2; n     int nowW = totalW; n     vector<int> candidate; n     for (int i = 0; i < n && testOK; i++) { n       if (V[i].second > maxH) { n         if (V[i].first > maxH) n           testOK = false; n         else { n           if (coda == 0) n             testOK = false; n           else n             nowW += V[i].second - V[i].first; n           coda--; n         } n       } else { n         if (V[i].first > V[i].second && V[i].first <= maxH) { n           candidate.emplace_back(V[i].second - V[i].first); n         } n       } n     } n     if (testOK) { n       sort((candidate).begin(), (candidate).end()); n       nowW += accumulate( n           candidate.begin(), n           candidate.begin() + min(((int)(candidate).size()), coda), 0); n       res = min(res, nowW * maxH); n     } n   } n   printf( %d n , res); n   return 0; n } n


#include <bits/stdc++.h> n int findMaximalK(int n); n using namespace std; n int main() { n   int n, b, p, x, y; n   cin >> n >> b >> p; n   y = n * p; n   x = 0; n   int remaining = n; n   while (remaining > 1) { n     int doingMatch = pow(2, findMaximalK(remaining)); n     x += doingMatch * b + doingMatch / 2; n     int passToNextRound = remaining - doingMatch / 2; n     remaining = passToNextRound; n   } n   cout << x <<     << y; n } n int findMaximalK(int n) { n   int count = 0; n   while (n > 1) { n     n /= 2; n     count++; n   } n   return count; n } n


#include <bits/stdc++.h> n using namespace std; n using LL = long long; n const int M = 1e7 + 5; n const int N = 3e5 + 5; n struct node { n   vector<int> h, c; n   node() { n     h.clear(); n     c.clear(); n   } n   void read(int n) { n     int ans; n     for (int i = 0; i < n; i++) { n       scanf( %d , &ans); n       h.push_back(ans); n     } n     for (int i = 0; i < n; i++) { n       scanf( %d , &ans); n       c.push_back(ans); n     } n   } n }; n vector<node> a; n int h[M]; n LL c[M]; n int tl[M], tr[M]; n LL dp[M]; n stack<int> sk; n void reads(int &n) { n   int m, k, q; n   LL mul; n   scanf( %d%d , &m, &n); n   for (int i = 1; i <= m; i++) { n     scanf( %d , &k); n     node tmp; n     tmp.read(k); n     a.push_back(tmp); n   } n   scanf( %d , &q); n   n = 0; n   while (q--) { n     scanf( %d%I64d , &k, &mul); n     k--; n     for (int i = 0; i < a[k].h.size(); i++) { n       h[++n] = a[k].h[i]; n       c[n] = mul * a[k].c[i]; n     } n   } n } n void make_tl(int n) { n   for (int i = n; i >= 1; i--) { n     while (!sk.empty() && h[sk.top()] <= sk.top() - i) { n       tl[sk.top()] = i + 1; n       sk.pop(); n     } n     sk.push(i); n   } n   while (!sk.empty()) { n     tl[sk.top()] = 1; n     sk.pop(); n   } n } n void make_tr(int n) { n   for (int i = 1; i <= n; i++) { n     while (!sk.empty() && h[sk.top()] <= i - sk.top()) { n       tr[sk.top()] = i - 1; n       sk.pop(); n     } n     sk.push(i); n   } n   while (!sk.empty()) { n     tr[sk.top()] = n; n     sk.pop(); n   } n } n int main() { n   int n; n   reads(n); n   make_tl(n); n   make_tr(n); n   for (int i = 1; i <= n; i++) { n     dp[i] = dp[tl[i] - 1] + c[i]; n     while (!sk.empty() && tr[sk.top()] < i) sk.pop(); n     if (!sk.empty()) dp[i] = min(dp[i], dp[sk.top() - 1] + c[sk.top()]); n     if (sk.empty() || dp[sk.top() - 1] + c[sk.top()] > dp[i - 1] + c[i]) n       sk.push(i); n   } n   printf( %I64d n , dp[n]); n   return 0; n } n


#include <bits/stdc++.h> n long long Max(const long long x, const long long y) { n   if (x > y) return x; n   return y; n } n const long long mo = 1e9 + 7, M = 1005; n char s[M]; n long long k, a[M], len, dp[M][M][2]; n long long dfs(const long long pos, const long long dis, const bool pd, n               const bool lim) { n   if (!pos) return pd; n   if (!lim && ~dp[pos][dis][pd]) return dp[pos][dis][pd]; n   long long res = 0, up = lim ? a[pos] : 9; n   for (long long i = 0; i <= up; ++i) n     if (i != 4 && i != 7) n       res = (res + dfs(pos - 1, Max(dis - 1, 0), pd, lim && i == up)) % mo; n     else n       res = (res + dfs(pos - 1, k, dis || pd, lim && i == up)) % mo; n   if (!lim) dp[pos][dis][pd] = res; n   return res; n } n long long check() { n   long long last = 0x3f3f3f3f; n   for (long long i = len; i; --i) n     if (a[i] == 4 || a[i] == 7) { n       if (last - i <= k) return 1; n       last = i; n     } n   return 0; n } n signed main() { n   memset(dp, -1, sizeof dp); n   long long T; n   scanf( %lld%lld , &T, &k); n   while (T--) { n     scanf( %s , s); n     len = strlen(s); n     for (long long i = 0; i < len; ++i) a[len - i] = s[i] ^ 48; n     long long ans = (dfs(len, 0, 0, 1) - check() + mo) % mo; n     scanf( %s , s); n     len = strlen(s); n     for (long long i = 0; i < len; ++i) a[len - i] = s[i] ^ 48; n     ans = (dfs(len, 0, 0, 1) - ans + mo) % mo; n     printf( %lld n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n template <class Class> n void Print(Class instance) { n   std::cout << instance << std::endl; n } n void Print() { std::cout << std::endl; } n int main() { n   std::ios_base::sync_with_stdio(false); n   size_t n; n   std::cin >> n; n   std::vector<int> time(n + 1); n   std::vector<int> paid(n + 1); n   size_t hour_pointer = 0; n   size_t day_pointer = 0; n   int hour_paid = 0; n   int day_paid = 0; n   for (size_t index = 1; index < n + 1; ++index) { n     std::cin >> time[index]; n     while (time[hour_pointer] + 90 <= time[index]) { n       hour_paid -= paid[hour_pointer]; n       ++hour_pointer; n     } n     while (time[day_pointer] + 1440 <= time[index]) { n       day_paid -= paid[day_pointer]; n       ++day_pointer; n     } n     paid[index] = std::min({20, 50 - hour_paid, 120 - day_paid}); n     hour_paid += paid[index]; n     day_paid += paid[index]; n     Print(paid[index]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n multiset<int> s1; n struct node { n   char s; n   int a1; n } p1[int(1e6 + 50)]; n char s[50]; n int main() { n   int n, a; n   while (~scanf( %d , &n)) { n     int cnt = 0; n     s1.clear(); n     for (int i = 0; i < n; i++) { n       scanf( %s , s); n       if (s[0] !=  r ) { n         scanf( %d , &a); n         if (s[0] ==  i ) { n           s1.insert(a); n           p1[cnt].s =  i ; n           p1[cnt++].a1 = a; n         } else if (s[0] ==  g ) { n           while (!s1.empty() && (*s1.begin()) < a) { n             s1.erase(s1.begin()); n             p1[cnt++].s =  r ; n           } n           if (s1.empty() || (!s1.empty() && (*s1.begin()) > a)) { n             s1.insert(a); n             p1[cnt].s =  i ; n             p1[cnt++].a1 = a; n           } n           p1[cnt].s =  g ; n           p1[cnt++].a1 = a; n         } n       } else { n         if (!s1.empty()) { n           s1.erase(s1.begin()); n         } else { n           p1[cnt].s =  i ; n           p1[cnt++].a1 = 0; n         } n         p1[cnt++].s =  r ; n       } n     } n     printf( %d n , cnt); n     for (int i = 0; i < cnt; i++) { n       if (p1[i].s ==  i ) { n         printf( insert %d n , p1[i].a1); n       } else if (p1[i].s ==  g ) { n         printf( getMin %d n , p1[i].a1); n       } else if (p1[i].s ==  r ) { n         printf( removeMin n ); n       } n     } n     puts(  ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n using vi = vector<int>; n using vll = vector<long long>; n using vvll = vector<vll>; n using vpii = vector<pair<int, int> >; n using vpll = vector<pair<long long, long long> >; n using vld = vector<long double>; n using vb = vector<bool>; n const int inf = (int)1e9 + 7; n const long long llinf = (long long)1e18 + 7; n void solve() { n   long long n; n   cin >> n; n   vll a[n]; n   for (long long i = 0; i < n; i++) { n     long long ki; n     cin >> ki; n     for (long long j = 0; j < ki; j++) { n       long long x; n       cin >> x; n       a[i].push_back(x); n     } n     sort(a[i].begin(), a[i].end()); n   } n   long long ans = 0; n   for (long long i = 0; i < n; i++) { n     long long l, r; n     l = (i - 1 + n) % n; n     r = (i + 1 + n) % n; n     for (long long j = 1; j <= (a[i].size()) - 1; j++) { n       ans += (upper_bound(a[r].begin(), a[r].end(), a[i][j]) - n                   upper_bound(a[r].begin(), a[r].end(), a[i][j - 1]) != n               upper_bound(a[l].begin(), a[l].end(), a[i][j]) - n                   upper_bound(a[l].begin(), a[l].end(), a[i][j - 1])); n     } n   } n   cout << ans <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int ttt = 1; n   while (ttt--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   vector<int> yarr(51, 0); n   vector<int> xarr(51, 0); n   int q = 4 * n + 1; n   vector<pair<int, int>> trav; n   for (int i = 0; i < q; i++) { n     int x, y; n     cin >> x >> y; n     trav.push_back(make_pair(x, y)); n     xarr[x]++; n     yarr[y]++; n   } n   int maxy = 0; n   int maxx = 0; n   int minx = 1000, miny = 1000; n   int indexx = 0, indexy = 0; n   for (int i = 0; i < 51; i++) { n     if (yarr[i] >= n && i > maxy) { n       maxy = i; n     } n     if (xarr[i] >= n && i > maxx) { n       maxx = i; n     } n     if (yarr[i] >= n && i < miny) { n       miny = i; n     } n     if (xarr[i] >= n && i < minx) { n       minx = i; n     } n   } n   for (int i = 0; i < trav.size(); i++) { n     if (trav[i].first == minx && trav[i].second >= miny && n         trav[i].second <= maxy) n       continue; n     if (trav[i].first == maxx && trav[i].second >= miny && n         trav[i].second <= maxy) n       continue; n     if (trav[i].second == miny && trav[i].first >= minx && n         trav[i].first <= maxx) n       continue; n     if (trav[i].second == maxy && trav[i].first >= minx && n         trav[i].first <= maxx) n       continue; n     cout << trav[i].first <<     << trav[i].second <<   n ; n     break; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long s[10], m[10]; n const long long md = 1e9 + 7; n string str; n int n; n vector<int> v[100010]; n int main() { n   for (int i = 0; i <= 9; i++) { n     m[i] = 10; n     s[i] = i; n   } n   cin >> str; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     string st; n     cin >> st; n     v[i].push_back(st[0] -  0 ); n     for (int j = 3; j < st.size(); j++) v[i].push_back(st[j] -  0 ); n   } n   for (int i = n; i >= 1; i--) { n     int cur = v[i][0]; n     long long mm = 1, ss = 0; n     for (int j = 1; j < v[i].size(); j++) { n       ss = (ss * m[v[i][j]]) % md; n       mm = (mm * m[v[i][j]]) % md; n       ss += s[v[i][j]]; n       if (ss >= md) ss -= md; n     } n     s[cur] = ss; n     m[cur] = mm; n   } n   long long ans = 0; n   for (int i = 0; i < str.size(); i++) { n     ans = (ans * m[str[i] -  0 ]) % md; n     ans += s[str[i] -  0 ]; n     if (ans >= md) ans -= md; n   } n   printf( %lld , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } n long long lcm(long long a, long long b) { return a * (b / gcd(a, b)); } n const int MAXN = 600010; n set<int> g[MAXN]; n int n, t, m, k; n int visited[MAXN]; n void dfs(int i) { n   visited[i] = 1; n   for (auto x : g[i]) n     if (!visited[x]) dfs(x); n   return; n } n int main() { n   cin >> n; n   pair<int, int> a[n]; n   for (long long i = 0; i < n; i++) cin >> a[i].first >> a[i].second; n   sort(a, a + n); n   int res = 0; n   set<pair<int, pair<int, int> > > s; n   for (long long i = 0; i < n; i++) { n     auto x = n         s.lower_bound({a[i].first, {-((long long)1e15), -((long long)1e15)}}); n     while (x != s.begin()) { n       s.erase(s.begin()); n     } n     auto it = n         s.upper_bound({a[i].second, {-((long long)1e15), -((long long)1e15)}}); n     x = s.begin(); n     while (x != it) { n       g[i].insert(x->second.second); n       g[x->second.second].insert(i); n       res++; n       if (res >= n) { n         cout <<  NO  n              <<   n ; n         return 0; n       } n       x++; n     } n     s.insert({a[i].second, {a[i].first, i}}); n   } n   dfs(0); n   for (long long i = 0; i < n; i++) { n     if (!visited[i]) { n       cout <<  NO ; n       return 0; n     } n   } n   if (res == n - 1) n     cout <<  YES ; n   else n     cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int a, b, c; n vector<pair<int, int> > v[200005], e[200005]; n int p[200005]; n int fin(int x) { n   if (p[x] != x) p[x] = fin(p[x]); n   return p[x]; n } n void onion(int x, int y) { n   if (fin(x) == fin(y)) return; n   p[fin(y)] = fin(x); n } n priority_queue<pair<long long, pair<int, int> > > q; n int lca[200005][25], maxi[200005][25]; n int depth[200005]; n long long sum = 0; n long long cen[200005]; n void dfs(int node, int prev) { n   for (auto it : e[node]) { n     if (it.first == prev) continue; n     depth[it.first] = depth[node] + 1; n     lca[it.first][0] = node; n     maxi[it.first][0] = it.second; n     dfs(it.first, node); n   } n } n int plz(int x, int y) { n   if (depth[x] < depth[y]) swap(x, y); n   int res = 0; n   for (int i = 20; i >= 0; i--) { n     if (depth[x] - (1 << i) >= depth[y]) { n       res = max(res, maxi[x][i]); n       x = lca[x][i]; n     } n   } n   if (x == y) return res; n   for (int i = 20; i >= 0; i--) { n     if (lca[x][i] != lca[y][i]) { n       res = max(res, maxi[x][i]); n       res = max(res, maxi[y][i]); n       x = lca[x][i]; n       y = lca[y][i]; n     } n   } n   res = max(res, maxi[x][0]); n   res = max(res, maxi[y][0]); n   return res; n } n pair<int, int> rr[200005]; n map<pair<int, int>, int> mapa; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; m >= i; i++) { n     scanf( %d%d%d , &a, &b, &c); n     rr[i] = {a, b}; n     cen[i] = c; n     v[a].push_back({b, c}); n     v[b].push_back({a, c}); n     q.push({-c, {a, b}}); n   } n   for (int i = 1; n >= i; i++) p[i] = i; n   while (!q.empty()) { n     int cost = -q.top().first; n     int u = q.top().second.first; n     int uu = q.top().second.second; n     q.pop(); n     if (fin(u) == fin(uu)) continue; n     onion(u, uu); n     e[u].push_back({uu, cost}); n     e[uu].push_back({u, cost}); n     mapa[{u, uu}] = 1; n     sum += cost; n   } n   dfs(1, -1); n   for (int j = 1; 20 >= j; j++) n     for (int i = 1; n >= i; i++) { n       lca[i][j] = lca[lca[i][j - 1]][j - 1]; n       maxi[i][j] = max(maxi[i][j - 1], maxi[lca[i][j - 1]][j - 1]); n     } n   int qq; n   qq = m; n   for (int i = 1; m >= i; i++) { n     int a = rr[i].first; n     int b = rr[i].second; n     if (mapa[{a, b}] == 1) { n       printf( %lld n , sum); n       continue; n     } n     long long wynik = 1LL * sum + cen[i] - plz(a, b); n     printf( %lld n , wynik); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 3010; n const int INF = 0x3f3f3f3f; n int d[MAXN], e[MAXN]; n int main() { n   memset(d, INF, sizeof d); n   memset(e, INF, sizeof e); n   int n; n   cin >> n; n   string s; n   cin >> s; n   int at = INF; n   for (int i = 0; i < n; i++) { n     if (s[i] ==  R ) n       at = 0; n     else if (s[i] ==  L ) n       at = INF; n     d[i] = at; n     if (at < INF) at++; n   } n   at = INF; n   for (int i = n - 1; i >= 0; i--) { n     if (s[i] ==  R ) n       at = INF; n     else if (s[i] ==  L ) n       at = 0; n     e[i] = at; n     if (at < INF) at++; n   } n   int cont = 0; n   for (int i = 0; i < n; i++) { n     if (d[i] == e[i]) cont++; n   } n   cout << cont << endl; n } n


#include <bits/stdc++.h> n using namespace std; n inline void Rd(int &res) { n   char c; n   res = 0; n   while (c = getchar(), c <  0 ) n     ; n   do { n     res = (res << 1) + (res << 3) + (c ^ 48); n   } while (c = getchar(), c >=  0 ); n } n const int S = 200; n long long n, s, ans; n int work(long long x) { n   int rs = 0; n   while (x) { n     rs += x % 10; n     x /= 10; n   } n   return rs; n } n int main() { n   cin >> n >> s; n   for (int i = 1; i <= n && i <= S; i++) { n     int sum = work(i); n     if (abs(sum - i) >= s) ans++; n   } n   if (n > S) { n     ans += max((long long)0, n - (s + S) + 1); n     if (s < S) { n       for (long long i = S + 1; i < s + S && i <= n; i++) { n         int sum = work(i); n         if (abs(sum - i) >= s) ans++; n       } n     } else { n       for (long long i = s; i < s + S && i <= n; i++) { n         int sum = work(i); n         if (abs(sum - i) >= s) ans++; n       } n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long a, b, c, i, fuck = 0; n   scanf( %lld , &a); n   scanf( %lld , &b); n   scanf( %lld , &c); n   for (i = a; i >= 1; i--) { n     if ((2 * i) <= b && (4 * i) <= c) { n       fuck = 1; n       break; n     } n   } n   if (fuck == 0) n     printf( 0 n ); n   else n     printf( %lld  , i + 2 * i + 4 * i); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n unsigned long long pw[1020304], iv[1020304]; n unsigned long long h[1020304], f[1020304]; n int check(int l, int r) { n   unsigned long long p1 = (h[r] + 1000000007 - n                            h[l - 1] * pw[r - l + 1] % 1000000007) % n                           1000000007, n                      p2 = (f[r] + 1000000007 - f[l - 1]) * iv[l - 1] % n                           1000000007; n   return p1 == p2; n } n int n, d; n char s[1020304]; n bool dfs(int i, bool w) { n   if (i == 4) { n     int z = i; n   } n   if (i > n && w) cout << s + 1 << endl; n   if (i > n) return w; n   char t = s[i]; n   for (char c = (w) ?  a  : s[i]; c <=  z ; c++) { n     if (i >= d) n       if (check(i - d + 2, i - 1) && s[i - d + 1] == c) continue; n     if (i > d) n       if (check(i - d + 1, i - 1) && s[i - d] == c) continue; n     h[i] = (h[i - 1] * 10007 + c) % 1000000007; n     f[i] = (f[i - 1] + pw[i - 1] * c) % 1000000007; n     s[i] = c; n     if (dfs(i + 1, c > t || w)) return true; n   } n   s[i] = t; n   return false; n } n int main() { n   unsigned long long t = 1; n   pw[0] = iv[0] = 1; n   for (int i = 1; i <= 400000; ++i) n     pw[i] = pw[i - 1] * 10007 % 1000000007, n     iv[i] = iv[i - 1] * 820925358 % 1000000007; n   cin >> d; n   scanf( %s , s + 1); n   n = strlen(s + 1); n   if (d == 1) { n     puts( Impossible ); n     return 0; n   } n   if (!dfs(1, 0)) { n     puts( Impossible ); n     return 0; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 150000; n const int L = 19; n vector<int> edges[N]; n long long x[N], y[N]; n int anc[N][L], tin[N], tout[N], s[N], moment; n void dfs(int x, int last) { n   tin[x] = ++moment; n   anc[x][0] = last; n   for (int i = 1; i < L; i++) anc[x][i] = anc[anc[x][i - 1]][i - 1]; n   for (int i = 0; i < edges[x].size(); i++) n     if (edges[x][i] != last) dfs(edges[x][i], x); n   tout[x] = ++moment; n } n long long coef(int i, int j, int k) { n   long long a = y[i] - y[j], b = x[j] - x[i], c = x[i] * y[j] - x[j] * y[i]; n   return (a * x[k] + b * y[k] + c); n } n bool father(int x, int y) { return (tin[x] <= tin[y] && tout[y] <= tout[x]); } n int lca(int a, int b) { n   if (father(a, b)) return a; n   if (father(b, a)) return b; n   for (int i = L - 1; i >= 0; i--) n     if (!father(anc[a][i], b)) a = anc[a][i]; n   return anc[a][0]; n } n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %I64d %I64d , &x[i], &y[i]); n   int cnt = 1; n   s[cnt] = n; n   for (int i = n - 1; i >= 1; i--) { n     s[++cnt] = i; n     while (cnt > 2 && coef(s[cnt], s[cnt - 2], s[cnt - 1]) < 0) n       swap(s[cnt - 1], s[cnt]), cnt--; n     edges[s[cnt]].push_back(s[cnt - 1]); n     edges[s[cnt - 1]].push_back(s[cnt]); n   } n   moment = 0, dfs(n, n); n   int quests; n   scanf( %d , &quests); n   for (int i = 1; i <= quests; i++) { n     int x, y; n     scanf( %d %d , &x, &y); n     printf( %d  , lca(x, y)); n   } n   return 0; n } n