#include <bits/stdc++.h> n using namespace std; n int tag[10]; n int main() { n   string a, b; n   while (cin >> a >> b) { n     memset(tag, 0, sizeof(tag)); n     ; n     int len = a.size(); n     int len1 = b.size(); n     for (int i = 0; i < len1; i++) { n       int num = b[i] -  0 ; n       tag[num]++; n     } n     for (int i = 0; i < len; i++) { n       int p = a[i] -  0 ; n       int j = 9; n       while (j > p) { n         if (!tag[j]) j--; n         if (tag[j] && j > p) { n           a[i] = j +  0 ; n           tag[j]--; n           break; n         } n       } n     } n     for (int i = 0; i < len; i++) cout << a[i]; n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1000000007; n int n, k; n bool d[1001]; n long long dp[1001][2][1001]; n int cnt[10001]; n long long DP(int i, bool f, int j) { n   if (i == n) return cnt[j] == k; n   if (dp[i][f][j] != -1) return dp[i][f][j]; n   long long ret = 0; n   if (d[i] == 1) n     ret = (ret + DP(i + 1, f, j + 1)) % mod; n   else if (d[i] == 0 and f) n     ret = (ret + DP(i + 1, f, j + 1)) % mod; n   ret = (ret + DP(i + 1, f | (d[i] == 1), j)) % mod; n   return dp[i][f][j] = ret; n } n int main() { n   for (int i = 1; i <= 1000; i++) { n     cnt[i] = 1 + cnt[__builtin_popcount(i)]; n   } n   string s; n   cin >> s; n   n = s.size(); n   cin >> k; n   int i = 0; n   for (char c : s) d[i++] = c -  0 ; n   memset(dp, -1, sizeof dp); n   cout << DP(0, 0, 0) - (k == 1); n } n


#include <bits/stdc++.h> n using namespace std; n const long long int INF = 1e18, N = 2e6 + 3, BASE = 130; n int main() { n   long long int n, k; n   cin >> n >> k; n   vector<int> arr(n), up(n); n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n   } n   for (int i = n - 2; i >= 0; i--) { n     if (arr[i + 1] == 1) { n       up[i] = up[i + 1] + 1; n     } else { n       up[i] = 0; n     } n   } n   long long int ans = 0; n   for (int i = 0; i < n; i++) { n     int ind = i; n     long long int p = 1; n     long long int sum = 0; n     int cnt = 0; n     while (ind < n && cnt <= 20) { n       if (p > INF / arr[ind]) { n         break; n       } n       p *= arr[ind]; n       sum += arr[ind]; n       cnt++; n       if (p % sum == 0 && p / sum == k) { n         ans++; n       } n       if (up[ind]) { n         if (p % k == 0) { n           long long int num = p / k; n           if (sum + 1 <= num && num <= sum + up[ind]) { n             ans++; n           } n         } n       } n       sum += up[ind]; n       ind = ind + 1 + up[ind]; n     } n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n void PLAY() { n   cout << fixed << setprecision(10); n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long n, m; n   cin >> n >> m; n   vector<string> x(n), y(m), ans; n   for (int i = 0; i < n; i++) { n     cin >> x[i]; n   } n   for (int i = 0; i < m; i++) { n     cin >> y[i]; n   } n   long long q; n   cin >> q; n   while (q--) { n     long long a; n     cin >> a; n     a--; n     cout << x[a % n] + y[a % m] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /stack:64000000 ) n using namespace std; n const double PI = 2 * acos(0.0); n const double eps = 1e-9; n template <class T> n T jog(T a, T b) { n   return a + b; n } n template <class T> n T bog(T a, T b) { n   return a - b; n } n template <class T> n T gon(T a, T b) { n   return a * b; n } n template <class T> n T sq(T x) { n   return x * x; n } n template <class T> n T gcd(T a, T b) { n   return b == 0 ? a : gcd(b, a % b); n } n template <class T> n T lcm(T a, T b) { n   return (a / gcd(a, b)) * b; n } n template <class T> n T power(T a, T p) { n   T res = 1, x = a; n   while (p) { n     if (p & 1) res = res * x; n     x = x * x; n     p >>= 1; n   } n   return res; n } n template <class T> n T cordinatlenth(T a, T b, T c, T d) { n   return sqrt(sq(a - c) + sq(b - d)); n } n template <class T> n string toString(T n) { n   ostringstream oss; n   oss << n; n   oss.flush(); n   return oss.str(); n } n int toInt(string s) { n   int r = 0; n   istringstream sin(s); n   sin >> r; n   return r; n } n long long bigmod(long long a, long long p, long long mod) { n   long long res = 1, x = a; n   while (p) { n     if (p & 1) res = (res * x) % mod; n     x = (x * x) % mod; n     p >>= 1; n   } n   return res; n } n int diraction1[] = {-1, 0, 0, 1, 1, -1, -1, 1}; n int diraction2[] = {0, -1, 1, 0, 1, -1, 1, -1}; n int horsed1[] = {-2, -2, -1, 1, 2, 2, 1, -1}; n int horsed2[] = {1, -1, -2, -2, -1, 1, 2, 2}; n void input(); n const int INF = 2147483647; n const long long LINF = 9223372036854775807ll; n int n, in[1000010]; n int main() { n   input(); n   return 0; n } n void input() { n   memset(in, 0, sizeof(in)); n   cin >> n; n   for (int i = 0, tmp; i < n; i++) { n     scanf( %d , &tmp); n     in[tmp]++; n   } n   long long ans = 0; n   for (int i = 0; i < 1000005; i++) { n     if (in[i] == 0 || in[i] == 1) continue; n     while (in[i] > 1) { n       ans++; n       in[i + 1]++; n       in[i]--; n     } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int changed; n int answer1, answer2 = 1023; n int main() { n   cin >> n; n   int i; n   for (i = 0; i < n; i++) { n     char tp; n     cin >> tp; n     if (tp ==  | ) { n       int number; n       cin >> number; n       answer1 |= number; n       answer2 |= number; n       changed |= number; n     } else if (tp ==  & ) { n       int number; n       cin >> number; n       answer1 &= number; n       answer2 &= number; n       number = 1023 - number; n       changed |= number; n     } else { n       int number; n       cin >> number; n       answer1 ^= number; n       answer2 ^= number; n     } n   } n   int answer11 = 0; n   int answer22 = 0; n   int answer33 = 0; n   for (i = 0; i < 10; i++) { n     if ((answer1 & (1 << i)) && (answer2 & (1 << i))) { n       answer11 += 1 << i; n     } else { n       if ((answer1 & (1 << i)) && (!(answer2 & (1 << i)))) { n         answer33 += 1 << i; n         answer22 += 1 << i; n       } n       if ((!(answer1 & (1 << i))) && (answer2 & (1 << i))) { n         answer22 += 1 << i; n       } n     } n   } n   cout << 3 << endl; n   cout <<  &   << answer22 << endl; n   cout <<  |   << answer11 << endl; n   cout <<  ^   << answer33 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 1; n string s, t; n char c[N]; n int cs[2][N], ct[2][N], q; n int main() { n   scanf(  %s , c); n   s = c; n   scanf(  %s , c); n   t = c; n   int at = 0; n   for (auto &i : s) { n     at++; n     if (i ==  C  || i ==  B ) { n       i =  B ; n       cs[1][at] = cs[1][at - 1] + 1; n     } else { n       cs[1][at] = cs[1][at - 1]; n       cs[0][at] = cs[0][at - 1] + 1; n     } n   } n   at = 0; n   for (auto &i : t) { n     at++; n     if (i ==  C  || i ==  B ) { n       i =  B ; n       ct[1][at] = ct[1][at - 1] + 1; n     } else { n       ct[1][at] = ct[1][at - 1]; n       ct[0][at] = ct[0][at - 1] + 1; n     } n   } n   scanf( %d , &q); n   for (int i = 0, a, b, c, d; i < q; i++) { n     scanf( %d%d%d%d , &a, &b, &c, &d); n     int difs = cs[1][b] - cs[1][a - 1]; n     int dift = ct[1][d] - ct[1][c - 1]; n     bool thereis = 0; n     int need = dift - difs; n     int trails = min(b - a + 1, cs[0][b]); n     int trailt = min(d - c + 1, ct[0][d]); n     if (need & 1) { n       printf( 0 ); n       continue; n     } n     if (need < 0) { n       printf( 0 ); n       continue; n     } n     if (!need) { n       if (trails < trailt || (trails - trailt) % 3) { n         printf( 0 ); n         continue; n       } n       printf( 1 ); n       continue; n     } n     if (need && trails > trailt) { n       trails = trailt; n       thereis = 1; n     } n     if ((trails - trailt) % 3 || trails < trailt) { n       printf( 0 ); n       continue; n     } n     thereis |= difs; n     if (!thereis && need) { n       printf( 0 ); n       continue; n     } n     printf( 1 ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline T maxi(T a, T b) { n   return a > b ? a : b; n } n template <typename T> n inline T mini(T a, T b) { n   return a < b ? a : b; n } n template <typename T> n inline T absl(T a) { n   return a > 0 ? a : -a; n } n template <typename T> n inline T gcd(T a, T b) { n   T t; n   if (a < b) { n     while (a) { n       t = a; n       a = b % a; n       b = t; n     } n     return b; n   } else { n     while (b) { n       t = b; n       b = a % b; n       a = t; n     } n     return a; n   } n } n template <typename T> n inline T ceiling(T a, T b) { n   if (b < 0) { n     a *= -1; n     b *= -1; n   } n   if ((double)a / (double)b < 0) return 0; n   { n     if (a % b == 0) return (a / b); n     return ((a / b) + 1); n   } n } n vector<int> adj[500000]; n map<pair<int, int>, int> m; n vector<pair<int, int> > v(500000); n vector<bool> visited(500000, false); n int d; n void dfs(int u, int c) { n   visited[u] = true; n   for (auto it : adj[u]) { n     if (!visited[it]) { n       m[{u, it}] = c % d; n       ++c; n       dfs(it, c); n     } n   } n } n void input() { n   int n, k; n   cin >> n >> k; n   for (int i = 0; i < n - 1; ++i) { n     int s, d; n     cin >> s >> d; n     --s; n     --d; n     adj[s].push_back(d); n     adj[d].push_back(s); n     v[i].first = s; n     v[i].second = d; n   } n   int b[n]; n   for (int i = 0; i < n; ++i) { n     b[i] = adj[i].size(); n   } n   sort(b, b + n, greater<int>()); n   d = b[k]; n   cout << d << endl; n   dfs(0, 0); n   for (int i = 0; i < n - 1; ++i) { n     cout << maxi(m[v[i]], m[{v[i].second, v[i].first}]) + 1 <<    ; n   } n } n int main() { n   input(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 5; n struct node { n   int l, r, sum; n } hjt[maxn * 40]; n int a[maxn], root[maxn], cnt; n int last[maxn]; n void insert(int l, int r, int pre, int &now, int p, int v) { n   hjt[++cnt] = hjt[pre]; n   now = cnt; n   hjt[now].sum = hjt[pre].sum + v; n   if (l == r) return; n   int m = (l + r) >> 1; n   if (p <= m) n     insert(l, m, hjt[pre].l, hjt[now].l, p, v); n   else n     insert(m + 1, r, hjt[pre].r, hjt[now].r, p, v); n } n int query(int now, int l, int r, int k) { n   if (l == r) { n     if (hjt[now].sum <= k) return l; n     return l - 1; n   } n   int mid = (l + r) >> 1; n   int tmp = hjt[hjt[now].l].sum; n   if (tmp > k) n     return query(hjt[now].l, l, mid, k); n   else n     return query(hjt[now].r, mid + 1, r, k - tmp); n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   for (int i = n; i >= 1; i--) { n     if (last[a[i]]) { n       insert(1, n, root[i + 1], root[i], last[a[i]], -1); n       insert(1, n, root[i], root[i], i, 1); n     } else n       insert(1, n, root[i + 1], root[i], i, 1); n     last[a[i]] = i; n   } n   for (int i = 1; i <= n; i++) { n     int ans = 0, l = 1, r = 0; n     while (l <= n) { n       r = query(root[l], 1, n, i); n       l = r + 1; n       ans++; n     } n     cout << ans; n     if (i == n) n       cout <<   n ; n     else n       cout <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, k; n int num = 0; n int e[21][21]; n int flag[21]; n int dfs(int x, int sum, int mid) { n   if (x > n) { n     num = num + 1; n   } else { n     mid = dfs(x + 1, sum, mid); n     for (int y = 1; y <= n; y++) { n       if (num >= k) { n         break; n       } n       if (flag[y] == 0 && e[x][y] != 0 && sum + e[x][y] <= mid) { n         flag[y] = 1; n         mid = dfs(x + 1, sum + e[x][y], mid); n         flag[y] = 0; n       } n     } n   } n   return mid; n } n int main() { n   int u, v, w; n   cin >> n >> m >> k; n   for (int i = 0; i <= 20; i++) { n     for (int j = 0; j <= 20; j++) { n       e[i][j] = 0; n     } n   } n   int l = 0; n   int h = n * 1000; n   int mid = 0; n   for (int i = 0; i < m; i++) { n     cin >> u >> v >> w; n     e[u][v] = w; n   } n   while (l < h) { n     num = 0; n     mid = (l + h) / 2; n     mid = dfs(1, 0, mid); n     if (num < k) { n       l = mid + 1; n     } else { n       h = mid; n     } n   } n   cout << h << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 50005; n const int MAXD = 55; n const int MOD = 1e9 + 7; n template <typename T> n inline void read(T &AKNOI) { n   T x = 0, flag = 1; n   char ch = getchar(); n   while (!isdigit(ch)) { n     if (ch ==  - ) flag = -1; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   AKNOI = flag * x; n } n namespace ModCalculator { n inline void Inc(int &x, int y) { n   x += y; n   if (x >= MOD) x -= MOD; n } n inline void Dec(int &x, int y) { n   x -= y; n   if (x < 0) x += MOD; n } n inline int Add(int x, int y) { n   Inc(x, y); n   return x; n } n inline int Sub(int x, int y) { n   Dec(x, y); n   return x; n } n inline int Mul(int x, int y) { return 1LL * x * y % MOD; } n }  // namespace ModCalculator n using namespace ModCalculator; n char s[MAXN], sx[MAXD], sy[MAXD]; n int n, d, dg[MAXD]; n int tot, ch[MAXN][10], fail[MAXN], ed[MAXN], q[MAXN]; n int dp[MAXD][MAXN]; n void Insert(int l, int r) { n   int p = 0; n   for (int i = l; i <= r; ++i) { n     int x = s[i] -  0 ; n     if (!ch[p][x]) ch[p][x] = ++tot; n     p = ch[p][x]; n   } n   ed[p] = 1; n } n void GetFail() { n   int head = 1, tail = 0; n   for (int i = 0; i < 10; ++i) { n     if (ch[0][i]) { n       q[++tail] = ch[0][i]; n     } n   } n   while (head <= tail) { n     int u = q[head++]; n     for (int i = 0; i < 10; ++i) { n       if (ch[u][i]) { n         fail[ch[u][i]] = ch[fail[u]][i]; n         q[++tail] = ch[u][i]; n       } else { n         ch[u][i] = ch[fail[u]][i]; n       } n     } n   } n } n int DP(int i, int j, bool full) { n   if (i > d) return 1; n   if (!full && dp[i][j] != -1) return dp[i][j]; n   int ret = 0, r = (full ? dg[i] : 9); n   for (int k = 0; k <= r; ++k) { n     if (ed[ch[j][k]]) continue; n     Inc(ret, DP(i + 1, ch[j][k], full && (k == r))); n   } n   if (!full) dp[i][j] = ret; n   return ret; n } n int Calc(char *ss) { n   int ret = 0; n   for (int i = 1; i <= d; ++i) { n     ret = Add(Mul(ret, 10), (dg[i] = ss[i] -  0 )); n   } n   return Sub(ret, DP(1, 0, 1)); n } n void init() { n   scanf( %s , s + 1); n   n = strlen(s + 1); n   scanf( %s , sx + 1); n   scanf( %s , sy + 1); n   d = strlen(sx + 1); n   int d2 = d / 2; n   for (int i = 1; i + d2 - 1 <= n; ++i) { n     Insert(i, i + d2 - 1); n   } n   GetFail(); n } n void solve() { n   int p = d; n   for (; sx[p] ==  0 ; sx[p--] =  9 ) n     ; n   sx[p] -= 1; n   memset(dp, -1, sizeof(dp)); n   printf( %d n , Sub(Calc(sy), Calc(sx))); n } n int main() { n   init(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long mdin(long long a, long long m) { n   long long r1 = m, r2 = a, t1 = 0, t2 = 1; n   while (r2 > 0) { n     long long q = r1 / r2; n     long long r = r1 % r2; n     r1 = r2; n     r2 = r; n     long long t = (t1 - q * t2) % m; n     t1 = t2; n     t2 = t; n   } n   return (t1 + m) % m; n } n long long ex(long long a, long long n, long long m) { n   if (n == 0) return 1; n   long long ans = ex((a * a) % m, n / 2, m); n   if (n % 2) ans = (a * ans) % m; n   return ans; n } n void bfs(int r, vector<int> adj[], int n) { n   queue<int> q, l; n   vector<int> vis(n + 1, 0); n   q.push(r); n   l.push(1); n   while (!q.empty()) { n     int nd = q.front(); n     q.pop(); n     int lv = l.front(); n     l.pop(); n     for (auto it : adj[nd]) { n       if (vis[it] == 0) { n         vis[it] = 1; n         q.push(it); n         l.push(lv + 1); n       } n     } n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long t = 1; n   while (t--) { n     long long n, k; n     cin >> n >> k; n     if (k > n - 1) { n       cout << 0; n       return 0; n     } n     long long f[n + 1], inf[n + 1]; n     f[0] = 1; n     inf[0] = 1; n     for (long long i = 1; i <= n; i++) { n       f[i] = (i * f[i - 1]) % 998244353; n       inf[i] = mdin(f[i], 998244353); n     } n     if (k == 0) { n       cout << f[n] << endl; n     } else { n       long long ans = (((f[n] * inf[n - k]) % 998244353) * inf[k]) % 998244353; n       k = n - k; n       long long ml = 0; n       int s = 1; n       for (long long i = 0; i <= k - 1; i++) { n         long long cm = (((f[k] * inf[i]) % 998244353) * inf[k - i]) % 998244353; n         long long t = k - i; n         long long et = ex(t, n, 998244353); n         ml = (ml + (cm * et * s) % 998244353 + 998244353) % 998244353; n         s *= -1; n       } n       ans = (((ml * ans) % 998244353) * 2) % 998244353; n       cout << ans << endl; n     } n   } n   return 0; n } n


#include<bits/stdc++.h> n using namespace std; n #define mod        1000000007 n #define pb         push_back n #define mp         make_pair n #define rep(n)     for(int i=0;i<n;i++) n #define repit(x)   for(auto it : x) n #define ll         long long n #define vi         vector<int> n #define vc         vector<char> n #define vs         vector<string> n #define mii        map<int ,int> n #define mci        map<char ,int> n /* bool prime[100001]; n  const int N=100000; n  void sieve() { n  int i,j; n for(i=2;i<=N;i++) n prime[i]=true; n  prime[0]=prime[1]=false; n for(i=2;i*i<=N;i++){ n if(prime[i]){ n for(j=i*i;j<=N;j+=i) n prime[j]=false; n } n } n }*/ n int gcd(int a,int b){ n if(b==0) n return a; n else n return gcd(b,a%b); n } n void swap(int *x,int *y){ n int temp=*x; n *x=*y; n *y=temp; n } n int main() n { n      n ios::sync_with_stdio(0); n cin.tie(0); n cout.tie(0); n int t; n cin>>t; n while(t--){ n int i,n,c=0,j; n cin>>n; n string s,s2; n cin>>s; n map<string,int>m; n for(i=97;i<=122;i++){ n     string s1; n     s1.push_back(i); n     m.insert(make_pair(s1,0)); n } n for(i=0;i<n;i++){ n     string s1; n     s1.push_back(s[i]); n     m[s1]++; n } n for(auto it=m.begin();it!=m.end();it++){ n     if(it->second==0){ n         c=1; n         s2=it->first; n         break; n     } n } n if(c){ n     cout<<s2<<  n ; n } n else{ n     m.clear(); n     for(i=97;i<=122;i++){ n         for(j=97;j<=122;j++){ n             string s1; n             s1.push_back(i); n             s1.push_back(j); n             m.insert(make_pair(s1,0)); n         } n     } n     for(i=0;i<n-1;i++){ n         string s1; n         s1.push_back(s[i]); n         s1.push_back(s[i+1]); n         m[s1]++; n     } n     for(auto it=m.begin();it!=m.end();it++){ n         if(it->second==0){ n             c=1; n             s2=it->first; n             break; n         } n     } n     if(c) n     cout<<s2<<  n ; n     else{ n         m.clear(); n         for(i=97;i<=122;i++){ n             for(j=97;j<=122;j++){ n                 for(int k=97;k<=122;k++){ n                     string s1; n                     s1.push_back(i); n                     s1.push_back(j); n                     s1.push_back(k); n                     m.insert(make_pair(s1,0)); n                 } n             } n         } n         for(i=0;i<n-2;i++){ n             string s1; n             s1.push_back(s[i]); n             s1.push_back(s[i+1]); n             s1.push_back(s[i+2]); n             m[s1]++; n              n         } n         for(auto it=m.begin();it!=m.end();it++){ n             if(it->second==0){ n                 c=1; n                 s2=it->first; n                 break; n             } n         } n         cout<<s2<<  n ; n     } n } n } n return 0; n }


#include <bits/stdc++.h> n using namespace std; n int n, m; n int table[1000][1000]; n int main() { n   int k = 0; n   scanf( %d %d %d , &n, &m, &k); n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < m; ++j) { n       scanf( %d , &table[i][j]); n     } n   } n   while (k) { n     char command; n     int x, y; n     scanf(  %c %d %d , &command, &x, &y); n     if (command ==  g ) n       printf( %d n , table[x - 1][y - 1]); n     else if (command ==  c ) n       for (int i = 0; i < n; ++i) swap(table[i][x - 1], table[i][y - 1]); n     else n       for (int i = 0; i < m; ++i) swap(table[x - 1][i], table[y - 1][i]); n     k--; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n ifstream Cin( input.txt ); n ofstream Cout( output.txt ); n int main() { n   long long i, n4 = 0, n7 = 0, n; n   string a, b; n   cin >> a >> b; n   for (i = 0; i < a.length(); i++) n     if (a[i] != b[i]) { n       if (a[i] ==  4 ) n         n4++; n       else n         n7++; n     } n   cout << n4 + n7 - min(n4, n7); n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n ll min(ll a, ll b) { n   if (a < b) return a; n   return b; n } n void solve() { n   ll testCases; n   cin >> testCases; n   while (testCases--) { n     ll n; n     cin >> n; n     vector<ll> arr(n); n     vector<ll> res(n, 1); n     for (int i = 0; i < n; i++) cin >> arr[i]; n     int i = 0; n     for (i = 0; i < n; i++) { n       if (arr[i] != arr[(i + 1) % n]) break; n     } n     if (i == n) { n       cout <<  1 n ; n       for (i = 0; i < n; i++) cout << 1 <<    ; n       cout <<   n ; n       continue; n     } n     if (n % 2 == 0) { n       cout <<  2 n ; n       for (i = 0; i < n; i++) cout << (i % 2) + 1 <<    ; n       cout <<   n ; n       continue; n     } else { n       bool isTwo = 0; n       for (i = 0; i < n; i++) { n         if (arr[i] == arr[(i + 1) % n]) { n           for (int j = 0, k = i + 1; k < n; k++, j ^= 1) res[k] = j + 1; n           for (int j = 0, k = i; k >= 0; k--, j ^= 1) res[k] = j + 1; n           cout <<  2 n ; n           for (auto e : res) cout << e <<    ; n           cout <<   n ; n           isTwo = 1; n           break; n         } n       } n       if (!isTwo) { n         cout <<  3 n ; n         for (i = 0; i < n - 1; i++) cout << i % 2 + 1 <<    ; n         cout <<  3 n ; n       } n     } n   } n } n int main() { n   { n     ios_base::sync_with_stdio(false); n     cin.tie(NULL); n   }; n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxN = 3000 + 1; n pair<int, int> a[maxN]; n pair<int, int> b[maxN]; n int nx[maxN], pr[maxN], v[maxN]; n int r, c, n; n inline int gg(int i) { n   pair<int, int> x = {a[i].second, a[i].first}; n   return lower_bound(b, b + n + 2, x) - b; n } n inline int ff(int i) { return b[i].first; } n int nex(int i) { n   if (v[nx[i]]) return nx[i]; n   return nx[i] = nex(nx[i]); n } n int prev(int i) { n   if (v[pr[i]]) return pr[i]; n   return pr[i] = prev(pr[i]); n } n int L[11], R[11]; n int main() { n   cin >> r >> c >> n; n   int k; n   cin >> k; n   for (int i = 0; i < n; i++) { n     cin >> a[i].first >> a[i].second; n     b[i] = {a[i].second, a[i].first}; n   } n   b[n] = {0, 0}, b[n + 1] = {c + 1, 0}; n   sort(a, a + n); n   sort(b, b + n + 2); n   v[0] = v[n + 1] = 1; n   pr[0] = 0, nx[n + 1] = n + 1; n   long long ans = 0; n   for (int i = 1; i <= r; i++) { n     int ind = 0; n     for (; ind < n && a[ind].first < i; ind++) n       ; n     if (ind == n) break; n     for (int j = ind; j < n; j++) v[gg(j)] = 1; n     for (int j = 1; j <= n; j++) { n       nx[j] = j + 1; n       pr[j] = j - 1; n     } n     int curr = 0; n     for (int j = 1; j <= n; j++) n       if (v[j]) { n         int x = j; n         for (int t = 1; t < k; t++, x = nex(x)) n           ; n         curr += (ff(j) - ff(prev(j))) * (c + 1 - ff(x)); n       } n     int ptr = n; n     for (int i2 = r; i2 >= i; i2--) { n       ans += curr; n       for (; ptr > ind && a[ptr - 1].first >= i2; ptr--) { n         L[0] = R[0] = gg(ptr - 1); n         for (int t = 1; t < k; t++) { n           L[t] = prev(L[t - 1]); n           R[t] = nex(R[t - 1]); n         } n         for (int t = 0; t < k; t++) { n           int left = L[t]; n           int right = R[k - t - 1]; n           curr -= (ff(left) - ff(prev(left))) * (ff(nex(right)) - ff(right)); n         } n         v[L[0]] = 0; n       } n     } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int arr[500005], brr[500005]; n pair<int, int> tree[500005 * 4]; n vector<int> vv[500005]; n vector<pair<int, int> > qr[500005]; n int ans[500005]; n void build(int node, int b, int e) { n   if (b == e) { n     tree[node] = make_pair(500000000, 500000000); n     return; n   } n   int left = 2 * node, right = left + 1, mid = (b + e) / 2; n   build(left, b, mid); n   build(right, mid + 1, e); n   tree[node] = tree[left]; n } n void update(int node, int b, int e, int i, int v) { n   if (e < i || b > i) return; n   if (b == e && b == i) { n     tree[node] = make_pair(v, arr[b]); n     return; n   } n   int left = 2 * node, right = left + 1, mid = (b + e) / 2; n   update(left, b, mid, i, v); n   update(right, mid + 1, e, i, v); n   if (tree[left].first <= tree[right].first) { n     tree[node] = tree[left]; n   } else n     tree[node] = tree[right]; n } n pair<int, int> query(int node, int b, int e, int l, int r) { n   if (b > r || e < l) return make_pair(500000000, 500000000); n   if (b >= l && e <= r) return tree[node]; n   int left = 2 * node, right = left + 1, mid = (b + e) / 2; n   pair<int, int> p = query(left, b, mid, l, r); n   pair<int, int> q = query(right, mid + 1, e, l, r); n   if (p.first <= q.first) return p; n   return q; n } n int main() { n   int n; n   scanf( %d , &n); n   int mx = 0; n   for (int i = 1; i <= n; i++) { n     scanf( %d , &arr[i]); n     vv[arr[i]].push_back(i); n     mx = max(mx, arr[i]); n   } n   int q; n   scanf( %d , &q); n   for (int i = 1; i <= q; i++) { n     int l, r; n     scanf( %d %d , &l, &r); n     qr[r].push_back(make_pair(l, i)); n   } n   build(1, 1, n); n   for (int i = 1; i <= n; i++) { n     int id = lower_bound(vv[arr[i]].begin(), vv[arr[i]].end(), i) - n              vv[arr[i]].begin(); n     int v; n     if (id == 0) { n       v = 1; n       brr[arr[i]] = i; n     } else { n       v = vv[arr[i]][id - 1] + 1; n       update(1, 1, n, brr[arr[i]], 500000000); n       brr[arr[i]] = i; n     } n     update(1, 1, n, i, v); n     for (int j = 0; j < qr[i].size(); j++) { n       pair<int, int> ret = query(1, 1, n, qr[i][j].first, i); n       if (qr[i][j].first >= ret.first) ans[qr[i][j].second] = ret.second; n     } n   } n   for (int i = 1; i <= q; i++) printf( %d n , ans[i]); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 10; n const int MAXN = 200000; n int n, tot; n bool bz[N]; n vector<int> g[N], edge[N]; n int e[N * 2][2], gg[N]; n int sz; n long long ans[N]; n void add(int x, int y) { n   e[++tot][0] = y; n   e[tot][1] = gg[x]; n   gg[x] = tot; n } n void dfs(int x) { n   bz[x] = 0; n   sz++; n   for (int i = gg[x]; i; i = e[i][1]) n     if (bz[e[i][0]]) dfs(e[i][0]); n   gg[x] = 0; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     int x; n     scanf( %d , &x); n     for (int j = 1; 1ll * j * j <= x; j++) n       if ((x % j) == 0) { n         g[j].push_back(i); n         if (j * j != x) g[x / j].push_back(i); n       } n   } n   for (int i = 1; i < n; i++) { n     int x, y; n     scanf( %d %d , &x, &y); n     edge[x].push_back(y); n     edge[y].push_back(x); n   } n   for (int i = 1; i <= MAXN; i++) n     if (g[i].size()) { n       tot = 0; n       for (auto u : g[i]) bz[u] = 1; n       for (auto u : g[i]) n         for (auto v : edge[u]) n           if (bz[v]) add(u, v); n       for (auto u : g[i]) n         if (bz[u]) { n           sz = 0; n           dfs(u); n           ans[i] += 1ll * sz * (sz - 1) / 2 + sz; n         } n     } n   for (int i = MAXN; i; i--) n     for (int j = 2; 1ll * j * i <= MAXN; j++) ans[i] -= ans[i * j]; n   for (int i = 1; i <= MAXN; i++) n     if (ans[i]) printf( %d %lld n , i, ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int k, n; n   scanf( %d%d , &n, &k); n   vector<int> a(n), b(n), distributed(n), p(n); n   vector<vector<int> > cart(k); n   for (int i = 0; i < n; ++i) { n     scanf( %d%d , &a[i], &b[i]); n     distributed[i] = 0; n     p[i] = i; n   } n   for (int i = 0; i < n; ++i) { n     for (int j = i + 1; j < n; ++j) { n       if (a[p[i]] < a[p[j]]) { n         swap(p[i], p[j]); n       } n     } n   } n   int cart_ptr = 0; n   for (int i = 0; i < n && cart_ptr < k; ++i) { n     if (b[p[i]] == 1) { n       distributed[p[i]] = 1; n       cart[cart_ptr].push_back(p[i]); n       ++cart_ptr; n     } n   } n   deque<int> available; n   for (int i = 0; i < n; ++i) { n     if (!distributed[p[i]]) { n       available.push_back(p[i]); n     } n   } n   while (cart_ptr < k) { n     cart[cart_ptr].push_back(available.front()); n     available.pop_front(); n     ++cart_ptr; n   } n   while (!available.empty()) { n     cart[k - 1].push_back(available.front()); n     available.pop_front(); n   } n   long long ans = 0; n   for (int i = 0; i < k; ++i) { n     int min_cost = 2000000000; n     for (int j = 0; j < (int)cart[i].size(); ++j) { n       ans += a[cart[i][j]] * 2; n       min_cost = min(min_cost, a[cart[i][j]]); n     } n     if (b[cart[i][0]] == 1) { n       ans -= min_cost; n     } n   } n   cout << ans / 2; n   if (ans % 2 == 0) { n     cout <<  .0  << endl; n   } else { n     cout <<  .5  << endl; n   } n   for (int i = 0; i < k; ++i) { n     cout << cart[i].size(); n     for (int j = 0; j < (int)cart[i].size(); ++j) { n       cout <<     << cart[i][j] + 1; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = (int)1e9; n const int mod = (int)1e9 + 7; n const double pi = acos(-1.0); n const double eps = 1e-9; n const int maxn = (int)2e6 + 11; n long long n; n long long a[maxn]; n long long sum[maxn]; n int main() { n   cin >> n; n   a[1] = 2; n   sum[1] = 2; n   for (int i = 2; i <= 2000000; i++) { n     a[i] = a[i - 1] + 3; n     sum[i] = sum[i - 1] + a[i]; n   } n   int ans = 0; n   for (int i = 1; sum[i] <= n; i++) { n     if ((n - sum[i]) % 3 == 0) ans++; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<long long, long long> mp[12]; n long long a[200005], dig[200005]; n long long state[200005][12]; n int main() { n   long long n, k; n   scanf( %lld %lld , &n, &k); n   for (int i = 0; i < n; i++) scanf( %lld , &a[i]); n   for (int i = 0; i < n; i++) { n     dig[i] = log10(a[i]) + 1; n   } n   for (int i = 0; i < n; i++) { n     long long x = a[i]; n     long long tencnt = 1; n     while (tencnt < 11) { n       x = (x * 10LL) % k; n       mp[tencnt][x]++; n       state[i][tencnt] = x; n       tencnt++; n     } n   } n   long long ans = 0; n   for (int i = 0; i < n; i++) { n     long long len = dig[i]; n     long long m = (k - (a[i] % k)) % k; n     if (mp[len].count(m) == 0) continue; n     ans += mp[len][m]; n   } n   for (int i = 0; i < n; i++) { n     long long len = dig[i]; n     long long x = state[i][len]; n     if ((x + a[i]) % k == 0) { n       ans--; n     } n   } n   printf( %lld n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n const long long LINF = ~0ULL >> 1; n const int N = 1e3 + 7; n const int INF = 2e8 + 7; n const int MOD = 1e9 + 7; n const double PI = acos(-1.0); n const double EPS = 1e-8; n int n; n long long calc(long long A, long long B, long long C) { n   return (A + 1) * (B + 2) * (C + 2); n } n void solve() { n   long long mn = LINF, mx = -LINF, tmp; n   for (int i = 1; 1ll * i * i * i <= n; ++i) { n     if (n % i) continue; n     for (int j = i, J = n / i; 1ll * j * j <= J; ++j) { n       if (J % j) continue; n       int k = n / i / j; n       tmp = calc(i, j, k); n       mn = min(mn, tmp), mx = max(mx, tmp); n       tmp = calc(j, i, k); n       mn = min(mn, tmp), mx = max(mx, tmp); n       tmp = calc(k, i, j); n       mn = min(mn, tmp), mx = max(mx, tmp); n     } n   } n   cout << mn - n <<     << mx - n << endl; n } n int main() { n   std::ios_base::sync_with_stdio(false); n   cin >> n; n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, x, r[100000], pos[100000], flag = 1, flag2; n pair<int, int> a[100000]; n vector<int> ans, ans2; n int l(int n) { n   if (!n) return 1; n   if (n % 10 == 4 || n % 10 == 7) return l(n / 10); n   return 0; n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d , &x); n     a[i] = pair<int, int>(x, i); n   } n   sort(a, a + n); n   for (int i = 0; i < n; i++) { n     r[a[i].second] = i; n     pos[i] = a[i].second; n     if (a[i].second != i) flag = 0; n     if (!flag2 && l(a[i].first)) x = i, flag2 = 1; n   } n   if (flag) { n     cout << 0 << endl; n     return 0; n   } n   if (!flag2) { n     cout << -1 << endl; n     return 0; n   } n   for (int i = 0; i < n; i++) n     if (i != x) { n       if (pos[i] == i) continue; n       if (pos[x] != i) { n         ans.push_back(pos[x]), ans2.push_back(i); n         int temp = pos[x]; n         swap(pos[x], pos[r[i]]); n         swap(r[temp], r[i]); n       } n       ans.push_back(pos[x]), ans2.push_back(pos[i]); n       swap(pos[x], pos[i]); n       swap(r[pos[x]], r[pos[i]]); n     } n   cout << ans.size() << endl; n   for (int i = 0; i < ans.size(); i++) n     cout << ans[i] + 1 <<     << ans2[i] + 1 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e3 + 10; n const int MOD = 998244353; n const int INF = 10; n int fact[MAXN], invfact[MAXN]; n int add(int a, int b) { n   int sum = a + b; n   return (sum >= MOD ? sum -= MOD : (sum < 0 ? sum += MOD : sum)); n } n int mul(int a, int b) { return 1LL * a * b % MOD; } n int pwr(int a, int b) { n   return (!b ? 1 n              : (b & 1 ? 1LL * a * pwr(1LL * a * a % MOD, b >> 1) % MOD n                       : pwr(1LL * a * a % MOD, b >> 1) % MOD)); n } n int inv(int x) { return pwr(x, MOD - 2); } n void first() { n   fact[0] = 1; n   for (int i = 1; i < MAXN; i++) fact[i] = mul(fact[i - 1], i); n   for (int i = 0; i < MAXN; i++) invfact[i] = inv(fact[i]); n } n int C(int n, int k) { return mul(mul(fact[n], invfact[k]), invfact[n - k]); } n int P(int n, int k) { return mul(fact[n], invfact[n - k]); } n int n, a[MAXN], dp[MAXN]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   first(); n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   dp[n] = 1; n   for (int i = n - 1; i >= 0; i--) { n     if (a[i] <= 0 || a[i] + i - 1 > n - 1) { n       continue; n     } n     for (int j = n - 1; j >= a[i] + i; j--) { n       dp[i] = add(dp[i], mul(dp[j + 1], C(j - i, a[i]))); n     } n   } n   int ans = 0; n   for (int i = 0; i < n; i++) { n     ans = add(ans, dp[i]); n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int* m = new int[n]; n   int* r = new int[n]; n   for (int i = 0; i < n; i++) { n     cin >> m[i]; n   } n   for (int i = 0; i < n; i++) { n     cin >> r[i]; n   } n   double total = 0; n   for (int i = 0; i < 1000000; i++) { n     for (int j = 0; j < n; j++) { n       if (i % m[j] == r[j]) { n         total++; n         break; n       } n     } n   } n   double percentage = total / 1000000; n   cout << fixed << setprecision(6) << percentage << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int dx[] = {1, 1, -1, -1}; n int dy[] = {1, -1, -1, 1}; n long long int n, k, d1, d2, rem; n bool _solve(long long int w1, long long int w2, long long int w3) { n   long long int tar = n / 3; n   if (w1 < 0 || w2 < 0 || w3 < 0) return false; n   if (tar >= w1 && tar >= w2 && tar >= w3 && abs(w1 - w2) == d1 && n       abs(w2 - w3) == d2) n     return true; n   return false; n } n bool solve() { n   long long int _d1, w1, w2, w3, _d2; n   for (int i = 0; i < 4; i++) { n     _d1 = d1 * dx[i]; n     _d2 = d2 * dy[i]; n     w1 = (k + 2 * _d1 + _d2) / 3; n     w2 = w1 - _d1; n     w3 = w2 - _d2; n     if (w1 + w2 + w3 != k) continue; n     if (_solve(w1, w2, w3)) return true; n   } n   return false; n } n int main() { n   ios_base ::sync_with_stdio(false); n   cin.tie(0); n   int t; n   cin >> t; n   while (t--) { n     cin >> n >> k >> d1 >> d2; n     if (n % 3 == 0) { n       if (solve()) n         cout <<  yes  << endl; n       else n         cout <<  no  << endl; n     } else n       cout <<  no  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 1.01e9; n const double eps = 1e-9; n const int N = 1010; n char s[N][N]; n char res[N * N]; n int resc = 0; n char ch[4] = { D ,  L ,  R ,  U }; n int dx[4] = {1, 0, 0, -1}; n int dy[4] = {0, -1, 1, 0}; n pair<int, int> q[N * N]; n int st = 0, en = 0; n int d[N][N]; n int n, m; n void bfs(int x, int y) { n   for (int i = 0; (i) < (n); ++i) n     for (int j = 0; (j) < (m); ++j) d[i][j] = inf; n   d[x][y] = 0; n   q[en++] = {x, y}; n   while (st < en) { n     x = q[st].first; n     y = q[st].second; n     st++; n     for (int i = 0; (i) < (4); ++i) { n       int xx = x + dx[i]; n       int yy = y + dy[i]; n       if (0 <= xx && xx < n && 0 <= yy && yy < m && s[xx][yy] ==  .  && n           d[xx][yy] == inf) { n         d[xx][yy] = d[x][y] + 1; n         q[en++] = {xx, yy}; n       } n     } n   } n } n int main() { n   int k; n   scanf( %d%d%d , &n, &m, &k); n   if (k & 1) { n     printf( IMPOSSIBLE n ); n     return 0; n   } n   for (int i = 0; (i) < (n); ++i) scanf( %s , s[i]); n   int px = -1, py = -1; n   for (int i = 0; (i) < (n); ++i) n     for (int j = 0; (j) < (m); ++j) n       if (s[i][j] ==  X ) { n         px = i; n         py = j; n         s[i][j] =  . ; n       } n   bfs(px, py); n   int x = px, y = py; n   for (int it = 0; (it) < (k); ++it) { n     int ok = 0; n     for (int i = 0; (i) < (4); ++i) { n       int xx = x + dx[i]; n       int yy = y + dy[i]; n       if (0 <= xx && xx < n && 0 <= yy && yy < m && d[xx][yy] <= k - it - 1) { n         res[resc++] = ch[i]; n         x = xx; n         y = yy; n         ok = 1; n         break; n       } n     } n     if (!ok) { n       printf( IMPOSSIBLE n ); n       return 0; n     } n   } n   printf( %s n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char s[100010], a[110], b[110]; n int pa[110], pb[110]; n int main() { n   scanf( %s%s%s , s, a, b); n   int i, j; n   j = -1; n   pa[0] = -1; n   int lena = strlen(a); n   for (i = 1; i < lena; i++) { n     while (j > -1 && a[j + 1] != a[i]) j = pa[j]; n     if (a[j + 1] == a[i]) j++; n     pa[i] = j; n   } n   j = -1; n   pb[0] = -1; n   int lenb = strlen(b); n   for (i = 1; i < lenb; i++) { n     while (j > -1 && b[j + 1] != b[i]) j = pb[j]; n     if (b[j + 1] == b[i]) j++; n     pb[i] = j; n   } n   int len = strlen(s); n   int minn = len + 1; n   j = -1; n   for (i = 0; i < len; i++) { n     while (j > -1 && a[j + 1] != s[i]) j = pa[j]; n     if (a[j + 1] == s[i]) j++; n     if (j == lena - 1) { n       minn = i; n       break; n     } n   } n   bool flag1 = false, flag2 = false; n   j = -1; n   for (i = minn + 1; i < len; i++) { n     while (j > -1 && b[j + 1] != s[i]) j = pb[j]; n     if (b[j + 1] == s[i]) j++; n     if (j == lenb - 1) { n       flag1 = true; n       break; n     } n   } n   for (i = 0; i < len / 2; i++) swap(s[i], s[len - i - 1]); n   j = -1; n   minn = len; n   for (i = 0; i < len; i++) { n     while (j > -1 && a[j + 1] != s[i]) j = pa[j]; n     if (a[j + 1] == s[i]) j++; n     if (j == lena - 1) { n       minn = i; n       break; n     } n   } n   j = -1; n   for (i = minn + 1; i < len; i++) { n     while (j > -1 && b[j + 1] != s[i]) j = pb[j]; n     if (b[j + 1] == s[i]) j++; n     if (j == lenb - 1) { n       flag2 = true; n       break; n     } n   } n   if (flag1 && flag2) n     printf( both n ); n   else if (flag1) n     printf( forward n ); n   else if (flag2) n     printf( backward n ); n   else n     printf( fantasy n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long N, a[100010], s[100100], cnt[100010]; n void update(long long *t, long long pos, long long val) { n   for (; pos <= N; pos += pos & (-pos)) t[pos] += val; n } n long long query(long long *t, long long r) { n   long long res = 0; n   for (; r > 0; r -= r & (-r)) res += t[r]; n   return res; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> N; n   int i; n   long long nr, sum; n   long double res = 0; n   for (i = 1; i <= N; i++) { n     cin >> a[i]; n     update(cnt, a[i], 1); n     update(s, a[i], i); n   } n   for (i = 1; i <= N; i++) { n     nr = query(cnt, a[i] - 1); n     sum = query(s, a[i] - 1); n     res += nr - (1.0 * i * (nr * (N + 1) - sum)) / (N * (N + 1)); n     nr = N - i - nr; n     sum = query(s, N) - sum - i; n     res += (1.0 * i * (nr * (N + 1) - sum)) / (N * (N + 1)); n     update(cnt, a[i], -1); n     update(s, a[i], -i); n   } n   cout << fixed << setprecision(15) << res <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int res[111]; n int a[111][111]; n bool used[111]; n ; n bool used_j[111]; n int v[111]; n int main() { n   int n, m, k; n   cin >> n >> m >> k; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       cin >> a[i][j]; n     } n   } n   for (int i = 0; i < m; i++) { n     for (int w = 0; w < 111; w++) { n       v[w] = 0; n     } n     for (int j = 0; j < n; j++) { n       if (!used[a[j][i]] && !used_j[j]) n         v[a[j][i]]++; n       else if (!used_j[j] && used[a[j][i]]) { n         if (res[j] == 0) res[j] = i + 1; n         used_j[j] = 1; n       } n     } n     for (int j = 0; j < n; j++) { n       if (v[a[j][i]] > 1 && a[j][i] != 0) { n         if (res[j] == 0) res[j] = i + 1; n         used[a[j][i]] = 1; n         used_j[j] = 1; n       } n     } n   } n   for (int i = 0; i < n; i++) { n     cout << res[i] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n const int N = 1e6 + 10; n const long long INF = 1e18; n const long double EPS = 1e-12; n int a[300005]; n map<int, int> mi; n int l_, r_; n long long pre[300005]; n int b[300005]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = (int)1; i < (int)n + 1; i++) { n     scanf( %d , &a[i]); n   } n   for (int i = (int)1; i < (int)n + 1; i++) { n     if (mi[a[i]] == 0) mi[a[i]] = i; n   } n   for (int i = (int)1; i < (int)n + 1; i++) { n     pre[i] = pre[i - 1]; n     if (a[i] >= 0) pre[i] += (long long)(a[i]); n   } n   long long ans = -(long long)(1e12); n   l_ = 0; n   r_ = 0; n   for (int i = n; i >= 1; i--) { n     int x = a[i]; n     int l = mi[x]; n     if (i == mi[x]) continue; n     long long net = pre[i - 1] - pre[l]; n     net += 2LL * (long long)(x); n     if (net > ans) { n       ans = net; n       l_ = l; n       r_ = i; n     } n   } n   int cnt = 0; n   for (int i = 0; i < (int)n + 1; i++) b[i] = 0; n   for (int i = (int)1; i < (int)n + 1; i++) { n     if (i == l_ || i == r_) { n       b[i] = 1; n       continue; n     } n     if (l_ < i && i < r_ && a[i] > 0) { n       b[i] = 1; n     } else { n       b[i] = 0; n       cnt++; n     } n   } n   cout << ans <<     << cnt << endl; n   for (int i = (int)1; i < (int)n + 1; i++) { n     if (b[i] == 0) { n       printf( %d  , i); n     } n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int Read() { n   int x = 0, f = 1, c = getchar(); n   for (; !isdigit(c); c = getchar()) n     if (c ==  - ) f = -1; n   for (; isdigit(c); c = getchar()) x = x * 10 + c -  0 ; n   return x * f; n } n const int MAXN = 300005; n int n, m, a[MAXN], f[MAXN], g[MAXN]; n inline bool Chk(int d) { n   f[0] = g[0] = 1; n   for (int i = 1, j = 1; i <= n; i++) { n     while (a[i] - a[j] > d) j++; n     if (i >= m && g[i - m] - (j == 1 ? 0 : g[j - 2]) > 0) n       f[i] = 1; n     else n       f[i] = 0; n     g[i] = g[i - 1] + f[i]; n   } n   return f[n]; n } n int main() { n   n = Read(), m = Read(); n   for (int i = 1; i <= n; i++) a[i] = Read(); n   sort(a + 1, a + n + 1); n   int l = 0, r = a[n], ret = 0; n   while (l <= r) { n     int mid = l + r >> 1; n     if (Chk(mid)) n       ret = mid, r = mid - 1; n     else n       l = mid + 1; n   } n   return printf( %d n , ret), 0; n } n


#include <bits/stdc++.h> n using namespace std; n int t; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> t; n   while (t--) { n     int m; n     int n; n     cin >> m >> n; n     vector<int> a[500]; n     vector<int> b[500]; n     for (int i = 0; i < m; i++) { n       for (int j = 0; j < n; j++) { n         int temp; n         cin >> temp; n         a[i].push_back(temp); n       } n     } n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < m; j++) { n         int temp; n         cin >> temp; n         b[i].push_back(temp); n       } n     } n     pair<int, int> place; n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < m; j++) { n         if (a[0][0] == b[i][j]) { n           place = make_pair(i, j); n         } n       } n     } n     map<int, int> rows; n     for (int i = 0; i < m; i++) { n       rows.insert({a[i][0], i}); n     } n     for (int i = 0; i < m; i++) { n       for (int j = 0; j < n; j++) { n         cout << a[rows[b[place.first][i]]][j] <<    ; n       } n       cout <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int t; n   cin >> t; n   auto solve = []() { n     int n; n     cin >> n; n     vector<pair<int, int>> a(n); n     for (int i = 0; i < n; i++) { n       cin >> a[i].first; n       a[i].first--; n     } n     vector<int> ans(n + n); n     vector<int> used(n + n); n     for (int i = 0; i < n; i++) { n       ans[i * 2] = a[i].first; n       a[i].second = i * 2; n       used[a[i].first] = 1; n     } n     for (int i = 1; i < n + n; i += 2) { n       int x = ans[i - 1]; n       int j; n       for (j = x + 1; j < n + n && used[j]; j++) n         ; n       if (j == n + n) { n         cout << -1 <<   n ; n         return 0; n       } n       used[j] = 1; n       ans[i] = j; n     } n     for (int i = 0; i < n + n; i++) { n       if (i > 0) cout <<    ; n       cout << ans[i] + 1; n     } n     cout <<   n ; n   }; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int res(long long int x) { n   long long int ans = 0; n   if (x > 0) ans = (x * (x - 1)) / 2; n   return ans; n } n int main() { n   long int n; n   long long int d; n   cin >> n >> d; n   vector<long long int> vect; n   long long int x; n   for (long int i = 0; i < n; i++) { n     cin >> x; n     vect.push_back(x); n   } n   vector<long long int>::iterator it, it2; n   long long int ans = 0; n   for (long int i = 2; i < n; i++) { n     it = lower_bound(vect.begin(), vect.end(), vect[i] - d); n     if (it != vect.end()) { n       ans = ans + res(i - (it - vect.begin())); n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool Check(int N, int pos) { return (bool)(N & (1 << pos)); } n int Set(int N, int pos) { return (N | (1 << pos)); } n long long BigMod(long long B, long long P, long long M) { n   long long R = 1; n   while (P > 0) { n     if (P % 2 == 1) { n       R = (R * B) % M; n     } n     P /= 2; n     B = (B * B) % M; n   } n   return R; n } n int n, m; n vector<pair<double, int> > v; n int dp[5005][5005][2]; n inline int call(int pos, int got, int stat) { n   if (got > m) return 10000; n   if (pos == n) { n     if (got == m) n       return 0; n     else n       return 10000; n   } n   if (dp[pos][got][stat] != -1) return dp[pos][got][stat]; n   int ret = 0; n   int g = 0; n   if (v[pos].second != got and stat == 0) g = 1; n   ret = min(call(pos + 1, got, 0), call(pos, got + 1, 1)); n   return dp[pos][got][stat] = ret + g; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(nullptr); n   memset(dp, -1, sizeof(dp)); n   cin >> n >> m; n   v.resize(n); n   for (int i = 0; i < n; i++) { n     cin >> v[i].second >> v[i].first; n   } n   sort(v.begin(), v.end()); n   int mx = 10000; n   for (int i = 0; i < m; i++) { n     mx = min(mx, call(0, i + 1, 0)); n   } n   cout << mx; n } n


#include <bits/stdc++.h> n using namespace std; n char bd[1100][1100]; n int n, m; n int rt[1100][1100]; n int down[1100][1100]; n int cnt[1100][1100]; n int check(int width, int height, int x, int y) { n   int filled = 0; n   bool first = true; n   while (true) { n     int godown = down[x][y] - height; n     if (godown < 0) break; n     if (cnt[x][y] - cnt[x][y + width] - cnt[x + down[x][y]][y] + n             cnt[x + down[x][y]][y + width] == n         width * down[x][y]) { n       if (first) { n         filled += width * down[x][y]; n         first = false; n       } else { n         filled += height + width * godown; n       } n     } else n       break; n     if ((y + 1) + width <= m) { n       y++; n       x += godown; n     } else n       break; n   } n   return filled; n } n int main() { n   scanf( %d %d , &n, &m); n   for (int i = 0; i < n; i++) scanf( %s , bd[i]); n   for (int i = n - 1; i >= 0; i--) { n     for (int j = m - 1; j >= 0; j--) { n       if (bd[i][j] ==  . ) { n         rt[i][j] = down[i][j] = 0; n       } else { n         rt[i][j] = 1 + rt[i][j + 1]; n         down[i][j] = 1 + down[i + 1][j]; n       } n     } n   } n   int tlx = -1; n   int tly = -1; n   int tot = 0; n   for (int i = n - 1; i >= 0; i--) { n     for (int j = m - 1; j >= 0; j--) { n       if (bd[i][j] ==  X ) { n         tlx = i; n         tly = j; n         tot++; n       } n       cnt[i][j] = n           (bd[i][j] ==  X ) + cnt[i + 1][j] + cnt[i][j + 1] - cnt[i + 1][j + 1]; n     } n   } n   int best = 1000000000; n   for (int height = 1; height <= down[tlx][tly]; height++) { n     int width = rt[tlx][tly]; n     int filled = check(width, height, tlx, tly); n     if (filled == tot) best = min(best, width * height); n   } n   for (int width = 1; width <= rt[tlx][tly]; width++) { n     int height = down[tlx][tly]; n     int filled = check(width, height, tlx, tly); n     if (filled == tot) best = min(best, width * height); n   } n   printf( %d n , best == 1000000000 ? -1 : best); n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 1 << 30; n const int N = 100100; n char a[N], b[N]; n int n, m, k, p[27]; n int Par(int i) { return p[i] = (i == p[i]) ? i : Par(p[i]); } n bool same(int i, int j) { return Par(i) == Par(j); } n int comp[27]; n void mrg(int i, int j) { n   if (same(i, j)) return; n   i = Par(i); n   j = Par(j); n   p[i] = p[j]; n   comp[i] += comp[j]; n } n vector<pair<char, char> > v; n int main() { n   scanf( %d , &n); n   scanf( %s , a); n   scanf( %s , b); n   for (int i = 0; i < 26; i++) { n     p[i] = i; n     comp[i] = 1; n   } n   for (int i = 0; i < n; i++) { n     if (same(b[i] -  a , a[i] -  a )) continue; n     mrg(b[i] -  a , a[i] -  a ); n     v.push_back({a[i], b[i]}); n   } n   printf( %d n , v.size()); n   for (auto x : v) printf( %c %c n , x.first, x.second); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double pi = acos(-1); n struct point { n   int x, y; n } p[2005]; n vector<double> v; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d%d , &p[i].x, &p[i].y); n   long long ans = 0; n   for (int i = 1; i <= n; i++) { n     v.clear(); n     for (int j = 1; j <= n; j++) n       if (j != i) v.push_back(atan2(p[j].y - p[i].y, p[j].x - p[i].x)); n     sort(v.begin(), v.end()); n     int k = 0; n     int now = 0; n     for (int j = 0; j < v.size(); j++) { n       while (v[k] + now * pi < v[j] + pi) { n         k++; n         if (k >= v.size()) k = 0, now += 2; n       } n       int w = (j - k >= 0 ? j - k : k - j - 1); n       ans += 1ll * w * (w - 1) / 2 * (n - w - 2) * (n - w - 3) / 2; n     } n   } n   printf( %lld n , ans / 2); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 6, MAX = 2e5 + 5, OO = 1000000009; n long long gcd(long long a, long long b) { return !b ? a : gcd(b, a % b); } n long long lcm(long long x, long long y) { return (x * y / gcd(x, y)); } n long long fact(long long z) { return (z <= 1) ? 1 : z * fact(z - 1); } n int a, b, aa, bb, ans; n void Check(int p) { n   while (a % p == 0) { n     a /= p; n     aa++; n   } n   while (b % p == 0) { n     b /= p; n     bb++; n   } n   ans += abs(aa - bb); n   aa = bb = 0; n } n int main() { n   scanf( %d %d , &a, &b); n   Check(2); n   Check(3); n   Check(5); n   return printf( %d , a == b ? ans : -1), 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1000 * 1000 * 1000 + 7; n const int INF = 2000 * 1000 * 1000; n const double EPS = 1e-9; n const double pi = acos(-1.0); n const int maxn = 100010; n inline long long sqr(int n) { return n * 1ll * n; } n struct Point { n   int x, y; n   Point() {} n   inline bool operator<(const Point& p) const { return y < p.y; } n   inline long long dist_to(const Point& p) const { n     return sqr(x - p.x) + sqr(y - p.y); n   } n }; n Point a[maxn]; n int n, x, sum; n long long minans = INF * 1ll * INF; n inline void update(const Point& p1, const Point& p2) { n   minans = min(minans, p1.dist_to(p2)); n } n inline void go(int l, int r) { n   if (r - l <= 3) { n     for (int i = l; i <= r; i++) n       for (int j = i + 1; j <= r; j++) update(a[i], a[j]); n     sort(a + l, a + r + 1); n     return; n   } n   int mid = (l + r) >> 1; n   int midx = a[mid].x; n   int buf_size = 0; n   go(l, mid - 1); n   go(mid, r); n   static Point buf[maxn]; n   merge(a + l, a + mid, a + mid, a + r + 1, buf); n   copy(buf, buf + (r - l + 1), a + l); n   for (int i = l; i <= r; i++) { n     if (sqr(abs(a[i].x - midx)) < minans) { n       for (int j = buf_size - 1; j >= 0 && sqr(a[i].y - buf[j].y) < minans; j--) n         update(a[i], buf[j]); n       buf[buf_size++] = a[i]; n     } n   } n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d , &x); n     sum += x; n     a[i].x = i + 1; n     a[i].y = sum; n   } n   go(0, n - 1); n   printf( %I64d , minans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   long long n, m; n   cin >> n >> m; n   vector<long long> col(m); n   for (long long i = 0; i < m; i++) { n     cin >> col[i]; n   } n   vector<long long> suf(m, 0); n   suf[m - 1] = 0; n   for (long long i = m - 2; i >= 0; i--) { n     suf[i] = suf[i + 1] + col[i + 1]; n   } n   vector<long long> ans; n   for (long long i = 0, cur = 0, r = 0; i < m; i++) { n     ans.push_back(cur); n     r = cur + col[i]; n     if (r > n) { n       cout <<  -1  << endl; n       return 0; n     } n     cur++; n     if (suf[i] < n - r) { n       cout <<  -1  << endl; n       return 0; n     } else if (suf[i] < n - cur) { n       long long pos = n - suf[i]; n       for (long long j = i + 1; j < m; j++) { n         ans.push_back(pos); n         pos += col[j]; n       } n       break; n     } n   } n   for (long long i = 0; i < m; i++) { n     cout << ans[i] + 1 <<    ; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n const int iinf = 1 << 29; n const long long llinf = 1ll << 60; n const double PI = 3.14159265; n mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); n long long mrand(long long B) { return (unsigned long long)rng() % B; } n long long cross(pair<long long, long long> p1, pair<long long, long long> p2) { n   return p1.first * p2.second - p2.first * p1.second; n } n bool online(pair<long long, long long> a, pair<long long, long long> b, n             pair<long long, long long> c) { n   pair<long long, long long> p1{b.first - a.first, b.second - a.second}; n   pair<long long, long long> p2{c.first - a.first, c.second - a.second}; n   return cross(p1, p2) == 0; n } n void z_func(vector<int> &z, vector<int> &s, int n) { n   int L = 0, R = 0; n   for (int i = 1; i < n; i++) { n     if (i > R) { n       L = R = i; n       while (R < n && s[R - L] == s[R]) R++; n       z[i] = R - L; n       R--; n     } else { n       int k = i - L; n       if (z[k] < R - i + 1) n         z[i] = z[k]; n       else { n         L = i; n         while (R < n && s[R - L] == s[R]) R++; n         z[i] = R - L; n         R--; n       } n     } n   } n } n const int MXN = 400; n long long ans; n int n, m; n int v[MXN][MXN], up[MXN][MXN], down[MXN][MXN], leftt[MXN][MXN], n     rightt[MXN][MXN], ul[MXN][MXN], ur[MXN][MXN], lol[MXN][MXN], lr[MXN][MXN]; n void populate() { n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < m; ++j) { n       if (!v[i][j]) continue; n       if (i && v[i - 1][j]) up[i][j] = 1 + up[i - 1][j]; n       if (j && v[i][j - 1]) leftt[i][j] = 1 + leftt[i][j - 1]; n       if (i && j && v[i - 1][j - 1]) ul[i][j] = 1 + ul[i - 1][j - 1]; n       if (i && j != m - 1 && v[i - 1][j + 1]) ur[i][j] = 1 + ur[i - 1][j + 1]; n     } n   } n   for (int i = n - 1; i >= 0; --i) { n     for (int j = m - 1; j >= 0; --j) { n       if (!v[i][j]) continue; n       if (i != n - 1 && v[i + 1][j]) down[i][j] = 1 + down[i + 1][j]; n       if (j != m - 1 && v[i][j + 1]) rightt[i][j] = 1 + rightt[i][j + 1]; n       if (i != n - 1 && j != m - 1 && v[i + 1][j + 1]) n         lr[i][j] = 1 + lr[i + 1][j + 1]; n       if (i != n - 1 && j && v[i + 1][j - 1]) lol[i][j] = 1 + lol[i + 1][j - 1]; n     } n   } n } n void do_lower_leftt(int x, int y) { n   for (int i = x + 1, j = y - 1; i < n && j >= 0 && v[i][j]; ++i, --j) { n     ans += down[x][y] >= 2 * (i - x) && lr[i][j] >= i - x; n     ans += down[x][y] >= i - x && rightt[i][j] >= y - j; n     ans += lr[x][y] >= i - x && rightt[i][j] >= 2 * (y - j); n   } n } n void do_down(int x, int y) { n   for (int i = x + 1; i < n && v[i][y]; ++i) { n     ans += lr[x][y] >= i - x && rightt[i][y] >= i - x; n     ans += rightt[x][y] >= i - x && ur[i][y] >= i - x; n     ans += n         (i - x) % 2 == 0 && lr[x][y] >= (i - x) / 2 && ur[i][y] >= (i - x) / 2; n   } n } n void do_lower_rightt(int x, int y) { n   for (int i = x + 1, j = y + 1; i < n && j < m && v[i][j]; ++i, ++j) { n     ans += rightt[x][y] >= j - y && up[i][j] >= i - x; n     ans += rightt[x][y] >= 2 * (j - y) && ur[i][j] >= i - x; n   } n } n void work() { n   cin >> n >> m; n   for (int i = 0; i < n; ++i) n     for (int j = 0; j < m; ++j) { n       char c; n       cin >> c; n       while (c !=  0  && c !=  1 ) cin >> c; n       v[i][j] = c ==  0  ? 1 : 0; n     } n   populate(); n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < m; ++j) { n       if (!v[i][j]) continue; n       do_lower_leftt(i, j); n       do_down(i, j); n       do_lower_rightt(i, j); n     } n   } n   cout << ans << endl; n } n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   work(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, i, x, sum = 0; n   long long int c[100009]; n   scanf( %lld %lld , &n, &x); n   for (i = 0; i < n; i++) { n     scanf( %lld , &c[i]); n   } n   sort(c, c + n); n   for (i = 0; i < n; i++) { n     sum = sum + c[i] * x; n     x--; n     if (x == 0) x++; n   } n   printf( %lld n , sum); n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = 2147383647; n const double pi = 2 * acos(0.0); n const double eps = 1e-9; n struct debugger { n   template <typename T> n   debugger& operator,(const T& v) { n     cerr << v <<    ; n     return *this; n   } n } dbg; n inline long long gcd(long long a, long long b) { n   a = ((a) < 0 ? -(a) : (a)); n   b = ((b) < 0 ? -(b) : (b)); n   while (b) { n     a = a % b; n     swap(a, b); n   } n   return a; n } n int ext_gcd(int a, int b, int& x, int& y) { n   if (a == 0) { n     x = 0; n     y = 1; n     return b; n   } n   int x1, y1; n   int d = ext_gcd(b % a, a, x1, y1); n   x = y1 - (b / a) * x1; n   y = x1; n   return d; n } n inline long long modInv(int a, int m) { n   int x, y; n   ext_gcd(a, m, x, y); n   if (x < 0) x += m; n   return x; n } n inline long long power(long long a, long long p) { n   long long res = 1, x = a; n   while (p) { n     if (p & 1) res = (res * x); n     x = (x * x); n     p >>= 1; n   } n   return res; n } n inline long long bigmod(long long a, long long p, long long m) { n   long long res = 1, x = a % m; n   while (p) { n     if (p & 1) res = (res * x) % m; n     x = (x * x) % m; n     p >>= 1; n   } n   return res; n } n bool ispalin(string s) { n   string s2 = s; n   reverse(s2.begin(), s2.end()); n   if (s == s2) n     return true; n   else n     return false; n } n int main() { n   string s, t, x; n   cin >> s; n   int len = s.size(); n   for (int i = (0); i <= (len); ++i) { n     for (int j = ( a ); j <= ( z ); ++j) { n       t = s; n       x = j; n       t.insert(i, x); n       if (ispalin(t)) { n         cout << t << endl; n         return 0; n       } n     } n   } n   cout <<  NA  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int f[4040][4040], n, k, ans; n struct info { n   int be, ti; n } e[4040]; n int main() { n   scanf( %d%d , &n, &k); n   for (int i = 1; i <= n; i++) scanf( %d%d , &e[i].be, &e[i].ti); n   e[n + 1].be = 86401; n   if (n == k) { n     printf( 86400 ); n     return 0; n   } n   f[1][0] = e[1].be + e[1].ti - 1; n   for (int i = 2; i <= n; i++) { n     f[i][0] = max(e[i].be + e[i].ti - 1, f[i - 1][0] + e[i].ti); n     for (int j = 1; j <= min(i, k); j++) { n       if (i - 1 >= j) n         f[i][j] = min(max(f[i - 1][j] + e[i].ti, e[i].be + e[i].ti - 1), n                       f[i - 1][j - 1]); n       else n         f[i][j] = min(e[i].be + e[i].ti - 1, f[i - 1][j - 1]); n     } n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 0; j <= k; j++) { n       ans = max(ans, e[min(n + 1, k - j + i + 1)].be - f[i][j] - 1); n     } n   } n   printf( %d , ans); n } n


#include <bits/stdc++.h> n using namespace std; n inline long long fmm(long long a, long long b, long long m = 1000000007) { n   long long r = 0; n   a %= m; n   b %= m; n   while (b > 0) { n     if (b & 1) { n       r += a; n       r %= m; n     } n     a += a; n     a %= m; n     b >>= 1; n   } n   return r % m; n } n inline long long fme(long long a, long long b, long long m = 1000000007) { n   long long r = 1; n   a %= m; n   while (b > 0) { n     if (b & 1) { n       r *= a; n       r %= m; n     } n     a *= a; n     a %= m; n     b >>= 1; n   } n   return r % m; n } n inline long long sfme(long long a, long long b, long long m = 1000000007) { n   long long r = 1; n   a %= m; n   while (b > 0) { n     if (b & 1) r = fmm(r, a, m); n     a = fmm(a, a, m); n     b >>= 1; n   } n   return r % m; n } n std::vector<long long> primes; n long long primsiz; n std::vector<long long> fact; n std::vector<long long> invfact; n inline void sieve(long long n) { n   long long i, j; n   std::vector<bool> a(n); n   a[0] = true; n   a[1] = true; n   for (i = 2; i * i < n; ++i) { n     if (!a[i]) { n       for (j = i * i; j < n; j += i) { n         a[j] = true; n       } n     } n   } n   for (i = 2; i < n; ++i) n     if (!a[i]) primes.push_back(i); n   primsiz = primes.size(); n } n inline void sieve() { n   long long n = 1010000, i, j, k = 0; n   std::vector<bool> a(n); n   primes.resize(79252); n   a[0] = a[1] = true; n   for (i = 2; (j = (i << 1)) < n; ++i) a[j] = true; n   for (i = 3; i * i < n; i += 2) { n     if (!a[i]) { n       k = (i << 1); n       for (j = i * i; j < n; j += k) a[j] = true; n     } n   } n   k = 0; n   for (i = 2; i < n; ++i) n     if (!a[i]) primes[k++] = i; n   primsiz = k; n } n inline bool isPrimeSmall(unsigned long long n) { n   if (((!(n & 1)) && n != 2) || (n < 2) || (n % 3 == 0 && n != 3)) return false; n   for (unsigned long long k = 1; 36 * k * k - 12 * k < n; ++k) n     if ((n % (6 * k + 1) == 0) || (n % (6 * k - 1) == 0)) return false; n   return true; n } n bool _p(unsigned long long a, unsigned long long n) { n   unsigned long long t, u, i, p, c = 0; n   u = n / 2, t = 1; n   while (!(u & 1)) { n     u /= 2; n     ++t; n   } n   p = fme(a, u, n); n   for (i = 1; i <= t; ++i) { n     c = (p * p) % n; n     if ((c == 1) && (p != 1) && (p != n - 1)) return 1; n     p = c; n   } n   if (c != 1) return 1; n   return 0; n } n inline bool isPrime(unsigned long long n) { n   if (((!(n & 1)) && n != 2) || (n < 2) || (n % 3 == 0 && n != 3)) return 0; n   if (n < 1373653) { n     for (unsigned long long k = 1; (((36 * k * k) - (12 * k)) < n); ++k) n       if ((n % (6 * k + 1) == 0) || (n % (6 * k - 1) == 0)) return 0; n     return 1; n   } n   if (n < 9080191) { n     if (_p(31, n) || _p(73, n)) return 0; n     return 1; n   } n   if (_p(2, n) || _p(7, n) || _p(61, n)) return 0; n   return 1; n } n unsigned long long nCk(long long n, long long k, n                        unsigned long long m = 1000000007) { n   if (k < 0 || k > n || n < 0) return 0; n   if (k == 0 || k == n) return 1; n   if (fact.size() >= (unsigned long long)n && isPrime(m)) { n     return (((fact[n] * invfact[k]) % m) * invfact[n - k]) % m; n   } n   unsigned long long i = 0, j = 0, a = 1; n   k = ((k) < (n - k) ? (k) : (n - k)); n   for (; i < (unsigned long long)k; ++i) { n     a = (a * (n - i)) % m; n     while (j < (unsigned long long)k && (a % (j + 1) == 0)) { n       a = a / (j + 1); n       ++j; n     } n   } n   while (j < (unsigned long long)k) { n     a = a / (j + 1); n     ++j; n   } n   return a % m; n } n void nCkInit(unsigned long long m = 1000000007) { n   long long i, mx = 1010000; n   fact.resize(mx + 1); n   invfact.resize(mx + 1); n   fact[0] = 1; n   for (i = 1; i <= mx; ++i) { n     fact[i] = (i * fact[i - 1]) % m; n   } n   invfact[mx] = fme(fact[mx], m - 2, m); n   for (i = mx - 1; i >= 0; --i) { n     invfact[i] = (invfact[i + 1] * (i + 1)) % m; n   } n } n template <class T> n T gcd(T a, T b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n void extGCD(long long a, long long b, long long &x, long long &y) { n   if (b == 0) { n     x = 1, y = 0; n     return; n   } n   long long x1, y1; n   extGCD(b, a % b, x1, y1); n   x = y1; n   y = x1 - (a / b) * y1; n } n inline void get(long long &x) { n   int n = 0; n   x = 0; n   char c = getchar_unlocked(); n   if (c ==  - ) n = 1; n   while (c <  0  || c >  9 ) { n     c = getchar_unlocked(); n     if (c ==  - ) n = 1; n   } n   while (c >=  0  && c <=  9 ) { n     x = (x << 3) + (x << 1) + c -  0 ; n     c = getchar_unlocked(); n   } n   if (n) x = -x; n } n inline int get(char *p) { n   char c = getchar_unlocked(); n   int i = 0; n   while (c !=   n  && c !=   0  && c !=     && c !=   r  && c != EOF) { n     p[i++] = c; n     c = getchar_unlocked(); n   } n   p[i] =   0 ; n   return i; n } n inline void put(long long a) { n   int n = (a < 0 ? 1 : 0); n   if (n) a = -a; n   char b[20]; n   int i = 0; n   do { n     b[i++] = a % 10 +  0 ; n     a /= 10; n   } while (a); n   if (n) putchar_unlocked( - ); n   i--; n   while (i >= 0) putchar_unlocked(b[i--]); n   putchar_unlocked(   ); n } n inline void putln(long long a) { n   int n = (a < 0 ? 1 : 0); n   if (n) a = -a; n   char b[20]; n   int i = 0; n   do { n     b[i++] = a % 10 +  0 ; n     a /= 10; n   } while (a); n   if (n) putchar_unlocked( - ); n   i--; n   while (i >= 0) putchar_unlocked(b[i--]); n   putchar_unlocked(  n ); n } n const int K = 3; n std::vector<std::vector<long long> > mul(std::vector<std::vector<long long> > a, n                                          std::vector<std::vector<long long> > b, n                                          unsigned long long m = 1000000007) { n   std::vector<std::vector<long long> > c(K, std::vector<long long>(K)); n   for (int ii = 0; ii < (K); ++ii) n     for (int jj = 0; jj < (K); ++jj) n       for (int kk = 0; kk < (K); ++kk) n         c[ii][jj] = (c[ii][jj] + a[ii][kk] * b[kk][jj]) % m; n   return c; n } n std::vector<std::vector<long long> > fme(std::vector<std::vector<long long> > a, n                                          unsigned long long n, n                                          unsigned long long m = 1000000007) { n   if (n == 1) return a; n   if (n & 1) return mul(a, fme(a, n - 1, m), m); n   std::vector<std::vector<long long> > x = fme(a, n / 2, m); n   return mul(x, x, m); n } n int main() { n   std::ios_base::sync_with_stdio(false); n   std::cin.tie(NULL); n   std::cout.tie(NULL); n   long long t = 0, n = 0, m = 0, maxx = 0, minn = 0, curr = 0, k = 0, num = 0, n             siz = 0, n1 = 0, n2 = 0, n3 = 0, n4 = 0, ind = 0; n   long long root = 0, sum = 0, diff = 0, q = 0, choice = 0, d = 0, len = 0, n             begg = 0, endd = 0, pos = 0, cnt = 0, lo = 0, hi = 0, mid = 0, n             ans = 0; n   bool flag = false; n   std::string s1, s2, s3, str; n   char ch, ch1, ch2, ch3, *ptr; n   double dub = 0; n   cin >> n1 >> n2; n   root = 1; n   while (1) { n     if (pos == 0) { n       if (n1 < root) break; n       n1 -= root; n     } else { n       if (n2 < root) break; n       n2 -= root; n     } n     ++root; n     pos = 1 - pos; n   } n   if (pos == 0) n     cout <<  Vladik ; n   else n     cout <<  Valera ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long t = 1; n void solve() { n   long long n, x, y, cnt = 0, p = 1; n   cin >> n; n   vector<long long> arr(n + 1), vis(n + 1, 0); n   vector<vector<long long> > v(n + 1); n   for (long long i = 0; i < n - 1; i += 1) { n     cin >> x >> y; n     v[x].push_back(y); n     v[y].push_back(x); n   } n   queue<long long> q1, q; n   for (long long i = 0; i < n; i += 1) { n     cin >> x; n     arr[i] = x; n     q1.push(x); n   } n   q.push(1); n   q1.pop(); n   while (!q.empty()) { n     x = q.front(); n     vector<long long> vv; n     set<long long> s; n     cnt = 0; n     for (long long i = 0; i < int((v[x]).size()); i += 1) { n       if (vis[v[x][i]] == 0) { n         cnt++; n         vv.push_back(v[x][i]); n       } n     } n     vis[q.front()] = 1; n     q.pop(); n     for (long long i = p; i < p + cnt; i += 1) { n       s.insert(arr[i]); n       q.push(arr[i]); n       vis[arr[i]] = 1; n     } n     for (long long i = 0; i < int((vv).size()); i += 1) { n       if (((s).find(vv[i]) != (s).end())) { n         s.erase(vv[i]); n       } else { n         cout <<  No ; n         return; n       } n     } n     p = p + cnt; n   } n   cout <<  Yes ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n pair<int, int> point[100]; n double ang[100]; n double dp[10000000]; n int i, j, k, N, M, K, l, r; n void clear() {} n void init() { n   scanf( %d%d%d , &N, &l, &r); n   for (i = 0; i <= N - 1; i++) { n     int first; n     scanf( %d%d%d , &point[i].first, &point[i].second, &first); n     ang[i] = (first * 1.0) * 3.1415926535 / 180.0; n   } n } n double func(int a, int h, double ang, double st) { n   double alpha = atan(fabs(a - st) / h); n   if (a > st) { n     double beta = alpha - ang; n     if (beta > 0) { n       double q = h * tan(beta); n       return min(1.0 * r, a - q); n     } else { n       beta *= -1; n       return min(1.0 * r, a + h * tan(beta)); n     } n   } else { n     if (alpha + ang > 3.1415926535 / 2) { n       return 1.0 * r; n     } else { n       double beta = alpha + ang; n       return min(1.0 * r, h * tan(beta) + a); n     } n   } n } n void calc() { n   for (i = 0; i <= (1 << N) - 1; i++) { n     dp[i] = l * 1.0; n   } n   for (i = 0; i <= (1 << N) - 1; i++) { n     for (j = 0; j <= N - 1; j++) { n       if (!(i & (1 << j))) n         dp[i | (1 << j)] = n             max(dp[i | (1 << j)], n                 func(point[j].first, point[j].second, ang[j], dp[i])); n     } n   } n   printf( %.8f n , dp[(1 << N) - 1] - l); n } n int main() { n   clear(); n   init(); n   calc(); n } n


#include <bits/stdc++.h> n using namespace std; n int vis[100005], n, m, x, y, mi, eg, re; n char t; n vector<vector<pair<int, int> > > adj; n vector<vector<int> > re1; n vector<int> t0, t1; n void dfs(int u, int ty) { n   if (vis[u] == -1) { n     vis[u] = ty; n     if (ty == 0) n       t0.push_back(u); n     else n       t1.push_back(u); n     for (int j = 0; j < adj[u].size(); j++) { n       pair<int, int> te = adj[u][j]; n       if (te.second == eg) n         dfs(te.first, ty); n       else n         dfs(te.first, 1 - ty); n     } n   } else if (vis[u] != ty) { n     mi = -1; n     return; n   } n } n int main() { n   cin >> n >> m; n   adj.assign(n + 1, vector<pair<int, int> >(0)); n   re1.assign(2, vector<int>(0)); n   for (int i = 0; i < m; i++) { n     cin >> x >> y >> t; n     adj[x].push_back(pair<int, int>(y, t ==  B )); n     adj[y].push_back(pair<int, int>(x, t ==  B )); n   } n   memset(vis, -1, sizeof(vis)); n   mi = 0; n   eg = 1; n   for (int i = 1; i <= n; i++) { n     t0.clear(); n     t1.clear(); n     if (vis[i] == -1) dfs(i, 0); n     if (mi == -1) break; n     if (t0.size() < t1.size()) { n       mi += t0.size(); n       for (int j = 0; j < t0.size(); j++) re1[0].push_back(t0[j]); n     } else { n       mi += t1.size(); n       for (int j = 0; j < t1.size(); j++) re1[0].push_back(t1[j]); n     } n   } n   re = mi; n   memset(vis, -1, sizeof(vis)); n   mi = 0; n   eg = 0; n   for (int i = 1; i <= n; i++) { n     t0.clear(); n     t1.clear(); n     if (vis[i] == -1) dfs(i, 0); n     if (mi == -1) break; n     if (t0.size() < t1.size()) { n       mi += t0.size(); n       for (int j = 0; j < t0.size(); j++) re1[1].push_back(t0[j]); n     } else { n       mi += t1.size(); n       for (int j = 0; j < t1.size(); j++) re1[1].push_back(t1[j]); n     } n   } n   if (mi == -1) { n     if (re == -1) n       cout << -1 << endl; n     else { n       cout << re << endl; n       for (int i = 0; i < re1[0].size(); i++) cout << re1[0][i] <<    ; n       cout << endl; n     } n   } else if (re == -1) { n     cout << mi << endl; n     for (int i = 0; i < re1[1].size(); i++) cout << re1[1][i] <<    ; n     cout << endl; n   } else { n     if (mi < re) { n       cout << mi << endl; n       for (int i = 0; i < re1[1].size(); i++) cout << re1[1][i] <<    ; n       cout << endl; n     } else { n       cout << re << endl; n       for (int i = 0; i < re1[0].size(); i++) cout << re1[0][i] <<    ; n       cout << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int hashCh = 31; n struct HashCh { n   string code; n   int number; n }; n long long getHash(string text) { n   long long hash = 0; n   int size = text.size(); n   for (int i = 0; i < size; i++) { n     int ch = static_cast<int>(text[i]); n     hash += pow(hashCh, i) * ch; n   } n   return hash; n } n int main() { n   vector<string> password; n   map<long long, HashCh> numbers; n   char buf[80]; n   string oneNumber; n   for (int i = 0; i < 80; i++) { n     char ch; n     cin >> ch; n     oneNumber += ch; n     if ((i % 10) == 9) { n       password.push_back(oneNumber); n       oneNumber =   ; n     } n   } n   for (int i = 0; i < 10; i++) { n     string number; n     cin >> number; n     HashCh ch = HashCh(); n     ch.code = number; n     ch.number = i; n     long long hash = getHash(number); n     numbers[hash] = ch; n   } n   vector<int> result; n   for (int i = 0; i < 8; i++) { n     auto it = numbers.find(getHash(password[i])); n     if (it != numbers.end() && it->second.code == password[i]) { n       result.push_back(it->second.number); n     } n   } n   for (auto i = result.begin(); i != result.end(); i++) { n     cout << *i; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct fastIO { n   fastIO() { ios_base::sync_with_stdio(0); } n }; n const int MAXN = 1e6 * 2; n long long n, a[MAXN], x, ans = MAXN, b; n int main() { n   fastIO(); n   cin >> n; n   for (long long i = 0; i < n; i++) cin >> a[i]; n   for (x = 0; x < n; x++) { n     b = 0; n     for (long long i = 0; i < n; i++) { n       b += (abs(x - i) + i + x) * a[i] * 2; n     } n     if (0) cerr << ans << endl; n     ans = min(ans, b); n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n, k, x; n     cin >> n >> k; n     char s; n     string str; n     int p = n / k, q = n % k; n     for (int i = 0; i < k; ++i) { n       x = i + 97; n       s = x; n       if (i == k - 1) p = p + q; n       for (int j = 0; j < p; ++j) str += s; n     } n     cout << str << endl; n   } n } n


#include<bits/stdc++.h> n using namespace std; n typedef long long ll; n const int maxn=405; n const int P=998244353; n ll dp[2][maxn][maxn][3][3],sum[maxn][maxn]; n int cur,las,n,c[26]; n void add(ll &dpp,ll add) { n  add%=P; n  while(add<0) add+=P; n  dpp+=add; n  while(dpp>=P) dpp-=P; n } n int main() { n  scanf( %d ,&n); n  for(int i=0;i<26;++i) n   scanf( %d ,&c[i]); n  dp[0][0][0][0][0]=1; n  las=1;cur=0; n  for(int i=1;i<=n;++i) { n   swap(las,cur); n   for(int j=0;j<=i;++j) n    for(int k=0;k<=i;++k) n     for(int l=0;l<3;++l) n      for(int m=0;m<3;++m) n       dp[cur][j][k][l][m]=0; n   for(int j=0;j<=i;++j) n    for(int k=0;k<=i;++k) n     for(int l=0;l<3;++l) n      for(int m=0;m<3;++m) { n       if(!dp[las][j][k][l][m])continue; n       add(dp[cur][j][k][m][0],1LL*dp[las][j][k][l][m]*((l||(i<=2))?24:23)%P); n       if(l!=1) add(dp[cur][j+1][k][m][1],dp[las][j][k][l][m]); n       if(l!=2) add(dp[cur][j][k+1][m][2],dp[las][j][k][l][m]); n      } n  }  n  for(int i=0;i<=n;++i) n   for(int j=0;j<=n;++j) n    for(int k=0;k<3;++k) n     for(int kk=0;kk<3;++kk) n      add(sum[i][j],dp[cur][i][j][k][kk]); n  for(int i=n;i>=0;--i) n   for(int j=n;j>=0;--j) n    add(sum[i][j],sum[i+1][j]+sum[i][j+1]-sum[i+1][j+1]); n  ll res=1; n  for(int i=1;i<=n;++i) n   res=res*((i<=2)?26:25)%P; n  for(int i=0;i<26;++i) n   add(res,-sum[c[i]+1][0]); n  for(int i=0;i<26;++i) n   for(int j=i+1;j<26;++j) n    add(res,sum[c[i]+1][c[j]+1]); n  res+=P; n  res%=P; n  printf( %lld n ,res); n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { n   a = abs(a), b = abs(b); n   if (a < b) swap(a, b); n   return b ? gcd(b, a % b) : a; n } n vector<vector<int> > adj; n set<int> st; n const int dx[] = {0, 0, -1, 1}; n const int dy[] = {-1, 1, 0, 0}; n long long n, ary[300000 + 5] = {4, 8, 15, 16, 23, 42}, dp[300000 + 5]; n int main(void) { n   ios::sync_with_stdio(false), cin.tie(NULL); n   cin >> n; n   for (int i = 0, j, x; i < n; ++i) { n     cin >> x; n     j = lower_bound(ary, ary + 6, x) - ary; n     if (j == 0) n       dp[0] += 1; n     else if (dp[j - 1]) { n       dp[j - 1] -= 1; n       dp[j] += 1; n     } n   } n   cout << n - dp[5] * 6; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> v[100001]; n int n, cost[100001]; n int gr, nod, gr2; n void DFS(int x, int t, int val) { n   for (int i = 1; i <= v[x][0]; i++) { n     if (v[x][i] != t) { n       if (val == 0) { n         DFS(v[x][i], x, cost[v[x][i]]); n       } else { n         if (val != cost[v[x][i]]) { n           gr++; n           if (nod != x) gr2++; n           nod = x; n         } n         DFS(v[x][i], x, cost[v[x][i]]); n       } n     } n   } n } n int main() { n   int a, b, i, j; n   cin >> n; n   for (i = 1; i <= n; i++) { n     v[i].push_back(0); n   } n   for (i = 1; i < n; i++) { n     cin >> a >> b; n     v[a][0]++; n     v[b][0]++; n     v[a].push_back(b); n     v[b].push_back(a); n   } n   for (i = 1; i <= n; i++) cin >> cost[i]; n   DFS(1, 0, 0); n   if (gr2 <= 2) { n     if (gr == 0) { n       cout <<  YES  n            <<   n  n            << 1; n     } else { n       gr = 0; n       DFS(nod, 0, 0); n       if (gr != 0) { n         cout <<  NO ; n       } else { n         cout <<  YES ; n         cout <<   n  << nod; n       } n     } n   } else { n     cout <<  NO ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int Ans[1001], A[1001], B[1001], C[1001], D[1001], E[1001]; n int value(int Arr[], int i, int j, int k) { n   return (Arr[j] - Arr[i]) * (Arr[k] - Arr[i]); n } n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> A[i] >> B[i] >> C[i] >> D[i] >> E[i]; n   } n   int index = 0; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       if (j != i) { n         for (int k = 0; k < n; k++) { n           if (k != i && k != j) { n             if ((value(A, i, j, k) + value(B, i, j, k) + value(C, i, j, k) + n                  value(D, i, j, k) + value(E, i, j, k)) > 0) { n               Ans[i] = -1; n               break; n             } n           } n         } n       } n     } n   } n   int count = 0; n   for (int i = 0; i < n; i++) { n     if (Ans[i] != -1) { n       count++; n     } n   } n   cout << count << endl; n   for (int i = 0; i < n; i++) { n     if (Ans[i] != -1) { n       cout << i + 1 << endl; n       count++; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   int T; n   cin >> T; n   while (T--) { n     int N; n     cin >> N; n     vector<int> a(N); n     ; n     for (int& x : a) cin >> x; n     int ans = (a[0] == 1); n     for (int i = 1; i < N; ++i) { n       if (a[i] == 0) continue; n       int j = i; n       while (j < N && a[j] == 1) ++j; n       ans += (j - i) / 3; n       i = j - 1; n     } n     cout << ans <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using Vi = vector<int>; n using Pii = pair<int, int>; n int uplg(int n) { return 32 - __builtin_clz(n); } n int uplg(ll n) { return 64 - __builtin_clzll(n); } n void run(); n int main() { n   cin.sync_with_stdio(0); n   cin.tie(0); n   cout << fixed << setprecision(12); n   run(); n   cout << flush; n   _Exit(0); n } n ll ch2(ll n) { return n * (n - 1) / 2; } n ll ch3(ll n) { return n * (n - 1) * (n - 2) / 6; } n void run() { n   int n; n   double p; n   cin >> n >> p; n   constexpr ll SCALE = 10000; n   ll pScaled = llround(p * SCALE); n   ll low = pScaled * ch3(n) * 2; n   for (int a = (0); a < (n + 1); a++) { n     ll cur = (ch3(a) * 2 + ch2(a) * (n - a) * 2 + a * ch2(n - a)) * SCALE; n     if (cur >= low) { n       cout << a <<   n ; n       return; n     } n   } n   assert(0); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string q[41]; n   long long n, p, i, count = 1, check = 1; n   cin >> n >> p; n   for (i = 0; i < n; i++) cin >> q[i]; n   for (i = n - 2; i >= 0; i--) { n     if (q[i] ==  halfplus ) { n       count = count * 2 + 1; n       check++; n     } else n       count = count * 2; n   } n   cout << p * count - (p / 2) * check << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve(); n const int N = 10000005; n long long i, j, n, s; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t = 1; n   while (t--) { n     solve(); n     cout <<   n ; n   } n   return 0; n } n void solve() { n   cin >> n; n   for (i = 1; i <= n; i = i * 10) { n     s += n - i + 1; n   } n   cout << s; n } n


#include <bits/stdc++.h> n using namespace std; n int par[100005], val[100005]; n int lefm[100005], rim[100005]; n int root = 0; n vector<vector<int> > adjl; n int n; n void pre(int node) { n   if (adjl[node].size() == 0) { n     lefm[node] = val[node]; n     rim[node] = val[node]; n     return; n   } n   for (int i = (0); i < (adjl[node].size()); ++i) { n     pre(adjl[node][i]); n     if (val[adjl[node][i]] < val[node]) n       lefm[node] = lefm[adjl[node][i]]; n     else n       rim[node] = rim[adjl[node][i]]; n   } n } n double res[100005]; n pair<int, int> qu[100005]; n void query(int node, int left, int right, long long cost, int dep) { n   if (adjl[node].size() == 0) { n     for (int i = (left); i < (right); ++i) res[qu[i].second] += cost * 1. / dep; n     return; n   } n   int ind = lower_bound(qu + left, qu + right, make_pair(val[node], 0)) - qu; n   if (ind > left && adjl[node].size() == 2) n     query(adjl[node][0], left, ind, cost + lefm[adjl[node][1]], dep + 1); n   if (ind < right && adjl[node].size() == 2) n     query(adjl[node][1], ind, right, cost + rim[adjl[node][0]], dep + 1); n } n int main() { n   int x, y, k; n   scanf( %d , &n); n   adjl.clear(); n   adjl.resize(n); n   for (int i = (0); i < (n); ++i) { n     scanf( %d %d , &x, &y); n     x--; n     val[i] = y; n     par[i] = x; n     if (x >= 0) adjl[x].push_back(i); n     if (x < 0) root = i; n   } n   for (int i = (0); i < (adjl.size()); ++i) { n     if (adjl[i].size() == 2) n       if (val[adjl[i][0]] > val[adjl[i][1]]) swap(adjl[i][0], adjl[i][1]); n     if (adjl[i].size() == 1) throw 2; n   } n   pre(root); n   scanf( %d , &k); n   for (int i = (0); i < (k); ++i) scanf( %d , &qu[i].first), qu[i].second = i; n   sort(qu, qu + k); n   query(root, 0, k, 0, 0); n   for (int i = (0); i < (k); ++i) printf( %.12lf n , res[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long double judge1(long long k, long long d, long long t) { n   long double ans; n   long double unit = k + (d - k) * 1.0 / 2; n   ans = floor(t / unit) * d; n   long double res = t * 1.0 - floor(t / unit) * unit; n   if (res <= k) { n     ans += res; n     return ans; n   } else { n     ans += k; n     res -= k; n   } n   ans += (res * 2); n   return ans; n } n long double judge2(long long k, long long d, long long t) { n   long double ans; n   ans = t; n   return ans; n } n long double judge3(long long k, long long d, long long t) { n   long double ans; n   long double unitt = (k / d + 1) * d * 1.0; n   long double unitw = k + (unitt - k) / 2.0; n   ans += floor(t / unitw) * unitt; n   long double res = t - floor(t / unitw) * unitw; n   if (fabs(res - k) <= 1e-9 || res < k) { n     ans += res; n     return ans; n   } else { n     ans += k; n     res -= k; n   } n   ans += (res * 2.0); n   return ans; n } n int main() { n   ios::sync_with_stdio(false); n   long long k, d, t; n   while (cin >> k >> d >> t) { n     long double ans; n     if (k < d) n       ans = judge1(k, d, t); n     else if (k % d == 0) n       ans = judge2(k, d, t); n     else n       ans = judge3(k, d, t); n     cout << fixed << setprecision(1) << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int l[2005], r[2005]; n long long k, w[2005]; n long long s[2005][2005]; n long long f[2005]; n long long h[2005]; n long long up(long long x) { return (x / k + (x % k > 0)); } n int main() { n   cin >> n >> k; n   for (int i = (1); i <= (n); i++) cin >> l[i] >> r[i] >> w[i]; n   for (int i = (1); i <= (n); i++) h[i] = -1; n   for (int i = (1); i <= (n); i++) n     for (int j = (1); j <= (n); j++) s[i][j] = -1; n   for (int i = (1); i <= (n); i++) { n     long long start = k; n     long long has = 0; n     for (int j = (i); j <= (n); j++) { n       if (start >= w[j]) { n         if (l[j] < l[j + 1]) s[i][j] = has + start; n         has += w[j]; n         start -= w[j]; n         if (j == n) h[i] = has; n       } else { n         long long t = up(w[j] - start) + 1; n         if (t > r[j] - l[j] + 1) break; n         if (l[j] + t - 1 < l[j + 1]) s[i][j] = has + (t - 1) * k + start; n         has += w[j]; n         if (j == n) h[i] = has; n         start = start + (t - 1) * k - w[j]; n       } n     } n   } n   for (int i = (1); i <= (n - 1); i++) { n     f[i] = -1; n     for (int j = (i - 1); j >= (0); j--) { n       if (s[j + 1][i] >= 0 && f[j] >= 0) n         if (f[i] < 0 || f[j] + s[j + 1][i] < f[i]) f[i] = f[j] + s[j + 1][i]; n     } n   } n   long long ans = -1; n   for (int i = (0); i <= (n - 1); i++) n     if (f[i] >= 0 && h[i + 1] >= 0) { n       if (ans < 0 || f[i] + h[i + 1] < ans) ans = f[i] + h[i + 1]; n     } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 310; n int n, a[maxn]; n int main() { n   scanf( %d , &n); n   for (int i = (1); i <= (n); ++i) scanf( %d , &a[i]); n   for (int i = (1); i <= (n - 1); ++i) { n     for (int j = (1); j <= (a[i]); ++j) printf( PRL ); n     printf( R ); n   } n   for (int j = (1); j <= (a[n]); ++j) printf( LRP ); n } n


#include <bits/stdc++.h> n using namespace std; n long long n, h, ok; n long long cal(long long dep, long long pos, int tag) { n   if (ok) return 0; n   if (dep == 0) { n     ok = 1; n     return 0; n   } n   long long tmp; n   if (!tag) { n     if (pos > (1LL << (dep - 1))) n       tmp = (1LL << dep) + cal(dep - 1, pos - (1LL << (dep - 1)), 0); n     else n       tmp = 1LL + cal(dep - 1, pos, 1); n   } else { n     if (pos > (1LL << (dep - 1))) n       tmp = 1LL + cal(dep - 1, pos - (1LL << (dep - 1)), 0); n     else n       tmp = (1LL << dep) + cal(dep - 1, pos, 1); n   } n   return tmp; n } n int main() { n   while (cin >> h >> n) { n     ok = 0; n     cout << cal(h, n, 0) << endl; n   } n } n


#include <bits/stdc++.h> n long long a[100][100]; n using namespace std; n struct DynamicDisjointSetDS { n   std::unordered_map<int, int> parent, degree; n   int N; n   DynamicDisjointSetDS(int n) { N = n; } n   int getParent(int vertex) { n     if (parent.find(vertex) != parent.end()) { n       if (parent[vertex] != vertex) { n         parent[vertex] = getParent(parent[vertex]); n         return parent[vertex]; n       } n     } else { n       parent.insert(make_pair(vertex, vertex)); n       degree.insert(make_pair(vertex, 1)); n     } n     return vertex; n   } n   void Union(int vertexA, int vertexB) { n     int x = getParent(vertexA); n     int y = getParent(vertexB); n     if (x == y) return; n     if (degree[x] > degree[y]) { n       parent[y] = x; n       degree[x] = degree[x] + degree[y]; n     } else { n       parent[x] = y; n       degree[y] = degree[y] + degree[x]; n     } n   } n   int GetTotalComponent() { n     unordered_set<int> total; n     for (auto itr = parent.begin(); itr != parent.end(); itr++) { n       total.insert(getParent(itr->first)); n     } n     return N - parent.size() + total.size(); n   } n }; n int main() { n   using namespace std; n   long long n, m, q; n   cin >> n >> m >> q; n   unordered_set<long long> s, s2; n   for (long long i = 1; i <= n; i++) { n     s.insert(i); n   } n   for (long long i = 1; i <= m; i++) { n     s2.insert(i); n   } n   DynamicDisjointSetDS ds(m + 1); n   vector<vector<long long>> v(m + 1); n   while (q--) { n     long long r, c; n     cin >> r >> c; n     v[c].push_back(r); n     s.erase(r); n     s2.erase(c); n   } n   for (vector<long long> v2 : v) { n     for (long long i = 1; i < v2.size(); i++) { n       ds.Union(v2[i], v2[i - 1]); n     } n   } n   unordered_set<long long> sc; n   for (long long i = 1; i <= n; i++) { n     sc.insert(ds.getParent(i)); n   } n   cout << (s2.size() + sc.size() - 1) << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int read() { n   int f = 1, x = 0; n   char c = getchar(); n   while (c <  0  || c >  9 ) { n     if (c ==  - ) f = -f; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) x = x * 10 + c -  0 , c = getchar(); n   return f * x; n } n inline int lowbit(int x) { return x & (-x); } n inline int modadd(int x, int y) { n   return (x + y >= 1000000007 ? x + y - 1000000007 : x + y); n } n inline int sgn(int x) { return (x < 0 ? -1 : (x > 0 ? 1 : 0)); } n template <typename T> n T gcd(T a, T b) { n   return (!b) ? a : gcd(b, a % b); n } n int poww(int a, int b) { n   int res = 1; n   while (b > 0) { n     if (b & 1) res = 1ll * res * a % 1000000007; n     a = 1ll * a * a % 1000000007, b >>= 1; n   } n   return res; n } n const int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1}; n const int ddx[] = {-1, -1, -1, 0, 0, 1, 1, 1}, n           ddy[] = {-1, 0, 1, -1, 1, -1, 0, 1}; n int n, a[100005]; n void init() { n   n = read(); n   for (int i = 1; i <= n; ++i) a[i] = read(); n } n bool check1() { n   long long tot = 0; n   for (int i = n - 1; i >= 1; --i) { n     if (a[i] < tot) return false; n     if (a[i] > a[i + 1]) tot += a[i] - a[i + 1]; n   } n   return true; n } n bool check2() { n   long long tot = 0; n   for (int i = 2; i <= n; ++i) { n     if (a[i] < tot) return false; n     if (a[i] > a[i - 1]) tot += a[i] - a[i - 1]; n   } n   return true; n } n void solve() { n   if (check1()) n     printf( YES n ); n   else if (check2()) n     printf( YES n ); n   else n     printf( NO n ); n } n int main() { n   int T = read(); n   while (T--) { n     init(); n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct grp { n   long long to, wt, edgeCode; n   char type; n }; n struct Node { n   long long x, dis; n }; n struct comp { n   inline bool operator()(const Node& n1, const Node& n2) const { n     if (n1.dis != n2.dis) return n1.dis < n2.dis; n     return n1.x < n2.x; n   } n }; n void dijkstra(long long n, vector<vector<grp> >& edges, vector<long long>& dis, n               vector<bool>& visited, vector<pair<long long, long long> >& par) { n   set<Node, comp> myset; n   for (long long i = 1; i <= n; ++i) { n     Node node; n     node.x = i, node.dis = dis[i]; n     myset.insert(node); n   } n   while (myset.size()) { n     set<Node>::iterator it = myset.begin(); n     Node node = *it; n     myset.erase(it); n     visited[node.x] = true; n     for (long long i = 0; i < edges[node.x].size(); ++i) { n       grp& edge = edges[node.x][i]; n       if (visited[edge.to]) { n         continue; n       } n       long long newDis = dis[node.x] + edge.wt; n       if (newDis < dis[edge.to] || n           (newDis == dis[edge.to] && edge.type ==  r )) { n         Node prevNode, newNode; n         prevNode.x = edge.to, prevNode.dis = dis[edge.to]; n         newNode.x = edge.to, newNode.dis = newDis; n         dis[edge.to] = newDis; n         par[edge.to] = {node.x, edge.edgeCode}; n         myset.erase(myset.find(prevNode)); n         myset.insert(newNode); n       } n     } n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   long long n, m, k; n   cin >> n >> m >> k; n   vector<vector<grp> > edges(n + 1); n   long long edgeCode = 0; n   for (long long i = 0; i < m; i++) { n     long long x, y, w; n     cin >> x >> y >> w; n     grp g; n     g.to = y, g.wt = w, g.type =  r , g.edgeCode = edgeCode++; n     edges[x].push_back(g); n     g.to = x, g.edgeCode = edgeCode++; n     edges[y].push_back(g); n   } n   vector<pair<long long, long long> > trainPaths(k); n   for (long long i = 0; i < k; ++i) { n     long long y, w; n     cin >> y >> w; n     trainPaths[i] = {y, edgeCode}; n     grp g; n     g.to = y, g.wt = w, g.type =  t , g.edgeCode = edgeCode++; n     edges[1].push_back(g); n   } n   vector<long long> dis(n + 1, 100000000000000000); n   vector<bool> visited(n + 1, false); n   vector<pair<long long, long long> > par(n + 1, {-1, -1}); n   dis[1] = 0; n   dijkstra(n, edges, dis, visited, par); n   long long ans = 0; n   for (long long i = 0; i < k; ++i) { n     long long dest = trainPaths[i].first, edgeCode = trainPaths[i].second; n     if (par[dest].first == 1 && par[dest].second == edgeCode) { n       continue; n     } n     ++ans; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n long long int power(long long int x, long long int b) { n   long long int p = 1; n   while (b > 0) { n     if (b & 1) { n       p = p * x; n       p %= 1000000007; n     } n     b >>= 1; n     x *= x; n     x %= 1000000007; n   } n   return p % 1000000007; n } n using namespace std; n struct lex_compare { n   bool operator()( n       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) ? p1.second.first < p2.second.first n                                   : p1.first > p2.first; n   } n }; n long long int n, m, b, mo; n long long int dp[2][500 + 1][500 + 1]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   ; n   long long int w, m; n   cin >> w >> m; n   bool f = 1; n   map<long long int, long long int> coeff; n   long long int term = 1; n   while (m >= term && term <= 10000000000) { n     long long int x = (term * (m / term) - (w * term) * (m / (w * term))); n     x /= term; n     if (x <= 1) { n       term *= w; n     } else { n       if (x != (w - 1)) { n         f = 0; n         break; n       } else { n         m += (w * term - term); n       } n       term *= w; n     } n   } n   if (f) n     cout <<  YES  n          <<   n ; n   else n     cout <<  NO  n          <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> di[100003]; n struct trie { n   trie *zero, *one; n   int val; n   trie() { n     zero = one = NULL; n     val = 100003; n   } n } * root[100003]; n void add(trie *cur, int a) { n   int i, ck; n   for (i = 20; i >= 0; i--) { n     ck = (1 << i & a); n     if (ck == 0) { n       cur->val = min(cur->val, a); n       if (cur->zero == NULL) cur->zero = new trie(); n       cur = cur->zero; n       cur->val = min(cur->val, a); n     } else { n       cur->val = min(cur->val, a); n       if (cur->one == NULL) cur->one = new trie(); n       cur = cur->one; n       cur->val = min(cur->val, a); n     } n   } n } n int query(trie *cur, int a, int lim) { n   int i, ck, ans; n   for (i = 20; i >= 0; i--) { n     ck = (1 << i & a); n     if (ck == 0) { n       if (cur->one != NULL && cur->one->val + a <= lim) n         cur = cur->one; n       else if (cur->zero != NULL) n         cur = cur->zero; n       ans = cur->val; n     } else { n       if (cur->zero != NULL && cur->zero->val + a <= lim) n         cur = cur->zero; n       else if (cur->one != NULL) n         cur = cur->one; n       ans = cur->val; n     } n   } n   return ans; n } n void pre() { n   for (int i = 1; i < 100003; i++) { n     for (int j = i; j < 100003; j += i) di[j].push_back(i); n     root[i] = new trie(); n   } n } n int main() { n   pre(); n   int q, a, k, s, v, ty; n   scanf( %d , &q); n   while (q--) { n     scanf( %d , &ty); n     if (ty == 1) { n       scanf( %d , &a); n       for (int x : di[a]) { n         add(root[x], a); n       } n     } else { n       scanf( %d %d %d , &v, &k, &s); n       if (root[k]->val + v > s || root[k]->val == 0 || v % k) n         printf( -1 n ); n       else n         printf( %d n , query(root[k], v, s)); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool isPrime(int n) { n   if (n <= 1) return false; n   for (int i = 2; i < n; i++) n     if (n % i == 0) return false; n   return true; n } n int main() { n   long long int t; n   cin >> t; n   while (t--) { n     long long int n; n     cin >> n; n     vector<long long int> v; n     for (long long int i = 0; i < n; i++) { n       long long int x; n       cin >> x; n       v.push_back(x); n     } n     if (v[0] + v[1] <= v[n - 1]) { n       cout << 1 <<     << 2 <<     << n <<   n ; n     } else { n       cout << -1 <<   n ; n     } n   } n   return 0; n } n