#include <bits/stdc++.h> n using namespace std; n   n #define ll long long n #define vi vector<int> n #define pii pair<int,int> n #define mii map<int,int> n #define pqb priority_queue<int> n #define pqs priority_queue<int,vi,greater<int> > n   n #define ff first n #define ss second n #define pb push_back n #define mp make_pair n #define PI 3.1415926535897932384626 n #define MOD 1000000007 n #define ps(x,y) fixed<<setprecision(y)<<x n  n long long int fact[200005]; n  n long long int pwr(long long int a,long long int b){ n     if(b==0) return 1; n     if(b==1) return a%MOD; n     long long int q = pwr((a*a)%MOD,b/2); n     if(b%2) return (q*a)%MOD; n     return q; n } n  n long long int C(long long int a,long long int b){ n     if(b<0) return 0; n     if(a<b) return 0; n  n     long long int t1 = (fact[a])%MOD; n     long long int t2 = (fact[b]*fact[a-b])%MOD; n     t2 = pwr(t2,MOD-2)%MOD; n  n     return (t1*t2)%MOD; n } n  n void solve(){ n  n     long long int n,l,r; n     cin>>n>>l>>r; n  n     long long int ans=0; n     ans = C(n,n/2)%MOD; n     if(n%2) ans = (ans*2)%MOD; n     ans = (ans*(min(1-l,r-n)))%MOD;  n     for(long long int p=min(1-l,r-n)+1;p<=min(r,n+1-l);p++){ n         long long int a = p + l - 1; n         if(a<=0) a = 0; n         long long int b = n + p - r; n         if(b<=0) b=0; n  n         ans = (C(n-a-b,n/2-a) + ans)%MOD; n         if(n%2) ans = (C(n-a-b,n/2-b) + ans)%MOD; n     } n     cout<<ans<<endl; n  n     return;     n }  n   n int main(){ n  n     ios_base::sync_with_stdio(false); n     cin.tie(NULL); n   n     // freopen( input.txt ,  r , stdin); n     // freopen( error.txt ,  w , stderr); n     // freopen( output.txt ,  w , stdout); n  n     fact[0]=1; n     for(long long int i=1;i<200005;i++){ n         fact[i] = (fact[i-1]*i)%MOD; n     } n   n     int t=1; n     cin>>t; n   n     for(int i=1;i<=t;i++){ n         // cout<< Case # <<i<< :  ; n         solve(); n     } n   n     cerr <<  time taken :   << (float)clock() / CLOCKS_PER_SEC <<   secs  << endl; n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n char str[10000]; n int n, m, Q; n map<string, int> mem; n map<string, int>::iterator iter; n struct Func { n   string name; n   int num; n   int v[10]; n   bool operator==(const Func& A) const { n     if (name != A.name) return false; n     if (num != A.num) return false; n     for (int i = 0; i < num; ++i) n       if (v[i] * A.v[i] != 0 && v[i] != A.v[i]) return false; n     return true; n   } n } g[2000], pre; n int Type(string x) { n   if (x ==  T ) return 0; n   if (x ==  int ) return 1; n   if (x ==  double ) return 2; n   if (x ==  string ) return 3; n   return -1; n } n void inputFunc(int gs) { n   gets(str); n   int i = 0, j = 0, len = strlen(str); n   while (str[j] !=  d ) ++j; n   ++j; n   while (j < len) { n     if (str[j] !=    ) str[i++] = str[j]; n     j++; n   } n   len = i; n   i = 0; n   g[gs].name =   ; n   while (str[i] !=  ( ) { n     g[gs].name += str[i]; n     i++; n   } n   string temp =   ; n   i++; n   for (;;) { n     if (str[i] ==  ,  || str[i] ==  ) ) { n       g[gs].v[g[gs].num++] = Type(temp); n       temp =   ; n       if (str[i] ==  ) ) break; n     } else n       temp += str[i]; n     i++; n   } n } n void inputType() { n   string a, b; n   cin >> a >> b; n   mem[b] = Type(a); n } n void solve() { n   gets(str); n   int i = 0, j = 0, len = strlen(str); n   for (j = 0; j < len; ++j) { n     if (str[j] !=    ) str[i++] = str[j]; n   } n   len = i; n   i = 0; n   pre.name =   ; n   pre.num = 0; n   while (str[i] !=  ( ) { n     pre.name += str[i]; n     i++; n   } n   string temp =   ; n   i++; n   for (;;) { n     if (str[i] ==  ,  || str[i] ==  ) ) { n       pre.v[pre.num++] = mem.find(temp)->second; n       temp =   ; n       if (str[i] ==  ) ) break; n     } else n       temp += str[i]; n     i++; n   } n   int ans = 0; n   for (int i = 0; i < n; ++i) n     if (pre == g[i]) ++ans; n   printf( %d n , ans); n } n int main() { n   scanf( %d , &n); n   gets(str); n   for (int i = 0; i < n; ++i) inputFunc(i); n   scanf( %d , &m); n   gets(str); n   mem.clear(); n   while (m--) inputType(); n   scanf( %d , &Q); n   gets(str); n   while (Q--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1000000007; n int main() { n   long long n, a; n   long long fuck = 0, fucck = 0; n   cin >> n; n   priority_queue<int> prio1, prio2; n   for (int i = 0; i < n; ++i) { n     cin >> a; n     prio1.push(a); n     fuck += a; n   } n   for (int i = 0; i < n; ++i) { n     cin >> a; n     prio2.push(a); n     fucck += a; n   } n   prio1.push(-1); n   prio2.push(-1); n   long long sum1 = 0, sum2 = 0; n   int mark = 1; n   for (int i = 0; i < 2 * n; ++i) { n     if (mark == 1) { n       if (prio1.top() >= prio2.top()) { n         fuck -= prio1.top(); n         sum1 += prio1.top(); n         prio1.pop(); n       } else { n         fucck -= prio2.top(); n         prio2.pop(); n       } n       mark = 2; n     } else { n       if (prio2.top() >= prio1.top()) { n         fucck -= prio2.top(); n         sum2 += prio2.top(); n         prio2.pop(); n       } else { n         fuck -= prio1.top(); n         prio1.pop(); n       } n       mark = 1; n     } n   } n   cout << (sum1 - sum2) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n pair<char, int> check(string s) { n   map<char, int> used; n   int l = s.size(), i, maxVal = 0; n   char ans; n   for (i = 0; i < l; i++) { n     used[s.at(i)]++; n     if (used[s.at(i)] > maxVal) { n       maxVal = used[s.at(i)]; n       ans = s.at(i); n     } n   } n   return make_pair(ans, maxVal); n } n int execute() { n   int n, i, diff[4] = {0}, minVal = 10e8 + 7; n   cin >> n; n   vector<pair<char, int> > val(4); n   string a[3]; n   for (i = 0; i < 3; i++) { n     cin >> a[i]; n     val[i] = check(a[i]); n     diff[i] = a[i].size() - val[i].second; n     diff[i] = max(0 + (diff[i] + 1 == n && !diff[i]), diff[i] - n); n     minVal = min(minVal, diff[i]); n   } n   int index; n   for (i = 0; i < 3; i++) { n     if (minVal == diff[i]) { n       index = i; n     } n   } n   sort(diff, diff + 3); n   if (diff[0] == diff[1]) n     cout <<  Draw  << endl; n   else { n     switch (index) { n       case (0): n         cout <<  Kuro  << endl; n         break; n       case (1): n         cout <<  Shiro  << endl; n         break; n       default: n         cout <<  Katie  << endl; n     } n   } n   return 0; n } n char mtest = false; n unsigned short TE = 1; n int main() { n   ios_base::sync_with_stdio(false); n   if (mtest) cin >> TE; n   while (TE--) execute(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 1; n long long a[4], mn = 1e12 + 1; n char b, c, d; n int main() { n   cin >> a[0] >> a[1] >> a[2] >> a[3]; n   cin >> b >> c >> d; n   sort(a, a + 4); n   do { n     long long first, second, temp; n     if (b ==  + ) n       first = a[0] + a[1]; n     else n       first = a[0] * a[1]; n     long long a1[3]; n     a1[0] = first; n     a1[1] = a[2]; n     a1[2] = a[3]; n     sort(a1, a1 + 3); n     do { n       if (c ==  + ) n         second = a1[0] + a1[1]; n       else n         second = a1[0] * a1[1]; n       if (d ==  + ) n         temp = second + a1[2]; n       else n         temp = second * a1[2]; n       mn = min(mn, temp); n     } while (next_permutation(a1, a1 + 3)); n   } while (next_permutation(a, a + 4)); n   cout << mn; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char s[100010]; n typedef struct record { n   char ch; n   int po; n } rec; n int main() { n   int leng, ans, l, r; n   rec tmp1, tmp2; n   int tmp3; n   bool flag; n   stack<rec> st; n   while (~scanf( %s , s)) { n     flag = false; n     while (!st.empty()) st.pop(); n     leng = strlen(s); n     for (int i = 0; i < leng; i++) { n       if (!st.empty()) tmp2 = st.top(); n       if (!st.empty() && ((tmp2.ch ==  (  && s[i] ==  ) ) || n                           (tmp2.ch ==  [  && s[i] ==  ] ))) { n         st.pop(); n       } else { n         tmp1.ch = s[i]; n         tmp1.po = i; n         st.push(tmp1); n       } n     } n     ans = 0; n     if (st.empty()) { n       for (int i = 0; i < leng; i++) n         if (s[i] ==  [ ) ans++; n       printf( %d n%s n , ans, s); n     } else { n       bool once = true; n       flag = false; n       ans = -1; n       int tmpr, tmpl; n       while (st.size() != 0) { n         tmp3 = 0; n         if (once) { n           tmpr = leng - 1; n           tmpl = st.top().po + 1; n           once = false; n         } else { n           if (st.size() != 1) { n             tmpr = st.top().po - 1; n             st.pop(); n             tmpl = st.top().po + 1; n           } else { n             tmpr = st.top().po - 1; n             tmpl = 0; n             st.pop(); n           } n         } n         if (tmpl < tmpr) { n           flag = true; n           for (int j = tmpl; j <= tmpr; j++) n             if (s[j] ==  [ ) tmp3++; n         } n         if (tmp3 > ans) { n           ans = tmp3; n           l = tmpl; n           r = tmpr; n         } n       } n       if (flag) { n         printf( %d n , ans); n         for (int i = l; i <= r; i++) printf( %c , s[i]); n         printf(  n ); n       } else n         printf( 0 n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize(2) n using namespace std; n const int INF = 0x3f3f3f3f; n const long long inf = 0x3f3f3f3f3f3f3f3f; n const int mod = 1e9 + 7; n const int maxn = 1e3 + 5; n int n, m, qq, s[maxn][maxn], f[maxn][maxn]; n bool vis[maxn][maxn]; n int a[4] = {0, 1, 0, -1}; n int b[4] = {-1, 0, 1, 0}; n bool judge(int x, int y) { n   for (int i = 0; i <= 3; i++) { n     int xx = x + a[i], yy = y + b[i]; n     if (xx <= 0 || xx > n || yy <= 0 || yy > m) continue; n     if (s[xx][yy] == s[x][y]) return true; n   } n   return false; n } n void bfs() { n   queue<pair<int, int> > q; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       if (judge(i, j)) { n         vis[i][j] = 1; n         f[i][j] = 0; n         q.push({i, j}); n       } n     } n   } n   while (!q.empty()) { n     int x = q.front().first; n     int y = q.front().second; n     q.pop(); n     for (int i = 0; i <= 3; i++) { n       int xx = x + a[i], yy = y + b[i]; n       if (xx <= 0 || xx > n || yy <= 0 || yy > m) continue; n       if (vis[xx][yy]) continue; n       f[xx][yy] = f[x][y] + 1; n       vis[xx][yy] = 1; n       q.push({xx, yy}); n     } n   } n } n int main() { n   scanf( %d%d%d , &n, &m, &qq); n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       scanf( %1d , &s[i][j]); n     } n   } n   bfs(); n   while (qq--) { n     int x, y; n     long long t; n     scanf( %d%d%lld , &x, &y, &t); n     if (vis[x][y]) { n       if (t < f[x][y]) n         printf( %d n , s[x][y]); n       else n         printf( %d n , s[x][y] ^ ((t - f[x][y]) & 1)); n     } else { n       printf( %d n , s[x][y]); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n vector<int> adj[100005]; n vector<int> vis(100005, 1); n vector<int> ans; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int T = 1; n   for (int qq = 1; qq <= T; qq++) { n     cin >> n >> m; n     for (int i = 0; i < m; i++) { n       int x, y; n       cin >> x >> y; n       adj[x].push_back(y); n       adj[y].push_back(x); n     } n     priority_queue<int> v; n     v.push(-1); n     vis[1] = 0; n     while (!v.empty()) { n       int u = -v.top(); n       v.pop(); n       ans.push_back(u); n       for (auto i : adj[u]) n         if (vis[i]) v.push(-i), vis[i] = 0; n     } n     for (auto i : ans) cout << i <<    ; n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = 1e18 + 1e17; n const long long mod = 1e9 + 7; n long long modInverse(long long a, long long m) { n   long long m0 = m; n   long long y = 0, x = 1; n   if (m == 1) return 0; n   while (a > 1) { n     long long q = a / m; n     long long t = m; n     m = a % m, a = t; n     t = y; n     y = x - q * y; n     x = t; n   } n   if (x < 0) x += m0; n   return x; n } n long long powm(long long a, long long b) { n   a = a % mod; n   long long res = 1; n   while (b) { n     if (b & 1) res = (res * a) % mod; n     a = (a * a) % mod; n     b >>= 1; n   } n   return (res % mod); n } n const long long N = 5e3 + 6; n int n, k, water; n vector<int> v; n int dp[N][2], dpr[N][2]; n int arr[N]; n bool brr[N]; n int ip = 1e9; n void makedp() { n   dp[0][0] = 1; n   dp[0][1] = -1; n   for (int i = 0; i < n; i++) { n     int x = arr[i] % k; n     memset(dpr, -1, sizeof(dpr)); n     dpr[0][0] = 1; n     for (int j = 1; j < k; j++) { n       if (dp[j][0] == 0 && dp[(j - x + k) % k][0] == 1) { n         dpr[j][0] = 1; n         dpr[j][1] = i; n       } else { n         dpr[j][0] = dp[j][0]; n         dpr[j][1] = dp[j][1]; n       } n     } n     swap(dpr, dp); n   } n } n bool check() { n   int tt = 0; n   for (int i = 0; i < n; i++) tt += arr[i]; n   if (tt < water || dp[water % k][0] == 0) { n     cout <<  NO n ; n     return true; n     ; n   } n   return false; n } n void find() { n   int val = water % k; n   int x = dp[val][1]; n   while (x != -1) { n     v.push_back(dp[val][1]); n     val = (val - arr[x] % k + k) % k; n     x = dp[val][1]; n   } n } n void seperate() { n   memset(brr, 0, sizeof(brr)); n   int tt = 0; n   for (int i : v) { n     brr[i] = 1; n     tt += arr[i]; n   } n   for (int i = 1; i < v.size(); i++) n     cout << ip <<     << v[i] + 1 <<     << v[0] + 1 <<   n ; n   int mark = -1; n   int tc = 0; n   for (int i = 0; i < n; i++) n     if (!brr[i]) { n       mark = i; n       tc += arr[i]; n     } n   for (int i = 0; i < n; i++) n     if (!brr[i] && mark != i) n       cout << ip <<     << i + 1 <<     << mark + 1 <<   n ; n   if (v.size() == 0) v.push_back(0); n   if (tt > water) n     cout << (tt - water) / k <<     << v[0] + 1 <<     << (0 == v[0] ? 2 : 1) n          <<   n ; n   else if (tt < water) n     cout << (water - tt) / k <<     << mark + 1 <<     << v[0] + 1 <<   n ; n } n void solve() { n   cin >> n >> k >> water; n   for (int i = 0; i < n; i++) cin >> arr[i]; n   makedp(); n   if (check()) return; n   cout <<  YES n ; n   find(); n   seperate(); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long t = 1; n   for (long long i = 1; i <= t; i++) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int freq[27]; n char sent[101]; n int main() { n   int n; n   scanf( %d , &n); n   scanf( %s , sent); n   while (n--) { n     if (isupper(sent[n])) sent[n] = tolower(sent[n]); n     freq[sent[n] -  a ]++; n   } n   for (int i = 0; i < 26; i++) n     if (!freq[i]) { n       printf( NO n ); n       return 0; n     } n   printf( YES n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 5; n const int INF = 1e9 + 5; n int read(int base = 10) { n   int x = 0; n   char ch = getchar(); n   while (!isdigit(ch)) ch = getchar(); n   while (isdigit(ch)) x = x * base + ch -  0 , ch = getchar(); n   return x; n } n struct Node { n   int num[12][2]; n } tree[N << 2]; n void push_up(int o, int l, int r) { n   int tmp[4]; n   for (int ii = 1; ii <= 11; ii++) { n     tmp[0] = tree[o << 1].num[ii][0]; n     tmp[1] = tree[o << 1].num[ii][1]; n     tmp[2] = tree[o << 1 | 1].num[ii][0]; n     tmp[3] = tree[o << 1 | 1].num[ii][1]; n     sort(tmp, tmp + 4); n     tree[o].num[ii][0] = tmp[0]; n     tree[o].num[ii][1] = tmp[1]; n   } n } n void build(int o, int l, int r) { n   if (l == r) { n     int x = read(); n     int tmp = x; n     for (int ii = 1; ii <= 11; tmp /= 10, ii++) { n       if (tmp % 10 == 0) { n         tree[o].num[ii][0] = tree[o].num[ii][1] = INF; n         continue; n       } n       tree[o].num[ii][0] = x; n       tree[o].num[ii][1] = INF; n     } n     return; n   } n   int mid = (l + r) >> 1; n   build(o << 1, l, mid); n   build(o << 1 | 1, mid + 1, r); n   push_up(o, l, r); n } n int id, x; n void update(int o, int l, int r) { n   if (l == r) { n     int tmp = x; n     for (int ii = 1; ii <= 11; ii++, tmp /= 10) { n       if (tmp % 10) { n         tree[o].num[ii][0] = x; n         tree[o].num[ii][1] = INF; n       } else { n         tree[o].num[ii][0] = tree[o].num[ii][1] = INF; n       } n     } n     return; n   } n   int mid = (l + r) >> 1; n   if (id <= mid) n     update(o << 1, l, mid); n   else n     update(o << 1 | 1, mid + 1, r); n   push_up(o, l, r); n } n int L, R, ans[12][2]; n int tmp[4]; n void query_up(int o, int i) { n   tmp[0] = ans[i][0]; n   tmp[1] = ans[i][1]; n   tmp[2] = tree[o].num[i][0]; n   tmp[3] = tree[o].num[i][1]; n   sort(tmp, tmp + 4); n   ans[i][0] = tmp[0]; n   ans[i][1] = tmp[1]; n } n void query(int o, int l, int r) { n   if (L <= l && r <= R) { n     for (int ii = 1; ii <= 11; ii++) { n       query_up(o, ii); n     } n     return; n   } n   int mid = (l + r) >> 1; n   if (L <= mid) query(o << 1, l, mid); n   if (R > mid) query(o << 1 | 1, mid + 1, r); n } n int main() { n   int n = read(), m = read(); n   build(1, 1, n); n   while (m--) { n     int op = read(); n     if (op == 1) { n       id = read(), x = read(); n       update(1, 1, n); n     } else { n       L = read(), R = read(); n       for (int i = 1; i <= 11; i++) ans[i][0] = ans[i][1] = INF; n       query(1, 1, n); n       int all = INF * 2; n       ; n       for (int i = 1; i <= 11; i++) { n         if (ans[i][0] == INF || ans[i][1] == INF) continue; n         all = min(all, ans[i][0] + ans[i][1]); n       } n       if (all == INF * 2) { n         cout <<  -1 n ; n       } else { n         cout << all <<   n ; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <typename Arg1> n void __f(const char* name, Arg1&& arg1) { n   cerr << name <<  :   << arg1 << endl; n } n template <typename Arg1, typename... Args> n void __f(const char* names, Arg1&& arg1, Args&&... args) { n   const char* comma = strchr(names + 1,  , ); n   cerr.write(names, comma - names) <<  :   << arg1 <<   | ; n   __f(comma + 1, args...); n } n const int INF = 1 << 30; n const int MOD = 1e9 + 7; n const int N = 1e6; n int a[N]; n double sqr(double x) { return x * x; } n int main() { n   int cas; n   scanf( %d , &cas); n   double ret = 0; n   for (int num = 0; num < cas; ++num) { n     int n = 250; n     double ave = 0; n     for (int i = 0; i < n; ++i) { n       scanf( %d , &a[i]); n       ave += a[i]; n     } n     ave /= n; n     double V = 0; n     for (int i = 0; i < n; ++i) { n       V += sqr(ave - a[i]); n     } n     V /= n; n     double ratio = ave / V; n     ret = max(ret, abs(ratio - 1)); n     puts(abs(ratio - 1) < 0.5 ?  poisson  :  uniform ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   string s; n   cin >> s; n   int ans = 0; n   for (int i = 1; i < n; i++) n     if (s[i - 1] ==  .  && s[i] ==  . ) ans++; n   while (m--) { n     int k; n     char c; n     cin >> k >> c; n     if (c != s[k - 1]) { n       if (c ==  . ) { n         if (k > 1 && s[k - 2] ==  . ) ans++; n         if (k < n && s[k] ==  . ) ans++; n       } n       if (s[k - 1] ==  . ) { n         if (k > 1 && s[k - 2] ==  . ) ans--; n         if (k < n && s[k] ==  . ) ans--; n       } n       s[k - 1] = c; n     } n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int me; n   int bebia; n   int iliko = 0; n   int ilarioni = 0; n   cin >> me; n   for (int i = 0; i < me; i++) { n     cin >> bebia; n     if (bebia > 0) iliko += bebia; n     if (bebia == -1) { n       if (iliko - 1 < 0) { n         ilarioni++; n       } else n         iliko--; n     } n   } n   cout << ilarioni; n } n


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


#include <bits/stdc++.h> n using namespace std; n int rx[4] = {0, 0, 1, -1}; n int ry[4] = {-1, 1, 0, 0}; n int main() { n   int n, k, a, b, u, v; n   cin >> n >> k; n   cin >> a >> b >> u >> v; n   if (k < n + 1 || n == 4) { n     cout << -1 << endl; n     return 0; n   } n   int ind1, ind2; n   vector<int> p(n + 1); n   p[1] = a; n   int ct = 2, i = 1; n   while (ct <= n - 1) { n     if (i == a || i == b) { n       i++; n       continue; n     } n     p[ct] = i; n     ct++; n     i++; n   } n   p[n] = b; n   for (int i = 1; i < n - 2; i++) { n     if ((p[i] == u && p[i + 1] == v) || (p[i] == v && p[i + 1] == u)) { n       swap(p[i + 1], p[i + 2]); n     } n   } n   if ((p[n - 2] == u && p[n - 1] == v) || (p[n - 2] == v && p[n - 1] == u)) { n     swap(p[n - 3], p[n - 2]); n   } n   for (int i = 1; i <= n; i++) { n     if (p[i] == u) { n       ind1 = i; n     } n     if (p[i] == v) { n       ind2 = i; n     } n   } n   for (int i = 1; i <= n; i++) { n     cout << p[i] <<    ; n   } n   cout << endl; n   if (u < v) { n     int s = ind1; n     while (s >= 1) { n       cout << p[s] <<    ; n       s--; n     } n     s = ind1 + 1; n     while (s < ind2) { n       cout << p[s] <<    ; n       s++; n     } n     s = n; n     while (s >= ind2) { n       cout << p[s] <<    ; n       s--; n     } n   } else { n     int s = ind1; n     while (s <= n) { n       cout << p[s] <<    ; n       s++; n     } n     s = ind1 - 1; n     while (s > ind2) { n       cout << p[s] <<    ; n       s--; n     } n     s = 1; n     while (s <= ind2) { n       cout << p[s] <<    ; n       s++; n     } n   } n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long a, b, c, d; n   cin >> a >> b >> c >> d; n   long long sum = a + b + c + d; n   if (sum % 2 == 0) { n     if (a + b == sum / 2 || a + c == sum / 2 || a + d == sum / 2 || n         b + c == sum / 2 || b + d == sum / 2 || c + d == sum / 2 || n         a == sum / 2 || b == sum / 2 || c == sum / 2 || d == sum / 2) n       cout <<  YES  << endl; n     else n       cout <<  NO  << endl; n   } else n     cout <<  NO  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int i, j, r, n, x; n   cin >> x; n   r = x % 4; n   if (r == 0) { n     cout <<  1  n          <<   A ; n   } n   if (r == 1) { n     cout <<  0  n          <<   A ; n   } n   if (r == 2) { n     cout <<  1  n          <<   B ; n   } n   if (r == 3) { n     cout <<  2  n          <<   A ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline bool SR(int &x) { return scanf( %d , &x) == 1; } n inline bool SR(long long &x) { return scanf( %lld , &x) == 1; } n inline bool SR(double &x) { return scanf( %lf , &x); } n inline bool SR(char *s) { return scanf( %s , s); } n inline bool RI() { return 1; } n template <typename I, typename... T> n bool RI(I &x, T &...tail) { n   return SR(x) && RI(tail...); n } n inline void SP(const int x) { printf( %d , x); } n inline void SP(const long long x) { printf( %lld , x); } n inline void SP(const double x) { printf( %.10f , x); } n inline void SP(const char *s) { printf( %s , s); } n inline void PL() { puts(  ); } n template <typename I, typename... T> n void PL(const I x, const T... tail) { n   SP(x); n   if (sizeof...(tail)) putchar(   ); n   PL(tail...); n } n template <typename I> n inline void _DOING(const char *s, I &&x) { n   cerr << s <<   =   << x <<   n ; n } n template <typename I, typename... T> n void _DOING(const char *s, I &&x, T &&...tail) { n   int c = 0; n   while (*s !=  ,  || c != 0) { n     if (*s ==  (  || *s ==  [  || *s ==  { ) c++; n     if (*s ==  )  || *s ==  ]  || *s ==  } ) c--; n     cerr << *s++; n   } n   cerr <<   =   << x <<   ,  ; n   _DOING(s + 1, tail...); n } n const int INF = 0x3f3f3f3f; n const long long INF64 = 0x3f3f3f3f3f3f3f3fll; n inline int RAND() { n   static int x = 880301; n   return x = (x * 0xdefacedll + 1ll) % 0x7fffffffll; n } n int n; n map<pair<int, int>, int> rd; n inline bool check(long long a, int b) { n   long long x = 1; n   while (b) { n     if (a >= n) return 0; n     if (b & 1) x = x * a; n     if (x >= n) return 0; n     a = a * a; n     b >>= 1; n   } n   return 1; n } n int dfs(int a, int b) { n   if (rd.count(make_pair(a, b))) return rd[make_pair(a, b)]; n   if (!check(a, b)) return rd[make_pair(a, b)] = 1; n   if (a == 1) { n     if (!check(a + 1, b)) n       return rd[make_pair(a, b)] = 0; n     else if (dfs(a + 1, b) == -1 || dfs(a, b + 1) == -1) n       return rd[make_pair(a, b)] = 1; n     else if (dfs(a + 1, b) == 0 || dfs(a, b + 1) == 0) n       return rd[make_pair(a, b)] = 0; n     else n       return rd[make_pair(a, b)] = -1; n   } else if (b == 1) { n     if (1ll * a * a >= n) { n       if ((n - a) & 1) n         return rd[make_pair(a, b)] = -1; n       else n         return rd[make_pair(a, b)] = 1; n     } else if (dfs(a + 1, b) == -1 || dfs(a, b + 1) == -1) n       return rd[make_pair(a, b)] = 1; n     else if (dfs(a + 1, b) == 0 || dfs(a, b + 1) == 0) n       return rd[make_pair(a, b)] = 0; n     else n       return rd[make_pair(a, b)] = -1; n   } else { n     if (dfs(a + 1, b) == -1 || dfs(a, b + 1) == -1) n       return rd[make_pair(a, b)] = 1; n     else n       return rd[make_pair(a, b)] = -1; n   } n } n int main() { n   int a, b; n   RI(a, b, n); n   int res = dfs(a, b); n   if (res == 1) n     PL( Masha ); n   else if (res == -1) n     PL( Stas ); n   else n     PL( Missing ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxN = 50000 + 10; n int n; n int B[maxN]; n vector<pair<int, int>> VB; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %d , &B[i]); n   for (int i = 0; i < n; i++) { n     VB.push_back(make_pair(B[i] + B[(i + 1) % n], i)); n   } n   sort(VB.begin(), VB.end()); n   for (int i = 0; i < n; i++) { n     auto p = VB[i]; n     B[p.second] = i; n   } n   for (int i = 0; i < n; i++) printf( %d%c , B[i], (i == n - 1) ?   n  :    ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int arr[5001], k = 0; n   string s, in; n   stack<int> s1; n   while (getline(cin, s)) { n     in = in + s; n   } n   for (int i = 0; i < in.size(); i++) { n     char st[5001] = {  0 }; n     if (in[i] ==  < ) { n       i++; n       int j = 0; n       while (in[i] !=  > ) { n         st[j++] = in[i]; n         i++; n       } n     } n     string st1 =  table , st2 =  tr , st3 =  td , st4 =  /table , st5 =  /tr , n            st6 =  /td ; n     if (st1.compare(st) == 0) arr[k++] = 1; n     if (st2.compare(st) == 0) arr[k++] = 2; n     if (st3.compare(st) == 0) arr[k++] = 3; n     if (st4.compare(st) == 0) arr[k++] = 4; n     if (st5.compare(st) == 0) arr[k++] = 5; n     if (st6.compare(st) == 0) arr[k++] = 6; n   } n   int z = -1, max = z; n   vector<int> v, v1; n   for (int i = 0; i < k; i++) { n     if (arr[i] == 1) { n       s1.push(arr[i]); n       v.push_back(0); n     } n     if (arr[i] == 2) s1.push(arr[i]); n     if (arr[i] == 3) s1.push(arr[i]); n     if (arr[i] == 4) { n       s1.pop(); n       v1.push_back(v.back()); n       v.pop_back(); n     } n     if (arr[i] == 5) s1.pop(); n     if (arr[i] == 6) { n       s1.pop(); n       v.back()++; n     } n   } n   sort(v1.begin(), v1.end()); n   for (int i = 0; i < v1.size(); i++) cout << v1[i] <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int a, b, c; n   int a1, b1, c1; n   int s = 0; n   scanf( %d%d%d%d%d%d , &a, &b, &c, &a1, &b1, &c1); n   if (a > a1) { n     s = s + ((a - a1) / 2); n   } else n     s = s - (a1 - a); n   if (b > b1) { n     s = s + ((b - b1) / 2); n   } else n     s = s - (b1 - b); n   if (c > c1) { n     s = s + ((c - c1) / 2); n   } else n     s = s - (c1 - c); n   if (s >= 0) { n     printf( YES ); n   } else n     printf( NO ); n } n


#include <bits/stdc++.h> n using namespace std; n int main(int argc, char *argv[]) { n   int n, tmp = 1, res = 1; n   cin >> n; n   int A[n][2]; n   for (int i = 0; i < n; i++) cin >> A[i][0] >> A[i][1]; n   for (int i = 0; i < n - 1; i++) { n     if ((A[i][0] == A[i + 1][0]) && (A[i][1] == A[i + 1][1])) { n       tmp++; n       if (res < tmp) res = tmp; n     } else n       tmp = 1; n   } n   cout << res << endl; n   return EXIT_SUCCESS; n } n


#include <bits/stdc++.h> n using namespace std; n unsigned long long int expo_fast(long long int a, long long int b) { n   a = a; n   long long int result = 1; n   while (b) { n     if (b & 1) result = (result * a); n     b >>= 1; n     a = (a * a); n   } n   return (result); n } n void take_in(vector<long long int> *arr) { n   for (int i = 0; i < arr->size(); i++) cin >> (*(arr))[i]; n } n long long int gcd(long long int a, long long int b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n unsigned long long int power(long long int x, unsigned long long int y, n                              long long int p) { n   long long int res = 1; n   x = x % p; n   while (y > 0) { n     if (y & 1) res = (res * x) % p; n     y = y >> 1; n     x = (x * x) % p; n   } n   return res; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int n, x1, x2, done = 0; n   cin >> n >> x1 >> x2; n   vector<pair<long long int, long long int> > arr; n   vector<long long int> dp(n + 1); n   for (int i = 0; i < n; i++) { n     long long int x; n     cin >> x; n     arr.push_back({x, i}); n   } n   sort(arr.begin(), arr.end()); n   dp[n] = arr[n - 1].first; n   for (int i = n - 1; i > 0; i--) { n     dp[i] = max(dp[i + 1], arr[i - 1].first * (n - i + 1)); n   } n   auto bs = [n](long long int x, long long int tar) -> long long int { n     int low = 1, high = n, ans = n + 1; n     while (low <= high) { n       int mid = (low + high) / 2; n       if (x * mid >= tar) { n         ans = mid; n         high = mid - 1; n       } else n         low = mid + 1; n     } n     return ans; n   }; n   for (int i = 1; i <= n; i++) { n     vector<int> indx; n     long long int len = bs(arr[i - 1].first, x1); n     if (i + len - 1 < n && dp[i + len] >= x2) { n       done = 1; n       cout <<  Yes n ; n       for (int j = n; j >= 1; j--) { n         if (dp[j] >= x2) { n           indx.push_back(j); n           indx.push_back(n); n           break; n         } n       } n       cout << len <<     << indx[1] - indx[0] + 1 <<   n ; n       for (int k = i; k < i + len; k++) cout << arr[k - 1].second + 1 <<    ; n       cout <<   n ; n       for (int k = indx[0]; k <= n; k++) cout << arr[k - 1].second + 1 <<    ; n       break; n     } n     if (!done) { n       len = bs(arr[i - 1].first, x2); n       if (i + len - 1 < n && dp[i + len] >= x1) { n         done = 1; n         cout <<  Yes n ; n         for (int j = n; j >= 1; j--) { n           if (dp[j] >= x1) { n             indx.push_back(j); n             indx.push_back(n); n             break; n           } n         } n         cout << indx[1] - indx[0] + 1 <<     << len <<   n ; n         for (int k = indx[0]; k <= n; k++) cout << arr[k - 1].second + 1 <<    ; n         cout <<   n ; n         for (int k = i; k < i + len; k++) cout << arr[k - 1].second + 1 <<    ; n         break; n       } n     } n   } n   if (!done) cout <<  No ; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, k, l[100100], c[100100]; n vector<int> g[100100]; n vector<int> s; n void dfs(int t) { n   s.push_back(t); n   l[t] = s.size(); n   for (auto v : g[t]) { n     if (!l[v]) n       dfs(v); n     else { n       if (l[t] - l[v] + 1 >= k) { n         printf( 2 n%d n , l[t] - l[v] + 1); n         for (int i = l[v] - 1; i < l[t]; i++) printf( %d  , s[i]); n         exit(0); n       } n     } n   } n   if (!c[t]) n     for (auto v : g[t]) c[v] = 1; n   s.pop_back(); n } n int main() { n   int i, a, b; n   scanf( %d %d , &n, &m); n   for (i = 0; i < m; i++) { n     scanf( %d %d , &a, &b); n     g[a].push_back(b); n     g[b].push_back(a); n   } n   while (k * k < n) k++; n   dfs(1); n   printf( 1 n ); n   for (i = 1; k; i++) { n     if (!c[i]) { n       printf( %d  , i); n       k--; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> v[200008]; n int val[200008]; n int n, tk; n int dp[200008]; n int lim; n int siz[200008]; n int cnt, tot; n int no[200008]; n void dfs(int x, int p) { n   no[x] = val[x] < lim; n   dp[x] = 0; n   int b1 = 0, b2 = 0, b3 = 0; n   siz[x] = 1; n   for (int i = 0; i < v[x].size(); i++) { n     int y = v[x][i]; n     if (y == p) continue; n     dfs(y, x); n     siz[x] += siz[y], no[x] += no[y]; n     if (no[y] == 0) n       b3 += siz[y]; n     else { n       if (dp[y] > b1) n         b2 = b1, b1 = dp[y]; n       else n         b2 = max(b2, dp[y]); n     } n   } n   if (val[x] < lim) return; n   dp[x] = b1 + b3 + 1; n   if (tot == no[x]) n     cnt = max(cnt, 1 + n - siz[x] + b1 + b2 + b3); n   else n     cnt = max(cnt, 1 + b1 + b2 + b3); n } n int check(int mid) { n   lim = mid; n   cnt = 0; n   tot = 0; n   for (int i = 1; i <= n; i++) n     if (val[i] < lim) tot++; n   dfs(1, 0); n   return cnt >= tk; n } n int main() { n   scanf( %d%d , &n, &tk); n   for (int i = 1; i <= n; i++) scanf( %d , &val[i]); n   for (int i = 1; i < n; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     v[x].push_back(y); n     v[y].push_back(x); n   } n   int l = 0, r = 1000000; n   int ans = -1; n   while (l <= r) { n     int mid = (l + r) / 2; n     if (check(mid)) n       ans = mid, l = mid + 1; n     else n       r = mid - 1; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int mini(long long int p, long long int q) { n   if (p < q) return p; n   return q; n } n int main() { n   long long int k2, k3, k5, k6, total = 0; n   cin >> k2 >> k3 >> k5 >> k6; n   int m = 0; n   m = mini(k2, k5); n   m = mini(m, k6); n   total = m * 256; n   m = mini(k2 - m, k3); n   total += m * 32; n   cout << total << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = 2.0 * acos(0.0); n inline int getInt() { n   int x; n   scanf( %d , &x); n   return x; n } n inline long long getLL() { n   long long x; n   scanf( %lld , &x); n   return x; n }; n inline int NUM(char c) { return (int)c - 48; } n inline char CHR(int n) { return (char)(n + 48); } n template <class T> n inline T MAX(T a, T b) { n   if (a > b) return a; n   return b; n } n template <class T> n inline T MIN(T a, T b) { n   if (a < b) return a; n   return b; n } n inline void OPEN(const string &s) { n   freopen((s +  .in ).c_str(),  r , stdin); n   freopen((s +  .out ).c_str(),  w , stdout); n } n map<string, int> score, sco, mis; n map<string, int> times; n int main() { n   vector<string> v; n   for (int k = 0, _n = (5); k < _n; k++) { n     string A, B, C; n     cin >> A >> B; n     cin >> C; n     for (int i = 0, _n = ((C).size()); i < _n; i++) n       if (C[i] ==  : ) C[i] =    ; n     stringstream ss(C); n     int s, m; n     ss >> s >> m; n     if (s > m) n       score[A] += 3; n     else if (s < m) n       score[B] += 3; n     else { n       score[A]++; n       score[B]++; n     } n     sco[A] += s; n     mis[A] += m; n     sco[B] += m; n     mis[B] += s; n     v.push_back(A); n     v.push_back(B); n     times[A]++; n     times[B]++; n   } n   sort((v).begin(), (v).end()); n   v.resize(unique((v).begin(), (v).end()) - v.begin()); n   string OTHER; n   for (int i = 0, _n = ((v).size()); i < _n; i++) { n     if (times[v[i]] == 2 && v[i] !=  BERLAND ) OTHER = v[i]; n   } n   vector<pair<int, pair<int, pair<int, string> > > > sorted; n   for (int d = (1), _b = (50); d <= _b; d++) { n     for (int m = (0), _b = (50); m <= _b; m++) { n       int s = m + d; n       string A, B; n       A =  BERLAND , B = OTHER; n       score[A] += 3; n       sco[A] += s; n       sco[B] += m; n       mis[B] += s; n       mis[A] += m; n       sorted.clear(); n       for (int i = 0, _n = ((v).size()); i < _n; i++) { n         sorted.push_back(make_pair( n             -score[v[i]], n             make_pair(-sco[v[i]] + mis[v[i]], make_pair(-sco[v[i]], v[i])))); n       } n       sort((sorted).begin(), (sorted).end()); n       if (sorted[0].second.second.second ==  BERLAND  || n           sorted[1].second.second.second ==  BERLAND ) { n         cout << s <<  :  << m << endl; n         return 0; n       } n       score[A] -= 3; n       sco[A] -= s; n       sco[B] -= m; n       mis[B] -= s; n       mis[A] -= m; n     } n   } n   cout <<  IMPOSSIBLE  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 1 << 20, KOR = 32000; n const long long INF = 1e18; n int n; n int p[MAX]; n int sito[KOR]; n int bezp[MAX], sp[MAX]; n int cijseg, cijprom; n vector<int> Pr; n void Eratosten() { n   for (int i = 2; i < KOR; i++) { n     if (!sito[i]) { n       Pr.push_back(i); n       for (int j = 2 * i; j < KOR; j += i) sito[j] = i; n     } n   } n } n vector<int> Fak(int x) { n   vector<int> R; n   for (int i = 0; i < (int)Pr.size(); i++) { n     if (!(x % Pr[i])) R.push_back(Pr[i]); n     for (; !(x % Pr[i]); x /= Pr[i]) n       ; n   } n   if (x > 1) R.push_back(x); n   return R; n } n long long Rijesi(int x) { n   memset(bezp, 0, sizeof bezp); n   memset(sp, 0, sizeof sp); n   int prvi = n, zadnji = 0; n   int ukprom = 0; n   for (int i = 0; i < n; i++) { n     if (!(p[i] % x)) bezp[i] = 1; n     if (!((p[i] + 1) % x) || !((p[i] - 1) % x)) sp[i] = 1; n     if (!bezp[i] && !sp[i]) { n       prvi = min(prvi, i); n       zadnji = max(zadnji, i); n     } n     if (!bezp[i] && sp[i]) ukprom++; n   } n   if (!ukprom && prvi == n) return 0; n   long long ret = INF; n   if (prvi == n) ret = (long long)cijprom * ukprom; n   long long maxx = -cijseg; n   int tprom = 0; n   for (int i = 0; i < n; i++) { n     if (!bezp[i] && sp[i]) tprom++; n     if (i >= zadnji) n       ret = min(ret, (long long)i * cijseg - maxx + n                          ((long long)ukprom - tprom) * cijprom); n     if (i < prvi) n       maxx = max(maxx, (long long)-tprom * cijprom + (long long)i * cijseg); n   } n   return ret; n } n int main() { n   Eratosten(); n   scanf( %d%d%d , &n, &cijseg, &cijprom); n   for (int i = 0; i < n; i++) scanf( %d , &p[i]); n   long long rje = INF; n   vector<int> Bitni; n   for (int i = -1; i <= 1; i++) { n     vector<int> T = Fak(p[0] + i); n     for (int j = 0; j < (int)T.size(); j++) Bitni.push_back(T[j]); n   } n   for (int i = -1; i <= 1; i++) { n     vector<int> T = Fak(p[n - 1] + i); n     for (int j = 0; j < (int)T.size(); j++) Bitni.push_back(T[j]); n   } n   sort(Bitni.begin(), Bitni.end()); n   Bitni.resize(unique(Bitni.begin(), Bitni.end()) - Bitni.begin()); n   for (int i = 0; i < (int)Bitni.size(); i++) rje = min(rje, Rijesi(Bitni[i])); n   printf( %lld n , rje); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1000006, MOD = 1e9 + 7, INF = 1e16; n long long a[N], b, n, m, k, g[105][105], q, x, ans, Ans, pre[N], l, r; n string s; n pair<int, int> p[N]; n int main() { n   ios_base::sync_with_stdio(false), cin.tie(0); n   cin >> n >> m; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) cin >> g[i][j]; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) n       if (g[i][j] < 1) x = 1; n   while (!x) { n     Ans++; n     q += min(n, m); n     for (int i = 1; i <= n; i++) n       for (int j = 1; j <= m; j++) { n         g[i][j]--; n         if (g[i][j] < 1) x = 1; n       } n   } n   ans = 0; n   x = 0; n   for (int i = 1; i <= n; i++) n     if (!g[i][1]) x = 1; n   while (!x) { n     ans++, r++; n     for (int i = 1; i <= n; i++) { n       g[i][1]--; n       if (!g[i][1]) x = 1; n     } n   } n   x = 0; n   for (int i = 1; i <= m; i++) n     if (!g[1][i]) x = 1; n   while (!x) { n     ans++, l++; n     for (int i = 1; i <= m; i++) { n       g[1][i]--; n       if (!g[1][i]) x = 1; n     } n   } n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) n       if (g[i][j] != g[1][j] + g[i][1]) { n         cout << -1; n         return 0; n       } n   for (int i = 1; i <= n; i++) ans += g[i][1]; n   for (int i = 2; i <= m; i++) ans += g[1][i]; n   cout << ans + q << endl; n   for (int i = 1; i <= l; i++) n     cout <<  row  n          <<     << 1 << endl; n   for (int i = 1; i <= r; i++) n     cout <<  col  n          <<     << 1 << endl; n   for (int i = 1; i <= n; i++) n     for (int u = 1; u <= g[i][1]; u++) n       cout <<  row  n            <<     << i << endl; n   for (int i = 1; i <= m; i++) n     for (int u = 1; u <= g[1][i]; u++) n       cout <<  col  n            <<     << i << endl; n   if (n < m) n     s =  row ; n   else n     s =  col ; n   for (int i = 0; i < min(n, m) * Ans; i++) n     cout << s <<     << i % min(n, m) + 1 << endl; n } n


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


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n   int n, k; n   cin >> n >> k; n   int i = 1; n   for (i = k - 1; i > 0; i--) { n     if ((n % i) == 0) break; n   } n   if (i == 0) i = 1; n   int x = (n / i) * k; n   for (; x % k != i; x++) { n   } n   cout << x; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n const ll INF = 4e18; n const int MAXN = 1.1e5; n ll T[MAXN]; n int main() { n   ios::sync_with_stdio(0), cin.tie(0); n   ll N, M, A, D; n   cin >> N >> M >> A >> D; n   for (ll i = 0; i < M; i++) { n     cin >> T[i]; n   } n   sort(T, T + M); n   T[M] = INF; n   auto getEmployee = [&](ll i) -> ll { return i <= N ? i * A : INF; }; n   ll est = 1; n   ll cst = 0; n   ll ans = 0; n   while (est <= N || cst < M) { n     ll nxt = D + min(getEmployee(est), T[cst]); n     ll een = min(N + 1, (nxt / A) + 1); n     ll cen = ll(upper_bound(T, T + M, nxt) - T); n     ans++; n     if (cst == cen) { n       ll delta = een - est; n       assert(delta > 0); n       ll tgt = min(N + 1, T[cst] / A); n       ll extra = max(tgt - een, ll(0)) / delta; n       ans += extra; n       een += extra * delta; n     } n     assert(een > est || cen > cst); n     assert(est <= een && cst <= cen); n     tie(est, cst) = {een, cen}; n   } n   cout << ans <<   n ; n   return 0; n } n


#include <iostream> n #include <cstring> n using namespace std; n int a[10005],s[10005]; n signed main() { n   int T; n   cin >> T; n   while(T--) n   { n     int n; n     cin >> n; n     memset(s,0,sizeof s); n     for(int i=1;i<=n;i++) cin >> a[i]; n     long long ans=0; n     for(int i=1;i<=n;i++) n     { n       ans+=max(0,a[i]-s[i]-1); n       s[i]=max(s[i],a[i]-1); n       for(int j=i+2;j<=min(n,i+a[i]);j++) n         ++s[j]; n       s[i+1]+=max(0,s[i]-a[i]+1); n     } n     cout << ans <<   n ; n   } n   return 0; n }


#include <bits/stdc++.h> n using namespace std; n int l[51], r[51], a[51], c[51], dp[51][51][51]; n void inline FilterRestrictions(int i, int j, int h, vector<int> &v) { n   vector<int> u; n   for (int x : v) { n     if (r[x] < i || j < l[x] || a[x] >= h) { n       continue; n     } n     u.push_back(x); n   } n   v = u; n   u.clear(); n   sort(v.begin(), v.end(), [](int i, int j) { return l[i] > l[j]; }); n } n int Solve(int i, int j, int h, vector<int> v) { n   if (j < i) { n     return 0; n   } n   if (dp[i][j][h] != -1) { n     return dp[i][j][h]; n   } n   FilterRestrictions(i, j, h, v); n   int ans = Solve(i, j, h - 1, v), cost = h * h; n   vector<vector<int> > V(51 + 1); n   vector<int> v1; n   for (int x = i; x <= j; ++x) { n     for (; !v.empty() && l[v.back()] == x; v.pop_back()) { n       int k = v.back(); n       cost -= c[k]; n       V[r[k] + 1].push_back(k); n     } n     for (auto k : V[x]) { n       cost += c[k]; n       v1.push_back(k); n     } n     int ansx = cost + Solve(i, x - 1, h, v1) + Solve(x + 1, j, h, v); n     ans = max(ans, ansx); n   } n   dp[i][j][h] = ans; n   return ans; n } n int main() { n   int n, h, m; n   cin >> n >> h >> m; n   for (int i = 1; i <= m; ++i) { n     cin >> l[i] >> r[i] >> a[i] >> c[i]; n   } n   vector<int> v; n   for (int i = 1; i <= m; ++i) { n     v.push_back(i); n   } n   for (int i = 1; i <= n; ++i) { n     for (int j = i; j <= n; ++j) { n       for (int x = 1; x <= h; ++x) { n         dp[i][j][x] = -1; n       } n     } n   } n   cout << Solve(1, n, h, v) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC optimize( unroll-loops ) n #pragma GCC target(   n      sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native ) n using namespace std; n template <typename T1, typename T2> n void ckmin(T1 &a, T2 b) { n   if (a > b) a = b; n } n template <typename T1, typename T2> n void ckmax(T1 &a, T2 b) { n   if (a < b) a = b; n } n int read() { n   int x = 0, f = 0; n   char ch = getchar(); n   while (!isdigit(ch)) f |= ch ==  - , ch = getchar(); n   while (isdigit(ch)) x = 10 * x + ch -  0 , ch = getchar(); n   return f ? -x : x; n } n template <typename T> n void print(T x) { n   if (x < 0) putchar( - ), x = -x; n   if (x >= 10) print(x / 10); n   putchar(x % 10 +  0 ); n } n template <typename T> n void print(T x, char let) { n   print(x), putchar(let); n } n const int mlen = 1 << 20; n const int N = mlen + 5; n const int mod = 998244353; n unsigned long long omgs[N]; n int rev[N]; n int qpow(int a, int b = mod - 2) { n   int res = 1; n   while (b) { n     if (b & 1) res = 1ll * res * a % mod; n     a = 1ll * a * a % mod; n     b >>= 1; n   } n   return res; n } n const int G = qpow(3, (mod - 1) / mlen); n void setup() { n   omgs[mlen / 2] = 1; n   for (int i = mlen / 2 + 1; i < mlen; i++) omgs[i] = G * omgs[i - 1] % mod; n   for (int i = mlen / 2 - 1; i > 0; i--) omgs[i] = omgs[i << 1]; n } n int getR(int len) { n   int lim = 1, L = 0; n   while (lim <= len) lim <<= 1, L++; n   for (int i = 0; i < lim; i++) n     rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << L - 1); n   return lim; n } n inline void dft(vector<int> &a, int lim) { n   a.resize(lim); n   static unsigned long long A[N]; n   for (int i = 0; i < lim; i++) A[rev[i]] = a[i]; n   for (int mid = 1; mid < lim; mid <<= 1) { n     for (int i = 0; i < lim; i += mid << 1) { n       unsigned long long *W = omgs + mid; n       for (int j = 0; j < mid; j++) { n         unsigned long long tmp = *(W++) * A[i + j + mid] % mod; n         A[i + j + mid] = A[i + j] + mod - tmp; n         A[i + j] += tmp; n       } n     } n   } n   for (int i = 0; i < lim; i++) a[i] = A[i] % mod; n } n inline void idft(vector<int> &a, int lim) { n   a.resize(lim); n   dft(a, lim), reverse(a.begin() + 1, a.end()); n   int linv = mod - (mod - 1) / lim; n   for (int i = 0; i < lim; i++) a[i] = 1ll * a[i] * linv % mod; n } n vector<int> operator*(vector<int> a, vector<int> b) { n   int len = (int(a.size())) + (int(b.size())) - 1, lim = getR(len); n   dft(a, lim), dft(b, lim); n   for (int i = 0; i < lim; i++) a[i] = 1ll * a[i] * b[i] % mod; n   idft(a, lim), a.resize(len); n   return a; n } n vector<int> adj[N]; n int n; n int son[N]; n void dfs1(int u, int fa) { n   for (auto v : adj[u]) { n     if (v == fa) continue; n     son[u]++; n     dfs1(v, u); n   } n } n int fac[N], ifac[N]; n inline int C(int n, int m) { n   return 1ll * fac[n] * ifac[n - m] % mod * ifac[m] % mod; n } n int main() { n   setup(); n   n = read(); n   fac[0] = 1; n   for (int i = 1; i <= n; i++) fac[i] = 1ll * fac[i - 1] * i % mod; n   ifac[n] = qpow(fac[n]); n   for (int i = n; i >= 1; i--) ifac[i - 1] = 1ll * ifac[i] * i % mod; n   for (int i = 1; i <= n - 1; i++) { n     int u = read(), v = read(); n     adj[u].push_back(v), adj[v].push_back(u); n   } n   dfs1(1, 0); n   vector<int> f = {1}; n   sort(son + 1, son + n + 1, greater<int>()); n   for (int i = 1; i <= n; i++) { n     int j = i; n     while (j < n && son[i] == son[j + 1]) j++; n     int up = j - i + 1; n     vector<int> g(up + 1); n     int cf = 1; n     for (int i = 0; i <= up; i++) { n       int coef = 1ll * C(up, i) * cf % mod; n       g[i] = coef; n       cf = 1ll * cf * (mod - son[j]) % mod; n     } n     f = f * g; n     i = j; n   } n   int ans = 0; n   for (int i = 0; i <= n - 1; i++) ans = (ans + 1ll * fac[n - i] * f[i]) % mod; n   print(ans,   n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct MinMax { n   int min, max; n   MinMax() : min(INT_MAX), max(INT_MIN){}; n   MinMax(int min, int max) : min(min), max(max){}; n }; n int main(int argc, char const *argv[]) { n   ios_base::sync_with_stdio(false); n   int n, m; n   cin >> n >> m; n   MinMax a[n]; n   for (int i = 0; i < m; i++) { n     int c; n     cin >> c; n     a[c - 1].min = min(a[c - 1].min, i); n     a[c - 1].max = max(a[c - 1].max, i); n   } n   int sum = 0; n   for (int i = 0; i < n; i++) { n     int cnt = 3; n     if (i == 0 || i == n - 1) cnt--; n     if (a[i].min < INT_MAX) cnt--; n     if (i < n - 1) { n       if (a[i].min < a[i + 1].max) cnt--; n     } n     if (i > 0) { n       if (a[i - 1].max > a[i].min) cnt--; n     } n     sum += cnt; n   } n   if (n == 1) sum = 0; n   cout << sum << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int t, n, i, j, k, h = 0; n   cin >> n; n   long long int a[n], b[n]; n   for (i = 0; i < n; ++i) { n     cin >> a[i] >> b[i]; n   } n   if (n == 1) cout << a[0] << endl; n   if (n > 1) { n     set<long long int> s1, s, s3; n     set<long long int>::iterator jt, it; n     j = a[0]; n     while (j % 2 == 0) { n       s.insert(2); n       j /= 2; n     } n     for (k = 3; k <= sqrt(j); k = k + 2) { n       while (j % k == 0) { n         s.insert(k); n         j /= k; n       } n     } n     if (j > 2) s.insert(j); n     j = b[0]; n     while (j % 2 == 0) { n       s.insert(2); n       j /= 2; n     } n     for (k = 3; k <= sqrt(j); k = k + 2) { n       while (j % k == 0) { n         s.insert(k); n         j /= k; n       } n     } n     if (j > 2) s.insert(j); n     j = a[1]; n     while (j % 2 == 0) { n       s1.insert(2); n       j /= 2; n     } n     for (k = 3; k <= sqrt(j); k = k + 2) { n       while (j % k == 0) { n         s1.insert(k); n         j /= k; n       } n     } n     if (j > 2) s1.insert(j); n     j = b[1]; n     while (j % 2 == 0) { n       s1.insert(2); n       j /= 2; n     } n     for (k = 3; k <= sqrt(j); k = k + 2) { n       while (j % k == 0) { n         s1.insert(k); n         j /= k; n       } n     } n     if (j > 2) s1.insert(j); n     for (it = s.begin(); it != s.end(); ++it) { n       for (jt = s1.begin(); jt != s1.end(); jt++) { n         if ((*jt) % (*it) == 0) { n           s3.insert(*it); n         } else if ((*it) % (*jt) == 0) n           s3.insert(*jt); n       } n     } n     s.clear(); n     for (i = 2; i < n; ++i) { n       for (it = s3.begin(); it != s3.end(); ++it) { n         if (a[i] % (*it) == 0) s.insert(*it); n         if (b[i] % (*it) == 0) s.insert(*it); n       } n       s3.clear(); n       for (it = s.begin(); it != s.end(); ++it) { n         s3.insert(*it); n       } n       s.clear(); n     } n     if (s3.size() > 0) { n       for (it = s3.begin(); it != s3.end(); ++it) { n         cout << *it << endl; n         break; n       } n     } else n       cout <<  -1 n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n; n   cin >> n; n   if (n == 0) n     cout << 1; n   else { n     n %= 4; n     if (n == 0) n = 4; n     n = pow(8, n); n     cout << n % 10; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   vector<double> a(n), b(n); n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     if (a[i] <= 1) { n       printf( -1 n ); n       exit(0); n     } n   } n   for (int i = 0; i < n; i++) { n     cin >> b[i]; n     if (b[i] <= 1) { n       printf( -1 n ); n       exit(0); n     } n   } n   double s = m; n   s += s / (b[0] - 1); n   for (int i = n - 1; i >= 1; i--) { n     s += s / (a[i] - 1); n     s += s / (b[i] - 1); n   } n   s += s / (a[0] - 1); n   printf( %.10f n , s - m); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ull = unsigned long long; n const int MAX_N = 1e6 + 5; n int fen[MAX_N]; n int n; n int lowbit(int x) { return x & (-x); } n void add(int x, int val) { n   for (int i = x; i <= n; i += lowbit(i)) fen[i] += val; n } n int sum(int x) { n   int s = 0; n   for (int i = x; i; i -= lowbit(i)) s += fen[i]; n   return s; n } n int sum(int l, int r) { return sum(r) - sum(l); } n map<int, int> cnt; n int pre[MAX_N]; n int back[MAX_N]; n int arr[MAX_N]; n int main() { n   ios::sync_with_stdio(false); n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n     cnt[arr[i]]++; n     pre[i] = cnt[arr[i]]; n   } n   cnt.clear(); n   for (int i = n - 1; i >= 0; i--) { n     cnt[arr[i]]++; n     back[i] = cnt[arr[i]]; n     add(back[i], 1); n   } n   ll ans = 0; n   for (int i = 0; i < n; i++) { n     add(back[i], -1); n     ans += sum(pre[i] - 1); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 9; n bool p[N]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   vector<int> k; n   for (int i = 2; i <= 1e6; i++) { n     if (p[i]) continue; n     k.push_back(i); n     for (int j = i + i; j <= 1e6; j += i) { n       p[j] = 1; n     } n   } n   int t; n   cin >> t; n   while (t--) { n     long long x; n     cin >> x; n     long long m = sqrtl(x); n     long long r = upper_bound(k.begin(), k.end(), x) - k.begin(); n     long long l = upper_bound(k.begin(), k.end(), m) - k.begin(); n     cout << r - l + 1 <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1000000007; n const int N = 100100; n int n; n int main() { n   cin >> n; n   for (int i = n + 1; i <= n + n; i++) cout << i <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n const double EPS = 1e-9; n struct DDMY { n   ostringstream o; n   template <class T> n   DDMY &operator,(const T &t) { n     o << t <<  , ; n     return *this; n   } n   string operator*() { return o.str().substr(0, o.str().size() - 1); } n }; n template <class T> n std::ostream &operator<<(std::ostream &o, const std::vector<T> &v) { n   o <<  [ ; n   for (std::size_t i = 0; i < v.size(); i++) n     o << v[i] << (i < v.size() - 1 ?  ,  :   ); n   return o <<  ] ; n } n template <class T, class U> n std::ostream &operator<<(std::ostream &o, const std::pair<T, U> &v) { n   return o << v.first <<  :  << v.second; n } n template <class T, class U> n std::ostream &operator<<(std::ostream &o, const std::map<T, U> &v) { n   o <<  { ; n   typename std::map<T, U>::const_iterator i = v.begin(), j = i; n   ++j; n   for (; i != v.end(); ++i, ++j) o << *i << (j != v.end() ?  ,  :   ); n   return o <<  } ; n } n template <class T> n std::ostream &operator<<(std::ostream &o, const std::set<T> &v) { n   o <<  { ; n   typename std::set<T>::const_iterator i = v.begin(), j = i; n   ++j; n   for (; i != v.end(); ++i, ++j) o << *i << (j != v.end() ?  ,  :   ); n   return o <<  } ; n } n void error() { n   cout << -1 << endl; n   exit(0); n } n int main(int argc, char *argv[]) { n   ios::sync_with_stdio(false); n   int a1, a2, a3, a4; n   cin >> a1 >> a2 >> a3 >> a4; n   if (max(a3, a4) > a1 || max(a3, a4) > a2) { n     error(); n   } n   string line; n   if (a3 >= a4) { n     for (int i = 0; i < a3; i++) { n       line +=  47 ; n     } n     a1 -= a3; n     a2 -= a3; n     a4 -= a3 - 1; n     if (a1 < 0 || a2 < 0 || a4 < 0) { n       error(); n     } n     if (a4 == 1) { n       if (a1 == 0) { n         if (a2 > 0) { n           line =  7  + line; n           a2--; n           if (a2 > 0) line += string(a2,  7 ); n         } else { n           error(); n         } n       } else { n         line += string(a2,  7 ); n         line +=  4 ; n         a1--; n       } n     } else if (a4 == 0) { n       line += string(a2,  7 ); n     } else { n       error(); n     } n     if (a1 > 0) line = string(a1,  4 ) + line; n   } else { n     for (int i = 0; i < a4; i++) { n       line +=  74 ; n     } n     a1 -= a4; n     a2 -= a4; n     a3 -= a4 - 1; n     if (a1 < 0 || a2 < 0 || a4 < 0) { n       error(); n     } n     if (a3 == 0) { n       if (a4 > 1) { n         line = line.substr(0, line.size() - 2); n         line = line.substr(2); n         line =  7  + string(a1 + 1,  4 ) + line; n         line += string(a2 + 1,  7 ) +  4 ; n       } else if (a4 == 1) { n         a1 += a4; n         a2 += a4; n         if (a2 > 0 && a1 > 0) { n           line = string(a2,  7 ) + string(a1,  4 ); n         } else { n           error(); n         } n       } else if (a4 == 0) { n         if (a1 == 0 || a2 == 0) { n           if (a1 > 0) { n             line = string(a1,  4 ); n           } else if (a2 > 0) { n             line = string(a2,  7 ); n           } n         } else { n           error(); n         } n       } else { n         error(); n       } n     } else { n       error(); n     } n   } n   cout << line << endl; n   return 0; n } n


#include <bits/stdc++.h> n  n template <class T> n inline void read(T &res) n { n  res = 0; bool bo = 0; char c; n  while (((c = getchar()) <  0  || c >  9 ) && c !=  - ); n  if (c ==  - ) bo = 1; else res = c - 48; n  while ((c = getchar()) >=  0  && c <=  9 ) n   res = (res << 3) + (res << 1) + (c - 48); n  if (bo) res = ~res + 1; n } n  n const int N = 505; n  n int n, m, a[N][N]; n  n int main() n { n  read(n); read(m); n  for (int i = 1; i <= n; i++) n   for (int j = 1; j <= m; j++) n    read(a[i][j]); n  for (int i = 1; i <= n; i++) n  { n   for (int j = 1; j <= m; j++) n    printf( %d  , i + j & 1 ? 720720 : 720720 - a[i][j] n     * a[i][j] * a[i][j] * a[i][j]); n   puts(  ); n  } n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n long double pi = 3.141592653589793238462643383279502884197; n vector<long long int> v(14); n void Compute() { n   for (long long int i = 1; i < 14; i++) { n     v[i] = pow(2, i) + v[i - 1]; n   } n } n void solve() { n   long long int n; n   cin >> n; n   long long int ans = 0; n   string s = to_string(n); n   long long int si = s.size(); n   ans += v[si - 1]; n   for (long long int i = 0; i < s.size(); i++) { n     if (s[i] ==  7 ) ans += pow(2, (si - 1 - i)); n   } n   ans++; n   cout << ans << endl; n } n int main() { n   Compute(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int V = 100000 + 5; n int n, num[V]; n int main() { n   int i, j; n   scanf( %d , &n); n   for (i = 0; i < n; ++i) scanf( %d , &num[i]); n   sort(num, num + n); n   printf( %d  , num[n - 1]); n   for (i = 1; i < n - 1; ++i) printf( %d  , num[i]); n   printf( %d n , num[0]); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 10; n const double pi = acos(-1.0); n struct Complex { n   double x, y; n   Complex(double x = 0.0, double y = 0.0) { n     this->x = x; n     this->y = y; n   } n   Complex operator-(const Complex &b) const { n     return Complex(x - b.x, y - b.y); n   } n   Complex operator+(const Complex &b) const { n     return Complex(x + b.x, y + b.y); n   } n   Complex operator*(const Complex &b) const { n     return Complex(x * b.x - y * b.y, x * b.y + y * b.x); n   } n }; n void change(Complex y[], int len) { n   for (int i = 1, j = len / 2; i < len - 1; i++) { n     if (i < j) swap(y[i], y[j]); n     int k = len / 2; n     while (j >= k) { n       j -= k; n       k /= 2; n     } n     if (j < k) j += k; n   } n } n void fft(Complex y[], int len, int on) { n   change(y, len); n   for (int h = 2; h <= len; h <<= 1) { n     Complex wn(cos(-on * 2 * pi / h), sin(-on * 2 * pi / h)); n     for (int j = 0; j < len; j += h) { n       Complex w(1, 0); n       for (int k = j; k < j + h / 2; k++) { n         Complex u = y[k]; n         Complex t = w * y[k + h / 2]; n         y[k] = u + t; n         y[k + h / 2] = u - t; n         w = w * wn; n       } n     } n   } n   if (on == -1) n     for (int i = 0; i < len; i++) y[i].x /= len; n } n int n, m, k, i, j, p[30]; n Complex A[N], B[N], C[N]; n int a[N], b[N]; n char sa[N], sb[N]; n int main() { n   for (i = 1; i <= 26; i++) scanf( %d , &p[i]); n   scanf( %s %s , sa, sb); n   m = strlen(sa), n = strlen(sb); n   for (i = 0, j = m - 1; i < j; i++, j--) swap(sa[i], sa[j]); n   for (i = 0; i < m; i++) a[i] = sa[i] -  a  + 1; n   for (i = 0; i < n; i++) b[i] = sb[i] -  a  + 1; n   for (k = 1; k < n; k <<= 1) n     ; n   for (i = 0; i < k; i++) n     A[i] = Complex(2 * a[i] * p[a[i]] * (a[i] + p[a[i]]), 0), n     B[i] = Complex(b[i], 0); n   for (fft(A, k, 1), fft(B, k, 1), i = 0; i < k; i++) C[i] = C[i] - A[i] * B[i]; n   for (i = 0; i < k; i++) n     A[i] = Complex(a[i] * a[i] + p[a[i]] * p[a[i]] + 4 * a[i] * p[a[i]], 0), n     B[i] = Complex(b[i] * b[i], 0); n   for (fft(A, k, 1), fft(B, k, 1), i = 0; i < k; i++) C[i] = C[i] + A[i] * B[i]; n   for (i = 0; i < k; i++) n     A[i] = Complex(2 * (a[i] + p[a[i]]), 0), n     B[i] = Complex(b[i] * b[i] * b[i], 0); n   for (fft(A, k, 1), fft(B, k, 1), i = 0; i < k; i++) C[i] = C[i] - A[i] * B[i]; n   for (i = 0; i < k; i++) { n     if (i < m) n       A[i] = Complex(1, 0); n     else n       A[i] = Complex(0, 0); n     B[i] = Complex(b[i] * b[i] * b[i] * b[i], 0); n   } n   for (fft(A, k, 1), fft(B, k, 1), i = 0; i < k; i++) C[i] = C[i] + A[i] * B[i]; n   for (i = 0; i < k; i++) { n     A[i] = Complex(a[i] * a[i] * p[a[i]] * p[a[i]], 0); n     if (i < n) n       B[i] = Complex(1, 0); n     else n       B[i] = Complex(0, 0); n   } n   for (fft(A, k, 1), fft(B, k, 1), i = 0; i < k; i++) C[i] = C[i] + A[i] * B[i]; n   fft(C, k, -1); n   for (int i = 0; i < n - m + 1; i++) n     printf( %d , ((long long)(C[i + m - 1].x + 0.5) == 0)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline void read(int& n) { n   int x = 0, f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) n     x = (x << 1) + (x << 3) + ch -  0 , ch = getchar(); n   n = x * f; n } n int outputarray[20]; n inline void write(int k) { n   int num = 0; n   if (k < 0) putchar( - ), k = -k; n   do { n     outputarray[++num] = k % 10, k /= 10; n   } while (k); n   while (num) putchar(outputarray[num--] +  0 ); n   puts(  ); n } n const int N = 50; n int n, m, sg[N][N][N][N][2]; n char s[N][N]; n int dfs(int x0, int x2, int y0, int y2, int type) { n   if (sg[x0][x2][y0][y2][type] >= 0) return sg[x0][x2][y0][y2][type]; n   int a[N * N]; n   memset(a, 0, sizeof(a)); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) n       if (((i + j) & 1) == type) { n         int x = i + j, y = i - j + m; n         if (x >= x0 && x < x2 && y >= y0 && y < y2) { n           int tmp = 0; n           if (s[i][j] ==  L ) n             tmp = dfs(x0, x, y0, y2, type) ^ dfs(x + 1, x2, y0, y2, type); n           if (s[i][j] ==  R ) n             tmp = dfs(x0, x2, y0, y, type) ^ dfs(x0, x2, y + 1, y2, type); n           if (s[i][j] ==  X ) n             tmp = dfs(x0, x, y0, y, type) ^ dfs(x0, x, y + 1, y2, type) ^ n                   dfs(x + 1, x2, y0, y, type) ^ dfs(x + 1, x2, y + 1, y2, type); n           a[tmp] = 1; n         } n       } n   while (a[++sg[x0][x2][y0][y2][type]]) n     ; n   return sg[x0][x2][y0][y2][type]; n } n int main() { n   memset(sg, -1, sizeof(sg)); n   read(n), read(m); n   for (int i = 0; i < n; i++) scanf( %s , s[i]); n   if (dfs(0, n + m, 0, n + m, 0) ^ dfs(0, n + m, 0, n + m, 1)) n     puts( WIN ); n   else n     puts( LOSE ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, ans = 0; n   cin >> n >> m; n   while (n != m) { n     if (n < m) { n       if (m % 2 == 0) n         m /= 2, ans++; n       else n         m++, ans++; n     } else { n       ans++; n       m++; n     } n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, q; n int matrix[52][52]; n long long pref[3][52][52]; n long long all[2][52][52][52][52]; n void precalculate() { n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       for (int k = i; k <= n; k++) { n         for (int ij = j; ij <= m; ij++) { n           all[1][i][j][k][ij] = all[1][i][j][k][ij - 1] + n                                 all[1][i][j][k - 1][ij] - n                                 all[1][i][j][k - 1][ij - 1]; n           int r = ij - j + 1; n           for (int c = k; c >= i && r > 0; c--) { n             r = (r > pref[2][c][ij]) ? pref[2][c][ij] : r; n             all[1][i][j][k][ij] += r; n           } n         } n       } n     } n   } n } n int main() { n   scanf( %d %d %d , &n, &m, &q); n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       char c =  2 ; n       while (c !=  0  && c !=  1 ) scanf( %c , &c); n       matrix[i][j] = c - 48; n       pref[2][i][j] = pref[2][i][j - 1]; n       if (matrix[i][j] == 1) n         pref[2][i][j] = 0; n       else n         pref[2][i][j]++; n     } n   } n   precalculate(); n   while (q--) { n     int a, b, c, d; n     cin >> a >> b >> c >> d; n     cout << all[1][a][b][c][d] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int arr[4005]; n int nxt[4005], prv[4005]; n map<int, int> M; n map<int, int> Q; n int main() { n   int n; n   cin >> n; n   int ans = 0; n   for (int i = 0; i < n; ++i) { n     cin >> arr[i]; n     if (M.find(arr[i]) != M.end()) { n       nxt[M[arr[i]]] = i; n       prv[i] = M[arr[i]]; n       Q[arr[i]]++; n       ans = max(ans, Q[arr[i]]); n     } else { n       prv[i] = -1; n       Q[arr[i]]++; n       ans = max(ans, Q[arr[i]]); n     } n     M[arr[i]] = i; n     nxt[i] = -1; n   } n   for (int i = 0; i < n; ++i) { n     if (prv[i] != -1) { n       continue; n     } n     for (int j = i + 1; j < n; ++j) { n       if (prv[j] != -1) { n         continue; n       } n       int cur = 2; n       int t1 = i, t2 = j; n       while (t1 != -1 && t2 != -1) { n         if (t1 < t2) { n           if (nxt[t1] > t2) { n             ++cur; n           } n           t1 = nxt[t1]; n         } else { n           if (nxt[t2] > t1) { n             ++cur; n           } n           t2 = nxt[t2]; n         } n       } n       ans = max(ans, cur); n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int b = 3e6; n long long a[b]; n void ans(long long x, long long y, long long k, long long i) { n   long long sidl = k + k * y, ans = k; n   long long ez1 = (sidl - 3 + x) / (x - 1); n   ans += ez1; n   a[i] = ans; n } n int main() { n   long long t, x, y, k, j; n   cin >> t; n   for (int i = 0; i < t; i++) { n     cin >> x >> y >> k; n     ans(x, y, k, i); n   } n   for (int i = 0; i < t; i++) cout << a[i] << endl; n   return 0; n }; n


#include <bits/stdc++.h> n using namespace std; n int test() { n   long long n; n   cin >> n; n   long long cL = 0, cR = 0; n   long long si = 3 * 100001; n   long long *right = new long long[si], *left = new long long[si]; n   for (long long i = 0; i < si; i++) { n     right[i] = 0; n     left[i] = 0; n   } n   long long prop = 0, l = 0, r = 0; n   string temp; n   for (long long i = 0; i < n; i++) { n     cin >> temp; n     long long tempS = temp.size(); n     for (long long i = 0; i < tempS; i++) { n       if (temp[i] ==  ( ) { n         cL++; n         l++; n       } else { n         cR++; n         if (l != 0) n           l--; n         else n           r++; n       } n     } n     if (r != 0 && l != 0) { n     } else if (cR == cL) n       prop++; n     else if (cR > cL) n       right[cR - cL]++; n     else n       left[cL - cR]++; n     l = 0; n     r = 0; n     cL = 0; n     cR = 0; n   } n   long long S = 0; n   for (long long i = 0; i < si; i++) { n     if (right[i] != 0 && left[i] != 0) { n       S += right[i] * left[i]; n     } n   } n   S += prop * prop; n   cout << S; n   return 0; n } n int hacker() { return 0; } n int main() { n   while (true) { n     return test(); n     cout <<   n n-- ; n     hacker(); n     cout <<   n n n----------------------------- n n n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double EPS = 1e-6; n double n, r; n bool check(double R) { n   double x1 = r + R; n   double y1 = 0; n   double x2 = cos(2 * acos(-1) / n) * (r + R); n   double y2 = sin(2 * acos(-1) / n) * (r + R); n   double len = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); n   if (len <= 2 * R) n     return true; n   else n     return false; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> r; n   double l1 = 0, r1 = 10000; n   for (int i = 0; i < 500; i++) { n     double mm = (l1 + r1) / 2.0; n     if (check(mm)) n       r1 = mm; n     else n       l1 = mm; n   } n   cout.fixed; n   cout.precision(15); n   cout << l1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, c; n   cin >> a; n   cin >> b; n   cin >> c; n   int d = a + b - c; n   int e = b + c - a; n   int f = a + c - b; n   if (d < 0 || e < 0 || f < 0 || e % 2 || d % 2 || f % 2) { n     cout <<  Impossible ; n     return 0; n   } n   cout << d / 2 <<     << e / 2 <<     << f / 2 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   int l, r, num; n } a[100005]; n int n, ans[100005]; n bool cmp(node x, node y) { n   if (x.l == y.l) return x.r < y.r; n   return x.l < y.l; n } n void solve() { n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i].l >> a[i].r; n     a[i].num = i + 1; n   } n   sort(a + 1, a + n + 1, cmp); n   set<int> F; n   int mx = -1; n   for (int i = 1; i <= n - 1; i++) { n     F.insert(a[i].num); n     mx = max(mx, a[i].r); n     if (mx < a[i + 1].l) { n       for (int j = 1; j <= n; j++) { n         if (F.count(a[j].num)) n           ans[a[j].num] = 1; n         else n           ans[a[j].num] = 2; n       } n       for (int j = 2; j <= n + 1; j++) cout << ans[j] <<    ; n       cout << endl; n       return; n     } n   } n   cout << -1 << endl; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int T; n   cin >> T; n   while (T--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, a, b, c, d, e, f, g, h; n   cin >> n; n   vector<long long int> values(n), kalues(n), malues; n   for (int i = 0; i < n; i++) { n     cin >> values[i]; n     cin >> kalues[i]; n     kalues[i]--; n     malues.push_back(values[i]); n     malues.push_back(kalues[i]); n   } n   sort(values.begin(), values.end()); n   sort(kalues.begin(), kalues.end()); n   sort(malues.begin(), malues.end()); n   long long int mini = -5, year = -1; n   malues.erase(unique(malues.begin(), malues.end()), malues.end()); n   for (int i = 0; i < 2 * n; i++) { n     long long int helebele = n         lower_bound(kalues.begin(), kalues.end(), malues[i]) - kalues.begin(); n     helebele += n - (upper_bound(values.begin(), values.end(), malues[i]) - n                      values.begin()); n     if (mini < n - helebele) mini = n - helebele, year = malues[i]; n   } n   cout << year <<     << mini << endl; n } n


#include <bits/stdc++.h> n using namespace std; n char s[55]; n int l; n int df[55][10]; n int f(int pos, int c) { n   if (pos == l) return 1; n   if (df[pos][c] != -1) return df[pos][c]; n   int res = 0; n   int nn = c + s[pos]; n   res += f(pos + 1, nn / 2); n   if (nn & 1) res += f(pos + 1, (nn + 1) / 2); n   return df[pos][c] = res; n } n bool good() { n   for (int i = 1; i < l; i++) { n     int nn = s[i] + s[i - 1]; n     if (nn & 1) { n       if (nn / 2 != s[i] && (nn + 1) / 2 != s[i]) return false; n     } else { n       if (nn / 2 != s[i]) return false; n     } n   } n   return true; n } n int main() { n   memset(df, -1, sizeof(df)); n   scanf( %s , s); n   l = strlen(s); n   for (int i = 0; i < l; i++) s[i] -=  0 ; n   long long ans = 0; n   for (int i = 0; i < 10; i++) ans += f(1, i); n   if (good()) ans--; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n const int INF = 1e9; n using namespace std; n int R, C, frow, fcol, row, col, srow, scol, nxcol, nxrow; n int ans, intest[1005][1005], goal; n char grid[1005][1005]; n queue<pair<int, int> > q; n const int dr[4] = {-1, 0, 1, 0}; n const int dc[4] = {0, -1, 0, 1}; n bool inside(int r, int c) { return r > 0 && c > 0 && r <= R && c <= C; } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cin >> R >> C; n   for (int r = 1; r <= R; r++) { n     for (int c = 1; c <= C; c++) { n       cin >> grid[r][c]; n       intest[r][c] = INF; n       if (grid[r][c] ==  S ) n         srow = r, scol = c; n       else if (grid[r][c] ==  E ) n         frow = r, fcol = c; n     } n   } n   intest[frow][fcol] = 0; n   q.push(make_pair(frow, fcol)); n   while (!q.empty()) { n     pair<int, int> cur = q.front(); n     q.pop(); n     row = cur.first; n     col = cur.second; n     for (int i = 0; i <= 3; i++) { n       nxrow = row + dr[i]; n       nxcol = col + dc[i]; n       if (!inside(nxrow, nxcol)) continue; n       if (grid[nxrow][nxcol] ==  T ) continue; n       if (intest[nxrow][nxcol] != INF) continue; n       intest[nxrow][nxcol] = intest[row][col] + 1; n       q.push(make_pair(nxrow, nxcol)); n     } n   } n   goal = intest[srow][scol]; n   for (int i = 1; i <= R; i++) { n     for (int j = 1; j <= C; j++) { n       if (grid[i][j] -  0  > 0 && grid[i][j] < 58) { n         srow = i, scol = j; n         if (intest[srow][scol] <= goal) ans += (grid[i][j] -  0 ); n       } n     } n   } n   cout << ans << endl; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int x, y, z; n int _x, _y, _z; n int a[6]; n long long int ans = 0LL; n int main() { n   ios::sync_with_stdio(false); n   cin >> x >> y >> z; n   cin >> _x >> _y >> _z; n   for (int i = 0; i < 6; i++) cin >> a[i]; n   if (y < 0) ans += a[0]; n   if (y > _y) ans += a[1]; n   if (z < 0) ans += a[2]; n   if (z > _z) ans += a[3]; n   if (x < 0) ans += a[4]; n   if (x > _x) ans += a[5]; n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int SZ = 8e6 + 10; n const int INF = 1e9 + 10; n const int mod = 998244353; n const long double eps = 1e-8; n long long read() { n   long long n = 0; n   char a = getchar(); n   bool flag = 0; n   while (a >  9  || a <  0 ) { n     if (a ==  - ) flag = 1; n     a = getchar(); n   } n   while (a <=  9  && a >=  0 ) { n     n = n * 10 + a -  0 , a = getchar(); n   } n   if (flag) n = -n; n   return n; n } n int a[SZ], f[1010][1010], b[SZ], c[SZ]; n int main() { n   int n = read(), k = read(); n   for (int i = 1; i <= n; i++) a[i] = read(); n   sort(a + 1, a + 1 + n); n   for (int i = 1; i <= n; i++) { n     for (int j = i + 1; j <= n; j++) { n       b[++b[0]] = a[j] - a[i]; n     } n   } n   sort(b + 1, b + 1 + b[0]); n   b[0] = unique(b + 1, b + 1 + b[0]) - b - 1; n   int ans = 0; n   for (int j = 1; j <= b[0]; j++) { n     int minx = b[j]; n     if (minx > 1e5 / (k - 1)) break; n     for (int i = 1; i <= n; i++) f[i][1] = f[i - 1][1] + 1; n     for (int l = 2; l <= k; l++) { n       for (int i = 2, lst = 0; i <= n; i++) { n         while (lst < i && a[lst + 1] <= a[i] - minx) lst++; n         f[i][l] = f[lst][l - 1]; n       } n       for (int i = 1; i <= n; i++) (f[i][l] += f[i - 1][l]) %= mod; n     } n     c[j] = f[n][k]; n   } n   for (int j = 1; j <= b[0]; j++) n     (ans += 1ll * b[j] * (c[j] - c[j + 1] + mod) % mod) %= mod; n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int book[3]; n char s[105], rs[105]; n int main() { n   int t, ans, n; n   scanf( %d , &t); n   for (int i = 0; i < t; i++) { n     scanf( %d , &n); n     ans = 0; n     memset(rs,   0 , sizeof(rs)); n     for (int i = 0; i < 3; i++) { n       scanf( %d , &book[i]); n     } n     scanf( %s , s); n     for (int i = 0; i < n; i++) { n       if (s[i] ==  R ) { n         if (book[1] > 0) { n           ans++; n           book[1]--; n           rs[i] =  P ; n         } n       } else if (s[i] ==  P ) { n         if (book[2] > 0) { n           ans++; n           book[2]--; n           rs[i] =  S ; n         } n       } else { n         if (book[0] > 0) { n           ans++; n           book[0]--; n           rs[i] =  R ; n         } n       } n     } n     if (ans >= (n + 1) / 2) { n       int k = 0; n       for (int i = 0; i < n; i++) { n         if (rs[i] ==   0 ) { n           while (book[k] == 0) k++; n           if (k == 0) n             rs[i] =  R ; n           else if (k == 1) n             rs[i] =  P ; n           else n             rs[i] =  S ; n           book[k]--; n         } n       } n       printf( YES n%s n , rs); n     } else n       printf( NO n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 5; n long long le[N], ri[N], mid, lee, rii; n long long a[N], b[N], c[N], mp[35], ans, n, answ, idx, idx1, pw[35], bb; n int main() { n   std::ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   pw[0] = 1; n   for (int i = 1; i <= 35; i++) { n     pw[i] = pw[i - 1] * 2; n   } n   for (int i = 0; i <= 30; i++) { n     for (int j = 1; j <= n; j++) { n       b[j] += a[j] & pw[i]; n       c[j] = b[j]; n     } n     std::stable_sort(c + 1, c + n + 1); n     idx = n; n     for (int j = 1; j <= n; j++) { n       idx = max(idx, j + 1LL); n       bb = 0; n       while (c[j] + c[idx] >= pw[i] && idx > j) { n         idx--; n         bb = 1; n       } n       if (bb == 1) idx++; n       if (c[j] + c[idx] < pw[i] || idx > n) n         le[j] = -1; n       else n         le[j] = idx; n     } n     idx = n; n     for (int j = 1; j <= n; j++) { n       idx = max(idx, j + 1LL); n       while (c[j] + c[idx] >= pw[i + 1] && idx > j + 1) { n         idx--; n       } n       if (c[j] + c[idx] >= pw[i + 1] || idx > n) n         ri[j] = -1; n       else n         ri[j] = idx; n     } n     for (int j = 1; j <= n; j++) { n       if (le[j] != -1 && ri[j] != -1) mp[i] += ri[j] - le[j] + 1; n     } n     idx = n; n     for (int j = 1; j <= n; j++) { n       idx = max(idx, j + 1LL); n       bb = 0; n       while (c[j] + c[idx] >= pw[i + 1] + pw[i] && idx > j) { n         bb = 1; n         idx--; n       } n       if (bb == 1) idx++; n       if (c[j] + c[idx] < pw[i + 1] + pw[i] || idx > n) n         le[j] = -1; n       else n         le[j] = idx; n     } n     idx = n; n     for (int j = 1; j <= n; j++) { n       idx = max(idx, j + 1LL); n       while (c[j] + c[idx] >= pw[i + 2] && idx > j + 1) { n         idx--; n       } n       if (c[j] + c[idx] >= pw[i + 2] || idx > n) n         ri[j] = -1; n       else n         ri[j] = idx; n     } n     for (int j = 1; j <= n; j++) { n       if (le[j] != -1 && ri[j] != -1) mp[i] += ri[j] - le[j] + 1; n     } n   } n   for (int i = 0; i <= 30; i++) { n     if (mp[i] % 2) answ += (1LL << i); n   } n   cout << answ << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int a[20000], num[20000]; n int dp[400]; n int n, m, t, k, ans, maxm; n int main() { n   scanf( %d%d , &n, &m); n   maxm = 0; n   ans = 0; n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     num[a[i]]++; n     maxm = max(maxm, num[a[i]]); n   } n   t = min(n, m); n   k = t; n   int temp = 0; n   while (--k) { n     temp += n; n     for (int i = 1; i <= n; i++) a[temp + i] = a[i]; n   } n   for (int i = 1; i <= n * t; i++) { n     dp[a[i]]++; n     for (int j = a[i] - 1; j >= 0; j--) dp[a[i]] = max(dp[a[i]], dp[j] + 1); n     ans = max(dp[a[i]], ans); n   } n   ans += maxm * (m - t); n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n const int inf = 1500000000; n const int d = 1e9 + 7; n using namespace std; n struct triple { n   int a; n   int b; n   int c; n   bool flag; n   triple() { flag = true; } n }; n int main() { n   int n; n   cin >> n; n   vector<triple> t(n - 1, triple()); n   vector<int> adj[n + 1]; n   for (int i = 1; i <= n - 2; i++) { n     cin >> t[i].a >> t[i].b >> t[i].c; n     adj[t[i].a].push_back(i); n     adj[t[i].b].push_back(i); n     adj[t[i].c].push_back(i); n   } n   int first; n   triple f; n   for (int i = 1; i <= n; i++) { n     if (adj[i].size() == 1) { n       first = i; n       f = t[adj[i][0]]; n       break; n     } n   } n   int second; n   if (adj[f.a].size() == 2) { n     second = f.a; n   } n   if (adj[f.b].size() == 2) { n     second = f.b; n   } n   if (adj[f.c].size() == 2) { n     second = f.c; n   } n   vector<int> perm; n   perm.push_back(first); n   perm.push_back(second); n   while (perm.size() != n) { n     int third; n     for (int i = 0; i < adj[first].size(); i++) { n       int ind = adj[first][i]; n       if (t[ind].flag == true) { n         t[ind].flag = false; n         third = (t[ind].a + t[ind].b + t[ind].c) - first - second; n         perm.push_back(third); n         first = second; n         second = third; n         break; n       } n     } n   } n   for (int i = 0; i < perm.size(); i++) cout << perm[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int k; n string s; n int main() { n   cin >> k; n   cin >> s; n   int n = s.length(); n   for (int i = 0; i < n; ++i) { n     if (s[i] ==  ?  && s[n - i - 1] ==  ? ) { n       continue; n     } else if (s[i] ==  ? ) { n       s[i] = s[n - i - 1]; n     } else if (s[n - i - 1] ==  ? ) { n       s[n - i - 1] = s[i]; n     } else if (s[i] != s[n - i - 1]) { n       cout <<  IMPOSSIBLE ; n       return 0; n     } n   } n   bool flag[26]; n   memset(flag, 0, sizeof(flag)); n   for (int i = 0; i < n; ++i) { n     if (s[i] !=  ? ) { n       flag[s[i] -  a ] = true; n     } n   } n   int miss = 0; n   for (int c = 0; c < k; ++c) { n     if (!flag[c]) { n       ++miss; n     } n   } n   int have = 0; n   for (int i = 0; i <= n - i - 1; ++i) { n     if (s[i] ==  ? ) { n       ++have; n     } n   } n   if (miss > have) { n     cout <<  IMPOSSIBLE ; n     return 0; n   } n   for (int i = 0; i < n; ++i) { n     if (s[i] ==  ? ) { n       if (have > miss) { n         s[i] = s[n - i - 1] =  a ; n       } else { n         for (int c = 0; c < k; ++c) { n           if (!flag[c]) { n             s[i] = s[n - i - 1] = c +  a ; n             flag[c] = true; n             break; n           } n         } n         --miss; n       } n       --have; n     } n   } n   cout << s << endl; n   return 0; n } n


#include <bits/stdc++.h> n const long double EPS = 1e-10; n const long long int MOD = 1000000007ll; n const long long int mod1 = 1000000009ll; n const long long int mod2 = 1100000009ll; n int INF = (int)1e9; n long long int INFINF = (long long int)1e18; n int debug = 0; n long double PI = acos(-1.0); n using namespace std; n long long int bit_count(long long int _x) { n   long long int _ret = 0; n   while (_x) { n     if (_x % 2 == 1) _ret++; n     _x /= 2; n   } n   return _ret; n } n long long int bit(long long int _mask, long long int _i) { n   return (_mask & (1 << _i)) == 0 ? 0 : 1; n } n long long int powermod(long long int _a, long long int _b, n                        long long int _m = MOD) { n   long long int _r = 1; n   while (_b) { n     if (_b % 2 == 1) _r = (_r * _a) % _m; n     _b /= 2; n     _a = (_a * _a) % _m; n   } n   return _r; n } n long long int add(long long int a, long long int b, long long int m = MOD) { n   long long int x = a + b; n   while (x >= m) x -= m; n   while (x < 0) x += m; n   return x; n } n long long int sub(long long int a, long long int b, long long int m = MOD) { n   long long int x = a - b; n   while (x < 0) x += m; n   while (x >= m) x -= m; n   return x; n } n long long int mul(long long int a, long long int b, long long int m = MOD) { n   long long int x = a * 1ll * b; n   x %= m; n   if (x < 0) x += m; n   return x; n } n int N, M, K; n vector<int> G[1010]; n bool spec[1010]; n bool vis[1010]; n int tot, cnt; n vector<int> sizes; n void dfs(int u) { n   tot++; n   cnt++; n   vis[u] = true; n   for (int v : G[u]) { n     if (!vis[v]) { n       dfs(v); n     } n   } n } n int main() { n   if (0) { n     freopen( input.txt ,  r , stdin); n     debug = 1; n   } n   srand(time(NULL)); n   scanf( %d , &N); n   scanf( %d , &M); n   scanf( %d , &K); n   for (int i = 1; i <= K; i++) { n     int x; n     scanf( %d , &x); n     spec[x] = true; n   } n   for (int i = 1; i <= M; i++) { n     int u, v; n     scanf( %d , &u); n     scanf( %d , &v); n     G[u].push_back(v); n     G[v].push_back(u); n   } n   for (int i = 1; i <= N; i++) { n     if (spec[i]) { n       cnt = 0; n       dfs(i); n       sizes.push_back(cnt); n     } n   } n   sort(sizes.begin(), sizes.end()); n   reverse(sizes.begin(), sizes.end()); n   sizes[0] += (N - tot); n   long long int ans = 0; n   for (int it : sizes) { n     ans += (it * (it - 1)) / 2; n   } n   cout << ans - M; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename Q> n void inin(Q &x) { n   x = 0; n   int f = 0; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = 1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) x = x * 10 + ch -  0 , ch = getchar(); n   x = f ? -x : x; n } n int n, a[100010], b[100010]; n long long sum[100010], ans[100010], tot[100010]; n int erfen(int x, int l, int r) { n   int ll = l, rr = r, mid; n   while (ll < rr) { n     mid = (ll + rr) >> 1; n     if (sum[mid] - sum[l - 1] < x) n       ll = mid + 1; n     else n       rr = mid; n   } n   if (ll == n && sum[n] - sum[l - 1] < x) return n + 1; n   return ll; n } n int main() { n   inin(n); n   for (int i = (1); i <= (n); i++) inin(a[i]); n   for (int i = (1); i <= (n); i++) inin(b[i]), sum[i] = sum[i - 1] + b[i]; n   for (int i = (1); i <= (n); i++) { n     int x = erfen(a[i], i, n); n     tot[x]--; n     ans[x] += a[i] - (sum[x - 1] - sum[i - 1]); n     tot[i] += tot[i - 1] + 1; n     ans[i] += 1LL * b[i] * tot[i]; n   } n   for (int i = (1); i <= (n); i++) printf( %lld  , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n const long long sze = 1e5 + 2; n const long long inf = 0x3f3f3f3f; n void solve() { n   long long n, k; n   cin >> n; n   cin >> k; n   long long swap = k; n   long long last = n; n   long long i = 1; n   bool turn = true; n   while (swap) { n     if (turn) { n       cout << i <<    ; n       i++; n       turn = false; n     } else { n       cout << last <<    ; n       last--; n       turn = true; n     } n     swap--; n   } n   if (k % 2 == 0) { n     while (last >= i) { n       cout << last <<    ; n       last--; n     } n     return; n   } n   while (i <= last) { n     cout << i <<    ; n     i++; n   } n } n signed main() { n   long long test = 1; n   while (test--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b; n   vector<pair<int, int> > v, re; n   scanf( %d %d  , &a, &b); n   v.push_back(make_pair(a, b)); n   scanf( %d %d  , &a, &b); n   v.push_back(make_pair(a, b)); n   scanf( %d %d  , &a, &b); n   v.push_back(make_pair(a, b)); n   sort(v.begin(), v.end()); n   pair<int, int> A = v[0], B = v[1], C = v[2]; n   int x = A.first, y = A.second; n   re.push_back(make_pair(x, y)); n   if (C.second > A.second) { n     int dy = max(min(B.second, C.second), A.second); n     for (int i = A.second + 1; i <= dy; i++) { n       re.push_back(make_pair(x, i)); n     } n     y = dy; n     for (int i = A.first + 1; i <= C.first; i++) { n       re.push_back(make_pair(i, y)); n     } n     x = C.first; n     for (int i = y + 1; i <= C.second; i++) { n       re.push_back(make_pair(x, i)); n     } n   } else { n     int dy = min(max(B.second, C.second), A.second); n     for (int i = A.second - 1; i >= dy; i--) { n       re.push_back(make_pair(x, i)); n     } n     y = dy; n     for (int i = A.first + 1; i <= C.first; i++) { n       re.push_back(make_pair(i, y)); n     } n     x = C.first; n     for (int i = y - 1; i >= C.second; i--) { n       re.push_back(make_pair(x, i)); n     } n   } n   if (B.second > y) { n     for (int i = y + 1; i <= B.second; i++) { n       re.push_back(make_pair(B.first, i)); n     } n   } else { n     for (int i = y - 1; i >= B.second; i--) { n       re.push_back(make_pair(B.first, i)); n     } n   } n   printf( %d n , re.size()); n   for (int i = 0; i < re.size(); i++) { n     printf( %d %d n , re[i].first, re[i].second); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int len, pos; n void insert(int x) { n   len++; n   if (x <= pos) pos++; n } n void erase(int x) { n   if (x >= pos) n     len = x; n   else n     len -= x, pos -= x; n } n int main() { n   int m, t; n   scanf( %d%d%d%d , &len, &pos, &m, &t); n   while (t--) { n     int type, x; n     scanf( %d%d , &type, &x); n     if (type == 1) n       insert(x); n     else n       erase(x); n     printf( %d %d n , len, pos); n   } n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n const int sz = 5e5 + 10; n int ar[sz]; n int main() { n   int n, ans; n   while (cin >> n) { n     for (int i = 0; i < n; i++) cin >> ar[i]; n     sort(ar, ar + n); n     ans = n; n     for (int i = n - 1, j = (n / 2) - 1; j >= 0; j--) { n       if (ar[i] >= ar[j] * 2) { n         ans--; n         i--; n       } n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int DX[8] = {1, -1, 0, 0, 1, 1, -1, -1}; n const int DY[8] = {0, 0, 1, -1, 1, -1, 1, -1}; n const int intmax = 0x7fffffff; n map<int, int> mp; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     int a; n     scanf( %d , &a); n     mp[a]++; n   } n   map<int, int>::iterator it = mp.begin(); n   int deg = (it->second) * 2; n   int last = it->first; n   it++; n   for (; it != mp.end(); it++) { n     if ((it->first) != last + 1) { n       puts( NO ); n       return 0; n     } n     if (!deg) { n       puts( NO ); n       return 0; n     } n     deg = it->second * 2 - deg; n     if (deg < 0) { n       puts( NO ); n       return 0; n     } n     last = it->first; n   } n   if (!deg) n     puts( YES ); n   else n     puts( NO ); n } n


#include <bits/stdc++.h> n using namespace std; n const long double PI = acos(-1); n const long double eps = 0.0000000001; n const long long INF = 0x3fffffffffffffff; n long long a[200005], sum[200005], n, q, x, y; n long long lowbit(long long now) { return now & -now; } n void add(long long pos, long long v) { n   while (pos <= n) { n     sum[pos] += v; n     pos += lowbit(pos); n   } n } n long long query(long long now) { n   long long res = 0; n   while (now) { n     res += sum[now]; n     now ^= lowbit(now); n   } n   return res; n } n long long findnext(long long now) { n   long long l = now + 1, r = n + 1, v = query(now); n   while (l < r) { n     long long mid = (l + r) >> 1; n     if (query(mid) < v << 1) { n       l = mid + 1; n     } else { n       r = mid; n     } n   } n   return l; n } n signed main() { n   ios::sync_with_stdio(false); n   cin >> n >> q; n   for (long long i = 1; i <= n; ++i) { n     cin >> a[i]; n     add(i, a[i]); n   } n   for (long long i = 1; i <= q; ++i) { n     cin >> x >> y; n     add(x, -a[x]); n     a[x] = y; n     add(x, a[x]); n     long long now = 1; n     bool flag = false; n     if (a[1] == 0) { n       cout << 1 << endl; n       continue; n     } n     while (now <= n) { n       now = findnext(now); n       if (now <= n && query(now) == a[now] << 1) { n         cout << now << endl; n         flag = true; n         break; n       } n     } n     if (!flag) { n       cout << -1 << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int n, p, q, b, l; n long long int gcd(long long int a, long long int c) { n   if (!c) return a; n   return gcd(c, a % c); n } n long long int read_ll() { n   char ch; n   long long int num = 0; n   ch = getchar(); n   while (!isdigit(ch)) { n     ch = getchar(); n   } n   while (isdigit(ch)) { n     num = num * 10 + (long long int)ch - (long long int) 0 ; n     ch = getchar(); n   } n   return num; n } n int main() { n   scanf( %ld , &n); n   while (n--) { n     p = read_ll(); n     q = read_ll(); n     b = read_ll(); n     long long int d = gcd(p, q); n     p = p / d; n     q = q / d; n     while (1) { n       b = gcd(q, b); n       if (b == 1) { n         if (q == 1) n           printf( Finite n ); n         else n           printf( Infinite n ); n         break; n       } n       while (q % b == 0) q /= b; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int x, y, z; n   cin >> x >> y >> z; n   int total = x - y; n   if (total < 0) { n     if (total + z < 0) { n       cout <<  - ; n     } else { n       cout <<  ? ; n     } n   } n   if (total > 0) { n     if (total - z > 0) { n       cout <<  + ; n     } else { n       cout <<  ? ; n     } n   } n   if (total == 0) { n     if (total + z == 0) { n       cout <<  0 ; n     } else { n       cout <<  ? ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int main() { n   cin >> n; n   if (n == 1) n     cout <<  -1 ; n   else n     cout << n <<     << n + 1 <<     << n * n + n; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma comment(linker,  /STACK:100000000 ) n int n; n double l; n pair<int, int> mas[1010]; n int lst[1010]; n double dp[1010]; n bool check(double r) { n   for (int i = 0; i <= n; i++) lst[i] = -1; n   for (int i = 1; i <= n; i++) dp[i] = -1e15; n   for (int i = 0; i < n; i++) { n     if (lst[i] == -1 && i != 0) continue; n     for (int j = i + 1; j <= n; j++) { n       double p = sqrt(abs(l - (double)(mas[j].first - mas[i].first))); n       if (dp[j] < dp[i] + (double)mas[j].second * r - p) { n         lst[j] = i; n         dp[j] = dp[i] + (double)mas[j].second * r - p; n       } n     } n   } n   if (lst[n] != -1 && dp[n] > -1e-9) n     return true; n   else n     return false; n } n int main() { n   scanf( %d %lf , &n, &l); n   for (int i = 1; i <= n; i++) scanf( %d %d , &mas[i].first, &mas[i].second); n   double l = 0, r = 1e9; n   for (int i = 0; i < 100; i++) { n     double av = (l + r) / 2.0; n     if (check(av)) n       r = av; n     else n       l = av; n   } n   check(l); n   vector<int> ans; n   int cur = n; n   while (cur != 0) { n     ans.push_back(cur); n     cur = lst[cur]; n   } n   for (int i = (int)(ans).size() - 1; i >= 0; i--) printf( %d  , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int m = 31; n const int maxn = 31; n long long n, k; n long long f[maxn + 5], g[maxn + 5]; n void init() { n   f[1] = g[1] = 1; n   for (int i = 2; i <= m; i++) { n     f[i] = (1LL << i * 2 - 2) + f[i - 1]; n     g[i] = (1LL << i) - 1 + g[i - 1]; n   } n } n int main() { n   init(); n   long long T; n   cin >> T; n   while (T--) { n     cin >> n >> k; n     if (n > m) { n       printf( YES %I64d n , n - 1); n       continue; n     } n     int flg = -1; n     for (int i = 0; i <= n; i++) { n       if (k >= g[i] && n           k <= f[i] + f[n - i] * ((1ll << i) - 1) * ((1ll << i) - 1)) { n         flg = i; n         break; n       } n     } n     if (~flg) n       printf( YES %I64d n , n - flg); n     else n       printf( NO n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   int n, m; n   cin >> n >> m; n   cin >> s; n   for (int i = 0; i < m; i++) { n     int l, r; n     char c1, c2; n     scanf( %d %d %c %c , &l, &r, &c1, &c2); n     for (int j = l - 1; j < r; j++) { n       if (s[j] == c1) { n         s[j] = c2; n       } n     } n   } n   cout << s << endl; n   cin >> n; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long h, n; n   cin >> h >> n; n   long long l = 1, r = (1LL << h), ret = 0, cur = 0, dir = 0; n   while (cur < h) { n     long long size = (1LL << (h - cur)), mid = (l + r) / 2; n     if (dir == 0) { n       if (n <= mid) n         ++ret, r = mid, dir = 1; n       else n         ret += size, l = mid + 1; n     } else { n       if (n <= mid) n         ret += size, r = mid; n       else n         ++ret, l = mid + 1, dir = 0; n     } n     ++cur; n   } n   cout << ret << endl; n } n