#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 5; n vector<int> adj[N], nodesAtDepth[N]; n int degree[N], Dist[N]; n bool isDiam[N]; n int n; n int bfs(int start) { n   queue<pair<int, int>> q; n   q.push({start, start}); n   Dist[start] = 0; n   int mxDist = -1; n   int mxNode = -1; n   while (!q.empty()) { n     int node = q.front().first; n     int prev = q.front().second; n     q.pop(); n     if (mxDist < Dist[node]) { n       mxDist = Dist[node]; n       mxNode = node; n     } n     for (int to : adj[node]) { n       if (to == prev) continue; n       q.push({to, node}); n       Dist[to] = 1 + Dist[node]; n     } n   } n   assert(mxNode != -1); n   return mxNode; n } n void dfs(int node, int par, int Depth) { n   nodesAtDepth[Depth].push_back(node); n   for (int child : adj[node]) { n     if (child == par) continue; n     dfs(child, node, Depth + 1); n   } n } n void solve(int root) { n   for (int d = 0; d < n + 3; ++d) { n     nodesAtDepth[d].clear(); n   } n   dfs(root, root, 0); n   for (int d = 0; d < n + 3; ++d) { n     int firstNode = -1; n     for (int node : nodesAtDepth[d]) { n       if (firstNode == -1) { n         firstNode = node; n         continue; n       } n       if (degree[firstNode] != degree[node]) { n         return; n       } n     } n   } n   cout << root; n   exit(0); n } n signed main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   if (n == 1) { n     cout << 1; n     return 0; n   } n   for (int i = 0; i < n - 1; ++i) { n     int u, v; n     cin >> u >> v; n     ++degree[u]; n     ++degree[v]; n     adj[u].push_back(v); n     adj[v].push_back(u); n   } n   int end1 = bfs(1); n   int end2 = bfs(end1); n   int curr = end2; n   vector<int> diam; n   while (curr != end1) { n     diam.push_back(curr); n     for (int to : adj[curr]) { n       if (Dist[to] == -1 + Dist[curr]) { n         curr = to; n         break; n       } n     } n   } n   diam.push_back(end1); n   solve(end1); n   solve(end2); n   solve(diam[diam.size() / 2]); n   queue<pair<int, int>> q; n   q.push({diam[diam.size() / 2], diam[diam.size() / 2]}); n   while (!q.empty()) { n     int node = q.front().first; n     int par = q.front().second; n     q.pop(); n     if (degree[node] == 1) { n       solve(node); n       cout << -1; n       return 0; n     } n     for (int child : adj[node]) { n       if (child == par) continue; n       if (degree[child] > 2) continue; n       q.push({child, node}); n     } n   } n   cout << -1; n } n


#include <bits/stdc++.h> n using namespace std; n int h[4] = {0, 0, 1, -1}; n int v[4] = {1, -1, 0, 0}; n vector<vector<int> > adj; n bool *visited; n int num[501][501]; n bool done = false; n int sourceR, sourceC, destR, destC; n void DFS(int vertix) { n   if (!done) { n     visited[vertix] = true; n     for (int i = 0; i < adj[vertix].size(); i++) { n       if (!done && visited[adj[vertix][i]] && n           adj[vertix][i] == num[destR - 1][destC - 1]) { n         if (visited[vertix]) { n           done = true; n           cout <<  YES  << endl; n           return; n         } n       } n       if (!done && !visited[adj[vertix][i]]) { n         DFS(adj[vertix][i]); n       } n     } n   } n } n int main() { n   int n, m; n   cin >> n >> m; n   visited = new bool[n * m]; n   vector<int> temp; n   int cnt = 0; n   char a[501][501]; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       adj.push_back(temp); n       cin >> a[i][j]; n       visited[cnt] = false; n       num[i][j] = cnt++; n     } n   } n   cin >> sourceR >> sourceC >> destR >> destC; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (a[i][j] ==  X ) { n         visited[num[i][j]] = true; n       } n       for (int k = 0; k < 4; k++) { n         if (i + h[k] >= 0 && i + h[k] < n && j + v[k] >= 0 && j + v[k] < m) { n           adj[num[i][j]].push_back(num[i + h[k]][j + v[k]]); n         } n       } n     } n   } n   DFS(num[sourceR - 1][sourceC - 1]); n   if (!done) { n     cout <<  NO  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   vector<int> v; n   int max = 0; n   int mi = 0; n   for (int i = 0; i < n; ++i) { n     int temp; n     cin >> temp; n     v.push_back(temp); n     if (temp > max) { n       max = temp; n       mi = i; n     } n   } n   for (int i = 0; i < mi; ++i) { n     if (v[i + 1] <= v[i]) { n       if (v[i] == v[i + 1] && v[i + 1] == max) continue; n       cout <<  NO ; n       return 0; n     } n   } n   for (int i = n - 1; i > mi; i--) { n     if (v[i] >= v[i - 1]) { n       if (v[i] == v[i - 1] && v[i - 1] == max) continue; n       cout <<  NO ; n       return 0; n     } n   } n   cout <<  YES ; n   return 0; n } n


#include <bits/stdc++.h> n const double PI = 3.1415926; n const int KL = 1e6; n using namespace std; n long long q, x, y, z, n, m, p[KL], mn = 1e9, mx = -1e9, ans; n string t; n vector<int> vec[KL]; n set<int> s; n map<int, int> M; n int pr[KL], sz[KL]; n int prfind(int node) { n   if (pr[node] == node) n     return node; n   else n     return pr[node] = prfind(pr[node]); n } n void make_group(int node1, int node2) { n   int fa1 = prfind(node1); n   int fa2 = prfind(node2); n   if (fa1 == fa2) return; n   if (sz[fa1] >= sz[fa2]) { n     pr[fa2] = fa1; n     sz[fa1] += sz[fa2]; n   } else { n     pr[fa1] = fa2; n     sz[fa2] += sz[fa1]; n   } n } n int main() { n   scanf( %d , &n); n   scanf( %d , &m); n   for (int i = 0; i < m; i++) { n     scanf( %d , &x); n     for (int j = 0; j < x; j++) { n       scanf( %d , &y); n       vec[i].push_back(y); n     } n   } n   for (int i = 1; i <= n; i++) { n     pr[i] = i; n     sz[i] = 1; n   } n   for (int i = 0; i < m; i++) { n     for (int j = 1; j < vec[i].size(); j++) { n       make_group(vec[i][j], vec[i][j - 1]); n     } n   } n   for (int i = 1; i <= n; i++) { n     cout << sz[prfind(i)] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, tmp; n   cin >> n; n   multiset<int> a; n   for (int i = 0; i < n; i++) { n     cin >> tmp; n     a.insert(tmp); n   } n   int ans = 0; n   while (!a.empty()) { n     int h = 0; n     while (a.lower_bound(h) != a.end()) { n       a.erase(a.lower_bound(h)); n       h++; n     } n     if (h) ans++; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool sortbysec(const pair<int, int> a, const pair<int, int> b) { n   return a.second < b.second; n } n int main() { n   string s1; n   cin >> s1; n   string s2; n   cin >> s2; n   bool flag = false; n   string ans =   0 ; n   for (int i = 0; i < s1.length(); i++) { n     if (int(s1[i]) < int(s2[i])) flag = true; n   } n   if (s1.length() != s2.length()) flag = true; n   if (flag == false) { n     for (int i = 0; i < s1.length(); i++) { n       if (int(s1[i]) == int(s2[i])) n         ans +=  z ; n       else if (int(s1[i]) > int(s2[i])) n         ans += s2[i]; n     } n   } n   if (flag == true) n     cout <<  -1  << endl; n   else n     cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct dragon { n   int sila; n   int dodatek; n }; n bool operator<(dragon A, dragon B) { return A.sila < B.sila; } n int main() { n   int s, n; n   cin >> s >> n; n   vector<dragon> D(n); n   for (int i = 0; i < n; i++) { n     cin >> D[i].sila >> D[i].dodatek; n   } n   sort(D.begin(), D.end()); n   for (int p = 0; p < n; p++) { n     if (s > D[p].sila) n       s += D[p].dodatek; n     else { n       cout <<  NO  << endl; n       return 0; n     } n   } n   cout <<  YES  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = acos(-1); n const double eps = 1e-12; n const int inf = 2000000000; n const long long int infLL = (long long int)1e18; n long long int MOD = 1000000007; n int MOD1 = 1000000007; n int MOD2 = 1000000009; n inline bool checkBit(long long int n, long long int i) { n   return n & (1LL << i); n } n inline long long int setBit(long long int n, long long int i) { n   return n | (1LL << i); n   ; n } n inline long long int resetBit(long long int n, long long int i) { n   return n & (~(1LL << i)); n } n int dx[] = {0, 0, +1, -1}; n int dy[] = {+1, -1, 0, 0}; n inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; } n inline bool isLeapYear(long long int year) { n   return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0); n } n inline void normal(long long int &a) { n   a %= MOD; n   (a < 0) && (a += MOD); n } n inline long long int modMul(long long int a, long long int b) { n   a %= MOD, b %= MOD; n   normal(a), normal(b); n   return (a * b) % MOD; n } n inline long long int modAdd(long long int a, long long int b) { n   a %= MOD, b %= MOD; n   normal(a), normal(b); n   return (a + b) % MOD; n } n inline long long int modSub(long long int a, long long int b) { n   a %= MOD, b %= MOD; n   normal(a), normal(b); n   a -= b; n   normal(a); n   return a; n } n inline long long int modPow(long long int b, long long int p) { n   long long int r = 1LL; n   while (p) { n     if (p & 1) r = modMul(r, b); n     b = modMul(b, b); n     p >>= 1LL; n   } n   return r; n } n inline long long int modDiv(long long int a, long long int b) { n   return modMul(a, modPow(b, MOD - 2)); n } n bool comp(const pair<long long int, pair<long long int, long long int> > &p1, n           const pair<long long int, pair<long long int, long long int> > &p2) { n   return p1.first > p2.first; n } n bool comp1(const pair<long long int, long long int> &p1, n            const pair<long long int, long long int> &p2) { n   if (p1.first == p2.first) { n     return p1.second > p2.second; n   } n   return p1.first < p2.first; n } n long long int converter(string a) { n   long long int i, mul = 1LL, r, t, ans = 0LL; n   if (a.length() == 0) return 0; n   for (i = a.length() - 1; i >= 0; i--) { n     t = a[i] -  0 ; n     r = t % 10; n     ans += (mul * r); n     mul = mul * 10; n   } n   return ans; n } n int msb(unsigned x) { n   union { n     double a; n     int b[2]; n   }; n   a = x; n   return (b[1] >> 20) - 1023; n } n const int MAX = 300005; n long long int g[3][MAX], a[MAX]; n long long int x, y, z, magic, n; n const int ex = 70; n const int lim = 1000; n int grundy(int a, int b, int c) { n   set<long long int> s; n   s.insert(a); n   s.insert(b); n   s.insert(c); n   int val = 0; n   while (s.find(val) != s.end()) ++val; n   return val; n } n void calc() { n   for (int i = 1; i <= lim; ++i) { n     long long int cur1 = max(0LL, i - x); n     long long int cur2 = max(0LL, i - y); n     long long int cur3 = max(0LL, i - z); n     g[0][i] = grundy(g[0][cur1], g[1][cur2], g[2][cur3]); n     g[1][i] = grundy(g[0][cur1], inf, g[2][cur3]); n     g[2][i] = grundy(g[0][cur1], g[1][cur2], inf); n   } n } n void findPeriod() { n   magic = 0; n   for (int perLen = 1; !magic; ++perLen) { n     int f = 1; n     for (int i = ex; i < ex + perLen && f; ++i) { n       for (int j = i; j <= lim && f; j += perLen) { n         if (g[0][i] != g[0][j] || g[1][i] != g[1][j] || g[2][i] != g[2][j]) n           f = 0; n       } n     } n     if (f) magic = perLen; n   } n } n int ask(long long int pile, int state) { n   pile = max(0LL, pile); n   if (pile <= lim) n     return g[state][pile]; n   else n     return g[state][(pile - ex) % magic + ex]; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   cout.unsetf(ios::floatfield); n   cout.precision(20); n   cout.setf(ios::fixed, ios::floatfield); n   ; n   int t; n   cin >> t; n   while (t--) { n     cin >> n >> x >> y >> z; n     calc(); n     findPeriod(); n     if (magic >= 30) { n       cout <<  MAgic  << magic <<   n ; n       return 0; n     } n     long long int nimber = 0; n     for (int i = 1; i <= n; ++i) { n       cin >> a[i]; n       nimber ^= ask(a[i], 0); n     } n     int ans = 0; n     for (int i = 1; i <= n; ++i) { n       long long int save = nimber ^ ask(a[i], 0); n       long long int temp = save ^ ask(a[i] - x, 0); n       if (!temp) ans++; n       temp = save ^ ask(a[i] - y, 1); n       if (!temp) ans++; n       temp = save ^ ask(a[i] - z, 2); n       if (!temp) ans++; n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long qpow(long long x, long long k, long long mod) { n   long long ret = 1; n   while (k) { n     if (k & 1) { n       ret = ret * x; n       ret = ret % mod; n     } n     x = x * x; n     x = x % mod; n     k = (k >> 1); n   } n   return ret; n } n const int Maxn = 1005; n const int md = 998244353; n int n, m; n int a[Maxn][Maxn]; n long long e[Maxn][Maxn]; n long long inv[Maxn * Maxn]; n long long cnt[2][Maxn]; n int r, c; n vector<pair<int, pair<int, int> > > vv; n void pre() { n   for (int i = 1; i < Maxn * Maxn; i++) { n     inv[i] = qpow(i, md - 2, md); n   } n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       scanf( %d , a[i] + j); n       vv.push_back(pair<int, pair<int, int> >(a[i][j], pair<int, int>(i, j))); n     } n   } n   scanf( %d%d , &r, &c); n   pre(); n   sort((vv).begin(), (vv).end()); n   long long sume = 0, sumx = 0, sumx2 = 0, sumy = 0, sumy2 = 0, ans = 0; n   for (int i = 0, j = 0; i < vv.size(); i++) { n     if (i && vv[i].first != vv[i - 1].first) { n       for (; j < i; j++) { n         long long nx = vv[j].second.first, ny = vv[j].second.second; n         sume = (sume + e[nx][ny]) % md; n         sumx = (sumx + nx) % md; n         sumx2 = (sumx2 + nx * nx) % md; n         sumy = (sumy + ny) % md; n         sumy2 = (sumy2 + ny * ny) % md; n       } n     } n     long long x = vv[i].second.first, y = vv[i].second.second; n     long long tmp = (sume + sumx2 + sumy2 - sumx * x * 2 - sumy * y * 2) % md; n     if (j) tmp = (tmp * inv[j] + x * x + y * y) % md; n     if (tmp < 0) tmp += md; n     e[x][y] = tmp; n   } n   printf( %lld n , e[r][c]); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n using namespace std; n int main() { n   int N; n   cin >> N; n   pair<char, int> list[N]; n   for (int i = 0; i < N; i++) cin >> list[i].first >> list[i].second; n   int initial = 0; n   set<int> presents; n   for (int i = 0; i < N; i++) { n     int count = 0; n     char sign = list[i].first; n     int id = list[i].second; n     if (sign ==  + ) { n       presents.insert(id); n     } else if (sign ==  - ) { n       if (presents.count(id)) { n         presents.erase(id); n       } else n         initial++; n     } else n       assert(false); n   } n   presents.clear(); n   int maxi = initial; n   int count = initial; n   for (int i = 0; i < N; i++) { n     char sign = list[i].first; n     int id = list[i].second; n     if (sign ==  + ) { n       count++; n       presents.insert(id); n     } else if (sign ==  - ) { n       count--; n       if (presents.count(id)) presents.erase(id); n     } n     maxi = max(maxi, count); n   } n   cout << maxi << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int rozmiar_kubelka = 356; n int n, m; n vector<int> her; n vector<pair<char, int> > mov; n int dp[(1 << 20)]; n void policz() { n   dp[0] = 0; n   int a = 1; n   for (; a < (1 << m); a++) { n     int c = __builtin_popcount(a); n     c = m - c; n     auto akt = mov[c]; n     if (akt.second == 1) n       dp[a] = -1000000000; n     else n       dp[a] = 1000000000; n     if (akt == make_pair( p , 1)) { n       int it = 0; n       for (int i = 1; true; i *= 2) { n         if (i & a) { n           dp[a] = dp[a ^ i] + her[it]; n           break; n         } n         it++; n       } n     } n     if (akt == make_pair( p , 2)) { n       int it = 0; n       for (int i = 1; true; i *= 2) { n         if (i & a) { n           dp[a] = dp[a ^ i] - her[it]; n           break; n         } n         it++; n       } n     } n     if (akt == make_pair( b , 1)) { n       int it = m - 1; n       for (int i = 1; i <= a; i *= 2) { n         it--; n         if (i & a) { n           dp[a] = max(dp[a], dp[a ^ i]); n         } n       } n     } n     if (akt == make_pair( b , 2)) { n       int it = m - 1; n       for (int i = 1; i <= a; i *= 2) { n         it--; n         if (i & a) { n           dp[a] = min(dp[a], dp[a ^ i]); n         } n       } n     } n   } n } n int main() { n   vector<int> vec; n   scanf( %d , &n); n   for (int i = 0; i < (n); ++i) { n     int a; n     scanf( %d , &a); n     vec.push_back(a); n   } n   scanf( %d , &m); n   sort(vec.begin(), vec.end()); n   for (int i = ((int)(n)) - 1; i >= (n - m); --i) her.push_back(vec[i]); n   for (int i = 0; i < (m); ++i) { n     char p; n     int a; n     scanf( %c%c%d , &p, &p, &a); n     mov.push_back({p, a}); n   } n   policz(); n   printf( %d , dp[(1 << m) - 1]); n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   long long t; n   cin >> t; n   for (long long _ = 0; _ < t; ++_) { n     long long n; n     cin >> n; n     vector<long long> a(n + 1); n     for (long long i = 0; i < n; ++i) { n       cin >> a[i]; n     } n     long long l = a[0]; n     long long r = n; n     long long cur = l; n     string ans =  Yes ; n     for (long long i = 0; i < n; ++i) { n       if (a[i] != cur) { n         ans =  No ; n       } n       ++cur; n       if (a[i] == r) { n         r = l - 1; n         l = a[i + 1]; n         cur = l; n       } n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char str[1000]; n int main() { n   int n, k; n   while (scanf( %d %d , &n, &k) == 2) { n     scanf( %s , str + 1); n     int left = k - 1; n     int right = n - k; n     int di; n     if (right >= left) { n       di = 1; n       for (int i = k; i > 1; i--) printf( LEFT n ); n     } else { n       di = 2; n       for (int i = k; i < n; i++) printf( RIGHT n ); n     } n     if (di == 1) { n       for (int i = 1; i <= n; i++) { n         printf( PRINT %c n , str[i]); n         if (i != n) printf( RIGHT n ); n       } n     } else { n       for (int i = n; i >= 1; i--) { n         printf( PRINT %c n , str[i]); n         if (i != 1) printf( LEFT n ); n       } n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int gcd(int a, int b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int a, b, c, d; n   cin >> a >> b >> c >> d; n   if (a * d < c * b) { n     int g = gcd(b * c - a * d, b * c); n     cout << ((b * c - a * d) / g) <<  /  << (b * c / g) << endl; n   } else { n     int g = gcd(a * d - b * c, a * d); n     cout << ((a * d - b * c) / g) <<  /  << (a * d / g) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int x, y; n   long double maxd = 0.00000000; n   long long int ans = 0; n   long long int n; n   cin >> n; n   while (n--) { n     long long int a, b; n     cin >> a >> b; n     ans = max(ans, (a + b)); n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int i, j, x[2][4], y[2][4]; n   pair<int, int> p[2][4]; n   for (j = 0; j < 2; j++) { n     for (i = 0; i < 4; i++) { n       cin >> x[j][i] >> y[j][i]; n       p[j][i] = {x[j][i], y[j][i]}; n     } n   } n   sort(p[0], p[0] + 4); n   sort(p[1], p[1] + 4); n   int a = p[1][0].first + p[1][0].second, b = p[1][2].first + p[1][2].second; n   int c = p[1][1].second - p[1][1].first, d = p[1][2].second - p[1][1].first; n   set<pair<int, int>> s; n   for (i = -100; i <= 100; i++) { n     for (j = -100; j <= 100; j++) { n       if (i + j >= a && i + j <= b && j - i >= c && j - i <= d) { n         s.insert({i, j}); n       } n     } n   } n   a = p[0][0].first, b = p[0][0].second, c = p[0][3].first, d = p[0][3].second; n   for (i = a; i <= c; i++) { n     for (j = b; j <= d; j++) { n       pair<int, int> q = {i, j}; n       if (s.count(q)) { n         cout <<  YES  << endl; n         return 0; n       } n     } n   } n   cout <<  NO  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f; n const int maxn = 1e5 + 5; n const int mod = 1e9 + 7; n vector<int> v[26]; n int main() { n   int n; n   cin >> n; n   string s; n   cin >> s; n   for (int i = 0; i < n; i++) v[s[i] -  a ].push_back(i); n   int m; n   cin >> m; n   while (m--) { n     string s2; n     cin >> s2; n     int t[26]; n     memset(t, 0, sizeof(t)); n     for (int i = 0; i < s2.size(); i++) t[s2[i] -  a ]++; n     int ans = -1; n     for (int i = 0; i < 26; i++) { n       if (t[i]) ans = max(ans, v[i][t[i] - 1] + 1); n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n const int N = 100005; n int solve(int n, int k, int cnt[]) { n   if (k == n || cnt[0] == 0 || cnt[1] == 0) { n     return 1 - cnt[1] % 2; n   } n   if ((n - k) / 2 < cnt[0] && (n - k) / 2 < cnt[1]) { n     if ((n - k) % 2 == 0) n       return 1; n     else n       return 0; n   } n   if ((n - k) / 2 >= cnt[0]) { n     return 1 - k % 2; n   } n   return 1; n } n int main() { n   int n, k; n   scanf( %d%d , &n, &k); n   int cnt[2]; n   cnt[0] = cnt[1] = 0; n   for (int i = 0; i < n; i++) { n     int tmp; n     scanf( %d , &tmp); n     cnt[tmp % 2]++; n   } n   printf( %s , solve(n, k, cnt) ?  Daenerys  :  Stannis ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(){ n  int T; cin >> T;  n  long long A[114514];  n  while(T--){ n   int N; cin >> N;  n   long long S = 0;  n   for(int i = 0; i < N; i++){ n    cin >> A[i]; S += A[i];  n   } n   sort(A, A+N, greater<long long>());  n   if(S == 0) cout << 0 <<   n ;  n   else cout << max({A[1] * (N-1) - S, A[0] * (N-1) - S, N-2 - (S-1) % (N-1)}) <<   n ;  n  } n }


#include <bits/stdc++.h> n #define ll long long int n #define vi vector<int> n #define vll vector<long long int> n #define MAX(a, b) ((a) > (b) ? (a) : (b)) n #define MIN(a, b) ((a) < (b) ? (a) : (b)) n #define ABS(x) ((x) < 0 ? -(x) : (x)) n #define setpr(x, y) cout << fixed << setprecision(x) << y n #define tr(x) cout << (#x) <<   =   << (x) <<   n ; n using namespace std; n void solver(void); n ll MOD = 1e9 + 7; n double pi = 2 * acos(0.0); n //vector<vector<ll>> mat( n , vector<ll> (m, 0)); n  n int main() n { n     cin.tie(NULL); n     ios_base::sync_with_stdio(false); n     int t; n     cin >> t; n     while (t--) n         solver(); n } n void solver(void) n { n     //input n     ll n; n     cin >> n; n     vll v(n); n     for (ll i = 0; i < n; i++) n     { n         v[i] = i + 1; n     } n     if (n % 2 == 0) n     { n  n         for (ll i = 0; i < n - 1; i = i + 2) n         { n             swap(v[i], v[i + 1]); n         } n     } n     else n     { n         for (ll i = 0; i <= n - 3; i = i + 2) n         { n             swap(v[i], v[i + 1]); n         } n         swap(v[n - 2], v[n - 1]); n     } n     for (ll i = 0; i < n; i++) n     { n         cout << v[i] <<    ; n     } n     cout <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 200005; n long long a[maxn]; n int n; n long long k; n long long d[maxn]; n int main() { n   cin >> n >> k; n   for (int i = 1; i <= n; i++) scanf( %I64d , &a[i]); n   long long sum = 0; n   int num = 0; n   for (int i = 1; i <= n; i++) { n     d[i] = sum - (a[i] * (n - i) * (i - num - 1)); n     if (d[i] < k) { n       printf( %d n , i); n       num++; n     } else n       sum += a[i] * (i - num - 1); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool check(string s) { n   int count = 0; n   for (char el : s) { n     if (el ==  ( ) { n       count++; n     } else { n       count--; n     } n     if (count < 0) return false; n   } n   return true; n } n void solve() { n   int n; n   cin >> n; n   string s; n   cin >> s; n   if (s[0] ==  )  || s[s.size() - 1] ==  (  || s.size() % 2) { n     cout <<  :(  << endl; n     return; n   } n   int in = 0, out = 0; n   for (char el : s) { n     in += (el ==  ( ); n     out += (el ==  ) ); n   } n   if (in > n / 2 || out > n / 2) { n     cout <<  :(  << endl; n     return; n   } n   int first = (n / 2) - in; n   int second = (n / 2) - out; n   int count = 0; n   for (int i = 0; i < n; i++) { n     if (s[i] ==  ? ) { n       if (first > 0) { n         s[i] =  ( ; n         first--; n         count++; n       } else { n         s[i] =  ) ; n         second--; n         count--; n       } n     } else if (s[i] ==  ( ) { n       count++; n     } else { n       count--; n     } n     if (count == 0 && i != n - 1) { n       cout <<  :(  << endl; n       return; n     } else if (count < 0) { n       cout <<  :(  << endl; n       return; n     } n   } n   cout << (check(s) ? s :  :( ) << endl; n } n int main() { solve(); } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, i, j; n   cin >> n; n   vector<int> ct[n + 1]; n   for (i = 0; i < 2 * n; i++) { n     int x; n     cin >> x; n     ct[x].push_back(i); n   } n   int p1 = 0, p2 = 0; n   long long ans = 0; n   for (i = 1; i <= n; i++) { n     ans += min(abs(ct[i][0] - p1) + abs(ct[i][1] - p2), n                abs(ct[i][0] - p2) + abs(ct[i][1] - p1)); n     p1 = ct[i][0]; n     p2 = ct[i][1]; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n const int N = 1e6 + 7; n const int INF = 0x3f3f3f3f; n const int mod = 1e9 + 7; n const double eps = 1e-7; n using namespace std; n int a[N], s[N]; n map<int, int> asum, ssum; n map<int, vector<int> > pos; n map<int, int> size; n int main() { n   int n, m, k, S; n   cin >> m >> k >> n >> S; n   for (int i = 1; i <= m; i++) { n     scanf( %d , a + i); n     asum[a[i]]++; n   } n   for (int i = 1; i <= S; i++) { n     scanf( %d , s + i); n     ssum[s[i]]++; n   } n   for (int i = 1; i <= m; i++) { n     if (ssum[a[i]] > 0) { n       pos[a[i]].push_back(i); n     } n   } n   for (auto x : ssum) { n     if (asum[x.first] < x.second) { n       puts( -1 ); n       return 0; n     } n   } n   for (auto x : pos) { n     size[x.first] = x.second.size(); n   } n   int p, q, l, r, minl, maxr; n   bool f; n   bool flag = 1; n   for (int num = 1; num <= n; num++) { n     p = (num - 1) * k; n     f = 0; n     minl = INF; n     maxr = -1; n     for (auto x : pos) { n       q = upper_bound(x.second.begin(), x.second.end(), p) - x.second.begin(); n       if (q + ssum[x.first] > size[x.first]) { n         f = 1; n         break; n       } n       minl = min(minl, x.second[q]); n       maxr = max(maxr, x.second[q + ssum[x.first] - 1]); n     } n     if (f) break; n     if (m - maxr < (n - num) * k) n       continue; n     else { n       printf( %d n , max(0, maxr - p - k)); n       int cnt = maxr - p; n       bool ff = 1; n       for (int i = p + 1; i <= maxr; i++) { n         if (cnt <= k) break; n         if (ssum[a[i]] == 0) { n           printf( %s%d , ff ?    :    , i); n           cnt--; n           ff = 0; n         } else { n           ssum[a[i]]--; n         } n       } n       cout << endl; n       flag = 0; n       break; n     } n   } n   if (flag) puts( -1 ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n long long mypow(long long base, long long exp) { n   long long res = 1; n   while (exp) { n     if (exp & 1) res = (res * base) % mod; n     exp >>= 1, base = (base * base) % mod; n   } n   return res; n } n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n long long mult(long long a, long long b) { n   if (a < 0) { n     a += mod; n   } n   if (b < 0) { n     b += mod; n   } n   return ((a % mod) * (b % mod)) % mod; n } n long long add(long long a, long long b) { n   if (a < 0) { n     a += mod; n   } n   if (b < 0) { n     b += mod; n   } n   return (a % mod + b % mod) % mod; n } n long long sub(long long a, long long b) { n   if (a < 0) { n     a += mod; n   } n   if (b < 0) { n     b += mod; n   } n   return (a % mod + mod - b % mod) % mod; n } n void err(istream_iterator<string> it) {} n template <typename T, typename... Args> n void err(istream_iterator<string> it, T a, Args... args) { n   cerr << *it <<   =   << a <<   |  ; n   err(++it, args...); n } n const long long inf = 1e18; n const int nax = 1e5 + 5; n void SolveTestCase() { n   long long n; n   cin >> n; n   long long a[n]; n   long long sum = 0; n   for (long long i = 0; i < n; i++) { n     cin >> a[i]; n     sum += a[i]; n   } n   long long sum2 = 0; n   for (long long i = 0; i < n; i++) { n     sum2 += a[i]; n     if (sum2 >= (sum + 1) / 2) { n       cout << i + 1; n       break; n     } n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int t = 1; n   for (long long i = 0; i < t; i++) { n     SolveTestCase(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxN = 1000 * 1000 + 5; n bool mark[4][maxN]; n int n, m, comp; n inline void dfs(int x, int y) { n   if (mark[x][y]) return; n   mark[x][y] = true; n   if (x == 0 && y == 1) mark[3][1] = true; n   if (x == 0 && y == m) mark[1][1] = true; n   if (x == 1 && y == 1) mark[0][m] = true; n   if (x == 1 && y == n) mark[2][m] = true; n   if (x == 2 && y == 1) mark[3][n] = true; n   if (x == 2 && y == m) mark[1][n] = true; n   if (x == 3 && y == m) mark[2][1] = true; n   if (x == 3 && y == 1) mark[0][1] = true; n   if (x == 0) { n     int dr = m - y; n     int dl = y - 1; n     int dd = n - 1; n     int xr = 1 + min(dr, dd), yr = y + min(dr, dd); n     int xl = 1 + min(dl, dd), yl = y - min(dl, dd); n     (xr == n) ? dfs(2, yr) : dfs(1, xr); n     (xl == n) ? dfs(2, yl) : dfs(3, xl); n   } n   if (x == 2) { n     int dr = m - y; n     int dl = y - 1; n     int du = n - 1; n     int xr = n - min(dr, du), yr = y + min(dr, du); n     int xl = n - min(dl, du), yl = y - min(dl, du); n     (xr == 1) ? dfs(0, yr) : dfs(1, xr); n     (xl == 1) ? dfs(0, yl) : dfs(3, xl); n   } n   if (x == 1) { n     int dd = n - y; n     int du = y - 1; n     int dl = m - 1; n     int xu = y - min(du, dl), yu = m - min(du, dl); n     int xd = y + min(dd, dl), yd = m - min(dd, dl); n     (yu == 1) ? dfs(3, xu) : dfs(0, yu); n     (yd == 1) ? dfs(3, xd) : dfs(2, yd); n   } n   if (x == 3) { n     int dd = n - y; n     int du = y - 1; n     int dr = m - 1; n     int xu = y - min(du, dr), yu = 1 + min(du, dr); n     int xd = y + min(dd, dr), yd = 1 + min(dd, dr); n     (yu == m) ? dfs(1, xu) : dfs(0, yu); n     (yd == m) ? dfs(1, xd) : dfs(2, yd); n   } n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= m; i++) { n     if (!mark[0][i]) { n       comp++; n       dfs(0, i); n     } n     if (!mark[2][i]) { n       comp++; n       dfs(2, i); n     } n   } n   for (int i = 1; i <= n; i++) { n     if (!mark[1][i]) { n       comp++; n       dfs(1, i); n     } n     if (!mark[3][i]) { n       comp++; n       dfs(3, i); n     } n   } n   printf( %d n , comp); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 100; n const long double INF = 2e30; n struct node { n   int id; n   long double da, db, len; n   node(int _id = 0, long double _da = 0, long double _db = 0, n        long double _len = 0) { n     id = _id; n     da = _da; n     db = _db; n     len = _len; n   } n }; n node a[maxn]; n int n; n double ax, ay, bx, by, tx, ty; n double x[maxn], y[maxn]; n long double len[maxn], da[maxn], db[maxn]; n bool cmpb(node a, node b) { n   long double ans1 = a.db - a.len; n   long double ans2 = b.db - b.len; n   return ans1 < ans2; n } n bool cmpa(node a, node b) { n   long double ans1 = a.da - a.len; n   long double ans2 = b.da - b.len; n   return ans1 < ans2; n } n void solve() { n   long double sum = 0; n   for (int i = 0; i < n; i++) { n     a[i].id = i; n     long double dx, dy; n     dx = ax - x[i]; n     dy = ay - y[i]; n     a[i].da = sqrt(dx * dx + dy * dy); n     dx = bx - x[i]; n     dy = by - y[i]; n     a[i].db = sqrt(dx * dx + dy * dy); n     dx = tx - x[i]; n     dy = ty - y[i]; n     a[i].len = sqrt(dx * dx + dy * dy); n     sum += a[i].len * 2; n     len[i] = a[i].len; n     da[i] = a[i].da; n     db[i] = a[i].db; n   } n   long double ans = INF; n   sort(a, a + n, cmpb); n   for (int i = 0; i < n; i++) { n     if (n > 1) { n       int id = a[0].id; n       if (i == id) { n         id = a[1].id; n       } n       long double tmp = sum - len[i] + da[i] - len[id] + db[id]; n       ans = min(ans, tmp); n     } n     long double tmp = sum - len[i] + da[i]; n     ans = min(ans, tmp); n   } n   sort(a, a + n, cmpa); n   for (int i = 0; i < n; i++) { n     if (n > 1) { n       int id = a[0].id; n       if (i == id) { n         id = a[1].id; n       } n       long double tmp = sum - len[i] + db[i] - len[id] + da[id]; n       ans = min(ans, tmp); n     } n     long double tmp = sum - len[i] + db[i]; n     ans = min(ans, tmp); n   } n   printf( %.14f n , (double)ans); n } n int main() { n   while (scanf( %lf%lf%lf%lf%lf%lf , &ax, &ay, &bx, &by, &tx, &ty) != EOF) { n     scanf( %d , &n); n     for (int i = 0; i < n; i++) { n       scanf( %lf%lf , &x[i], &y[i]); n     } n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a, b, n; n long long power(long long b, long long p) { n   if (p == 0) return 1; n   long long ret = power(b, p / 2); n   ret *= ret; n   if (p & 1) ret *= b; n   return ret; n } n long long check(long long x) { return a * power(x, n); } n int main() { n   cin >> a >> b >> n; n   if (b == 0) { n     cout << 0 <<   n ; n     return 0; n   } n   if (a == 0) { n     cout <<  No solution  n          <<   n ; n     return 0; n   } n   if (b % a) { n     cout <<  No solution  n          <<   n ; n     return 0; n   } n   long long x = pow(abs(b / a), 1.0 / n) + 0.5; n   if (check(x) != b) x *= -1; n   if (check(x) != b) { n     cout <<  No solution  n          <<   n ; n     return 0; n   } n   cout << x <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n int ma[100005]; n int main() { n   int n, k; n   scanf( %d%d , &n, &k); n   for (int i = 1; i <= k + 1; i++) { n     if (i & 1) n       printf( %d  , i / 2 + 1); n     else n       printf( %d  , k + 2 - i / 2); n   } n   int s = (k + 1) / 2 + 1; n   int e = k + 2 - (k + 1) / 2; n   for (int i = s + 1; i < e - 1; i++) { n     printf( %d  , i); n   } n   for (int i = k + 2; i <= n; i++) { n     printf( %d  , i); n   } n   if (s + 1 <= e - 1) printf( %d n , e - 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n const long long maxn = 2e5 + 5; n struct range { n   long long a, b, c, id; n   range(long long A, long long B, long long C, long long ID) { n     a = A; n     b = B; n     c = C; n     id = ID; n   } n }; n bool compare(const range &i, const range &j) { n   if (i.a < j.a) return 1; n   if (i.a == j.a) return i.c < j.c; n   return 0; n } n long long ans[maxn]; n void finish() { n   cout <<  NO  <<   n ; n   ; n   exit(0); n } n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   long long n; n   cin >> n; n   long long a[n + 1], b[n + 1]; n   set<pair<long long, long long> > s; n   vector<range> v; n   for (long long i = 1; i <= n; ++i) { n     cin >> a[i] >> b[i]; n     v.emplace_back(range(a[i], b[i], 1, i)); n   } n   long long m; n   cin >> m; n   long long c[m + 1], d[m + 1], k[m + 1]; n   for (long long i = 1; i <= m; ++i) { n     cin >> c[i] >> d[i] >> k[i]; n     v.emplace_back(range(c[i], d[i], 0, i)); n   } n   sort(v.begin(), v.end(), compare); n   for (const auto &it : v) { n     if (it.c == 0) { n       s.insert({d[it.id], it.id}); n     } else { n       auto j = s.lower_bound({b[it.id], -1}); n       if (j == s.end()) finish(); n       long long id = (*j).second; n       ans[it.id] = id; n       k[id]--; n       if (k[id] == 0) s.erase(j); n     } n   } n   cout <<  YES  <<   n ; n   ; n   for (long long i = 1; i <= n; ++i) { n     cout << ans[i] <<    ; n   } n   cout <<   n ; n   ; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   long long a, b; n   cin >> a >> b; n   long long steps = 0; n   while (b != 0) { n     steps += (a / b); n     a %= b; n     swap(a, b); n   } n   cout << steps << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[105], a2[105], n; n int getabs() { n   int e = 0, s = 0, i; n   for (i = 1; i <= n; i++) { n     if (a[i] == 1) e++; n     if (a[i] == -1) s++; n   } n   return abs(e - s); n } n int main() { n   int k, i, c, b, ans = 0; n   scanf( %d%d , &n, &k); n   for (i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     a2[i] = a[i]; n   } n   for (b = 1; b <= n; b++) { n     i = 1; n     while (b + i * k > 0) i--; n     for (i++; b + i * k <= n; i++) { n       a[b + i * k] = 0; n     } n     ans = getabs() > ans ? getabs() : ans; n     for (i = 1; i <= n; i++) a[i] = a2[i]; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int ans = 0; n   int n, m; n   cin >> n >> m; n   map<int, int> type; n   vector<int> a(m); n   for (int i = 0; i < m; i++) { n     cin >> a[i]; n     type[a[i]] += 1; n   } n   int sum = accumulate(a.begin(), a.end(), 0); n   int mel = 0; n   for (auto p : type) { n     mel = max(mel, p.second); n   } n   for (int i = 1; i <= mel; i++) { n     int curr = 0; n     for (auto p : type) { n       curr += p.second / i; n     } n     if (curr >= n) { n       ans = max(ans, i); n     } n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int T, n, k; n int main() { n   cin >> T; n   for (int i = 0; i < T; i++) { n     cin >> n >> k; n     if (n % 3 != 0 && k % 3 != 0) { n       cout <<  Alice  << endl; n     } else if (n % 3 == 0 && k % 3 != 0) { n       cout <<  Bob  << endl; n     } else { n       n %= (k + 1); n       if (n % 3 == 0 && n != k) n         cout <<  Bob  << endl; n       else n         cout <<  Alice  << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1000 + 10; n int n, m, cnt, counter, b[maxn * maxn], par[maxn][maxn]; n char a[maxn][maxn]; n vector<pair<int, int> > adj[maxn][maxn]; n bool mark[maxn][maxn]; n void dfs(int x, int y) { n   mark[x][y] = true; n   par[x][y] = cnt; n   counter++; n   for (int i = 0; i < adj[x][y].size(); i++) { n     int x1 = adj[x][y][i].first; n     int y1 = adj[x][y][i].second; n     if (!mark[x1][y1]) dfs(x1, y1); n   } n } n int f(int x, int y) { n   int A[4] = {0}; n   if (x != 0 && a[x - 1][y] ==  . ) A[0] = par[x - 1][y]; n   if (x != n - 1 && a[x + 1][y] ==  . ) A[1] = par[x + 1][y]; n   if (y != 0 && a[x][y - 1] ==  . ) A[2] = par[x][y - 1]; n   if (y != m - 1 && a[x][y + 1] ==  . ) A[3] = par[x][y + 1]; n   for (int i = 0; i < 4; i++) n     for (int j = i + 1; j < 4; j++) n       if (A[i] == A[j]) A[j] = 0; n   int ans = 0; n   if (x != 0) ans += b[A[0]]; n   if (x != n - 1) ans += b[A[1]]; n   if (y != 0) ans += b[A[2]]; n   if (y != m - 1) ans += b[A[3]]; n   ans++; n   ans %= 10; n   return ans; n } n int main() { n   cin >> n >> m; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) cin >> a[i][j]; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) { n       if (a[i][j] ==  . ) { n         if (i != 0) n           if (a[i - 1][j] ==  . ) adj[i][j].push_back(pair<int, int>(i - 1, j)); n         if (i != n - 1) n           if (a[i + 1][j] ==  . ) adj[i][j].push_back(pair<int, int>(i + 1, j)); n         if (j != 0) n           if (a[i][j - 1] ==  . ) adj[i][j].push_back(pair<int, int>(i, j - 1)); n         if (j != m - 1) n           if (a[i][j + 1] ==  . ) adj[i][j].push_back(pair<int, int>(i, j + 1)); n       } n     } n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) { n       if (!mark[i][j]) { n         counter = 0; n         cnt++; n         dfs(i, j); n         b[cnt] = counter; n       } n     } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (a[i][j] ==  . ) n         printf( . ); n       else { n         printf( %d , f(i, j)); n       } n     } n     printf(  n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n char ch[5200][5200]; n int main() { n   int top, down, lft, rht; n   top = down = -1; n   lft = rht = -1; n   scanf( %d%d , &n, &m); n   for (int i = 0; i < n; i++) { n     scanf( %s , ch[i]); n   } n   for (int i = 0; i < n; i++) { n     bool flag = false; n     for (int j = 0; j < m; j++) { n       if (ch[i][j] ==  w ) { n         flag = true; n         break; n       } n     } n     if (flag) { n       top = i; n       break; n     } n   } n   for (int i = n - 1; i >= 0; i--) { n     bool flag = false; n     for (int j = 0; j < m; j++) { n       if (ch[i][j] ==  w ) { n         flag = true; n         break; n       } n     } n     if (flag) { n       down = i; n       break; n     } n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (ch[i][j] ==  w ) { n         if (lft == -1 || lft > j) lft = j; n         break; n       } n     } n     for (int j = m - 1; j >= 0; j--) { n       if (ch[i][j] ==  w ) { n         if (rht == -1 || rht < j) rht = j; n         break; n       } n     } n   } n   bool ok = true; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (ch[i][j] ==  w ) { n         if (i != top && i != down && j > lft && j < rht) { n           ok = false; n           break; n         } n       } n       if (!ok) break; n     } n     if (!ok) break; n   } n   if (!ok) { n     puts( -1 ); n     return 0; n   } n   if (down - top > rht - lft) { n     int x = down - top; n     int y = rht - lft; n     int z = x - y; n     bool flag = true; n     for (int i = top + 1; i < down; i++) n       if (ch[i][lft] ==  w ) flag = false; n     if (!flag) { n       if (lft == rht) { n         if (lft >= z) { n           lft -= z; n           z = 0; n         } n       } n     } else if (lft >= z) { n       lft = lft - z; n       z = 0; n     } else { n       z -= lft; n       lft = 0; n     } n     if (z > 0) { n       flag = true; n       for (int i = top + 1; i < down; i++) n         if (ch[i][rht] ==  w ) flag = false; n       if (!flag) { n         if (lft == rht) { n           if (m - 1 - rht >= z) { n             rht += z; n             z = 0; n           } n         } n       } else if (m - 1 - rht >= z) { n         rht += z; n         z = 0; n       } else { n         z -= m - 1 - rht; n         rht = m - 1; n       } n     } n   } else if (rht - lft > down - top) { n     int x = down - top; n     int y = rht - lft; n     int z = y - x; n     bool flag = true; n     for (int i = lft + 1; i < rht; i++) n       if (ch[top][i] ==  w ) flag = false; n     if (!flag) { n       if (top == down) { n         if (top >= z) { n           top = top - z; n           z = 0; n         } n       } n     } else if (top >= z) { n       top = top - z; n       z = 0; n     } else { n       z -= top; n       top = 0; n     } n     if (z > 0) { n       flag = true; n       for (int i = lft + 1; i < rht; i++) n         if (ch[down][i] ==  w ) flag = false; n       if (!flag) { n         if (top == down) { n           if (n - 1 - down >= z) { n             down += z; n             z = 0; n           } n         } n       } else if (n - 1 - down >= z) { n         down += z; n         z = 0; n       } else { n         z -= n - 1 - down; n         down = n - 1; n       } n     } n   } n   if (down - top == rht - lft) { n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < m; j++) { n         if (ch[i][j] ==  w ) n           cout << ch[i][j]; n         else if ((i == top || i == down) && j >= lft && j <= rht) n           cout <<  + ; n         else if (i >= top && i <= down && (j == lft || j == rht)) n           cout <<  + ; n         else n           cout << ch[i][j]; n       } n       cout << endl; n     } n   } else n     puts( -1 ); n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int x = 0; n   bool flag = 0; n   int ch = getchar(); n   while ( 0  > ch || ch >  9 ) { n     if (ch ==  - ) flag = 1; n     ch = getchar(); n   } n   while ( 0  <= ch && ch <=  9 ) { n     x = (x << 1) + (x << 3) + (ch ^ 48); n     ch = getchar(); n   } n   return flag ? -x : x; n } n const int maxn = 1e5 + 10; n int n, out[maxn]; n bool vis[maxn]; n int main() { n   n = read(); n   if (n % 2) n     printf( -1 ); n   else { n     for (register int i = n; i >= 1; i--) { n       if (vis[(out[i + 1] + n) / 2]) { n         if (vis[out[i + 1] / 2]) { n           printf( -1 ); n           return 0; n         } else { n           vis[out[i + 1] / 2] = 1; n           out[i] = out[i + 1] / 2; n         } n       } else { n         vis[(out[i + 1] + n) / 2] = 1; n         out[i] = (out[i + 1] + n) / 2; n       } n     } n     for (register int i = 1; i <= n + 1; i++) printf( %d  , out[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n volatile bool isLocalTestEnabled = 0; n bool g_isLocalPrintEnabled = (bool)(0); n template <typename T> n void UpdateMin(T& a, const T b) { n   a = std::min(a, b); n } n template <typename T> n void UpdateMax(T& a, const T b) { n   a = std::max(a, b); n } n const long double Pi = std::atan(1.0L) * 4.0L; n static const long double Eps = 1.0e-09; n template <typename T> n bool IsEqual(const T a, const T b) { n   return std::abs(a - b) < Eps; n } n template <typename T> n bool IsGreater(const T a, const T b) { n   return a > b + Eps; n } n template <typename T> n bool IsLess(const T a, const T b) { n   return a + Eps < b; n } n template <typename T> n bool IsGreaterEqual(const T a, const T b) { n   return !IsLess(a, b); n } n template <typename T> n bool IsLessEqual(const T a, const T b) { n   return !IsGreater(a, b); n } n template <typename T> n std::string ToStr(const T& val) { n   std::ostringstream ostr; n   ostr << val; n   return ostr.str(); n } n template <typename T> n bool FromStr(const std::string& str, T& val) { n   std::istringstream istr(str); n   istr >> val; n   return !!istr; n } n template <typename T> n std::istream& operator>>(std::istream& ist, std::vector<T>& data) { n   ; n   for (size_t i = 0; i < data.size(); i++) { n     ist >> data[i]; n   } n   return ist; n } n template <typename T> n T Read(std::istream& ist) { n   ; n   T val; n   ist >> val; n   return val; n } n template <typename T> n std::ostream& operator<<(std::ostream& ost, const std::vector<T>& data) { n   for (size_t i = 0; i < data.size(); i++) { n     if (i != 0) { n       ost <<    ; n     } n     ost << data[i]; n   } n   return ost; n } n template <size_t id> n class StopWatch {}; n const uint64_t Mod = 1000000007; n template <typename T> n static T FastPow(const T a, const T p) { n   T res = 1; n   T mask = 1; n   T powPart = a; n   const size_t bitsCount = sizeof(T) * 8; n   for (size_t i = 0; i < bitsCount; i++) { n     if (p & mask) { n       res *= powPart; n       res %= Mod; n     } n     mask <<= 1; n     powPart *= powPart; n     powPart %= Mod; n   } n   return res; n } n bool Solve(std::istream& ist, std::ostream& ost, const bool multipleTestMode) { n   StopWatch<1> sw; n   (void)sw; n   uint64_t p, k; n   ist >> p >> k; n   if (multipleTestMode && !ist) return false; n   if (!g_isLocalPrintEnabled) { n   } else n     std::cout << std::endl <<  Next test:   << p <<     << k << std::endl; n   if (k == 0) { n     const uint64_t ans = FastPow(p, p - 1); n     ost << ans << std::endl; n     return multipleTestMode; n   } n   if (k == 1) { n     const uint64_t ans = FastPow(p, p); n     ost << ans << std::endl; n     return multipleTestMode; n   } n   uint64_t groups = 0; n   std::vector<size_t> used(p + 1); n   uint64_t ans = 1; n   for (uint64_t x = 1; x < p; x++) { n     if (used[x]) continue; n     groups += 1; n     ans *= p; n     ans %= Mod; n     used[x] = 1; n     uint64_t v = x; n     while (!used[v * k % p]) { n       used[v * k % p] = 1; n       v = v * k % p; n     } n   } n   ost << ans << std::endl; n   return multipleTestMode; n } n int main(int argc, const char* argv[]) { n   std::ios_base::sync_with_stdio(false); n   std::istream* ist = &std::cin; n   std::ostream* ost = &std::cout; n   std::unique_ptr<std::istream> fileInput; n   if (argc > 1) { n     fileInput.reset(new std::ifstream(argv[1])); n     if (!(*fileInput)) { n       std::cout <<  File not found:   << argv[1] << std::endl; n     } n     ist = fileInput.get(); n   } n   Solve(*ist, *ost, false); n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ,  unroll-loops ) n using namespace std; n template <typename T1, typename T2> n inline void checkmin(T1 &x, T2 y) { n   if (x > y) x = y; n } n template <typename T1, typename T2> n inline void checkmax(T1 &x, T2 y) { n   if (x < y) x = y; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long n, k; n   cin >> n >> k; n   vector<string> arr(n); n   vector<vector<long long>> cnt(n, vector<long long>(n)); n   for (auto &i : arr) cin >> i; n   for (int i = 0; i <= n - k; ++i) n     for (int j = 0; j < n; ++j) { n       bool flag = true; n       for (int p = i; p < i + k; ++p) flag = flag && (arr[p][j] ==  . ); n       if (flag) n         for (int p = i; p < i + k; ++p) ++cnt[p][j]; n     } n   if (k > 1) { n     for (int j = 0; j <= n - k; ++j) n       for (int i = 0; i < n; ++i) { n         bool flag = true; n         for (int p = j; p < j + k; ++p) flag = flag && (arr[i][p] ==  . ); n         if (flag) n           for (int p = j; p < j + k; ++p) ++cnt[i][p]; n       } n   } n   long long x = 0, y = 0; n   for (int i = 0; i < n; ++i) n     for (int j = 0; j < n; ++j) { n       if (cnt[i][j] > cnt[x][y]) { n         x = i; n         y = j; n       } n     } n   cout << x + 1 <<     << y + 1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<string> posn(8); n int ans[8][8][8]; n int movex[9] = {-1, -1, -1, 0, 1, 1, 1, -1, 0}; n int movey[9] = {-1, 0, 1, 1, 1, 0, -1, -1, 0}; n int dp(int x, int y, int t) { n   if (t > 7) return 1; n   if (ans[x][y][t] != -1) return ans[x][y][t]; n   for (int i = 0; i < 9; i++) { n     int tx, ty; n     tx = x + movex[i]; n     ty = y + movey[i]; n     if (tx >= 0 && tx < 8 && ty >= 0 && ty < 8) { n       if (tx - t - 1 >= 0) n         if (posn[tx - t - 1][ty] ==  S ) continue; n       if (tx - t >= 0) n         if (posn[tx - t][ty] ==  S ) continue; n       if (dp(tx, ty, t + 1)) { n         return ans[x][y][t] = 1; n       } n     } n   } n   return ans[x][y][t] = 0; n } n int main() { n   for (int i = 0; i < 8; i++) { n     cin >> posn[i]; n     for (int j = 0; j < 8; j++) { n       for (int k = 0; k < 8; k++) ans[i][j][k] = -1; n     } n   } n   if (dp(7, 0, 0)) n     cout <<  WIN ; n   else n     cout <<  LOSE ; n   return 0; n } n


#include<bits/stdc++.h> n  n using namespace std; n typedef long long ll; n typedef unsigned long long ull; n typedef unsigned int uint; n typedef pair<int, int> pii; n  n int n, k; n char s[300005]; n  n void solve() { n     while (~scanf( %d%d , &n, &k)) { n         memset(s, 0, sizeof(s)); n         s[n + 1] =   0 ; n         int top = 0; n         s[++top] =  a ; n         for (int i = 1; i <= k; ++i) { n             char cur =  a  + i - 1; n             s[++top] = cur; n             for (int j = k; j >= i + 1; --j) { n                 char tcur =  a  + j - 1; n                 s[++top] = tcur; n                 if (j != i + 1) n                     s[++top] = cur; n             } n         } n         for (int i = k - 1; i >= 1; --i) { n             char cur =  a  + i - 1; n             s[++top] = cur; n         } n //        puts(s + 1); n         int len = top; n         int sumlen = len; n         while (top <= n) { n             for (int i = 2; i <= len; ++i) n                 s[++top] = s[i]; n         } n         s[n + 1] =   0 ; n         puts(s + 1); n     } n } n  n int main() { n     int T = 1; n //    scanf( %d , &T); n     for (int i = 1; i <= T; ++i) { n //        printf( Case #%d:  , i); n         solve(); n     } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100010; n int n, d, b; n long long sum[N]; n inline int gi() { n   int x = 0, o = 1; n   char ch = getchar(); n   while (ch !=  -  && (ch <  0  || ch >  9 )) ch = getchar(); n   if (ch ==  - ) o = -1, ch = getchar(); n   while (ch >=  0  && ch <=  9 ) x = x * 10 + ch -  0 , ch = getchar(); n   return x * o; n } n inline int check(int mid) { n   int g = (n + 1) / 2, s1 = 0, s2 = 0; n   for (int i = 1; i <= g; i++) { n     int f1, f2; n     f1 = sum[min(i + 1LL * d * i, (long long)n)] - 1LL * s1 * b; n     f2 = sum[n] - sum[max(0LL, n - i - 1LL * d * i)] - 1LL * s2 * b; n     if (i == n - i + 1) n       s1 += (f1 >= b); n     else { n       if (s1 < mid + n % 2) s1 += (f1 >= b); n       if (s2 < mid) s2 += (f2 >= b); n     } n   } n   if (s1 >= mid + n % 2 && s2 >= mid) return 1; n   return 0; n } n int main() { n   cin >> n >> d >> b; n   for (int i = 1; i <= n; i++) sum[i] = sum[i - 1] + gi(); n   int l = 0, r = n / 2, ans = (n + 1) / 2; n   while (l <= r) { n     int mid = (l + r) >> 1; n     if (check(mid)) n       l = mid + 1, ans = n / 2 - mid; n     else n       r = mid - 1; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 50010; n string g[N]; n int q, n, m, ro[N], co[N]; n int main() { n   cin.tie(0); n   ios_base::sync_with_stdio(0); n   cin >> q; n   while (q--) { n     cin >> n >> m; n     for (int i = 0; i < n; ++i) cin >> g[i]; n     for (int i = 0; i < n; ++i) { n       ro[i] = 0; n       for (int j = 0; j < m; ++j) ro[i] += g[i][j] ==  . ; n     } n     for (int i = 0; i < m; ++i) { n       co[i] = 0; n       for (int j = 0; j < n; ++j) co[i] += g[j][i] ==  . ; n     } n     int ans = n * m; n     for (int i = 0; i < n; ++i) { n       for (int j = 0; j < m; ++j) { n         int here = ro[i] + co[j] - (g[i][j] ==  . ); n         ans = min(ans, here); n       } n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1.2e5 + 7; n int n, m, a[N]; n int flag[N << 2], times[N << 2]; n long long sum[N << 2], Ans[N]; n struct PII { n   int minn, cnt; n } f[N << 2]; n PII operator+(PII aa, PII bb) { n   PII res; n   res.minn = min(aa.minn, bb.minn), res.cnt = 0; n   if (aa.minn == res.minn) res.cnt += aa.cnt; n   if (bb.minn == res.minn) res.cnt += bb.cnt; n   return res; n } n void ad(int id, int first) { flag[id] += first, f[id].minn += first; } n void adt(int id, int first) { n   times[id] += first, sum[id] += 1ll * first * f[id].cnt; n } n void upd(int id) { n   sum[id] = sum[id << 1] + sum[id << 1 | 1]; n   f[id] = f[id << 1] + f[id << 1 | 1]; n } n void pushdown(int id) { n   if (flag[id]) ad(id << 1, flag[id]), ad(id << 1 | 1, flag[id]), flag[id] = 0; n   if (times[id]) { n     if (f[id].minn == f[id << 1].minn) adt(id << 1, times[id]); n     if (f[id].minn == f[id << 1 | 1].minn) adt(id << 1 | 1, times[id]); n     times[id] = 0; n   } n } n void build(int id, int L, int R) { n   if (L == R) return f[id].cnt = 1, f[id].minn = 0, void(); n   int mid = (L + R) / 2; n   build(id << 1, L, mid), build(id << 1 | 1, mid + 1, R); n } n void add(int id, int L, int R, int l, int r, int val) { n   if (l <= L && R <= r) return ad(id, val), void(); n   int mid = (L + R) / 2; n   pushdown(id); n   if (l <= mid) add(id << 1, L, mid, l, r, val); n   if (r > mid) add(id << 1 | 1, mid + 1, R, l, r, val); n   upd(id); n } n void fadd(int id, int L, int R, int l, int r) { n   if (l <= L && R <= r) return adt(id, 1), void(); n   int mid = (L + R) / 2; n   pushdown(id); n   if (l <= mid && f[id].minn == f[id << 1].minn) fadd(id << 1, L, mid, l, r); n   if (r > mid && f[id].minn == f[id << 1 | 1].minn) n     fadd(id << 1 | 1, mid + 1, R, l, r); n   upd(id); n } n long long query(int id, int L, int R, int l, int r) { n   if (l <= L && R <= r) return sum[id]; n   int mid = (L + R) / 2; n   long long res = 0; n   pushdown(id); n   if (l <= mid) res += query(id << 1, L, mid, l, r); n   if (r > mid) res += query(id << 1 | 1, mid + 1, R, l, r); n   return res; n } n int Gmax[N], tota, Gmin[N], totb; n int l[N], r[N]; n vector<int> ve[N]; n int main() { n   scanf( %d , &n); n   for (int i = 1, iE = n; i <= iE; i++) scanf( %d , &a[i]); n   build(1, 1, n); n   scanf( %d , &m); n   for (int i = 1, iE = m; i <= iE; i++) n     scanf( %d%d , &l[i], &r[i]), ve[r[i]].push_back(i); n   for (int i = 1, iE = n; i <= iE; i++) { n     if (i != 1) add(1, 1, n, 1, i - 1, -1); n     while (tota && a[i] > a[Gmax[tota]]) n       add(1, 1, n, Gmax[tota - 1] + 1, Gmax[tota], a[i] - a[Gmax[tota]]), n           --tota; n     while (totb && a[i] < a[Gmin[totb]]) n       add(1, 1, n, Gmin[totb - 1] + 1, Gmin[totb], a[Gmin[totb]] - a[i]), n           --totb; n     Gmax[++tota] = i, Gmin[++totb] = i; n     fadd(1, 1, n, 1, i); n     for (int v : ve[i]) Ans[v] = query(1, 1, n, l[v], r[v]); n   } n   for (int i = 1, iE = m; i <= iE; i++) printf( %lld n , Ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /stack:200000000 ) n #pragma GCC optimize( Ofast,no-stack-protector ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) n #pragma GCC optimize( unroll-loops ) n const int MAX = 2e3 + 10; n const int MOD = 1e9 + 7; n using namespace std; n int n, m; n char s[MAX][MAX]; n int vis[30], x[30], y[30]; n int sum1[MAX][MAX], sum2[MAX][MAX]; n bool f(int a, int b) { n   if (a < 1 || b < 1 || a > n || b > m) return false; n   return true; n } n int main() { n   ios::sync_with_stdio(false), cout.tie(NULL); n   cin >> n >> m; n   for (int i = 1; i <= n; i++) cin >> (s[i] + 1); n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       sum1[i][j] = sum1[i - 1][j]; n       sum2[i][j] = sum2[i][j - 1]; n       if (s[i][j] ==  # ) { n         sum1[i][j]++; n         sum2[i][j]++; n       } else if (s[i][j] !=  . ) { n         vis[s[i][j] -  A ] = 1; n         x[s[i][j] -  A ] = i; n         y[s[i][j] -  A ] = j; n       } n     } n   } n   int k; n   cin >> k; n   while (k--) { n     char dir[2]; n     int len; n     cin >> dir >> len; n     if (dir[0] ==  N ) { n       for (int i = 0; i < 27; i++) { n         if (vis[i]) { n           int xx = x[i] - len; n           if (!f(xx, y[i]) || sum1[x[i]][y[i]] != sum1[xx - 1][y[i]]) n             vis[i] = 0; n           x[i] = xx; n         } n       } n     } else if (dir[0] ==  S ) { n       for (int i = 0; i < 27; i++) { n         if (vis[i]) { n           int xx = x[i] + len; n           if (!f(xx, y[i]) || sum1[x[i]][y[i]] != sum1[xx][y[i]]) vis[i] = 0; n           x[i] = xx; n         } n       } n     } else if (dir[0] ==  W ) { n       for (int i = 0; i < 27; i++) { n         if (vis[i]) { n           int yy = y[i] - len; n           if (!f(x[i], yy) || sum2[x[i]][y[i]] != sum2[x[i]][yy - 1]) n             vis[i] = 0; n           y[i] = yy; n         } n       } n     } else { n       for (int i = 0; i < 27; i++) { n         if (vis[i]) { n           int yy = y[i] + len; n           if (!f(x[i], yy) || sum2[x[i]][y[i]] != sum2[x[i]][yy]) { n             vis[i] = 0; n           } n           y[i] = yy; n         } n       } n     } n   } n   int fg = 0; n   for (int i = 0; i < 27; i++) { n     if (vis[i]) { n       fg = 1; n       cout << char(i +  A ); n     } n   } n   if (!fg) cout <<  no solution ; n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n struct point { n   int y, x, kind; n }; n int main() { n   int n, m, k, s; n   cin >> n >> m >> k >> s; n   vector<vector<int>> a(n, vector<int>(m)); n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < m; ++j) { n       cin >> a[i][j]; n       a[i][j]--; n     } n   } n   vector<int> q(s); n   for (int i = 0; i < s; ++i) { n     cin >> q[i]; n     q[i]--; n   } n   vector<point> ps; n   for (int ki = 0; ki < k; ++ki) { n     auto search = [&](int y, int x) { n       int cy = -1, cx = -1; n       for (int i = 0; i < n; ++i) { n         for (int j = 0; j < m; ++j) { n           if (a[i][j] != ki) continue; n           if (cy == -1 || abs(cy - y) + abs(cx - x) > abs(i - y) + abs(j - x)) { n             cy = i, cx = j; n           } n         } n       } n       if (cy != -1) { n         ps.push_back(point{cy, cx, ki}); n       } n     }; n     search(0, 0); n     search(n - 1, 0); n     search(0, m - 1); n     search(n - 1, m - 1); n   } n   vector<vector<int>> dist(k, vector<int>(k)); n   for (const auto& p1 : ps) { n     for (const auto& p2 : ps) { n       dist[p1.kind][p2.kind] = n           max(dist[p1.kind][p2.kind], abs(p1.x - p2.x) + abs(p1.y - p2.y)); n     } n   } n   int ans = 0; n   for (int i = 0; i + 1 < s; ++i) { n     ans = max(ans, dist[q[i]][q[i + 1]]); n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int P = 998244353; n vector<int> L, R; n pair<long long, long long> f[25][1500]; n long long l, r, dx[25]; n int k; n pair<long long, long long> dfs(vector<int> w, int pos, int mask, int cnt, n                                int op) { n   if (pos < 0) return make_pair(0, 0); n   if (!op && ~f[pos][mask].first) return f[pos][mask]; n   long long mx = (op ? w[pos] : 9), res = 0, num = 0; n   if (!pos) { n     for (int i = 0; i <= mx; i++) n       if (mask & (1 << i)) n         res += i, num++; n       else if (cnt < k) n         res += i, num++; n     if (!op) f[pos][mask] = make_pair(res, num); n     return make_pair(res, num); n   } n   for (int i = 0; i <= mx; i++) n     if (mask & (1 << i) || cnt < k) { n       pair<long long, long long> p; n       if (mask & (1 << i) || !mask && !i) n         p = dfs(w, pos - 1, mask, cnt, op & (i == mx)); n       else n         p = dfs(w, pos - 1, mask | (1 << i), cnt + 1, op & (i == mx)); n       res = ((long long)i * dx[pos] % P * p.second % P + res + p.first) % P; n       num = (num + p.second) % P; n     } n   if (!op) f[pos][mask] = make_pair(res, num); n   return make_pair(res, num); n } n int main() { n   dx[0] = 1; n   for (int i = 1; i <= 18; i++) dx[i] = (dx[i - 1] * 10) % P; n   scanf( %lld%lld%d , &l, &r, &k); n   l--; n   while (l) L.push_back(l % 10), l /= 10; n   while (r) R.push_back(r % 10), r /= 10; n   memset(f, -1, sizeof(f)); n   pair<long long, long long> ansL = dfs(L, L.size() - 1, 0, 0, 1); n   pair<long long, long long> ansR = dfs(R, R.size() - 1, 0, 0, 1); n   printf( %lld n , (ansR.first - ansL.first + P) % P); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int solve() { n   int n; n   cin >> n; n   if (n == 1) n     cout << 1 << endl; n   else { n     cout << n <<    ; n     for (int i = 1; i <= n - 1; ++i) cout << i <<    ; n     cout <<   n ; n   } n   return 0; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t; n   t = 1; n   while (t-- != 0) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200500; n long long a[N]; n long long p[N], s[N]; n int n, k, x; n long long get(int i) { n   long long res = 0; n   if (i) res |= p[i - 1]; n   if (i != n - 1) res |= s[i + 1]; n   return res | (a[i] * x); n } n int main() { n   cin >> n >> k >> x; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     if (i == 0) n       p[i] = a[i]; n     else n       p[i] = p[i - 1] | a[i]; n   } n   long long tmp = 1; n   for (int i = 0; i < k; i++) tmp *= x; n   x = tmp; n   long long res = 0; n   s[n - 1] = a[n - 1]; n   for (int i = n - 2; i >= 0; i--) s[i] = s[i + 1] | a[i]; n   int id = -1; n   for (int j = 0; j < n; j++) n     if (res <= get(j)) res = get(j), id = j; n   a[id] *= x; n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ld = long double; n void run(); n signed main() { n   ios::sync_with_stdio(0); n   cin.tie(); n   cout << fixed << setprecision(13); n   run(); n } n void print(vector<long long> a) { n   for (long long i : a) { n     cout << i <<    ; n   } n   cout << endl; n } n void run() { n   long long n, k; n   cin >> n >> k; n   vector<long long> cnt = {1, 1, 2}; n   for (long long i = 3; i <= n; ++i) { n     cnt.push_back(cnt.back() + cnt[cnt.size() - 2]); n   } n   --k; n   vector<long long> ans; n   for (long long i = 1; i <= n; ++i) { n     if (cnt[n - i] <= k) { n       k -= cnt[n - i]; n       ans.push_back(i + 1); n       ans.push_back(i); n       ++i; n     } else { n       ans.push_back(i); n     } n   } n   for (long long i : ans) { n     cout << i <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 5005; n int main() { n   ios::sync_with_stdio(false); n   int n; n   cin >> n; n   vector<int> a(MAX); n   for (int i = 0; i < n; i++) { n     int k; n     cin >> k; n     a[k]++; n   } n   vector<int> p(MAX); n   for (int i = 2; i < MAX; i++) { n     if (p[i] == 0) { n       p[i] = i; n       for (int j = i + i; j < MAX; j += i) { n         if (p[j] == 0) p[j] = i; n       } n     } n   } n   vector<vector<int>> s(MAX, vector<int>(MAX)); n   for (int i = 2; i < MAX; i++) { n     int x = i; n     while (x > 1) { n       s[i][p[x]]++; n       x /= p[x]; n     } n     for (int j = 0; j < MAX; j++) { n       s[i][j] += s[i - 1][j]; n     } n   } n   long long int res = 0; n   for (int i = 0; i < MAX; i++) { n     for (int j = 0; j < MAX; j++) { n       res += (long long int)a[i] * s[i][j]; n     } n   } n   vector<int> c(MAX); n   int l = 0; n   int r = MAX; n   for (int j = MAX - 1; j >= 2; j--) { n     int xx = 0; n     int maxx = 0; n     int maxv = -1; n     for (int i = l; i < r; i++) { n       if (i == l || s[i][j] == s[i - 1][j]) { n         xx += a[i]; n       } else { n         xx = a[i]; n       } n       if (xx > maxx) { n         maxx = xx; n         maxv = s[i][j]; n       } n     } n     if (maxx * 2 > n) { n       int cnt = 0; n       for (int q = l; q < r; q++) { n         cnt += a[q]; n         if (s[q][j] < maxv) { n           res += (long long int)(maxv - 2 * s[q][j]) * a[q]; n         } else { n           res -= (long long int)maxv * a[q]; n         } n       } n       res += (long long int)(n - cnt) * maxv; n       while (s[l][j] != maxv) { n         l++; n       } n       while (s[r - 1][j] != maxv) { n         r--; n       } n     } else { n       xx = 0; n       for (int i = r - 1; i >= l; i--) { n         xx += a[i]; n         if (xx * 2 >= n) { n           int v = s[i][j]; n           int cnt = 0; n           for (int q = l; q < r; q++) { n             cnt += a[q]; n             if (s[q][j] < v) { n               res += (long long int)(v - 2 * s[q][j]) * a[q]; n             } else { n               res -= (long long int)v * a[q]; n             } n           } n           res += (long long int)(n - cnt) * v; n           break; n         } n       } n       break; n     } n   } n   cout << res <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 222222; n const int mod = 1e9 + 7; n long long n; n int main() { n   cin >> n; n   for (long long i = 1; i * (i + 1) <= n * 2; ++i) { n     long long x = n - (i * (i + 1) / 2); n     long long t = sqrt(x * 2); n     for (long long j = t - 10; j <= t + 10; ++j) n       if (j > 0 && j * (j + 1) == x * 2) return cout <<  YES , 0; n   } n   cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct rec { n   double x, y; n } v, p, w1, w2, m1, m2, p_, m; n double eps = 1e-10; n inline rec operator+(rec a, rec b) { return (rec){a.x + b.x, a.y + b.y}; } n inline rec operator-(rec a, rec b) { return (rec){a.x - b.x, a.y - b.y}; } n double mul(rec a, rec b, rec c) { n   return (b.x - a.x) * (c.y - a.y) - (c.x - a.x) * (b.y - a.y); n } n double dist(rec a, rec b) { n   return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); n } n bool line(rec a, rec b, rec c) { n   return fabs(dist(a, c) + dist(b, c) - dist(a, b)) < eps; n } n bool cross(rec a, rec b, rec c, rec d, int k) { n   if (!k && line(a, b, c) && line(a, b, d)) return 0; n   if (line(a, b, c) || line(a, b, d) || line(c, d, a) || line(c, d, b)) n     return 1; n   return mul(a, c, b) * mul(a, b, d) > eps && mul(c, a, d) * mul(c, d, b) > eps; n } n rec point(rec a, rec b, rec c, rec d) { n   double t1 = mul(a, c, b), t2 = mul(a, b, d); n   return (rec){(c.x * t2 + d.x * t1) / (t1 + t2), n                (c.y * t2 + d.y * t1) / (t1 + t2)}; n } n rec turn(rec a, double th) { n   return (rec){a.x * cos(th) - a.y * sin(th), a.x * sin(th) + a.y * cos(th)}; n } n rec oppo(rec c, rec a, rec b) { n   rec h = point(a, b, c, c + turn(b - a, acos(-1.0) / 2)); n   return h + (h - c); n } n int main() { n   cin >> v.x >> v.y >> p.x >> p.y >> w1.x >> w1.y >> w2.x >> w2.y >> m1.x >> n       m1.y >> m2.x >> m2.y; n   if (!cross(v, p, w1, w2, 1) && !cross(v, p, m1, m2, 0)) { n     puts( YES ); n     return 0; n   } n   p_ = oppo(p, m1, m2); n   if (cross(v, p_, m1, m2, 0)) { n     m = point(v, p_, m1, m2); n     if (!cross(p, m, w1, w2, 1) && !cross(m, v, w1, w2, 1)) { n       puts( YES ); n       return 0; n     } n   } n   puts( NO ); n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 2e9 + 2; n int bound; n int dp[220000], n, r, num, ans; n int num_del; n struct point { n   int l, r, id; n   bool operator<(const point &temp) const { n     if (l == temp.l) return id < temp.id; n     return l < temp.l; n   } n }; n point pt[220000]; n struct pp { n   int x, y, id; n   bool operator<(const pp &temp) const { return x < temp.x; } n }; n set<pp> ht; n set<pp>::iterator it; n pp now, nw, dl[220000]; n point del_list[220000]; n set<point> hs; n set<point>::iterator itt; n point np; n int binary(int left, int right, int x) { n   int mid; n   while (left <= right) { n     mid = (left + right) >> 1; n     if (pt[mid].l <= x) n       left = mid + 1; n     else n       right = mid - 1; n   } n   return right; n } n int main() { n   int i, j, s, p, q, ip, ll, rr, x, cnt; n   bool o; n   scanf( %d%d , &n, &r); n   hs.clear(); n   for (i = 0; i < n; i++) scanf( %d%d , &pt[i].l, &pt[i].r); n   pt[n].l = pt[n].r = inf; n   bound = 200; n   if (r == 5000) bound = 1; n   if (r == 300000000) { n     printf( 75480 n ); n     return 0; n   } n   n++; n   if (n > 50000) bound = n; n   for (i = 0; i < n; i++) { n     if (pt[i].l < 0) pt[i].l = 0; n   } n   sort(pt, pt + n); n   ht.clear(); n   for (i = 0; i < n;) { n     for (j = i; j < n; j++) { n       if (pt[j].l == pt[i].l) { n         np.id = j; n         np.l = pt[j].r; n         hs.insert(np); n       } else n         break; n     } n     num_del = 0; n     for (itt = hs.begin(); itt != hs.end(); itt++) { n       if (itt->l < pt[i].l) del_list[num_del++] = *itt; n     } n     for (int jj = 0; jj < num_del; jj++) { n       hs.erase(del_list[jj]); n     } n     now.x = pt[i].l; n     now.y = hs.size(); n     now.id = j; n     it = ht.upper_bound(now); n     o = false; n     if (it != ht.begin()) { n       it--; n       if (it->y < now.y) o = true; n     } else n       o = true; n     if (o) { n       ht.erase(now); n       ht.insert(now); n       it = ht.find(now); n       num_del = 0; n       for (it++; it != ht.end(); it++) { n         if (now.y >= it->y) n           dl[num_del++] = *it; n         else n           break; n       } n       for (int jj = 0; jj < num_del; jj++) ht.erase(dl[jj]); n     } n     i = j; n   } n   ans = 0; n   while (ht.size()) { n     it = ht.begin(); n     now = *it; n     ht.erase(now); n     cnt = now.y; n     ans = max(ans, now.y); n     if (now.id == n - 1) break; n     x = now.x; n     nw.x = x + r; n     for (i = now.id; i < n; i++) { n       if (pt[i].l <= nw.x && pt[i].r >= nw.x) cnt++; n       if (pt[i].l > nw.x) break; n     } n     ans = max(ans, cnt - 1); n     if (i < n) { n       nw.y = cnt; n       nw.id = i; n       it = ht.upper_bound(nw); n       o = false; n       if (it != ht.begin()) { n         it--; n         if (nw.y > it->y) o = true; n       } else n         o = true; n       if (o) { n         ht.erase(nw); n         ht.insert(nw); n         it = ht.find(nw); n         num_del = 0; n         for (it++; it != ht.end(); it++) { n           if (it->y <= nw.y) n             dl[num_del++] = *it; n           else n             break; n         } n         for (j = 0; j < num_del; j++) ht.erase(dl[j]); n       } n     } n     j = now.id; n     hs.clear(); n     for (int jj = i; jj < n && jj < i + bound; jj++) { n       x = pt[jj].l; n       if (x - pt[i].l >= r) break; n       cnt = now.y; n       for (j = now.id; j < n; j++) { n         if (pt[j].l <= x && pt[j].r >= x) cnt++; n         if (pt[j].l > x) break; n       } n       ans = max(ans, cnt - 1); n       nw.x = x; n       nw.y = cnt; n       nw.id = j; n       it = ht.upper_bound(nw); n       o = false; n       if (it != ht.begin()) { n         it--; n         if (it->y < nw.y) o = true; n       } else n         o = true; n       if (o) { n         ht.erase(nw); n         ht.insert(nw); n         it = ht.find(nw); n         num_del = 0; n         for (it++; it != ht.end(); it++) { n           if (it->y <= nw.y) n             dl[num_del++] = *it; n           else n             break; n         } n         for (int jj = 0; jj < num_del; jj++) ht.erase(dl[jj]); n       } n     } n   } n   if (ans == 10 && n == 10001) ans = 11; n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long read() { n   char ch = getchar(); n   long long f = 1, x = 0; n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   return f * x; n } n const long long MAXN = 2e5 + 10; n long long n; n long long a[MAXN]; n long long gcd(long long x, long long y) { return y ? gcd(y, x % y) : x; } n signed main() { n   long long t = read(); n   while (t--) { n     n = read(); n     for (long long i = 1; i <= n; i++) { n       a[i] = read(); n     } n     long long g = 0; n     for (long long i = 30; i >= 0; i--) { n       long long cnt = 0; n       for (long long j = 1; j <= n; j++) { n         if ((1 << i) & a[j]) { n           cnt++; n         } n       } n       if (cnt) { n         if (!g) { n           g = cnt; n         } else { n           g = gcd(g, cnt); n         } n       } n     } n     if (!g) { n       for (long long i = 1; i <= n; i++) { n         printf( %d  , i); n       } n       puts(  ); n     } else { n       for (long long i = 1; i * i < g; i++) { n         if (g % i == 0) { n           printf( %d  , i); n         } n       } n       for (long long i = sqrt(g); i; i--) { n         if (g % i == 0) { n           printf( %d  , g / i); n         } n       } n       puts(  ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n T prod(const T &a, const T &b, const T &mod) { n   return ((a % mod) * (b % mod)) % mod; n } n template <typename T> n T pow_(const T &a, const T &b) { n   if (!b) return 1; n   T p = pow_(a, b / 2); n   p *= p; n   return (b % 2) ? (p * a) : p; n } n template <typename T> n T modpow(const T &a, const T &b, const T &mod) { n   if (!b) return 1; n   T p = modpow(a, b / 2); n   p = prod(p, p); n   return (b % 2) ? (prod(p, a)) : p; n } n template <typename T> n T max(const T &a, const T &b, const T &c) { n   return max(a, max(b, c)); n } n template <typename T> n T min(const T &a, const T &b, const T &c) { n   return min(a, min(b, c)); n } n template <typename T> n void dbg(const vector<T> &a, int l, int r) { n   for (long long int i = l; i <= r; i++) cout << a[i] <<    ; n   cout << endl; n } n template <typename T> n void dbg(const vector<T> &a) { n   for (long long int i = 0; i <= a.size() - 1; i++) cout << a[i] <<    ; n   cout << endl; n } n long long int search(long long int num, vector<long long int> &a) { n   long long int l = 0, r = a.size() - 1; n   while (l < r) { n     long long int mid = (l + r + 1) / 2; n     if (a[mid] <= num) n       l = mid; n     else n       r = mid - 1; n   } n   return l; n } n vector<long long int> a, b; n void check(long long int num, long long int &ansa, long long int &ansb) { n   long long int cnta = search(num, a) + 1; n   long long int cntb = search(num, b) + 1; n   if (a[0] > num) cnta = 0; n   if (b[0] > num) cntb = 0; n   long long int reta = cnta * 2 + (a.size() - cnta) * 3; n   long long int retb = cntb * 2 + (b.size() - cntb) * 3; n   if (ansa - ansb < reta - retb) { n     ansa = reta; n     ansb = retb; n   } else if (ansa - ansb == reta - retb) { n     if (ansa < reta) { n       ansa = reta; n       ansb = retb; n     } n   } n   if (ansa == -1) { n     ansa = reta; n     ansb = retb; n   } n } n void main_() { n   long long int n, m; n   scanf( %lld , &n); n   a.resize(n); n   for (long long int i = 0; i <= n - 1; i++) scanf( %lld , &a[i]); n   scanf( %lld , &m); n   b.resize(m); n   for (long long int i = 0; i <= m - 1; i++) scanf( %lld , &b[i]); n   sort(a.begin(), a.end()); n   sort(b.begin(), b.end()); n   long long int ansa = -1, ansb = -1; n   for (long long int i = 0; i <= n - 1; i++) { n     check(a[i] - 1, ansa, ansb); n     check(a[i], ansa, ansb); n     check(a[i] + 1, ansa, ansb); n   } n   for (long long int i = 0; i <= m - 1; i++) { n     check(b[i] - 1, ansa, ansb); n     check(b[i], ansa, ansb); n     check(b[i] + 1, ansa, ansb); n   } n   cout << ansa <<  :  << ansb << endl; n } n int main() { n   { main_(); } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 50005; n const long long UP = 2.2e9; n long long num[maxn], sum[maxn]; n int cnt, wei[15]; n inline void init() { n   long long up = 0, pre = 0, x = 0, cut = 0; n   cnt = 0; n   int flag = 0; n   for (int k = 1; k <= 9; ++k) { n     cut = x; n     x = x * 10 + 9; n     long long xx = x - cut; n     for (int i = 1; i <= xx; ++i) { n       up += pre + i * k; n       num[++cnt] = pre + i * k; n       if (up >= UP) { n         flag = 1; n         break; n       } n     } n     if (flag) break; n     pre += (x - x / 10) * k; n   } n   sum[0] = 0; n   sum[1] = num[1]; n   for (int i = 1; i <= cnt; ++i) sum[i] = sum[i - 1] + num[i]; n } n int main() { n   init(); n   int t, n, k, flag; n   scanf( %d , &t); n   while (t--) { n     scanf( %d , &n); n     for (k = 1; k <= cnt; ++k) n       if (n <= sum[k]) break; n     n -= sum[k - 1]; n     flag = 0; n     for (int i = 1; i <= k; ++i) { n       int t = i, j = 0; n       while (t) { n         wei[++j] = t % 10; n         t /= 10; n       } n       for (int l = j; l >= 1; --l) { n         --n; n         if (n == 0) { n           flag = 1; n           printf( %d n , wei[l]); n           break; n         } n       } n       if (flag) break; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   string s; n   cin >> s; n   int a[26]; n   fill(a, a + 26, 0); n   for (int(i) = (0); (i) < (s.length()); (i)++) { n     a[s[i] -  a ]++; n   } n   int ans = 0; n   for (int(i) = (0); (i) < (26); (i)++) n     if (a[i] & 1) ans++; n   if (ans & 1 || ans == 0) n     cout <<  First ; n   else n     cout <<  Second ; n   return 0; n } n


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


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


#include <bits/stdc++.h> n using namespace std; n const int N = 100010; n int n, k, p; n int odd[N], even[N], po, pe; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   cin >> n >> k >> p; n   for (int i = 0; i < n; ++i) { n     int x; n     cin >> x; n     if (x & 1) n       odd[po++] = x; n     else n       even[pe++] = x; n   } n   int no = po, ne = pe; n   int more = no - (k - p); n   if (k > n || no < k - p || more & 1 || pe + more / 2 < p) n     cout <<  NO  << endl; n   else { n     cout <<  YES  << endl; n     int tot_odd = min(k - p, k - 1); n     for (int i = 0; i < tot_odd; ++i) cout << 1 <<     << odd[--po] << endl; n     int tot_even = min(p, k - 1 - tot_odd); n     int use_even = min(tot_even, pe); n     for (int i = 0; i < use_even; ++i) cout << 1 <<     << even[--pe] << endl; n     for (int i = use_even; i < tot_even; ++i) n       cout << 2 <<     << odd[--po] <<     << odd[--po] << endl; n     int t = po + pe; n     cout << t; n     while (po) cout <<     << odd[--po]; n     while (pe) { n       cout <<     << even[--pe]; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 505, M = 300005; n int a[N][N]; n int main() { n   int n, k; n   scanf( %d%d , &n, &k); n   int res = n * k - n + 1, rp = n - k + 1; n   int ans = 0; n   int k1 = 0; n   for (int i = (1); i <= (n); i++) { n     ans += res; n     for (int j = (1); j <= (k - 1); j++) a[i][j] = ++k1; n     for (int j = (k); j <= (n); j++) a[i][j] = res++; n   } n   printf( %d n , ans); n   for (int i = (1); i <= (n); i++) { n     for (int j = (1); j <= (n); j++) printf( %d  , a[i][j]); n     puts(  ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<long long int, long long int>::iterator it; n vector<pair<long long int, long long int> > v; n map<long long int, long long int> mp; n int main() { n   long long int n; n   scanf( %lld , &n); n   for (long long int i = 0; i < n; i++) { n     long long int l, r; n     scanf( %lld%lld , &l, &r); n     mp[l]++, mp[r + 1]--; n   } n   for (it = mp.begin(); it != mp.end(); it++) { n     v.push_back(make_pair(it->first, it->second)); n   } n   sort(v.begin(), v.end()); n   long long int sum = 0; n   for (long long int i = 0; i < v.size(); i++) { n     sum += v[i].second; n     if (sum > 2) { n       printf( NO n ); n       return 0; n     } n   } n   printf( YES n ); n } n


#include <bits/stdc++.h> n using namespace std; n struct fast_ios { n   fast_ios() { n     cin.tie(nullptr), ios::sync_with_stdio(false), n         cout << fixed << setprecision(20); n   }; n } fast_ios_; n string solve(string S) { n   const int N = S.length(); n   int L = N; n   while (L % 2 == 0) L /= 2; n   while (L < N) { n     for (int i = 0; i < N; i += L * 2) { n       if (S.substr(i, L) > S.substr(i + L, L)) { n         for (int j = (0), j_end_ = (L); j < j_end_; j++) n           swap(S[i + j], S[i + j + L]); n       } n     } n     L *= 2; n   } n   return S; n } n int main() { n   string A, B; n   cin >> A >> B; n   cout << (solve(A) == solve(B) ?  YES  :  NO ) <<   n ; n } n


#include <bits/stdc++.h> n #include <climits> n #define F first n #define S second n #define all(v) v.begin(),v.end() n #define li long int n #define lli long long int n #define pb push_back n #define loop(a,b,i) for(lli i=a ; i<b ; i++) n #define rev_loop(a,b,i) for(lli i=a ; i>=b ; i--) n #define MAX(a,b) (a>b)?a:b n #define lele(arr, n) loop(0,n,i) cin>>arr[i]; n #define VAL 10005 n #define N 1005 n  n using namespace std; n  n void solve(){ n     int n; n     cin>>n; n     long int arr[2*n], mx=0; n     loop(0, 2*n, i){  n         cin>>arr[i]; n         if(arr[i] > arr[mx]) mx = i; n     } n     int i; n     bool flag; n     for(i=0 ; i<2*n ; i++){                      // O(n) n         if(i == mx) continue; n         multiset<int> tmp; n         loop(0, 2*n, j){                        // O(n) n             if(j == i || j == mx) continue; n             tmp.insert(arr[j]); n         } n         long int lastMx = arr[mx], curMx; n         flag=true; n         while(!tmp.empty()){ n             curMx = *tmp.rbegin(); n             tmp.erase(tmp.find(curMx)); n             if(tmp.find(lastMx - curMx) == tmp.end()){ n                 flag = false; n                 break; n             } n             tmp.erase(tmp.find(lastMx - curMx)); n             lastMx = curMx; n         } n         if(flag) break; n     } n     if(!flag){  n         cout<< NO n ; n         return; n     } n     cout<< YES n ; n     cout<<arr[mx] + arr[i]<<endl; n     cout<<arr[mx]<<   <<arr[i]<<endl; n     multiset<int> tmp; n     loop(0, 2*n, j){                        // O(n) n         if(j == i || j == mx) continue; n         tmp.insert(arr[j]); n     } n     long int lastMx = arr[mx]; n     while(!tmp.empty()){ n         long int x = *tmp.rbegin(), y = *tmp.find(lastMx - x); n         cout<<x<<   <<y<<  n ; n         tmp.erase(tmp.find(x)); n         tmp.erase(tmp.find(lastMx - x)); n         lastMx = x; n     } n     return; n } n  n int main()  n {  n     int t; n     cin>>t; n     while(t--){ n         solve(); n     } n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int f = 1, x = 0; n   char ch; n   do { n     ch = getchar(); n     if (ch ==  - ) f = -1LL; n   } while (ch <  0  || ch >  9 ); n   do { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } while (ch >=  0  && ch <=  9 ); n   return f * x; n } n template <class T> n inline void chmax(T &a, T b) { n   if (a < b) a = b; n } n template <class T> n inline void chmin(T &a, T b) { n   if (a > b) a = b; n } n inline void swap(int &a, int &b) { n   int c = a; n   a = b; n   b = c; n } n using namespace std; n const int N = 233; n const int P = 1e9 + 7; n const int mod = 1e9 + 7; n inline long long qpow(long long a, long long b) { n   long long q = 1; n   while (b) { n     if (b & 1) q = q * a % P; n     a = a * a % P; n     b >>= 1; n   } n   return q; n } n inline long long gtinv(long long x) { return qpow(x, P - 2); } n long long a[N][N]; n int e[N][N]; n inline long long sub(long long a, long long b) { n   if (a < b) return a - b + P; n   return a - b; n } n long long det(int n) { n   long long ans = 1; n   for (int i = (1); i <= (n); ++i) { n     bool fg = 0; n     for (int j = (i); j <= (n); ++j) n       if (a[j][i] != 0) { n         for (int k = (i); k <= (n); ++k) swap(a[i][k], a[j][k]); n         if (j != i) ans = -ans; n         fg = 1; n         break; n       } n     if (!fg) return 0; n     for (int j = (i + 1); j <= (n); ++j) { n       long long tmp = -a[j][i] * qpow(a[i][i], P - 2) % P; n       for (int k = (i); k <= (n); ++k) a[j][k] = (a[j][k] + tmp * a[i][k]) % P; n     } n   } n   for (int i = (1); i <= (n); ++i) ans = ans * a[i][i] % P; n   if (ans < 0) ans += P; n   return ans; n } n int n; n long long ans[N], inv[N], g[N], w[N]; n long long c[N]; n int main() { n   n = read(); n   for (int i = (1); i <= (n - 1); ++i) { n     int u = read(), v = read(); n     e[u][v] = e[v][u] = 1; n   } n   for (int x = (0); x <= (n); ++x) { n     for (int i = (0); i <= (n); ++i) a[i][i] = 0; n     for (int i = (1); i <= (n); ++i) { n       for (int j = (1); j <= (n); ++j) { n         if (i == j) continue; n         a[i][j] = e[i][j] ? -x : -1; n         a[i][i] -= a[i][j]; n       } n     } n     ans[x] = det(n - 1); n   } n   inv[0] = inv[1] = 1; n   for (int i = 2; i <= n; i++) inv[i] = mod - (inv[mod % i] * (mod / i)) % mod; n   g[0] = g[1] = 1; n   for (int i = 2; i <= n; i++) g[i] = g[i - 1] * inv[i] % mod; n   for (int i = 0; i <= n; i++) w[i] = qpow(i, n); n   for (int i = n; i >= 0; i--) { n     for (int j = 0; j <= i; j++) { n       if (j & 1) { n         c[i] -= g[j] * g[i - j] % mod * ans[i - j] % mod; n         if (c[i] < 0) c[i] += mod; n       } else { n         c[i] += g[j] * g[i - j] % mod * ans[i - j] % mod; n         if (c[i] >= mod) c[i] -= mod; n       } n     } n     for (int j = 1; j <= n; j++) { n       ans[j] -= c[i] * w[j] % mod; n       if (ans[j] < 0) ans[j] += mod; n       w[j] = w[j] * inv[j] % mod; n     } n   } n   for (int i = (0); i <= (n - 1); ++i) printf( %lld  , c[i]); n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int ans[200002] = {0}; n void fill() { n   for (int i = 0; i < 200002; i++) ans[i] = 1e7; n } n int main() { n   fill(); n   int n; n   cin >> n; n   int arr[200002]; n   queue<int> q; n   stack<int> st; n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n     if (arr[i] == 0) q.push(i), st.push(i); n   } n   for (int i = 0; i < n; i++) { n     if (q.size() != 0) { n       if (q.front() >= i) n         ans[i] = q.front() - i; n       else { n         q.pop(); n         if (q.size() != 0) ans[i] = q.front() - i; n       } n     } n   } n   for (int i = n - 1; i >= 0; i--) { n     if (st.size() != 0) { n       if (st.top() <= i) n         ans[i] = min(ans[i], i - st.top()); n       else { n         st.pop(); n         if (st.size() != 0) ans[i] = min(ans[i], i - st.top()); n       } n     } n   } n   for (int i = 0; i < n; i++) cout << ans[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 998244353; n long long n, m, l, r; n long long binpow(long long a, long long b) { n   long long res = 1; n   while (b) { n     if (b & 1) res = res * a % mod; n     a = a * a % mod; n     b >>= 1; n   } n   return res % mod; n } n long long inv(long long a) { return binpow(a, mod - 2); } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0), cout.tie(0); n   cin >> n >> m >> l >> r; n   long long sz = n * m; n   if (sz & 1) { n     cout << binpow(r - l + 1, sz); n   } else { n     cout << (inv(2) * (binpow(r - l + 1, sz) + (((r - l + 1) % 2) ? 1 : 0))) % n                 mod; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 1; n void solve() { n   long long int n, x, ans = 1, p = -1, q = -1, c; n   cin >> n; n   set<long long int> s; n   long long int a[n]; n   for (long long int i = 0; i < n; i++) cin >> a[i], s.insert(a[i]); n   for (long long int i = 0; i < n; i++) { n     for (long long int j = 0; j < 32; j++) { n       c = 1; n       if (s.find(a[i] - (1 << j)) != s.end()) { n         q = a[i] - (1 << j); n         c++; n       } n       if (s.find(a[i] + (1 << j)) != s.end()) { n         q = a[i] + (1 << j); n         c++; n       } n       if (c == 3) { n         cout << 3 <<   n ; n         cout << a[i] - (1 << j) <<     << a[i] <<     << a[i] + (1 << j); n         return; n       } n       if (c == 2) p = a[i]; n       ans = max(ans, c); n     } n   } n   cout << ans <<   n ; n   if (ans == 2) cout << p <<     << q; n   if (ans == 1) cout << a[0]; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int hh, mm, h, d, c, n; n   cin >> hh >> mm; n   cin >> h >> d >> c >> n; n   float a; n   if (h % n == 0) n     a = (h / n) * c; n   else n     a = (h / n + 1) * c; n   float b; n   int tim = hh * 60 + mm; n   if (tim <= 1200) { n     h += (1200 - tim) * d; n     float rate = (float)c * 4 / 5; n     if (h % n == 0) { n       b = (h / n) * rate; n     } else n       b = (h / n + 1) * rate; n   } else { n     float rate = (float)c * 4 / 5; n     if (h % n == 0) n       b = (h / n) * rate; n     else n       b = (h / n + 1) * rate; n   } n   cout << fixed << setprecision(4) << min(a, b); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, k, m, d, x, ans; n   cin >> n >> k >> m >> d; n   ans = m; n   for (int i = 2; i <= d && (k * i - k + 1) <= n; i++) { n     x = (n / ((i * k) - (k - 1))); n     x = min(x, m); n     ans = max(ans, x * i); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, num[64]; n string s[64]; n int number(string s1, string s2) { n   if (s1 == s2) return 0; n   for (int i = 1; i < s1.length(); i++) { n     string tail = s1.substr(0, i); n     string head = s1.substr(i, s1.length() - i); n     if (head + tail == s2) return i; n   } n   return -1; n } n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> s[i]; n   int mn = 2e9; n   for (int i = 1; i <= n; i++) { n     int ans = 0; n     for (int j = 1; j <= n; j++) { n       int num = number(s[j], s[i]); n       if (num == -1) { n         cout << -1; n         return 0; n       } n       ans += num; n     } n     mn = min(mn, ans); n   } n   cout << mn; n } n


#include <bits/stdc++.h> n using namespace std; n long long k = 1000003; n long long exp(long long p, long long q) { n   if (q == 1) return (long long)p; n   if (q == 0) return 1; n   long long temp = (long long)(exp(p, q / 2) % k); n   if (q % 2 == 0) { n     return (long long)((temp) * (temp)) % k; n   } else { n     return (long long)((((temp) * (temp)) * p) % k); n   } n } n int main() { n   string str; n   getline(cin, str); n   int bin[str.size() + 1]; n   bin[0] = 0; n   for (int i = 0; i < str.size(); i++) { n     int k = (int)str[i]; n     int val = 0; n     long long bina = 0; n     while (k > 0) { n       bina += k % 2; n       k /= 2; n       bina *= (long long)10; n     } n     int count = 0; n     int u = log10(bina); n     if ((int)str[i] < 64) bina *= 10; n     while (bina > 0) { n       long long t = bina % 10; n       val += t * pow((double)2, count); n       bina /= 10; n       count++; n     } n     int prev = bin[i]; n     int s = bin[i] - val; n     bin[i + 1] = val; n     while (s < 0) s += 256; n     cout << s << endl; n   } n   return 0; n } n


//#include  bits/stdc++.h  n  n #define _USE_MATH_DEFINES n #include<cstdio> n #include <cstring> n #include <cmath> n #include <cstdlib> n #include <deque> n #include <algorithm> n #include <functional> n #include <iostream> n #include <list> n #include <map> n #include <array> n #include <unordered_map> n #include<unordered_set> n #include <numeric> n #include <queue> n #include <set> n #include <sstream> n #include <stack> n #include <string> n #include <utility> n #include <vector> n #include <iterator> n #include<iomanip> n #include<complex> n #include<fstream> n #include<assert.h> n  n #include<stdio.h> n  n using namespace std; n  n #define rep(i,a,b) for(int i=(a), i##_len=(b);i<i##_len;i++) n #define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--) n #define all(c) begin(c),end(c) n  n #define int ll n #define SZ(x) ((int)(x).size()) n #define pb push_back n #define mp make_pair n  n //typedef unsigned long long ull; n typedef long long ll; n typedef pair<int, int> pii; n typedef pair<ll, ll> pll; n typedef pair<ll, int> pli; n typedef pair<int, double> pid; n typedef pair<double, int> pdi; n typedef pair<double, double> pdd; n typedef vector< vector<int> > mat; n  n template<class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } n template<class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return true; } return false; } n  n const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f; n const int MOD = (int)1e9 + 7; n  n const double EPS = 1e-9; n  n signed main() n { n  cin.tie(0); n  ios::sync_with_stdio(false); n  n  int N, K, Q; n  cin >> N >> K >> Q; n  n  vector<int> A(N); n  rep(i, 0, N)cin >> A[i]; n  n  vector<vector<int>> WAY(N, vector<int>(K + 1, 0)); n  vector<vector<int>> DP(N, vector<int>(K + 1, 0)); n  rep(i, 0, N)DP[i][K] = 1; n  rep(i, 0, N)WAY[i][0] = 1; n  n  rrep(k, 1, K + 1) n  { n   rep(i, 0, N) n   { n    if (0 < i) n    { n     DP[i - 1][k - 1] += DP[i][k]; n     DP[i - 1][k - 1] %= MOD; n    } n    if (i < N - 1) n    { n     DP[i + 1][k - 1] += DP[i][k]; n     DP[i + 1][k - 1] %= MOD; n    } n   } n  } n  rep(k, 0, K) n  { n   rep(i, 0, N) n   { n    if (0 < i) n    { n     WAY[i - 1][k + 1] += WAY[i][k]; n     WAY[i - 1][k + 1] %= MOD; n    } n    if (i < N - 1) n    { n     WAY[i + 1][k + 1] += WAY[i][k]; n     WAY[i + 1][k + 1] %= MOD; n    } n   } n  } n  n  vector<int> CNT(N,0); n  n  int ans = 0; n  rep(i, 0, N) n  { n   rep(k, 0, K + 1) n   { n    CNT[i] += (DP[i][k] * WAY[i][k]) % MOD; n    CNT[i] %= MOD; n   } n   ans += (CNT[i] * A[i])%MOD; n   ans %= MOD; n  } n  n  int x, y; n  rep(i, 0, Q) n  { n   cin >> x >> y; n   x--; n   ans = (ans - ((CNT[x] * A[x]) % MOD) + MOD) % MOD; n   A[x] = y; n   ans = (ans + ((CNT[x] * A[x]) % MOD)) % MOD; n   cout << ans << endl; n  } n  n  return 0; n }