#include <bits/stdc++.h> n using namespace std; n vector<int> v[300005]; n int n, m, s; n int edgeck[300005], Etype[300005]; n pair<int, int> E[300005]; n int vis[300005]; n void bfs() { n   memset(vis, 0, sizeof(vis)); n   queue<int> que; n   que.push(s); n   while (!que.empty()) { n     int now = que.front(); n     que.pop(); n     vis[now] = 1; n     for (int i = 0; i < v[now].size(); i++) { n       int Enum = v[now][i]; n       int nxt = -1; n       if (edgeck[Enum] == -1) { n         nxt = E[Enum].second; n         if (nxt != now) { n           edgeck[Enum] = 0; n         } else { n           nxt = E[Enum].first; n           edgeck[Enum] = 1; n         } n         if (!vis[nxt]) { n           vis[nxt] = 1; n           que.push(nxt); n         } n       } else { n         nxt = (edgeck[Enum] == 0 ? E[Enum].second : E[Enum].first); n         if (vis[nxt]) continue; n         vis[nxt] = 1; n         que.push(nxt); n       } n     } n   } n   for (int i = 0; i < m; i++) { n     if (edgeck[i] == -1) edgeck[i] = 0; n   } n   int cnt = 0; n   for (int i = 1; i <= n; i++) { n     if (vis[i]) ++cnt; n   } n   printf( %d n , cnt); n   for (int i = 0; i < m; i++) { n     if (Etype[i] != 2) continue; n     if (edgeck[i] == 0) n       printf( + ); n     else n       printf( - ); n   } n   puts(  ); n } n void bfs2() { n   memset(vis, 0, sizeof(vis)); n   queue<int> que; n   que.push(s); n   while (!que.empty()) { n     int now = que.front(); n     que.pop(); n     vis[now] = 1; n     for (int i = 0; i < v[now].size(); i++) { n       int Enum = v[now][i]; n       int nxt = -1; n       if (edgeck[Enum] == -1) { n         nxt = E[Enum].second; n         if (nxt != now) { n           edgeck[Enum] = 1; n         } else { n           nxt = E[Enum].first; n           edgeck[Enum] = 0; n         } n       } else { n         nxt = (edgeck[Enum] == 0 ? E[Enum].second : E[Enum].first); n         if (vis[nxt]) continue; n         vis[nxt] = 1; n         que.push(nxt); n       } n     } n   } n   for (int i = 0; i < m; i++) { n     if (edgeck[i] == -1) edgeck[i] = 0; n   } n   int cnt = 0; n   for (int i = 1; i <= n; i++) { n     if (vis[i]) ++cnt; n   } n   printf( %d n , cnt); n   for (int i = 0; i < m; i++) { n     if (Etype[i] != 2) continue; n     if (edgeck[i] == 0) n       printf( + ); n     else n       printf( - ); n   } n   puts(  ); n } n int main() { n   memset(edgeck, -1, sizeof(edgeck)); n   scanf( %d %d %d , &n, &m, &s); n   for (int ty, a, b, i = 0; i < m; i++) { n     scanf( %d %d %d , &ty, &a, &b); n     v[a].push_back(i); n     v[b].push_back(i); n     if (ty == 1) { n       edgeck[i] = 0; n     } else { n       Etype[i] = 2; n     } n     E[i] = {a, b}; n   } n   bfs(); n   for (int i = 0; i < m; i++) { n     if (Etype[i] != 2) continue; n     edgeck[i] = -1; n   } n   bfs2(); n } n


#include<bits/stdc++.h> n using namespace std; n  n #define INF 1234567890 n #define ll long long n  n int T; n int N, K; n int A[200201]; n  n int main() n { n  //ios::sync_with_stdio(0); cin.tie(0); n  scanf( %d , &T); n  while(T--) n  { n   scanf( %d %d , &N, &K); n   for(int i=1; i<=N; i++) n   { n    if (i <= K) A[i] = i; n    else A[i] = K-(i-K); n   } n   // for(int i=1; i<=N; i++) n   //  printf( %d  , A[i]); n   // printf(  n ); n  n   int cnt = A[N]-1; // prefix n   for(int i=1; i<=cnt; i++) n    printf( %d  , i); n   for(int i=cnt+1; i<=K; i++) n    printf( %d  , cnt+1+K-i); n   printf(  n ); n  } n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n const int inf = 1e9 + 5; n const long long linf = 1e18 + 5; n const int N = 3000 + 5; n int n, x, y, all, up[N], down[N], w[N]; n vector<pair<int, int> > v[N]; n void dfs(int root, int x, int w, int up, int down) { n   ::up[x] = up; n   ::down[x] = down; n   ::w[x] = w; n   for (__typeof(v[x].begin()) it = v[x].begin(); it != v[x].end(); it++) n     if (it->first != root) { n       all += it->second; n       dfs(x, it->first, w, up + it->second, down + !it->second); n     } n } n int solve(int x) { n   all = 0; n   w[x] = 0; n   up[x] = 0; n   down[x] = 0; n   for (__typeof(v[x].begin()) it = v[x].begin(); it != v[x].end(); it++) { n     all += it->second; n     dfs(x, it->first, it->first, it->second, !it->second); n   } n   int ans = all, mni = -1; n   for (int i = 1; i <= n; i++) { n     if (all - up[i] + down[i] < ans) { n       ans = all - up[i] + down[i]; n       mni = w[i]; n     } n   } n   int ans2 = ans; n   for (int i = 1; i <= n; i++) { n     if (w[i] != mni and ans - up[i] + down[i] < ans2) { n       ans2 = ans - up[i] + down[i]; n     } n   } n   return ans2; n } n int main() { n   ios ::sync_with_stdio(0); n   cin >> n; n   for (int i = 1; i <= n - 1; i++) { n     cin >> x >> y; n     v[x].push_back(make_pair(y, 0)); n     v[y].push_back(make_pair(x, 1)); n   } n   int ans = inf; n   for (int i = 1; i <= n; i++) ans = min(ans, solve(i)); n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, temp; n   cin >> n; n   vector<int> num; n   while (n != 0) { n     cin >> temp; n     num.push_back(temp); n     n--; n   } n   sort(num.begin(), num.end()); n   int smallest = 1; n   for (int i = 0; i < num.size(); i++) { n     if (smallest != num[i]) { n       break; n     } n     smallest++; n   } n   cout << smallest; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[300001]; n int m, n, res = 1, x; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cin >> n; n   m = n; n   printf( 1  ); n   for (int i = 1; i <= n; ++i) { n     cin >> x; n     a[x] = 1; n     res++; n     while (a[m] == 1 && m > 0) { n       res--; n       m--; n     } n     printf( %d  , res); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(int argc, char const *argv[]) { n   int n, a, b, ans = 0; n   cin >> n >> a >> b; n   if (b >= 0) { n     if (b >= n) { n       ans = b % n; n       ans += a; n     } else n       ans = a + b; n     if (ans > n) ans %= n; n   } else if (n == -b) { n     ans = a; n   } else { n     if (b == -1) { n       a += b; n       if (a == 0) n         ans = n; n       else n         ans = a; n     } else { n       int k = 0; n       while (k != b) { n         a--; n         k--; n         if (a == 0) a = n; n       } n       if (a < 0) n         ans = -a; n       else n         ans = a; n     } n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int cycles; n vector<vector<int> > g; n vector<int> vis; n void dfs(int u) { n   vis[u] = 1; n   for (auto v : g[u]) { n     if (vis[v]) { n       if (vis[v] == 1) cycles++; n       continue; n     } n     dfs(v); n   } n   vis[u] = 2; n } n long long gcd(long long a, long long b) { n   long long x = max(a, b); n   long long y = min(a, b); n   if (y == 0) return x; n   if (x % y == 0) return y; n   return gcd(y, x % y); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t; n   cin >> t; n   while (t--) { n     string s; n     cin >> s; n     int l = s.length(); n     int d = s[0] -  0 ; n     int ans = (d - 1) * (4 + 3 + 2 + 1); n     for (int i = 1; i <= l; i++) ans += i; n     cout << ans <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long N = 1e5 + 10; n vector<vector<long long> > v1(N, vector<long long>(2)); n vector<vector<long long> > v; n long long dfs(long long s, long long n, vector<long long>& level) { n   long long val = 0; n   for (auto u : v[s]) { n     if (level[u] == 0) { n       val = max(val, dfs(u, n, level)); n     } else if (level[u]) { n       val = max(val, level[u]); n     } n   } n   level[s] = val + 1; n   return level[s]; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long n, m; n   cin >> n >> m; n   for (long long i = 1; i <= m; i++) { n     cin >> v1[i][0] >> v1[i][1]; n   } n   long long low = 1, high = m, ans = -1; n   while (low <= high) { n     long long mid = (low + high) / 2; n     v.clear(); n     v.resize(n + 1); n     vector<long long> par(n + 1, -1), level(n + 1); n     for (long long i = 1; i <= mid; i++) { n       v[v1[i][0]].push_back(v1[i][1]); n       par[v1[i][1]] = v1[i][0]; n     } n     for (long long i = 1; i <= n; i++) { n       if (par[i] == -1) { n         if (dfs(i, n, level) == n) { n           ans = mid; n           high = mid - 1; n         } else { n           low = mid + 1; n         } n         break; n       } n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 7; n int n, m, k, s, t; n vector<int> g[N]; n int dis[N]; n bool spc[N]; n bool ok(int lim) { n   priority_queue<pair<int, int> > stk; n   fill_n(dis, n, INT_MAX); n   dis[s] = 0; n   stk.push({-dis[s], s}); n   while (!stk.empty()) { n     int d, u; n     tie(d, u) = stk.top(); n     stk.pop(); n     if (d != -dis[u] || -d >= lim) continue; n     for (auto v : g[u]) { n       if (dis[v] > dis[u] + 1) { n         dis[v] = dis[u] + 1; n         if (spc[v]) dis[v] = 0; n         stk.push({-dis[v], v}); n       } n     } n   } n   return dis[t] < INT_MAX; n } n int main() { n   scanf( %d%d%d , &n, &m, &k); n   for (int i = (0); i < (k); ++i) { n     int p; n     scanf( %d , &p); n     p--; n     spc[p] = true; n   } n   for (int i = (0); i < (m); ++i) { n     int a, b; n     scanf( %d%d , &a, &b); n     a--; n     b--; n     g[a].push_back(b); n     g[b].push_back(a); n   } n   scanf( %d%d , &s, &t); n   s--; n   t--; n   int ans = -1; n   for (int l = 1, r = n, mid = l + r >> 1; l <= r; mid = l + r >> 1) n     ok(mid) ? r = (ans = mid) - 1 : l = mid + 1; n   printf( %d , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   string s; n   cin >> s; n   int a[7]; n   for (int i = 0; i < 6; i++) { n     a[i] = s[i] -  0 ; n   } n   int ans = 3; n   for (int i = 0; i <= 9; i++) { n     for (int j = 0; j <= 9; j++) { n       for (int k = 0; k <= 9; k++) { n         for (int x = 0; x <= 9; x++) { n           for (int y = 0; y <= 9; y++) { n             for (int z = 0; z <= 9; z++) { n               if (i + j + k == x + y + z) { n                 int res = 0; n                 if (i != a[0]) res++; n                 if (j != a[1]) res++; n                 if (k != a[2]) res++; n                 if (x != a[3]) res++; n                 if (y != a[4]) res++; n                 if (z != a[5]) res++; n                 ans = min(ans, res); n               } n             } n           } n         } n       } n     } n   } n   for (int i = 0; i <= 9; i++) { n     for (int j = 0; j <= 9; j++) { n       for (int k = 0; k <= 9; k++) { n         if (i + j + k == a[3] + a[4] + a[5]) { n           int res = 0; n           if (i != a[0]) res++; n           if (j != a[1]) res++; n           if (k != a[2]) res++; n           ans = min(ans, res); n         } n       } n     } n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100 + 10; n int a[N]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n + n; i++) scanf( %d , &a[i]); n   sort(a, a + n + n); n   if (a[n] > a[n - 1]) n     puts( YES ); n   else n     puts( NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n, k; n   cin >> n >> k; n   vector<vector<int>> g(n); n   for (int i = 0; i < n - 1; ++i) { n     int x, y; n     cin >> x >> y; n     --x; n     --y; n     g[x].push_back(y); n     g[y].push_back(x); n   } n   int ans = 1; n   function<int(int, int)> dfs = [&](int v, int pv) { n     if ((int)g[v].size() == 1) { n       return 0; n     } n     vector<int> d; n     for (int to : g[v]) { n       if (to == pv) { n         continue; n       } n       d.push_back(1 + dfs(to, v)); n     } n     sort(d.begin(), d.end()); n     while ((int)d.size() >= 2) { n       if (d.back() + d[(int)d.size() - 2] <= k) { n         break; n       } n       ++ans; n       d.pop_back(); n     } n     return d.back(); n   }; n   for (int i = 0; i < n; ++i) { n     if ((int)g[i].size() > 1) { n       dfs(i, -1); n       break; n     } n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char s[2000]; n int n, len, ans; n int dpmin[120][55][2], dpmax[120][55][2]; n bool u[120][55][2]; n int inf = 10005000; n bool go(int i, int n, int d) { n   if (i == len) { n     if (n % 2 == 0) return 1; n     return 0; n   } n   if (u[i][n][d]) return 1; n   u[i][n][d] = 1; n   dpmin[i][n][d] = inf; n   dpmax[i][n][d] = -inf; n   if (s[i] ==  F ) { n     if (go(i + 1, n, d)) { n       dpmin[i][n][d] = min(dpmin[i][n][d], (d ? -1 : 1) + dpmin[i + 1][n][d]); n       dpmax[i][n][d] = max(dpmax[i][n][d], (d ? -1 : 1) + dpmax[i + 1][n][d]); n     } n     if (n > 0 && go(i + 1, n - 1, d ^ 1)) { n       dpmin[i][n][d] = min(dpmin[i][n][d], dpmin[i + 1][n - 1][d ^ 1]); n       dpmax[i][n][d] = max(dpmax[i][n][d], dpmax[i + 1][n - 1][d ^ 1]); n     } n   } else { n     if (n > 0 && go(i + 1, n - 1, d)) { n       dpmin[i][n][d] = n           min(dpmin[i][n][d], (d ? -1 : 1) + dpmin[i + 1][n - 1][d]); n       dpmax[i][n][d] = n           max(dpmax[i][n][d], (d ? -1 : 1) + dpmax[i + 1][n - 1][d]); n     } n     if (go(i + 1, n, d ^ 1)) { n       dpmin[i][n][d] = min(dpmin[i][n][d], dpmin[i + 1][n][d ^ 1]); n       dpmax[i][n][d] = max(dpmax[i][n][d], dpmax[i + 1][n][d ^ 1]); n     } n   } n   return 1; n } n int main() { n   scanf( %s%d , s, &n); n   len = strlen(s); n   go(0, n, 0); n   ans = max(-dpmin[0][n][0], dpmax[0][n][0]); n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 1e09 + 5e3; n const long long linf = 2e18 + 5e3; n const int mxn = 5e2 + 5; n void solve() { n   int n; n   cin >> n; n   vector<int> a(n), b(n); n   vector<pair<int, int> > as, bs; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   for (int i = 0; i < n; i++) { n     cin >> b[i]; n   } n   long long out = 1; n   if (n & 1) out = a[n / 2] == b[n / 2]; n   for (int i = 0, j = n - 1; i < n / 2; i++, j--) { n     as.emplace_back(min(a[i], a[j]), max(a[i], a[j])); n     bs.emplace_back(min(b[i], b[j]), max(b[i], b[j])); n   } n   sort((as).begin(), (as).end()); n   sort((bs).begin(), (bs).end()); n   cout << (out && as == bs ?  Yes  :  No ) <<   n ; n } n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   int t; n   cin >> t; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long n; n long long a[100005]; n long long preA[100005]; n int main() { n   cin >> n; n   for (long long i = 0; i < n; ++i) { n     cin >> a[i]; n   } n   preA[0] = 0; n   for (long long i = 1; i < n; ++i) { n     preA[i] = preA[i - 1] + a[i]; n   } n   set<long long> nos; n   map<long long, long long> freq; n   for (long long i = 0; i < n; ++i) { n     if (nos.find(preA[i]) != nos.end()) { n       freq[preA[i]]++; n     } else { n       nos.insert(preA[i]); n       freq[preA[i]] = 1; n     } n   } n   long long maxFreq = -1; n   for (set<long long>::iterator it = nos.begin(); it != nos.end(); ++it) { n     if (maxFreq < freq[*it]) { n       maxFreq = freq[*it]; n     } n   } n   cout << n - maxFreq; n } n


#include <bits/stdc++.h> n int n, a[100001], i, d = 0, s = 0; n int main() { n   scanf( %d , &n); n   for (i = 0; i < n; i++) scanf( %d , &a[i]); n   std::sort(a, a + n); n   for (i = 0; i < n; i++) n     if (a[i] >= s) { n       d++; n       s += a[i]; n     } n   printf( %d , d); n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1001001001; n const long long INFL = 1001001001001001001LL; n template <typename T> n void pv(T a, T b) { n   for (T i = a; i != b; ++i) cout << *i <<    ; n   cout << endl; n } n template <class T> n bool uin(T &a, T b) { n   return a > b ? (a = b, true) : false; n } n template <class T> n bool uax(T &a, T b) { n   return a < b ? (a = b, true) : false; n } n int in() { n   int x; n   cin >> x; n   return x; n } n double fin() { n   double x; n   cin >> x; n   return x; n } n string sin() { n   string x; n   cin >> x; n   return x; n } n char chin() { n   char x; n   cin >> x; n   return x; n } n long long lin() { n   long long x; n   cin >> x; n   return x; n } n struct card { n   int a, b, c; n }; n bool comp(card &a, card &b) { return a.a > b.a; } n int n, p, q, r; n long long sum[500011]; n vector<pair<int, int> > bounds; n vector<card> cards; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.setf(ios::fixed); n   cout.precision(20); n   cin >> n >> p >> q >> r; n   cards.resize(n); n   for (int i = 0; i < (int)(n); ++i) { n     cin >> cards[i].a >> cards[i].b >> cards[i].c; n     bounds.push_back({cards[i].b, cards[i].c}); n   } n   sort((cards).begin(), (cards).end(), comp); n   sort((bounds).rbegin(), (bounds).rend()); n   int ind = -1; n   for (int i = 0; i < bounds.size(); i++) { n     if (ind == -1 || bounds[ind].second < bounds[i].second) { n       bounds[++ind] = bounds[i]; n     } n   } n   bounds.resize(ind + 1); n   bounds.push_back({0, r}); n   reverse((bounds).begin(), (bounds).end()); n   bounds.push_back({q, 0}); n   for (int i = 1; i < bounds.size(); i++) { n     sum[i] = sum[i - 1] + (long long)(bounds[i - 1].second - bounds[i].second) * n                               (q - bounds[i - 1].first); n   } n   int bb = p; n   int minb = 0, minc = 0; n   int pbnd = 0, pcnd = bounds.size() - 1; n   long long sol = 0; n   cards.push_back({0, 0, 0}); n   for (int i = 0; i <= n; i++) { n     if (bb > cards[i].a) { n       while (pbnd < bounds.size() && bounds[pbnd].first < minb) pbnd++; n       while (pcnd > 0 && bounds[pcnd].second < minc) pcnd--; n       if (pbnd > pcnd) { n         sol += (long long)(bb - cards[i].a) * (q - minb) * (r - minc); n       } else { n         sol += (long long)(bb - cards[i].a) * n                (sum[pcnd] - sum[pbnd] + n                 (long long)(bounds[pcnd].second - minc) * n                     (q - bounds[pcnd].first) + n                 (long long)(q - minb) * (r - bounds[pbnd].second)); n       } n       bb = cards[i].a; n     } n     uax(minb, cards[i].b); n     uax(minc, cards[i].c); n   } n   cout << sol << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool bSearch(int* toys, int item, int from, int to) { n   if (from > to) n     return false; n   else { n     int midPoint = (from + to) / 2; n     if (item < toys[midPoint]) n       return bSearch(toys, item, from, midPoint - 1); n     else if (item == toys[midPoint]) n       return true; n     else n       return bSearch(toys, item, midPoint + 1, to); n   } n } n int main() { n   int n, m; n   scanf( %d %d , &n, &m); n   int toys[n]; n   for (int i = 0; i < n; i++) { n     scanf( %d , &toys[i]); n   } n   sort(toys, toys + n); n   long long int counter = 0; n   vector<int> v; n   for (int i = 1; i <= 1000000000 && i <= m && counter < m; i++) { n     if (!bSearch(toys, i, 0, n - 1)) { n       counter += i; n       if (counter <= m) v.push_back(i); n     } n   } n   printf( %d n , v.size()); n   for (int i = 0; i < v.size(); i++) { n     printf( %d  , v[i]); n   } n   printf(  n ); n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-9; n int a, b, n, p; n int main() { n   cin >> a >> b >> n; n   for (int x = -1000; x < 1001; x++) { n     p = 1; n     for (int i = 0; i < n && p <= 1000; i++) p *= x; n     if (a * p == b) { n       cout << x; n       return 0; n     } n   } n   cout <<  No solution ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   for (int j1 = 0; j1 < t; j1++) { n     long long int n; n     cin >> n; n     long long int arr[n]; n     long long int sum1 = 0; n     long long int sum2 = 0; n     for (int j = 0; j < (n / 2); j++) { n       arr[j] = ((2 * j) + 2); n       sum1 += arr[j]; n     } n     long long int p = 1; n     for (int k = n / 2; k < n - 1; k++) { n       arr[k] = (2 * p - 1); n       p++; n       sum2 += (arr[k]); n     } n     arr[n - 1] = sum1 - sum2; n     if (n % 4 == 0) { n       cout <<  YES  << endl; n       for (int m = 0; m < n; m++) { n         cout << arr[m] <<    ; n       } n       cout << endl; n     } else { n       cout <<  NO  << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double pi = 2 * acos(0); n void _R(int &x) { scanf( %d , &x); } n void _R(int64_t &x) { scanf( %lld , &x); } n void _R(double &x) { scanf( %lf , &x); } n void _R(char &x) { scanf(  %c , &x); } n void _R(char *x) { scanf( %s , x); } n int main() { n   long long x, n, s = 0, a, i; n   cin >> n >> x; n   for (i = 1; i <= n; i++) { n     if (x % i == 0 && x / i <= n) { n       s++; n     } n   } n   cout << s; n } n


#include <bits/stdc++.h> n using namespace std; n using LL = long long; n constexpr int N = 1e5 + 5; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   int n, k, m; n   cin >> n >> k >> m; n   vector<int> in(k); n   for (int &e : in) cin >> e; n   sort(begin(in), end(in)); n   const int A = accumulate(begin(in), end(in), 0); n   int ans = 0; n   for (int i = 0; i <= n; i++) { n     if (i * A > m) break; n     int points = i * (k + 1); n     int have = m - i * A; n     int g = n - i; n     for (int x : in) { n       int t = min(g, have / x); n       points += t; n       have -= t * x; n     } n     ans = max(ans, points); n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n void splitstr(const string &s, vector<T> &out) { n   istringstream in(s); n   out.clear(); n   copy(istream_iterator<T>(in), istream_iterator<T>(), back_inserter(out)); n } n static void redirect(int argc, const char **argv) { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   if (argc > 1) { n     static filebuf f; n     f.open(argv[1], ios::in); n     cin.rdbuf(&f); n     if (!cin) { n       cerr <<  Failed to open    << argv[1] <<     << endl; n       exit(1); n     } n   } n   if (argc > 2) { n     static filebuf f; n     f.open(argv[2], ios::out | ios::trunc); n     cout.rdbuf(&f); n     if (!cout) { n       cerr <<  Failed to open    << argv[2] <<     << endl; n     } n   } n } n template <typename T> n static T gcd(T a, T b) { n   return b ? gcd(b, a % b) : a; n } n template <typename T> n static T mod(T a, T m) { n   a %= m; n   if (a < 0) a += m; n   return a; n } n template <typename T> n static T inverse(T a, T m) { n   a = mod(a, m); n   if (a <= 1) return a; n   return mod((1 - inverse(m, a) * m) / a, m); n } n template <typename T, typename C, T Modulus> n class MR { n  private: n   struct tag_plus {}; n   struct tag_minus {}; n   struct tag_good {}; n   T value; n   static_assert(std::numeric_limits<C>::max() / Modulus / Modulus > 0, n                  compute type is too small ); n   static_assert(Modulus < std::numeric_limits<T>::max() / 2, n                  storage type is too small ); n   void reduce(tag_plus) { n     if (value >= Modulus) value -= Modulus; n   } n   void reduce(tag_minus) { n     if (value < 0) value += Modulus; n   } n   void reduce(tag_good) {} n  n  public: n   static const T modulus = Modulus; n   MR() : value(0) {} n   MR(C value) : value(value % Modulus) { reduce(tag_minus()); } n   template <typename tag_t> n   MR(T value, tag_t tag) : value(value) { n     reduce(tag); n   } n   MR &operator=(C value) { n     this->value = value % Modulus; n     reduce(tag_minus()); n     return *this; n   } n   MR operator+(MR b) const { return MR(value + b.value, tag_plus()); } n   MR operator-(MR b) const { return MR(value - b.value, tag_minus()); } n   MR operator*(MR b) const { n     return MR(C(value) * C(b.value) % Modulus, tag_good()); n   } n   MR operator-() const { return MR(-value, tag_minus()); } n   MR &operator+=(MR b) { n     value += b.value; n     reduce(tag_plus()); n     return *this; n   } n   MR &operator-=(MR b) { n     value -= b.value; n     reduce(tag_minus()); n     return *this; n   } n   MR &operator*=(MR b) { n     value = C(value) * C(b.value) % Modulus; n     return *this; n   } n   bool operator==(MR b) const { return value == b.value; } n   bool operator!=(MR b) const { return value != b.value; } n   T get() const { return value; } n   MR inverse() const { n     assert(value); n     return MR(::inverse(C(value), C(Modulus)), tag_good()); n   } n   MR operator/(MR b) const { return *this * b.inverse(); } n   MR &operator/=(MR b) { return *this *= b.inverse(); } n }; n template <typename T, typename C, T Modulus> n static inline std::ostream &operator<<(std::ostream &o, MR<T, C, Modulus> mr) { n   return o << mr.get(); n } n int main(int argc, const char **argv) { n   redirect(argc, argv); n   int K, pas, pbs; n   cin >> K >> pas >> pbs; n   MR<int, long long, 1000000007> pa = MR<int, long long, 1000000007>(pas) / n                                       MR<int, long long, 1000000007>(pas + pbs); n   MR<int, long long, 1000000007> pb = MR<int, long long, 1000000007>(pbs) / n                                       MR<int, long long, 1000000007>(pas + pbs); n   vector<vector<MR<int, long long, 1000000007>>> state( n       K + 1, vector<MR<int, long long, 1000000007>>(K + 1)); n   MR<int, long long, 1000000007> scale1 = pa / pb; n   state[1][0] = 1; n   MR<int, long long, 1000000007> ans; n   for (int a = 1; a <= K; a++) n     for (int s = 0; s <= K; s++) { n       if (state[a][s] == 0) continue; n       if (s + a >= K) { n         MR<int, long long, 1000000007> t = s + a; n         ans += (scale1 + t) * state[a][s]; n       } else { n         state[a + 1][s] += pa * state[a][s]; n         state[a][s + a] += pb * state[a][s]; n       } n     } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e9 + 1000; n const long long INF64 = 1e18; n const int N = 1001000; n const int MOD = 1e9 + 7; n const double EPS = 1E-9; n long long gcd(long long a, long long b) { return a == 0 ? b : gcd(b % a, a); } n int add(int first, int second, int m) { n   first += second; n   while (first >= m) first -= m; n   while (first < 0) first += m; n   return first; n } n int sub(int first, int second, int m) { return add(first, -second, m); } n int mul(int first, int second, int m) { return (first * 1ll * second) % m; } n int binpow(int first, int second, int m) { n   int z = 1; n   while (second) { n     if (second & 1) z = mul(z, first, m); n     first = mul(first, first, m); n     second >>= 1; n   } n   return z; n } n int divide(int first, int second, int m) { n   return mul(first, binpow(second, m - 2, m), m); n } n const int K = 2; n mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); n bool is_prime(int first) { n   for (int i = 2; i * 1ll * i <= first; i++) n     if (first % i == 0) return false; n   return true; n } n int gen_prime(int first, int offset) { n   int z = first + (rnd() % offset); n   while (!is_prime(z)) z++; n   return z; n } n array<int, K> P = {10, 10}; n array<int, K> M = {1000000007, 1000000009}; n void precalc() { n   for (int i = 0; i < K; i++) { n     P[i] = gen_prime(100, 100); n     M[i] = gen_prime(1000000000, 1000000); n   } n } n array<int, K> add(array<int, K> first, array<int, K> second) { n   array<int, K> res; n   for (int i = 0; i < K; i++) res[i] = add(first[i], second[i], M[i]); n   return res; n } n array<int, K> sub(array<int, K> first, array<int, K> second) { n   array<int, K> res; n   for (int i = 0; i < K; i++) res[i] = sub(first[i], second[i], M[i]); n   return res; n } n array<int, K> mul(array<int, K> first, array<int, K> second) { n   array<int, K> res; n   for (int i = 0; i < K; i++) res[i] = mul(first[i], second[i], M[i]); n   return res; n } n array<int, K> binpow(array<int, K> first, int second) { n   array<int, K> res; n   for (int i = 0; i < K; i++) res[i] = binpow(first[i], second, M[i]); n   return res; n } n array<int, K> divide(array<int, K> first, array<int, K> second) { n   array<int, K> z; n   for (int i = 0; i < K; i++) z[i] = divide(first[i], second[i], M[i]); n   return z; n } n int code(char c) { return (c -  0 ); } n array<int, K> make_hash(int first) { n   array<int, K> res; n   for (int i = 0; i < K; i++) res[i] = first; n   return res; n } n vector<array<int, K> > get_prefix_hashes(const string& s) { n   int n = s.size(); n   vector<array<int, K> > ans(n + 1); n   array<int, K> curP = make_hash(1); n   for (int i = 0; i < n; i++) { n     ans[i + 1] = add(ans[i], mul(make_hash(code(s[i])), curP)); n     curP = mul(curP, P); n   } n   return ans; n } n array<int, K> DP[N], DQ[N]; n array<int, K> substr_hash(const vector<array<int, K> >& p, int l, int r) { n   return mul(sub(p[r], p[l]), DQ[l]); n } n array<int, K> concat(array<int, K> a, array<int, K> b, int len) { n   return add(a, mul(b, DP[len])); n } n vector<array<int, K> > test; n void preprecalc() { n   DP[0] = make_hash(1); n   for (int i = 1; i < N; i++) DP[i] = mul(P, DP[i - 1]); n   for (int i = 0; i < N; i++) DQ[i] = divide(make_hash(1), DP[i]); n } n int ans = INF; n string s; n void rec(int l, int r, char c, int cur) { n   if (r - l == 1) { n     cur += (s[l] != c); n     ans = min(ans, cur); n     return; n   } n   int m = (r + l) / 2; n   int curl = 0, curr = 0; n   for (int i = l; i < m; ++i) { n     curl += (s[i] != c); n     curr += (s[i + m - l] != c); n   } n   rec(l, m, c + 1, cur + curr); n   rec(m, r, c + 1, cur + curl); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     set<int> have; n     for (int i = 0; i < 2 * n; ++i) { n       int first; n       cin >> first; n       if (!have.count(first)) { n         cout << first <<    ; n         have.insert(first); n       } n     } n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const double EPS = 1e-9; n const int INF = 2147483647; n const long long LLINF = 9223372036854775807LL; n template <class T> n T gcd(T a, T b) { n   if (a < b) swap(a, b); n   if (b == 0) return a; n   return gcd(b, a % b); n } n template <class T> n T lcm(T a, T b) { n   return (a * b) / gcd(a, b); n } n int dp[510001]; n int main() { n   int d, n; n   cin >> n >> d; n   vector<int> c(n); n   for (int i = 0; i < (n); i++) cin >> c[i]; n   for (int i = 0; i < (n); i++) { n     for (int j = (510000); j >= (c[i]); j--) { n       dp[j] = max(dp[j], c[i] + dp[j - c[i]]); n     } n   } n   int ans = 0, lAns = -1, days = -1; n   while (ans != lAns) { n     lAns = ans; n     ans = dp[ans + d]; n     days++; n   } n   if (ans == 0) n     cout << 0 <<     << 0 << endl; n   else n     cout << ans <<     << days << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize(2) n using namespace std; n inline long long read() { n   long long f = 1, x = 0; n   char s = getchar(); n   while (s <  0  || s >  9 ) { n     if (s ==  - ) f = -1; n     s = getchar(); n   } n   while (s >=  0  && s <=  9 ) { n     x = x * 10 -  0  + s; n     s = getchar(); n   } n   return x * f; n } n int zxy = 1e9 + 7; n int n, m, i, j, s, o, k, h; n int a[2005]; n int dl[2005], dr[2005]; n double dp[2005][2005][2][2], P; n bool f[2005][2005][2][2]; n double DP(int l, int r, int f1, int f2) { n   if (l > r) return 0.0; n   if (f[l][r][f1][f2]) return dp[l][r][f1][f2]; n   f[l][r][f1][f2] = 1; n   double dpp = 0.0; n   dpp += 0.5 * P * (min(a[l] - a[l - 1] - h * f1, h) + DP(l + 1, r, 0, f2)); n   dpp += 0.5 * (1.0 - P) * n          (min(a[r + 1] - a[r] - h * f2, h) + DP(l, r - 1, f1, 0)); n   int L = dr[l], R = dl[r]; n   if (L < r) n     dpp += 0.5 * (1.0 - P) * (a[L] - a[l] + h + DP(L + 1, r, 1, f2)); n   else n     dpp += 0.5 * (1.0 - P) * (a[r] - a[l] + min(a[r + 1] - a[r] - h * f2, h)); n   if (R > l) n     dpp += 0.5 * P * (a[r] - a[R] + h + DP(l, R - 1, f1, 1)); n   else n     dpp += 0.5 * P * (a[r] - a[l] + min(a[l] - a[l - 1] - h * f1, h)); n   return dp[l][r][f1][f2] = dpp; n } n signed main() { n   n = read(); n   h = read(); n   scanf( %lf , &P); n   for (int i = 1; i <= n; i++) a[i] = read(); n   sort(a + 1, a + 1 + n); n   a[0] = -0x7f7f7f7f; n   a[n + 1] = 0x7f7f7f7f; n   dr[n] = n; n   dl[1] = 1; n   for (int i = 2; i <= n; i++) { n     if (a[i] - a[i - 1] < h) n       dl[i] = dl[i - 1]; n     else n       dl[i] = i; n   } n   for (int i = n - 1; i > 0; i--) { n     if (a[i + 1] - a[i] < h) n       dr[i] = dr[i + 1]; n     else n       dr[i] = i; n   } n   printf( %.10lf n , DP(1, n, 0, 0)); n   return 0; n } n


#include <bits/stdc++.h> n int n, m, k; n std::unordered_set<int> G[100123]; n int deg[100123]; n inline bool CheckClique(int x) { n   std::vector<int> V({x}); n   for (int y : G[x]) V.emplace_back(y); n   for (int u : V) n     for (int v : V) n       if (u < v && (!G[u].count(v))) return false; n   return true; n } n inline void Solve() { n   for (int i = 1; i <= n; ++i) G[i].clear(); n   memset(deg, 0, sizeof(deg)); n   scanf( %d%d%d , &n, &m, &k); n   std::vector<std::pair<int, int>> input; n   for (int i = 1, u, v; i <= m; ++i) { n     scanf( %d%d , &u, &v); n     input.emplace_back(std::make_pair(u, v)); n   } n   if ((1LL * k * (k - 1) / 2) > m) return puts( -1 ), void(); n   for (auto& p : input) { n     G[p.first].insert(p.second); n     G[p.second].insert(p.first); n     ++deg[p.first]; n     ++deg[p.second]; n   } n   int cnt = 0; n   std::queue<int> q; n   for (int i = 1; i <= n; ++i) n     if (deg[i] < k) { n       if (deg[i] == k - 1 && CheckClique(i)) { n         puts( 2 ); n         for (int x : G[i]) printf( %d  , x); n         printf( %d n , i); n         return; n       } n       q.push(i); n       ++cnt; n     } n   while (!q.empty()) { n     int u = q.front(); n     q.pop(); n     for (int v : G[u]) { n       G[v].erase(u); n       if ((--deg[v]) == k - 1) { n         if (CheckClique(v)) { n           puts( 2 ); n           for (int x : G[v]) printf( %d  , x); n           printf( %d n , v); n           return; n         } n         q.push(v); n         ++cnt; n       } n     } n   } n   if (cnt < n) { n     std::vector<int> ans; n     for (int i = 1; i <= n; ++i) n       if (deg[i] >= k) ans.emplace_back(i); n     printf( 1 n%d n , static_cast<int>(ans.size())); n     for (int i : ans) printf( %d  , i); n     puts(  ); n   } else n     puts( -1 ); n } n int main() { n   int T; n   scanf( %d , &T); n   while (T--) Solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int long long n, c = 0; n   cin >> n; n   for (int long long i = 1; i < n; i++) { n     if (n % i == 0) { n       c++; n     } n   } n   cout << (c); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 105; n long long f[MAXN][MAXN][MAXN]; n int a[MAXN]; n string s; n int n; n long long solve(int l, int r, int pf) { n   if (l >= r) return 0; n   if (r - l == 1) return a[pf]; n   long long &cur = f[l][r][pf]; n   if (cur > 0) return cur; n   cur = max(cur, solve(l + 1, r, 1) + a[pf]); n   for (int i = l + 1; i < r; i++) n     if (s[l] == s[i]) cur = max(cur, solve(l + 1, i, 1) + solve(i, r, pf + 1)); n   return cur; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> s; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   cout << solve(0, n, 1) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int absi(int a) { return (a > 0) ? a : -a; } n const int md = 51123987; n inline void add(int &a, int b) { n   a += b; n   if (a >= md) a -= md; n } n int a[155]; n int tmp[4]; n int nxt[155][4]; n int dp[155][55][55][55]; n int main(void) { n   int n, mx; n   scanf( %d , &n); n   mx = (n + 2) / 3; n   char c = getchar(); n   while (c == 10 || c == 32) c = getchar(); n   for (int i = 1; i <= n; ++i) { n     a[i] = c -  a  + 1; n     c = getchar(); n   } n   memset(tmp, 255, sizeof(tmp)); n   for (int i = n; i; --i) { n     tmp[a[i]] = i; n     memcpy(nxt[i], tmp, sizeof(tmp)); n   } n   int res = 0; n   dp[1][0][0][0] = 1; n   for (int i = 1; i <= n; ++i) { n     for (int j = 0; j <= mx; ++j) { n       for (int k = 0; k <= mx; ++k) { n         for (int l = 0; l <= mx; ++l) { n           if (j + k + l > n) break; n           int val = dp[i][j][k][l]; n           if (j + k + l == n) { n             if (absi(j - k) <= 1 && absi(j - l) <= 1 && absi(k - l) <= 1) { n               add(res, val); n             } n             continue; n           } n           if (~nxt[i][1]) { n             add(dp[nxt[i][1]][j + 1][k][l], val); n           } n           if (~nxt[i][2]) { n             add(dp[nxt[i][2]][j][k + 1][l], val); n           } n           if (~nxt[i][3]) { n             add(dp[nxt[i][3]][j][k][l + 1], val); n           } n         } n       } n     } n   } n   printf( %d n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int T[5000000], sl[5000000], sr[5000000], a[200005], rt[200005], la[200005], n     pos[200005]; n int i, j, n, num, tmp, ans; n void build(int l, int r, int &node) { n   node = ++num; n   if (l == r) return; n   int mid = (l + r) >> 1; n   build(l, mid, sl[node]); n   build(mid + 1, r, sr[node]); n } n void Insert(int l, int r, int x, int y, int node) { n   int k = ++num; n   if (l == r) { n     T[k] = T[node] + y; n     return; n   } n   int mid = (l + r) >> 1; n   if (x <= mid) { n     sl[k] = num + 1, sr[k] = sr[node]; n     Insert(l, mid, x, y, sl[node]); n   } else { n     sl[k] = sl[node], sr[k] = num + 1; n     Insert(mid + 1, r, x, y, sr[node]); n   } n   T[k] = T[sl[k]] + T[sr[k]]; n } n int ask(int l, int r, int k, int node) { n   if (l == r) return l; n   int mid = (l + r) >> 1, now = T[sl[node]]; n   if (now >= k) return ask(l, mid, k, sl[node]); n   return ask(mid + 1, r, k - now, sr[node]); n } n int main() { n   scanf( %d , &n); n   for (i = 1; i <= n; i++) scanf( %d , &a[i]); n   build(1, n + 1, rt[0]); n   rt[1] = num + 1, Insert(1, n + 1, n + 1, 1, rt[0]); n   tmp = 1; n   for (i = n; i; i--) { n     if (pos[a[i]]) n       rt[tmp + 1] = num + 1, Insert(1, n + 1, pos[a[i]], -1, rt[tmp++]); n     rt[tmp + 1] = num + 1, Insert(1, n + 1, i, 1, rt[tmp++]); n     la[i] = tmp; n     pos[a[i]] = i; n   } n   for (i = 1; i <= n; i++) { n     ans = 0; n     j = 1; n     while ((j = ask(1, n + 1, i + 1, rt[la[j]])) <= n) ans++; n     printf( %d  , ans + 1); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int getint() { n   unsigned int c; n   int x = 0; n   while (((c = getchar()) -  0 ) >= 10) { n     if (c ==  - ) return -getint(); n     if (!~c) exit(0); n   } n   do { n     x = (x << 3) + (x << 1) + (c -  0 ); n   } while (((c = getchar()) -  0 ) < 10); n   return x; n } n long long MOD = 1000000007; n const int MAXN = 100011; n struct modint { n   long long v; n   modint() : v(0) {} n   modint(int v) : v(v > MOD ? v - MOD : v) {} n   modint(long long v) : v(v % MOD) {} n   void null() { v = -1; } n   bool is_null() { return v == -1; } n }; n const modint mod = MOD; n modint& operator+=(modint& x, modint y) { return x = x.v + y.v; } n modint& operator-=(modint& x, modint y) { return x = x.v - y.v + MOD; } n modint& operator*=(modint& x, modint y) { return x = (long long)x.v * y.v; } n modint operator+(modint x, modint y) { return x += y; } n modint operator-(modint x, modint y) { return x -= y; } n modint operator*(modint x, modint y) { return x *= y; } n modint operator-(modint x) { return x = modint(0) - x; } n modint minv[MAXN], mfct[MAXN], mifc[MAXN]; n modint mod_inv(const modint& a) { n   if (a.v < MAXN and minv[a.v].v) return minv[a.v]; n   if (a.v == 1) return minv[a.v] = 1; n   modint res = modint(MOD) - modint(MOD / a.v) * mod_inv(MOD % a.v); n   if (a.v < MAXN) minv[a.v] = res; n   return res; n } n modint& operator/=(modint& x, modint y) { return x *= mod_inv(y); } n modint operator/(modint x, modint y) { return x /= y; } n modint mod_pow(modint x, long long n) { n   modint res = 1; n   for (; n; x *= x, n >>= 1) n     if (n & 1) res *= x; n   return res; n } n modint mod_fact(int n) { n   if (mfct[n].v) return mfct[n]; n   mfct[0] = 1; n   for (long long i = 1; i < MAXN; i++) mfct[i] = mfct[i - 1] * i; n   return mfct[n]; n } n modint mod_ifact(int n) { n   return mifc[n].v ? mifc[n] : mifc[n] = mod_inv(mod_fact(n)); n } n modint mod_cmb(int n, int k) { n   if (k < 0 or n < k) return 0; n   return mod_fact(n) * mod_ifact(k) * mod_ifact(n - k); n } n ostream& operator<<(ostream& os, modint a) { n   os << a.v; n   return os; n } n const int N = 1000111000; n vector<int> am; n void make_lucky(long long n) { n   if (n > N) return; n   if (n) am.push_back(n); n   make_lucky(n * 10 + 7), make_lucky(n * 10 + 4); n } n map<int, int> mp; n int cnt[1111], other, k, nn; n long long dp[100111]; n int main() { n   int i, j, tcc, tc = 1 << 28; n   make_lucky(0); n   nn = am.size(); n   for (i = 0; i < am.size(); i++) mp[am[i]] = i; n   for (tcc = 0; tcc < tc; tcc++) { n     memset(cnt, 0, sizeof(cnt)); n     other = 0; n     int n = getint(); n     k = getint(); n     for (i = 0; i < n; i++) { n       int t = getint(); n       if (mp.count(t)) n         cnt[mp[t]]++; n       else n         other++; n     } n     for (i = 0; i <= k; i++) dp[i] = 0; n     dp[0] = 1; n     for (i = 0; i < nn; i++) n       if (cnt[i]) { n         for (j = k - 1; ~j; j--) dp[j + 1] += dp[j] * cnt[i]; n         for (j = 0; j <= k; j++) dp[j] %= MOD; n       } n     modint res = 0; n     for (i = 0; i <= k; i++) { n       res += dp[i] * mod_cmb(other, k - i); n     } n     printf( %d n , (int)res.v); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline void read(T &x) { n   char c; n   bool nega = 0; n   while ((!isdigit(c = getchar())) && c !=  - ) n     ; n   if (c ==  - ) { n     c = getchar(); n     nega = 1; n   } n   x = c - 48; n   while (isdigit(c = getchar())) x = x * 10 + c - 48; n   if (nega) x = -x; n } n inline void update(int64_t &A, int64_t B) { n   if (A > B) A = B; n } n int64_t dp[2][500][500][2]; n vector<pair<int, int>> robot[500][500]; n bool good[500000][4]; n int d[500000], e[500000]; n signed main() { n   int n, m, k; n   read(n); n   read(m); n   read(k); n   vector<int> dx = {0, 1, 1, 0}, dy = {0, -1, 0, 1}; n   int cap = max(m, n); n   for (int i = 0; i < k; i++) { n     int x, y, t; n     read(x); n     read(y); n     read(d[i]); n     read(t); n     read(e[i]); n     for (int zi = 0; zi < 4; zi++) { n       int nx = x + dx[zi] * d[i], ny = y + dy[zi] * d[i]; n       if (zi + t <= nx + ny && (zi + t) % 4 == (nx + ny) % 4) { n         robot[nx][ny].push_back({i, zi}); n         good[i][zi] = 1; n       } n     } n   } n   for (int X = 0; X < 2; X++) n     for (int Y = 0; Y < m; Y++) n       for (int W = 0; W < cap; W++) n         for (int vert = 0; vert < 2; vert++) dp[X][Y][W][vert] = LLONG_MAX; n   dp[0][0][0][0] = dp[0][0][0][1] = 0; n   int64_t ans = LLONG_MAX; n   for (int X = 0; X < n; X++) { n     int cur = X & 1; n     int nxt = 1 - cur; n     for (int Y = 0; Y < m; Y++) { n       for (int W = 0; W < cap; W++) { n         for (int vert = 0; vert < 2; vert++) { n           if (W > (vert ? X : Y) || dp[cur][Y][W][vert] == LLONG_MAX) break; n           if (Y < m - 1) { n             int right = (vert ? 0 : W) + 1; n             int64_t smash = 0; n             for (auto &[id, zi] : robot[X][Y + 1]) { n               if (zi == 2 && right >= d[id] && good[id][1]) continue; n               if (zi == 3 && right >= d[id] && good[id][0]) continue; n               smash += e[id]; n             } n             update(dp[cur][Y + 1][right][0], dp[cur][Y][W][vert] + smash); n             update(dp[cur][Y + 1][0][1], dp[cur][Y][W][vert] + smash); n           } n           if (X < n - 1) { n             int down = (vert ? W : 0) + 1; n             int64_t smash = 0; n             for (auto &[id, zi] : robot[X + 1][Y]) { n               if (zi == 2 && down >= d[id] && good[id][0]) continue; n               smash += e[id]; n             } n             update(dp[nxt][Y][down][1], dp[cur][Y][W][vert] + smash); n             update(dp[nxt][Y][0][0], dp[cur][Y][W][vert] + smash); n           } n         } n       } n     } n     if (X == n - 1) { n       for (int W = 0; W < cap; W++) n         for (int vert = 0; vert < 2; vert++) n           update(ans, dp[cur][m - 1][W][vert]); n       break; n     } n     for (int Y = 0; Y < m; Y++) n       for (int W = 0; W < cap; W++) n         for (int vert = 0; vert < 2; vert++) dp[cur][Y][W][vert] = LLONG_MAX; n   } n   printf( %lld , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int x1, x2, y1, y2, xmin, xmax, ymin, ymax; n   cin >> x1 >> y1 >> x2 >> y2; n   xmin = x1; n   xmax = x2; n   ymin = y1; n   ymax = y2; n   int total = (x2 - x1) * (y2 - y1); n   for (int a = 1; a < n; ++a) { n     cin >> x1 >> y1 >> x2 >> y2; n     xmin = min(x1, xmin); n     xmax = max(x2, xmax); n     ymin = min(y1, ymin); n     ymax = max(y2, ymax); n     total += (x2 - x1) * (y2 - y1); n   } n   if (ymax - ymin == xmax - xmin and (xmax - xmin) * (xmax - xmin) == total) n     cout <<  YES  << endl; n   else n     cout <<  NO  << endl; n   cin >> n; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k, x1, x2, s = 0, x, y1 = 0, y2 = 0, sm = 0; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> x; n     for (int i = 0; i < x; i++) cout << 7 <<    ; n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n vector<int> a[100010]; n int x; n int now; n void print() { n   cout <<  YES  << endl; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) cout << a[i][j] <<    ; n     cout << endl; n   } n } n int main() { n   cin >> n >> m; n   now = 1; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) { n       a[i].push_back(now); n       now += 1; n     } n   if (n == 1 && m == 1) { n     cout <<  YES  << endl; n     cout << 1; n     return 0; n   } n   if (n * m == 2) { n     cout <<  NO ; n     return 0; n   } n   if (n * m == 3) { n     cout <<  NO ; n     return 0; n   } n   if (n == 2 && m == 2) { n     cout <<  NO ; n     return 0; n   } n   if (n * m == 6 && min(n, m) == 2) { n     cout <<  NO ; n     return 0; n   } n   if (n == 3 && m == 3) { n     cout <<  YES  << endl; n     cout <<  6 1 8  << endl; n     cout <<  7 5 3  << endl; n     cout <<  2 9 4  << endl; n     return 0; n   } n   if (n == 1 && m >= 4) { n     now = 0; n     for (int i = 2; i <= m; i += 2) { n       a[0][now] = i; n       now += 1; n     } n     for (int i = 1; i <= m; i += 2) { n       a[0][now] = i; n       now += 1; n     } n     print(); n     return 0; n   } n   if (m == 1 && n >= 4) { n     now = 0; n     for (int i = 2; i <= n; i += 2) { n       a[now][0] = i; n       now += 1; n     } n     for (int i = 1; i <= n; i += 2) { n       a[now][0] = i; n       now += 1; n     } n     print(); n     return 0; n   } n   if (m >= 4) { n     for (int i = 0; i < n; i++) n       if (i % 2 == 0) { n         int tmp0 = a[i][m - 2]; n         int tmp1 = a[i][m - 1]; n         for (int j = m - 1; j >= 2; j--) a[i][j] = a[i][j - 2]; n         a[i][1] = tmp1; n         a[i][0] = tmp0; n       } n     for (int i = 0; i < m; i++) n       if (i % 2 == 0) { n         int tmp0 = a[0][i]; n         for (int j = 0; j < n - 1; j++) a[j][i] = a[j + 1][i]; n         a[n - 1][i] = tmp0; n       } n     print(); n     return 0; n   } n   if (n >= 4) { n     for (int i = 0; i < m; i++) n       if (i % 2 == 0) { n         int tmp0 = a[n - 2][i]; n         int tmp1 = a[n - 1][i]; n         for (int j = n - 1; j >= 2; j--) a[j][i] = a[j - 2][i]; n         a[1][i] = tmp1; n         a[0][i] = tmp0; n       } n     for (int i = 0; i < n; i++) n       if (i % 2 == 0) { n         int tmp0 = a[i][0]; n         for (int j = 0; j < m - 1; j++) a[i][j] = a[i][j + 1]; n         a[i][m - 1] = tmp0; n       } n     print(); n     return 0; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n = 0, d = 0, count = 0, maxCount = 0; n   cin >> n >> d; n   string s =   ; n   for (int i = 0; i < d; i++) { n     cin >> s; n     int res = 1; n     for (int j = 0; j < s.size(); j++) { n       res &= (s[j] -  0 ); n     } n     if (res == 1) { n       maxCount = max(count, maxCount); n       count = 0; n     } else { n       count++; n     } n   } n   maxCount = max(count, maxCount); n   cout << maxCount; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int read() { n   int x = 0, f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = (x << 3) + (x << 1) + (ch ^ 48); n     ch = getchar(); n   } n   return x * f; n } n int n, m, k, cnt, a[200005], Of, Os; n char ch[200005]; n int abs(int x, int y) { n   if (x > y) n     return x - y; n   else n     return y - x; n } n int main() { n   n = read(); n   m = read(); n   k = read(); n   ; n   for (int i = 1; i <= n; i++) { n     scanf( %s , (ch + 1)); n     int f = 0, s = 0, ff = 0, ss = 0; n     for (int j = 1; j <= m; j++) { n       if (ch[j] ==  G ) f = j, ff++; n       if (ch[j] ==  R ) s = j, ss++; n     } n     if (f == 0 && s != 0 && ss != m) Os = 1; n     if (f != 0 && s == 0 && ff != m) Of = 1; n     if (f && s) a[++cnt] = abs(s - f) - 1; n   } n   if (Os == 1 && Of == 1) n     puts( Draw ); n   else if (Os == 1 && Of == 0) n     puts( Second ); n   else if (Of == 1 && Os == 0) n     puts( First ); n   else { n     for (int i = 0; i <= 8; i++) { n       int ff = 0; n       for (int j = 1; j <= cnt; j++) ff += ((a[j] >> i) & 1); n       if (ff % (k + 1)) { n         puts( First ); n         return 0; n       } n     } n     puts( Second ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> zp; n int zer; n int one; n int n; n int allz; n int q() { n   cout <<  ?  <<    ; n   for (int i = 0; i < n; i++) { n     cout << zp[i]; n   } n   cout << endl; n   int x; n   cin >> x; n   return x; n } n void get(int bg, int ed, int W) { n   if ((zer != -1) and (one != -1)) { n     return; n   } n   int cur = W; n   int L = ed - bg; n   int d = cur - allz; n   if (d == -L) { n     one = bg; n     return; n   } n   if (d == L) { n     zer = bg; n     return; n   } n   int mid = (bg + ed) / 2; n   for (int i = mid; i < ed; i++) { n     zp[i] = 0; n   } n   int D = q(); n   get(bg, mid, D); n   for (int i = mid; i < ed; i++) { n     zp[i] = 1; n   } n   if ((zer != -1) and (one != -1)) { n     return; n   } n   for (int i = bg; i < mid; i++) { n     zp[i] = 0; n   } n   get(mid, ed, W - D + allz); n   for (int i = bg; i < mid; i++) { n     zp[i] = 1; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   zp.resize(n); n   zer = -1; n   one = -1; n   allz = q(); n   for (int i = 0; i < n; i++) { n     zp[i] = 1; n   } n   get(0, n, n - allz); n   cout <<  !   << zer + 1 <<     << one + 1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma warning(disable : 4996) n #pragma comment(linker,  /STACK:20000007 ) n #pragma GCC optimize( unroll-loops ) n const int N = (int)1e6 + 100; n const int maxn = (int)1e3 + 100; n const int base = (int)1e9; n const int mod = (int)1e9 + 7; n const int inf = INT_MAX; n const long long ll_inf = LLONG_MAX; n const long double PI = acos((long double)-1.0); n const long double eps = 1e-8; n int n, m, k; n char ch[1003][1003]; n bool mark[1003][1003]; n int d[1003][1003]; n int x, y, x1, y2; n vector<int> dx = {1, 0, -1, 0}; n vector<int> dy = {0, -1, 0, 1}; n void bfs(int x, int y) { n   d[x][y] = 1; n   queue<pair<int, int> > Q; n   Q.push(make_pair(x, y)); n   while (!Q.empty()) { n     auto P = Q.front(); n     Q.pop(); n     int x, y; n     tie(x, y) = P; n     mark[x][y] = true; n     if (x == x1 && y == y2) return; n     for (int i = 0; i < 4; ++i) { n       for (int j = 1; j <= k; ++j) { n         int r = x, c = y; n         r += dx[i] * j; n         c += dy[i] * j; n         if (r < 1 || c < 1 || c > m || r > n || n             (ch[r][c] ==  #  || (d[r][c] && d[r][c] <= d[x][y]))) n           break; n         if (d[r][c] > d[x][y] + 1) { n           d[r][c] = min(d[r][c], d[x][y] + 1); n           Q.push({r, c}); n         } else if (!d[r][c]) { n           d[r][c] = d[x][y] + 1; n           Q.push({r, c}); n         } n       } n     } n   } n } n void solve() { n   cin >> n >> m >> k; n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= m; ++j) { n       cin >> ch[i][j]; n     } n   } n   cin >> x >> y >> x1 >> y2; n   bfs(x, y); n   cout << --d[x1][y2] <<   n ; n } n int main() { n   int T = 1; n   for (; T; --T) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> res; n long long n, a, b; n void solve(long long x, long long y) { n   long long i, k; n   for (i = 1, k = 0; k < x; i += a, k++) { n     for (long long j = i + 1; j < i + a; j++) res.push_back(j); n     res.push_back(i); n   } n   for (k = 0; k < y; i += b, k++) { n     for (long long j = i + 1; j < i + b; j++) res.push_back(j); n     res.push_back(i); n   } n } n int main() { n   cout.sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n >> a >> b; n   for (long long i = 0; i <= n; i++) n     if (a * i <= n and (n - a * i) % b == 0) { n       solve(i, (n - a * i) / b); n       for (long long val : res) cout << val <<    ; n       cout << endl; n       return 0; n     } n   cout << -1 << 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-- > 0) { n     int N; n     cin >> N; n     struct MM { n       int min, max; n     }; n     MM data[129]; n     for (int i = 0; i <= 128; ++i) { n       if (i >= 63 && i < 63 + N) n         data[i] = {i - 62, i - 62}; n       else n         data[i] = {0, 0}; n     } n     for (int i = 62; i >= 0; --i) { n       MM& fi = data[2 * i + 1]; n       MM& se = data[2 * i + 2]; n       if (se.min == 0) { n         data[i] = fi; n       } else { n         string res; n         if (fi.min == fi.max && se.min == se.max) { n           cout <<  ?   << fi.min <<     << se.min << endl; n           cout.flush(); n           cin >> res; n           if (res ==  < ) n             data[i] = {fi.min, se.max}; n           else n             data[i] = {se.min, fi.max}; n         } else { n           cout <<  ?   << fi.min <<     << se.min << endl; n           cout.flush(); n           cin >> res; n           if (res ==  < ) n             data[i].min = fi.min; n           else n             data[i].min = se.min; n           cout <<  ?   << fi.max <<     << se.max << endl; n           cout.flush(); n           cin >> res; n           if (res ==  > ) n             data[i].max = fi.max; n           else n             data[i].max = se.max; n         } n       } n     } n     cout <<  !   << data[0].min <<     << data[0].max << endl; n     cout.flush(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, a, b; n   scanf( %d , &n); n   ; n   if (n == 1) { n     scanf( %d , &a); n     scanf( %d , &b); n     printf( %d %d n , a, b); n     return 0; n   } n   multimap<int, int> l; n   multimap<int, int>::iterator p, it; n   set<int> s; n   for (int i = 0; i < (n); i++) { n     scanf( %d , &a); n     ; n     scanf( %d , &b); n     ; n     l.insert(make_pair(a, b)); n     l.insert(make_pair(b, a)); n     if (s.count(a)) n       s.erase(a); n     else n       s.insert(a); n     if (s.count(b)) n       s.erase(b); n     else n       s.insert(b); n   } n   a = *s.begin(); n   b = *s.rbegin(); n   printf( %d  , a); n   p = l.find(a); n   while (true) { n     printf( %d  , p->second); n     it = l.find(p->second); n     if (it->second == p->first) { n       l.erase(it); n       it = l.find(p->second); n     } n     p = it; n     if (it->second == b) break; n   } n   printf( %d n , b); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int ToInt(string& s, int len) { n   int r = 0; n   for (int i = 0, max_ = (len); i < max_; ++i) { n     r *= 10; n     r += s[i] -  0 ; n   } n   return r; n } n int GCD(int a, int b) { return b != 0 ? GCD(b, a % b) : a; } n int LCM(int a, int b) { return a * (b / GCD(a, b)); } n long long Pow(long long n, long long e) { n   if (e == 0) return 1; n   if (e == 1) n     return n; n   else if (e & 1) { n     long long t = Pow(n, e / 2); n     return n * t * t; n   } else { n     long long t = Pow(n, e / 2); n     return t * t; n   } n } n pair<int, char> v[26]; n bool toRem[26]; n int main() { n   string s; n   getline(cin, s); n   int K; n   scanf( %d , &K); n   for (int i = 0, max_ = (26); i < max_; ++i) v[i].second =  a  + i; n   for (int i = 0, max_ = ((int)(s).size()); i < max_; ++i) n     ++v[s[i] -  a ].first; n   sort(v, v + 26); n   int j = 0, rem = 26; n   while (j < 26) { n     if (K < v[j].first) break; n     toRem[v[j].second -  a ] = true; n     K -= v[j].first; n     --rem; n     ++j; n   } n   printf( %d n , rem); n   for (int i = 0, max_ = ((int)(s).size()); i < max_; ++i) n     if (!toRem[s[i] -  a ]) printf( %c , s[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a = 0, b = 0, f = 0, t = 0, x[101010], s2 = 0, s3 = 0, i = 0, j = 0, n           p = 0, k = 0; n string d1, d2, d3; n int main() { n   cin >> a >> p; n   for (b = 1; b <= a; b++) { n     cin >> f; n     if (f > p) n       s2 += 2; n     else n       s2 += 1; n   } n   cout << s2; n } n


#include <bits/stdc++.h> n using namespace std; n  n #define fr(i, a, b) for(int i = a; i < b; ++i) n #define rf(i, a, b) for(int i = a - 1; i >= b; --i) n #define len(v) size(v) n #define all(v) begin(v), end(v) n #define int long long n #define ff first n #define ss second n #define ii pair<int, int> n #define vi vector<int> n #define vv vector<vi> n #define pb push_back n #define ub upper_bound n #define lb lower_bound n #define ts to_string n  n template<typename A, typename B> A amax(A &a, B b) {a = max(a, b); return a;} n template<typename A, typename B> A amin(A &a, B b) {a = min(a, b); return a;} n template<typename A, typename B> string ts(pair<A, B> p); n string ts(const string& s) {return     + s +    ;} n string ts(const char* s) {return ts((string)s);} n template<typename A> string ts(A v) { n     bool first = true; n     string res =  { ; n     for(const auto &x : v) { n         if(!first) res +=  ,  ; n         first = false, res += ts(x); n     } n     res +=  } ; n     return res; n } n template<typename A, typename B> string ts(pair<A, B> p) { n     return  (  + ts(p.ff) +  ,   + ts(p.ss) +  ) ; n } n void debug_out() {cerr <<   n ;} n template<typename H, typename... T> void debug_out(H h, T... t) { n     cerr <<     << ts(h); n     debug_out(t...); n } n  n #ifdef LOCAL n #define dbg(...) cerr <<  [  << #__VA_ARGS__ <<  ]: , debug_out(__VA_ARGS__) n #else n #define dbg(...)  n #endif n  n const int INF = 0x3f3f3f3f3f3f3f3fll; n const int MOD = 1e9 + 7;  n const int N   = 1e6 + 5; n  n void solve() {   n     int n; n     cin >> n; n     vector<pair<int, int>> a(n); n     fr(i, 0, n) { n         cin >> a[i].ff; n         a[i].ss = i + 1; n     } n     sort(all(a)); n  n     int ans = 0; n     fr(i, 0, n) { n         int n1 = a[i].ff, i1 = a[i].ss; n         fr(j, i + 1, n) { n             int n2 = a[j].ff, j1 = a[j].ss; n             if(n1 * n2 > 2 * n) break; n             if(n1 * n2 == i1 + j1) { n                 ans++; n             } n         } n     } n  n     cout << ans <<   n ; n  n } n  n int32_t main() { n     ios_base::sync_with_stdio(false);  n     cin.tie(nullptr); n  n     int t = 0; n     if(!t) cin >> t; n     while(t--) solve(); n }


#include <bits/stdc++.h> n using namespace std; n int main() { n   int q; n   cin >> q; n   while (q--) { n     long long x[3]; n     cin >> x[0] >> x[1] >> x[2]; n     sort(x, x + 3); n     x[2] -= x[1] - x[0]; n     cout << x[1] + x[2] / 2 << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, i, j, kol, tmp, flag[1000]; n char s[1000][1000]; n int main() { n   cin >> n >> m; n   for (i = 1; i <= n; i++) { n     cin >> s[i]; n   } n   for (j = 0; j < m; j++) { n     tmp = 0; n     for (i = 1; i <= n - 1; i++) n       if ((s[i][j] > s[i + 1][j]) && (flag[i] == 0)) tmp = 1; n     kol += tmp; n     if (tmp == 0) n       for (i = 1; i <= n - 1; i++) n         if ((s[i][j] < s[i + 1][j]) && (flag[i] == 0)) flag[i] = 1; n   } n   cout << kol; n } n


#include <bits/stdc++.h> n using namespace std; n int a[501][501], b[501][501], i, j, n, m, k; n int main() { n   vector<int> vea; n   vector<int> veb; n   cin >> n >> m; n   for (i = 1; i <= n; i++) { n     for (j = 1; j <= m; j++) cin >> a[i][j]; n   } n   for (i = 1; i <= n; i++) { n     for (j = 1; j <= m; j++) cin >> b[i][j]; n   } n   for (i = 1; i <= n; i++) { n     j = 1; n     k = i; n     while (true) { n       vea.push_back(a[k][j]); n       veb.push_back(b[k][j]); n       k--; n       j++; n       if (k == 0 || j == m + 1) break; n     } n     sort(vea.begin(), vea.end()); n     sort(veb.begin(), veb.end()); n     for (j = 0; j < vea.size(); j++) { n       if (vea[j] != veb[j]) { n         cout <<  NO ; n         return 0; n       } n     } n     while (!vea.empty()) vea.pop_back(); n     while (!veb.empty()) veb.pop_back(); n   } n   for (i = 1; i <= n; i++) { n     j = m; n     k = i; n     while (true) { n       vea.push_back(a[k][j]); n       veb.push_back(b[k][j]); n       k++; n       j--; n       if (k == n + 1 || j == 0) break; n     } n     sort(vea.begin(), vea.end()); n     sort(veb.begin(), veb.end()); n     for (j = 0; j < vea.size(); j++) { n       if (vea[j] != veb[j]) { n         cout <<  NO ; n         return 0; n       } n     } n     while (!vea.empty()) vea.pop_back(); n     while (!veb.empty()) veb.pop_back(); n   } n   for (j = 1; j <= m; j++) { n     i = 1; n     k = j; n     while (true) { n       vea.push_back(a[i][k]); n       veb.push_back(b[i][k]); n       k--; n       i++; n       if (k == 0 || i == n + 1) break; n     } n     sort(vea.begin(), vea.end()); n     sort(veb.begin(), veb.end()); n     for (i = 0; i < vea.size(); i++) { n       if (vea[i] != veb[i]) { n         cout <<  NO ; n         return 0; n       } n     } n     while (!vea.empty()) vea.pop_back(); n     while (!veb.empty()) veb.pop_back(); n   } n   for (j = m; j >= 1; j--) { n     i = n; n     k = j; n     while (true) { n       vea.push_back(a[i][k]); n       veb.push_back(b[i][k]); n       k++; n       i--; n       if (k == m + 1 || i == 0) break; n     } n     sort(vea.begin(), vea.end()); n     sort(veb.begin(), veb.end()); n     for (i = 0; i < vea.size(); i++) { n       if (vea[i] != veb[i]) { n         cout <<  NO ; n         return 0; n       } n     } n     while (!vea.empty()) vea.pop_back(); n     while (!veb.empty()) veb.pop_back(); n   } n   cout <<  YES ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, i; n   cin >> n; n   long long int a[n]; n   for (i = 0; i < n; i = i + 1) { n     cin >> a[i]; n   } n   long long int mn = a[n - 1]; n   long long int m[n]; n   for (i = n - 1; i >= 0; i--) { n     mn = min(mn, a[i]); n     m[i] = mn; n   } n   for (i = 0; i < n; i = i + 1) { n     long long int low = i; n     long long int high = n - 1; n     long long int res = i; n     while (low <= high) { n       long long int mid = (low + high) / 2; n       if (m[mid] >= a[i]) { n         high = mid - 1; n       } else if (m[mid] < a[i]) { n         low = mid + 1; n         res = max(res, mid); n       } n     } n     cout << res - i - 1 <<    ; n   } n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long n; n bool use[100000 + 10], b[100000 + 10]; n int a[100000 + 10], Now; n int Ans; n int L[100000 + 10], R[100000 + 10], CNT = 0; n int main() { n   int Cnt = 0; n   scanf( %I64d , &n); n   for (long long i = 3; i <= n; i += 2) { n     if (b[i]) continue; n     Now = 1; n     a[1] = i; n     for (long long j = 2 * i; j <= n; j += i) { n       b[j] = false; n       if (use[j]) continue; n       a[++Now] = j; n     } n     if (Now == 1) continue; n     if (Now % 2 == 0) { n       for (int i = 1; i + 1 <= Now; i += 2) { n         Ans++; n         L[Ans] = a[i]; n         use[a[i]] = true; n         R[Ans] = a[i + 1]; n         use[a[i + 1]] = true; n       } n     } else { n       Ans++; n       L[Ans] = a[1]; n       use[a[1]] = true; n       R[Ans] = a[3]; n       use[a[3]] = true; n       for (int i = 4; i + 1 <= Now; i += 2) { n         Ans++; n         L[Ans] = a[i]; n         use[a[i]] = true; n         R[Ans] = a[i + 1]; n         use[a[i + 1]] = true; n       } n     } n   } n   Now = 0; n   for (long long j = 2; j <= n; j += 2) { n     b[j] = false; n     if (use[j]) continue; n     a[++Now] = j; n   } n   if (Now % 2 == 0) { n     for (int i = 1; i + 1 <= Now; i += 2) { n       Ans++; n       L[Ans] = a[i]; n       use[a[i]] = true; n       R[Ans] = a[i + 1]; n       use[a[i + 1]] = true; n     } n   } else { n     if (Now != 1) { n       Ans++; n       L[Ans] = a[1]; n       use[a[1]] = true; n       R[Ans] = a[3]; n       use[a[3]] = true; n       for (int i = 4; i + 1 <= Now; i += 2) { n         Ans++; n         L[Ans] = a[i]; n         use[a[i]] = true; n         R[Ans] = a[i + 1]; n         use[a[i + 1]] = true; n       } n     } n   } n   printf( %d n , Ans); n   for (int i = 1; i <= Ans; i++) printf( %d %d n , L[i], R[i]); 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   cout.tie(NULL); n   long long n; n   cin >> n; n   long long a[n]; n   for (int i = 0; i < n; ++i) { n     a[i] = i + 1; n   } n   long long ans = 0; n   long long i = 1, j = 1, k = 2; n   for (int q = 0; q < n - 2; ++q) { n     ans += i * a[j] * a[k]; n     j = (j + 1) % (n); n     k = (k + 1) % (n); n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1005; n int n, q, len[N]; n char name[N][20], ip[N][20]; n bool dy(char ip1[], char ip2[], int len) { n   for (int i = 0; i < len; i++) n     if (ip1[i] != ip2[i]) return 0; n   return 1; n } n int main() { n   scanf( %d%d , &n, &q); n   for (int i = 1; i <= n; i++) n     scanf( %s%s , name[i], ip[i]), len[i] = strlen(ip[i]); n   while (q--) { n     char na[20], IP[20]; n     scanf( %s%s , na, IP); n     int L = strlen(IP) - 1; n     IP[L] =   0 ; n     for (int i = 1; i <= n; i++) n       if (len[i] == L && dy(IP, ip[i], L)) { n         printf( %s %s; #%s n , na, IP, name[i]); n         break; n       } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   vector<int> arr{2,  3,  5,  7,  11, 13, 17, 19, 23, 29, n                   31, 37, 41, 43, 47, 4,  9,  25, 49}; n   int i = 0, k = 0; n   int l = arr.size(); n   while (i < l) { n     string s; n     cout << arr[i] << endl; n     cin >> s; n     if (s ==  yes ) k++; n     if (k == 2) { n       cout <<  composite  << endl; n       break; n     } n     i++; n   } n   if (k < 2) { n     cout <<  prime  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, A[100001], Ans[100001]; n bool k[100001]; n int solve(int x) { n   if (Ans[x]) return Ans[x]; n   int tmp = 0, cnt = 0, pre = 1; n   for (int i = 1; i <= n; i++) { n     if (!k[A[i]]) { n       if (cnt == x) { n         tmp++; n         cnt = 1; n         for (int j = pre; j < i; j++) { n           k[A[j]] = 0; n         } n         pre = i; n         k[A[i]] = 1; n       } else { n         cnt++; n         k[A[i]] = 1; n       } n     } n   } n   if (cnt) { n     tmp++; n     for (int i = pre; i <= n; i++) k[A[i]] = 0; n   } n   return Ans[x] = tmp; n } n void work(int x, int y) { n   int l = solve(x), r = solve(y); n   if (l == r) { n     for (int i = x; i <= y; i++) { n       Ans[i] = l; n     } n     return; n   } n   int mid = (x + y) / 2; n   work(x, mid); n   work(mid + 1, y); n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &A[i]); n   work(1, n); n   for (int i = 1; i <= n; i++) printf( %d  , Ans[i]); n } n


#include <bits/stdc++.h> n int atob(int t, int a, int b) { n   int ret = 0, p = 1; n   while (t) { n     ret += t % b * p; n     p *= a; n     t /= b; n   } n   return ret; n } n int maxbit(int t) { n   int ret = t % 10; n   while (t) { n     if (ret < t % 10) ret = t % 10; n     t /= 10; n   } n   return ret; n } n int len(int t) { n   int cnt = 0; n   while (t) { n     cnt++; n     t /= 10; n   } n   return cnt; n } n int main() { n   int a, b, base, res = 0; n   scanf( %d%d , &a, &b); n   base = maxbit(a) + 1; n   if (base < maxbit(b) + 1) base = maxbit(b) + 1; n   for (; base <= 10; ++base) { n     int ta = atob(a, base, 10), tb = atob(b, base, 10), n         t = atob(ta + tb, 10, base); n     if (res < len(t)) res = len(t); n   } n   printf( %d , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long oo = 1e9; n const int maxSize = 1001000; n const int mod = 1e9 + 7; n const double EPS = (1e-9); n int dcmp(double x, double y) { return fabs(x - y) <= EPS ? 0 : x < y ? -1 : 1; } n int main() { n   int one = 0, remain1 = 0, two = 0, a, b; n   int n, res = 0; n   scanf( %d%d%d , &n, &a, &b); n   for (int i = 0; i < n; i++) { n     int num; n     scanf( %d , &num); n     if (num == 1) { n       if (a) n         a--; n       else if (remain1 < 2 * b) n         remain1++; n       else n         res++; n     } else { n       if (b - remain1 > 0) n         b--; n       else n         res += 2; n     } n   } n   cout << res; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e3 + 7; n int n; n int a[N], b[N], x[N], y[N]; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d%d , &a[i], &b[i]); n   } n   for (int i = 1; i <= n; i++) { n     scanf( %d%d , &x[i], &y[i]); n   } n   sort(a + 1, a + 1 + n); n   sort(b + 1, b + 1 + n); n   sort(x + 1, x + 1 + n); n   sort(y + 1, y + 1 + n); n   cout << a[1] + x[n] <<     << b[1] + y[n] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1791791791; n const long long INFLL = 1791791791791791791ll; n template <int input_buf_size, int output_buf_size> n class FastIO { n   char cbuf[input_buf_size + 1]; n   int icur = 0; n   inline bool go_to_next_token() { n     while (cbuf[icur] ==     || cbuf[icur] ==   n ) icur++; n     while (cbuf[icur] == 0) { n       icur = 0; n       if (fgets(cbuf, sizeof(cbuf), stdin) != cbuf) return false; n       while (cbuf[icur] ==     || cbuf[icur] ==   n ) icur++; n     } n     return true; n   } n  n  public: n   string readString() { n     assert(go_to_next_token()); n     string ans; n     while (cbuf[icur] !=     && cbuf[icur] !=   n  && cbuf[icur] != 0) n       ans.push_back(cbuf[icur++]); n     ans.shrink_to_fit(); n     return ans; n   } n   template <class int_type> n   int_type readInt() { n     assert(go_to_next_token()); n     int_type x = 0; n     bool m = cbuf[icur] ==  - ; n     if (m) icur++; n     while ( 0  <= cbuf[icur] && cbuf[icur] <=  9 ) { n       x *= 10; n       x += (cbuf[icur] -  0 ); n       icur++; n     } n     if (m) x = -x; n     return x; n   } n   bool seekEof() { return !go_to_next_token(); } n  n  private: n   char obuf[output_buf_size + 1]; n   int ocur = 0; n   inline void write_string(const char* str, size_t sz = 0) { n     if (sz == 0) sz = strlen(str); n     if (ocur + sz > output_buf_size) { n       fputs(obuf, stdout); n       fputs(str, stdout); n       ocur = 0; n       obuf[0] = 0; n       return; n     } n     strcpy(obuf + ocur, str); n     ocur += sz; n     obuf[ocur] = 0; n   } n  n  public: n   template <class int_type> n   void writeInt(int_type x, bool sp = true) { n     char buf[20]; n     int c = 0; n     if (x < 0) { n       buf[c++] =  - ; n       x = -x; n     } n     int s = c; n     if (x == 0) { n       buf[c++] =  0 ; n     } n     while (x > 0) { n       buf[c++] = (x % 10) +  0 ; n       x /= 10; n     } n     for (int i = 0; 2 * i < c - s; i++) { n       swap(buf[s + i], buf[c - 1 - i]); n     } n     buf[c] = 0; n     write_string(buf, c); n     if (sp) write_string(   , 1); n   } n   void writeString(string s, bool space = true) { n     write_string(s.c_str(), s.size()); n     if (space) write_string(   , 1); n   } n   void writeEndl() { write_string(  n , 1); } n   void flush() { n     fputs(obuf, stdout); n     ocur = 0; n     obuf[0] = 0; n   } n  n  private: n   bool lflush; n  n  public: n   FastIO(bool local_flush) { n     obuf[0] = 0; n     lflush = local_flush; n   } n   ~FastIO() { fputs(obuf, stdout); } n }; n FastIO<10000000, 10000000> IO(true); n const int maxn = 3179; n vector<int> graph[maxn]; n int dist[maxn][maxn]; n vector<int> mdff[maxn]; n vector<int> mdfs[maxn]; n int main() { n   int n = IO.readInt<int>(); n   int m = IO.readInt<int>(); n   for (int i = 0; i < ((int)(m)); ++i) { n     int u = IO.readInt<int>() - 1; n     int v = IO.readInt<int>() - 1; n     graph[u].push_back(v); n   } n   for (int start = 0; start < ((int)(n)); ++start) { n     fill(dist[start], dist[start] + n, -1); n     dist[start][start] = 0; n     queue<int> q; n     q.push(start); n     while (!q.empty()) { n       int a = q.front(); n       q.pop(); n       for (int v : graph[a]) { n         if (dist[start][v] == -1) { n           dist[start][v] = dist[start][a] + 1; n           q.push(v); n         } n       } n     } n   } n   for (int i = 0; i < ((int)(n)); ++i) { n     for (int j = 0; j < ((int)(n)); ++j) n       if (j != i) { n         mdfs[i].push_back(j); n         mdff[i].push_back(j); n       } n     sort((mdfs[i]).begin(), (mdfs[i]).end(), n          [&](const int& a, const int& b) -> bool { n            return dist[i][a] > dist[i][b]; n          }); n     sort((mdff[i]).begin(), (mdff[i]).end(), n          [&](const int& a, const int& b) -> bool { n            return dist[a][i] > dist[b][i]; n          }); n   } n   int a, b, c, d; n   int ans = -1; n   for (int m1 = 0; m1 < ((int)(n)); ++m1) { n     for (int m2 = 0; m2 < ((int)(n)); ++m2) { n       if (m1 != m2) { n         for (int i = 0; i < ((int)(3)); ++i) { n           for (int j = 0; j < ((int)(3)); ++j) { n             int l = mdff[m1][i]; n             int r = mdfs[m2][j]; n             if (m1 == r || m2 == l || l == r) continue; n             if (dist[l][m1] == -1 || dist[m1][m2] == -1 || dist[m2][r] == -1) n               continue; n             if (dist[l][m1] + dist[m1][m2] + dist[m2][r] > ans) { n               ans = dist[l][m1] + dist[m1][m2] + dist[m2][r]; n               a = l; n               b = m1; n               c = m2; n               d = r; n             } n           } n         } n       } n     } n   } n   cerr << ans << endl; n   IO.writeInt(a + 1); n   IO.writeInt(b + 1); n   IO.writeInt(c + 1); n   IO.writeInt(d + 1); n   IO.writeEndl(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double EPS = 1e-9; n bool operator<(const complex<double>& a, const complex<double>& b) { n   if (fabs(a.imag() - b.imag()) > EPS) return a.imag() < b.imag(); n   return a.real() + EPS < b.real(); n } n class cmp { n  public: n   bool operator()(const complex<double>& a, const complex<double>& b) { n     return a < b; n   } n }; n bool pointOnLeft(const complex<double>& p, const complex<double>& p1, n                  const complex<double>& p2) { n   return imag(conj(((p2) - (p1))) * (((p) - (p1)))) > EPS; n } n class cmpHull { n  public: n   complex<double> about, nex; n   cmpHull(const complex<double>& about, const complex<double>& nex) n       : about(about), nex(nex) {} n   bool operator()(const complex<double>& a, const complex<double>& b) { n     double crs = imag(conj(((a) - (about))) * (((b) - (about)))); n     if (fabs(crs) < EPS) { n       if (fabs(imag(conj(((nex) - (about))) * (((a) - (about))))) < EPS) n         return a < b; n       return b < a; n     } n     return crs > 0; n   } n }; n void sortAntiClockwise(vector<complex<double> >& polygon) { n   int about = 0; n   for (int c = 1; c < (int)polygon.size(); c++) n     if (polygon[c] < polygon[about]) about = c; n   swap(polygon[about], polygon[0]); n   sort(polygon.begin() + 1, polygon.end(), cmpHull(polygon[0], polygon[0])); n   sort(polygon.begin() + 2, polygon.end(), cmpHull(polygon[0], polygon[1])); n   return; n } n vector<complex<double> > convexHull(vector<complex<double> >& polygon) { n   sortAntiClockwise(polygon); n   vector<complex<double> > hull; n   int sz = 0; n   for (int i = 0; i < (int)polygon.size(); i++) { n     while (sz >= 2 && pointOnLeft(polygon[i], hull[sz - 1], hull[sz - 2])) { n       hull.pop_back(); n       sz--; n     } n     sz++; n     hull.push_back(polygon[i]); n   } n   return hull; n } n vector<complex<double> > shift(const vector<complex<double> >& A, int pivot) { n   vector<complex<double> > ret; n   for (int c = pivot; c < pivot + (int)A.size(); c++) n     ret.push_back(A[c % A.size()]); n   return ret; n } n vector<complex<double> > A, B, all; n int n, m; n int main() { n   set<complex<double>, cmp> ss; n   int c, c2; n   scanf( %d , &n); n   int x, y; n   for (c = 0; c < n; c++) { n     scanf( %d%d , &x, &y); n     A.push_back(complex<double>(x, y)); n     all.push_back(complex<double>(x, y)); n     ss.insert(complex<double>(x, y)); n   } n   scanf( %d , &m); n   for (c = 0; c < m; c++) { n     scanf( %d%d , &x, &y); n     B.push_back(complex<double>(x, y)); n     all.push_back(complex<double>(x, y)); n     ss.insert(complex<double>(x, y)); n   } n   if (ss.size() < all.size()) { n     printf( NO n ); n     return 0; n   } n   reverse(A.begin(), A.end()); n   reverse(B.begin(), B.end()); n   int pivot = 0; n   for (c = 1; c < (int)A.size(); c++) n     if (A[c] < A[pivot]) pivot = c; n   A = shift(A, pivot); n   vector<complex<double> > hull = convexHull(all); n   if (hull.size() != A.size()) { n     printf( NO n ); n     return 0; n   } n   bool yes = true; n   for (c = 0; c < (int)hull.size(); c++) n     if (hull[c] < A[c] || A[c] < hull[c]) yes = false; n   if (yes) n     printf( YES n ); n   else n     printf( NO n ); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n const long long INF = 1e9 + 5; n const long long mod = 1e9 + 7; n const double pi = 3.1415926536; n map<char, int> mp, mp2; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   ios_base::sync_with_stdio(0); n   ; n   string n, m; n   cin >> n >> m; n   for (int i = 0; i < n.size(); i++) { n     mp[n[i]]++; n   } n   set<char> st; n   for (int i = 0; i < m.size(); i++) { n     st.insert(m[i]); n     mp2[m[i]]++; n     if (!mp[m[i]]) return cout <<  -1 n , 0; n   } n   long long ans = 0; n   set<char>::iterator it; n   for (it = st.begin(); it != st.end(); it++) { n     ans += min(mp[*it], mp2[*it]); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, l, r, K, p; n bool check(long long x) { n   long long t = K % (n + x); n   if (t == 0) t = n + x; n   if (t > min(p * 2, p + x)) return 0; n   if (t < p + max(0LL, x - n + p - 1)) return 0; n   return 1; n } n signed main() { n   scanf( %lld%lld%lld%lld , &n, &l, &r, &K); n   p = (r - l + 1 + n) % n; n   if (p > K) { n     return puts( -1 ), 0; n   } n   if (p == 0) p += n; n   if (n <= 100000) { n     for (long long i = n; i >= 0; i--) n       if (check(i)) { n         printf( %lld n , i); n         return 0; n       } n     puts( -1 ); n     return 0; n   } n   long long ans = 0; n   for (long long t = ceil((double)(K - 2 * p) / (double)(2 * n)); n        t * n + p <= K; t++) { n     long long y = K - n * t - p; n     long long x; n     if (t != 0) n       x = min((long long)floor((double)(n - p + y) / (1 + t)), n               (long long)y / t); n     else { n       x = (K - p + 1) + (n - p); n     } n     y = K - n * t - p - t * x; n     if (y < 0 || y > p || y > x || x > n) continue; n     ans = max(ans, x); n     y = K - n * t - p + 1; n     if (t != 0) n       x = min((long long)floor((double)(n - p + y) / (1 + t)), n               (long long)y / t); n     else { n       x = (K - p + 1) + (n - p); n     } n     y = K - n * t - p - t * x; n     if (y < 0 || y > p || y > x || x > n) continue; n     ans = max(ans, x); n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { n   while (a) { n     b %= a; n     swap(a, b); n   } n   return b; n } n struct Vector2D { n   long long x, y; n   Vector2D() {} n   Vector2D(long long x, long long y) : x(x), y(y) {} n }; n Vector2D operator-(const Vector2D& lhs, const Vector2D& rhs) { n   return Vector2D(lhs.x - rhs.x, lhs.y - rhs.y); n } n bool operator==(const Vector2D& lhs, const Vector2D& rhs) { n   return lhs.x == rhs.x && lhs.y == rhs.y; n } n Vector2D normalize(const Vector2D& v) { n   const long long g = gcd(abs(v.x), abs(v.y)); n   return Vector2D(v.x / g, v.y / g); n } n struct Vector2DHash { n   size_t operator()(const Vector2D& v) const { return v.x * 1000000007 + v.y; } n }; n class OrderByLength { n  public: n   OrderByLength(const vector<Vector2D>& targets, const Vector2D& base) n       : targets_(targets), base_(base) {} n   bool operator()(const int lhs_, const int rhs_) const { n     const Vector2D lhs = targets_[lhs_] - base_; n     const Vector2D rhs = targets_[rhs_] - base_; n     return lhs.x * lhs.x + lhs.y * lhs.y < rhs.x * rhs.x + rhs.y * rhs.y; n   } n  n  private: n   const vector<Vector2D>& targets_; n   const Vector2D& base_; n }; n bool check(const vector<Vector2D>& bears, const vector<Vector2D>& targets, n            vector<unordered_map<Vector2D, vector<int>, Vector2DHash> >& maps, n            const int target, const vector<int>& order, int& next, n            vector<int>& killed) { n   if (next == order.size()) return false; n   const int bear = order[next]; n   const vector<int>& onLine = n       maps[bear][normalize(targets[target] - bears[bear])]; n   for (int nextTarget : onLine) { n     if (target == nextTarget) { n       killed.push_back(target); n       return true; n     } n     if (find(killed.begin(), killed.end(), nextTarget) != killed.end()) n       continue; n     if (!check(bears, targets, maps, nextTarget, order, ++next, killed)) break; n   } n   return false; n } n int main() { n   int k, n; n   cin >> k >> n; n   vector<Vector2D> bears(k); n   vector<Vector2D> targets(n); n   for (int i = 0; i < k; ++i) cin >> bears[i].x >> bears[i].y; n   for (int i = 0; i < n; ++i) cin >> targets[i].x >> targets[i].y; n   vector<unordered_map<Vector2D, vector<int>, Vector2DHash> > maps(k); n   for (int i = 0; i < k; ++i) { n     for (int j = 0; j < n; ++j) n       maps[i][normalize(targets[j] - bears[i])].push_back(j); n     for (auto& p : maps[i]) n       sort(p.second.begin(), p.second.end(), OrderByLength(targets, bears[i])); n   } n   int res = 0; n   for (int i = 0; i < n; ++i) { n     bool danger = false; n     vector<int> order(k); n     iota(order.begin(), order.end(), 0); n     do { n       vector<int> killed; n       int next = 0; n       danger |= check(bears, targets, maps, i, order, next, killed); n     } while (!danger && next_permutation(order.begin(), order.end())); n     res += danger; n   } n   cout << res << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 7007; n const int MAXM = 300007; n const int INF = 1 << 29; n const int dir[][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; n char reactor[15][15], rescue[15][15]; n int N; n int table[15][15]; n bool isIn(int x, int y) { return x >= 1 && y >= 1 && x <= N && y <= N; } n struct Edge { n   int u, v, cap, flow; n   Edge *next, *back; n }; n struct Graph { n   Edge *head[MAXN], adj[MAXM]; n   Edge *cur[MAXN], *path[MAXN]; n   int lev[MAXN]; n   int n, k; n   int S, T; n   void init(int nn) { n     S = 0, T = nn; n     n = nn, k = 0; n     for (int i = 0; i <= n; i++) head[i] = NULL; n   } n   void set(int s, int t) { S = s, T = t; } n   void addedge(int u, int v, int cap) { n     adj[k].u = u; n     adj[k].v = v; n     adj[k].flow = 0; n     adj[k].cap = cap; n     adj[k].next = head[u]; n     head[u] = adj + k++; n   } n   void add(int u, int v, int cap) { n     addedge(u, v, cap); n     addedge(v, u, 0); n     head[u]->back = head[v]; n     head[v]->back = head[u]; n   } n   bool bfs() { n     queue<int> q; n     fill(lev, lev + n + 1, -1); n     lev[S] = 0; n     q.push(S); n     int u, v; n     while (!q.empty()) { n       u = q.front(); n       q.pop(); n       for (Edge *f = head[u]; f; f = f->next) { n         if (f->flow == f->cap) continue; n         v = f->v; n         if (lev[v] == -1) { n           lev[v] = lev[u] + 1; n           q.push(v); n           if (v == T) return true; n         } n       } n     } n     return (lev[T] != -1); n   } n   int dinic() { n     int u, v, pt; n     int flow = 0; n     while (bfs()) { n       for (int i = 0; i <= n; i++) cur[i] = head[i]; n       pt = 0; n       u = S; n       while (true) { n         if (u == T) { n           int delta = INF, minI = 0; n           for (int i = 0; i < pt; i++) { n             if (delta > path[i]->cap - path[i]->flow) { n               delta = path[i]->cap - path[i]->flow; n               minI = i; n             } n           } n           for (int i = 0; i < pt; i++) { n             path[i]->flow += delta; n             path[i]->back->flow -= delta; n           } n           u = path[pt = minI]->u; n           flow += delta; n         } n         Edge *f; n         for (f = cur[u]; f; f = f->next) { n           if (f->cap == f->flow) continue; n           v = f->v; n           if (lev[v] == lev[u] + 1) break; n         } n         cur[u] = f; n         if (f) { n           path[pt++] = f; n           u = f->v; n         } else { n           lev[u] = -1; n           if (!pt) break; n           u = path[--pt]->u; n         } n       } n     } n     return flow; n   } n } graph; n void bfs(int sx, int sy, int t) { n   for (int i = 1; i <= N; i++) { n     fill(table[i], table[i] + N + 1, INF); n   } n   queue<pair<int, int> > q; n   q.push(pair<int, int>(sx, sy)); n   table[sx][sy] = 0; n   while (!q.empty()) { n     pair<int, int> cur = q.front(); n     q.pop(); n     for (int i = 0; i < 4; i++) { n       int x = cur.first + dir[i][0]; n       int y = cur.second + dir[i][1]; n       if (!isIn(x, y) || !isdigit(reactor[x][y]) || table[x][y] != INF) { n         continue; n       } n       table[x][y] = table[cur.first][cur.second] + 1; n       q.push(pair<int, int>(x, y)); n     } n   } n } n int main() { n   int n, t; n   int S, T; n   scanf( %d%d , &n, &t); n   N = n; n   int sx, sy; n   for (int i = 1; i <= n; i++) { n     scanf(  %s , reactor[i] + 1); n     for (int j = 1; j <= n; j++) { n       if (reactor[i][j] ==  Z ) { n         sx = i; n         sy = j; n       } n     } n   } n   bfs(sx, sy, t); n   for (int i = 1; i <= n; i++) { n     scanf(  %s , rescue[i] + 1); n   } n   S = 0, T = (t + 1) * n * n + 1; n   graph.init(T); n   graph.set(S, T); n   for (int cur = 0; cur < t; cur++) { n     int add1 = cur * n * n; n     int add2 = (cur + 1) * n * n; n     for (int i = 1; i <= n; i++) { n       for (int j = 1; j <= n; j++) { n         if (!isdigit(reactor[i][j])) continue; n         if (isdigit(rescue[i][j]) && rescue[i][j] !=  0 ) { n           graph.add(add1 + (i - 1) * n + j, add2 + (i - 1) * n + j, n                     rescue[i][j] -  0 ); n           if (cur >= table[i][j]) continue; n         } n         if (isdigit(reactor[i][j]) && cur < table[i][j]) { n           if (cur + 1 < table[i][j]) { n             graph.add(add1 + (i - 1) * n + j, add2 + (i - 1) * n + j, INF); n           } n           for (int k = 0; k < 4; k++) { n             int x = i + dir[k][0]; n             int y = j + dir[k][1]; n             if (!isIn(x, y) || !isdigit(reactor[x][y])) continue; n             if (cur + 1 < table[x][y] || n                 table[x][y] == cur + 1 && isdigit(rescue[x][y])) n               graph.add(add1 + (i - 1) * n + j, add2 + (x - 1) * n + y, INF); n           } n         } n       } n     } n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       if (isdigit(reactor[i][j]) && reactor[i][j] !=  0 ) { n         graph.add(S, (i - 1) * n + j, reactor[i][j] -  0 ); n       } n       if (isdigit(rescue[i][j]) && rescue[i][j] !=  0 ) { n         graph.add(t * n * n + (i - 1) * n + j, T, rescue[i][j] -  0 ); n       } n     } n   } n   printf( %d n , graph.dinic()); n   return 0; n } n


#include <bits/stdc++.h> n  n using namespace std; n  n pair<int, int> dataarr[502]; n  n int main() { n     int n; n     cin >> n; n     int minin = n, maxin = 0; n     for (int i = 1; i <= n; i++) { n         cin >> dataarr[i].first; n         dataarr[i].second = i; n         if (dataarr[i].first < minin) { n             minin = dataarr[i].first; n         } n         if (dataarr[i].first > maxin) { n             maxin = dataarr[i].first; n         } n     } n     int indif = maxin - minin; n     sort(dataarr, dataarr + (n + 1)); n     for (int d = indif; d >= 0; d--) { n         for (int i = 1; i <= n; i++) { n             for (int j = i + 1; j <= n; j++) { n                 if (dataarr[j].first - dataarr[i].first == d) { n                     cout <<  ?   << dataarr[j].second <<     << dataarr[i].second << endl; n                     string s; n                     cin >> s; n                     if (s ==  Yes ) { n                         cout <<  !   << dataarr[j].second <<     << dataarr[i].second << endl; n                         return(0); n                     } n                 } n             } n         } n     } n     cout <<  ! 0 0  << endl; n     return(0); n } n


#include <bits/stdc++.h> n using namespace std; n void loadData(void); n int proc(void); n void printRes(int); n int left1, right1, left2, right2; n int pow2[31]; n int getRes(int, int, int, int); n int getMaxCLevelAndPos(int, int, int&); n bool lyb_debug = false; n int main(int argc, char** argv) { n   loadData(); n   printRes(proc()); n   return 0; n } n int getMaxCLevelAndPos(int l, int r, int& pos) { n   if (lyb_debug) { n     printf( ==check for (%d - %d) n n , l, r); n   } n   for (int i = 31 - 1; i >= 0; i--) n     if (r >= pow2[i] - 1 && l <= pow2[i] - 1) { n       pos = pow2[i] - 1; n       return i; n     } n   int min_pow2_pos; n   for (min_pow2_pos = 31 - 1; min_pow2_pos >= 0; min_pow2_pos--) n     if (l >= pow2[min_pow2_pos]) break; n   if (min_pow2_pos == -1) n     return -1; n   else { n     int ret = n         getMaxCLevelAndPos(l - pow2[min_pow2_pos], r - pow2[min_pow2_pos], pos); n     if (ret != -1) pos += pow2[min_pow2_pos]; n     return ret; n   } n   return -1; n } n int getRes(int l1, int r1, int l2, int r2) { n   if (r1 < l1 || r2 < l2) return 0; n   int maxCPos1 = -1; n   int maxCLevel1 = getMaxCLevelAndPos(l1, r1, maxCPos1); n   if (lyb_debug) { n     printf( ==res for l:%d r:%d : level:%d pos:%d n n , l1, r1, maxCLevel1, n            maxCPos1); n   } n   int maxCPos2 = -1; n   int maxCLevel2 = getMaxCLevelAndPos(l2, r2, maxCPos2); n   if (lyb_debug) { n     printf( ==res for l:%d r:%d : level:%d pos:%d n n , l2, r2, maxCLevel2, n            maxCPos2); n   } n   if (maxCLevel1 == -1 || maxCLevel2 == -1) { n     if (maxCLevel1 == -1 && maxCLevel2 == -1) return 1; n     if (maxCLevel1 == -1) return r2 - l2 == 0 ? 0 : 1; n     if (maxCLevel2 == -1) return r1 - l1 == 0 ? 0 : 1; n   } n   if (maxCLevel1 == maxCLevel2) { n     int res = min(r1 - maxCPos1, r2 - maxCPos2) + n               min(maxCPos1 - l1, maxCPos2 - l2) + 1; n     int newl1, newr1, newl2, newr2; n     if (maxCPos1 - l1 > r1 - maxCPos1) { n       newl1 = l1; n       newr1 = maxCPos1 - 1; n     } else { n       newl1 = maxCPos1 + 1; n       newr1 = r1; n     } n     if (maxCPos2 - l2 > r2 - maxCPos2) { n       newl2 = l2; n       newr2 = maxCPos2 - 1; n     } else { n       newl2 = maxCPos2 + 1; n       newr2 = r2; n     } n     res = max(res, getRes(newl1, newr1, newl2, newr2)); n     return res; n   } else if (maxCLevel1 > maxCLevel2) { n     return max(getRes(max(maxCPos1 - pow2[maxCLevel2 + 1] + 1, l1), n                       maxCPos1 - 1, l2, r2), n                getRes(maxCPos1 + 1, n                       min(r1, maxCPos1 + pow2[maxCLevel2 + 1] + 1), l2, r2)); n   } else { n     return max(getRes(l1, r1, max(maxCPos2 - pow2[maxCLevel1 + 1] + 1, l2), n                       maxCPos2 - 1), n                getRes(l1, r1, maxCPos2 + 1, n                       min(r2, maxCPos2 + pow2[maxCLevel1 + 1] + 1))); n   } n   return 0; n } n void initPow2(void) { n   pow2[0] = 1; n   for (int i = 1; i < 31; i++) pow2[i] = 2 * pow2[i - 1]; n   if (lyb_debug) { n     printf( ==pow2== n ); n     for (int i = 0; i < 31; i++) printf( %d  , pow2[i]); n     printf(  n n ); n   } n   return; n } n int proc(void) { n   initPow2(); n   return getRes(left1, right1, left2, right2); n } n void loadData(void) { n   scanf( %d%d%d%d , &left1, &right1, &left2, &right2); n   left1--; n   right1--; n   left2--; n   right2--; n   return; n } n void printRes(int res) { n   printf( %d n , res); n   return; n } n


#include <bits/stdc++.h> n using namespace std; n struct point { n   long long int x, y; n   point() {} n   point(long long int _x, long long int _y) { n     x = _x; n     y = _y; n   } n }; n bool cmp(const point &a, const point &b) { n   if (a.x == b.x) return a.y < b.y; n   return a.x < b.x; n } n struct freq { n   long long int x_freq; n   set<long long int> y_freq; n }; n freq freq_list[200005]; n point S[200010]; n long long int mod; n long long int fact_mod(long long int k) { n   long long int ans = 1; n   while (k > 0) { n     ans = (ans % mod * k % mod) % mod; n     --k; n   } n   return ans % mod; n } n int main() { n   int N; n   cin >> N; n   for (int i = 0; i < N; ++i) { n     int x; n     cin >> x; n     S[i] = point(x, i); n   } n   for (int i = 0; i < N; ++i) { n     int x; n     cin >> x; n     S[N + i] = point(x, i); n   } n   cin >> mod; n   sort(S, S + 2 * N, cmp); n   int last = -1, cnt = -1, m; n   for (int i = 0; i < 2 * N; ++i) { n     if (S[i].x != last) { n       ++cnt; n       last = S[i].x; n       freq_list[cnt].x_freq = 1; n       freq_list[cnt].y_freq.insert(S[i].y); n     } else { n       ++freq_list[cnt].x_freq; n       freq_list[cnt].y_freq.insert(S[i].y); n     } n   } n   long long int ans = 1, n; n   for (int i = 0; i <= cnt; ++i) { n     m = freq_list[i].x_freq - freq_list[i].y_freq.size(); n     n = freq_list[i].x_freq; n     int k = n; n     for (int j = 0; j < m; ++j) { n       ans = ((ans % mod) * ((n * (n - 1) / 2LL) % mod)) % mod; n       n -= 2; n     } n     ans = ((ans % mod) * (fact_mod(k - 2 * m) % mod)) % mod; n   } n   cout << ans % mod << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double EPS = 1e-10; n const double PI = acos(-1.0); n struct Edge { n   int to, cost; n   Edge(int t, int c) : to(t), cost(c) {} n   bool operator>(const Edge& rhs) const { return cost > rhs.cost; } n }; n long long N, S; n vector<int> dp, parent; n vector<vector<Edge>> G; n void dfs(int par, int node, int d) { n   parent[node] = par; n   dp[node] = 1; n   for (int i = 0; i < int((G[node]).size()); ++i) { n     int& to = G[node][i].to; n     if (to == par) continue; n     dfs(node, to, d + 1); n     dp[node] += dp[to]; n     S += 2 * dp[to] * (N - dp[to]) * G[node][i].cost; n   } n } n int main() { n   cin.tie(0); n   ios_base::sync_with_stdio(false); n   cin >> N; n   G.assign(N + 1, vector<Edge>()); n   vector<pair<int, Edge>> edges; n   for (int i = (0); i < (N - 1); ++i) { n     int a, b, c; n     cin >> a >> b >> c; n     G[a].push_back(Edge(b, c)); n     G[b].push_back(Edge(a, c)); n     edges.push_back(make_pair(a, Edge(b, c))); n   } n   dp.assign(N + 1, 0); n   parent.assign(N + 1, -1); n   S = 0; n   dfs(-1, 1, 0); n   int Q; n   cin >> Q; n   while (Q--) { n     int idx, c; n     cin >> idx >> c; n     --idx; n     Edge& e = edges[idx].second; n     int a = edges[idx].first, b = e.to; n     if (parent[b] == a) swap(a, b); n     S -= (e.cost - c) * 2 * dp[a] * (N - dp[a]); n     cout << fixed << setprecision(7) << 3 * S * 1. / (N * (N - 1)) << endl; n     e.cost = c; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string a, b; n int k; n int main() { n   cin >> a; n   k = 1; n   if (a.size() % 2 == 0) { n     while (k != a.size() + 1) { n       if (k % 2 != 0) { n         b += a[a.size() - k / 2 - 1]; n       } else { n         b += a[k / 2 - 1]; n       } n       k++; n     } n   } else { n     while (k != a.size() + 1) { n       if (k % 2 == 0) { n         b += a[a.size() - k / 2]; n       } else { n         b += a[k / 2]; n       } n       k++; n     } n   } n   reverse(b.begin(), b.end()); n   cout << b; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2005, INF = 1e9; n int dx[] = {1, -1, 0, 0}; n int dy[] = {0, 0, 1, -1}; n pair<int, int> dis[N][N]; n bool done[N][N]; n string g[N]; n int n, m, sx, sy, L, R; n bool valid(int x, int y) { n   return x >= 0 && x < n && y >= 0 && y < m && g[x][y] ==  . ; n } n void solve(bool fs) { n   priority_queue<pair<pair<int, int>, pair<int, int>>, n                  vector<pair<pair<int, int>, pair<int, int>>>, n                  greater<pair<pair<int, int>, pair<int, int>>>> n       pq; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) dis[i][j] = {INF, INF}; n   dis[sx][sy] = {0, 0}; n   pq.push({{0, 0}, {sx, sy}}); n   while (!pq.empty()) { n     int x = pq.top().second.first; n     int y = pq.top().second.second; n     pair<int, int> d = pq.top().first; n     pq.pop(); n     if (dis[x][y] < d) continue; n     if (fs && dis[x][y].first <= L && dis[x][y].second <= R) done[x][y] = 1; n     if ((!fs) && dis[x][y].first <= R && dis[x][y].second <= L) done[x][y] = 1; n     for (int i = 0; i < 4; i++) { n       int nx = x + dx[i], ny = y + dy[i]; n       if (valid(nx, ny)) { n         pair<int, int> p = {0, 0}; n         if (dy[i] < 0) { n           if (fs) n             p = {1, 0}; n           else n             p = {0, 1}; n         } n         if (dy[i] > 0) { n           if (fs) n             p = {0, 1}; n           else n             p = {1, 0}; n         } n         pair<int, int> np = {d.first + p.first, d.second + p.second}; n         if (np < dis[nx][ny]) { n           dis[nx][ny] = np; n           pq.push({np, {nx, ny}}); n         } n       } n     } n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m; n   cin >> sx >> sy; n   sx--; n   sy--; n   cin >> L >> R; n   for (int i = 0; i < n; i++) cin >> g[i]; n   solve(0); n   solve(1); n   int ans = 0; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) ans += done[i][j]; n   cout << ans << endl; n   return 0; n } n