#include <bits/stdc++.h> n #pragma GCC optimize(2) n #pragma GCC optimize(3) n #pragma GCC optimize( Ofast ) n #pragma GCC optimize( inline ) n #pragma GCC optimize( -fgcse ) n #pragma GCC optimize( -fgcse-lm ) n #pragma GCC optimize( -fipa-sra ) n #pragma GCC optimize( -ftree-pre ) n #pragma GCC optimize( -ftree-vrp ) n #pragma GCC optimize( -fpeephole2 ) n #pragma GCC optimize( -ffast-math ) n #pragma GCC optimize( -fsched-spec ) n #pragma GCC optimize( unroll-loops ) n #pragma GCC optimize( -falign-jumps ) n #pragma GCC optimize( -falign-loops ) n #pragma GCC optimize( -falign-labels ) n #pragma GCC optimize( -fdevirtualize ) n #pragma GCC optimize( -fcaller-saves ) n #pragma GCC optimize( -fcrossjumping ) n #pragma GCC optimize( -fthread-jumps ) n #pragma GCC optimize( -funroll-loops ) n #pragma GCC optimize( -fwhole-program ) n #pragma GCC optimize( -freorder-blocks ) n #pragma GCC optimize( -fschedule-insns ) n #pragma GCC optimize( inline-functions ) n #pragma GCC optimize( -ftree-tail-merge ) n #pragma GCC optimize( -fschedule-insns2 ) n #pragma GCC optimize( -fstrict-aliasing ) n #pragma GCC optimize( -fstrict-overflow ) n #pragma GCC optimize( -falign-functions ) n #pragma GCC optimize( -fcse-skip-blocks ) n #pragma GCC optimize( -fcse-follow-jumps ) n #pragma GCC optimize( -fsched-interblock ) n #pragma GCC optimize( -fpartial-inlining ) n #pragma GCC optimize( no-stack-protector ) n #pragma GCC optimize( -freorder-functions ) n #pragma GCC optimize( -findirect-inlining ) n #pragma GCC optimize( -fhoist-adjacent-loads ) n #pragma GCC optimize( -frerun-cse-after-loop ) n #pragma GCC optimize( inline-small-functions ) n #pragma GCC optimize( -finline-small-functions ) n #pragma GCC optimize( -ftree-switch-conversion ) n #pragma GCC optimize( -foptimize-sibling-calls ) n #pragma GCC optimize( -fexpensive-optimizations ) n #pragma GCC optimize( -funsafe-loop-optimizations ) n #pragma GCC optimize( inline-functions-called-once ) n #pragma GCC optimize( -fdelete-null-pointer-checks ) n #pragma GCC target(   n      sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native ) n using namespace std; n struct node { n   int next, to; n } w[2000010]; n int n, m, cnt = 1, ans[1000005], h[1000005], top, ln, op, c[1000005], n           dfn[1000005], timer, bj[1000005], v[1000005], id[2000005]; n const int MAX_SIZE = 1 << 21; n char *l, *r, buf[MAX_SIZE]; n inline int gc() { n   if (l == r) n     if (l == (r = (l = buf) + fread(buf, 1, MAX_SIZE, stdin))) return -1; n   return *l++; n } n inline void read(int &x) { n   register char c = gc(); n   for (; c < 48 || c > 57; c = gc()) n     ; n   for (x = 0; c > 47 && c < 58; c = gc()) x = (x << 3) + (x << 1) + (c ^ 48); n } n inline void write(int x) { n   if (x > 9) write(x / 10); n   putchar(x % 10 +  0 ); n } n void add(int x, int y, int z) { n   cnt++; n   w[cnt].to = y; n   w[cnt].next = h[x]; n   id[cnt] = z; n   h[x] = cnt; n } n inline void DFS1(int u, int co, int tag) { n   register int i; n   c[u] = co; n   dfn[u] = ++timer; n   for (i = h[u]; i; i = w[i].next) { n     if (i == (tag ^ 1)) { n       continue; n     } n     int e = w[i].to; n     if (c[e] == 0) { n       DFS1(e, 3 - co, i); n     } else { n       if (dfn[u] < dfn[e]) { n         continue; n       } n       if (c[u] == c[e]) { n         op++; n         ln = i; n         bj[u]++; n         bj[e]--; n       } else { n         bj[u]--; n         bj[e]++; n       } n     } n   } n } n inline void DFS2(int u, int tag) { n   register int i; n   v[u] = 1; n   for (i = h[u]; i; i = w[i].next) { n     if (i == (tag ^ 1)) continue; n     int e = w[i].to; n     if (v[e] == 0) { n       DFS2(e, i); n       bj[u] += bj[e]; n     } n   } n   if (bj[u] == op) { n     top++; n     ans[top] = tag; n   } n } n int main() { n   register int i, x, y; n   read(n); n   read(m); n   for (i = 1; i <= m; i++) { n     read(x); n     read(y); n     if (x == y) { n       op++; n       add(0, 0, i); n       ln = cnt; n       continue; n     } n     add(x, y, i); n     add(y, x, i); n   } n   for (i = 1; i <= n; i++) { n     if (c[i] == 0) { n       DFS1(i, 1, 0); n     } n   } n   for (i = 1; i <= n; i++) { n     if (v[i] == 0) { n       DFS2(i, 0); n     } n   } n   if (op == 0) { n     write(m); n     putchar(  n ); n     for (i = 1; i <= m; i++) { n       write(i); n       putchar(   ); n     } n   } else { n     if (op == 1) { n       top++; n       ans[top] = ln; n     } n     write(top); n     putchar(  n ); n     sort(ans + 1, ans + 1 + top); n     for (i = 1; i <= top; i++) { n       write(id[ans[i]]); n       putchar(   ); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n char s[1111][1111]; n int d[1111][1111]; n queue<pair<int, int> > Q; n vector<pair<int, int> > v; n int n, m, lim; n int dx[4] = {1, 0, -1, 0}; n int dy[4] = {0, 1, 0, -1}; n void check(int x, int y, int z) { n   if (x < 0 || x >= n || y < 0 || y >= m) return; n   if (s[x][y] ==  T ) return; n   if (s[x][y] ==  S ) lim = z; n   d[x][y] = z; n   if ( 0  <= s[x][y] && s[x][y] <=  9 ) n     v.push_back(make_pair(s[x][y] -  0 , z)); n   s[x][y] =  T ; n   Q.push(make_pair(x, y)); n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < n; i++) scanf( %s , s[i]); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) n       if (s[i][j] ==  E ) Q.push(make_pair(i, j)); n   while (!Q.empty()) { n     pair<int, int> now = Q.front(), tmp; n     Q.pop(); n     for (int dir = 0; dir < 4; dir++) n       check(now.first + dx[dir], now.second + dy[dir], n             d[now.first][now.second] + 1); n   } n   int ans = 0; n   for (vector<pair<int, int> >::iterator ii = v.begin(); ii != v.end(); ii++) n     if (ii->second <= lim) ans += ii->first; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct less_key { n   bool operator()(pair<double, int64_t> p1, pair<double, int64_t> p2) { n     return (p1.first > p2.first) || n            ((p1.first == p2.first) && (p1.second < p2.second)); n   } n }; n struct pair_hash { n   std::size_t operator()(const pair<int64_t, int64_t>& k) const { n     return static_cast<size_t>(k.first ^ k.second); n   } n }; n const int64_t mod = 100000000007ll; n const int64_t inf = 10000000000000007ll; n const double eps = 0.00000001; n int64_t n, k; n string s; n int64_t amin[256]; n int64_t amax[256]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> k; n   cin >> s; n   for (int64_t i = 0; i < s.size(); i++) { n     if (!amin[s[i]]) { n       amin[s[i]] = i + 1; n     } n     amax[s[i]] = i + 1; n   } n   set<pair<int64_t, int64_t> > q; n   for (int64_t i =  A ; i <=  Z ; i++) { n     if (amin[i]) { n       q.insert({amin[i], 0}); n       q.insert({amax[i], 1}); n     } n   } n   int64_t z = 0; n   for (auto i : q) { n     if (i.second) { n       z--; n     } else { n       z++; n     } n     if (z > k) { n       cout <<  YES  << endl; n       return 0; n     } n   } n   cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n int main() { n   int n; n   cin >> n; n   vector<int> a(n); n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   map<vector<int>, int> S; n   for (int bit = 0; bit < (1 << 15); bit++) { n     vector<int> b(n); n     for (int i = 0; i < n; i++) { n       int c = a[i] ^ bit; n       for (int j = 0; j < 15; j++) { n         if (c & (1 << j)) b[i]++; n       } n     } n     vector<int> vec(n - 1); n     for (int i = 1; i < n; i++) { n       vec[i - 1] = b[i] - b[0]; n     } n     S[vec] = bit; n   } n   for (int bit = 0; bit < (1 << 15); bit++) { n     vector<int> b(n); n     for (int i = 0; i < n; i++) { n       int c = a[i] ^ (bit << 15); n       for (int j = 15; j < 30; j++) { n         if (c & (1 << j)) b[i]++; n       } n     } n     vector<int> vec(n - 1); n     for (int i = 1; i < n; i++) { n       vec[i - 1] = b[0] - b[i]; n     } n     if (S.find(vec) != S.end()) { n       cout << (bit << 15) + S[vec] << endl; n       return 0; n     } n   } n   cout << -1 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, c; n   cin >> a >> b >> c; n   int Time = 0; n   a -= 2; n   int Turn = 2; n   while (a > 0 || b > 0 || c > 0) { n     if (Turn == 1) { n       Time++; n       a -= 2; n       Turn = 2; n     } else if (Turn == 2) { n       Time++; n       b -= 2; n       Turn = 3; n     } else if (Turn == 3) { n       Time++; n       c -= 2; n       Turn = 1; n     } n   } n   Time += 30; n   cout << Time; n } n


#include <bits/stdc++.h> n using namespace std; n int gcd(int x, int y) { n   if (x == 0) return y; n   return gcd(y % x, x); n } n int main() { n   int n, m, x, g; n   cin >> n >> m >> x; n   g = x - 1; n   for (int i = 1; i < n; ++i) { n     scanf( %d , &x); n     g = gcd(g, x - 1); n   } n   set<int> second; n   int d = 1; n   while (d * d <= g) { n     if (g % d == 0) { n       for (int p = 1; p * d <= m; p *= 2) second.insert(p * d); n       for (int p = 1; p * (g / d) <= m; p *= 2) second.insert(p * (g / d)); n     } n     ++d; n   } n   long long ans = 0; n   for (set<int>::iterator it = second.begin(); it != second.end(); it++) n     ans += m - (*it); n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, c, f[6]; n   cin >> a >> b >> c; n   f[0] = a + b + c; n   f[1] = a + b * c; n   f[2] = a * b + c; n   f[3] = a * b * c; n   f[4] = (a + b) * c; n   f[5] = a * (b + c); n   sort(f, f + 6); n   cout << f[5]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long int> ar[100005]; n long long int vis[100005]; n long long int mi; n long long int val[100005]; n void dfs(long long int a) { n   vis[a] = 1; n   mi = min(val[a], mi); n   for (auto it : ar[a]) { n     if (vis[it] == 0) { n       dfs(it); n     } n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int n, i, m, a, b, sum = 0; n   cin >> n >> m; n   for (i = 1; i <= n; i++) cin >> val[i]; n   for (i = 1; i <= m; i++) { n     cin >> a >> b; n     ar[a].push_back(b); n     ar[b].push_back(a); n   } n   for (i = 1; i <= n; i++) { n     if (vis[i] == 0) { n       mi = INT_MAX; n       dfs(i); n       sum = sum + mi; n     } n   } n   cout << sum <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   int arr[10] = {0}; n   string s; n   cin >> n >> s; n   for (int i = 0; i < n; i++) arr[s[i] -  0 ]++; n   if ((arr[1] || arr[2] || arr[3]) && (arr[7] || arr[0] || arr[9]) && n       (((arr[1] || arr[4] || arr[7]) && (arr[3] || arr[6] || arr[9])) || n        arr[0])) n     cout <<  YES ; n   else n     cout <<  NO n ; n } n


#include <bits/stdc++.h> n using namespace std; n using namespace std; n const int INF = 1000000000; n const int inf = INF; n struct Edge { n   int from, to, cap, flow, index; n   Edge(int from, int to, int cap, int flow, int index) n       : from(from), to(to), cap(cap), flow(flow), index(index) {} n }; n struct Dinic { n   int N; n   vector<vector<Edge> > G; n   vector<Edge *> dad; n   vector<int> Q; n   Dinic(int N) : N(N), G(N), dad(N), Q(N) {} n   void AddEdge(int from, int to, int cap) { n     G[from].push_back(Edge(from, to, cap, 0, G[to].size())); n     if (from == to) G[from].back().index++; n     G[to].push_back(Edge(to, from, 0, 0, G[from].size() - 1)); n   } n   long long BlockingFlow(int s, int t) { n     fill(dad.begin(), dad.end(), (Edge *)NULL); n     dad[s] = &G[0][0] - 1; n     int head = 0, tail = 0; n     Q[tail++] = s; n     while (head < tail) { n       int x = Q[head++]; n       for (int i = 0; i < G[x].size(); i++) { n         Edge &e = G[x][i]; n         if (!dad[e.to] && e.cap - e.flow > 0) { n           dad[e.to] = &G[x][i]; n           Q[tail++] = e.to; n         } n       } n     } n     if (!dad[t]) return 0; n     long long totflow = 0; n     for (int i = 0; i < G[t].size(); i++) { n       Edge *start = &G[G[t][i].to][G[t][i].index]; n       int amt = INF; n       for (Edge *e = start; amt && e != dad[s]; e = dad[e->from]) { n         if (!e) { n           amt = 0; n           break; n         } n         amt = min(amt, e->cap - e->flow); n       } n       if (amt == 0) continue; n       for (Edge *e = start; amt && e != dad[s]; e = dad[e->from]) { n         e->flow += amt; n         G[e->to][e->index].flow -= amt; n       } n       totflow += amt; n     } n     return totflow; n   } n   long long GetMaxFlow(int s, int t) { n     long long totflow = 0; n     while (long long flow = BlockingFlow(s, t)) totflow += flow; n     return totflow; n   } n }; n struct node { n   int r, c, t; n   node() {} n   node(int r, int c, int t) : r(r), c(c), t(t) {} n }; n vector<node> qm, qf; n node read() { n   int r, c, t; n   cin >> r >> c >> t; n   r--; n   c--; n   return node(r, c, t); n } n const int MAXN = 25; n const int MAXT = 600; n char mat[MAXN][MAXN]; n int n, m, male, female; n int id[MAXN][MAXN]; n int dis[MAXT][MAXT]; n int main() { n   cin >> n >> m >> male >> female; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) cin >> mat[i][j]; n   if (male < female) n     qm.push_back(read()); n   else n     qf.push_back(read()); n   for (int i = 0; i < male; i++) qm.push_back(read()); n   for (int i = 0; i < female; i++) qf.push_back(read()); n   if (male < female) n     male++; n   else n     female++; n   if (male != female) { n     cout << -1 << endl; n     return 0; n   } n   int tot = 0; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) n       if (mat[i][j] ==  . ) id[i][j] = tot++; n   for (int i = 0; i < tot; i++) n     for (int j = 0; j < tot; j++) dis[i][j] = (i == j) ? 0 : inf; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) n       if (mat[i][j] ==  . ) n         for (int ii = 0; ii < n; ii++) n           for (int jj = 0; jj < m; jj++) n             if (mat[ii][jj] ==  .  && abs(i - ii) + abs(j - jj) == 1) n               dis[id[i][j]][id[ii][jj]] = 1; n   for (int k = 0; k < tot; k++) n     for (int i = 0; i < tot; i++) n       for (int j = 0; j < tot; j++) n         dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]); n   long long lo = 0, hi = 1LL << 60, ans = -1; n   while (lo <= hi) { n     long long mid = (lo + hi) / 2; n     Dinic res(male + female + 2 * tot + 2); n     for (int i = 0; i < (int)qm.size(); i++) { n       int index = id[qm[i].r][qm[i].c]; n       for (int j = 0; j < tot; j++) n         if (dis[index][j] != inf && (long long)dis[index][j] * qm[i].t <= mid) n           res.AddEdge(i, male + j, 1); n     } n     for (int j = 0; j < tot; j++) res.AddEdge(male + j, male + tot + j, 1); n     for (int j = 0; j < tot; j++) n       for (int k = 0; k < (int)qf.size(); k++) { n         int index = id[qf[k].r][qf[k].c]; n         if (dis[index][j] != inf && (long long)dis[index][j] * qf[k].t <= mid) n           res.AddEdge(male + tot + j, male + tot + tot + k, 1); n       } n     int source = male + tot + tot + female; n     int sink = source + 1; n     for (int i = 0; i < male; i++) res.AddEdge(source, i, 1); n     for (int i = 0; i < female; i++) res.AddEdge(male + tot + tot + i, sink, 1); n     if (res.GetMaxFlow(source, sink) == male) n       ans = mid, hi = mid - 1; n     else n       lo = mid + 1; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[200000 + 1]; n int n; n int b[200000 + 1]; n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     b[a[i]] = 0; n   } n   int max = -1; n   int k = 0; n   int numb; n   for (int i = n; i > 0; i--) { n     k = 0; n     if (b[a[i]] == 0) { n       b[a[i]] = 1; n       k = n - i; n     } n     if (k > max) { n       max = k; n       numb = a[i]; n     } n   } n   cout << numb; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int modulo = 1000000009; n long long A(int events, int days) { n   if (events < days) return 0; n   long long result = 1; n   for (int e = events - 1; e >= days; --e) { n     result *= e; n     result %= modulo; n   } n   for (int i = 0; i < days; i++) { n     result *= events; n     result %= modulo; n     --events; n   } n   return result; n } n int main() { n   int n, w, b; n   scanf( %i%i%i , &n, &w, &b); n   long long result = 0; n   for (int i = 2; i < n; i++) { n     result += ((i - 1) * A(w, i)) % modulo * A(b, n - i); n     result %= modulo; n   } n   printf( %i n , (int)result); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double p[100005], e1[100005], e2[100005]; n int main() { n   int n; n   scanf( %d , &n); n   for (register int i = 1; i <= n; ++i) scanf( %lf , p + i); n   for (register int i = 1; i <= n; ++i) { n     e1[i] = (e1[i - 1] + 1) * p[i]; n     e2[i] = e2[i - 1] + (e1[i - 1] + e1[i - 1] + 1) * p[i]; n   } n   printf( %.15lf , e2[n]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = (int)3e3 + 5; n const int INF = (int)1e9; n const string T =  aeiou ; n int dp[MAXN], par[MAXN]; n bool space[MAXN]; n bool vowel[256]; n string s; n int n; n bool bad(char a, char b, char c) { n   if (a == b && b == c) { n     return 0; n   } n   if (!vowel[a] && !vowel[b] && !vowel[c]) { n     return 1; n   } n   return 0; n } n void solve() { n   cin >> s; n   n = s.size(); n   s =  #  + s; n   for (char c : T) { n     vowel[c] = 1; n   } n   for (int i = 1; i <= n; ++i) { n     dp[i] = INF; n   } n   for (int l = 1; l <= n; ++l) { n     bool good = 1; n     for (int r = l; r <= n; ++r) { n       if (r - 2 >= l && bad(s[r - 2], s[r - 1], s[r])) { n         good = 0; n         break; n       } n       if (dp[l - 1] + 1 < dp[r]) { n         dp[r] = dp[l - 1] + 1; n         par[r] = l - 1; n       } n     } n   } n   for (int v = n; v > 0; v = par[v]) { n     space[v] = 1; n   } n   for (int i = 1; i <= n; ++i) { n     printf( %c , s[i]); n     if (space[i]) { n       printf(   ); n     } n   } n } n int main() { n   int tt = 1; n   while (tt--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int vis[6000]; n int dp[5][100], mp[5][55], num[55]; n int main() { n   int n; n   cin >> n; n   for (int i = 1; i <= 2; i++) n     for (int j = 1; j < n; j++) cin >> mp[i][j]; n   for (int i = 1; i <= n; i++) cin >> num[i]; n   memset(dp, 0, sizeof dp); n   for (int i = n - 1; i >= 1; i--) dp[2][i] = dp[2][i + 1] + mp[2][i]; n   int t1 = 10000000, t2 = 10000000, cnt = 0; n   for (int i = 1; i <= n; i++) { n     int kl = dp[2][i] + num[i] + cnt; n     if (kl < t2) { n       if (kl < t1) n         t2 = t1, t1 = kl; n       else n         t2 = kl; n     } n     cnt += mp[1][i]; n   } n   int ans = t1 + t2; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n set<string> ss; n string s; n void readInput() { cin >> s; } n void solve() { n   for (int i = 0; i <= s.length(); i++) { n     for (char c =  a ; c <=  z ; c++) { n       string tmp; n       tmp += s.substr(0, i) + c + s.substr(i, s.length()); n       ss.insert(tmp); n     } n   } n   cout << ss.size(); n } n int main() { n   readInput(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t, n; n   scanf( %d , &t); n   while (t--) { n     scanf( %d , &n); n     printf( %d n , n - 1); n     for (int i = 2; i <= n; ++i) printf( %d  , i); n     printf(  n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_TAILLE = 100 * 1000 + 3; n long long T[1]; n long long paire[MAX_TAILLE], impaire[MAX_TAILLE]; n int n, e, a; n long long best; n int main() { n   paire[0] = 0; n   impaire[0] = 0; n   int p = 1, ip = 1; n   cin >> n >> e; n   T[0] = 0; n   for (int i = 1; i <= n; i++) { n     cin >> a; n     if (i % 2 == 1) { n       paire[p] = paire[p - 1] + a - T[0]; n       p++; n     } else { n       impaire[ip] = impaire[ip - 1] + a - T[0]; n       ip++; n     } n     T[0] = a; n   } n   if ((n + 1) % 2 == 1) { n     paire[p] = paire[p - 1] + e - T[0]; n     ip--; n   } else { n     impaire[ip] = impaire[ip - 1] + e - T[0]; n     p--; n   } n   best = paire[p]; n   for (int i = 1; i <= p; i++) { n     best = max(best, paire[i] + impaire[ip] - impaire[i - 1] - 1); n   } n   cout << best; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma GCC optimize( Ofast ) n #pragma GCC optimize( unroll-loops ) n const long long maxn = 4005, maxk = 1e5 + 2, mod = 1e9 + 7; n long long a[maxn]; n vector<long long> v; n bool mrk[maxn]; n int32_t main() { n   long long t; n   cin >> t; n   while (t--) { n     long long n, mx = 0, lst = 0; n     cin >> n >> mx; n     for (long long i = 1; i < 2 * n; i++) { n       cin >> a[i]; n       if (a[i] > mx) { n         mx = a[i]; n         v.push_back(i - lst); n         lst = i; n       } n     } n     v.push_back(2 * n - lst); n     for (long long i = 1; i <= n; i++) mrk[i] = false; n     mrk[0] = true; n     for (long long i = 0; i < (long long)(v).size(); i++) { n       for (long long j = n - v[i]; j >= 0; j--) mrk[j + v[i]] |= mrk[j]; n     } n     if (mrk[n]) n       cout <<  YES  <<   n ; n     else n       cout <<  NO  <<   n ; n     v.clear(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   cin >> n >> k; n   int turn = 2 * k + 1; n   if (n % turn == 0) n     cout << n / turn <<    ; n   else n     cout << n / turn + 1 <<    ; n   double upperbound = (n - 1) % turn + 1; n   int MAXN = 0; n   upperbound /= 2; n   int temp = ceil(upperbound); n   for (int i = min(k + 1, temp); i <= n; i += turn) { n     cout << i <<    ; n     MAXN = i; n   } n   if (MAXN + k < n) cout << MAXN + 2 * k + 1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, l, r; n   cin >> n >> l >> r; n   cout << (n - l) + (int)pow(2, l) - 1 <<     n        << (n - r) * ((int)pow(2, r - 1)) + ((int)pow(2, r) - 1) << endl; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   char c; n   int x = 0; n   int b = 1; n   do { n     c = getchar(); n     if (c == 45) b = -1; n   } while (c > 57 || c < 48); n   do x = x * 10 + c - 48, c = getchar(); n   while (c <= 57 && c >= 48); n   x *= b; n   return x; n } n int n, m, t; n const int maxn = 300 + 5; n const int logn = 10 + 5; n int f[logn][maxn][maxn]; n int g[maxn][maxn], w[maxn][maxn]; n int main() { n   n = read(), m = read(); n   t = log2(n); n   memset(f, 0xcf, sizeof(f)); n   memset(w, 0xcf, sizeof(w)); n   for (int i = 1, x, y, z, l; i <= m; i++) { n     x = read(), y = read(); n     z = read(); n     l = read(); n     f[0][x][y] = z, f[0][y][x] = l; n   } n   for (int i = 1; i <= n; i++) w[i][i] = f[0][i][i] = 0; n   for (int s = 1; s <= 10; s++) { n     for (int k = 1; k <= n; k++) { n       for (int i = 1; i <= n; i++) { n         for (int j = 1; j <= n; j++) { n           f[s][i][j] = max(f[s][i][j], f[s - 1][i][k] + f[s - 1][k][j]); n         } n       } n     } n   } n   int ans = 0; n   for (int s = 10; s >= 0; s--) { n     memset(g, 0xcf, sizeof(g)); n     for (int k = 1; k <= n; k++) { n       for (int i = 1; i <= n; i++) { n         for (int j = 1; j <= n; j++) { n           g[i][j] = max(g[i][j], w[i][k] + f[s][k][j]); n         } n       } n     } n     int sig = 0; n     for (int i = 1; i <= n; i++) n       if (g[i][i] > 0) sig = 1; n     if (sig) n       continue; n     else { n       for (int i = 1; i <= n; i++) { n         for (int j = 1; j <= n; j++) { n           w[i][j] = g[i][j]; n         } n       } n       ans += (1 << s); n     } n   } n   printf( %d n , ans >= n ? 0 : ans + 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int a[1001]; n map<long long int, int> used; n int ans = 0; n map<pair<long long int, long long int>, bool> us; n void rec(long long int x, long long int y, int kol) { n   ans = max(ans, kol); n   long long int z = x + y; n   if (z > 1000000000 || z < -1000000000) return; n   x = y; n   y = z; n   if (used[z] > 0) { n     used[z]--; n     rec(x, y, kol + 1); n     used[z]++; n   } n } n int main() { n   int n; n   cin >> n; n   for (int i = 1; i <= n; ++i) { n     cin >> a[i]; n     used[a[i]]++; n   } n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= n; ++j) { n       if (us[make_pair(a[i], a[j])] || i == j) continue; n       us[make_pair(a[i], a[j])] = true; n       used[a[i]]--; n       used[a[j]]--; n       rec(a[i], a[j], 2); n       used[a[i]]++; n       used[a[j]]++; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n long long int M = 1000000007; n using namespace std; n vector<int> cnt(1e6 + 1); n void SieveOfEratosthenes(int n) { n   bool prime[n + 1]; n   memset(prime, true, sizeof(prime)); n   for (int p = 2; p * p <= n; p++) { n     if (prime[p] == true) { n       for (int i = p * p; i <= n; i += p) prime[i] = false; n     } n   } n   int c = 0; n   cnt[1] = c; n   cnt[0] = c; n   for (int p = 2; p <= n; p++) n     if (prime[p]) n       cnt[p] = ++c; n     else n       cnt[p] = c; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cout << setprecision(25); n   ; n   SieveOfEratosthenes(1e6 + 1); n   int T; n   cin >> T; n   while (T--) { n     int n; n     cin >> n; n     int r = floor(sqrt(n)); n     cout << 1 + cnt[n] - cnt[r] <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const long long INFF = 0x3f3f3f3f3f3f3f3fll; n const long long M = 1e9 + 7; n const long long maxn = 1e6 + 107; n const double pi = acos(-1.0); n const double eps = 0.0000000001; n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n template <typename T> n inline void pr2(T x, int k = 64) { n   long long i; n   for (i = 0; i < k; i++) fprintf(stderr,  %d , (x >> i) & 1); n   putchar(   ); n } n template <typename T> n inline void add_(T &A, int B, long long MOD = M) { n   A += B; n   (A >= MOD) && (A -= MOD); n } n template <typename T> n inline void mul_(T &A, long long B, long long MOD = M) { n   A = (A * B) % MOD; n } n template <typename T> n inline void mod_(T &A, long long MOD = M) { n   A %= MOD; n   A += MOD; n   A %= MOD; n } n template <typename T> n inline void max_(T &A, T B) { n   (A < B) && (A = B); n } n template <typename T> n inline void min_(T &A, T B) { n   (A > B) && (A = B); n } n template <typename T> n inline T abs(T a) { n   return a > 0 ? a : -a; n } n inline long long powMM(long long a, long long b, long long mod = M) { n   long long ret = 1; n   for (; b; b >>= 1ll, a = a * a % mod) n     if (b & 1) ret = ret * a % mod; n   return ret; n } n int startTime; n void startTimer() { startTime = clock(); } n void printTimer() { n   fprintf(stderr,  /--- Time: %ld milliseconds ---/ n , clock() - startTime); n } n const int mod = 1e9 + 7; n struct mint { n   long long x; n   mint() : x(0) {} n   mint(long long x) : x((x % mod + mod) % mod) {} n   mint &fix() { n     x = (x % mod + mod) % mod; n     return *this; n   } n   mint operator-() const { return mint(0) - *this; } n   mint operator~() const { return mint(1) / *this; } n   mint &operator+=(const mint &a) { n     if ((x += a.x) >= mod) x -= mod; n     return *this; n   } n   mint &operator-=(const mint &a) { n     if ((x += mod - a.x) >= mod) x -= mod; n     return *this; n   } n   mint &operator*=(const mint &a) { n     (x *= a.x) %= mod; n     return *this; n   } n   mint &operator/=(const mint &a) { n     (x *= a.pow(mod - 2).x) %= mod; n     return *this; n   } n   mint operator+(const mint &a) const { return mint(*this) += a; } n   mint operator-(const mint &a) const { return mint(*this) -= a; } n   mint operator*(const mint &a) const { return mint(*this) *= a; } n   mint operator/(const mint &a) const { return mint(*this) /= a; } n   mint pow(long long t) const { n     if (!t) return 1; n     mint res = pow(t / 2); n     res *= res; n     return (t & 1) ? res * x : res; n   } n   bool operator<(const mint &a) const { return x < a.x; } n   bool operator==(const mint &a) const { return x == a.x; } n }; n int bruteforce(string s) { n   set<string> S, T; n   S.insert(  ); n   for (auto c : s) { n     for (auto str : S) { n       if (str.length() == 0) n         T.insert(str + c); n       else { n         if (c ==  0 ) { n           T.insert(str); n           T.insert(str +  0 ); n         } n         if (c ==  1 ) { n           T.insert(str +  1 ); n           *str.rbegin() =  1 ; n           T.insert(str); n         } n       } n     } n     S.swap(T); n     set<string>().swap(T); n   } n   printf( %d n , (int)T.size()); n   return T.size(); n } n mint val_kzero[maxn], to_next[maxn]; n mint dp[maxn]; n char str[maxn]; n int main() { n   scanf( %s , str + 1); n   int n = strlen(str + 1), i; n   for (i = 1; i <= n; i++) n     if (str[i] ==  1 ) break; n   if (i == n + 1) return 0 * printf( %d  , n); n   int noww = i - 1; n   n -= noww; n   for (i = 1; i <= n; i++) str[i] = str[i + noww]; n   int count = 0; n   to_next[0] = mint(1); n   for (i = 1; i <= n; i++) { n     if (str[i] ==  1 ) { n       count = 0; n       dp[i] = to_next[count]; n       to_next[0] = dp[i]; n       to_next[1] += dp[i]; n     } else { n       count++; n       dp[i] = to_next[count]; n       to_next[count] = mint(0); n       to_next[count + 1] += dp[i]; n       to_next[0] += dp[i]; n     } n     val_kzero[count] += dp[i]; n   } n   mint ans(0); n   for (i = 0; i <= count; i++) ans += val_kzero[i]; n   printf( %lld n , ans.x * (noww + 1) % M); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int i, j, k, m, n, boys[105], girls[105]; n   while (cin >> n >> m) { n     memset(boys, 0, sizeof(boys)); n     memset(girls, 0, sizeof(girls)); n     cin >> k; n     while (k--) { n       cin >> j; n       boys[j] = 1; n     } n     cin >> k; n     while (k--) { n       cin >> j; n       girls[j] = 1; n     } n     int sqr = max(n, m); n     sqr = sqr * sqr; n     for (i = 0; i < sqr; i++) { n       j = (i % n); n       k = (i % m); n       boys[j] = (boys[j] || girls[k]); n       girls[k] = (boys[j] || girls[k]); n     } n     bool possible = true; n     for (i = 0; i < n; i++) { n       if (boys[i] == 0) possible = false; n     } n     for (i = 0; i < m; i++) { n       if (girls[i] == 0) possible = false; n     } n     if (possible) n       cout <<  Yes n ; n     else n       cout <<  No n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MN = 1e5 + 5; n int T, N, arr[MN]; n void solve() { n   scanf( %d , &N); n   for (int i = 0; i < N; i++) scanf( %d , arr + i); n   sort(arr, arr + N); n   printf( %d n , min(min(arr[N - 1], arr[N - 2]) - 1, N - 2)); n } n int main() { n   scanf( %d , &T); n   while (T--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1210, inf = 0x3f3f3f3f; n int n, m, k, x, y, rd[N], s, t, cnt = 1, head[N], stk[N], col[N], tot, sum, n                                 vis[N]; n struct edge { n   int to, nxt, c; n } e[N * 10]; n void Adde(int x, int y, int c) { n   e[++cnt].to = y; n   e[cnt].nxt = head[x]; n   head[x] = cnt; n   e[cnt].c = c; n } n void adde(int x, int y, int c) { n   Adde(x, y, c); n   Adde(y, x, 0); n } n void gg() { n   for (int i = (1); i <= (m); i++) printf( 0  ); n   puts(  ); n } n bool bfs() { n   queue<int> q; n   q.push(s); n   for (int i = (1); i <= (t); i++) vis[i] = -1; n   vis[s] = 1; n   while (!q.empty()) { n     int u = q.front(); n     q.pop(); n     for (int i = head[u], v; i; i = e[i].nxt) n       if (v = e[i].to, e[i].c && vis[v] == -1) vis[v] = vis[u] + 1, q.push(v); n   } n   return vis[t] != -1; n } n int dfs(int u, int flow) { n   if (u == t) return flow; n   int used = 0, w; n   for (int i = head[u], v; i; i = e[i].nxt) n     if (v = e[i].to, e[i].c && vis[v] == vis[u] + 1) { n       w = dfs(v, min(e[i].c, flow - used)); n       e[i].c -= w; n       e[i ^ 1].c += w; n       used += w; n       if (used == flow) return flow; n     } n   if (!used) vis[u] = -1; n   return used; n } n int dinic() { n   int res = 0; n   while (bfs()) res += dfs(s, inf); n   return res; n } n int Main() { n   scanf( %d%d%d , &n, &m, &k); n   s = n + m + 1, t = s + 1; n   tot = sum = 0; n   for (int i = (1); i <= (m); i++) { n     scanf( %d%d , &x, &y), rd[x]++, rd[y]++; n     adde(i + n, x, 1), adde(i + n, y, 1), adde(s, i + n, 1); n   } n   for (int i = (1); i <= (n); i++) n     if (rd[i] > k) adde(i, t, 2 * (rd[i] - k)), sum += 2 * (rd[i] - k); n   if (dinic() == sum) { n     memset(col, 0, m + 1 << 2); n     for (int u = (1); u <= (n); u++) { n       int top = 0; n       for (int i = head[u]; i; i = e[i].nxt) n         if (e[i].c) stk[++top] = e[i].to - n; n       for (int i = 1; i <= top; i += 2) col[stk[i]] = col[stk[i + 1]] = ++tot; n     } n     for (int i = (1); i <= (m); i++) n       if (!col[i]) col[i] = ++tot; n     for (int i = (1); i <= (m); i++) printf( %d  , col[i]); n     puts(  ); n   } else n     gg(); n   cnt = 1; n   memset(head, 0, t + 1 << 2); n   memset(rd, 0, n + 1 << 2); n   return 0; n } n int main() { n   int T; n   scanf( %d , &T); n   while (T--) Main(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 200010; n vector<int> g[maxn + 1]; n int father[maxn + 1], a[maxn + 1]; n int f[maxn + 1], fup[maxn + 1]; n int n, K; n int result; n int ge[maxn + 1], upge[maxn + 1]; n int MIN; n void init() { n   int i, x, y; n   scanf( %d%d , &n, &K); n   for (i = 1; i <= n; ++i) scanf( %d , &a[i]); n   for (i = 1; i < n; ++i) { n     scanf( %d%d , &x, &y); n     g[x].push_back(y); n     g[y].push_back(x); n   } n } n void calc_ge(int p, int fa) { n   int i, j; n   father[p] = fa; n   ge[p] = 1; n   for (i = 0; i < g[p].size(); ++i) { n     j = g[p][i]; n     if (j != fa) { n       calc_ge(j, p); n       ge[p] += ge[j]; n     } n   } n } n void calc_f(int p) { n   int i, j, zd = 0, sum = 0; n   if ((g[p].size() == 1) && (father[p] == g[p][0])) { n     if (a[p] >= MIN) n       f[p] = 1; n     else n       f[p] = 0; n     return; n   } n   if (a[p] < MIN) { n     f[p] = 0; n     for (i = 0; i < g[p].size(); ++i) { n       j = g[p][i]; n       if (j != father[p]) calc_f(j); n     } n     return; n   } n   for (i = 0; i < g[p].size(); ++i) { n     j = g[p][i]; n     if (j != father[p]) { n       calc_f(j); n       if (f[j] == ge[j]) n         sum += f[j]; n       else { n         if (f[j] > zd) zd = f[j]; n       } n     } n   } n   f[p] = 1 + sum + zd; n } n void calc_fup(int p) { n   int i, j, zd = 0, zd2 = 0, sum = 0; n   if ((g[p].size() == 1) && (father[p] == g[p][0])) return; n   if (a[p] < MIN) { n     for (i = 0; i < g[p].size(); ++i) { n       j = g[p][i]; n       if (j != father[p]) { n         fup[j] = 0; n         calc_fup(j); n       } n     } n     return; n   } n   if (fup[p] == upge[p]) n     sum = fup[p]; n   else n     zd = fup[p]; n   for (i = 0; i < g[p].size(); ++i) { n     j = g[p][i]; n     if (j != father[p]) { n       if (f[j] == ge[j]) n         sum += f[j]; n       else { n         if (f[j] > zd) { n           zd2 = zd; n           zd = f[j]; n         } else if (f[j] > zd2) n           zd2 = f[j]; n       } n     } n   } n   for (i = 0; i < g[p].size(); ++i) { n     j = g[p][i]; n     if (j != father[p]) { n       if (f[j] == ge[j]) n         fup[j] = 1 + sum - f[j] + zd; n       else if (f[j] == zd) n         fup[j] = 1 + sum + zd2; n       else n         fup[j] = 1 + sum + zd; n     } n   } n   for (i = 0; i < g[p].size(); ++i) { n     j = g[p][i]; n     if (j != father[p]) calc_fup(j); n   } n } n bool ky(int now) { n   int p, i, j, sum, zd; n   MIN = now; n   calc_f(1); n   fup[1] = 0; n   calc_fup(1); n   for (p = 1; p <= n; ++p) { n     sum = zd = 0; n     if (fup[p] == upge[p]) n       sum = fup[p]; n     else n       zd = fup[p]; n     if (a[p] < MIN) continue; n     for (i = 0; i < g[p].size(); ++i) { n       j = g[p][i]; n       if (j != father[p]) { n         if (f[j] == ge[j]) n           sum += f[j]; n         else { n           if (f[j] > zd) zd = f[j]; n         } n       } n     } n     if (1 + sum + zd >= K) return true; n   } n   return false; n } n void work() { n   int i, start, stop, mid; n   father[1] = 0; n   calc_ge(1, 0); n   for (i = 1; i <= n; ++i) upge[i] = n - ge[i]; n   start = 1; n   stop = 1000010; n   while (start <= stop) { n     mid = (start + stop) / 2; n     if (ky(mid)) { n       result = mid; n       start = mid + 1; n     } else n       stop = mid - 1; n   } n } n void output() { printf( %d n , result); } n int main() { n   init(); n   work(); n   output(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long double PI = 3.141592653589793238462643383; n struct Rcompare { n   bool operator()(pair<int, int> lhs, pair<int, int> rhs) { n     return lhs.first > rhs.first; n   } n }; n struct compare { n   bool operator()(pair<int, int> lhs, pair<int, int> rhs) { n     return lhs.first < rhs.first; n   } n }; n long long int gcd(long long int a, long long int b) { n   if (a == 0) return b; n   return gcd(b % a, a); n } n long long int lcm(long long int a, long long int b) { n   return a * (b / gcd(a, b)); n } n long long int Abs(long long int a) { n   if (a > 0) return a; n   return -a; n } n long long int Ceil(long long int a, long long int b) { n   if (a % b == 0) n     return a / b; n   else n     return a / b + 1; n } n double Abs(double a) { n   if (a > 0) return a; n   return -a; n } n long long int pow(long long int x, long long int y) { n   if (y == 0) return 1; n   long long int tmp = pow(x, y / 2); n   if (y % 2 == 1) return ((tmp * tmp) * x); n   return (tmp * tmp); n } n const int MAX = 100009; n const int MOD = 1e9 + 7; n const int inf = 1e9 + 10; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int a, b, c, m; n   cin >> c >> b >> a >> m; n   if (2 * a < m || a > 2 * m) { n     cout << -1 <<   n ; n     return 0; n   } else if (2 * m > 2 * b || 2 * m > 2 * c) { n     cout << -1 <<   n ; n     return 0; n   } else { n     for (int i = max(2 * m + 1, c); i <= (2 * c); ++i) n       for (int j = max(2 * m + 1, b); j <= (2 * b); ++j) n         for (int k = max(a, m); k <= (min(2 * a, 2 * m)); ++k) { n           if (k < j && j < i) { n             cout << i <<   n  << j <<   n  << k <<   n ; n             return 0; n           } n         } n   } n   cout << -1 <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int arr[100005]; n int main() { n   long long int n, a, b, x, val, pos; n   cin >> n >> a >> b; n   for (long long int i = 0; i < n; i++) { n     cin >> x; n     long long int val = x * a; n     long long int t = val / b; n     long long int tb = t * b; n     long long int bad = ceil((long double)tb / a); n     cout << x - bad << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 99999997; n const int mmax = 200010; n const double eps = 1e-8; n const double pi = acos(-1.0); n const int inf = 0x3fffffff; n int a[200010], b[200010]; n int main() { n   int n, m, sa, sb; n   int cnt1, cnt2, df; n   while (cin >> n) { n     for (int i = 0; i < n; i++) scanf( %d , &a[i]); n     cin >> m; n     for (int i = 0; i < m; i++) scanf( %d , &b[i]); n     sort(a, a + n); n     sort(b, b + m); n     sa = 3 * n, sb = 3 * m; n     df = sa - sb; n     cnt1 = 0, cnt2 = 0; n     while (cnt1 < n && cnt2 < m) { n       if (a[cnt1] == b[cnt2]) n         cnt1++, cnt2++; n       else if (a[cnt1] < b[cnt2]) { n         df -= 1; n         cnt1++; n       } else { n         df += 1; n         if (df > sa - sb) { n           sa = 2 * cnt1 + 3 * (n - cnt1); n           sb = 2 * (cnt2 + 1) + 3 * (m - cnt2 - 1); n         } n         cnt2++; n       } n     } n     if (2 * n - 2 * m > sa - sb) sa = 2 * n, sb = 2 * m; n     cout << sa <<  :  << sb << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   int n, m; n   cin >> n >> m; n   vector<int> a(n); n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n   } n   int lo = 0, hi = m; n   while (lo < hi) { n     int mid = (lo + hi) / 2; n     int last = 0; n     bool b = true; n     for (int i = 0; i < n; ++i) { n       int delta = (last - a[i] + m) % m; n       if (delta <= mid) continue; n       if (a[i] < last) { n         b = false; n         break; n       } n       last = a[i]; n     } n     if (b) { n       hi = mid; n     } else { n       lo = mid + 1; n     } n   } n   cout << lo <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int sieve[1000003 + 5]; n long long int num_primes[1000005] = {0}; n void calc_sieve() { n   for (long long int i = 0; i <= 1000003; i++) sieve[i] = 0; n   sieve[0] = sieve[1] = 1; n   for (long long int i = 2; i <= 1000003; i++) { n     num_primes[i] = num_primes[i - 1]; n     if (sieve[i] == 0) { n       num_primes[i]++; n       for (long long int j = i + i; j <= 1000003; j = j + i) sieve[j]++; n     } n   } n } n long long int ans = -1; n bool check(long long int a, long long int b, long long int l, long long int k) { n   for (long long int i = a; i <= b - l + 1; i++) { n     if (num_primes[i + l - 1] - num_primes[i - 1] < k) return false; n   } n   ans = l; n   return true; n } n void binary_search_primes(long long int a, long long int b, long long int k, n                           long long int start, long long int end) { n   if (end - start <= 1) { n     if (!check(a, b, start, k)) check(a, b, end, k); n     return; n   } n   long long int mid = (start + end) / 2; n   if (check(a, b, mid, k)) { n     binary_search_primes(a, b, k, start, mid - 1); n   } else { n     binary_search_primes(a, b, k, mid + 1, end); n   } n } n int main() { n   calc_sieve(); n   long long int a, b, k; n   cin >> a >> b >> k; n   binary_search_primes(a, b, k, 1, b - a + 1); n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     int n1 = n; n     int k1; n     int k[n + 1]; n     int pri[n + 1]; n     for (int i = 1; i <= n; i++) { n       k[i] = 0; n       pri[i] = 0; n     } n     int f = 0; n     std::vector<int> nhi; n     for (int j = 1; j <= n; j++) { n       f = 0; n       cin >> k1; n       int pr[k1 + 1]; n       for (int i = 1; i <= k1; i++) { n         cin >> pr[i]; n       } n       for (int i = 1; i <= k1; i++) { n         if (k[pr[i]] == 0) { n           k[pr[i]] = 1; n           pri[j] = 1; n           break; n         } n       } n       if (pri[j] == 0) { n         nhi.push_back(j); n       } n     } n     if (nhi.empty()) n       cout <<  OPTIMAL  n            <<   n ; n     else { n       int g; n       for (int i = 1; i <= n; i++) { n         if (k[i] == 0) { n           g = i; n           break; n         } n       } n       cout <<  IMPROVE  n            <<   n ; n       cout << nhi[0] <<     << g <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char buf[25]; n const int maxn = 1000010; n int a[maxn * 2], b[maxn * 2]; n int n, m, x, y, t, mx; n int read() { n   int x = 0, f = 0; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = 1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = (x << 1) + (x << 3) + (ch ^ 48); n     ch = getchar(); n   } n   return f ? -x : x; n } n void write(int x) { n   if (!x) { n     putchar( 0 ); n     return; n   } n   if (x < 0) { n     putchar( - ); n     x = -x; n   } n   int cnt = 0; n   while (x) { n     buf[++cnt] =  0  + x % 10; n     x /= 10; n   } n   for (int i = cnt; i >= 1; --i) putchar(buf[i]); n } n int main() { n   t = read(); n   for (int i = 1; i <= t; ++i) { n     int v = read(); n     mx = max(mx, v); n     ++a[v]; n   } n   int x = 1; n   for (int i = 1; i <= mx; ++i) n     if (a[i] != i * 4) { n       x = i; n       break; n     } n   for (int i = x * 2 - 1; i <= t; ++i) n     if (t % i == 0) { n       n = i; n       m = t / i; n       y = n + m - x - mx; n       if (y <= 0 || y * 2 - 1 > m) continue; n       bool flag = true; n       int t1 = 0, t2 = 0; n       for (int j = 1; j <= n; ++j) { n         bool f = true; n         for (int k = 1; k <= m; ++k) { n           t1 = j; n           t2 = k; n           int tmp = abs(j - x) + abs(k - y); n           ++b[tmp]; n           if (b[tmp] > a[tmp]) { n             flag = false; n             f = false; n             break; n           } n         } n         if (!f) break; n       } n       for (int j = 1; j <= n; ++j) { n         bool f = true; n         for (int k = 1; k <= m; ++k) { n           int tmp = abs(j - x) + abs(k - y); n           --b[tmp]; n           if (j == t1 && k == t2) { n             f = false; n             break; n           } n         } n         if (!f) break; n       } n       if (flag) { n         write(n); n         putchar(   ); n         write(m); n         putchar(  n ); n         write(x); n         putchar(   ); n         write(y); n         putchar(  n ); n         return 0; n       } n     } n   write(-1); n   putchar(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   string s; n   cin >> s; n   int a[n], i, j, k, cnt = 1e9, tmp = 0; n   for (i = 0; i < n; i++) cin >> a[i]; n   for (i = 0; i < s.size() - 1; i++) { n     if (s[i] ==  R  && s[i + 1] ==  L ) { n       tmp = (a[i + 1] - a[i]) / 2; n       if (tmp < cnt) cnt = tmp; n     } n   } n   if (cnt == 1e9) n     cout << -1 << endl; n   else n     cout << cnt << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, q; n const int max_w = 100; n vector<pair<int, int>> cnt_sort(vector<pair<int, int>>& a) { n   vector<int> cnt(n * max_w + 5); n   for (int i = 0; i < a.size(); ++i) { n     ++cnt[a[i].first + 1]; n   } n   for (int i = 1; i < cnt.size(); ++i) { n     cnt[i] += cnt[i - 1]; n   } n   vector<pair<int, int>> res(a.size()); n   for (int i = 0; i < a.size(); ++i) { n     res[cnt[a[i].first]++] = a[i]; n   } n   return res; n }; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> m >> q; n   vector<int> w(n); n   for (int i = 0; i < n; ++i) { n     cin >> w[i]; n   } n   reverse(w.begin(), w.end()); n   vector<int> cnt(1 << n, 0); n   string s; n   for (int i = 0; i < m; ++i) { n     cin >> s; n     int res = 0; n     for (int j = 0; j < n; ++j) { n       res <<= 1; n       res |= (s[j] ==  1 ); n     } n     ++cnt[res]; n   } n   vector<vector<pair<int, int>>> ans(1 << n, vector<pair<int, int>>()); n   vector<vector<int>> sum(1 << n, vector<int>(1 << n)); n   for (int i = 0; i < ans.size(); ++i) { n     for (int j = 0; j < cnt.size(); ++j) { n       if (cnt[j] == 0) continue; n       int kok = i ^ j; n       int res = 0; n       for (int k = 0; k < n; ++k) { n         if (!((kok >> k) & 1)) { n           res += w[k]; n         } n       } n       ans[i].push_back({res, cnt[j]}); n     } n     ans[i] = cnt_sort(ans[i]); n     sum[i][0] = ans[i][0].second; n     for (int j = 1; j < ans[i].size(); ++j) { n       sum[i][j] = sum[i][j - 1] + ans[i][j].second; n     } n   } n   int k; n   while (q--) { n     cin >> s >> k; n     int res = 0; n     for (int j = 0; j < n; ++j) { n       res <<= 1; n       res |= (s[j] ==  1 ); n     } n     int j = n         upper_bound(ans[res].begin(), ans[res].end(), make_pair(k, 100000000)) - n         ans[res].begin(); n     --j; n     if (j < 0) { n       cout << 0; n     } else { n       cout << sum[res][j]; n     } n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int p[1000006], n, k; n int main() { n   ios::sync_with_stdio(false); n   cin >> n >> k; n   iota(p + 1, p + n + 1, 1); n   for (int i = 2; i <= n; i++) { n     if (p[i] == i) { n       p[i]--; n       for (int j = i + i; j <= n; j += i) p[j] = p[j] / i * (i - 1); n     } n   } n   sort(p + 1, p + n + 1); n   long long ans = 0; n   for (int i = 1; i <= k + 2; i++) { n     ans += p[i]; n   } n   if (k == 1) ans--; n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Query { n   int id, left, right, time, blockLeft, blockRight; n   Query() n       : id(-1), left(-1), right(-1), time(-1), blockLeft(-1), blockRight(-1) {} n   Query(int id, int left, int right, int time, int blockLeft, int blockRight) n       : id(id), n         left(left), n         right(right), n         time(time), n         blockLeft(blockLeft), n         blockRight(blockRight) {} n   bool operator<(const Query &otherQuery) const { n     if (blockLeft != otherQuery.blockLeft) { n       return blockLeft < otherQuery.blockLeft; n     } n     if (blockRight != otherQuery.blockRight) { n       return blockRight < otherQuery.blockRight; n     } n     return time < otherQuery.time; n   } n }; n struct Operation { n   int pos, oldValue, newValue; n   Operation() : pos(-1), oldValue(-1), newValue(-1) {} n   Operation(int pos, int oldValue, int newValue) n       : pos(pos), oldValue(oldValue), newValue(newValue) {} n }; n void del(int value, int *cnts, int *saveValues) { n   if (cnts[value] > 0) saveValues[cnts[value]]--; n   cnts[value]--; n   if (cnts[value] > 0) saveValues[cnts[value]]++; n } n void add(int value, int *cnts, int *saveValues) { n   if (cnts[value] > 0) saveValues[cnts[value]]--; n   cnts[value]++; n   if (cnts[value] > 0) saveValues[cnts[value]]++; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n, q; n   cin >> n >> q; n   int sqrtValue = pow(n, 2.0 / 3.0), *values = new int[n + 1], n       *tmpValues = new int[n + 1]; n   if (sqrtValue == 0) sqrtValue = 1; n   vector<int> disValues; n   for (int i = 1; i <= n; i++) { n     cin >> values[i]; n     tmpValues[i] = values[i]; n     disValues.push_back(values[i]); n   } n   Query *querys = new Query[q + 1]; n   Operation *opts = new Operation[q + 1]; n   int cntQuerys = 0, cntOpts = 0; n   for (int i = 1; i <= q; i++) { n     int opt; n     cin >> opt; n     if (opt == 1) { n       int left, right; n       cin >> left >> right; n       ++cntQuerys; n       querys[cntQuerys] = Query(cntQuerys, left, right, cntOpts, n                                 left / sqrtValue, right / sqrtValue); n     } else { n       int pos, value; n       cin >> pos >> value; n       ++cntOpts; n       opts[cntOpts] = Operation(pos, tmpValues[pos], value); n       tmpValues[pos] = value; n       disValues.push_back(value); n     } n   } n   sort(disValues.begin(), disValues.end()); n   disValues.resize(unique(disValues.begin(), disValues.end()) - n                    disValues.begin()); n   for (int i = 1; i <= cntOpts; i++) { n     Operation &tmpOpt = opts[i]; n     tmpOpt.oldValue = n         lower_bound(disValues.begin(), disValues.end(), tmpOpt.oldValue) - n         disValues.begin(); n     tmpOpt.newValue = n         lower_bound(disValues.begin(), disValues.end(), tmpOpt.newValue) - n         disValues.begin(); n   } n   for (int i = 1; i <= n; i++) { n     values[i] = lower_bound(disValues.begin(), disValues.end(), values[i]) - n                 disValues.begin(); n   } n   int valueSize = static_cast<int>(disValues.size()), n       *cnts = new int[valueSize + 3], *saveValues = new int[n + 2]; n   memset(cnts, 0, sizeof(int) * (valueSize + 3)); n   memset(saveValues, 0, sizeof(int) * (n + 2)); n   int *results = new int[cntQuerys + 1]; n   sort(querys + 1, querys + cntQuerys + 1); n   int l = 1, r = 0, time = 0; n   for (int i = 1; i <= cntQuerys; i++) { n     Query &tmpQuery = querys[i]; n     while (l > tmpQuery.left) { n       l--; n       add(values[l], cnts, saveValues); n     } n     while (l < tmpQuery.left) { n       del(values[l], cnts, saveValues); n       l++; n     } n     while (r > tmpQuery.right) { n       del(values[r], cnts, saveValues); n       r--; n     } n     while (r < tmpQuery.right) { n       r++; n       add(values[r], cnts, saveValues); n     } n     while (time < tmpQuery.time) { n       time++; n       Operation &tmpOpt = opts[time]; n       if (tmpOpt.pos >= l && tmpOpt.pos <= r) { n         del(tmpOpt.oldValue, cnts, saveValues); n         add(tmpOpt.newValue, cnts, saveValues); n       } n       values[tmpOpt.pos] = tmpOpt.newValue; n     } n     while (time > tmpQuery.time) { n       Operation &tmpOpt = opts[time]; n       if (tmpOpt.pos >= l && tmpOpt.pos <= r) { n         del(tmpOpt.newValue, cnts, saveValues); n         add(tmpOpt.oldValue, cnts, saveValues); n       } n       values[tmpOpt.pos] = tmpOpt.oldValue; n       time--; n     } n     for (int i = 1;; i++) { n       if (i > n) { n         results[tmpQuery.id] = 0; n       } n       if (saveValues[i] == 0) { n         results[tmpQuery.id] = i; n         break; n       } n     } n   } n   for (int i = 1; i <= cntQuerys; i++) { n     cout << results[i] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n const int N = 200005; n const int M = 500005; n using namespace std; n pair<int, int> a[N]; n int p[20][M]; n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   int i, j; n   for (i = 0; i < n; i++) { n     scanf( %d%d , &a[i].first, &a[i].second); n   } n   sort(a, a + n); n   memset(p, -1, sizeof(p)); n   int maxe = -1; n   j = 0; n   for (i = 0; i < M; i++) { n     while (j < n && a[j].first <= i) { n       if (a[j].second > maxe) maxe = a[j].second; n       j++; n     } n     if (maxe >= i) n       p[0][i] = maxe; n     else n       p[0][i] = -1; n   } n   for (j = 0; j < 19; j++) n     for (i = 0; i < M; i++) n       if (p[j][i] == -1) n         p[j + 1][i] = -1; n       else n         p[j + 1][i] = p[j][p[j][i]]; n   int u, v; n   for (i = 0; i < m; i++) { n     scanf( %d%d , &u, &v); n     int ans = 0; n     for (j = 19; j >= 0; j--) n       if (p[j][u] < v) { n         u = p[j][u]; n         ans += 1 << j; n       } n     u = p[0][u]; n     ans++; n     if (u >= v) n       printf( %d n , ans); n     else n       printf( -1 n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, t; n int main() { n   cin >> t; n   while (t--) { n     int s = 0, a; n     cin >> n >> m; n     for (int i = 1; i <= n; i++) { n       cin >> a; n       s += 2 * a; n     } n     if (m < n || n < 3) { n       cout << -1 << endl; n       continue; n     } n     cout << s << endl; n     for (int i = 1; i < n; i++) cout << i <<     << i + 1 << endl; n     cout << n <<     << 1 << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n void solve(); n int main() { n   ios::sync_with_stdio(0); n   solve(); n   return 0; n } n list<pair<deque<int>, int> > basket; n int N, bN; n int p[100001]; n void update(pair<deque<int>, int>& it) { n   it.second = it.first[0]; n   for (int i = 0; i < (int)((it.first).size()); i++) { n     it.second = max(it.second, it.first[i]); n   } n } n void solve() { n   cin >> N; n   bN = 250; n   basket.push_back(pair<deque<int>, int>(deque<int>(), 0)); n   for (int i = 0; i < N; i++) { n     int a, c; n     cin >> a >> c; n     p[a] = i + 1; n     int cc = c; n     list<pair<deque<int>, int> >::iterator it = basket.begin(); n     for (; it != basket.end(); it++) { n       if (it->second > a || (int)it->first.size() > cc) { n         break; n       } n       cc -= it->first.size(); n     } n     if (it == basket.end()) { n       it--; n       cc += it->first.size(); n     } n     deque<int>& b = it->first; n     b.push_front(a); n     for (int j = 0; j < (int)((b).size()) - 1 && cc > 0; j++, cc--) { n       if (b[j] < b[j + 1]) break; n       swap(b[j], b[j + 1]); n     } n     if ((int)((b).size()) >= 2 * bN) { n       pair<deque<int>, int> item(deque<int>(), 0); n       while ((int)((b).size()) > bN) { n         item.first.push_back(b.front()); n         b.pop_front(); n       } n       update(*it); n       update(item); n       basket.insert(it, item); n     } else { n       update(*it); n     } n   } n   vector<int> ans; n   for (list<pair<deque<int>, int> >::iterator it = basket.begin(); n        it != basket.end(); it++) { n     deque<int>& b = it->first; n     for (int i = 0; i < (int)((b).size()); i++) { n       ans.push_back(p[b[i]]); n     } n   } n   reverse(ans.begin(), ans.end()); n   for (int i = 0; i < (int)((ans).size()); i++) cout << ans[i] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 5005; n const int maxm = maxn * 2; n const int maxe = 200005; n const int mod = 1e9 + 7; n int n, a, b; n string str; n int cc[26][maxn][26]; n int exist[26]; n int main() { n   cin >> str; n   n = str.length(); n   for (int i = 0; i < n; i++) { n     int idx = str[i] -  a ; n     exist[idx]++; n     for (int j = 1; j < n; j++) { n       int pos = (i + j) % n; n       int idx2 = str[pos] -  a ; n       cc[idx][j][idx2]++; n     } n   } n   double ans = 0; n   for (int i = 0; i < 26; i++) { n     double cccc = 0; n     for (int j = 1; j < n; j++) { n       double tp = 0; n       for (int ii = 0; ii <= n; ii++) { n         if (str[ii] -  a  != i) continue; n         int pos = (ii + j) % n; n         int idx2 = str[pos] -  a ; n         if (cc[i][j][idx2] == 1) tp++; n       } n       cccc = max(cccc, tp); n     } n     ans += cccc; n   } n   printf( %.15lf n , ans / n); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, m, k; n   scanf( %lld%lld%lld , &n, &m, &k); n   if (n % 2 == 0) { n     printf( Marsel n ); n     return 0; n   } n   if (n % 2 == 1) { n     if (k == 1 && m > 1) { n       printf( Timur n ); n       return 0; n     } n     for (long long a = 2; a <= sqrt(m); a++) n       if (m % a == 0) n         if ((m / a) >= k) { n           printf( Timur n ); n           return 0; n         } n   } n   printf( Marsel n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma comment(linker,  /STACK:33554432 ) n int N, Q, cnt[3][100005]; n char A[100005]; n int main() { n   int i, j, k; n   scanf( %s , A + 1); n   N = strlen(A + 1); n   for (i = 1; i <= N; i++) { n     for (j = 0; j < 3; j++) cnt[j][i] += cnt[j][i - 1]; n     cnt[A[i] -  x ][i]++; n   } n   for (scanf( %d , &Q); Q--;) { n     int c[3] = n         { n             0, n         }, n         s, e; n     scanf( %d%d , &s, &e); n     for (i = 0; i < 3; i++) c[i] = cnt[i][e] - cnt[i][s - 1]; n     int mx = 0, mn = 1e9; n     for (i = 0; i < 3; i++) mx = max(mx, c[i]), mn = min(mn, c[i]); n     if (e - s + 1 < 3 || mx - mn < 2) n       puts( YES ); n     else n       puts( NO ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, i, j, k, it, t; n long long r[101], rr[101], c[101], cc[101]; n long long a[101][101]; n long long R, C, x; n vector<long long> ans, ans2; n int main() { n   cin >> n >> m; n   int k = 1; n   x = 3; n   while (n != 0) { n     if (n % 2 == 1) { n       k = k * x % m; n     } n     x = x * x % m; n     n = n / 2; n   } n   cout << (k - 1 + m) % m; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double pi = 3.1415926536; n int main() { n   int n; n   cin >> n; n   int r[n]; n   for (int i = 0; i < n; i++) cin >> r[i]; n   double area = 0; n   sort(r, r + n); n   for (int i = n - 1; i >= 0; i -= 2) { n     if (i > 0) n       area += (r[i] * r[i] - r[i - 1] * r[i - 1]); n     else n       area += r[0] * r[0]; n   } n   area *= pi; n   printf( %0.10lf , area); n   return 0; n } n


#include <bits/stdc++.h> n const bool DEBUG = false; n using namespace std; n const long long maxn = 1e5 + 2; n const long long INF = 4e18; n const long long M = 1e9 + 7; n const int lg = 21; n const int z = 256 * 256; n int n, m, a[maxn], t[maxn], dp[maxn]; n string s; n vector<int> g[2], v[2]; n bool f(vector<int>(&v), int l, int r) { n   if (lower_bound(v.begin(), v.end(), l) == v.end()) return 1; n   int l1 = lower_bound(v.begin(), v.end(), l) - v.begin(); n   if (lower_bound(v.begin(), v.end(), r + 1) == v.end()) return 0; n   int l2 = lower_bound(v.begin(), v.end(), r + 1) - v.begin(); n   if (l1 == l2) return 1; n   return 0; n } n bool check(int l, int r) { n   if (l % 2 == 0) n     if (f(v[1], l, r) and f(g[0], l, r)) return 1; n   if (l % 2 == 1) n     if (f(v[0], l, r) and f(g[1], l, r)) return 1; n   return 0; n } n int par(int l, int r) { n   if (l == 0) return a[r]; n   return a[r] - a[l - 1]; n } n int main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   cin >> n >> s >> m; n   a[0] = bool(s[0] ==  ? ); n   for (int i = 1; i < n; i++) a[i] = a[i - 1] + bool(s[i] ==  ? ); n   for (int i = 0; i < n; i++) n     if (s[i] ==  a ) n       v[(i % 2)].push_back(i); n     else if (s[i] ==  b ) n       g[(i % 2)].push_back(i); n   t[n - m] = check(n - m, n - 1); n   for (int i = n - m - 1; i >= 0; i--) { n     if (check(i, i + m - 1)) n       t[i] = 1 + t[i + m]; n     else n       t[i] = t[i + 1]; n   } n   int maxx = t[0]; n   if (t[n - m] == 1) dp[n - m] = par(n - m, n - 1); n   for (int i = n - m - 1; i >= 0; i--) { n     if (check(i, i + m - 1)) { n       dp[i] = par(i, i + m - 1) + dp[i + m]; n       if (t[i + 1] == t[i]) dp[i] = min(dp[i], dp[i + 1]); n     } else n       dp[i] = dp[i + 1]; n   } n   cout << dp[0] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long GCD(long long a, long long b) { return b == 0 ? a : GCD(b, a % b); } n const long long inf = 0x7f7f7f7f; n const long long mod = 1e9 + 7; n double pi = acos(-1.0); n long long a[120]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long n, m, q; n   cin >> n >> m >> q; n   long long t = GCD(n, m); n   n /= t; n   m /= t; n   long long x1, y1, x2, y2; n   for (int i = 1; i <= q; i++) { n     cin >> x1 >> y1 >> x2 >> y2; n     if (t == 1) { n       cout <<  YES  << endl; n       continue; n     } n     long long p1, p2; n     if (x1 == 1) { n       p1 = y1 / n; n       if (y1 % n) p1++; n     } else { n       p1 = y1 / m; n       if (y1 % m) p1++; n     } n     if (x2 == 1) { n       p2 = y2 / n; n       if (y2 % n) p2++; n     } else { n       p2 = y2 / m; n       if (y2 % m) p2++; n     } n     if (p1 == p2) n       cout <<  YES  << endl; n     else n       cout <<  NO  << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = 1e9, maxn = 5e5 + 5, mod = 1e9 + 7; n long long x[maxn], y[maxn], sz[maxn], n, m; n set<long long> S; n unordered_map<long long, long long> cnt, id; n vector<long long> son[maxn]; n long long gc(long long x) { n   long long ret = 0, j = 0; n   while (x <= n) { n     ret += min(x + (1 << j) - 1, n) - x + 1; n     x <<= 1; n     ++j; n     ret %= mod; n   } n   return ret; n } n void dfs(long long x) { n   if (S.count(x)) { n     cnt[x] = 1; n     if ((x << 1) <= n) dfs(x << 1); n     if ((x << 1 | 1) <= n) dfs(x << 1 | 1); n   } else n     cnt[x] = gc(x); n } n long long vis[maxn]; n long long dfs2(long long x) { n   vis[x] = 1; n   long long ret = sz[x]; n   for (auto js : son[x]) n     if (!vis[js]) { n       (ret += dfs2(js)) %= mod; n     } n   vis[x] = 0; n   return ret; n } n signed main() { n   scanf( %lld%lld , &n, &m); n   if (m == 0) return printf( %lld n , 1ll * n * n % mod), 0; n   for (long long i = 1; i <= m; i++) { n     scanf( %lld%lld , &x[i], &y[i]); n     long long tmp = x[i]; n     while (tmp) { n       S.insert(tmp); n       tmp >>= 1; n     } n     tmp = y[i]; n     while (tmp) { n       S.insert(tmp); n       tmp >>= 1; n     } n   } n   S.insert(1); n   dfs(1); n   long long idc = 0; n   for (auto i : cnt) id[i.first] = ++idc; n   for (auto i : cnt) { n     long long u = id[i.first]; n     sz[u] = i.second; n     for (long long j = 0; j <= 1; j++) { n       long long v = i.first << 1 | j; n       if (!cnt.count(v)) continue; n       v = id[v]; n       son[u].push_back(v); n       son[v].push_back(u); n     } n   } n   for (long long i = 1; i <= m; i++) n     son[id[x[i]]].push_back(id[y[i]]), son[id[y[i]]].push_back(id[x[i]]); n   long long ans = 0; n   for (long long i = 1; i <= idc; i++) n     ans = (ans + 1ll * sz[i] * sz[i] % mod) % mod; n   for (long long i = 1; i <= idc; i++) { n     vis[i] = 1; n     long long cura = 0; n     for (auto js : son[i]) { n       cura = (cura + dfs2(js)) % mod; n     } n     ans = (ans + 1ll * cura * sz[i] % mod) % mod; n     vis[i] = 0; n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n, a, b, c = 0; n   scanf( %d , &n); n   while (n--) { n     scanf( %d , &a); n     c += a != b; n     b = a; n   } n   printf( %d , c); n } n


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


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100000 + 100; n pair<int, int> khar[maxn]; n bool mk[10 * maxn]; n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     int a, b; n     cin >> a >> b; n     mk[a] = true; n     khar[i] = pair<int, int>(a, b); n   } n   sort(khar, khar + n); n   int k = khar[n - 1].first; n   int x[k + 1]; n   fill(x, x + k + 1, 0); n   if (mk[0]) x[0] = 1; n   for (int i = 1; i <= k; i++) { n     if (mk[i]) n       x[i] += x[i - 1] + 1; n     else n       x[i] += x[i - 1]; n   } n   int h[n]; n   fill(h, h + n, 0); n   for (int i = 1; i < n; i++) { n     if (khar[i].first - khar[i].second <= 0) { n       h[i] = i; n       continue; n     } n     h[i] = x[khar[i].first - 1] - x[khar[i].first - khar[i].second - 1]; n   } n   int g[n]; n   fill(g, g + n, 0); n   for (int i = 1; i < n; i++) { n     if (khar[i].first - khar[i].second <= 0) { n       g[i] = i; n       continue; n     } n     if (h[i] == 0) n       g[i] = g[i - 1]; n     else n       g[i] = h[i] + g[i - h[i] - 1]; n   } n   for (int i = 0; i < n; i++) g[i] += n - i - 1; n   sort(g, g + n); n   cout << g[0] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dirx[] = {-1, -1, -1, 0, 0, 1, 1, 1}; n int diry[] = {-1, 0, 1, -1, 1, -1, 0, 1}; n int dx[] = {-1, 1, 0, 0}; n int dy[] = {0, 0, -1, 1}; n class SorT { n  public: n   bool operator()(pair<int, int>& p1, pair<int, int>& p2) { n     return p1.second < p2.second; n   } n }; n int ans[1000010]; n int main() { n   string s; n   cin >> s; n   int left = 0, right = s.size() - 1; n   for (int i = 0; i < s.size(); i++) { n     if (s[i] ==  l ) n       ans[right--] = i + 1; n     else n       ans[left++] = i + 1; n   } n   for (int i = 0; i < s.size(); i++) { n     printf( %d n , ans[i]); n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long int mod = 1e9 + 7; n void solve() { n   long long int n; n   cin >> n; n   long long int a[n]; n   long long int hi = 0; n   for (long long int i = 0; i < n; ++i) { n     cin >> a[i]; n     hi += a[i]; n   } n   sort(a, a + n); n   long long int ans = hi - n; n   if (n > 50) { n     cout << ans; n     return; n   } n   for (long long int i = 1; i < 1e9 + 1; ++i) { n     long long int c = i; n     long long int tmp = 0; n     long long int dig = 0; n     while (c > 0) { n       c = c / 10; n       dig++; n     } n     long long int no = 1; n     c = i; n     bool ex = true; n     for (long long int j = 0; j < n; ++j) { n       tmp += abs(a[j] - no); n       if ((no * c) > 1e14 || (no * c) < 0) { n         ex = false; n         break; n       } n       long long int digi = 0, yo = no; n       while (yo > 0) { n         yo = yo / 10; n         digi++; n       } n       if (digi + dig > 16) { n         ex = false; n         break; n       } n       no = no * c; n     } n     if (ex) n       ans = min(ans, tmp); n     else { n       break; n     } n   } n   cout << ans; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int t = 1; n   while (t--) { n     solve(); n   } n   cerr <<  Time elapsed:   << 1000 * clock() / CLOCKS_PER_SEC <<  ms n ; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, x, y; n long long bs1(long long l, long long r, long long num) { n   long long mid = (l + r) / 2; n   if (r - l <= 0) { n     return -1; n   } n   long long time = mid * x; n   long long highnow = mid + ((mid * x + x - 1) / y); n   long long lownow = mid + ((time) / y); n   if (highnow >= num && lownow < num) { n     return mid; n   } n   if (highnow < num) { n     return bs1(mid + 1, r, num); n   } n   return bs1(l, mid, num); n } n long long bs2(long long l, long long r, long long num) { n   long long mid = (l + r) / 2; n   if (r - l <= 0) { n     return -1; n   } n   long long time = mid * y; n   long long highnow = mid + ((mid * y + y - 1) / x); n   long long lownow = mid + ((time) / x); n   if (highnow >= num && lownow < num) { n     return mid; n   } n   if (highnow < num) { n     return bs2(mid + 1, r, num); n   } n   return bs2(l, mid, num); n } n int main() { n   cin >> n >> x >> y; n   for (int i = 0; i < n; i++) { n     long long num; n     cin >> num; n     long long ansy = bs1(0, num + 1, num); n     long long ansx = bs2(0, num + 1, num); n     if (ansy == -1 && ansx == -1) { n       cout <<  Both  << endl; n       continue; n     } n     if (ansx == -1) { n       cout <<  Vanya  << endl; n       continue; n     } n     if (ansy == -1) { n       cout <<  Vova  << endl; n       continue; n     } n     cout <<  Both  << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ii = pair<int, int>; n const int mod = 1e9 + 7; n int v[200005]; n int n, k; n bool guess(int x) { n   ll spent = x - v[n / 2]; n   for (int i = n / 2 + 1; i < n; i++) { n     spent += max(0, x - v[i]); n     if (spent > (ll)k) return false; n   } n   return spent <= (ll)k; n } n int solve() { n   int x = -1; n   for (int b = v[n - 1] + k; b >= 1; b /= 2) n     while (guess(x + b)) x += b; n   return x; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> k; n   for (int i = 0; i < n; i++) cin >> v[i]; n   sort(v, v + n); n   cout << solve() <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 500010; n const int MOD = 1000000007; n int n, m, q; n char s[14]; n int f[(1 << 12) + 10][(1 << 12) + 10], w[13], freq[(1 << 12) + 10]; n int v[(1 << 12) + 10][100 * 14]; n int main() { n   cin >> n >> m >> q; n   for (int i = 0; i < n; ++i) { n     scanf( %d , &w[i]); n   } n   for (int i = 0; i < m; ++i) { n     scanf( %s , s); n     int val = 0; n     for (int j = 0; j < n; ++j) { n       val += (s[j] -  0 ) * (1 << j); n     } n     freq[val]++; n   } n   for (int msk = 0; msk < (1 << n); ++msk) { n     for (int go = 0; go < (1 << n); ++go) { n       for (int bit = 0; bit < n; ++bit) { n         f[msk][go] += (((go & (1 << bit)) == (msk & (1 << bit))) * w[bit]); n       } n     } n   } n   for (int msk = 0; msk < (1 << n); ++msk) { n     for (int go = 0; go < (1 << n); ++go) { n       v[msk][f[msk][go]] += freq[go]; n     } n     for (int j = 1; j <= 1300; ++j) { n       v[msk][j] += v[msk][j - 1]; n     } n   } n   for (int k, i = 0; i < q; ++i) { n     scanf( %s%d , s, &k); n     int cur = 0; n     for (int j = 0; j < n; ++j) { n       cur += (1 << j) * (s[j] -  0 ); n     } n     printf( %d n , v[cur][k]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100001; n vector<pair<long long, int>> g[maxn]; n vector<pair<long long, int>> _g[maxn]; n int n, m, s, t; n int u[maxn], v[maxn]; n long long c[maxn]; n void enter() { n   cin >> n >> m >> s >> t; n   for (int i = (0), _b = (m); i < _b; ++i) { n     cin >> u[i] >> v[i] >> c[i]; n     g[u[i]].push_back(pair<long long, int>(c[i], v[i])); n     _g[v[i]].push_back(pair<long long, int>(c[i], u[i])); n   } n } n const long long mod = 1000000321; n long long f1[maxn]; n long long c1[maxn]; n long long c2[maxn]; n long long f2[maxn]; n bool d[maxn]; n const long long oo = 1000000000000123; n void dijkstra1() { n   for (int i = (1), _b = (n + 1); i < _b; ++i) f1[i] = oo; n   f1[s] = 0; n   c1[s] = 1; n   priority_queue<pair<long long, int>, vector<pair<long long, int>>, n                  greater<pair<long long, int>>> n       heap; n   heap.push(pair<long long, int>(0, s)); n   while (!heap.empty()) { n     pair<long long, int> q = heap.top(); n     heap.pop(); n     if (!d[q.second]) { n       d[q.second] = true; n       f1[q.second] = q.first; n       for (auto i : g[q.second]) n         if (!d[i.second]) { n           if (q.first + i.first < f1[i.second]) { n             f1[i.second] = q.first + i.first; n             c1[i.second] = c1[q.second]; n             heap.push(pair<long long, int>(f1[i.second], i.second)); n           } else if (q.first + i.first == f1[i.second]) { n             c1[i.second] += c1[q.second]; n             c1[i.second] = c1[i.second] % mod; n           } n         } n     } n   } n } n void dijkstra2() { n   for (int i = (1), _b = (n + 1); i < _b; ++i) { n     f2[i] = oo; n     d[i] = false; n   } n   f2[t] = 0; n   c2[t] = 1; n   priority_queue<pair<long long, int>, vector<pair<long long, int>>, n                  greater<pair<long long, int>>> n       heap; n   heap.push(pair<long long, int>(0, t)); n   while (!heap.empty()) { n     pair<long long, int> q = heap.top(); n     heap.pop(); n     if (!d[q.second]) { n       d[q.second] = true; n       f2[q.second] = q.first; n       for (auto i : _g[q.second]) n         if (!d[i.second]) { n           if (q.first + i.first < f2[i.second]) { n             f2[i.second] = q.first + i.first; n             c2[i.second] = c2[q.second]; n             heap.push(pair<long long, int>(f2[i.second], i.second)); n           } else if (q.first + i.first == f2[i.second]) { n             c2[i.second] += c2[q.second]; n             c2[i.second] = c2[i.second] % mod; n           } n         } n     } n   } n } n long long cnp(long long l, long long r, int i) { n   if (l == r) n     return l; n   else { n     long long mid = ((l + r) / 2) + 1; n     if (f1[u[i]] + f2[v[i]] + mid < f1[t]) n       return cnp(mid, r, i); n     else n       return cnp(l, mid - 1, i); n   } n } n void solve() { n   for (int i = (0), _b = (m); i < _b; ++i) { n     if (f1[u[i]] + c[i] + f2[v[i]] == f1[t] && n         (c1[u[i]] * c2[v[i]]) % mod == c1[t]) n       cout <<  YES n ; n     else { n       long long ans = cnp(0, c[i], i); n       if (ans == 0) n         cout <<  NO n ; n       else n         cout <<  CAN   << c[i] - ans <<   n ; n     } n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(); n   cout.tie(); n   enter(); n   dijkstra1(); n   dijkstra2(); n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 100, LEN = 21, M = 1 << LEN, inF = N * 30; n int n, len, ps[N], dp0[N], dp1[N], mini[M][LEN]; n string s[N]; n void _min(int &a, int b) { a = min(a, b); } n int same(string a, string b) { n   int A = 0, B = 0, res = 0; n   for (int i = 0; i < len; i++) { n     A |= (a[len - i - 1] -  0 ) << i; n     B = (B << 1) | (b[i] -  0 ); n     if (A == B) res = i + 1; n   } n   return res; n } n int dif(string a, string b) { return len - same(a, b); } n void add(int dex) { n   _min(mini[0][0], dp1[dex] - ps[dex] + len - 0); n   int val = 0; n   for (int i = 1; i <= len; i++) n     val |= (s[dex - 1][len - i] -  0 ) << (i - 1), n         _min(mini[val][i], dp1[dex] - ps[dex] + len - i); n } n void find_dp() { n   dp0[0] = dp1[0] = len; n   for (int i = 1; i < n; i++) { n     dp0[i] = min(dp0[i - 1], dp1[i - 1]) + dif(s[i - 1], s[i]); n     dp1[i] = min((i == 1 ? len << 1 : dp1[i - 1] + dif(s[i - 2], s[i])), n                  ps[i - 1] + len); n     int val = 0; n     _min(dp1[i], mini[val][0] + ps[i - 1]); n     for (int j = 1; j <= len; j++) n       val = (val << 1) | (s[i][j - 1] -  0 ), n       _min(dp1[i], mini[val][j] + ps[i - 1]); n     if (i != n - 1) add(i); n   } n } n int main() { n   for (int i = 0; i < M; i++) fill(mini[i], mini[i] + LEN, inF); n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> s[i]; n     ps[i] = (i ? ps[i - 1] + dif(s[i - 1], s[i]) : len = s[i].length()); n   } n   find_dp(); n   cout << min(dp0[n - 1], dp1[n - 1]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<vector<int64_t>> board, vis, diag1, diag2; n int main() { n   ios::sync_with_stdio(false), cin.tie(nullptr); n   int64_t n; n   cin >> n; n   for (int64_t i = 0; i < n; i++) { n     vector<int64_t> temp; n     vector<int64_t> zero; n     for (int64_t j = 0; j < n; j++) { n       int64_t t; n       cin >> t; n       temp.push_back(t); n       zero.push_back(0); n     } n     board.push_back(temp); n     vis.push_back(zero); n     diag1.push_back(zero); n     diag2.push_back(zero); n   } n   for (int64_t i = 0; i < n; i++) { n     for (int64_t j = 0; j < n; j++) { n       if (vis[i][j]) continue; n       int64_t t1 = i, t2 = j, s = 0; n       while (1) { n         vis[t1][t2] = 1; n         s += board[t1][t2]; n         t1 -= -1; n         t2 -= -1; n         if (t1 >= n || t2 >= n) break; n       } n       t1 = i, t2 = j; n       while (1) { n         diag1[t1][t2] = s; n         t1 -= -1; n         t2 -= -1; n         if (t1 >= n || t2 >= n) break; n       } n     } n   } n   for (int64_t i = 0; i < n; i++) n     for (int64_t j = 0; j < n; j++) vis[i][j] = 0; n   for (int64_t i = 0; i < n; i++) { n     for (int64_t j = 0; j < n; j++) { n       if (vis[i][j]) continue; n       int64_t t1 = i, t2 = j, s = 0; n       while (1) { n         vis[t1][t2] = 1; n         s += board[t1][t2]; n         t1 -= -1; n         t2 += -1; n         if (t1 >= n || t2 < 0) break; n       } n       t1 = i, t2 = j; n       while (1) { n         diag2[t1][t2] = s; n         t1 -= -1; n         t2 += -1; n         if (t1 >= n || t2 < 0) break; n       } n     } n   } n   int64_t m1 = 0, m2 = 0, x1 = 0, x2 = 0, y1 = 0, y2 = 0; n   for (int64_t i = 0; i < n; i++) { n     for (int64_t j = 0; j < n; j++) { n       int64_t tmp = diag1[i][j] + diag2[i][j] - board[i][j]; n       if ((i + j) % 2) { n         if (tmp >= m1) { n           m1 = tmp; n           x1 = i + 1; n           y1 = j + 1; n         } n       } else { n         if (tmp >= m2) { n           m2 = tmp; n           x2 = i + 1; n           y2 = j + 1; n         } n       } n     } n   } n   cout << m1 + m2 << endl; n   cout << x1 <<     << y1 <<     << x2 <<     << y2; n } n


#include <bits/stdc++.h> n using namespace std; n void boost() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n } n void free() { n   freopen( A.in ,  r , stdin); n   freopen( A.out ,  w , stdout); n } n int a[100001]; n int main() { n   boost(); n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) cin >> a[i]; n   long long ans = 1, h = a[0]; n   ans += h; n   for (int i = 1; i < n; i++) { n     if (a[i] >= h) n       ans += (a[i] - h + 1), h = a[i]; n     else if (a[i] < h) n       ans += (h - a[i] + 1), h = a[i]; n     ans++; n   } n   cout << ans, exit(0); n } n


#include <bits/stdc++.h> n using namespace std; n inline void read(long long &num) { n   num = 0; n   long long f = 1; n   char c = getchar(); n   while (!isdigit(c)) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (isdigit(c)) { n     num = num * 10 + c - 48; n     c = getchar(); n   } n   num *= f; n } n const long long maxn = 1e5 + 7; n long long n, a[maxn], al, t; n signed main() { n   read(n); n   al = 0; n   for (long long i = 1; i <= n; ++i) read(a[i]), al ^= a[i]; n   if (!(n & 1) && al) { n     puts( NO ); n     return 0; n   } n   if (!(n & 1)) --n; n   puts( YES ); n   cout << n - 1 << endl; n   for (long long i = 1; i <= n - 2; i += 2) n     cout << i <<     << i + 1 <<     << i + 2 << endl; n   for (long long i = 1; i <= n - 2; i += 2) n     cout << i <<     << i + 1 <<     << n << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int mod = 1000000007; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int l = 1, h, i, n, x, t, q, y = -1, z = 0, a = 0, b = 0, c = 0, k, n                 sum = 0, m, d = 0, r, w, j, ans, s, mini = INT_MAX, n                 maxi = INT_MIN; n   bool cou; n   float kl; n   long long int count = 0; n   t = 1; n   vector<bool> sie(1000001, 0); n   char cg; n   string str, std; n   a = 1; n   while (t--) { n     cin >> n >> m; n     vector<int> vec(n); n     map<int, bool> mp; n     for (i = 0; i < n; ++i) cin >> vec[i]; n     for (i = 0; i < m; ++i) { n       cin >> x; n       mp[x] = 1; n     } n     for (i = 0; i < n; ++i) { n       if (mp[vec[i]]) cout << vec[i] <<    ; n     } n   } n } n


#include <bits/stdc++.h> n #include<stdlib.h> n using namespace std; n #define pb push_back n #define ll long long n #define ff first n #define ss second n #define ppb pop_back n #define mp make_pair n #define vl vector<ll> n #define vi vector<int> n #define endl   n  n #define all(x) x.begin(), x.end() n #define deb(x) cout << #x <<     << x << endl; n #define f(i, n) for(i=0; i<n; i++) n #define F(i, k, n) for(i=k; i<n; i++) n #define max(a,b) (a<b?b:a) n #define min(a,b) (a>b?b:a) n #define INF 1001001001 n #define PI 3.1415926535897932384626 n #define MOD 1000000007 n  n void FAST() {  n  ios::sync_with_stdio(0); n  cin.tie(0); n  cout.tie(0); n } n  n // template<typename... T> n // void read(T&... args) { n //  FAST(); n //  ((cin >> args), ...); n // } n  n // template<typename... T> n // void write(T&&... args) {  n //  FAST(); n //  ((cout << args <<    ), ...); n // } n  n // template<typename T> n // T gcd(T a, T b) { n //     if (b == 0)  n //         return a;  n //     return gcd(b, a % b);   n // }  n  n // template <typename T> n // void printContainer(T &v){ n //     for(auto i : v){ n //         cout<<i<<   ;  n //     } n //     cout <<endl; n // } n  n int main() { n  FAST(); n  //code n     ll t; n     cin>>t; n     ll p; n     f(p,t){ n         string s; n         cin>>s; n         ll i, a1=0, x1=0, x2=0, a2=0,b1=0,b2=0; n         f(i,s.size()){ n             if(s[i]== ( ){ n                 a1++; n                 x1++; n             } n             else if(s[i]== )  && x1>0){ n                 a2++; n                 x1--; n             } n             else if(s[i]== [ ){ n                 b1++; n                 x2++; n             } n             else if(s[i]== ]  && x2>0){ n                 b2++; n                 x2--; n             } n         } n         cout<<min(a1,a2)+min(b1,b2)<<endl; n      n     } n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n const long long N = 1e5 + 5; n map<long long, long long> freq, freq2; n void func1(long long a[], long long n) { n   int i = 0, j = 0; n   while (i < n) { n     if (a[i] == 0) { n       i++; n       continue; n     } n     j = i; n     while (j < n && a[j] != 0) { n       j++; n     } n     for (int p = 1; p <= (j - i); p++) { n       freq[p] += (j - i) - p + 1; n     } n     i = j; n   } n } n void func2(long long a[], long long n) { n   int i = 0, j = 0; n   while (i < n) { n     if (a[i] == 0) { n       i++; n       continue; n     } n     j = i; n     while (j < n && a[j] != 0) { n       j++; n     } n     for (int p = 1; p <= (j - i); p++) { n       freq2[p] += (j - i) - p + 1; n     } n     i = j; n   } n } n int main() { n   long long n, a[N], b[N], r = 0, c = 0, ans = 0, cnt = 0, sum = 0, k, m; n   cin >> n >> m >> k; n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 0; i < m; i++) cin >> b[i]; n   func1(a, n); n   func2(b, m); n   for (int p = 1; p <= sqrt(k); p++) { n     if (k % p == 0) { n       ans += freq[p] * freq2[k / p]; n       if (p != (k / p)) ans += freq[k / p] * freq2[p]; n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct custom_hash { n   static uint64_t splitmix64(uint64_t x) { n     x += 0x9e3779b97f4a7c15; n     x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; n     x = (x ^ (x >> 27)) * 0x94d049bb133111eb; n     return x ^ (x >> 31); n   } n   size_t operator()(uint64_t x) const { n     static const uint64_t FIXED_RANDOM = n         chrono::steady_clock::now().time_since_epoch().count(); n     return splitmix64(x + FIXED_RANDOM); n   } n }; n const long long intmax = 2147483647; n const long long intmin = -2147483648; n const long long mod2 = 998244353; n const long long mod = 1e9 + 7; n const long long N = 3e5; n long long fact[N]; n long long add(long long a, long long b, long long p = mod) { n   long long res = (a % p + b % p) % p; n   return res; n } n long long sub(long long a, long long b, long long p = mod) { n   long long res = (a % p - b % p + p) % p; n   return res; n } n long long mul(long long a, long long b, long long p = mod) { n   long long res = ((a % p) * (b % p)) % p; n   return res; n } n void factorial(long long n, long long p = mod) { n   fact[0] = 1; n   for (long long i = 1; i < n; i++) { n     fact[i] = mul(i, fact[i - 1], p); n   } n } n long long binExp(long long x, long long y, long long p = mod) { n   long long res = 1; n   x = x % p; n   while (y > 0) { n     if (y & 1) res = (res * x) % p; n     y = y >> 1; n     x = (x * x) % p; n   } n   return res; n } n long long modInv(long long i, long long p = mod) { return binExp(i, p - 2, p); } n long long ncr(long long n, long long r, long long p = mod) { n   if (n < r) return 0; n   if (r == 0) return 1; n   return (fact[n] * modInv(fact[r], p) % p * modInv(fact[n - r], p) % p) % p; n } n long long divi(long long a, long long b, long long p = mod) { n   long long binv = modInv(b, p); n   long long res = ((a % p) * (binv % p)) % p; n   return res; n } n template <typename T> n istream &operator>>(istream &in, vector<T> &v) { n   for (auto &i : v) in >> i; n   return in; n } n template <typename T> n ostream &operator<<(ostream &ost, vector<T> v) { n   if (&ost == &cerr) { n     cerr <<  { ; n     long long cnt = v.size(); n     for (auto x : v) { n       cerr << x; n       cnt--; n       if (cnt) cerr <<  ,  ; n     } n     cerr <<  } ; n   } else n     for (auto i : v) ost << i <<    ; n   return ost; n } n template <typename T> n ostream &operator<<(ostream &ost, set<T> s) { n   if (&ost == &cerr) { n     cerr <<  { ; n     long long cnt = s.size(); n     for (auto x : s) { n       cerr << x; n       cnt--; n       if (cnt) cerr <<  ,  ; n     } n     cerr <<  } ; n   } else n     for (auto i : s) ost << i <<    ; n   return ost; n } n template <typename U, typename V> n ostream &operator<<(ostream &ost, map<U, V> &m) { n   if (&ost == &cerr) { n     cerr <<  { ; n     long long cnt = m.size(); n     for (auto x : m) { n       cerr << x; n       cnt--; n       if (cnt) cerr <<  ,  ; n     } n     cerr <<  } ; n   } else n     for (auto i : m) ost << i; n   return ost; n } n template <typename U, typename V> n istream &operator>>(istream &in, pair<U, V> &p) { n   in >> p.first >> p.second; n   return in; n } n template <typename U, typename V> n ostream &operator<<(ostream &ost, pair<U, V> p) { n   if (&ost == &cerr) n     ost <<  {  << p.first <<  :   << p.second <<  } ; n   else n     ost << p.first <<     << p.second <<    ; n   return ost; n } n template <typename... T> n void read(T &...args) { n   ((cin >> args), ...); n } n template <typename... T> n void print(T... args) { n   ((cout << args <<    ), ...); n   cout <<   n ; n } n void solution() { n   long long ans = -1; n   long long n, m; n   read(n, m); n   ; n   vector<long long> x(n); n   cin >> x; n   vector<vector<long long> > a(n, vector<long long>(m)); n   for (long long i = 0; 0 <= n ? i < n : i >= n; 0 < n ? i += 1 : i -= 1) n     for (long long j = 0; 0 <= m ? j < m : j >= m; 0 < m ? j += 1 : j -= 1) { n       scanf( %1d , &a[i][j]); n     } n   vector<long long> best; n   for (long long mask = 0; 0 <= 1 << n ? mask < 1 << n : mask >= 1 << n; n        0 < 1 << n ? mask += 1 : mask -= 1) { n     vector<long long> val(m); n     for (long long i = 0; 0 <= n ? i < n : i >= n; 0 < n ? i += 1 : i -= 1) { n       for (long long j = 0; 0 <= m ? j < m : j >= m; 0 < m ? j += 1 : j -= 1) { n         if (a[i][j]) { n           val[j] += ((mask >> i) & 1) ? +1 : -1; n         } n       } n     } n     vector<long long> p(m); n     iota(p.begin(), p.end(), 0LL); n     long long res = 0; n     for (long long i = 0; 0 <= n ? i < n : i >= n; 0 < n ? i += 1 : i -= 1) n       res += ((mask >> i) & 1) ? -x[i] : x[i]; n     sort(p.begin(), p.end(), n          [&](long long a, long long b) { return val[a] < val[b]; }); n     for (long long i = 0; 0 <= m ? i < m : i >= m; 0 < m ? i += 1 : i -= 1) { n       res += val[p[i]] * (i + 1); n     } n     if (res > ans) ans = res, best = p; n   } n   vector<long long> ansPerm(m); n   for (long long i = 0; 0 <= m ? i < m : i >= m; 0 < m ? i += 1 : i -= 1) n     ansPerm[best[i]] = i + 1; n   cout << ansPerm <<   n ; n } n signed main() { n   long long test = 1; n   cin >> test; n   for (long long t = 1; t <= test; t++) { n     solution(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, a, x, b, y; n   cin >> n >> a >> x >> b >> y; n   while (a != x && b != y) { n     if (a == b) break; n     a = (a % n) + 1; n     if (b == 1) n       b = n; n     else n       b--; n   } n   if (a == b) n     cout <<  YES ; n   else n     cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n int lcm(int a, int b) { return a / gcd(a, b) * b; } n const int mod = 1e9 + 7; n long long binpow(long long a, long long b) { n   long long res = 1; n   while (b > 0) n     if (b & 1) { n       res = (long long)(res * 1ll * a); n       --b; n     } else { n       a = (long long)(a * 1ll * a); n       b >>= 1; n     } n   return (long long)res; n } n vector<vector<int>> g; n vector<bool> used; n int pred = -1; n int timer = 0; n vector<int> tin, tout; n void dfs(int v, int p = -1) { n   used[v] = true; n   tin[v] = timer; n   timer++; n   for (auto to : g[v]) { n     if (!used[to]) dfs(to); n   } n   tout[v] = timer; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int q; n   cin >> q; n   while (q--) { n     int n, a, b, da, db; n     cin >> n >> a >> b >> da >> db; n     vector<vector<int>> g; n     g.resize(n); n     for (int i = 0; i < n - 1; i++) { n       int u, v; n       cin >> u >> v; n       u--; n       v--; n       g[u].push_back(v); n       g[v].push_back(u); n     } n     if (da * 2 >= db) { n       cout <<  Alice  << endl; n       continue; n     } n     int starta = a - 1; n     int startb = b - 1; n     vector<bool> used(n); n     queue<int> q; n     used[starta] = true; n     q.push(starta); n     vector<int> d(n); n     while (!q.empty()) { n       int v = q.front(); n       q.pop(); n       for (auto to : g[v]) { n         if (!used[to]) { n           used[to] = true; n           q.push(to); n           d[to] = d[v] + 1; n         } n       } n     } n     int ind_max = 0; n     int maxi = 0; n     for (int i = 0; i < n; i++) { n       if (d[i] > maxi) { n         maxi = d[i]; n         ind_max = i; n       } n     } n     if (abs(d[startb]) <= da) { n       cout <<  Alice  << endl; n       continue; n     } n     used.clear(); n     used.resize(n); n     used[ind_max] = true; n     q.push(ind_max); n     d.clear(); n     d.resize(n); n     while (!q.empty()) { n       int v = q.front(); n       q.pop(); n       for (auto to : g[v]) { n         if (!used[to]) { n           used[to] = true; n           q.push(to); n           d[to] = d[v] + 1; n         } n       } n     } n     int ans = 0; n     for (int i = 0; i < n; i++) { n       ans = max(ans, d[i]); n     } n     if (double(ans) / da > 2.0) { n       cout <<  Bob  << endl; n     } else { n       cout <<  Alice  << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n template <typename T> n T pow(T a, T b, long long m) { n   T ans = 1; n   while (b > 0) { n     if (b % 2 == 1) ans = (ans * a) % m; n     b /= 2; n     a = (a * a) % m; n   } n   return ans % m; n } n using namespace std; n bool B[100], G[100]; n int main() { n   int n, m; n   cin >> n >> m; n   int b; n   cin >> b; n   for (int(i) = (0); (i) < (b); (i)++) { n     int first; n     cin >> first; n     B[first] = 1; n   } n   int g; n   cin >> g; n   for (int(i) = (0); (i) < (g); (i)++) { n     int first; n     cin >> first; n     G[first] = 1; n   } n   for (int(i) = (0); (i) < (10000000); (i)++) n     B[i % n] = G[i % m] = B[i % n] | G[i % m]; n   bool boys = 1, girls = 1; n   for (int(i) = (0); (i) < (n); (i)++) boys &= B[i]; n   for (int(i) = (0); (i) < (m); (i)++) girls &= G[i]; n   if (boys && girls) n     cout <<  Yes  <<   n ; n   else n     cout <<  No  <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, lim; n struct node { n   int to; n   int val; n }; n stack<int> ans; n vector<node> g[5500]; n int f[5500][5500]; n int fa[5500][5500]; n void dfs(int u, int dep) { n   for (int i = 0; i != g[u].size(); i++) { n     int& co = g[u][i].val; n     int& v = g[u][i].to; n     if (f[dep][u] + co <= lim && f[dep][u] + co < f[dep + 1][v]) { n       fa[v][dep] = u; n       f[dep + 1][v] = f[dep][u] + co; n       dfs(v, dep + 1); n     } n   } n } n int main() { n   scanf( %d%d%d , &n, &m, &lim); n   int t1, t2, t3; n   for (int i = 0; i != m; i++) { n     scanf( %d%d%d , &t1, &t2, &t3); n     g[t1].push_back(node{t2, t3}); n   } n   memset(f, 0x7f, sizeof(f)); n   f[1][1] = 0; n   dfs(1, 1); n   int i = n; n   while (!fa[n][i]) i--; n   cout << i + 1 << endl; n   ans.push(n); n   while (fa[n][i]) { n     ans.push(fa[n][i]); n     n = fa[n][i]; n     i--; n   } n   cout << ans.top(); n   ans.pop(); n   while (!ans.empty()) { n     cout <<     << ans.top(); n     ans.pop(); n   } n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const long long LINF = 334ll << 53; n const int INF = 15 << 26; n const long long MOD = 1E9 + 7; n int main() { n   cin.tie(0); n   ios::sync_with_stdio(false); n   int n, q; n   cin >> n >> q; n   vector<int> t(n), s(n), d(n); n   priority_queue<pair<int, int>, vector<pair<int, int>>, n                  greater<pair<int, int>>> n       used; n   priority_queue<int, vector<int>, greater<int>> aval; n   for (int(i) = 0; (i) < (n); ++(i)) aval.push(i + 1); n   for (int(i) = 0; (i) < (q); ++(i)) { n     int now, s, t, ans = 0; n     cin >> now >> s >> t; n     while (!used.empty()) { n       if (used.top().first > now) break; n       aval.push(used.top().second); n       used.pop(); n     } n     if (aval.size() < s) { n       cout << -1 << endl; n     } else { n       for (int(j) = 0; (j) < (s); ++(j)) { n         ans += aval.top(); n         used.push(make_pair(now + t, aval.top())); n         aval.pop(); n       } n       cout << ans << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n using namespace std; n using ll = long long; n using ld = long double; n template <typename T> n using mat = vector<vector<T> >; n template <int mod> n struct modint { n   int x; n   modint() : x(0) {} n   modint(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {} n   modint &operator+=(const modint &p) { n     if ((x += p.x) >= mod) x -= mod; n     return *this; n   } n   modint &operator-=(const modint &p) { n     if ((x += mod - p.x) >= mod) x -= mod; n     return *this; n   } n   modint &operator*=(const modint &p) { n     x = (int)(1LL * x * p.x % mod); n     return *this; n   } n   modint &operator/=(const modint &p) { n     *this *= p.inverse(); n     return *this; n   } n   modint operator-() const { return modint(-x); } n   modint operator+(const modint &p) const { return modint(*this) += p; } n   modint operator-(const modint &p) const { return modint(*this) -= p; } n   modint operator*(const modint &p) const { return modint(*this) *= p; } n   modint operator/(const modint &p) const { return modint(*this) /= p; } n   bool operator==(const modint &p) const { return x == p.x; } n   bool operator!=(const modint &p) const { return x != p.x; } n   modint inverse() const { n     int a = x, b = mod, u = 1, v = 0, t; n     while (b > 0) { n       t = a / b; n       swap(a -= t * b, b); n       swap(u -= t * v, v); n     } n     return modint(u); n   } n   modint pow(int64_t n) const { n     modint ret(1), mul(x); n     while (n > 0) { n       if (n & 1) ret *= mul; n       mul *= mul; n       n >>= 1; n     } n     return ret; n   } n   friend ostream &operator<<(ostream &os, const modint &p) { return os << p.x; } n   friend istream &operator>>(istream &is, modint &a) { n     int64_t t; n     is >> t; n     a = modint<mod>(t); n     return (is); n   } n   static int get_mod() { return mod; } n }; n ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } n ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } n template <class T> n bool chmax(T &a, const T &b) { n   if (a < b) { n     a = b; n     return 1; n   } n   return 0; n } n template <class T> n bool chmin(T &a, const T &b) { n   if (b < a) { n     a = b; n     return 1; n   } n   return 0; n } n template <typename T> n T mypow(T x, ll n) { n   T ret = 1; n   while (n > 0) { n     if (n & 1) (ret *= x); n     (x *= x); n     n >>= 1; n   } n   return ret; n } n ll modpow(ll x, ll n, const ll mod) { n   ll ret = 1; n   while (n > 0) { n     if (n & 1) (ret *= x); n     (x *= x); n     n >>= 1; n     x %= mod; n     ret %= mod; n   } n   return ret; n } n uint64_t my_rand(void) { n   static uint64_t x = 88172645463325252ULL; n   x = x ^ (x << 13); n   x = x ^ (x >> 7); n   return x = x ^ (x << 17); n } n template <typename T> n struct edge { n   int src, to; n   T cost; n   edge(int to, T cost) : src(-1), to(to), cost(cost) {} n   edge(int src, int to, T cost) : src(src), to(to), cost(cost) {} n   edge &operator=(const int &x) { n     to = x; n     return *this; n   } n   operator int() const { return to; } n }; n template <typename T> n using Edges = vector<edge<T> >; n template <typename T> n using WeightedGraph = vector<Edges<T> >; n using UnWeightedGraph = vector<vector<int> >; n const long double eps = 0.001; n const long double PI = 3.141592653589793; n void solve() { n   ll x, y; n   cin >> x >> y; n   if (x > y) swap(x, y); n   ll a, b; n   cin >> a >> b; n   if (a * 2 > b) { n     cout << x * b + (y - x) * a << endl; n   } else { n     cout << (x + y) * a << endl; n   } n } n int main() { n   cin.tie(0); n   ios::sync_with_stdio(0); n   cout << setprecision(20); n   int t; n   cin >> t; n   for (int _ = (0), max_i = (t); _ < max_i; _++) { n     solve(); n   } n } n