#include <bits/stdc++.h> n using namespace std; n const int INF = 1000000009; n const double PI = acos(-1.0); n const double eps = 1e-8; n const int MAXN = 0; n const int MAXM = 0; n bool a[5002]; n int i, j, k, n, h, m, id, p, hesh; n map<int, int> M; n char c; n int ans = 0; n int main() { n   cin >> h >> m >> n; n   for (i = 0; i < n; i++) { n     cin >> c; n     if (c ==  - ) { n       cin >> id; n       a[M[id]] = 0; n     } else { n       cin >> id >> hesh; n       while (a[hesh] != 0) { n         hesh = (hesh + m) % h; n         ans++; n       } n       a[hesh] = true; n       M[id] = hesh; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, i, a, b, x, y, ans = 0; n   scanf( %d , &n); n   printf( YES n ); n   for (int i = 1; i <= n; i++) { n     scanf( %d %d %d %d , &a, &b, &x, &y); n     x = min(a, x); n     y = min(y, b); n     ans = abs(x) % 2; n     ans = ans * 2 + (abs(y) % 2); n     printf( %d n , ans + 1); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const long long LINF = 0x3f3f3f3f3f3f3f3fll; n vector<vector<string> > dp; n int n, m; n string resp =   ; n vector<string> a; n vector<set<char> > occr; n string solve(int at, int estado) { n   if (at == m) { n     return   ; n   } n   if (dp[at][estado] !=   ) { n     return dp[at][estado]; n   } n   for (char c : occr[at]) { n     bool pode = true; n     for (int I = 0; I < n; I++) { n       if (a[I][at] != c && (estado & (1 << I))) { n         pode = false; n       } n     } n     if (!pode) { n       continue; n     } n     int temp = estado; n     for (int I = 0; I < n; I++) { n       if (a[I][at] != c) { n         temp |= (1 << I); n       } n     } n     string res = solve(at + 1, temp); n     if (res !=    ) { n       return dp[at][estado] = c + res; n     } n   } n   return dp[at][estado] =    ; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int t; n   cin >> t; n   while (t--) { n     cin >> n >> m; n     a.clear(); n     a.resize(n); n     occr.clear(); n     occr.resize(m); n     dp.clear(); n     dp.resize(11, vector<string>(1030,   )); n     for (int I = 0; I < n; I++) { n       cin >> a[I]; n     } n     for (int I = 0; I < n; I++) { n       for (int J = 0; J < m; J++) { n         occr[J].insert(a[I][J]); n       } n     } n     string resp = solve(0, 0); n     if (resp ==    ) { n       cout << -1 <<   n ; n     } else { n       cout << resp <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MX = 1e5 + 10; n long long A[MX]; n long long cnt[MX]; n vector<long long> pos[MX]; n long long ans[MX]; n void init(long long n) { n   for (long long i = 0; i <= n + 2; i++) cnt[i] = 0; n   for (long long i = 0; i <= n + 2; i++) pos[i].clear(); n   for (long long i = 0; i <= n + 2; i++) ans[i] = -1; n } n void salida(long long n, long long val) { n   for (long long i = 0; i < n; i++) n     if (ans[i] == -1) ans[i] = val; n   cout <<  YES n ; n   for (long long i = 0; i < n; i++) cout << ans[i] <<    ; n   cout <<   n ; n } n void solve() { n   long long n, fijo, tot, x; n   cin >> n >> fijo >> tot; n   long long nfijo = tot - fijo; n   init(n); n   set<long long> s; n   for (long long i = 0; i < n; i++) { n     cin >> x; n     A[i] = x; n     cnt[x]++; n     pos[x].push_back(i); n     s.insert(x); n   } n   long long maxNum = n + 1; n   priority_queue<pair<long long, long long> > pq; n   for (long long i = 1; i <= maxNum; i++) { n     if (cnt[i] != 0) pq.push({cnt[i], i}); n   } n   while (!pq.empty() && fijo != 0) { n     long long cn = pq.top().first; n     long long val = pq.top().second; n     pq.pop(); n     cnt[val]--; n     if (cnt[val] != 0) pq.push({cnt[val], val}); n     long long sz = pos[val].size(); n     ans[pos[val][sz - 1]] = val; n     pos[val].erase(pos[val].begin() + sz - 1); n     fijo--; n   } n   if (fijo > 0) { n     cout <<  NO n ; n     return; n   } n   long long numExtra = -1; n   for (long long i = 1; i <= maxNum; i++) { n     if (s.find(i) == s.end()) numExtra = i; n   } n   if (nfijo == 0) { n     salida(n, numExtra); n     return; n   } n   long long falta = 0; n   for (long long i = 1; i <= maxNum; i++) falta += cnt[i]; n   long long mxSize = pq.top().first; n   vector<long long> posi; n   while (!pq.empty()) { n     long long val = pq.top().second; n     pq.pop(); n     for (long long xx : pos[val]) posi.push_back(xx); n   } n   if (mxSize <= falta - mxSize) { n     long long sz = posi.size(); n     long long mid = sz / 2; n     for (long long i = 0; i < sz; i++) { n       if (nfijo == 0) break; n       long long cur = i; n       long long nxt = (i + mid) % sz; n       ans[posi[cur]] = A[posi[nxt]]; n       nfijo--; n     } n     salida(n, numExtra); n   } else { n     reverse(posi.begin(), posi.end()); n     long long mxPos = falta - mxSize; n     if (mxPos * 2 < nfijo) { n       cout <<  NO n ; n       return; n     } n     long long sz = posi.size(); n     for (long long i = 0; i < sz; i++) { n       if (nfijo == 0) break; n       long long cur = i; n       long long nxt = i + mxPos; n       if (nfijo == 0) break; n       ans[posi[cur]] = A[posi[nxt]]; n       nfijo--; n       if (nfijo == 0) break; n       ans[posi[nxt]] = A[posi[cur]]; n       nfijo--; n     } n     salida(n, numExtra); n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cout.precision(10); n   cout << fixed; n   long long t; n   cin >> t; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-9; n const double pi = acos(-1.0); n const double dinf = 1e250; n const long long INF = (long long)(2e18); n const int inf = (int)(1e9 + 7); n struct myt { n   int d, s, t; n }; n void out(int a, int b) { cout << a <<     << b <<   n ; } n vector<int> ind; n vector<vector<int> > g; n int32_t main() { n   ios_base::sync_with_stdio(0); n   cout << fixed << setprecision(10); n   cin.tie(0); n   int n; n   cin >> n; n   vector<myt> ar(n); n   for (int i = 0; i < n; ++i) { n     cin >> ar[i].d; n     ar[i].s = 2 * i + 1; n     ar[i].t = 2 * i + 2; n   } n   sort(begin(ar), end(ar), [](myt a, myt b) { return a.d > b.d; }); n   g.resize(2 * n); n   ind.resize(2 * n, 0); n   for (int i = 0; i < n; ++i) { n     int d = ar[i].d; n     if (ind[i] == 0) n       ind[i] = ar[i].s; n     else { n       --d; n       g[i].push_back(ar[i].s); n     } n     if (d == 0) { n       out(ar[i].s, ar[i].t); n     } else if (ind[i + d] == 0) { n       ind[i + d] = ar[i].t; n     } else { n       g[i + d - 1].push_back(ar[i].t); n     } n   } n   for (int i = 0; i < g.size(); ++i) { n     if (ind[i] == 0) continue; n     for (auto& it : g[i]) out(ind[i], it); n   } n   for (int i = 0; i < ind.size() - 1; ++i) { n     if (ind[i] == 0) continue; n     if (ind[i + 1] == 0) continue; n     out(ind[i], ind[i + 1]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 100010; n long long quick_Mod(long long n) { n   long long ans = 1; n   long long x = 2; n   while (n) { n     if (n & 1) ans = ans * x % 1000000007; n     n = n >> 1; n     x = x * x % 1000000007; n   } n   return ans; n } n void ex_gcd(long long a, long long b, long long &gcd, long long &x, n             long long &y) { n   if (b == 0) { n     gcd = a; n     y = 0; n     x = 1; n     return; n   } else { n     ex_gcd(b, a % b, gcd, y, x); n     y -= x * (a / b); n   } n } n long long inverse(long long a) { n   long long gcd, x, y; n   ex_gcd(a, 1000000007, gcd, x, y); n   if (gcd == 1) n     return (x % 1000000007 + 1000000007) % 1000000007; n   else n     return -1; n } n int main() { n   char s[MAX]; n   long long k; n   scanf( %s , s); n   scanf( %I64d , &k); n   long long len = strlen(s); n   long long ans = 0; n   for (int i = 0; i < len; i++) { n     if (s[i] ==  0  || s[i] ==  5 ) { n       ans += quick_Mod(i) * inverse(quick_Mod(len) - 1) % 1000000007 * n              (quick_Mod(k * len) - 1) % 1000000007; n     } n   } n   printf( %I64d n , (ans % 1000000007 + 1000000007) % 1000000007); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, Q; n set<int> cut[2]; n map<int, int> mx[2]; n long long getmx() { n   map<int, int>::iterator it; n   it = mx[0].end(); n   it--; n   int u = (*it).first; n   it = mx[1].end(); n   it--; n   int v = (*it).first; n   return 1ll * u * v; n } n void Insert(int t, int x) { n   int u, v; n   set<int>::iterator it = cut[t].lower_bound(x); n   v = *it; n   it--; n   u = *it; n   mx[t][v - u]--; n   if (mx[t][v - u] == 0) mx[t].erase(v - u); n   cut[t].insert(x); n   mx[t][x - u]++; n   mx[t][v - x]++; n } n int main() { n   char t; n   int x; n   scanf( %d %d %d , &n, &m, &Q); n   cut[0].insert(0); n   cut[0].insert(n); n   mx[0][n]++; n   cut[1].insert(0); n   cut[1].insert(m); n   mx[1][m]++; n   for (int i = 1; i <= Q; i++) { n     scanf(  %c %d , &t, &x); n     if (t ==  V ) Insert(0, x); n     if (t ==  H ) Insert(1, x); n     printf( %I64d n , getmx()); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int IINF = 0x3f3f3f3f; n const long long LINF = 0x3f3f3f3f3f3f3f3fll; n const double DINF = numeric_limits<double>::infinity(); n const int MOD = 1000000007; n const double EPS = 1e-9; n const double PI = acos(-1.0); n const int DX[] = {1, 0, -1, 0, 1, -1, 1, -1}; n const int DY[] = {0, 1, 0, -1, 1, -1, -1, 1}; n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n long long sqr(long long x) { return x * x; } n long long sqr(int x) { return (long long)x * x; } n double sqr(double x) { return x * x; } n long double sqr(long double x) { return x * x; } n mt19937 mmtw(960172); n long long rnd(long long x, long long y) { n   static uniform_int_distribution<long long> d; n   return d(mmtw) % (y - x + 1) + x; n } n const int N = 1011; n struct Line { n   vector<int> cs; n   int si0; n }; n struct Seg { n   int x, y1, y2; n }; n int vc1, vc2; n Seg vs1[N], vs2[N]; n vector<int> e[N]; n int p2[N]; n int p1[N]; n bool vis1[N]; n bool ok1[N], ok2[N]; n bool dfs(int v) { n   if (vis1[v]) return false; n   vis1[v] = true; n   for (int nv : e[v]) { n     if (p2[nv] == -1) { n       p2[nv] = v; n       p1[v] = nv; n       return true; n     } else if (dfs(p2[nv])) { n       p2[nv] = v; n       p1[v] = nv; n       return true; n     } n   } n   return false; n } n void dfs2(int v) { n   if (vis1[v]) return; n   vis1[v] = 1; n   for (int nv : e[v]) n     if (p2[nv] != -1) dfs2(p2[nv]); n } n int main() { n   memset(p1, -1, sizeof(p1)); n   memset(p2, -1, sizeof(p2)); n   ios::sync_with_stdio(false); n   int n; n   scanf( %d , &n); n   map<int, Line> mx, my; n   for (int i = 0; i < n; ++i) { n     int x, y; n     scanf( %d%d , &x, &y); n     mx[x].cs.push_back(y); n     my[y].cs.push_back(x); n   } n   for (auto& p : mx) { n     int x = p.first; n     Line& l = p.second; n     sort((l.cs).begin(), (l.cs).end()); n     l.si0 = vc1; n     for (int i = 0; i < ((int)((l.cs).size())) - 1; ++i) n       vs1[vc1++] = {x, l.cs[i], l.cs[i + 1]}; n   } n   for (auto& p : my) { n     int y = p.first; n     Line& l = p.second; n     sort((l.cs).begin(), (l.cs).end()); n     l.si0 = vc2; n     for (int i = 0; i < ((int)((l.cs).size())) - 1; ++i) n       vs2[vc2++] = {y, l.cs[i], l.cs[i + 1]}; n   } n   for (int i = 0; i < vc1; ++i) n     for (int j = 0; j < vc2; ++j) { n       Seg s1 = vs1[i]; n       Seg s2 = vs2[j]; n       if (s1.y1 < s2.x && s2.x < s1.y2) n         if (s2.y1 < s1.x && s1.x < s2.y2) { n           e[i].push_back(j); n         } n     } n   while (true) { n     bool was = false; n     memset(vis1, 0, sizeof(bool) * vc1); n     for (int i = 0; i < vc1; ++i) n       if (p1[i] == -1 && dfs(i)) was = true; n     if (!was) break; n   } n   memset(vis1, 0, sizeof(bool) * vc1); n   for (int i = 0; i < vc1; ++i) n     if (p1[i] == -1) dfs2(i); n   for (int i = 0; i < vc1; ++i) n     if (vis1[i]) ok1[i] = 1; n   for (int i = 0; i < vc2; ++i) n     if (p2[i] == -1 || !vis1[p2[i]]) ok2[i] = 1; n   struct Ans { n     int x1, y1, x2, y2; n   }; n   vector<Ans> ans; n   for (auto const& p : my) { n     int y = p.first; n     Line const& l = p.second; n     int i = 0; n     while (i < ((int)((l.cs).size()))) { n       int j = i; n       while (j < ((int)((l.cs).size())) - 1) { n         int id = l.si0 + j; n         if (ok2[id]) n           ++j; n         else n           break; n       } n       ans.push_back({l.cs[i], y, l.cs[j], y}); n       i = j + 1; n     } n   } n   cout << ((int)((ans).size())) <<   n ; n   for (auto s : ans) n     cout << s.x1 <<     << s.y1 <<     << s.x2 <<     << s.y2 <<   n ; n   ans.clear(); n   for (auto const& p : mx) { n     int x = p.first; n     Line const& l = p.second; n     int i = 0; n     while (i < ((int)((l.cs).size()))) { n       int j = i; n       while (j < ((int)((l.cs).size())) - 1) { n         int id = l.si0 + j; n         if (ok1[id]) n           ++j; n         else n           break; n       } n       ans.push_back({x, l.cs[i], x, l.cs[j]}); n       i = j + 1; n     } n   } n   cout << ((int)((ans).size())) <<   n ; n   for (auto s : ans) n     cout << s.x1 <<     << s.y1 <<     << s.x2 <<     << s.y2 <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n template <class T> n std::string toStr(T t); n using namespace std; n int main() { n   int statA[4]; n   int statB[4]; n   for (__typeof(4) i = 0; i < 4; i++) { n     cin >> statA[i]; n     cin >> statB[i]; n   } n   int pos[4][4] = {{0, 1, 2, 3}, {1, 0, 2, 3}, {0, 1, 3, 2}, {1, 0, 3, 2}}; n   int res[4] = {0, 0, 0, 0}; n   for (__typeof(4) p = 0; p < 4; p++) { n     if (statA[pos[p][0]] > statB[pos[p][2]] && n         statB[pos[p][1]] > statA[pos[p][3]]) { n       res[p] = 1; n     } n     if (statA[pos[p][0]] < statB[pos[p][2]] && n         statB[pos[p][1]] < statA[pos[p][3]]) { n       res[p] = 2; n     } n   } n   if ((res[0] == 1 && res[2] == 1) || (res[1] == 1 && res[3] == 1)) { n     cout <<  Team 1  << endl; n     return 0; n   } n   if ((res[0] == 2 || res[2] == 2) && (res[1] == 2 || res[3] == 2)) { n     cout <<  Team 2  << endl; n     return 0; n   } n   cout <<  Draw  << endl; n } n template <class T> n string toStr(T t) { n   std::stringstream strstream; n   string resStr; n   strstream << t; n   strstream >> resStr; n   return resStr; n } n


#include <bits/stdc++.h> n using namespace std; n inline int scan() { n   int x; n   scanf( %d , &x); n   return x; n } n struct data { n   int first; n   int edge_used; n   int prev; n   data() { n     first = 0; n     edge_used = 0; n     prev = 0; n   } n }; n vector<pair<int, int> > v[100005]; n bool edge[100005] = {0}; n pair<int, int> edges[100005]; n data nodes[100005]; n bool visited[100005] = {0}; n bool used[100005] = {0}; n int dist[100005]; n int main() { n   int n = scan(), m = scan(); n   for (int i = 1; i <= m; i++) { n     int x = scan(), y = scan(), z = scan(); n     v[x].push_back(make_pair(y, i)); n     v[y].push_back(make_pair(x, i)); n     edge[i] = z; n     edges[i] = make_pair(x, y); n   } n   queue<int> q; n   q.push(1); n   visited[1] = 1; n   for (int i = 0; i <= n; i++) dist[i] = 999999; n   dist[1] = 0; n   bool aslivisit[100001] = {0}; n   while (!q.empty()) { n     int node = q.front(); n     aslivisit[node] = 1; n     q.pop(); n     int x = v[node].size(); n     int num = nodes[node].first; n     for (int i = 0; i < x; i++) { n       int next = v[node][i].first; n       if (aslivisit[next]) continue; n       int num2 = num; n       if (edge[v[node][i].second] == 0) { n         num2++; n       } n       if (!visited[next]) { n         visited[next] = 1; n         q.push(next); n         nodes[next].first = num2; n         nodes[next].edge_used = v[node][i].second; n         nodes[next].prev = node; n         dist[next] = dist[node] + 1; n       } else if (dist[next] >= dist[node] + 1) { n         if (num2 < nodes[next].first) { n           nodes[next].first = num2; n           nodes[next].edge_used = v[node][i].second; n           nodes[next].prev = node; n         } n       } n     } n   } n   int cur = n; n   while (cur != 0) { n     used[nodes[cur].edge_used] = 1; n     cur = nodes[cur].prev; n   } n   int counter = 0; n   queue<pair<pair<int, int>, bool> > ans; n   for (int i = 1; i <= m; i++) { n     if (!used[i] && edge[i] == 1) { n       counter++; n       ans.push(make_pair(edges[i], 0)); n     } else if (used[i] && edge[i] == 0) { n       counter++; n       ans.push(make_pair(edges[i], 1)); n     } n   } n   printf( %d n , counter); n   while (!ans.empty()) { n     printf( %d %d %d n , ans.front().first.first, ans.front().first.second, n            ans.front().second); n     ans.pop(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MX = 256; n vector<vector<vector<vector<vector<int> > > > > dp; n char grid[MX][MX]; n int R, C, mxmask, MOD = 1e9 + 7; n int ison(int x, int bit) { n   if ((x & (1 << bit))) return 1; n   return 0; n } n int turnoff(int x, int bit) { n   if ((x & (1 << bit))) x ^= (1 << bit); n   return x; n } n int turnon(int x, int bit) { n   x |= (1 << bit); n   return x; n } n int calc(int row, int mask, int col, int left, int tok) { n   if (col == C) return calc(row + 1, mask, 0, 0, tok); n   if (row == R) return 1; n   int &ret = dp[row][mask][col][left][tok]; n   if (ret != -1) return ret; n   ret = 0; n   if (grid[row][col] ==  x ) { n     return ret = calc(row, turnoff(mask, col), col + 1, 0, tok); n   } n   ret += calc(row, turnon(mask, col), col + 1, 1, tok); n   ret %= MOD; n   if (left || ison(mask, col)) { n     ret += calc(row, mask, col + 1, left, tok); n     ret %= MOD; n   } else { n     if (!tok) ret += calc(row, mask, col + 1, left, 1); n     ret %= MOD; n   } n   return ret; n } n int main() { n   cin >> R >> C; n   for (int j = 0; j < R; j++) { n     for (int i = 0; i < C; i++) { n       if (R >= C) n         cin >> grid[j][i]; n       else n         cin >> grid[i][j]; n     } n   } n   if (R < C) swap(R, C); n   mxmask = (1 << C); n   dp.resize(R + 2); n   for (auto &pp : dp) { n     pp.resize(mxmask); n     for (auto &qq : pp) { n       qq.resize(C + 2); n       for (auto &ll : qq) { n         ll.resize(2); n         for (auto &nn : ll) { n           nn.resize(2); n           nn[0] = nn[1] = -1; n         } n       } n     } n   } n   cout << calc(0, 0, 0, 0, 0); n } n


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


#include <bits/stdc++.h> n using namespace std; n template <class T1> n void deb(T1 e1) { n   cout << e1 << endl; n } n template <class T1, class T2> n void deb(T1 e1, T2 e2) { n   cout << e1 <<     << e2 << endl; n } n template <class T1, class T2, class T3> n void deb(T1 e1, T2 e2, T3 e3) { n   cout << e1 <<     << e2 <<     << e3 << endl; n } n template <class T1, class T2, class T3, class T4> n void deb(T1 e1, T2 e2, T3 e3, T4 e4) { n   cout << e1 <<     << e2 <<     << e3 <<     << e4 << endl; n } n template <class T1, class T2, class T3, class T4, class T5> n void deb(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5) { n   cout << e1 <<     << e2 <<     << e3 <<     << e4 <<     << e5 << endl; n } n template <class T1, class T2, class T3, class T4, class T5, class T6> n void deb(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5, T6 e6) { n   cout << e1 <<     << e2 <<     << e3 <<     << e4 <<     << e5 <<     << e6 n        << endl; n } n struct data { n   int x, y; n }; n data point[110]; n int num[110]; n int main() { n   memset(num, 0, sizeof(num)); n   int i, j, t, mark = 1, x1 = 0, y1 = 0; n   for (i = 0; i < 8; i += 2) { n     scanf( %d%d , &point[i].x, &point[i].y); n     scanf( %d%d , &point[i + 1].x, &point[i + 1].y); n     if (point[i].x == point[i + 1].x && point[i].y != point[i + 1].y) { n       x1++; n     } else if (point[i].y == point[i + 1].y && point[i].x != point[i + 1].x) { n       y1++; n     } else n       mark = 0; n   } n   if ((x1 != 2)) mark = 0; n   if (y1 != 2) mark = 0; n   for (i = 0; i < 8; i++) { n     for (j = 0; j < 8; j++) { n       if (point[i].x == point[j].x && point[i].y == point[j].y) { n         num[i]++; n       } n     } n   } n   for (i = 0; i < 8; i++) { n     if (num[i] != 2) { n       mark = 0; n     } n   } n   if (mark == 0) n     printf( NO ); n   else n     printf( YES ); n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T, bool MIN> n struct ConvexHullTrickAddMonotone { n   using Line = pair<T, T>; n   deque<Line> deq; n   inline bool check(const Line &a, const Line &b, const Line &c) { n     return (b.first - a.first) * (c.second - b.second) >= n            (b.second - a.second) * (c.first - b.first); n   } n   inline T f(const Line &a, const T x) { return a.first * x + a.second; } n   void add_line(T a, T b) { n     if (!MIN) a = -a, b = -b; n     Line line(a, b); n     if (deq.empty()) return (void)deq.emplace_back(line); n     if (deq.front().first <= a) { n       while (deq.size() >= 2 && check(line, deq.front(), deq[1])) n         deq.pop_front(); n       deq.emplace_front(line); n     } else { n       while (deq.size() >= 2 && check(deq[deq.size() - 2], deq.back(), line)) n         deq.pop_back(); n       deq.emplace_back(line); n     } n   } n   T query_monotone_inc(T x) { n     while (deq.size() >= 2 && f(deq.front(), x) >= f(deq[1], x)) n       deq.pop_front(); n     T res = f(deq.front(), x); n     return MIN ? res : -res; n   } n   T query_monotone_dec(T x) { n     while (deq.size() >= 2 && f(deq.back(), x) >= f(deq[deq.size() - 2], x)) n       deq.pop_back(); n     T res = f(deq.back(), x); n     return MIN ? res : -res; n   } n }; n const long long INF = 1LL << 60; n template <class T> n inline bool chmin(T &a, T b) { n   if (a > b) { n     a = b; n     return 1; n   } n   return 0; n } n int main() { n   int n, m; n   cin >> n >> m; n   int p; n   cin >> p; n   vector<int> d(n); n   for (int i = 0; i < n - 1; i++) { n     int x; n     cin >> x; n     d[i + 1] = d[i] + x; n   } n   vector<long long> x(m + 1); n   for (int i = 0; i < m; i++) { n     int h, t; n     cin >> h >> t; n     h--; n     x[i + 1] = t - d[h] + 1e9; n   } n   sort(begin(x), end(x)); n   vector<vector<long long>> dp(p + 1, vector<long long>(m + 1, INF)); n   for (int i = 0; i <= p; i++) dp[i][0] = 0; n   for (int i = 0; i < p; i++) { n     ConvexHullTrickAddMonotone<long long, true> cht; n     for (int j = 1; j <= m; j++) { n       cht.add_line(-(j - 1), dp[i][j - 1]); n       dp[i + 1][j] = cht.query_monotone_inc(x[j]) + x[j] * j; n     } n   } n   long long ans = dp[p][m]; n   for (int i = 0; i <= m; i++) ans -= x[i]; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1000000000000; n long long A, B, n, l, t, m, r; n bool check(long long len) { n   long long sum = A + (B * (l - 1)); n   long long mx = A + (B * (len - 1)); n   if (mx <= t && mx + sum <= 2 * m * t / (len - l + 1)) n     return true; n   else n     return false; n } n long long BS(long long L, long long R) { n   if (L > R) return r; n   long long mid = L + R >> 1; n   if (check(mid)) { n     r = mid; n     return BS(mid + 1, R); n   } else n     return BS(L, mid - 1); n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> A >> B >> n; n   while (n--) { n     cin >> l >> t >> m; n     r = l - 1; n     BS(l, INF); n     if (r >= l) n       cout << r <<   n ; n     else n       cout << -1 <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void maxtt(T& t1, T t2) { n   t1 = max(t1, t2); n } n template <typename T> n void mintt(T& t1, T t2) { n   t1 = min(t1, t2); n } n bool debug = 0; n int n, m, k; n int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; n string direc =  URDL ; n const long long MOD2 = (long long)1000000007 * (long long)1000000007; n long long ln, lk, lm; n void etp(bool f = 0) { n   puts(f ?  YES  :  NO ); n   exit(0); n } n void addmod(int& x, int y, int mod = 1000000007) { n   x += y; n   if (x >= mod) x -= mod; n   if (x < 0) x += mod; n   assert(x >= 0 && x < mod); n } n void et(int x = -1) { n   printf( %d n , x); n   exit(0); n } n long long fastPow(long long x, long long y, int mod = 1000000007) { n   long long ans = 1; n   while (y > 0) { n     if (y & 1) ans = (x * ans) % mod; n     x = x * x % mod; n     y >>= 1; n   } n   return ans; n } n long long gcd1(long long x, long long y) { return y ? gcd1(y, x % y) : x; } n char s[3001335]; n int id[3001335], cnt; n int mt[3001335]; n inline vector<int> cal(int l, int r) { n   vector<int> ans; n   if (l + 2 == r) { n     assert(s[l + 1] ==  * ); n     ans.push_back(id[l + 1]); n     return ans; n   } n   bool fg = 1; n   for (int i = l + 1; i < r;) { n     vector<int> tmp = cal(i, mt[i]); n     if (fg) { n       if (tmp.size() > ans.size()) swap(ans, tmp); n       for (int z : tmp) ans.push_back(z); n     } else { n       if (tmp.size() < ans.size()) swap(ans, tmp); n     } n     i = mt[i] + 1; n     if (i < r) { n       assert(s[i] ==  P  || s[i] ==  S ); n       if (s[i] ==  S ) fg = 0; n       i++; n     } n   } n   return ans; n } n void fmain(int tid) { n   int r; n   scanf( %d , &r); n   n = 0; n   cnt = 0; n   while (1) { n     char x = getchar(); n     if (x ==   n ) break; n     if (x ==    ) continue; n     if (x ==  * ) { n       s[++n] =  ( ; n       s[++n] =  * ; n       id[n] = ++cnt; n       s[++n] =  ) ; n     } else n       s[++n] = x; n   } n   vector<int> vp; n   for (int(i) = 1; (i) <= (int)(n); (i)++) { n     if (s[i] ==  ( ) n       vp.push_back(i); n     else if (s[i] ==  ) ) { n       int x = vp.back(); n       vp.pop_back(); n       mt[x] = i; n       mt[i] = x; n     } n   } n   vector<int> ans = cal(1, n); n   vector<bool> vis(cnt + 1, 0); n   for (int z : ans) vis[z] = 1; n   printf( REVOLTING ); n   for (int(i) = 1; (i) <= (int)(cnt); (i)++) n     if (vis[i]) { n       printf(  %lld , (long long)r * ans.size()); n     } else n       printf(  0 ); n   puts(  ); n } n int main() { n   int t = 1; n   scanf( %d , &t); n   for (int(i) = 1; (i) <= (int)(t); (i)++) { n     fmain(i); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(int argc, char** argv) { n   int n, k; n   scanf( %d %d , &n, &k); n   if (k > n * 3) { n     printf( %d , 0); n   } else { n     printf( %d , 3 * n - k); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline void read(int &x) { n   int v = 0, f = 1; n   char c = getchar(); n   while (!isdigit(c) && c !=  - ) c = getchar(); n   if (c ==  - ) n     f = -1; n   else n     v = (c & 15); n   while (isdigit(c = getchar())) v = (v << 1) + (v << 3) + (c & 15); n   x = v * f; n } n inline void read(long long &x) { n   long long v = 0ll, f = 1ll; n   char c = getchar(); n   while (!isdigit(c) && c !=  - ) c = getchar(); n   if (c ==  - ) n     f = -1; n   else n     v = (c & 15); n   while (isdigit(c = getchar())) v = (v << 1) + (v << 3) + (c & 15); n   x = v * f; n } n inline void readc(char &x) { n   char c; n   while (((c = getchar()) ==    ) || c ==   n ) n     ; n   x = c; n } n inline void writes(string s) { puts(s.c_str()); } n inline void writeln() { writes(  ); } n inline void writei(int x) { n   if (x < 0) { n     putchar( - ); n     x = abs(x); n   } n   if (!x) putchar( 0 ); n   char a[25]; n   int top = 0; n   while (x) { n     a[++top] = (x % 10) +  0 ; n     x /= 10; n   } n   while (top) { n     putchar(a[top]); n     top--; n   } n } n inline void writell(long long x) { n   if (x < 0) { n     putchar( - ); n     x = abs(x); n   } n   if (!x) putchar( 0 ); n   char a[25]; n   int top = 0; n   while (x) { n     a[++top] = (x % 10) +  0 ; n     x /= 10; n   } n   while (top) { n     putchar(a[top]); n     top--; n   } n } n int n, m, k, i, j, f[3605][1805], g[3605][1805], c1[3605], c2[3605], s1, s2, x, n     y, mod = 998244353, fac[3605], inv[3605], first[3605], ans; n int p(int x, int y) { n   if (x < y) return 0; n   return 1ll * fac[x] * first[x - y] % mod; n } n int main() { n   fac[0] = fac[1] = inv[0] = inv[1] = first[0] = first[1] = 1; n   for ((i) = (2); (i) <= (3600); (i)++) { n     fac[i] = 1ll * fac[i - 1] * i % mod; n     inv[i] = 1ll * (mod - mod / i) * inv[mod % i] % mod; n     first[i] = 1ll * first[i - 1] * inv[i] % mod; n   } n   read(n); n   read(m); n   read(k); n   for (((i)) = (1); ((i)) <= ((k)); ((i))++) { n     read(x); n     read(y); n     int lx = x, ly = y; n     if (++c1[x] > 1) { n       puts( 0 ); n       return 0; n     } n     if (++c2[y] > 1) { n       puts( 0 ); n       return 0; n     } n     read(x); n     read(y); n     if (lx != x && ++c1[x] > 1) { n       puts( 0 ); n       return 0; n     } n     if (ly != y && ++c2[y] > 1) { n       puts( 0 ); n       return 0; n     } n   } n   c1[0] = c2[0] = 1; n   f[0][0] = 1; n   for (((i)) = (1); ((i)) <= ((n)); ((i))++) { n     if (c1[i] == 0) s1++; n     for ((j) = (0); (j) <= (i / 2); (j)++) { n       f[i][j] = f[i - 1][j]; n       if (c1[i] == 0 && c1[i - 1] == 0) { n         (f[i][j] += f[i - 2][j - 1]) %= mod; n       } n     } n   } n   g[0][0] = 1; n   for (((i)) = (1); ((i)) <= ((m)); ((i))++) { n     if (c2[i] == 0) s2++; n     for ((j) = (0); (j) <= (i / 2); (j)++) { n       g[i][j] = g[i - 1][j]; n       if (c2[i] == 0 && c2[i - 1] == 0) { n         (g[i][j] += g[i - 2][j - 1]) %= mod; n       } n     } n   } n   for ((i) = (0); (i) <= (n / 2); (i)++) n     for ((j) = (0); (j) <= (m / 2); (j)++) { n       ans = (ans + 1ll * f[n][i] * g[m][j] % mod * p(s2 - 2 * j, i) % mod * n                        p(s1 - 2 * i, j)) % n             mod; n     } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> v[188]; n int dist[188][188]; n int n, m; n int dp[188][188]; n int best[188]; n void dfs(int x, int p) { n   for (int i = 0; i < v[x].size(); i++) { n     int t = v[x][i]; n     if (t != p) dfs(t, x); n   } n   best[x] = 1; n   for (int c = 1; c <= n; c++) { n     for (int j = 0; j < v[x].size(); j++) { n       int t = v[x][j]; n       if (t == p) continue; n       dp[x][c] += min(dp[t][c], dp[t][best[t]] + m); n     } n     dp[x][c] += dist[x][c]; n     if (dp[x][c] < dp[x][best[x]]) best[x] = c; n   } n } n int bel[188]; n void process(int x, int p, int c) { n   bel[x] = c; n   for (int i = 0; i < v[x].size(); i++) { n     int t = v[x][i]; n     if (t == p) continue; n     if (dp[t][c] < dp[t][best[t]] + m) n       process(t, x, c); n     else n       process(t, x, best[t]); n   } n } n int cost[188]; n int main() { n   cin >> n >> m; n   memset(dist, 0x3f, sizeof(dist)); n   for (int i = 1; i < n; i++) cin >> cost[i]; n   dist[1][1] = 0; n   for (int i = 1; i < n; i++) { n     dist[i + 1][i + 1] = 0; n     int x, y; n     scanf( %d%d , &x, &y); n     dist[x][y] = dist[y][x] = 1; n     v[x].push_back(y); n     v[y].push_back(x); n   } n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) n       for (int k = 1; k <= n; k++) n         dist[j][k] = min(dist[j][i] + dist[i][k], dist[j][k]); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) dist[i][j] = cost[dist[i][j]]; n   dfs(1, 0); n   cout << dp[1][best[1]] + m <<   n ; n   process(1, 0, best[1]); n   for (int i = 1; i <= n; i++) cout << bel[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int32_t main() { n   int n; n   cin >> n; n   if (n == 2) return cout << -1, 0; n   cout <<  385 n21 n165 n ; n   for (int i = 0; i < n - 3; i++) { n     cout << 21 * (i + 100) << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long int binpowmod(long long int a, long long int b, long long int m) { n   a %= m; n   long long int res = 1; n   while (b > 0) { n     if (b & 1) res = res * a % m; n     a = a * a % m; n     b >>= 1; n   } n   return res; n } n unsigned long long modInverse(unsigned long long n, int p) { n   return binpowmod(n, p - 2, p); n } n int main() { n   long long int t, n, j, k, i, f, h, l, r, t1, t2, a, b, x, ans; n   cin >> t; n   while (t--) { n     cin >> n; n     string s; n     cin >> s; n     f = 0; n     x = -1; n     for (i = 0; i < n; ++i) { n       if (s[i] ==  0 ) { n         f = 1; n         x = i + 1; n         break; n       } n     } n     if (f) { n       if (x <= n / 2) { n         cout << x <<     << n <<     << x + 1 <<     << n <<   n ; n       } else { n         cout << 1 <<     << x <<     << 1 <<     << x - 1 <<   n ; n       } n     } else n       cout << 1 <<     << n / 2 <<     << ((n + 1) / 2) + 1 <<     << n <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   long long n; n   cin >> n; n   string s; n   while (1) { n     if (n % 7 == 0) { n       n = n / 7; n       for (long long i = 0; i < n; i++) s.push_back( 7 ); n       break; n     } else { n       if (n > 0) { n         s.push_back( 4 ); n         n -= 4; n       } else { n         cout << -1; n         return 0; n       } n     } n   } n   if (s.empty()) n     cout << -1; n   else n     cout << s; n } n


#include <bits/stdc++.h> n using namespace std; n char s[1000050]; n int max(int x, int y) { return (x > y) ? x : y; } n int main() { n   scanf( %s , &s); n   int len = strlen(s); n   int cnt = 1, j; n   int result = 1; n   for (int i = len; i < 2 * len; i++) { n     s[i] = s[i - len]; n   } n   len = 2 * len; n   for (int i = 0; i < len; i = j) { n     for (j = i + 1; j < len; j++) { n       if (s[j] == s[j - 1]) { n         break; n       } n     } n     result = max(result, j - i); n   } n   if (result > len / 2) { n     result = len / 2; n   } n   printf( %d n , result); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   unsigned long long n, k; n   cin >> n >> k; n   unsigned long long r = 1, l = 0, num = n, mid; n   for (int i = 0; i < n; i++) { n     r <<= 1; n   } n   mid = (l + r) >> 1; n   while (mid != k) { n     --num; n     if (mid > k) n       r = mid; n     else n       l = mid; n     mid = (l + r) >> 1; n   } n   cout << num; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n const long long MX = 2e5 + 5; n const long long INF = 1e18; n const long double PI = acos((long double)-1); n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long tc; n   cin >> tc; n   while (tc--) { n     long long n, k; n     bool imposs = false; n     cin >> n >> k; n     set<long long> powers; n     for (int i = (1); i <= (n); i++) { n       long long x; n       cin >> x; n       long long cnt = 0; n       while (x) { n         if (imposs) break; n         long long a = x % k; n         if (a > 1) imposs = true; n         if (a && powers.count(cnt)) n           imposs = true; n         else if (a) n           powers.insert(cnt); n         x /= k; n         cnt++; n         if (imposs) break; n       } n     } n     if (imposs) n       cout <<  NO n ; n     else n       cout <<  YES n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n #define int long long n #define pb push_back n #define mp make_pair n #define lb lower_bound n #define up upper_bound n #define sz(h) (int)h.size() n #define all(h) h.begin(), h.end() n #define mod 1000000007 n #define mod1 998244353 n #define MAX 2000010 n #define cas(t) cout <<  Case #  << t <<  :  ; n struct hash_pair n { // use pair as key in unordered_map<pair<int,int>,int,hash_pair>; n  n     template <class T1, class T2> n  n     size_t operator()(const pair<T1, T2> &p) const n  n     { n  n         auto hash1 = hash<T1>{}(p.first); n  n         auto hash2 = hash<T2>{}(p.second); n  n         return hash1 ^ hash2; n     } n }; n signed main() n { n     ios_base::sync_with_stdio(false); n     cin.tie(NULL); n #ifndef ONLINE_JUDGE n     //freopen( input.txt ,  r , stdin); n     //freopen( output.txt ,  w , stdout); n     //freopen( input.txt ,  w , stdout); n #endif n     int tt = 1; n     cin >> tt; n     for (int t = 1; t <= tt; t++) n     { n         int a, b; n         cin >> a >> b; n         int u = 0; n         int down = 0; n         int left = 0; n         int right = 0; n         string s; n         cin >> s; n         for (auto i : s) n         { n             if (i ==  U ) n                 u++; n             if (i ==  D ) n                 down++; n             if (i ==  L ) n                 left++; n             if (i ==  R ) n                 right++; n         } n         int fxl = -down; n         int fxu = u; n         int fyu = right; n         int fyl = -left; n         if (a <= fyu and fyl <= a and b <= fxu and b >= fxl) n             cout <<  YES n ; n         else n             cout <<  NO n ; n     } n } n


#include <bits/stdc++.h> n using namespace std; n struct bottle { n   int number; n   double len; n   double lento; n   double diflen; n } a[100005], b[100005]; n int cmp(bottle a, bottle b) { n   if (a.diflen > b.diflen) return 1; n   return 0; n } n int main() { n   double x1, y1, x2, y2, c1, c2, x, y; n   scanf( %lf %lf %lf %lf %lf %lf , &x1, &y1, &x2, &y2, &c1, &c2); n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %lf %lf , &x, &y); n     a[i].len = sqrt(pow(x1 - x, 2) + pow(y1 - y, 2)); n     b[i].len = sqrt(pow(x2 - x, 2) + pow(y2 - y, 2)); n     a[i].lento = sqrt(pow(x - c1, 2) + pow(y - c2, 2)); n     b[i].lento = sqrt(pow(x - c1, 2) + pow(y - c2, 2)); n     a[i].diflen = a[i].lento - a[i].len; n     b[i].diflen = b[i].lento - b[i].len; n     a[i].number = i; n     b[i].number = i; n   } n   sort(a, a + n, cmp); n   sort(b, b + n, cmp); n   double length = a[0].len + b[0].len + a[0].lento + b[0].lento; n   int mark1 = a[0].number, mark2 = b[0].number; n   if (a[0].diflen < 0 || b[0].diflen < 0) { n     if (a[0].diflen > b[0].diflen) { n       mark2 = -1; n       length = a[0].len + a[0].lento; n     } else { n       mark1 = -1; n       length = b[0].len + b[0].lento; n     } n   } n   if (a[0].number == b[0].number && mark1 != -1 && mark2 != -1) { n     if (a[1].diflen < 0 || b[1].diflen < 0) { n       if (a[1].diflen < 0 && b[1].diflen < 0) { n         if (a[0].diflen > b[0].diflen) { n           mark2 = -1; n           mark1 = a[0].number; n           length = a[0].len + a[0].lento; n         } else { n           mark1 = -1; n           mark2 = b[0].number; n           length = b[0].len + b[0].lento; n         } n       } else if (a[1].diflen < 0) { n         if ((a[0].diflen + b[1].diflen) > b[0].diflen) { n           mark1 = a[0].number; n           mark2 = b[1].number; n           length = a[0].len + b[1].len + a[0].lento + b[1].lento; n         } else { n           mark1 = -1; n           mark2 = b[0].number; n           length = b[0].len + b[0].lento; n         } n       } else if (b[1].diflen < 0) { n         if ((b[0].diflen + a[1].diflen) > a[0].diflen) { n           mark1 = a[1].number; n           mark2 = b[0].number; n           length = a[1].len + b[0].len + a[1].lento + b[0].lento; n         } else { n           mark1 = a[0].number; n           mark2 = -1; n           length = a[0].len + a[0].lento; n         } n       } n     } else if ((a[0].diflen + b[1].diflen) > (a[1].diflen + b[0].diflen)) { n       length = a[0].len + b[1].len + a[0].lento + b[1].lento; n       mark1 = a[0].number; n       mark2 = b[1].number; n     } else { n       length = a[1].len + b[0].len + a[1].lento + b[0].lento; n       mark1 = a[1].number; n       mark2 = b[0].number; n     } n   } n   for (int i = 0; i < n; i++) { n     if (a[i].number != mark1 && a[i].number != mark2) { n       length += a[i].lento * 2; n     } n   } n   printf( %.12lf n , length); n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> d; n int countDivisors(int n) { n   if (d[n] != 0) return d[n]; n   int res = 0; n   for (int i = 1; i * i <= n; i++) { n     if (n % i == 0) { n       if (n / i == i) n         res++; n       else n         res += 2; n     } n   } n   d[n] = res; n   return res; n } n void solve() { n   int a, b, c; n   cin >> a >> b >> c; n   d = vector<int>(a * b * c + 1, 0); n   int ans = 0; n   for (auto i = (1); i < (a + 1); i++) n     for (auto j = (1); j < (b + 1); j++) n       for (auto k = (1); k < (c + 1); k++) ans += countDivisors(i * j * k); n   cout << ans % 1073741824 <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t = 1; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long a, b, m, ans = 0; n   scanf( %I64d%I64d%I64d , &a, &b, &m); n   if (a >= m || b >= m) n     printf( 0 n ); n   else if (a <= 0 && b <= 0) n     printf( -1 n ); n   else { n     if (a < 0 || b < 0) { n       if (a > b) swap(a, b); n       ans = -a / b; n       a += b * ans; n     } n     while (a < m && b < m) { n       if (a > b) swap(a, b); n       ans++; n       a += b; n     } n     printf( %I64d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void chkMax(T &x, T y) { n   if (y > x) x = y; n } n template <typename T> n void chkMin(T &x, T y) { n   if (y < x) x = y; n } n template <typename T> n void inline read(T &x) { n   int f = 1; n   x = 0; n   char s = getchar(); n   while (s <  0  || s >  9 ) { n     if (s ==  - ) f = -1; n     s = getchar(); n   } n   while (s <=  9  && s >=  0 ) x = x * 10 + (s ^ 48), s = getchar(); n   x *= f; n } n const int N = 55; n int n; n struct E { n   int a, b; n } e[N]; n bool operator<(const E &x, const E &y) { n   if (x.a == y.a) return x.b > y.b; n   return x.a > y.a; n }; n long double f[N], g[N], s[N]; n bool inline chk(long double mid) { n   for (int i = 1; i <= n; i++) f[i] = -9e18; n   for (int i = 1; i <= n; i++) { n     s[i] = s[i - 1] + e[i].b * mid - e[i].a; n   } n   f[0] = 0; n   for (int i = 1; i <= n; i++) { n     int j = i; n     while (j < n && e[j + 1].a == e[i].a) j++; n     memcpy(g, f, sizeof f); n     for (int j = 0; j <= n; j++) f[j] = -9e18; n     int l = j - i + 1; n     for (int j = 0; j <= n; j++) { n       if (g[j] <= -9e18) continue; n       for (int k = 0; k <= l; k++) { n         int re = l - k; n         if (re > j) continue; n         chkMax(f[j - re + k], g[j] + s[i + k - 1] - s[i - 1]); n       } n     } n     i = j; n   } n   for (int i = 0; i <= n; i++) { n     if (f[i] >= 0) return 1; n   } n   return 0; n } n int main() { n   read(n); n   for (int i = 1; i <= n; i++) read(e[i].a); n   for (int i = 1; i <= n; i++) read(e[i].b); n   sort(e + 1, e + 1 + n); n   long double l = 0, r = 1e8, eps = 1e-10; n   while (r - l > eps) { n     long double mid = (l + r) / 2; n     if (chk(mid)) n       r = mid; n     else n       l = mid; n   } n   printf( %lld n , (long long)(r * 1000 + 0.99999)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 5; n int n, m; n unsigned long long A, B, C; n struct E { n   int u, v; n   E(int u, int v) : u(u), v(v) {} n }; n struct Graph { n   int n, m, deg[maxn], cur[maxn]; n   unsigned long long hash_[maxn]; n   vector<E> edge; n   vector<int> small[maxn], large[maxn]; n   vector<int> G[maxn]; n   void init(int n) { n     this->n = n, m = 0; n     edge.clear(); n     for (int i = 0; i < n; ++i) { n       small[i].clear(); n       large[i].clear(); n       G[i].clear(); n     } n   } n   void add_edge(int u, int v) { n     if (u > v) swap(u, v); n     edge.push_back(E(u, v)); n     G[u].push_back(v); n     G[v].push_back(u); n     small[v].push_back(u); n     large[u].push_back(v); n     hash_[m++] = (unsigned long long)u * n + v; n   } n   unsigned long long sum0() { n     unsigned long long s = 0; n     for (int i = 0; i < n; ++i) { n       s += A * i * ((unsigned long long)(n - 1 - i) * (n - 2 - i) / 2); n       s += B * i * i * (n - 1 - i); n       s += C * i * ((unsigned long long)i * (i - 1) / 2); n     } n     return s; n   } n   unsigned long long sum1() { n     unsigned long long s = 0; n     for (E e : edge) { n       int u = e.u, v = e.v; n       s += A * ((unsigned long long)u * (u - 1) / 2) + B * u * u + C * v * u; n       s += A * u * (v - u - 1) + n            B * ((unsigned long long)(u + v) * (v - u - 1) / 2) + n            C * v * (v - u - 1); n       s += A * u * (n - 1 - v) + B * v * (n - 1 - v) + n            C * ((unsigned long long)(n + v) * (n - 1 - v) / 2); n     } n     return s; n   } n   unsigned long long sum2() { n     for (int i = 0; i < n; ++i) { n       sort(small[i].begin(), small[i].end()); n       sort(large[i].begin(), large[i].end()); n     } n     unsigned long long s = 0; n     for (int i = 0; i < n; ++i) { n       unsigned long long scnt = small[i].size(); n       unsigned long long lcnt = large[i].size(); n       s += A * i * ((unsigned long long)lcnt * (lcnt - 1) / 2); n       for (int j = 0; j < lcnt; ++j) n         s += large[i][j] * (B * (lcnt - 1 - j) + C * j); n       s += B * i * scnt * lcnt; n       for (int j = 0; j < scnt; ++j) s += small[i][j] * A * lcnt; n       for (int j = 0; j < lcnt; ++j) s += large[i][j] * C * scnt; n       s += C * i * ((unsigned long long)scnt * (scnt - 1) / 2); n       for (int j = 0; j < scnt; ++j) n         s += small[i][j] * (A * (scnt - 1 - j) + B * j); n     } n     return s; n   } n   unsigned long long sum3() { n     sort(hash_, hash_ + m); n     hash_[m] = -1; n     for (int i = 0; i < n; ++i) deg[i] = G[i].size(); n     unsigned long long s = 0; n     for (int i = 0; i < n; ++i) { n       int sz = 0; n       for (int v : G[i]) n         if (deg[v] > deg[i] || (deg[v] == deg[i] && v > i)) cur[sz++] = v; n       sort(cur, cur + sz); n       int l = 0; n       for (int j = 0; j < sz; ++j) n         for (int k = j + 1; k < sz; ++k) { n           int u = cur[j], v = cur[k]; n           unsigned long long hash = (unsigned long long)u * n + v; n           int pos = lower_bound(hash_ + l, hash_ + m, hash) - hash_; n           l = pos; n           if (hash_[pos] == hash) s += fun(i, u, v); n         } n     } n     return s; n   } n   unsigned long long sum() { return sum0() - sum1() + sum2() - sum3(); } n   unsigned long long fun(int a, int b, int c) { n     int mx = max(a, c); n     int mn = min(a, b); n     return B * (a + b + c) + (A - B) * mn + (C - B) * mx; n   } n } graph; n int main(void) { n   while (~scanf( %d%d , &n, &m)) { n     scanf( %llu%llu%llu , &A, &B, &C); n     graph.init(n); n     for (int i = 0; i < m; ++i) { n       int u, v; n       scanf( %d%d , &u, &v); n       graph.add_edge(u, v); n     } n     printf( %llu n , graph.sum()); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using namespace std; n long long m = 33554431; n long long bigmod(long long b, long long p) { n   if (p == 0) return 1; n   if (p % 2 == 0) { n     long long temp = bigmod(b, p / 2); n     return (temp * temp) % m; n   } n   return (bigmod(b, p - 1) * (b % m)) % m; n } n bool XX(pair<int, double> a, pair<int, double> b) { n   return a.second < b.second; n } n int main() { n   int n, m; n   cin >> n >> m; n   vector<pair<int, double> > vec(n); n   for (int i = 0; i < n; i++) { n     scanf( %d %lf , &vec[i].first, &vec[i].second); n   } n   sort(vec.begin(), vec.end(), XX); n   vector<int> ans; n   for (int i = 0; i < n; i++) { n     if (ans.empty() || ans.back() <= vec[i].first) n       ans.push_back(vec[i].first); n     else n       *upper_bound(ans.begin(), ans.end(), vec[i].first) = vec[i].first; n   } n   cout << n - ans.size() << endl; n   return 0; n } n


//begin #include <Core> n /* n  * Package: StandardCodeLibrary.Core n  * Last Update: 2012-12-21 n  * */ n #include <iostream> n #include <fstream> n #include <sstream> n #include <iomanip> n #include <utility> n #include <vector> n #include <list> n #include <string> n #include <stack> n #include <queue> n #include <deque> n #include <set> n #include <map> n #include <algorithm> n #include <functional> n #include <numeric> n #include <bitset> n #include <complex> n #include <cstdio> n #include <cstring> n #include <cmath> n #include <cstdlib> n #include <ctime> n #include <climits> n #if __GNUC__>=4 and __GNUC_MINOR__>=6 n  #include <ext/pb_ds/assoc_container.hpp> n  #include <ext/pb_ds/tree_policy.hpp> n  #include <ext/pb_ds/tag_and_trait.hpp> n #endif n using namespace std; n  n #define lp for(;;) n #define repf(i,a,b) for (int i=(a);i<(b);++i) n #define rep(i,n) repf(i,0,n) n #define ft(i,a,b) for (int i=(a);i<=(b);++i) n #define fdt(i,a,b) for (int i=(a);i>=b;--i) n #define feach(e,s) for (typeof((s).begin()) e=(s).begin();e!=(s).end();++e) n #define fsubset(subset,set) for (int subset=set&(set-1);subset;subset=(subset-1)&set) n #define forin(i,charset) for (cstr i=charset;*i;i++) n #define whl while n #define rtn return n #define fl(x,y) memset((x),char(y),sizeof(x)) n #define clr(x) fl(x,char(0)) n #define cpy(x,y) memcpy(x,y,sizeof(x)) n #define pb push_back n #define mp make_pair n #define ins insert n #define rnk order_of_key n #define sel find_by_order n #define x first n #define y second n #define sz(x) (int((x).size())) n #define all(x) (x).begin(),(x).end() n #define srt(x) sort(all(x)) n #define uniq(x) srt(x),(x).erase(unique(all(x)),x.end()) n #define vec vector n #define pr pair n #define que queue n #define prq priority_queue n #define itr iterator n #define sf scanf n #define pf printf n #define pdb(prcs,x) (cout<<setprecision(prcs)<<fixed<<(x)) n #ifdef DEBUG n #define prt(x) cerr<<#x = <<(x)<<endl n #define asrtWA(s) do if(!(s))do{cerr<< assert( #s ) <<endl;}whl(0);whl(0) n #define asrtTLE(s) do if(!(s))do{cerr<< assert( #s ) <<endl;}whl(0);whl(0) n #define asrtMLE(s) do if(!(s))do{cerr<< assert( #s ) <<endl;}whl(0);whl(0) n #define asrtOLE(s) do if(!(s))do{cerr<< assert( #s ) <<endl;}whl(0);whl(0) n #define asrtRE(s) do if(!(s))do{cerr<< assert( #s ) <<endl;}whl(0);whl(0) n #define runtime() cerr<< Used:  <<db(clock())/CLOCKS_PER_SEC<< s <<endl n #define input(in) do{}whl(0) n #define output(out) do{}whl(0) n #else n #define endl (char(  n )) n #define prt(x) (cerr) n #define asrtWA(s) do if(!(s))exit(0);whl(0) n #define asrtTLE(s) do if(!(s))whl(1);whl(0) n #define asrtMLE(s) do if(!(s))whl(new int);whl(0) n #define asrtOLE(s) do if(!(s))whl(1)puts( OLE );whl(0) n #define asrtRE(s) do if(!(s))*(int*)0=0;whl(0) n #define runtime() cerr n #define input(in) freopen(in, r ,stdin) n #define output(out) freopen(out, w ,stdout) n #endif n  n typedef long long int lli; n typedef double db; n typedef const char* cstr; n typedef string str; n typedef vec<int> vi; n typedef vec<vi> vvi; n typedef vec<bool> vb; n typedef vec<vb> vvb; n typedef vec<str> vs; n typedef pr<int,int> pii; n typedef pr<lli,lli> pll; n typedef pr<db,db> pdd; n typedef pr<str,int> psi; n typedef map<int,int> mii; n typedef map<str,int> msi; n typedef map<char,int> mci; n typedef set<int> si; n typedef set<str> ss; n typedef que<int> qi; n typedef prq<int> pqi; n #if __GNUC__>=4 and __GNUC_MINOR__>=7 n  template<typename key,typename value>class ext_map:public __gnu_pbds::tree<key,value,less<key>,__gnu_pbds::rb_tree_tag,__gnu_pbds::tree_order_statistics_node_update>{}; n  template<typename key>class ext_set:public __gnu_pbds::tree<key,__gnu_pbds::null_type,less<key>,__gnu_pbds::rb_tree_tag,__gnu_pbds::tree_order_statistics_node_update>{}; n #elif __GNUC__>=4 and __GNUC_MINOR__>=6 n  template<typename key,typename value>class ext_map:public __gnu_pbds::tree<key,value,less<key>,__gnu_pbds::rb_tree_tag,__gnu_pbds::tree_order_statistics_node_update>{}; n  template<typename key>class ext_set:public __gnu_pbds::tree<key,__gnu_pbds::null_mapped_type,less<key>,__gnu_pbds::rb_tree_tag,__gnu_pbds::tree_order_statistics_node_update>{}; n #endif n  n const int oo=(~0u)>>1; n const lli ooll=(~0ull)>>1; n const db inf=1e+10; n const db eps=1e-10; n const db pi=acos(-1.0); n const int MOD=1000000007; n  n template<typename type>inline bool cmax(type& a,const type& b){rtn a<b?a=b,true:false;} n template<typename type>inline bool cmin(type& a,const type& b){rtn b<a?a=b,true:false;} n template<typename type>inline type sqr(const type& x){rtn x*x;} n inline int dbcmp(const db& a,const db& b){rtn (a>b+eps)-(a<b-eps);} n inline int sgn(const db& x){rtn dbcmp(x,0);} n template<typename ostream,typename type>ostream& operator<<(ostream& cout,const pr<type,type>& x){rtn cout<< ( <<x.x<< , <<x.y<< ) ;} n template<typename type>pr<type,type> operator-(const pr<type,type>& x){rtn mp(-x.x,-x.y);} n template<typename type>pr<type,type> operator+(const pr<type,type>& a,const pr<type,type>& b){rtn mp(a.x+b.x,a.y+b.y);} n template<typename type>pr<type,type> operator-(const pr<type,type>& a,const pr<type,type>& b){rtn mp(a.x-b.x,a.y-b.y);} n template<typename type>inline type cross(const pr<type,type>& a,const pr<type,type>& b,const pr<type,type>& c){rtn (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);} n template<typename type>inline type dot(const pr<type,type>& a,const pr<type,type>& b,const pr<type,type>& c){rtn (b.x-a.x)*(c.x-a.x)+(b.y-a.y)*(c.y-a.y);} n template<typename type>inline type gcd(type a,type b){if(b)whl((a%=b)&&(b%=a));rtn a+b;} n template<typename type>inline type lcm(type a,type b){rtn a*b/gcd(a,b);} n template<typename type>inline void bit_inc(vec<type>& st,int x,type inc){whl(x<sz(st))st[x]+=inc,x|=x+1;} n template<typename type>inline type bit_sum(const vec<type>& st,int x){type s=0;whl(x>=0)s+=st[x],x=(x&(x+1))-1;rtn s;} n template<typename type>inline type bit_kth(const vec<type>& st,int k){int x=0,y=0,z=0;whl((1<<(++y))<=sz(st));fdt(i,y-1,0){if((x+=1<<i)>sz(st)||z+st[x-1]>k)x-=1<<i;else z+=st[x-1];}rtn x;} n inline void make_set(vi& st){rep(i,sz(st))st[i]=i;} n inline int find_set(vi& st,int x){int y=x,z;whl(y!=st[y])y=st[y];whl(x!=st[x])z=st[x],st[x]=y,x=z;rtn y;} n inline bool union_set(vi& st,int a,int b){a=find_set(st,a),b=find_set(st,b);rtn a!=b?st[a]=b,true:false;} n template<typename type>inline void merge(type& a,type& b){if(sz(a)<sz(b))swap(a,b);whl(sz(b))a.insert(*b.begin()),b.erase(b.begin());} n template<typename type>inline void merge(prq<type>& a,prq<type>& b){if(sz(a)<sz(b))swap(a,b);whl(sz(b))a.push(b.top()),b.pop();} n  n struct Initializer{Initializer(){ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);}~Initializer(){runtime();}}initializer; n //end #include <Core> n  n #define idx(l,r) (((l)+(r))|((l)!=(r))) n #define rt idx(l,r) n #define lrt idx(l,m) n #define rrt idx(m+1,r) n const int MAXN=100000; n struct node n { n  int cnt,size; n  lli sum[5]; n }; n int a[MAXN]; n mii idx; n node st[(MAXN<<1)-1]; n void upd(int l,int r,int p,int v) n { n  if (p<l||r<p) ; n  else if (p<=l&&r<=p) n  { n   st[rt].cnt+=v; n   if (st[rt].cnt) n   { n    st[rt].size=1; n    rep(i,5) st[rt].sum[i]=0; n    st[rt].sum[0]=a[p]; n   } n   else n   { n    st[rt].size=0; n    rep(i,5) st[rt].sum[i]=0; n   } n  } n  else n  { n   int m=(l+r)>>1; n   upd(l,m,p,v),upd(m+1,r,p,v); n   st[rt].size=st[lrt].size+st[rrt].size; n   rep(i,5) st[rt].sum[i]=st[lrt].sum[i]; n   rep(i,5) st[rt].sum[(st[lrt].size+i)%5]+=st[rrt].sum[i]; n  } n } n lli qry(int l,int r) n { n  rtn l<=r?st[rt].sum[2]:0; n } n #undef rc n #undef lc n #undef p n #undef idx n  n str op[MAXN]; n int opx[MAXN]; n int opxcpy[MAXN]; n  n int main() n { n  int n; n  cin>>n; n  rep(i,n) n  { n   cin>>op[i]; n   if (op[i]!= sum ) cin>>opx[i]; n  } n  cpy(opxcpy,opx); n  sort(opxcpy,opxcpy+n); n  rep(i,n) if (!idx.count(opxcpy[i])) idx.insert(mp(a[sz(idx)]=opxcpy[i],sz(idx))); n  rep(i,n) n  { n   if (op[i]== add ) upd(0,sz(idx)-1,idx[opx[i]],+1); n   else if (op[i]== del ) upd(0,sz(idx)-1,idx[opx[i]],-1); n   else cout<<qry(0,sz(idx)-1)<<endl; n  } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 200005; n pair<int, int> a[MAX]; n int bin_Upper(vector<pair<int, int> >& a, int l, int r, int x, int last) { n   int m = (l + r) / 2; n   if (l > r) return last; n   if (x > a[m].second) return bin_Upper(a, m + 1, r, x, last); n   if (x <= a[m].second) return bin_Upper(a, l, m - 1, x, m); n } n int main() { n   int n, q, cmd, p, x; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a[i].first; n     a[i].second = 0; n   } n   cin >> q; n   vector<pair<int, int> > stak; n   for (int k = 1; k <= q; k++) { n     cin >> cmd; n     if (cmd == 1) { n       cin >> p >> x; n       a[p - 1] = {x, k}; n     } else { n       cin >> x; n       while (!stak.empty() && stak.back().first <= x) stak.pop_back(); n       stak.push_back({x, k}); n     } n   } n   for (int i = 0; i < n; i++) { n     int temp = bin_Upper(stak, 0, stak.size() - 1, a[i].second, -1); n     if (temp == -1 || stak[temp].first <= a[i].first) n       cout << a[i].first <<    ; n     else n       cout << stak[temp].first <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n int max(int a, int b) { return a > b ? a : b; } n long long int min(long long int a, long long int b) { return a < b ? a : b; } n int main() { n   using namespace std; n   int t, a, b; n   cin >> t; n   while (t--) { n     cin >> a >> b; n     cout << min(min(a, b), (a + b) / 3) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   while (n--) { n     string s; n     cin >> s; n     while (s.size() && s[0] ==  0 ) { n       s.erase(s.begin()); n     } n     while (s.size() && s[(int)s.size() - 1] ==  0 ) { n       s.erase(s.begin() + (int)s.size() - 1); n     } n     int cnt = 0; n     for (auto x : s) cnt += (x ==  0 ); n     cout << cnt << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char pic[105][105]; n int vis[105][105]; n int disx[] = {-1, 1, 0, 0}; n int disy[] = {0, 0, -1, 1}; n struct node { n   int ro, lo, len; n   node() {} n   node(int rr, int ll, int le) { n     ro = rr; n     lo = ll; n     len = le; n   } n }; n int main() { n   int n, m; n   cin >> n >> m; n   vector<node> ve; n   for (int i = 0; i < n; i++) cin >> pic[i]; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (pic[i][j] ==  * ) { n         int tot = 105, cnt; n         for (int k = 0; k < 4; k++) { n           cnt = 0; n           for (int h = 1; h <= n; h++) { n             int xx = i + disx[k] * h; n             int yy = j + disy[k] * h; n             if (xx >= 0 && xx < n && yy >= 0 && yy < m && pic[xx][yy] ==  * ) { n               cnt++; n             } else n               break; n           } n           if (cnt != 0) n             tot = min(tot, cnt); n           else { n             tot = 105; n             break; n           } n         } n         if (tot != 105) { n           for (int k = 0; k < 4; k++) { n             for (int h = 1; h <= tot; h++) { n               int xx = i + disx[k] * h; n               int yy = j + disy[k] * h; n               if (xx >= 0 && xx < n && yy >= 0 && yy < m && pic[xx][yy] ==  * ) n                 vis[xx][yy]++; n             } n           } n           vis[i][j]++; n           ve.push_back(node(i + 1, j + 1, tot)); n         } n       } n     } n   } n   bool flag = true; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (pic[i][j] ==  * ) { n         if (vis[i][j] == 0) { n           flag = false; n           break; n         } n       } n     } n     if (!flag) break; n   } n   if (!flag) n     cout << -1 << endl; n   else { n     cout << ve.size() << endl; n     for (int i = 0; i < ve.size(); i++) { n       cout << ve[i].ro <<     << ve[i].lo <<     << ve[i].len << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, x, y; n   cin >> n >> m; n   pair<int, pair<int, int> > arr[n]; n   int arr1[n], arr2[n]; n   for (int i = 0; i < n; ++i) cin >> arr1[i]; n   for (int i = 0; i < n; ++i) cin >> arr2[i]; n   for (int i = 0; i < n; ++i) { n     arr[i].second.first = arr1[i]; n     arr[i].second.second = arr2[i]; n     arr[i].first = arr[i].second.first - arr[i].second.second; n   } n   sort(arr, arr + n); n   long long sum = 0; n   if (n == m) { n     for (int i = 0; i < n; ++i) sum += arr[i].second.first; n     cout << sum << endl; n     return 0; n   } n   for (int i = 0; i < n; ++i) { n     if (m > 0) { n       sum += arr[i].second.first; n       --m; n     } else { n       if (arr[i].second.first > arr[i].second.second) n         sum += arr[i].second.second; n       else n         sum += arr[i].second.first; n     } n   } n   cout << sum << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long double eps = 1e-9; n const int inf = (1 << 30) - 1; n int n, m, it; n bool a[250][250]; n int u[250][250], lx, ly, rx, ry, k; n const int dx[8] = {-1, -1, -1, 0, 0, 1, 1, 1}; n const int dy[8] = {-1, 0, 1, -1, 1, -1, 0, 1}; n void go(int vx, int vy) { n   if (u[vx][vy]) return; n   u[vx][vy] = it; n   lx = min(lx, vx); n   rx = max(rx, vx); n   ly = min(ly, vy); n   ry = max(ry, vy); n   ++k; n   for (int i = 0; i < 8; ++i) { n     int x = vx + dx[i]; n     int y = vy + dy[i]; n     if (0 <= x && x < n && 0 <= y && y < m) n       if (a[x][y] == 1) go(x, y); n   } n } n bool check1(int x, int y) { n   if (k < 4) return 0; n   if (rx - lx != ry - ly) return 0; n   int tmp = 0; n   for (int i = lx; i <= rx; ++i) { n     if (!u[i][ly] || !u[i][ry]) return 0; n     tmp += 2; n   } n   for (int i = ly + 1; i < ry; ++i) { n     if (!u[lx][i] || !u[rx][i]) return 0; n     tmp += 2; n   } n   if (tmp == k) n     return 1; n   else n     return 0; n } n bool check2(int x, int y) { n   if (k < 4) return 0; n   if (rx - lx != ry - ly) return 0; n   if (ry - y != y - ly) return 0; n   int tmp = 0; n   int x1 = x; n   int y1 = y; n   int x2 = lx + y - ly; n   int y2 = ly; n   for (int i = 0; i <= y - ly; ++i) { n     if (!u[x1 + i][y1 + i] || !u[x2 + i][y2 + i]) return 0; n     tmp += 2; n   } n   tmp -= 4; n   x1 = x2; n   y1 = y2; n   x2 = rx; n   y2 = y; n   for (int i = 0; i <= y - ly; ++i) { n     if (!u[x1 - i][y1 + i] || !u[x2 - i][y2 + i]) return 0; n     tmp += 2; n   } n   if (tmp == k) n     return 1; n   else n     return 0; n } n void Work() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < n; ++i) n     for (int j = 0; j < m; ++j) { n       char ch; n       scanf(  %c , &ch); n       a[i][j] = ch ==  1 ; n     } n   for (int i = 0; i < n; ++i) n     for (int j = 0; j < m; ++j) u[i][j] = 0; n   it = 0; n   int res = 0; n   for (int i = 0; i < n; ++i) n     for (int j = 0; j < m; ++j) n       if (a[i][j] && !u[i][j]) { n         k = 0; n         ++it; n         lx = rx = i; n         ly = ry = j; n         go(i, j); n         if (check1(i, j) || check2(i, j)) ++res; n       } n   printf( %d n , res); n } n int main() { n   int T; n   scanf( %d , &T); n   while (T--) Work(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long vp(pair<int, int> a, pair<int, int> b) { n   return (long long)a.first * b.second - (long long)a.second * b.first; n } n pair<int, int> mn(pair<int, int> a, pair<int, int> b) { n   return make_pair(a.first - b.first, a.second - b.second); n } n int main() { n   srand(239); n   int n, m; n   scanf( %d %d , &n, &m); n   if (m == 3) { n     if (n > 4) { n       printf( -1 n ); n       return 0; n     } n     if (n == 3) { n       printf( 0 0 n0 1 n1 0 n ); n       return 0; n     } n     if (n == 4) { n       printf( 0 0 n0 3 n3 0 n1 1 n ); n       return 0; n     } n     assert(false); n   } n   int R1 = 1e7; n   int R2 = 2e7; n   vector<pair<int, int> > v; n   for (int i = 0; i < m; i++) { n     v.push_back( n         make_pair(R1 * cos(M_PI * 2 * i / m), R1 * sin(M_PI * 2 * i / m))); n     if (i < n - m) n       v.push_back(make_pair(R2 * cos(M_PI * 2 * i / m + M_PI * 0.0001), n                             R2 * sin(M_PI * 2 * i / m + M_PI * 0.0001))); n   } n   while (1) { n     bool good = true; n     for (int i = 0; i < n; i++) { n       for (int j = i + 1; j < n; j++) { n         for (int k = j + 1; k < n; k++) { n           if (vp(mn(v[i], v[k]), mn(v[j], v[k])) == 0) { n             v[k].first += rand() % 10; n             v[k].second += rand() % 10; n             good = false; n           } n         } n       } n     } n     if (good) break; n   } n   assert((int)(v).size() == n); n   for (int i = 0; i < n; i++) { n     printf( %d %d n , v[i].first, v[i].second); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, x = 0, y; n   cin >> n; n   y = n * n / 2.0 + n / 2.0; n   cout << 6 * y + 1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> data; n long long a, b, h, w, n, t, minVal; n inline void swap(int &a, int &b) { n   int tmp = a; n   a = b; n   b = tmp; n } n inline int mini(int a, int b) { return a < b ? a : b; } n bool cmp(long long a, long long b) { return a > b; } n int dfs(int crt, long long th, long long tw) { n   if (th >= a && tw >= b || th >= b && tw >= a) return crt; n   if (crt >= 34 || crt >= data.size()) return -1; n   if (data[crt] == minVal) { n     while (th < a) { n       th *= minVal; n       crt++; n     } n     while (tw < b) { n       tw *= minVal; n       crt++; n     } n     return crt <= data.size() ? crt : -1; n   } else { n     int reta = 0, retb = 0; n     if (th < 100000) reta = dfs(crt + 1, th * data[crt], tw); n     if (tw < 100000) retb = dfs(crt + 1, th, tw * data[crt]); n     if (reta <= 0 && retb <= 0) return -1; n     if (reta <= 0) return retb; n     if (retb <= 0) return reta; n     return mini(reta, retb); n   } n } n int main(void) { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> a >> b >> h >> w >> n; n   if (a > b) swap(a, b); n   if (h > w) swap(h, w); n   if (h >= a && w >= b) { n     printf( 0 n ); n     return 0; n   } n   for (int i = 0; i < n; i++) { n     cin >> t; n     data.push_back(t); n   } n   sort(data.begin(), data.end(), cmp); n   minVal = data[mini(33, data.size() - 1)]; n   printf( %d n , mini(dfs(0, h, w), dfs(0, w, h))); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename A, typename B> n string to_string(pair<A, B> p); n template <typename A, typename B, typename C> n string to_string(tuple<A, B, C> p); n template <typename A, typename B, typename C, typename D> n string to_string(tuple<A, B, C, D> p); n string to_string(const string &s) { return     + s +    ; } n string to_string(const char *s) { return to_string((string)s); } n string to_string(bool b) { return (b ?  true  :  false ); } n string to_string(vector<bool> v) { n   bool first = true; n   string res =  { ; n   for (int i = 0; i < static_cast<int>(v.size()); i++) { n     if (!first) { n       res +=  ,  ; n     } n     first = false; n     res += to_string(v[i]); n   } n   res +=  } ; n   return res; n } n template <size_t N> n string to_string(bitset<N> v) { n   string res =   ; n   for (size_t i = 0; i < N; i++) { n     res += static_cast<char>( 0  + v[i]); n   } n   return res; n } n template <typename A> n string to_string(A v) { n   bool first = true; n   string res =  { ; n   for (const auto &x : v) { n     if (!first) { n       res +=  ,  ; n     } n     first = false; n     res += to_string(x); n   } n   res +=  } ; n   return res; n } n class xxx { n  public: n   xxx(int x) : i(x) {} n   int i; n   void operator()() const { cout << i << endl; } n }; n template <typename A, typename B> n string to_string(pair<A, B> p) { n   return  (  + to_string(p.first) +  ,   + to_string(p.second) +  ) ; n } n template <typename A, typename B, typename C> n string to_string(tuple<A, B, C> p) { n   return  (  + to_string(get<0>(p)) +  ,   + to_string(get<1>(p)) +  ,   + n          to_string(get<2>(p)) +  ) ; n } n template <typename A, typename B, typename C, typename D> n string to_string(tuple<A, B, C, D> p) { n   return  (  + to_string(get<0>(p)) +  ,   + to_string(get<1>(p)) +  ,   + n          to_string(get<2>(p)) +  ,   + to_string(get<3>(p)) +  ) ; n } n void debug_out() { cerr << endl; } n template <typename Head, typename... Tail> n void debug_out(Head H, Tail... T) { n   cerr <<     << to_string(H); n   debug_out(T...); n } n const long long mod = 1e9 + 7; n inline long long gcd(long long a, long long b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n class RMQ { n  public: n   std::vector<long long> v; n   vector<vector<long long>> f; n   RMQ(vector<long long> &t) { n     v = t; n     build(); n   } n   void build() { n     int n = v.size(); n     int level = log(n) / log(2) + 1; n     f = vector<vector<long long>>(n, vector<long long>(level, 0)); n     for (int i = 0; i < n; ++i) f[i][0] = v[i]; n     for (int i = 1; i < level; ++i) { n       for (int j = 0; j <= n - (1 << i); ++j) { n         f[j][i] = gcd(f[j][i - 1], f[j + (1 << i - 1)][i - 1]); n       } n     } n   } n   long long query(int l, int r) { n     int k = log(r - l + 1) / log(2); n     return gcd(f[l][k], f[r - (1 << k) + 1][k]); n   } n }; n const long long INF = LLONG_MAX; n const int maxn = 2e5 + 5; n long long n, m; n long long sums = 0; n int k; n void read1() {} n void solve() { n   int X, d; n   cin >> X >> d; n   vector<long long> res; n   long long num = 1; n   for (int i = 31; i >= 0; --i) { n     if (((X >> i) & 1)) { n       for (int j = 0; j < i; ++j) res.push_back(num); n       res.push_back(num + d); n       num += 2 * d; n     } n   } n   cout << res.size() << endl; n   for (auto x : res) cout << x <<    ; n   cout << endl; n } n int main() { n   std::ios::sync_with_stdio(false); n   cin.tie(0); n   int tests = 1; n   for (int tc = 1; tc <= tests; tc++) { n     solve(); n     cout << flush; n   } n   return 0; n } n int _, __; n


#include <bits/stdc++.h> n using namespace std; n inline int toInt(string s) { n   int v; n   istringstream sin(s); n   sin >> v; n   return v; n } n inline long long toLongLong(string s) { n   long long v; n   istringstream sin(s); n   sin >> v; n   return v; n } n template <class T> n inline string toString(T x) { n   ostringstream sout; n   sout << x; n   return sout.str(); n } n inline vector<char> toVC(string s) { n   vector<char> data(s.begin(), s.end()); n   return data; n } n vector<string> SPRIT(const string &s, const string &delim) { n   vector<string> result; n   string::size_type pos = 0; n   while (pos != string::npos) { n     string::size_type p = s.find(delim, pos); n     if (p == string::npos) { n       result.push_back(s.substr(pos)); n       break; n     } else { n       result.push_back(s.substr(pos, p - pos)); n     } n     pos = p + delim.size(); n   } n   return result; n } n string TRIM(const string &str, const char *trimCharacterList =    t v r n ) { n   string result; n   string::size_type left = str.find_first_not_of(trimCharacterList); n   if (left != string::npos) { n     string::size_type right = str.find_last_not_of(trimCharacterList); n     result = str.substr(left, right - left + 1); n   } n   return result; n } n string REPLACE_STRING(const string source, const string find, n                       const string alt) { n   string result = source; n   string::size_type pos = 0; n   while (pos = result.find(find, pos), pos != string::npos) { n     result.replace(pos, find.length(), alt); n     pos += alt.length(); n   } n   return result; n } n template <typename T> n bool VECTOR_EXISTS(vector<T> vec, T data) { n   auto itr = std::find(vec.begin(), vec.end(), data); n   size_t index = distance(vec.begin(), itr); n   if (index != vec.size()) n     return true; n   else n     return false; n } n template <typename T> n void VECTOR_REMOVE_VALUE_ALL(vector<T> &vec, T data) { n   vec.erase(remove(vec.begin(), vec.end(), data), vec.end()); n } n template <typename T> n vector<T> VECTOR_REMOVE_VALUE_ALL_FAST(vector<T> vec, T data) { n   vector<T> vec2; n   for (auto &x : vec) n     if (x != data) vec2.push_back(x); n   return vec2; n } n bool REG_MATCH(string const &text, regex const &re) { n   bool result = regex_match(text, re); n   return result; n } n bool REG_MATCH(string const &text, smatch &match, regex const &re) { n   bool result = regex_match(text, match, re); n   return result; n } n smatch REG_SEARCH(string const &text, regex const &re) { n   smatch m; n   regex_search(text, m, re); n   return m; n } n vector<smatch> REG_ALL_SEARCH(string const &text, regex const &re) { n   vector<smatch> matchs; n   sregex_iterator iter(text.cbegin(), text.cend(), re); n   sregex_iterator end; n   for (; iter != end; iter++) matchs.push_back(*iter); n   return matchs; n } n string REG_REPLACE(string const &text, regex const &re, string const &replace) { n   string result = n       regex_replace(text, re, replace, regex_constants::format_first_only); n   return result; n } n string REG_ALL_REPLACE(string const &text, regex const &re, n                        string const &replace) { n   string result = regex_replace(text, re, replace); n   return result; n } n template <typename T> n vector<T> COMPRESS(vector<T> v) { n   sort(v.begin(), v.end()); n   v.erase(unique(v.begin(), v.end()), v.end()); n   return v; n } n template <typename T> n pair<map<T, int>, map<int, T>> MAPPING(const vector<T> v) { n   map<T, int> zip; n   map<int, T> unzip; n   for (int i = 0; i < v.size(); i++) zip[v[i]] = i; n   for (int i = 0; i < v.size(); i++) unzip[i] = v[i]; n   return make_pair(zip, unzip); n } n template <typename T> n vector<pair<T, int>> RUN_LENGTH(const vector<T> &v) { n   vector<pair<T, int>> ret; n   int len = v.size(); n   for (int i = 0, j = 0; i < len; i = j) { n     while (j < len && v[i] == v[j]) j++; n     ret.push_back(make_pair(v[i], j - i)); n   } n   return ret; n } n template <typename T, typename U, typename V, typename W> n auto operator+(const std::pair<T, U> &l, const std::pair<V, W> &r) n     -> std::pair<decltype(l.first + r.first), decltype(l.second + r.second)> { n   return {l.first + r.first, l.second + r.second}; n } n template <typename T, typename U, typename V, typename W> n auto operator-(const std::pair<T, U> &l, const std::pair<V, W> &r) n     -> std::pair<decltype(l.first - r.first), decltype(l.second - r.second)> { n   return {l.first - r.first, l.second - r.second}; n } n double ceil_n(double dIn, long long nLen) { n   double dOut; n   dOut = dIn * pow(10.0, nLen); n   dOut = (double)(long long)(dOut + 0.9); n   return dOut * pow(10.0, -nLen); n } n double floor_n(double dIn, long long nLen) { n   double dOut; n   dOut = dIn * pow(10.0, nLen); n   dOut = (double)(long long)(dOut); n   return dOut * pow(10.0, -nLen); n } n double round_n(double dIn, long long nLen) { n   double dOut; n   dOut = dIn * pow(10.0, nLen); n   dOut = (double)(long long)(dOut + 0.5); n   return dOut * pow(10.0, -nLen); n } n int take_a_n(long long num, int n) { n   string str = toString(num); n   return str[str.length() - n] -  0 ; n } n int bitcount32(int bits) { n   bits = (bits & 0x55555555) + (bits >> 1 & 0x55555555); n   bits = (bits & 0x33333333) + (bits >> 2 & 0x33333333); n   bits = (bits & 0x0f0f0f0f) + (bits >> 4 & 0x0f0f0f0f); n   bits = (bits & 0x00ff00ff) + (bits >> 8 & 0x00ff00ff); n   return (bits & 0x0000ffff) + (bits >> 16 & 0x0000ffff); n } n long long bitcount64(long long bits) { n   bits = (bits & 0x5555555555555555) + (bits >> 1 & 0x5555555555555555); n   bits = (bits & 0x3333333333333333) + (bits >> 2 & 0x3333333333333333); n   bits = (bits & 0x0f0f0f0f0f0f0f0f) + (bits >> 4 & 0x0f0f0f0f0f0f0f0f); n   bits = (bits & 0x00ff00ff00ff00ff) + (bits >> 8 & 0x00ff00ff00ff00ff); n   bits = (bits & 0x0000ffff0000ffff) + (bits >> 16 & 0x0000ffff0000ffff); n   return (bits & 0x00000000ffffffff) + (bits >> 32 & 0x00000000ffffffff); n } n template <class T1, class T2> n inline bool chmin(T1 &a, T2 b) { n   if (a > b) { n     a = b; n     return true; n   } n   return false; n } n template <class T1, class T2> n inline bool chmax(T1 &a, T2 b) { n   if (a < b) { n     a = b; n     return true; n   } n   return false; n } n template <typename T1, typename T2> n istream &operator>>(istream &in, pair<T1, T2> &p) { n   in >> p.first >> p.second; n   return in; n } n template <typename T> n istream &operator>>(istream &in, vector<T> &v) { n   for (auto &x : v) in >> x; n   return in; n } n template <typename T1, typename T2> n ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) { n   out <<  [  << p.first <<  ,   << p.second <<  ]  n       <<   n ; n   return out; n } n const double EPS = 1e-10; n const double PI = acos(-1.0); n const int dx[] = {-1, 0, 1, 0}; n const int dy[] = {0, 1, 0, -1}; n template <typename T> n inline bool BETWEEN(const T aim, const T min, const T max) { n   if (min <= aim && aim <= max) { n     return true; n   } else { n     return false; n   } n } n template <typename T1, typename T2> n inline bool IS_OUT(const T1 toY, const T1 toX, const T2 H, const T2 W) { n   return (toY < 0 || toY >= H || toX < 0 || toX >= W); n } n template <class T> n inline T SQR(const T x) { n   return x * x; n } n template <class T1, class T2> n inline T1 POW(const T1 x, const T2 y) { n   if (!y) n     return 1; n   else if ((y & 1) == 0) { n     return SQR(POW(x, y >> 1)); n   } else n     return POW(x, y ^ 1) * x; n } n template <typename T> n constexpr T ABS(T x) { n   return x < 0 ? -x : x; n } n template <class Iter> n bool next_partial_permutation(Iter first, Iter middle, Iter last) { n   reverse(middle, last); n   return next_permutation(first, last); n } n template <class Iter> n bool next_combination(Iter first1, Iter last1, Iter first2, Iter last2) { n   if ((first1 == last1) || (first2 == last2)) { n     return false; n   } n   Iter m1 = last1; n   Iter m2 = last2; n   --m2; n   while (--m1 != first1 && !(*m1 < *m2)) { n   } n   bool result = (m1 == first1) && !(*first1 < *m2); n   if (!result) { n     while (first2 != m2 && !(*m1 < *first2)) { n       ++first2; n     } n     first1 = m1; n     std::iter_swap(first1, first2); n     ++first1; n     ++first2; n   } n   if ((first1 != last1) && (first2 != last2)) { n     m1 = last1; n     m2 = first2; n     while ((m1 != first1) && (m2 != last2)) { n       std::iter_swap(--m1, m2); n       ++m2; n     } n     std::reverse(first1, m1); n     std::reverse(first1, last1); n     std::reverse(m2, last2); n     std::reverse(first2, last2); n   } n   return !result; n } n template <typename T> n constexpr bool ODD(T x) { n   return x % 2 != 0; n } n template <typename T> n constexpr bool EVEN(T x) { n   return x % 2 == 0; n } n template <class T> n inline T GCD(const T x, const T y) { n   if (x < 0) return GCD(-x, y); n   if (y < 0) return GCD(x, -y); n   return (!y) ? x : GCD(y, x % y); n } n template <class T> n inline T LCM(const T x, const T y) { n   if (x < 0) return LCM(-x, y); n   if (y < 0) return LCM(x, -y); n   return x * (y / GCD(x, y)); n } n template <class T> n inline T EXTGCD(const T a, const T b, T &x, T &y) { n   if (a < 0) { n     T d = EXTGCD(-a, b, x, y); n     x = -x; n     return d; n   } n   if (b < 0) { n     T d = EXTGCD(a, -b, x, y); n     y = -y; n     return d; n   } n   if (!b) { n     x = 1; n     y = 0; n     return a; n   } else { n     T d = EXTGCD(b, a % b, x, y); n     T t = x; n     x = y; n     y = t - (a / b) * y; n     return d; n   } n } n template <class T> n inline T INV_MOD(const T a, const T m) { n   T x, y; n   EXTGCD(a, m, x, y); n   return (x + m) % m; n } n template <class T> n inline bool ISPRIME(const T x) { n   if (x <= 1) return false; n   for (T i = 2; i * i <= x; i++) n     if (x % i == 0) return false; n   return true; n } n template <class T> n vector<bool> ERATOSTHENES(const T n) { n   vector<bool> arr(n + 1, true); n   arr[0] = arr[1] = false; n   for (T i = 2; i * i <= n; i++) { n     if (arr[i]) { n       for (T j = i * 2LL; j <= n; j += i) { n         arr[j] = false; n       } n     } n   } n   return arr; n } n template <typename T> n vector<bool> ERATOSTHENES(const T a, const T b) { n   vector<bool> small = ERATOSTHENES(b); n   vector<bool> prime(b - a, true); n   for (T i = 2; i * i <= b; i++) { n     if (small[i]) { n       for (T j = max(2LL, (a + i - 1) / i) * i; j < b; j += i) { n         prime[j - a] = false; n       } n     } n   } n   return prime; n } n template <typename T> n vector<T> DIVISOR(T n) { n   vector<T> v; n   for (T i = 1; i * i <= n; ++i) { n     if (n % i == 0) { n       v.push_back(i); n       if (i != n / i) { n         v.push_back(n / i); n       } n     } n   } n   sort(v.begin(), v.end()); n   return v; n } n template <typename T> n vector<vector<T>> DIVISOR_ALL(T n) { n   vector<vector<T>> res(n + 1); n   for (T i = 1; i <= n; i++) { n     for (T j = i; j <= n; j += i) { n       res[j].push_back(i); n     } n   } n   return res; n } n template <typename T> n vector<pair<T, T>> FACTORIZATION(T x) { n   vector<pair<T, long long>> ans; n   for (T i = 2; i * i <= x; i++) { n     if (x % i == 0) { n       T count = 0; n       while (x % i == 0) { n         count++; n         x /= i; n       } n       ans.push_back(make_pair(i, count)); n     } n   } n   if (x != 1) ans.push_back(make_pair(x, 1)); n   return ans; n } n long long POW_MOD(long long N, long long P, long long M) { n   long long ret = 1; n   while (P > 0) { n     if (P & 1LL) ret = (ret * N) % M; n     N = (N * N) % M; n     P >>= 1LL; n   } n   return ret; n } n template <typename T> n inline T NCR(T n, T r) { n   if (r > n - r) r = n - r; n   T ans = 1LL; n   for (T i = 0; i < r; i++) { n     ans = ans * (n - i) / (i + 1); n   } n   return ans; n } n long long NCR_MOD(long long n, long long r, long long M) { n   if (r > n - r) return NCR_MOD(n, n - r, M); n   long long numerator = 1LL; n   long long denominator = 1LL; n   for (long long i = 0; i < r; i++) { n     numerator *= (n - i); n     numerator %= M; n     denominator *= (i + 1); n     denominator %= M; n   } n   return numerator * POW_MOD(denominator, M - 2, M) % M; n } n int main() { n   int T; n   cin >> T; n   while (T--) { n     long long n, m, k; n     cin >> n >> m >> k; n     vector<long long> h(n); n     for (int i = (0); i < (n); ++i) scanf( %lld , &h[i]); n     bool good = true; n     for (int i = 0; i < n - 1; i++) { n       long long as = max(0LL, h[i + 1] - k); n       if (h[i] >= as) { n         m += h[i] - as; n         continue; n       } n       m -= (as - h[i]); n       if (m < 0) { n         good = false; n       } n     } n     if (good) n       printf( YES n ); n     else n       printf( NO n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int imax = 1e9 + 7; n const long long lmax = 1e18; n const int maxn = 2005; n int P[maxn], C[maxn], A[maxn]; n vector<int> adj[maxn]; n vector<int> dfs(int cur) { n   vector<int> ans(0), v(0); n   for (int next : adj[cur]) { n     v = dfs(next); n     ans.insert(ans.end(), v.begin(), v.end()); n   } n   if (((int)ans.size()) < C[cur]) { n     cout <<  NO n ; n     exit(0); n   } n   ans.insert(ans.begin() + C[cur], cur); n   return ans; n } n int calc() { n   int i, n; n   cin >> n; n   for (i = 0; i < maxn; ++i) adj[i].clear(); n   for (i = 1; i < n + 1; ++i) { n     cin >> P[i] >> C[i]; n     adj[P[i]].push_back(i); n   } n   vector<int> v = dfs(adj[0][0]); n   for (i = 0; i < ((int)v.size()); ++i) { n     A[v[i]] = i + 1; n   } n   cout <<  YES n ; n   for (i = 1; i < n + 1; ++i) cout << A[i] <<    n [i == n]; n   return 0; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   calc(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void fastIO() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.precision(20); n } n long long A[100006]; n long long B[100006]; n long long C[100006]; n long long rev(long long a, long long b) { return a > b; } n int main() { n   fastIO(); n   vector<long long> V; n   set<long long> S; n   set<long long>::iterator it; n   map<string, long long> M; n   map<string, long long>::iterator it1; n   long long a, b, c; n   long long n, m; n   long long cnt = 0, cntr = 0, sum = 0, ans = 1; n   long long p, q, r; n   long long v = 1; n   long long check = 0; n   long long x, y, z; n   long long Max = 0, Min = INT_MAX; n   string s, s1[100006], s2[100006], s3, s4; n   cin >> n >> m; n   for (int i = 0; i < n; i++) cin >> A[i]; n   sort(A, A + n); n   for (int i = 0; i < n; i++) { n     sum += (A[i] * m--); n     if (m == 0) m = 1; n   } n   cout << sum << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count()); n const long long inf = 1e17 + 7; n string first(long long num) { n   string second =   ; n   for (long long i = 0; i < num; i++) second +=  * ; n   return second; n } n signed main() { n   cin.tie(0); n   cout.tie(0); n   ios_base::sync_with_stdio(0); n   cout.precision(10); n   map<string, long long> a; n   a[ void ] = 0; n   long long n; n   cin >> n; n   while (n--) { n     string type, st, to; n     cin >> type >> st; n     long long l = st.find_first_not_of( & ); n     long long r = st.find_last_not_of( * ); n     long long cnt = st.size() - r - 1 - l; n     string temp = st.substr(l, r - l + 1); n     if (type ==  typeof ) { n       if (a.find(temp) == a.end() || a[temp] + min(0ll, cnt) < 0) n         cout <<  errtype  << endl; n       else n         cout <<  void  << first(a[temp] + cnt) << endl; n     } else { n       cin >> to; n       if (a.find(temp) == a.end() || a[temp] < 0) n         a[to] = -1; n       else n         a[to] = a[temp] + cnt; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, x; n bool vis[300010]; n int a[300010]; n int ans = 1; n int main() { n   scanf( %d , &n); n   x = n; n   printf( %d  , ans); n   for (int i = 1; i < n; i++) { n     scanf( %d , &a[i]); n     vis[a[i]] = 1; n     bool p = 0; n     bool v = 0; n     while (vis[x]) { n       ans--; n       x--; n     } n     ans++; n     printf( %d  , ans); n   } n   printf( %d  , 1); n } n


#include <bits/stdc++.h> n using namespace std; n struct piece { n   int x, y; n   char p[20][20]; n   piece(int _x, int _y) : x(_x), y(_y) {} n   bool operator<(const piece& rhs) const { n     if ((x * y) != (rhs.x * rhs.y)) n       return (x * y) < (rhs.x * rhs.y); n     else n       return x < rhs.x; n   } n }; n struct Piece { n   int x, y; n   char p[20][20]; n   Piece() {} n   bool operator==(const Piece& rhs) const { n     bool eq = true; n     for (int i = 0; i < x; i++) n       for (int j = 0; j < y; j++) n         if (p[i][j] != rhs.p[i][j]) { n           eq = false; n           goto n1; n         } n   n1: n     if (eq) return true; n     char tmp[20][20]; n     if (x == y) { n       for (int i = 0; i < x; i++) n         for (int j = 0; j < y; j++) tmp[i][j] = rhs.p[y - (j + 1)][i]; n       eq = true; n       for (int i = 0; i < x; i++) n         for (int j = 0; j < y; j++) n           if (p[i][j] != tmp[i][j]) { n             eq = false; n             goto n2; n           } n     n2: n       if (eq) return true; n     } n     for (int i = 0; i < x; i++) n       for (int j = 0; j < y; j++) tmp[i][j] = rhs.p[x - (i + 1)][y - (j + 1)]; n     eq = true; n     for (int i = 0; i < x; i++) n       for (int j = 0; j < y; j++) n         if (p[i][j] != tmp[i][j]) { n           eq = false; n           goto n3; n         } n   n3: n     if (eq) return true; n     if (x == y) { n       for (int i = 0; i < x; i++) n         for (int j = 0; j < y; j++) tmp[i][j] = rhs.p[j][x - (i + 1)]; n       eq = true; n       for (int i = 0; i < x; i++) n         for (int j = 0; j < y; j++) n           if (p[i][j] != tmp[i][j]) { n             eq = false; n             goto n4; n           } n     n4: n       if (eq) return true; n     } n     return false; n   } n }; n char p[20][20]; n set<int> fa, fb; n set<piece> P; n int a, b; n int good(int x, int y) { n   vector<Piece> ps; n   int ih = a / x, jh = b / y; n   for (int i = 1; i <= ih; i++) n     for (int j = 1; j <= jh; j++) { n       Piece tmp; n       tmp.x = x; n       tmp.y = y; n       int kh = (i - 1) * x + x - 1, lh = (j - 1) * y + y - 1; n       for (int k = (i - 1) * x, m = 0; k <= kh; k++, m++) n         for (int l = (j - 1) * y, n = 0; l <= lh; l++, n++) n           tmp.p[m][n] = p[k][l]; n       for (unsigned int k = 0; k < ps.size(); k++) n         if (ps[k] == tmp) return false; n       ps.push_back(tmp); n     } n   return true; n } n int main() { n   cin >> a >> b; n   for (int i = 0; i < a; i++) n     for (int j = 0; j < b; j++) cin >> p[i][j]; n   for (int i = 1; i <= a; i++) n     if (!(a % i)) fa.insert(i); n   for (int i = 1; i <= b; i++) n     if (!(b % i)) fb.insert(i); n   for (set<int>::iterator ita = fa.begin(); ita != fa.end(); ita++) n     for (set<int>::iterator itb = fb.begin(); itb != fb.end(); itb++) { n       int x = *ita, y = *itb; n       if (good(x, y)) P.insert(piece(x, y)); n     } n   cout << P.size() << endl; n   cout << (*(P.begin())).x <<     << (*(P.begin())).y << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int l, r, i, j, d, tp; n   cin >> l >> r; n   bool f; n   for (i = l; i <= r; i++) { n     tp = i; n     int arr[10]; n     for (j = 0; j < 10; j++) arr[j] = 0; n     while (tp > 0) { n       d = tp % 10; n       tp = tp / 10; n       arr[d]++; n     } n     f = true; n     for (j = 0; j < 10; j++) { n       if (arr[j] <= 1) n         f = true; n       else { n         f = false; n         break; n       } n     } n     if (!f) n       f = false; n     else { n       cout << i; n       f = true; n       break; n     } n   } n   if (!f) cout <<  -1 ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using LL = long long; n using VI = vector<int>; n using VC = vector<char>; n using VS = vector<string>; n using VL = vector<long long>; n using VVI = vector<VI>; n using VVL = vector<VL>; n using MII = map<int, int>; n using MIVI = map<int, VI>; n using MSS = map<string, string>; n using MLL = map<LL, LL>; n int READ_INT() { n   int temp; n   cin >> temp; n   return temp; n } n LL READ_LONG() { n   LL temp; n   cin >> temp; n   return temp; n } n void mcs(VI &series, int &max_sum) { n   int s = 0; n   for (auto x : series) { n     s += x; n     max_sum = max(max_sum, s); n     if (s < 0) s = 0; n   } n } n template <typename T> n T MAX(T t) { n   return t; n } n template <typename T, typename... Args> n T MAX(T t, Args... args) { n   return max(t, MAX(args...)); n } n const int MOD = int(1e9) + 7; n const int INF = 1e9 + 5; n const double PI = acos(-1.0); n const double EPS = 1e-9; n int n, m, k; n int matrix[105][105][105]; n int main() { n   ios::sync_with_stdio(false); n   cin >> n >> m >> k; n   for (int i = 1; i < n + 1; ++i) { n     for (int j = 1; j < m + 1; ++j) { n       string s; n       cin >> s; n       for (int q = 0; q < k; ++q) { n         matrix[i][j][q + 1] = s[q] -  0 ; n       } n     } n   } n   int count = 0; n   int l = k; n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= m; ++j) { n       for (int k = 1; k <= l; ++k) { n         if (matrix[i][j][k] == 0) continue; n         int t = 0; n         t += (matrix[i][j][k - 1] && matrix[i][j][k + 1]); n         t += (matrix[i][j - 1][k] && matrix[i][j + 1][k]); n         t += (matrix[i - 1][j][k] && matrix[i + 1][j][k]); n         if ((matrix[i][j][k - 1] && matrix[i][j + 1][k]) && n             matrix[i][j + 1][k - 1] == 0) n           t++; n         if ((matrix[i][j - 1][k] && matrix[i][j][k + 1]) && n             matrix[i][j - 1][k + 1] == 0) n           t++; n         if ((matrix[i][j - 1][k] && matrix[i + 1][j][k]) && n             matrix[i + 1][j - 1][k] == 0) n           t++; n         if ((matrix[i - 1][j][k] && matrix[i][j + 1][k]) && n             matrix[i - 1][j + 1][k] == 0) n           t++; n         if ((matrix[i - 1][j][k] && matrix[i][j][k + 1]) && n             matrix[i - 1][j][k + 1] == 0) n           t++; n         if ((matrix[i][j][k - 1] && matrix[i + 1][j][k]) && n             matrix[i + 1][j][k - 1] == 0) n           t++; n         if (t) count++; n       } n     } n   } n   cout << count <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 15000001; n int64_t primes[N]; n int64_t gcd(int64_t a, int64_t b) { n   if (a == 0) n     return b; n   else n     return gcd(b % a, a); n } n int64_t Power(int64_t base, int64_t e) { n   if (e == 0) return 1; n   int64_t ans = base; n   e--; n   while (e) { n     if (e & 1) ans = (ans * base); n     base = (base * base); n     e >>= 1; n   } n   return (ans); n } n void sieve() { n   for (int64_t i = 2; i <= N; i++) { n     if (primes[i] == -1) { n       for (int64_t j = i; j <= N; j += i) primes[j] = i; n     } n   } n } n int main() { n   for (int64_t i = 0; i < N; i++) { n     primes[i] = -1; n   } n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   sieve(); n   map<int64_t, int64_t> mp; n   int64_t n; n   cin >> n; n   int64_t a[n]; n   int64_t tmp; n   for (int64_t i = 0; i < n; i++) cin >> a[i]; n   int64_t ggcd; n   ggcd = a[0]; n   for (int64_t i = 0; i < n; i++) { n     ggcd = gcd(ggcd, a[i]); n   } n   int64_t ggcd2 = 1; n   for (int64_t i = 0; i < n; i++) { n     a[i] = a[i] / ggcd; n   } n   for (int64_t i = 0; i < n; i++) { n     tmp = a[i]; n     while (tmp > 1) { n       int64_t y = primes[tmp]; n       while (tmp % y == 0) { n         tmp = tmp / y; n       } n       mp[y]++; n     } n   } n   int64_t freq = 0; n   int64_t val = ggcd; n   for (auto kk : mp) { n     if (freq <= kk.second) { n       val = kk.first; n       freq = kk.second; n     } n   } n   if (freq) { n     cout << (n - freq) << endl; n   } else n     cout << -1 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int t, i, j, cnt, k, lo, hi, mid; n   cin >> t; n   while (t--) { n     string s, t; n     cin >> s >> t; n     vector<long long int> v[26]; n     for (i = 0; s[i]; i++) { n       v[s[i] -  a ].push_back(i); n     } n     bool flag = true; n     cnt = 1; n     j = -1; n     for (i = 0; t[i]; i++) { n       k = -1; n       lo = 0; n       hi = (int)v[t[i] -  a ].size() - 1; n       while (lo <= hi) { n         mid = (lo + hi) / 2LL; n         if (v[t[i] -  a ][mid] > j) { n           k = v[t[i] -  a ][mid]; n           hi = mid - 1; n         } else { n           lo = mid + 1; n         } n       } n       if (j == -1 and k == -1) { n         flag = false; n         break; n       } else if (k != -1) { n         j = k; n       } else { n         j = -1; n         i--; n         cnt++; n       } n     } n     if (!flag) cnt = -1; n     cout << cnt <<   n ; n   } n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n vector<int> a[10005]; n int main() { n   int n, x, k = 0; n   cin >> n; n   x = (sqrt(1.0 + 8 * n) + 1) / 2; n   cout << x << endl; n   for (int i = 1; i <= x; i++) n     for (int j = i + 1; j <= x; j++) a[i].push_back(++k), a[j].push_back(k); n   for (int i = 1; i <= x; i++, cout << endl) n     for (int j = 0; j < a[i].size(); j++) cout << a[i][j] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char s[200010]; n int n, k; n int sa[200010]; n int ra[200010], tr[200010]; n int lcp[200010]; n int l[200010], r[200010]; n stack<int> ls, rs; n set<long long> ss; n bool CmpSa(int a, int b) { n   if (ra[a] != ra[b]) return ra[a] < ra[b]; n   int aa = a + k < n ? ra[a + k] : -1; n   int bb = b + k < n ? ra[b + k] : -1; n   return aa < bb; n } n void GaoSa() { n   for (int i = 0; i <= n; ++i) { n     sa[i] = i; n     ra[i] = i < n ? s[i] : -1; n   } n   for (k = 1; k <= n; k <<= 1) { n     sort(sa, sa + n + 1, CmpSa); n     tr[sa[0]] = 0; n     for (int i = 1; i <= n; ++i) { n       tr[sa[i]] = tr[sa[i - 1]] + CmpSa(sa[i - 1], sa[i]); n     } n     memcpy(ra, tr, sizeof(int) * (n + 1)); n   } n } n void GaoLcp() { n   int tmp = 0; n   for (int i = 0; i < n; ++i) { n     int rk = ra[i]; n     int j = sa[rk - 1]; n     if (tmp > 0) --tmp; n     while (i + tmp < n && j + tmp < n && s[i + tmp] == s[j + tmp]) { n       ++tmp; n     } n     lcp[rk - 1] = tmp; n   } n } n inline long long Hash(int b, int c) { return b * 1000000ll + c; } n int main() { n   scanf( %s , s); n   n = strlen(s); n   s[n] =  | ; n   memcpy(s + n + 1, s, sizeof(char) * n); n   n = 2 * n + 1; n   GaoSa(); n   GaoLcp(); n   for (int i = 0; i <= n; ++i) { n     while (!rs.empty() && lcp[rs.top()] > lcp[i]) { n       r[rs.top()] = i; n       rs.pop(); n     } n     rs.push(i); n   } n   for (int i = n; i >= 0; --i) { n     while (!ls.empty() && lcp[ls.top()] > lcp[i]) { n       l[ls.top()] = i; n       ls.pop(); n     } n     ls.push(i); n   } n   long long ans = 0; n   for (int i = 1; i < n - 1; ++i) { n     if (lcp[i]) { n       if (ss.find(Hash(lcp[i], l[i])) != ss.end()) continue; n       long long tmp = (r[i] - l[i]) / 2; n       tmp = tmp * (tmp + 1) / 2; n       ans += tmp * (lcp[i] - max(lcp[l[i]], lcp[r[i]])); n       ss.insert(Hash(lcp[i], l[i])); n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename G, typename H> n struct triple { n   G first, second, T; n   triple(G x, G y, G z) : first(x), second(y), T(z) {} n   triple() : first(0), second(0), T(0) {} n   bool operator<(const triple<G, H> other) const { return first > other.first; } n }; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n, m; n   cin >> n >> m; n   vector<int> v(m); n   for (auto &i : v) cin >> i; n   sort(v.begin(), v.end()); n   int g, r; n   cin >> g >> r; n   int oo = 2e9; n   vector<vector<int>> dp(m, vector<int>(g + 1, oo)); n   dp[0][g] = 0; n   deque<pair<int, int>> pq; n   pq.push_back({0, g}); n   while (!pq.empty()) { n     pair<int, int> t = pq.front(); n     pq.pop_front(); n     if (t.second == 0 && dp[t.first][g] > dp[t.first][t.second] + 1) { n       dp[t.first][g] = dp[t.first][t.second] + 1; n       pq.push_back({t.first, g}); n     } n     if (t.second != 0) { n       int i = t.first, j = t.second; n       if (i > 0) { n         int w = v[i] - v[i - 1]; n         if (w <= j && dp[i - 1][j - w] > dp[i][j]) { n           dp[i - 1][j - w] = dp[i][j]; n           pq.push_front({i - 1, j - w}); n         } n       } n       if (i < m - 1) { n         int w = v[i + 1] - v[i]; n         if (w <= j && dp[i + 1][j - w] > dp[i][j]) { n           dp[i + 1][j - w] = dp[i][j]; n           pq.push_front({i + 1, j - w}); n         } n       } n     } n   } n   int ans = oo; n   for (int i = 0; i <= g; i++) n     if (dp[m - 1][i] != oo) ans = min(ans, dp[m - 1][i] * (r + g) + g - i); n   if (ans == oo) ans = -1; n   cout << ans <<   n ; n   return 0; n } n