#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int t; n   cin >> t; n   while (t--) { n     long long int x, k; n     cin >> x >> k; n     long long int temp = k % 4; n     long long int i = 1; n     while (temp--) { n       if (x % 2) { n         x += ((k / 4) * 4) + i; n       } else { n         x -= ((k / 4) * 4) + i; n       } n       i++; n     } n     cout << x << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s; n vector<char> t; n int main() { n   cin >> s; n   for (int i = 0; i < s.size(); i++) { n     if (t.size() != 0 and t.back() == s[i]) { n       t.pop_back(); n     } else { n       t.push_back(s[i]); n     } n   } n   for (int i = 0; i < t.size(); i++) { n     cout << t[i]; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n, d, i, j, k, s, t, ans; n   cin >> n >> d; n   s = 0; n   for (i = 0; i < n; i++) { n     cin >> t; n     s += t; n   } n   if (s + (n - 1) * 10 > d) n     ans = -1; n   else n     ans = (d - s) / 5; n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int gcd(int a, int b) { n   if (a % b == 0) n     return b; n   else n     return gcd(b, a % b); n } n int a, b, c, d; n int main() { n   scanf( %d %d %d %d , &a, &b, &c, &d); n   int t, bo; n   if (a * d >= c * b) { n     t = b * c; n     bo = d * a; n   } else { n     t = a * d; n     bo = c * b; n   } n   t = bo - t; n   int cc = gcd(t, bo); n   printf( %d/%d n , t / cc, bo / cc); n } n


#include <bits/stdc++.h> n using namespace std; n long long spf[101]; n long long fac[101]; n void sieve() { n   spf[1] = 1; n   for (long long i = 2; i < 101; i++) spf[i] = i; n   for (long long i = 4; i < 101; i += 2) spf[i] = 2; n   for (long long i = 3; i * i < 101; i++) { n     if (spf[i] == i) { n       for (long long j = i * i; j < 101; j += i) n         if (spf[j] == j) spf[j] = i; n     } n   } n } n map<long long, long long> getfactor(long long a) { n   map<long long, long long> m; n   while (a > 1) { n     m[spf[a]]++; n     a /= spf[a]; n   } n   return m; n } n long long power(long long x, long long y, long long p) { n   long long res = 1; n   x = x; n   if (x == 0) return 0; n   while (y > 0) { n     if (y & 1) res = (res * x) % p; n     y = y >> 1; n     x = (x * x) % p; n   } n   return res; n } n long long gcd(long long a, long long b) { n   if (a == 0) return b; n   return gcd(b % a, a); n } n long long inverse(long long a, long long p) { return power(a, p - 2, p); } n long long ncr(long long n, long long r, long long p) { n   if (r == 0) return 1; n   return (fac[n] * inverse(fac[r], p) % p * inverse(fac[n - r], p) % p) % p; n } n long long pre1[200000] = {0}, pre2[200000] = {0}; n void solve() { n   long long n; n   cin >> n; n   for (long long i = 1; i <= n; i++) { n     long long x; n     cin >> x; n     pre1[i] += pre1[i - 1] + (x == 1); n     pre2[i] += pre2[i - 1] + (x == 2); n   } n   for (long long i = n + 1; i <= 150000; i++) { n     pre1[i] += pre1[i - 1] + 1; n     pre2[i] += pre2[i - 1] + 1; n   } n   vector<pair<long long, long long> > v; n   for (long long i = 1; i <= n; i++) { n     long long j = 0; n     long long count1 = 0, count2 = 0; n     long long counter = 0; n     while (1) { n       long long g = lower_bound(pre1 + j, pre1 + 140000, pre1[j] + i) - pre1; n       long long l = lower_bound(pre2 + j, pre2 + 140000, pre2[j] + i) - pre2; n       if (g == n && l > n) { n         count1++; n         if (count1 > count2) { n           v.push_back(make_pair(count1, i)); n           break; n         } n       } n       if (l == n && g > n) { n         count2++; n         if (count2 > count1) { n           v.push_back(make_pair(count2, i)); n           break; n         } n       } n       if (l > n && g > n) break; n       if (l < g) { n         count2++; n         j = l; n       } else { n         count1++; n         j = g; n       } n     } n   } n   sort(v.begin(), v.end()); n   cout << v.size() <<   n ; n   for (auto it : v) cout << it.first <<     << it.second <<   n ; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long t = 1; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n unsigned long long hs[1050], hL[1 << 15], bs[50]; n int main() { n   mt19937_64 rnd(time(0)); n   srand(time(0)); n   int n, s, d; n   scanf( %d%d%d , &n, &s, &d); n   for (int i = 0; i < 1050; i++) hs[i] = rnd(); n   random_shuffle(hs, hs + 1050); n   for (int i = 0; i < s; i++) { n     int c; n     scanf( %d , &c); n     while (c--) { n       int k; n       scanf( %d , &k); n       bs[i] ^= hs[k]; n     } n   } n   int mid = min(10, s / 2); n   for (int bits = 0; bits < (1 << mid); bits++) { n     for (int i = 0; i < mid; i++) { n       if (bits >> i & 1) { n         hL[bits] ^= bs[i]; n       } n     } n   } n   unordered_map<unsigned long long, int> mp; n   mp[0] = 0; n   for (int bits = 0; bits < (1 << (s - mid)); bits++) { n     unsigned long long val = 0; n     for (int i = 0; i < s - mid; i++) { n       if (bits >> i & 1) { n         val ^= bs[i + mid]; n       } n     } n     int cnt = __builtin_popcount(bits); n     if (!mp.count(val) || mp[val] > cnt) { n       mp[val] = cnt; n     } n   } n   while (d--) { n     int c; n     scanf( %d , &c); n     unsigned long long v = 0; n     while (c--) { n       int k; n       scanf( %d , &k); n       v ^= hs[k]; n     } n     int res = INT_MAX; n     for (int bits = 0; bits < (1 << mid); bits++) { n       unsigned long long val = hL[bits] ^ v; n       if (mp.count(val)) res = min(res, mp[val] + __builtin_popcount(bits)); n     } n     if (res == INT_MAX) n       puts( -1 ); n     else n       printf( %d n , res); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int x_array[5000], y_array[5000]; n long long int dx[50001], dy[50001]; n int main() { n   int a; n   char str[5000]; n   long long int cnt = 0; n   scanf( %d , &a); n   scanf( %s , str); n   int len = strlen(str); n   for (int i = 0; i < len; i++) { n     int j = i + 1; n     x_array[j] = x_array[j - 1] + str[i] -  0 ; n     y_array[j] = y_array[j - 1] + str[i] -  0 ; n   }; n   for (int i = 1; i <= len; i++) { n     for (int j = i; j <= len; j++) { n       dx[x_array[j] - x_array[i - 1]]++; n       dy[y_array[j] - y_array[i - 1]]++; n     } n   } n   if (a != 0) { n     for (int i = 1; i < 50000; i++) { n       if (dx[i] && a % i == 0) { n         int temp = a / i; n         if (temp >= 50000) continue; n         cnt += dx[i] * dy[temp]; n       } n     } n   } else if (a == 0) { n     for (int i = 1; i < 50000; i++) { n       cnt += dx[0] * dy[i] + dx[i] * dy[0]; n     } n     if (dx[0]) cnt += dx[0] * dy[0]; n   } n   printf( %I64d n , cnt); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int64_t N = 1e6 + 10; n string second; n int64_t X, Y, n, m; n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m; n   for (int64_t i = 0; i < n; i++) { n     cin >> second; n     for (int64_t j = 0; j < second.size(); j++) n       if (second[j] ==  * ) X ^= j, Y ^= i; n   } n   cout << Y + 1 <<     << X + 1; n   return -0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, d[1002], s[1002], t, f, sum, k; n int main() { n   cin >> m >> k; n   for (int i = 2; i <= m + 1; i++) { n     cin >> d[i]; n   } n   for (int i = 1; i <= m; i++) { n     cin >> s[i]; n   } n   for (int i = 1; i <= m; i++) { n     f += s[i]; n     t = max(t, s[i]); n     while (f < d[i + 1]) { n       sum += k, f += t; n     } n     sum += d[i + 1], f -= d[i + 1]; n   } n   cout << sum; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     vector<long long> arr(3); n     for (int i = 0; i < 3; i++) cin >> arr[i]; n     sort(arr.begin(), arr.end()); n     if (arr[2] <= arr[1] + arr[0] + 1) n       cout <<  Yes  << endl; n     else n       cout <<  No  << endl; n   } n } n


#include <bits/stdc++.h> n const int mod = 1000000007; n using namespace std; n int dp[5009][5009]; n char s1[5009], s2[5009]; n int main() { n   while (scanf( %s%s , s1, s2) == 2) { n     memset(dp, 0, sizeof(dp)); n     int len2 = strlen(s2); n     for (int i = 1; s1[i - 1]; i++) { n       for (int j = 1; s2[j - 1]; j++) { n         if (s1[i - 1] == s2[j - 1]) { n           dp[i][j] = (dp[i - 1][j - 1] + dp[i][j - 1] + 1) % mod; n         } else n           dp[i][j] = dp[i][j - 1]; n       } n     } n     int ans = 0; n     for (int i = 1; s1[i - 1]; i++) ans = (ans + dp[i][len2]) % mod; n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename A> n ostream &operator<<(ostream &cout, vector<A> const &v); n template <typename A, typename B> n ostream &operator<<(ostream &cout, pair<A, B> const &p) { n   return cout <<  (  << p.first <<  ,  <<     << p.second <<  ) ; n } n template <typename A> n ostream &operator<<(ostream &cout, vector<A> const &v) { n   cout <<  [ ; n   for (int i = 0; i < v.size(); i++) { n     if (i) cout <<  , ; n     cout << v[i]; n   } n   return cout <<  ]  << endl; n } n const long long mod = 1000000007; n bool compare1(vector<int> &a, vector<int> &b) { n   if (a[0] == b[0]) { n     return a[1] < b[1]; n   } n   return a[0] < b[0]; n } n bool compare2(vector<int> &a, vector<int> &b) { n   if (a[1] == b[1]) { n     return a[0] < b[0]; n   } n   return a[1] < b[1]; n } n void dfs(int cur, int par, vector<vector<int>> &tree, vector<int> &vis, int &c, n          vector<int> &val) { n   vis[cur] = 1; n   c = min(c, val[cur]); n   for (auto it : tree[cur]) { n     if (!vis[it]) { n       dfs(it, cur, tree, vis, c, val); n     } n   } n } n void solve() { n   int n, k; n   cin >> n >> k; n   vector<vector<int>> val(n, vector<int>(4)); n   vector<vector<int>> tree(n); n   vector<int> vis(n); n   vector<int> cost(n); n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < 3; j++) { n       cin >> val[i][j]; n     } n     val[i][3] = i; n     cost[i] = val[i][2]; n   } n   sort(val.begin(), val.end(), compare1); n   for (int i = 1; i < n; i++) { n     if (val[i][0] == val[i - 1][0] && abs(val[i][1] - val[i - 1][1]) <= k) { n       tree[val[i][3]].push_back(val[i - 1][3]); n       tree[val[i - 1][3]].push_back(val[i][3]); n     } n   } n   sort(val.begin(), val.end(), compare2); n   for (int i = 1; i < n; i++) { n     if (val[i][1] == val[i - 1][1] && abs(val[i][0] - val[i - 1][0]) <= k) { n       tree[val[i][3]].push_back(val[i - 1][3]); n       tree[val[i - 1][3]].push_back(val[i][3]); n     } n   } n   deque<int> st; n   for (int i = 0; i < n; i++) { n     if (!vis[i]) { n       int c = INT_MAX; n       dfs(i, i, tree, vis, c, cost); n       st.push_back(c); n     } n   } n   sort(st.begin(), st.end()); n   int t = 0; n   while (((int)st.size())) { n     while (((int)st.size()) && st.front() == t) { n       st.pop_front(); n     } n     if (((int)st.size())) { n       st.pop_back(); n     } n     if (((int)st.size())) { n       t++; n     } n   } n   cout << t <<   n ; n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int tc = 1; n   cin >> tc; n   while (tc--) { n     solve(); n   } n   return 0; n } n


#include<stdio.h> n #include<vector> n #include<algorithm> n using namespace std; n #define R register int n #define I inline n #define N 100001 n vector<int>G[N],C[N],H[N]; n int dep[N],g[448][N],fa[N],b[448],sz[N]; n char ans[N]; n bool f[448][N]; n struct Painter{ n  char Character; n  int LeftNum; n }; n I void DFS(int x){ n  dep[x]=dep[fa[x]]+1; n  C[dep[x]].push_back(x); n  sz[x]=1; n  for(vector<int>::iterator T=G[x].begin();T!=G[x].end();T++){ n   DFS(*T); n   sz[x]+=sz[*T]; n  } n } n I bool Compare(int x,int y){ n  return sz[x]<sz[y]; n } n I void Swap(Painter&A,Painter&B){ n  Painter Tem=A; n  A=B; n  B=Tem; n } n int main(){ n  Painter A,B; n  A.Character= a ; n  B.Character= b ; n  int n,maxdep=0; n  scanf( %d%d ,&n,&A.LeftNum); n  B.LeftNum=n-A.LeftNum; n  for(int i=2;i<=n;i++){ n   scanf( %d ,fa+i); n   G[fa[i]].push_back(i); n  } n  DFS(1); n  for(R i=1;i<=n;i++){ n   if(dep[i]>maxdep){ n    maxdep=dep[i]; n   } n  } n  for(int i=1;i<=maxdep;i++){ n   H[C[i].size()].push_back(i); n  } n  f[0][0]=true; n  int tot=0; n  for(R i=1;i<=n;i++){ n   if(H[i].empty()==false){ n    tot++; n    b[tot]=i; n    int ct=H[i].size(); n    for(R j=0;j!=i;j++){ n     f[tot][j]=f[tot-1][j]; n    } n    for(R j=i;j<=n;j++){ n     if(f[tot-1][j]==true){ n      f[tot][j]=true; n     }else{ n      if(f[tot][j-i]==true){ n       g[tot][j]=g[tot][j-i]+1; n       if(g[tot][j]<=ct){ n        f[tot][j]=true; n       } n      } n     } n    } n   } n  } n  if(f[tot][A.LeftNum]==true){ n   printf( %d n ,maxdep); n   int p=A.LeftNum; n   for(R i=1;i<=n;i++){ n    ans[i]= b ; n   } n   for(R i=tot;i!=0;i--){ n    int t=g[i][p]; n    p-=t*b[i]; n    for(R j=0;j!=t;j++){ n     int d=H[b[i]][j]; n     for(vector<int>::iterator T=C[d].begin();T!=C[d].end();T++){ n      ans[*T]= a ; n     } n    } n   } n  }else{ n   printf( %d n ,maxdep+1); n   for(R i=1;i<=maxdep;i++){ n    sort(C[i].begin(),C[i].end(),Compare); n    if(A.LeftNum<B.LeftNum){ n     Swap(A,B); n    } n    while(C[i].empty()==false&&A.LeftNum!=0){ n     ans[C[i].back()]=A.Character; n     A.LeftNum--; n     C[i].pop_back(); n    } n    while(C[i].empty()==false){ n     ans[C[i].back()]=B.Character; n     B.LeftNum--; n     C[i].pop_back(); n    } n   } n  } n  for(R i=1;i<=n;i++){ n   putchar(ans[i]); n  } n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n string s, u; n stack<char> t; n vector<char> mins; n int main() { n   cin >> s; n   u =   ; n   int inicio = 0, tam = s.length(); n   char ant =  z  + 1; n   for (int i = tam - 1; i >= 0; i--) { n     ant = min(ant, s[i]); n     mins.push_back(ant); n   } n   reverse(mins.begin(), mins.end()); n   while (inicio < tam) { n     if (t.empty()) { n       t.push(s[inicio]); n       inicio++; n     } else { n       if (t.top() <= mins[inicio]) { n         u += t.top(); n         t.pop(); n       } else { n         t.push(s[inicio]); n         inicio++; n       } n     } n   } n   while (!t.empty()) { n     u += t.top(); n     t.pop(); n   } n   if (inicio < tam) { n     u += s[inicio]; n   } n   cout << u <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = 1e18; n const long double eps = 1e-6; n const long long maxn = 1e6 + 100; n vector<long long> g[maxn]; n long long szc1[maxn], szc2[maxn]; n vector<long long> used; n void dfsc(long long v, long long col) { n   used[v] = 1; n   szc1[col]++; n   szc2[col]++; n   for (long long u : g[v]) { n     if (used[u]) continue; n     dfsc(u, col); n   } n } n bitset<maxn> dp; n signed main() { n   ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); n   long long n, k; n   cin >> n >> k; n   vector<long long> p(n); n   for (long long i = 0; i < n; ++i) { n     cin >> p[i], --p[i]; n     g[i].push_back(p[i]); n     g[p[i]].push_back(i); n   } n   long long idxc = 0; n   used.assign(n, 0); n   for (long long i = 0; i < n; ++i) { n     if (!used[i]) { n       dfsc(i, idxc); n       ++idxc; n     } n   } n   long long ansl = 0, ansr = 0; n   long long kl = k, kr = k; n   for (long long i = 0; i < idxc; ++i) { n     if (szc1[i] >= 2 && kr) { n       --kr; n       ansr += 2; n     } n   } n   long long add = 0; n   for (long long i = 0; i < idxc && kr; ++i) { n     long long t = szc1[i]; n     long long x = t / 2 - 1; n     if (t >= 3 && t % 2) { n       ++add; n     } n     x = min(x, kr); n     if (x >= 1) { n       ansr += 2 * x; n       kr -= x; n     } n   } n   add = min(add, kr); n   ansr += add; n   kr -= add; n   vector<pair<long long, long long>> gg; n   vector<long long> cnt(n + 1, 0), us(n + 1, 0); n   for (long long i = 0; i < idxc; ++i) { n     cnt[szc2[i]]++; n   } n   for (long long i = 0; i < idxc; ++i) { n     if (us[szc2[i]]) continue; n     gg.push_back({szc2[i], cnt[szc2[i]]}); n     us[szc2[i]] = 1; n   } n   dp[0] = 1; n   for (long long i = 0; i < (long long)gg.size(); ++i) { n     for (long long t = 1; t <= gg[i].second; t *= 2) { n       long long x = t * gg[i].first; n       auto bb = dp; n       bb <<= x; n       dp |= bb; n       gg[i].second -= t; n     } n     long long x = gg[i].second * gg[i].first; n     auto bb = dp; n     bb <<= x; n     dp |= bb; n   } n   if (dp[kl]) n     ansl = kl; n   else n     ansl = kl + 1; n   cout << ansl <<     << ansr; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n vector<vector<int> > adj(1e5 + 1); n set<int> rem; n vector<int> ord; n void search(int pred1, int pred2) { n   if (ord.size() == n) return; n   int nxt = 0; n   for (int j = 0; j < 4 && nxt == 0; j++) { n     for (int k = 0; k < 4; k++) { n       int x = adj[pred1][j], y = adj[pred2][k]; n       if (x == y && rem.find(x) != rem.end()) { n         nxt = x; n         rem.erase(x); n         break; n       } n     } n   } n   if (nxt) { n     ord.push_back(nxt); n     search(nxt, pred1); n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n; n   for (int i = 0; i < 2 * n; i++) { n     int a, b; n     cin >> a >> b; n     adj[a].push_back(b); n     adj[b].push_back(a); n   } n   for (int i = 1; i <= n; i++) { n     if (adj[i].size() != 4) { n       cout << -1 <<   n ; n       return 0; n     } n   } n   for (int i = 0; i < 4; i++) { n     for (int j = i + 1; j < 4; j++) { n       rem.clear(); n       ord.clear(); n       for (int k = 2; k <= n; k++) { n         rem.insert(k); n       } n       ord.push_back(adj[1][i]); n       rem.erase(adj[1][i]); n       ord.push_back(1); n       ord.push_back(adj[1][j]); n       rem.erase(adj[1][j]); n       search(adj[1][j], 1); n       if (ord.size() == n) { n         for (int i = 0; i < n; i++) { n           cout << ord[i] <<    ; n         } n         cout <<   n ; n         return 0; n       } n     } n   } n   cout << -1 <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 1e5 + 6.66; n const int MAXN = MAX << 2; n const int N = MAX << 1; n pair<int, int> p[MAX]; n int up[MAXN]; n int down[MAXN]; n int uppre[MAXN], upsuf[MAXN], downpre[MAXN], downsuf[MAXN]; n int main() { n   memset(down, 63, sizeof down); n   memset(downpre, 63, sizeof downpre); n   memset(downsuf, 63, sizeof downsuf); n   memset(up, -63, sizeof up); n   memset(uppre, -63, sizeof uppre); n   memset(upsuf, -63, sizeof upsuf); n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) cin >> p[i].first >> p[i].second; n   for (int i = 0; i < n; i++) { n     int x = p[i].first + p[i].second + N; n     int y = p[i].first - p[i].second + N; n     up[x] = max(up[x], y); n     down[x] = min(down[x], y); n   } n   for (int i = 0; i < MAXN; i++) uppre[i] = upsuf[i] = up[i]; n   for (int i = 0; i < MAXN; i++) downpre[i] = downsuf[i] = down[i]; n   for (int i = 0; i < MAXN - 2; i++) { n     uppre[i + 2] = max(uppre[i + 2], uppre[i]); n     downpre[i + 2] = min(downpre[i + 2], downpre[i]); n   } n   for (int i = MAXN - 1; i > 1; i--) { n     upsuf[i - 2] = max(upsuf[i], upsuf[i - 2]); n     downsuf[i - 2] = min(downsuf[i - 2], downsuf[i]); n   } n   for (int i = 0; i < MAXN; i++) n     down[i] = max((i - 1 > -1 && i - 1 < MAXN ? downpre[i - 1] : -1e9 - 7), n                   (i + 1 > -1 && i + 1 < MAXN ? downsuf[i + 1] : -1e9 - 7)); n   for (int i = 0; i < MAXN; i++) n     up[i] = min((i - 1 > -1 && i - 1 < MAXN ? uppre[i - 1] : 1e9 + 7), n                 (i + 1 > -1 && i + 1 < MAXN ? upsuf[i + 1] : 1e9 + 7)); n   long long ans = 0; n   for (int i = 1; i < MAXN - 1; i++) { n     ans += max(0, up[i] - down[i]) / 2; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long dx[] = {-1, 0, 0, 1}; n const long long dy[] = {0, -1, 1, 0}; n const int Mass = (int)2e5 + 5; n const int inf = (int)1e9 + 5; n const long long linf = (long long)1e18 + 5; n const long long Mod = (long long)1e9 + 7; n long long ModFix(long long a, long long Mod) { n   return ((((a) % (Mod)) + (Mod)) % (Mod)); n } n long long ModSum(long long a, long long b, long long Mod) { n   return ModFix(((a) + (b)), (Mod)); n } n long long ModSub(long long a, long long b, long long Mod) { n   return ModFix(((a) - (b)), (Mod)); n } n long long ModMul(long long a, long long b, long long Mod) { n   return ModFix(((a) * (b)), (Mod)); n } n int a[150]; n int main() { n   ios_base ::sync_with_stdio(false); n   cin.tie(NULL); n   int n, m; n   cin >> n; n   cin >> m; n   int res = 0; n   for (int i = 1; i <= n; ++i) cin >> a[i]; n   for (int i = 1; i <= m; ++i) { n     int l, r; n     cin >> l >> r; n     int sum = 0; n     for (int j = l; j <= r; ++j) { n       sum += a[j]; n     } n     if (sum >= 0) res += sum; n   } n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n const int N = 1000 + 10; n struct tnode { n   int x, y; n   long long val; n   tnode() {} n   tnode(int _x, int _y, long long _val) { n     x = _x; n     y = _y; n     val = _val; n   } n }; n int n, m, r, c; n int a[N][N], b[N][N], f[N][N]; n long long sum[N][N]; n int Q[N], head, tail; n std::vector<tnode> ans, out; n bool vis[N][N]; n long long S(int x1, int y1, int x2, int y2) { n   return sum[x2][y2] - sum[x2][y1 - 1] - sum[x1 - 1][y2] + sum[x1 - 1][y1 - 1]; n } n bool cmp(const tnode& i, const tnode& j) { n   if (i.val != j.val) n     return i.val < j.val; n   else if (i.x != j.x) n     return i.x < j.x; n   else n     return i.y < j.y; n } n int main() { n   scanf( %d%d%d%d , &n, &m, &r, &c); n   for (int i = 1; i <= n; ++i) n     for (int j = 1; j <= m; ++j) { n       scanf( %d , &a[i][j]); n       sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + a[i][j]; n     } n   for (int i = 1; i <= n; ++i) { n     head = tail = 0; n     for (int j = 1; j <= m; ++j) { n       while (head != tail && a[i][Q[tail - 1]] >= a[i][j]) { n         if (--tail < 0) tail = N - 1; n       } n       while (head != tail && Q[head] < j - c + 1) { n         if (++head == N) head = 0; n       } n       Q[tail++] = j; n       if (tail == N) tail = 0; n       if (j >= c) b[i][j - c + 1] = a[i][Q[head]]; n     } n   } n   for (int j = 1; j + c - 1 <= m; ++j) { n     head = tail = 0; n     for (int i = 1; i <= n; ++i) { n       while (head != tail && b[Q[tail - 1]][j] >= b[i][j]) { n         if (--tail < 0) tail = N - 1; n       } n       while (head != tail && Q[head] < i - r + 1) { n         if (++head == N) head = 0; n       } n       Q[tail++] = i; n       if (tail == N) tail = 0; n       if (i >= r) f[i - r + 1][j] = b[Q[head]][j]; n     } n   } n   for (int i = 1; i + r - 1 <= n; ++i) n     for (int j = 1; j + c - 1 <= m; ++j) { n       ans.push_back(tnode( n           i, j, S(i, j, i + r - 1, j + c - 1) - (long long)f[i][j] * r * c)); n     } n   std::sort(ans.begin(), ans.end(), cmp); n   for (int i = 0; i < ans.size(); ++i) { n     if (vis[ans[i].x][ans[i].y]) continue; n     out.push_back(ans[i]); n     for (int j = std::max(1, ans[i].x - r + 1); j <= ans[i].x + r - 1; ++j) n       for (int k = std::max(1, ans[i].y - c + 1); k <= ans[i].y + c - 1; ++k) n         vis[j][k] = true; n   } n   printf( %d n , out.size()); n   for (int i = 0; i < out.size(); ++i) { n     printf( %d %d %I64d n , out[i].x, out[i].y, out[i].val); n   } n   return 0; n } n


#include <bits/stdc++.h> n using LL = long long; n const LL inf = 1LL << 60; n const int kN = 14; n int a[kN], n; n int delta[kN]; n int cnt[1 << kN]; n LL f[1 << kN]; n int decode(char c) { n   if (c >=  0  && c <=  9 ) return c -  0 ; n   return c -  a  + 10; n } n char encode(int x) { n   if (x < 10) return x +  0 ; n   return x - 10 +  a ; n } n LL solve() { n   for (int mask = 1; mask < 1 << n; ++mask) { n     int p = __builtin_popcount((mask & -mask) - 1); n     cnt[mask] = cnt[mask ^ 1 << p] - delta[p]; n     f[mask] = inf; n     if (cnt[mask] < 0 || cnt[mask] >= 16) continue; n     for (int i = 0; i < n; ++i) n       if (mask >> i & 1) { n         int q = cnt[mask ^ 1 << i]; n         if (q < 0 || q >= 16) continue; n         f[mask] = n             std::min(f[mask], f[mask ^ 1 << i] + (cnt[mask] * 1LL << 4 * i)); n       } n   } n   return f[(1 << n) - 1]; n } n int main() { n   char str[15]; n   scanf( %s , str); n   n = strlen(str); n   for (int i = 0; i < n; ++i) a[i] = decode(str[n - 1 - i]); n   LL result = inf; n   for (int mask = 0; mask < 1 << n - 1; ++mask) { n     bool ok = true; n     int sum = 0; n     for (int i = 0; i < n; ++i) { n       int d = a[i]; n       if (mask >> i & 1) d -= 16; n       if (i && (mask >> i - 1 & 1)) d += 1; n       ok &= -16 < d && d < 16; n       sum += d; n       delta[i] = d; n     } n     if (ok && sum == 0) result = std::min(result, solve()); n   } n   if (result == inf) n     puts( NO ); n   else { n     for (int i = n - 1; i >= 0; --i) putchar(encode(result >> 4 * i & 15)); n     puts(  ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, b, g, ans; n int main() { n   scanf( %d%d%d , &b, &g, &n); n   for (register int i = 0; i <= n; ++i) { n     register int a = i, f = n - i; n     if (a <= b && f <= g) ++ans; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int t, a, b, c; n int main() { n   cin >> t; n   for (; t; t--) { n     cin >> a >> b >> c; n     int res = 0; n     if (c >= b) { n       swap(b, c); n     } n     if (b >= a) { n       swap(a, b); n     } n     if (c > 0) { n       c--; n       res++; n     } n     if (b > 0) { n       b--; n       res++; n     } n     if (a > 0) { n       a--; n       res++; n     } n     if ((a > 0) && (b > 0)) { n       a--; n       b--; n       res++; n     } n     if ((c > 0) && (a > 0)) { n       a--; n       c--; n       res++; n     } n     if ((c > 0) && (b > 0)) { n       c--; n       b--; n       res++; n     } n     if ((c > 0) && (b > 0) && (a > 0)) { n       a--; n       b--; n       c--; n       res++; n     } n     cout << res <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   long long v; n   bool c; n } arr[12000]; n bool comparator(node a, node b) { return a.v < b.v; } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long i, j, p, v, l, n = 0, a, ans = 0; n   cin >> a; n   string s; n   cin >> s; n   l = s.length(); n   s =  +  + s; n   for (i = 0; i < l; i += 3) { n     v = 0; n     p = s[i++] ==  +  ? 1 : -1; n     if (s[i] >=  0  and s[i] <=  9 ) n       for (; s[i] >=  0  and s[i] <=  9 ; i++) v = v * 10 + s[i] - 48; n     else n       v = 1; n     if (s[i] ==  * ) i++; n     n++; n     arr[n].v = v * p; n     arr[n].c = s[i] ==  a ; n   } n   sort(arr + 1, arr + 1 + n, comparator); n   for (i = 1; i <= n; i++) { n     if (arr[i].c) n       ans += arr[i].v * a++; n     else n       ans += arr[i].v * ++a; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 3000 + 50; n struct Edge { n   int to, next; n   long long w; n } edge[maxn * 2]; n int k = 1, head[maxn]; n void add(int u, int v) { n   edge[++k].to = v; n   edge[k].next = head[u]; n   head[u] = k; n } n long long n, m, b[maxn], w[maxn], v[maxn], sz[maxn], T; n pair<long long, long long> dp[maxn][maxn], temp[maxn]; n void update(pair<long long, long long> &x, pair<long long, long long> y) { n   if (y > x) x = y; n } n void dfs(int u, int f) { n   sz[u] = 1; n   dp[u][1] = make_pair(0, v[u]); n   for (int i = head[u]; i; i = edge[i].next) { n     if (edge[i].to == f) continue; n     dfs(edge[i].to, u); n     for (int j = 1; j <= sz[u] + sz[edge[i].to]; j++) n       temp[j] = make_pair(-1, 0); n     for (int j = 1; j <= sz[u]; j++) { n       for (int k = 1; k <= sz[edge[i].to]; k++) { n         update(temp[j + k], make_pair(dp[u][j].first + dp[edge[i].to][k].first + n                                           (dp[edge[i].to][k].second > 0), n                                       dp[u][j].second)); n         update(temp[j + k - 1], n                make_pair(dp[u][j].first + dp[edge[i].to][k].first, n                          dp[u][j].second + dp[edge[i].to][k].second)); n       } n     } n     for (int j = 1; j <= sz[u] + sz[edge[i].to]; j++) dp[u][j] = temp[j]; n     sz[u] += sz[edge[i].to]; n   } n } n int main() { n   cin >> T; n   while (T--) { n     cin >> n >> m; n     for (int i = 1; i <= n; i++) cin >> b[i], v[i] -= b[i]; n     for (int i = 1; i <= n; i++) cin >> w[i], v[i] += w[i]; n     for (int i = 1; i < n; i++) { n       int a, b; n       cin >> a >> b; n       add(a, b); n       add(b, a); n     } n     dfs(1, 0); n     long long ans = dp[1][m].first + (dp[1][m].second > 0); n     cout << ans << endl; n     k = 1; n     memset(head, 0, sizeof(int) * (n + 10)); n     memset(v, 0, sizeof(long long) * (n + 10)); n     memset(sz, 0, sizeof(long long) * (n + 10)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n T gcd(T x, T y) { n   if (x < y) swap(x, y); n   while (y > 0) { n     T f = x % y; n     x = y; n     y = f; n   } n   return x; n } n const int maxn = 100007; n int BLOCKSIZE = 250; n set<int> s[maxn]; n int a[maxn]; n int nxt[maxn]; n int prv[maxn]; n int n, m; n long long b[maxn]; n int d[maxn]; n int lst[maxn]; n long long query(int block, int R) { n   int iBlockStart = block * BLOCKSIZE; n   int iBlockEnd = min((block + 1) * BLOCKSIZE, n); n   int idx = upper_bound(d + iBlockStart, d + iBlockEnd, R, n                         [](int i, int j) { return i < nxt[j]; }) - n             d; n   if (idx == iBlockStart) return 0; n   return b[idx - 1]; n } n void updateBlock(int block) { n   int iBlockStart = block * BLOCKSIZE; n   int iBlockEnd = min((block + 1) * BLOCKSIZE, n); n   sort(d + iBlockStart, d + iBlockEnd, n        [](int i, int j) { return nxt[i] < nxt[j]; }); n   b[iBlockStart] = nxt[d[iBlockStart]] - d[iBlockStart]; n   for (int i = iBlockStart + 1; i < iBlockEnd; i++) n     b[i] = b[i - 1] + nxt[d[i]] - d[i]; n } n int main(int argc, char *argv[]) { n   std::cin.sync_with_stdio(false); n   std::cin.tie(nullptr); n   cin >> n >> m; n   { n     for (int i = 0; i < n; i++) nxt[i] = n, prv[i] = -1, d[i] = i; n     memset(lst, -1, sizeof(lst)); n     for (int i = 0; i < n; i++) { n       cin >> a[i]; n       int pr = lst[a[i]]; n       if (pr != -1) { n         nxt[pr] = i; n         prv[i] = pr; n       } n       lst[a[i]] = i; n       s[a[i]].insert(i); n     } n     for (int i = 0; i * BLOCKSIZE < n; i++) updateBlock(i); n     for (int i = 0; i < m; i++) { n       int op; n       cin >> op; n       if (op == 1) { n         int p, x; n         cin >> p >> x; n         p--; n         if (a[p] == x) continue; n         if (nxt[p] != n) prv[nxt[p]] = prv[p]; n         if (prv[p] != -1) { n           nxt[prv[p]] = nxt[p]; n           if (prv[p] / BLOCKSIZE != p / BLOCKSIZE) n             updateBlock(prv[p] / BLOCKSIZE); n         } n         s[a[p]].erase(p); n         a[p] = x; n         nxt[p] = n; n         prv[p] = -1; n         auto it = s[x].emplace(p).first; n         bool updatedNext = false; n         if (it != s[x].begin()) { n           int pr = *prev(it); n           int nx = nxt[pr]; n           nxt[pr] = p; n           prv[p] = pr; n           if (pr / BLOCKSIZE != p / BLOCKSIZE) updateBlock(pr / BLOCKSIZE); n           if (nx != n) { n             prv[nx] = p; n             nxt[p] = nx; n             updatedNext = true; n           } n         } n         if (!updatedNext) { n           auto nxiter = next(it); n           if (nxiter != s[x].end()) { n             int nx = *nxiter; n             prv[nx] = p; n             nxt[p] = nx; n           } n         } n         updateBlock(p / BLOCKSIZE); n       } else { n         int l, r, R; n         cin >> l >> r; n         l--; n         r--; n         R = r; n         long long res = 0; n         for (; l <= r && l % BLOCKSIZE; l++) n           if (nxt[l] <= R) res += nxt[l] - l; n         for (; l <= r && (r + 1) % BLOCKSIZE; r--) n           if (nxt[r] <= R) res += nxt[r] - r; n         if (l <= r) { n           for (int i = l / BLOCKSIZE, j = r / BLOCKSIZE; i <= j; i++) { n             res += query(i, R); n           } n         } n         cout << res << endl; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n priority_queue<pair<int, int> > p1, p2, p12; n int n, s1, s2; n int num1[3005]; n int num2[3005]; n bool vis[3005]; n int ans1[3005]; n int ans2[3005]; n int main() { n   cin >> n >> s1 >> s2; n   for (int i = 0; i < n; i++) { n     scanf( %d , &num1[i]); n     p1.push(make_pair(num1[i], i)); n   } n   for (int i = 0; i < n; i++) { n     scanf( %d , &num2[i]); n     p2.push(make_pair(num2[i], i)); n   } n   long long ans = 0; n   for (int i = 0; i < s1; i++) { n     pair<int, int> u; n     u = p1.top(); n     p1.pop(); n     vis[u.second] = true; n     ans1[i] = u.second; n     ans += u.first; n     p12.push(make_pair(num2[u.second] - num1[u.second], i)); n   } n   for (int i = 0; i < s2; i++) { n     pair<int, int> u, v, w; n     while (!p1.empty() && vis[p1.top().second]) p1.pop(); n     while (!p2.empty() && vis[p2.top().second]) p2.pop(); n     if (!p1.empty()) u = p1.top(); n     if (!p2.empty()) v = p2.top(); n     w = p12.top(); n     if (p2.empty() || u.first + w.first > v.first) { n       ans2[i] = ans1[w.second]; n       ans1[w.second] = u.second; n       vis[u.second] = true; n       ans += u.first + w.first; n       p1.pop(); n       int idx = u.second; n       p12.push(make_pair(num2[idx] - num1[idx], w.second)); n       p12.pop(); n     } else { n       ans += v.first; n       ans2[i] = v.second; n       vis[v.second] = true; n       p2.pop(); n     } n   } n   cout << ans << endl; n   for (int i = 0; i < s1; i++) { n     cout << ans1[i] + 1 <<    ; n   } n   cout << endl; n   for (int i = 0; i < s2; i++) { n     cout << ans2[i] + 1 <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int mx = 2e6 + 10; n int n, m; n bool vis[mx]; n int sta[mx], siz, is; n int dfn[mx], id[mx]; n int be[mx], ty; n vector<int> g[mx]; n void tarjan(int u) { n   dfn[u] = id[u] = ++is; n   sta[++siz] = u; n   vis[u] = 1; n   for (int v : g[u]) { n     if (!dfn[v]) { n       tarjan(v); n       id[u] = min(id[u], id[v]); n     } else if (vis[v]) n       id[u] = min(id[u], dfn[v]); n   } n   if (id[u] == dfn[u]) { n     ty++; n     while (sta[siz] != u) { n       be[sta[siz]] = ty; n       vis[sta[siz]] = 0; n       siz--; n     } n     be[u] = ty, vis[u] = 0, siz--; n   } n } n int cnt_one() { n   int cnt = 0; n   for (int i = 1; i <= n; i++) n     if (be[i] == 1) cnt++; n   return cnt; n } n int main() { n   int t; n   scanf( %d , &t); n   while (t--) { n     scanf( %d%d , &n, &m); n     for (int i = 1; i <= 2 * n; i++) g[i].clear(); n     ty = is = siz = 0; n     for (int i = 1; i <= 2 * n; i++) dfn[i] = vis[i] = 0; n     for (int i = 0, u, v; i < m; i++) { n       scanf( %d%d , &u, &v); n       if (u == v) n         g[v + n].push_back(u); n       else n         g[u].push_back(v + n); n     } n     if (n == 1) { n       puts( No ); n       continue; n     } n     for (int i = 1; i <= n; i++) n       if (!dfn[i]) tarjan(i); n     int cnt = cnt_one(); n     if (cnt == n) n       puts( No ); n     else { n       puts( Yes ); n       printf( %d %d n , cnt, n - cnt); n       for (int i = 1; i <= n; i++) n         if (be[i] == 1) printf( %d  , i); n       puts(  ); n       for (int i = 1; i <= n; i++) n         if (be[i] != 1) printf( %d  , i); n       puts(  ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   long long n, m, k, i; n   bool t = 0; n   cin >> n >> m >> k; n   if ((2 * m * n) % k > 0) n     cout <<  NO ; n   else { n     if (k == 2) n       cout <<  YES n0 0 n  << n <<   0 n0   << m; n     else { n       k = (2 * m * n) / k; n       if (m >= n) { n         for (i = max(1, (int)(k / m)); i <= n; i++) { n           if (k % i == 0 && k / i <= m) { n             t = 1; n             break; n           } n         } n         if (!t) n           cout <<  NO ; n         else n           cout <<  YES n0 0 n  << i <<   0 n0   << k / i; n       } else { n         for (i = max(1, (int)(k / n)); i <= m; i++) { n           if (k % i == 0 && k / i <= n) { n             t = 1; n             break; n           } n         } n         if (!t) n           cout <<  NO ; n         else n           cout <<  YES n0 0 n  << k / i <<   0 n0   << i; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long read(long long num = 0, char c = 0) { n   while (c >  9  || c <  0 ) c = getchar(); n   while (c >=  0  && c <=  9 ) num = num * 10 + c - 48, c = getchar(); n   return num; n } n struct node { n   int x, y, val, num; n   void in() { x = read(), y = read(), val = read(); } n } p[500010]; n int f[2005], n, m, q, l, r; n int find(int x) { return f[x] == x ? x : f[x] = find(f[x]); } n bool cmp(node a, node b) { return a.val > b.val; } n int work() { n   for (register int i = 1; i <= m; ++i) { n     if (p[i].num >= l && p[i].num <= r) { n       if (find(p[i].x) == find(p[i].y)) { n         return p[i].val; n       } else { n         f[find(p[i].x + n)] = f[find(p[i].y)]; n         f[find(p[i].y + n)] = f[find(p[i].x)]; n       } n     } n   } n   return -1; n } n int main() { n   n = read(), m = read(), q = read(); n   for (register int i = 1; i <= m; ++i) p[i].in(), p[i].num = i; n   sort(p + 1, p + 1 + m, cmp); n   for (register int i = 1; i <= q; ++i) { n     l = read(), r = read(); n     for (register int i = 1; i <= 2 * n; ++i) f[i] = i; n     printf( %d n , work()); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool isSafe(const int n, const int m, const int row, const int col) { n   return row >= 0 && row < n && col >= 0 && col < m; n } n int main(int argc, char* argv[]) { n   int n, m; n   static pair<int, int> pos[] = {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 0}, n                                  {0, 1},   {1, -1}, {1, 0},  {1, 1}}; n   int posLen = sizeof(pos) / sizeof(pos[0]); n   cin >> n >> m; n   vector<string> vec(n); n   for (int i = 0; i < n; i++) { n     cin >> vec[i]; n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       int count = 0; n       if (vec[i][j] ==  * ) { n         continue; n       } n       for (int k = 0; k < posLen; k++) { n         if (isSafe(n, m, i + pos[k].first, j + pos[k].second) && n             vec[i + pos[k].first][j + pos[k].second] ==  * ) { n           count++; n         } n       } n       if ((count == 0 && vec[i][j] !=  . ) || n           (count != 0 && count != vec[i][j] -  0 )) { n         cout <<  NO ; n         return 0; n       } n     } n   } n   cout <<  YES ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct box { n   int a, o; n   int id; n }; n int n; n box x[201000]; n int cmp(box a, box b) { return a.a < b.a; } n int used[201000]; n int main() { n   int t; n   scanf( %d , &t); n   for (int tt = 0; tt < t; tt++) { n     scanf( %d , &n); n     n = 2 * n - 1; n     for (int i = 1; i <= n; i++) used[i] = 0; n     for (int i = 1; i <= n; i++) { n       scanf( %d%d , &x[i].a, &x[i].o); n       x[i].id = i; n     } n     printf( YES n ); n     if (n == 1) { n       printf( 1 n ); n       continue; n     } n     sort(x + 1, x + n + 1, cmp); n     for (int i = 1; i < n; i++) n       if (x[i].o <= x[i + 1].o) { n         printf( %d  , x[i + 1].id); n         used[i] = 1, used[i + 1] = 1, i++; n       } n     int tk = 0; n     for (int i = 1; i <= n; i++) n       if (!used[i]) { n         tk++; n         if (tk % 2 == 1) printf( %d  , x[i].id); n       } n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( -O2 ) n using namespace std; n void err(istream_iterator<string> it) { cerr << endl; } n template <typename T, typename... Args> n void err(istream_iterator<string> it, T a, Args... args) { n   cerr << *it <<   =   << a <<   t ; n   err(++it, args...); n } n template <typename T1, typename T2> n ostream& operator<<(ostream& c, pair<T1, T2>& v) { n   c <<  (  << v.first <<  ,  << v.second <<  ) ; n   return c; n } n template <template <class...> class TT, class... T> n ostream& operator<<(ostream& out, TT<T...>& c) { n   out <<  {  ; n   for (auto& x : c) out << x <<    ; n   out <<  } ; n   return out; n } n const int LIM = 1.5e3 + 5, MOD = 998244353; n const long double EPS = 1e-9; n int read() { n   int xx = 0, ff = 1; n   char ch = getchar(); n   while (ch >  9  || ch <  0 ) { n     if (ch ==  - ) ff = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     xx = (xx << 3) + (xx << 1) + ch -  0 ; n     ch = getchar(); n   } n   return xx * ff; n } n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n int n; n int link[LIM], sz[LIM], edges[LIM]; n long long poly[LIM][LIM]; n int find(int x) { n   if (x == link[x]) return x; n   return link[x] = find(link[x]); n } n void polymul(int a, int b) { n   long long temp[LIM] = {0}; n   for (int i = 0; i < n + 1; ++i) { n     for (int j = 0; j + i <= n; j++) { n       temp[i + j] += (poly[a][i] * poly[b][j]) % MOD; n     } n   } n   for (int i = 0; i < LIM; ++i) { n     poly[a][i] = temp[i] % MOD; n   } n   return; n } n void unite(int a, int b) { n   a = find(a); n   b = find(b); n   if (sz[a] < sz[b]) swap(a, b); n   if (a == b) { n     edges[a]++; n     if (edges[a] == sz[a] * (sz[a] - 1) / 2) { n       poly[a][1]++; n     } n     return; n   } n   link[b] = a; n   sz[a] += sz[b]; n   polymul(a, b); n   edges[a]++; n   edges[a] += edges[b]; n   if (edges[a] == sz[a] * (sz[a] - 1) / 2) { n     poly[a][1]++; n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n; n   for (int i = 0; i < n; ++i) { n     poly[i][1] = 1; n     link[i] = i; n     sz[i] = 1; n   } n   vector<tuple<int, int, int>> v; n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < n; ++j) { n       int x; n       cin >> x; n       if (i < j) { n         v.push_back({x, i, j}); n       } n     } n   } n   sort(v.begin(), v.end()); n   for (auto& r : v) { n     int cost, first, second; n     tie(cost, first, second) = r; n     unite(first, second); n   } n   int a = find(0); n   for (int i = 1; i < n + 1; ++i) { n     cout << poly[a][i] % MOD <<    ; n   } n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n  n typedef long long int ll; n #define M 1000000007 n  n int main() { n     std::ios_base::sync_with_stdio(false); n     cin.tie(NULL); n     cout.tie(NULL); n      n     int t; n     cin>>t; n     while(t--) n     { n         string s; n         cin>>s; n         for(int i=0; i<s.length(); i++) n         { n             if(i%2==0) n             { n                 if(s[i] !=  a ) n                     s[i] =  a ; n                 else n                     s[i] =  b ; n             } n             else n             { n                 if(s[i] !=  z ) n                     s[i] =  z ; n                 else n                     s[i] =  y ; n             } n         } n         cout<<s<<endl; n     } n  return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int Maxlen = 2e5 + 2; n long long ans; n long long lt[Maxlen], ls[2 * Maxlen]; n int n, m, dpt[Maxlen], dps[Maxlen]; n char ct[Maxlen], cs[2 * Maxlen]; n long long cmp(int pos) { n   if (cs[0] != ct[pos] || ls[0] > lt[pos]) return 0; n   if (m == 1) return lt[pos] - ls[0] + 1; n   pos++; n   int p1 = 1; n   while (p1 < m && pos < n && cs[p1] == ct[pos] && ls[p1] == lt[pos]) { n     pos++; n     p1++; n   } n   if (p1 < m && pos < n && cs[p1] == ct[pos] && ls[p1] < lt[pos]) p1++; n   if (p1 == m) return 1; n   return 0; n } n int kmp() { n   int res = 0; n   for (int i = 1; i < n + m; i++) { n     int j = dps[i - 1]; n     if (j == m - 2 && cs[i] == cs[j] && ls[i] >= ls[j]) res++; n     while (j > 0 && (cs[i] != cs[j] || ls[i] != ls[j])) j = dps[j - 1]; n     if (cs[j] == cs[i]) n       if (ls[j] == ls[i] || (j == 0 && ls[i] >= ls[j])) ++j; n     dps[i] = j; n   } n   return res; n } n int main() { n   scanf( %d%d , &n, &m); n   scanf( %d-%c , &lt[0], &ct[0]); n   for (int i = 1; i < n; i++) { n     scanf( %d-%c , &lt[i], &ct[i]); n     if (ct[i] == ct[i - 1]) { n       lt[i - 1] += lt[i]; n       n--; n       i--; n     } n   } n   scanf( %d-%c , &ls[0], &cs[0]); n   for (int i = 1; i < m; i++) { n     scanf( %d-%c , &ls[i], &cs[i]); n     if (cs[i] == cs[i - 1]) { n       ls[i - 1] += ls[i]; n       m--; n       i--; n     } n   } n   if (m == 1) { n     for (int i = 0; i < n; i++) ans = ans + cmp(i); n   } else { n     cs[m++] =  # ; n     for (int i = 0; i < n; i++) { n       cs[m + i] = ct[i]; n       ls[m + i] = lt[i]; n     } n     ans = kmp(); n   } n   printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1500; n const int MOD = 1e9 + 9; n int ing[128]; n long long dp[1005][105][15]; n int n, m; n struct ac_auto { n   int nxt[maxn][4], fail[maxn], ed[maxn], q[maxn]; n   int L = 0; n   void inserta(char buf[]) { n     int now = 0; n     int len = strlen(buf); n     for (int i = 0; buf[i]; i++) { n       int son = ing[(int)buf[i]]; n       if (!nxt[now][son]) nxt[now][son] = ++L; n       now = nxt[now][son]; n     } n     ed[now] = len; n   } n   void getfail() { n     int front = 0, back = 0, now = 0; n     for (int i = 0; i < 4; i++) n       if (nxt[now][i]) q[++back] = nxt[now][i]; n     while (front < back) { n       now = q[++front]; n       for (int i = 0; i < 4; i++) { n         if (nxt[now][i]) { n           fail[nxt[now][i]] = nxt[fail[now]][i]; n           q[++back] = nxt[now][i]; n         } else n           nxt[now][i] = nxt[fail[now]][i]; n         ed[nxt[now][i]] = max(ed[nxt[now][i]], ed[nxt[fail[now]][i]]); n       } n     } n   } n   void work() { n     memset(dp, 0, sizeof dp); n     dp[0][0][0] = 1; n     for (int i = 0; i < n; i++) { n       for (int j = 0; j <= L; j++) { n         for (int k = 0; k <= 10; k++) n           for (int f = 0; f < 4; f++) { n             if (ed[nxt[j][f]] > k) { n               dp[i + 1][nxt[j][f]][0] += dp[i][j][k]; n               dp[i + 1][nxt[j][f]][0] %= MOD; n             } else { n               dp[i + 1][nxt[j][f]][k + 1] += dp[i][j][k]; n               dp[i + 1][nxt[j][f]][k + 1] %= MOD; n             } n           } n       } n     } n     long long ans = 0; n     for (int i = 0; i <= L; i++) { n       ans += dp[n][i][0]; n       ans %= MOD; n     } n     cout << ans << endl; n   } n } ac; n char ss[15]; n int main() { n   ing[ A ] = 0, ing[ T ] = 1, ing[ C ] = 2, ing[ G ] = 3; n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= m; i++) { n     scanf( %s , ss); n     ac.inserta(ss); n   } n   ac.getfail(); n   ac.work(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int r[100005], g[100005], b[100005], nr, ng, nb, pos; n long long ans = 9e18; n long long calc(long long a, long long b, long long c) { n   long long temp = (a - b) * (a - b) + (a - c) * (a - c) + (b - c) * (b - c); n   return temp; n } n int bsearch(int a[], int len, int k) { n   int l = 1, r = len, mid, ans = 0; n   while (l < r) { n     mid = (l + r) >> 1; n     if (a[mid] > k) n       r = mid; n     else if (a[mid] == k) n       return mid; n     else { n       if (a[mid + 1] > k) n         return mid; n       else if (a[mid + 1] == k) n         return mid + 1; n       else n         l = mid + 1; n     } n   } n   return l; n } n void check(int a[], int b[], int c[], int lena, int lenb, int lenc) { n   int i, aa, bb, cc1, cc2, pos; n   for (i = 1; i <= lena; i++) { n     aa = a[i]; n     pos = lower_bound(b + 1, b + lenb + 1, aa) - b; n     if (!(pos >= 1 && pos <= lenb && b[pos] >= aa)) continue; n     bb = b[pos]; n     int mid = (aa + bb) / 2; n     pos = lower_bound(c + 1, c + lenc + 1, mid) - c; n     if (pos >= 1 && pos <= lenc && c[pos] >= mid) { n       cc1 = c[pos]; n       ans = min(ans, calc((long long)aa, (long long)bb, (long long)cc1)); n     } n     pos = bsearch(c, lenc, mid); n     if (pos <= lenc && pos >= 1 && c[pos] <= mid) { n       cc2 = c[pos]; n       ans = min(ans, calc((long long)aa, (long long)bb, (long long)cc2)); n     } n   } n } n int main() { n   int t; n   cin >> t; n   while (t--) { n     ans = 9e18; n     int i; n     scanf( %d%d%d , &nr, &ng, &nb); n     for (i = 1; i <= nr; i++) scanf( %d , &r[i]); n     for (i = 1; i <= ng; i++) scanf( %d , &g[i]); n     for (i = 1; i <= nb; i++) scanf( %d , &b[i]); n     sort(r + 1, r + nr + 1), sort(g + 1, g + ng + 1), sort(b + 1, b + nb + 1); n     check(r, g, b, nr, ng, nb); n     check(r, b, g, nr, nb, ng); n     check(g, r, b, ng, nr, nb); n     check(g, b, r, ng, nb, nr); n     check(b, r, g, nb, nr, ng); n     check(b, g, r, nb, ng, nr); n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[100005], f[100005]; n int l[100005], r[100005]; n int main() { n   int n; n   while (cin >> n) { n     memset(a, 0, sizeof(a)); n     memset(f, 0, sizeof(f)); n     for (int i = 0; i < n; i++) { n       cin >> a[i]; n     } n     int m; n     cin >> m; n     for (int i = 0; i < m; i++) { n       cin >> f[i]; n     } n     memset(l, 0, sizeof(l)); n     memset(r, 0, sizeof(r)); n     for (int i = 0; i < n; i++) { n       if (l[a[i]] == 0) { n         l[a[i]] = i; n       } n     } n     for (int i = n - 1; i >= 0; i--) { n       if (r[a[i]] == 0) { n         r[a[i]] = n - i; n       } n     } n     long long count1 = 0, count2 = 0; n     for (int i = 0; i < m; i++) { n       count1 = count1 + l[f[i]] + 1; n     } n     for (int i = 0; i < m; i++) { n       count2 = count2 + r[f[i]]; n     } n     cout << count1 <<     << count2 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, m; n   int wyn = 0; n   long long a, b; n   cin >> n; n   a = 1; n   m = 2; n   while (m <= n) { n     wyn++; n     b = m; n     m += a; n     a = b; n   } n   cout << wyn <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n string s, t; n vector<char> vv; n int main() { n   cin >> n >> m; n   cin >> s; n   for (int i = 0; i < n; i++) vv.push_back(s[i]); n   sort(vv.begin(), vv.end()); n   if (((int)(s).size()) < m) { n     t = s; n     for (int i = ((int)(s).size()); i < m; i++) t += vv[0]; n     cout << t << endl; n     return 0; n   } else if (((int)(s).size()) == m) { n     t = s; n     int k = 1; n     while (1) { n       if (s[((int)(s).size()) - k] == vv[((int)(vv).size()) - 1]) { n         t[((int)(s).size()) - k] = vv[0]; n         k++; n         continue; n       } n       int bb = upper_bound((vv).begin(), (vv).end(), t[((int)(s).size()) - k]) - n                vv.begin(); n       t[((int)(s).size()) - k] = vv[bb]; n       break; n     } n     cout << t << endl; n     return 0; n   } else { n     t = s.substr(0, m); n     int k = 1; n     while (1) { n       if (s[m - k] == vv[((int)(vv).size()) - 1]) { n         t[m - k] = vv[0]; n         k++; n         continue; n       } n       int bb = upper_bound((vv).begin(), (vv).end(), t[m - k]) - vv.begin(); n       t[m - k] = vv[bb]; n       break; n     } n     cout << t << endl; n     return 0; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 998244353; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n, m; n   cin >> n >> m; n   vector<int> a(n), b(m); n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 0; i < m; i++) cin >> b[i]; n   sort(a.begin(), a.end()); n   sort(b.begin(), b.end()); n   if (a[n - 1] > b[0]) { n     cout << -1 << endl; n     return 0; n   } n   long long sum = 0LL; n   for (int i = 0; i < m; i++) sum = sum + 1LL * b[i]; n   for (int i = 0; i < n - 1; i++) sum = sum + 1LL * m * a[i]; n   if (n > 1 && b[0] > a[n - 1]) sum = sum + a[n - 1] - a[n - 2]; n   cout << sum << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100000; n const double eps = 1e-15; n int n; n double p[N + 9], q[N + 9]; n void into() { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) scanf( %lf , &p[i]); n   for (int i = 1; i <= n; ++i) scanf( %lf , &q[i]); n } n double a[N + 9], b[N + 9]; n double Get_root(double a, double b, double c) { n   return fabs(a) <= eps ? -c / b n                         : (sqrt(max(b * b - 4 * a * c, 0.0)) - b) / (2 * a); n } n void Get_ans() { n   for (int i = 1; i <= n; ++i) { n     double x = p[i] + a[i - 1] * b[i - 1]; n     double y = p[i] + q[i] + a[i - 1] + b[i - 1]; n     b[i] = Get_root(1, -y, x); n     a[i] = y - b[i]; n   } n   for (int i = n; i >= 1; --i) a[i] -= a[i - 1], b[i] -= b[i - 1]; n } n void work() { Get_ans(); } n void outo() { n   for (int i = 1; i <= n; ++i) printf( %.10lf  , a[i]); n   puts(  ); n   for (int i = 1; i <= n; ++i) printf( %.10lf  , b[i]); n   puts(  ); n } n int main() { n   int T = 1; n   for (; T--;) { n     into(); n     work(); n     outo(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int OO = (int)1e6; n set<pair<int, int> > nods; n map<pair<int, int>, bool> vis; n pair<int, int> fr, sc; n int n, r, a, p, ans, sz; n bool ok = true; n int rx[] = {1, 1, 1, 0, 0, -1, -1, -1}; n int ry[] = {1, 0, -1, 1, -1, 1, 0, -1}; n bool val(pair<int, int> per) { return nods.find(per) != nods.end(); } n void fun(pair<int, int> pos) { n   pair<int, int> cur; n   queue<pair<int, int> > q; n   ans = 0; n   q.push(pos); n   vis[pos] = true; n   sz = 1; n   ok = true; n   for (; !q.empty() && ok; sz = q.size(), ans++) { n     while (ok && sz--) { n       pos = q.front(); n       q.pop(); n       for (int i = 0; ok && i < 8; i++) { n         cur.first = (pos.first) + rx[i]; n         cur.second = (pos.second) + ry[i]; n         if (val(cur) && !vis[cur]) { n           q.push(cur); n           vis[cur] = true; n           if (cur == sc) ok = false; n         } n       } n     } n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> fr.first >> fr.second >> sc.first >> sc.second >> n; n   for (int i = 0; i < n; i++) { n     cin >> r >> a >> p; n     for (int j = a; j <= p; j++) { n       nods.insert({r, j}); n       vis[{r, j}] = false; n     } n   } n   fun(fr); n   if (!ok) n     cout << ans << endl; n   else n     cout << -1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int in() { n   int x; n   scanf( %d , &x); n   return x; n } n template <typename T> n istream &operator>>(istream &in, vector<T> &vec) { n   for (int i = 0; i < vec.size(); ++i) in >> vec[i]; n   return in; n } n template <typename T> n ostream &operator<<(ostream &out, const vector<T> &vec) { n   for (int i = 0; i < vec.size(); ++i) out << vec[i] <<    ; n   return out; n } n const int INT = 1e6 + 1; n long long eds = 0; n int main() { n   long long a = in(), b = in(), c, n = 0, jac = 1; n   vector<int> x(a); n   vector<int> y(a); n   vector<bool> f(a); n   priority_queue<pair<long long, int>> qu; n   for (int i = 0; i < a; ++i) { n     x[i] = in(); n   } n   for (int i = 0; i < a; ++i) { n     y[i] = in(); n   } n   for (int i = 0; i < a; ++i) { n     if (x[i] % 100) { n       qu.push(make_pair(-(100 - x[i] % 100) * y[i], i)); n       b -= x[i] % 100; n       if (b < 0) { n         n += -(qu.top().first); n         f[qu.top().second] = true; n         qu.pop(); n         b += 100; n       } n     } n   } n   cout << n << endl; n   for (int i = 0; i < a; ++i) { n     cout << f[i] + x[i] / 100 <<     << (f[i] ? 0 : x[i] % 100) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n const int N = 2e5 + 5; n const int mod = 998244353; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   int t; n   cin >> t; n   while (t--) { n     int n, x; n     cin >> n >> x; n     vector<int> g[n + 1]; n     for (int i = 1; i < n; i++) { n       int u, v; n       cin >> u >> v; n       g[u].push_back(v); n       g[v].push_back(u); n     } n     if (g[x].size() == 1) { n       cout <<  Ayush n ; n       continue; n     } n     vector<int> flag(n + 1); n     for (int k : g[x]) { n       flag[k] = 1; n     } n     int sz = g[x].size(); n     int cnt = 0; n     for (int i = 1; i <= n; i++) { n       if (i != x and flag[i] == 0) cnt++; n     } n     4; n     int total = cnt + sz - 1; n     if (total % 2 == 1) { n       cout <<  Ashish n ; n     } else n       cout <<  Ayush n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> mark[4]; n string sins[4]; n int cost[5]; n int sta[1010]; n int dp[1010][0xffff + 1]; n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < 4; i++) cin >> cost[i]; n   for (int i = 0; i < 4; i++) cin >> sins[i]; n   for (int j = 0; j < n; j++) { n     sta[j] = 0; n     for (int i = 0; i < 4; i++) { n       sta[j] |= (sins[i][j] ==  * ) ? (1 << i) : 0; n     } n   } n   int S = 0; n   for (int i = 0; i < 4; i++) { n     S |= sta[i] << (i * 4); n     sta[n + i] = 0; n   } n   for (int i = 0; i < 4; i++) { n     for (int j = 3; j >= i; j--) { n       int t = 0xffff; n       for (int k = 0; k <= i; k++) { n         for (int y = j; y >= j - i; y--) t ^= 1 << (4 * k + y); n       } n       mark[i].push_back(t); n     } n   } n   memset(dp, 0x7f, sizeof(dp)); n   dp[0][S] = 0; n   for (int i = 0; i < n; i++) { n     for (int j = 0xffff; j >= 0; j--) { n       if (dp[i][j] == 0x7f7f7f7f) continue; n       if ((j & 0xf) == 0) { n         int S = (((j & 0xffff) >> 4) | (0xf000 & (sta[i + 4] << 12))); n         dp[i + 1][S] = min(dp[i + 1][S], dp[i][j]); n       } n       for (int k = 0; k < 4 && k + i < n; k++) { n         vector<int>::iterator y; n         for (y = mark[k].begin(); y != mark[k].end(); y++) { n           dp[i][j & (*y)] = min(dp[i][j & (*y)], dp[i][j] + cost[k]); n         } n       } n     } n   } n   cout << dp[n][0] << endl; n } n


#include <bits/stdc++.h> n using namespace std; n enum { DUNNO = -1, AB, BA }; n vector<pair<int, int> > E[200010]; n map<pair<int, int>, int> edge_id; n int dir[200010]; n int val[200010]; n bool vis[200010]; n int main() { n   int N, M; n   cin >> N >> M; n   memset(val, 0, sizeof(val)); n   memset(vis, 0, sizeof(vis)); n   memset(dir, DUNNO, sizeof(dir)); n   int a, b, c; n   for (int j = (1); j <= (M); ++j) { n     scanf( %d%d%d , &a, &b, &c); n     E[a].push_back(make_pair(b, c)); n     E[b].push_back(make_pair(a, c)); n     edge_id[make_pair(a, b)] = j; n     edge_id[make_pair(b, a)] = -j; n     val[a] += c; n     val[b] += c; n   } n   for (int i = (2); i <= (N - 1); ++i) val[i] /= 2; n   queue<int> Q; n   val[1] = 0; n   Q.push(1); n   while (!Q.empty()) { n     int i = Q.front(); n     Q.pop(); n     if (vis[i]) continue; n     vis[i] = true; n     assert(val[i] == 0); n     int numE = E[i].size(); n     for (int x = 0; x < (numE); ++x) { n       int j = E[i][x].first; n       int c = E[i][x].second; n       if (vis[j]) continue; n       int e = edge_id[make_pair(i, j)]; n       if (dir[abs(e)] != DUNNO) continue; n       if (e > 0) n         dir[e] = 0; n       else n         dir[-e] = 1; n       val[j] -= c; n       if (!val[j]) Q.push(j); n     } n   } n   for (int j = (1); j <= (M); ++j) cout << dir[j] << endl; n } n


#include <bits/stdc++.h> n int main() { n   int n, a, b, c, t, i, m, ans = 0; n   scanf( %d%d%d%d%d , &n, &a, &b, &c, &t); n   ans = n * a; n   for (i = 0; i < n; i++) { n     scanf( %d , &m); n     ans += (t - m) * (c - b); n   } n   if (b >= c) n     printf( %d , n * a); n   else n     printf( %d , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename... T> n void read(T &...args) { n   ((cin >> args), ...); n } n template <typename... T> n void write(T... args) { n   ((cout << args <<    ), ...); n } n const long long MOD = 1000000007; n const long long INF = 1000000050; n const long long BIG = (long long)2e18 + 50; n const long long MX = 200010; n const double EPS = 1e-9; n long long a[MX]; n bool good; n long long m; n vector<long long> g[MX]; n long long sz[MX]; n long long cnt[MX]; n long long mx = 0; n void dfs(long long v) { n   if (g[v].size() == 0) n     cnt[v] = 1; n   else n     cnt[v] = 0; n   sz[v] = a[v]; n   for (auto &to : g[v]) { n     dfs(to); n     cnt[v] += cnt[to]; n     sz[v] += sz[to]; n   } n   mx = max(mx, (sz[v] + cnt[v] - 1) / cnt[v]); n } n void solve() { n   long long n; n   cin >> n; n   long long arr[n]; n   for (long long i = 0; i < n; i++) cin >> arr[i]; n   if (n == 1) { n     cout << 0 <<   n ; n     return; n   } n   sort(arr, arr + n); n   long long mid = n / 2; n   long long ret1 = 0; n   long long ret2 = 0; n   long long arr_val[n]; n   for (long long i = 0; i < n; i++) { n     arr_val[i] = arr[mid] - mid + i; n   } n   if (arr_val[0] <= 0) { n     long long val = 1 - arr_val[0]; n     for (long long i = 0; i < n; i++) arr_val[i] += val; n   } n   for (long long i = 0; i < n; i++) { n     ret1 += abs(arr_val[i] - arr[i]); n   } n   write(  n ); n   for (long long i = 0; i < n; i++) write(arr_val[i]); n   if (n % 2 == 0) { n     mid--; n     for (long long i = 0; i < n; i++) { n       arr_val[i] = arr[mid] - mid + i; n     } n     if (arr_val[0] <= 0) { n       long long val = 1 - arr_val[0]; n       for (long long i = 0; i < n; i++) arr_val[i] += val; n     } n     for (long long i = 0; i < n; i++) { n       ret2 += abs(arr_val[i] - arr[i]); n     } n   } n   write(  n ); n   for (long long i = 0; i < n; i++) write(arr_val[i]); n   if (n % 2 == 0) n     cout << min(ret1, ret2); n   else n     cout << ret1; n   cout <<   n ; n } n long long arr[201]; n long long dp[201][405]; n long long n; n long long recur(long long index, long long time) { n   if (index == n) return 0; n   if (dp[index][time] != -1) return dp[index][time]; n   long long ret = INT_MAX; n   for (long long i = time + 1; i <= 405; i++) { n     ret = min(ret, abs(arr[index] - i) + recur(index + 1, i)); n   } n   return dp[index][time] = ret; n } n void solveA() { n   cin >> n; n   memset(dp, -1, sizeof(dp)); n   for (long long i = 0; i < n; i++) cin >> arr[i]; n   sort(arr, arr + n); n   cout << recur(0, 0) <<   n ; n } n int32_t main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   srand(chrono::high_resolution_clock::now().time_since_epoch().count()); n   long long x; n   cin >> x; n   while (x--) { n     solveA(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> v[200010]; n bool vis[200010]; n int amid[200010]; n int n, m; n int a[200010]; n vector<int> vv; n vector<pair<int, int> > t; n void dfs(int node) { n   vis[node] = 1; n   t.push_back({a[node], node}); n   int sz = v[node].size(); n   for (int i = 0; i < sz; i++) { n     int nx = v[node][i]; n     if (!vis[nx]) dfs(nx); n   } n } n void cal() { n   int d = 0; n   bool yy = 1; n   map<int, int> mm; n   for (int i = 0; i < t.size(); i++) { n     mm[t[i].first]++; n   } n   set<int> s; n   for (int i = 0; i < t.size(); i++) { n     int node = t[i].second, r = t[i].first; n     if (!s.count(r)) d += mm[r]; n     s.insert(r); n     amid[node] -= (d - mm[r]); n   } n } n int main() { n   cin >> n >> m; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     vv.push_back(a[i]); n   } n   sort(vv.begin(), vv.end()); n   for (int i = 1; i <= n; i++) { n     amid[i] = lower_bound(vv.begin(), vv.end(), a[i]) - vv.begin(); n   } n   for (int i = 0; i < m; i++) { n     int x, y; n     cin >> x >> y; n     v[x].push_back(y); n     v[y].push_back(x); n   } n   for (int i = 1; i <= n; i++) { n     int sz = v[i].size(); n     int r = a[i]; n     for (int j = 0; j < sz; j++) { n       int nx = v[i][j]; n       if (a[nx] < a[i]) amid[i]--; n     } n   } n   for (int i = 1; i <= n; i++) { n     cout << amid[i] <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long MOD = 1000000007; n int d[201][201]; n pair<pair<int, int>, int> E[20000]; n vector<std::pair<int, int> > Intervals; n int n, m; n bool check(int idx, int x) { n   int u = E[idx].first.first, v = E[idx].first.second, w = E[idx].second; n   Intervals.clear(); n   for (int i = 0; i < n; ++i) { n     int d1 = d[u][i], d2 = d[v][i]; n     if (d1 > x and d2 > x) return 0; n     int a = 0, b = w; n     a = min(w, x - d1) + 1; n     b = max(0, w - (x - d2)) - 1; n     if (d1 <= x and d2 <= x) { n       if (a > b) n         continue; n       else n         Intervals.push_back(std::make_pair(a, b)); n     } else if (d1 <= x) { n       if (a <= w) Intervals.push_back(std::make_pair(a, w)); n     } else { n       if (b >= 0) Intervals.push_back(std::make_pair(0, b)); n     } n   } n   if (Intervals.size() < 1) return 1; n   sort(Intervals.begin(), Intervals.end()); n   if (Intervals[0].first > 0) return 1; n   int maxY = Intervals[0].second; n   for (int i = 1; i < Intervals.size(); ++i) { n     if (Intervals[i].first > maxY) return 1; n     maxY = max(maxY, Intervals[i].second); n   } n   return maxY < w; n } n int solve(int idx) { n   int u = E[idx].first.first, v = E[idx].first.second; n   int l = 0, r = 0, m; n   for (int i = 0; i < n; ++i) { n     r = max(r, min(d[u][i], d[v][i])); n   } n   r += E[idx].second + 10; n   while (r - l > 1) { n     m = (l + r) >> 1; n     bool f = check(idx, m); n     if (f) n       r = m; n     else n       l = m; n   } n   if (check(idx, l)) n     return l; n   else n     return r; n } n int main() { n   int u, v, w; n   scanf( %d %d , &n, &m); n   for (int i = 0; i < (n); ++i) n     for (int j = 0; j < (n); ++j) d[i][j] = (i == j ? 0 : MOD); n   for (int i = 0; i < (m); ++i) { n     scanf( %d %d %d , &u, &v, &w); n     u -= 1, v -= 1, w <<= 1; n     d[u][v] = d[v][u] = w; n     E[i] = std::make_pair(std::make_pair(u, v), w); n   } n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < n; ++j) n       for (int k = 0; k < n; ++k) { n         d[j][k] = min(d[j][k], d[j][i] + d[i][k]); n       } n   } n   int ans = MOD; n   for (int i = 0; i < (m); ++i) ans = min(ans, solve(i)); n   cout << ans / 2.0 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = (int)1e3 + 10; n int n; n pair<string, int> a[N]; n string ans[N]; n int main() { n   cin >> n; n   for (int i = 0; i < n; ++i) { n     char c; n     cin >> c >> c >> c >> c; n     cin >> a[i].first; n     a[i].second = i; n     if (a[i].first ==  9 ) ans[i] =  1989 ; n     if (a[i].first ==  99 ) ans[i] =  1999 ; n   } n   sort(a, a + n); n   for (int i = 0; i < n; ++i) { n     if (a[i].first.length() == 1) { n       if (a[i].first ==  9 ) continue; n       ans[a[i].second] =  199 ; n       ans[a[i].second] += a[i].first; n     } else if (a[i].first.length() == 2) { n       if (a[i].first ==  99 ) continue; n       ans[a[i].second] =  20 ; n       ans[a[i].second] += a[i].first; n     } else if (a[i].first.length() == 3) { n       if (a[i].first <  100  && a[i].first !=  099 ) { n         ans[a[i].second] =  3 ; n         ans[a[i].second] += a[i].first; n       } else { n         ans[a[i].second] =  2 ; n         ans[a[i].second] += a[i].first; n       } n     } else if (a[i].first.length() == 4) { n       if ( 0000  <= a[i].first && a[i].first <  3099 ) ans[a[i].second] =  1 ; n       ans[a[i].second] += a[i].first; n     } else if (a[i].first.length() == 5) { n       if ( 00000  <= a[i].first && a[i].first <  13099 ) ans[a[i].second] =  1 ; n       ans[a[i].second] += a[i].first; n     } else if (a[i].first.length() == 6) { n       if ( 000000  <= a[i].first && a[i].first <  113099 ) n         ans[a[i].second] =  1 ; n       ans[a[i].second] += a[i].first; n     } else if (a[i].first.length() == 7) { n       if ( 0000000  <= a[i].first && a[i].first <  1113099 ) n         ans[a[i].second] =  1 ; n       ans[a[i].second] += a[i].first; n     } else if (a[i].first.length() == 8) { n       if ( 00000000  <= a[i].first && a[i].first <  11113099 ) n         ans[a[i].second] =  1 ; n       ans[a[i].second] += a[i].first; n     } else if (a[i].first.length() == 9) { n       if ( 000000000  <= a[i].first && a[i].first <  111113099 ) n         ans[a[i].second] =  1 ; n       ans[a[i].second] += a[i].first; n     } n   } n   for (int i = 0; i < n; ++i) cout << ans[i] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 10, M = 1e6 + 10; n struct node { n   int a, b, w; n } e[M]; n int fa[N]; n bool cmp(node a, node b) { return a.w < b.w; } n int find(int x) { n   if (fa[x] != x) fa[x] = find(fa[x]); n   return fa[x]; n } n int du[N]; n int main() { n   int n, m; n   cin >> n >> m; n   int a1, b1, w1; n   for (int i = 1; i <= m; ++i) { n     scanf( %d%d%d , &e[i].a, &e[i].b, &e[i].w); n     if (i == 1) { n       a1 = e[i].a; n       b1 = e[i].b; n     } n   } n   for (int i = 1; i <= n; ++i) fa[i] = i; n   sort(e + 2, e + 1 + m, cmp); n   for (int i = 2; i <= m; ++i) { n     int f1 = find(e[i].a); n     int f2 = find(e[i].b); n     if (f1 == f2) continue; n     fa[f1] = f2; n     if (find(a1) == find(b1)) { n       printf( %d n , e[i].w); n       return 0; n     } n   } n   printf( 1000000000 n ); n } n


#include <bits/stdc++.h> n using namespace std; n long long n, k, a[600000], L, R, r, t, l, f; n long long s; n bool ok(long long x) { n   long long r = 0; n   for (long long i = 0; i < n; i++) n     if (a[i] > x) r += (a[i] - x); n   if (r <= t) return 1; n   return 0; n } n bool ok1(long long x) { n   long long r = 0; n   for (long long i = 0; i < n; i++) n     if (a[i] < x) r += (x - a[i]); n   if (r <= t) return 1; n   return 0; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n >> t; n   L = 2e9; n   for (long long i = 0; i < n; i++) { n     cin >> a[i]; n     s += a[i]; n     L = min(L, a[i]); n     R = max(R, a[i]); n   } n   long long d1 = R, d2 = L; n   l = L; n   r = R; n   while (r - l > 10) { n     long long mid = (r + l) / 2; n     if (ok(mid)) n       r = mid; n     else n       l = mid + 1; n   } n   for (long long i = l; i <= r; i++) n     if (ok(i)) d1 = min(d1, i); n   l = L; n   r = R; n   while (r - l > 10) { n     long long mid = (r + l) / 2; n     if (ok1(mid)) n       l = mid; n     else n       r = mid - 1; n   } n   for (long long i = l; i <= r; i++) n     if (ok1(i)) d2 = max(d2, i); n   if (s % n == 0) n     f = 0; n   else n     f = 1; n   cout << max(f, d1 - d2); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, k, s; n   cin >> n >> k >> s; n   if (k > s || k * (n - 1) < s) { n     cout <<  NO n ; n   } else { n     cout <<  YES n ; n     long long h = 1; n     while (k--) { n       long long d = min(n - 1, s - k); n       s -= d; n       if (h - d <= 0) n         h += d; n       else n         h -= d; n       cout << h <<    ; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double EPS = 1e-9; n inline int comp(double A, double B = 0) { n   return (A <= B + EPS) ? (A + EPS < B) ? -1 : 0 : 1; n } n struct point { n   double x, y; n   point(double x = 0, double y = 0) : x(x), y(y) {} n   bool operator<(const point &other) const { n     if (!comp(x, other.x)) return comp(y, other.y) < 0; n     return comp(x, other.x) < 0; n   } n   point operator+(const point &other) const { n     return point(x + other.x, y + other.y); n   } n   point operator-(const point &other) const { n     return point(x - other.x, y - other.y); n   } n }; n double inner(const point &A, const point &B) { return A.x * B.x + A.y * B.y; } n double outer(const point &A, const point &B) { return A.x * B.y - A.y * B.x; } n double dist(const point &A, const point &B) { n   return sqrt(inner(A - B, A - B)); n } n double angle(const point &A, const point &O, const point &B) { n   return acos(inner(A - O, B - O) / (dist(A, O) * dist(B, O))); n } n int side(const point &A, const point &O, const point &B) { n   return comp(outer(A - O, B - O)); n } n vector<point> convex_hull(vector<point> &points) { n   int size = (int)points.size(); n   if (size < 3) return points; n   point O = *min_element(points.begin(), points.end()); n   sort(points.begin(), points.end(), [O](const point &A, const point &B) { n     double foo = outer(A - O, B - O); n     return comp(foo) > 0 or (!comp(foo) and comp(dist(A, O), dist(B, O)) < 0); n   }); n   vector<point> polygon = {points.back(), points[0], points[1]}; n   for (int i = 2; i < size;) n     if (polygon.size() <= 2 or n         side(polygon.rbegin()[0], polygon.rbegin()[1], points[i]) > 0) n       polygon.push_back(points[i++]); n     else n       polygon.pop_back(); n   polygon.pop_back(); n   return polygon; n } n vector<int> pre_kmp(const vector<double> &A) { n   int i = 0, j, size = (int)A.size(); n   vector<int> back(size + 1); n   back[0] = j = -1; n   while (i < size) { n     while (j >= 0 and comp(A[i], A[j])) j = back[j]; n     ++i; n     ++j; n     back[i] = j; n   } n   return back; n } n vector<int> kmp(const vector<double> &A, const vector<double> &B) { n   int i = 0, j = 0, sizea = (int)A.size(), sizeb = (int)B.size(); n   vector<int> match, back = pre_kmp(B); n   while (i < sizea) { n     while (j >= 0 and comp(A[i], B[j])) j = back[j]; n     ++i; n     ++j; n     if (j == sizeb) { n       match.push_back(i - j); n       j = back[j]; n     } n   } n   return match; n } n vector<double> stringify(const vector<point> &P) { n   vector<double> foo; n   int size = (int)P.size(); n   for (int i = 0; i < size; ++i) { n     foo.push_back(dist(P[i], P[(i + 1) % size])); n     foo.push_back(angle(P[i], P[(i + 1) % size], P[(i + 2) % size])); n   } n   return foo; n } n int main() { n   int n, m; n   scanf( %d %d , &n, &m); n   vector<point> A(n), B(m); n   for (int i = 0; i < n; ++i) scanf( %lf %lf , &A[i].x, &A[i].y); n   for (int i = 0; i < m; ++i) scanf( %lf %lf , &B[i].x, &B[i].y); n   vector<double> X = stringify(convex_hull(A)), Y = stringify(convex_hull(B)); n   X.insert(X.end(), X.begin(), X.end()); n   printf( %s n , kmp(X, Y).empty() ?  NO  :  YES ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long fib[100005], mod = 1e9 + 7; n void pre() { n   fib[0] = fib[1] = 1; n   for (int i = 2; i < 100005; i++) fib[i] = (fib[i - 1] + fib[i - 2]) % mod; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   pre(); n   long long m, n, x; n   cin >> m >> n; n   x = (fib[m] + fib[n] - 1) % mod; n   cout << (2LL * x) % mod; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MAXN = 1e2 + 7; n long long p[MAXN], dp[MAXN]; n pair<long long, long long> fig[MAXN]; n int32_t main() { n   long long n; n   cin >> n; n   for (long long i = 0; i < n; ++i) cin >> fig[i].second >> fig[i].first; n   sort(fig, fig + n); n   long long t; n   cin >> t; n   for (long long i = 1; i <= t; ++i) cin >> p[i]; n   p[t + 1] = (1LL << 60); n   long long pos = 1, ans = 0, sum = 0; n   for (long long i = 0; i < n; ++i) { n     while (fig[i].second) { n       if (p[pos] - sum > fig[i].second) { n         sum += fig[i].second; n         ans += fig[i].second * pos * fig[i].first; n         fig[i].second = 0; n       } else { n         ans += fig[i].first * (p[pos] - sum) * pos; n         fig[i].second -= p[pos] - sum; n         sum = p[pos]; n         pos++; n       } n     } n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n int btf(int n) { n   static int num[10]; n   memset(num, 0, sizeof(num)); n   for (int i = 0; i < 4; i++) { n     num[n % 10]++; n     n /= 10; n   } n   for (int i = 0; i < 10; i++) n     if (num[i] > 1) return 0; n   return 1; n } n void solve(int n) { n   for (int i = n + 1; 1; i++) n     if (btf(i)) { n       printf( %d n , i); n       return; n     } n } n int main() { n   int n; n   while (scanf( %d , &n) != EOF) solve(n); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a, b, m; n void deal() { n   for (int i = 0; i <= a && i < m; i++) { n     if ((m - (long long int)i * 1000000000 % m) % m > b) { n       printf( 1 %.9d n , i); n       return; n     } n   } n   printf( 2 n ); n } n int main() { n   while (scanf( %d%d%d , &a, &b, &m) != EOF) { n     deal(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s; n long long ans[2200], MOD = 1000003; n long long dp[2200][2200], inv[2200], fac[2200], ifac[2200]; n void init_math(int N) { n   inv[1] = 1LL; n   for (int i = 2; i <= N; i++) n     inv[i] = (MOD - ((MOD / i) * inv[MOD % i]) % MOD); n   fac[0] = 1LL; n   ifac[0] = 1LL; n   for (int i = 1; i <= N; i++) { n     fac[i] = (fac[i - 1] * (long long)(i)) % MOD; n     ifac[i] = (ifac[i - 1] * inv[i]) % MOD; n   } n } n int st[2200], stc, r, len, ms, t[2200], tc; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n   init_math(2000); n   cin >> s; n   len = s.size(); n   ans[1] = 1; n   for (int i = 1; i <= 2000; i++) n     ans[i + 1] = inv[i + 1] * fac[i * 2] % MOD * ifac[i] % MOD * ifac[i] % MOD; n   tc = 0; n   bool isn = 0, ist; n   for (int i = 0; i < len; i++) { n     ist = 0; n     if (s[i] <=  9  && s[i] >=  0 ) n       isn = 1; n     else { n       if (isn) { n         t[tc++] = 0; n         isn = 0; n       } n       if (i == 0 || !(s[i - 1] <=  9  && s[i - 1] >=  0 )) { n         if (s[i] ==  *  || s[i] ==  / ) { n           cout << 0 << endl; n           return 0; n         } n         t[tc++] = 1; n       } else n         ist = 1; n     } n   } n   if (isn) t[tc++] = 0; n   if (t[tc - 1] == 1 || ist) { n     cout << 0 << endl; n     return 0; n   } n   stc = 0; n   for (int i = 0; i < tc; i++) { n     if (t[i] == 0) n       st[stc]++; n     else n       stc++; n   } n   if (stc == 0) { n     cout << ans[tc] << endl; n     return 0; n   } n   dp[stc + 1][0] = 1; n   for (int i = stc; i >= 0; i--) { n     r = st[i]; n     if (i) { n       ms += r; n       for (int j = 1; j <= ms; j++) n         for (int k = max(r - j, 0); k <= ms - j; k++) { n           dp[i][k + 1] += (dp[i + 1][k + j - r] * ans[j]) % MOD; n           if (dp[i][k + 1] >= MOD) dp[i][k + 1] -= MOD; n         } n     } else { n       for (int j = 0; j <= ms; j++) { n         dp[i][j + r] += dp[i + 1][j]; n         if (dp[i][j + r] >= MOD) dp[i][j + r] -= MOD; n       } n     } n   } n   long long res = 0; n   for (int i = 1; i <= tc; i++) { n     res += (dp[0][i] * ans[i]) % MOD; n     if (res >= MOD) res -= MOD; n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int two(int x) { return 1 << x; } n inline long long TWO(int x) { return (long long)1 << x; } n template <class T> n void Min(T &x, T y) { n   if (y < x) x = y; n } n template <class T> n void Max(T &x, T y) { n   if (y > x) x = y; n } n template <class T> n T _abs(T x) { n   return x > 0 ? x : -x; n } n template <class T> n T gcd(T a, T b) { n   return !b ? a : gcd(b, a % b); n } n const int maxn = 200010; n const int mod = 1000000007; n int get_val() { n   int ret(0); n   char c; n   while ((c = getchar()) ==     || c ==   n  || c ==   r ) n     ; n   ret = c -  0 ; n   while ((c = getchar()) !=     && c !=   n  && c !=   r ) n     ret = ret * 10 + c -  0 ; n   return ret; n } n int n, m, k; n int a[maxn], b[maxn]; n int num[maxn]; n int main() { n   cin >> n >> m >> k; n   map<int, int> mpa, mpb, mp; n   for (int i = 0; i < n; i++) a[i] = get_val(), mpa[a[i]]++, mp[a[i]]++; n   for (int i = 0; i < m; i++) b[i] = get_val(), mpb[b[i]]++, mp[b[i]]--; n   int tot = 0; n   for (map<int, int>::iterator it = mp.begin(); it != mp.end(); it++) { n     if (it->second) num[tot++] = it->second; n   } n   long long s1 = 0, s2 = 0; n   for (int i = tot; i >= 0; i--) { n     s1 += num[i]; n     if (s1 > 0) { n       return puts( YES ), 0; n     } n   } n   puts( NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n1, n2, k, a; n   cin >> n1 >> n2; n   vector<int> arr1, arr2; n   for (int l = 0; l < n1; l++) { n     cin >> a; n     arr1.push_back(a); n   } n   for (int l = 0; l < n2; l++) { n     cin >> a; n     arr2.push_back(a); n   } n   sort(arr1.begin(), arr1.end()); n   sort(arr2.begin(), arr2.end()); n   for (int k = 0; k < arr1.size(); k++) { n     for (int l = 0; l < arr2.size(); l++) { n       if (arr1[k] == arr2[l]) { n         cout << arr1[k] <<   n ; n         return 0; n       } n     } n   } n   if (arr1[0] < arr2[0]) { n     k = 10 * arr1[0] + arr2[0]; n     cout << k <<   n ; n   } else { n     k = 10 * arr2[0] + arr1[0]; n     cout << k <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   for (int i = 0; i < s.length(); i++) { n     if (s[i] !=  a  && s[i] !=  e  && s[i] !=  i  && s[i] !=  o  && n         s[i] !=  u  && s[i] !=  n ) { n       if (i != s.length() - 1) { n         if (s[i + 1] !=  a  && s[i + 1] !=  e  && s[i + 1] !=  i  && n             s[i + 1] !=  o  && s[i + 1] !=  u ) { n           cout <<  NO  << endl; n           return 0; n         } n       } else { n         cout <<  NO  << endl; n         return 0; n       } n     } n   } n   cout <<  YES  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int main() { n   scanf( %d %d , &n, &m); n   if (m > n) { n     printf( -1 ); n     return 0; n   } n   m -= 2; n   if (n == 1) { n     printf( a ); n     return 0; n   } n   if (n > 1 && m < 0) { n     printf( -1 ); n     return 0; n   } n   for (int i = 1; i <= n - m; i++) n     if (i % 2 == 1) n       printf( a ); n     else n       printf( b ); n   for (int i = 1; i <= m; i++) printf( %c , char(i + 98)); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 1000000007; n const long long linf = 1000000000000000000LL; n const double eps = 0.000001; n const double pi = 3.14159265358979323846; n template <class T> n T abs(T k) { n   return k >= 0 ? k : -k; n } n template <class T> n T sqr(T n) { n   return n * n; n } n template <class T> n T gcd(T a, T b) { n   return b ? gcd(b, a % b) : a; n } n template <class T> n T mod(T a, T b) { n   return (a % b + b) % b; n } n template <class T> n T addmod(T a, T b, T c) { n   return (T)((long long)(a + b) % c); n } n template <class T> n T mulmod(T a, T b, T c) { n   return (T)((long long)a * b % c); n } n template <class T> n T powmod(T a, T b, T c) { n   return (T)(b ? sqr((long long)powmod(a, b >> 1, c)) % c * (b & 1 ? a : 1) % c n                : 1); n } n template <class T> n void maxe(T &a, T b) { n   a = max(a, b); n } n template <class T> n void mine(T &a, T b) { n   a = min(a, b); n } n template <class T> n void mode(T &a, T b) { n   a = mod(a, b); n } n template <class T> n void addmode(T &a, T b, T c) { n   a = addmod(a, b, c); n } n template <class T> n void mulmode(T &a, T b, T c) { n   a = mulmod(a, b, c); n } n template <class T> n void powmode(T &a, T b, T c) { n   a = powmod(a, b, c); n } n void fileio_in_out() { n   freopen( .in ,  r , stdin); n   freopen( .out ,  w , stdout); n } n void fileio_txt() { n   freopen( input.txt ,  r , stdin); n   freopen( output.txt ,  w , stdout); n } n const int N = 111111; n int test; n int n, m, k; n long long ans; n long long d[N], h[N], t[N], s[N], st[N]; n long long dp[111][N]; n int q[N], head, tail; n long long f1(int i, int j, int k) { return dp[i][k] + st[k] - t[j] * k; } n long long f2(int i, int j, int k) { n   return ((dp[i][j] + st[j]) - (dp[i][k] + st[k])) / (j - k); n } n long long getans() { n   sort(t + 1, t + 1 + m); n   for (int i = 1; i <= m; i++) { n     st[i] = st[i - 1] + t[i]; n     dp[1][i] = t[i] * i - st[i]; n   } n   for (int i = 2; i <= k; i++) { n     head = tail = 0; n     for (int j = 1; j <= m; j++) { n       while (head < tail && f1(i - 1, j, q[head]) >= f1(i - 1, j, q[head + 1])) n         head++; n       while (head < tail && n              f2(i - 1, q[tail - 1], q[tail]) >= f2(i - 1, q[tail], j)) n         tail--; n       dp[i][j] = t[j] * j - st[j] + f1(i - 1, j, q[head]); n       q[++tail] = j; n     } n   } n   return dp[k][m]; n } n int main() { n   scanf( %d%d%d , &n, &m, &k); n   for (int i = 2; i <= n; i++) { n     scanf( %I64d , &d[i]); n     s[i] = s[i - 1] + d[i]; n   } n   for (int i = 1; i <= m; i++) { n     scanf( %I64d%I64d , &h[i], &t[i]); n     t[i] -= s[h[i]]; n   } n   ans = getans(); n   printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n int n, m, vmx; n long long seed; n struct node { n   int l, r; n   mutable long long val; n   bool operator<(const node &x) const { return l < x.l; } n }; n set<node> tr; n set<node>::iterator split(int pos) { n   set<node>::iterator iter = tr.lower_bound((node){pos}); n   if (iter != tr.end() && iter->l == pos) return iter; n   iter--; n   int L = iter->l, R = iter->r; n   long long Val = iter->val; n   tr.erase(iter); n   tr.insert((node){L, pos - 1, Val}); n   return tr.insert((node){pos, R, Val}).first; n } n void assign(int l, int r, long long val) { n   set<node>::iterator R = split(r + 1), L = split(l); n   tr.erase(L, R); n   tr.insert((node){l, r, val}); n   return; n } n void add(int l, int r, long long val) { n   set<node>::iterator R = split(r + 1), L = split(l); n   for (; L != R; ++L) L->val = L->val + val; n   return; n } n struct Lsh { n   long long val; n   int sum; n } ls[N]; n inline bool Lscmp(Lsh a, Lsh b) { return a.val < b.val; } n inline long long rnk(int l, int r, int x) { n   set<node>::iterator R = split(r + 1), L = split(l); n   int tot = 0; n   for (; L != R; ++L) ls[++tot].val = L->val, ls[tot].sum = (L->r - L->l + 1); n   sort(ls + 1, ls + tot + 1, Lscmp); n   for (int i = 1; i <= tot; i++) { n     x -= ls[i].sum; n     if (x <= 0) return ls[i].val; n   } n } n inline long long ksm(long long a, long long b, long long mod) { n   a %= mod; n   long long ans = 1; n   while (b) { n     if (b & 1) ans = ans * a % mod; n     a = a * a % mod; n     b >>= 1; n   } n   return ans; n } n inline long long calc(int l, int r, int x, int y) { n   set<node>::iterator R = split(r + 1), L = split(l); n   long long ans = 0; n   for (; L != R; ++L) n     ans = (ans + ksm(L->val, x, y) * (L->r - L->l + 1) % y) % y; n   return ans; n } n inline int rnd() { n   int ret = seed; n   seed = (seed * 7 + 13) % 1000000007; n   return ret; n } n int op, l, r, x, y; n int tot; n int main() { n   scanf( %d%d%lld%d , &n, &m, &seed, &vmx); n   for (int i = 1; i <= n; i++) tr.insert((node){i, i, rnd() % vmx + 1}); n   tr.insert((node){n + 1, n + 1, 0}); n   for (int i = 1; i <= m; i++) { n     op = rnd() % 4 + 1; n     l = rnd() % n + 1; n     r = rnd() % n + 1; n     if (l > r) swap(l, r); n     if (op == 3) n       x = rnd() % (r - l + 1) + 1; n     else n       x = rnd() % vmx + 1; n     if (op == 4) y = rnd() % vmx + 1; n     if (op == 1) n       add(l, r, x); n     else if (op == 2) n       assign(l, r, x); n     else if (op == 3) n       printf( %lld n , rnk(l, r, x)); n     else n       printf( %lld n , calc(l, r, x, y)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n double a[200005], b[200005]; n double l = -10001, r = 10001; n const double eps = 2e-12; n double solve(double x) { n   for (int i = 1; i <= n; ++i) b[i] = a[i] - x; n   double res = 0, tmp = 0; n   for (int i = 1; i <= n; ++i) { n     if (tmp < 0) tmp = 0; n     tmp += b[i]; n     res = max(res, tmp); n   } n   tmp = 0; n   for (int i = 1; i <= n; ++i) { n     if (tmp < 0) tmp = 0; n     tmp -= b[i]; n     res = max(res, tmp); n   } n   return res; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) { n     scanf( %lf , &a[i]); n   } n   while (r > eps + l) { n     double ll = (2 * l + r) / 3, rr = (l + 2 * r) / 3; n     double lll = solve(ll); n     double rrr = solve(rr); n     if (lll < rrr) n       r = rr; n     else n       l = ll; n   } n   cout << fixed << setprecision(12) << solve((l + r) / 2) << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, c, i; n   cin >> n >> m; n   c = 2 * n; n   i = 1; n   while (i <= 2 * n) { n     if (m > c) { n       c++; n       cout << c <<    ; n     } n     cout << i <<    ; n     i++; n     if (m < i) break; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long t, sx, sy, ex, ey; n int e, s, n, w; n string ch; n int main() { n   cin >> t >> sx >> sy >> ex >> ey; n   if (sx < ex) n     e = ex - sx; n   else n     w = sx - ex; n   if (sy < ey) n     n = ey - sy; n   else n     s = sy - ey; n   cin >> ch; n   long long d = 1; n   for (long long i = 0; i < t; i++) { n     if (ch[i] ==  S ) n       s--; n     else if (ch[i] ==  N ) n       n--; n     else if (ch[i] ==  E ) n       e--; n     else n       w--; n     if ((s <= 0 && n <= 0) && (e <= 0 && w <= 0)) { n       d = 0; n       cout << i + 1 << endl; n       break; n     } n   } n   if (d == 1) cout << -1 << endl; n } n


#include<bits/stdc++.h> n using namespace std; n #define read(x) scanf( %d ,&x) n const int N=1e5+10; n string s; n int main() n { n   n  int t; read(t); n  while(t--) n  { n   cin>>s; n   for(int i=0;i<s.size();i++) n   { n    if(i%2==0) n    { n     if(s[i]!= a ) s[i]= a ; n     else s[i]= b ; n    } n    else n    { n     if(s[i]!= z ) s[i]= z ; n     else s[i]= y ; n    } n   } n   cout<<s<<endl; n  } n  n  return 0; n  }


#include <bits/stdc++.h> n using namespace std; n const int N = 123456; n void solve() { n   long long n; n   cin >> n; n   if (n % 2 == 0) { n     cout << (10 + n) <<   10 ; n   } else { n     cout << (9 + n) <<   9 ; n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long t; n   t = 1; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ,  inline ,  -ffast-math ) n #pragma GCC target( avx,sse2,sse3,sse4,mmx ) n inline long long read() { n   long long s(0); n   bool w(1); n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) w = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) s = s * 10 + ch -  0 , ch = getchar(); n   return s * w; n } n long long l, r, n, m, x, y, num; n int main() { n   n = read(), x = read(); n   for (int i = 1; i <= n; i++) { n     num = read(); n     l = l * x + num; n   } n   scanf( %lld%lld , &m, &y); n   for (int i = 1; i <= m; i++) { n     num = read(); n     r = r * y + num; n   } n   puts(l == r ?  =  : (l > r ?  >  :  < )); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, c = 0; n   cin >> a; n   for (int i = 0; i < a; ++i) { n     cin >> b; n     if (b == 1) { n       c = 1; n     } n   } n   if (c == 0) { n     cout <<  EASY n ; n   } else { n     cout <<  HARD n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const int maxn = 4e3 + 5; n const int mod = 1e9 + 7; n const double eps = 1e-8; n const double pi = asin(1.0) * 2; n const double e = 2.718281828459; n bool Finish_read; n template <class T> n inline void read(T &x) { n   Finish_read = 0; n   x = 0; n   int f = 1; n   char ch = getchar(); n   while (!isdigit(ch)) { n     if (ch ==  - ) { n       f = -1; n     } n     if (ch == EOF) { n       return; n     } n     ch = getchar(); n   } n   while (isdigit(ch)) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   x *= f; n   Finish_read = 1; n }; n int main(int argc, char *argv[]) { n   int n, m; n   read(n); n   read(m); n   vector<int> a; n   a.push_back(0); n   for (int i = 1, x; i <= n; ++i) { n     read(x); n     a.push_back(x); n   } n   a.push_back(m); n   vector<int> f[2]; n   f[0].assign(a.size(), 0); n   f[1].assign(a.size(), 0); n   for (int i = int(a.size()) - 2; i >= 0; --i) { n     f[0][i] = f[1][i + 1]; n     f[1][i] = (a[i + 1] - a[i]) + f[0][i + 1]; n   } n   int ans = f[1][0]; n   for (int i = 0; i + 1 < int(a.size()); ++i) { n     if (a[i + 1] - a[i] < 2) { n       continue; n     } n     int tp = (i & 1) ^ 1; n     int pSum = f[1][0] - f[tp][i]; n     ans = max(ans, pSum + (a[i + 1] - a[i] - 1) + f[tp][i + 1]); n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> Q[123]; n int stk[123]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     int x; n     scanf( %d , &x); n     for (int j = 0; j < x; j++) { n       int u; n       scanf( %d , &u); n       Q[i].push_back(u); n     } n   } n   int cnt = 0; n   int A = 0, B = 0; n   for (int i = 0; i < n; i++) { n     if (Q[i].size() % 2 == 1) { n       stk[cnt++] = Q[i][Q[i].size() / 2]; n     } n     for (int j = 0; j < Q[i].size() / 2; j++) A += Q[i][j]; n     for (int j = 0; j < Q[i].size() / 2; j++) B += Q[i][Q[i].size() - 1 - j]; n   } n   sort(stk, stk + cnt); n   for (int i = cnt - 2; i > -1; i -= 2) B += stk[i]; n   for (int i = cnt - 1; i > -1; i -= 2) A += stk[i]; n   cout << A <<     << B <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n class Solution { n  public: n   void solve(std::istream& in, std::ostream& out) { n     int T; n     in >> T; n     for (int _t = 0; _t < (int)(T); ++_t) { n       int n, s, t; n       in >> n >> s >> t; n       int res = n - min(s, t) + 1; n       out << res << endl; n     } n   } n }; n void solve(std::istream& in, std::ostream& out) { n   out << std::setprecision(12); n   Solution solution; n   solution.solve(in, out); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   istream& in = cin; n   ostream& out = cout; n   solve(in, out); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[2001]; n int ou, ji; n int main() { n   int n; n   scanf( %d , &n); n   long long ans = 0; n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     ans += a[i]; n     if (a[i] % 2 == 0) n       ou++; n     else n       ji++; n   } n   if (ou > ji) n     ou = ji + 1; n   else if (ji > ou) n     ji = ou + 1; n   sort(a + 1, a + 1 + n); n   for (int i = n; i >= 1; i--) { n     if (a[i] % 2 == 0 && ou) { n       ans -= a[i]; n       ou--; n     } else if (a[i] % 2 != 0 && ji) { n       ans -= a[i]; n       ji--; n     } n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n namespace FGF { n const int N = 2e5 + 5; n int n, m; n long long read() { n   long long s = 0, w = 1; n   char ch = getchar(); n   while (!isdigit(ch)) { n     if (ch ==  - ) w = -w; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     s = s * 10 + ch -  0 ; n     ch = getchar(); n   } n   return s * w; n } n struct Node { n   long long a, b, tim; n   Node(){}; n   Node(long long _a, long long _b, long long _t) : a(_a), b(_b), tim(_t){}; n } c[N]; n bool operator<(Node x, Node y) { return x.tim > y.tim; } n bool check(long long x) { n   priority_queue<Node> q; n   while (q.size()) q.pop(); n   for (int i = 1; i <= n; i++) n     if (c[i].tim < m) q.push(c[i]); n   if (q.empty()) return 1; n   for (int i = 0; i < m; i++) { n     Node u = q.top(); n     q.pop(); n     if (u.tim < i) return 0; n     u.a += x; n     u.tim = u.a / u.b; n     if (u.tim < m) q.push(u); n     if (q.empty()) return 1; n   } n   return 1; n } n void work() { n   n = read(), m = read(); n   for (int i = 1; i <= n; i++) c[i].a = read(); n   for (int i = 1; i <= n; i++) c[i].b = read(), c[i].tim = c[i].a / c[i].b; n   long long l = 0, r = 5e12, ans = -1; n   while (l <= r) { n     long long mid = (l + r) >> 1; n     if (check(mid)) { n       ans = mid; n       r = mid - 1; n     } else n       l = mid + 1; n   } n   printf( %lld n , ans); n } n }  // namespace FGF n int main() { n   FGF::work(); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize(3) n using namespace std; n const double PI = acos(-1.0); n const double eps = 1e-6; n const int INF = 0x3f3f3f3f; n const long long SEED = 19260817; n const long long MOD = 1e9 + 9; n const int maxn = 2e5 + 11; n int N, M, T; n char a[maxn]; n long long cnt[maxn][26], dif[maxn], cx[maxn], cy[maxn]; n void solve(int x, int y, int l) { n   for (int i = 0; i < 26; i++) { n     cx[i] = (cnt[x + l - 1][i] - cnt[x - 1][i] * dif[l] % MOD + MOD) % MOD; n     cy[i] = (cnt[y + l - 1][i] - cnt[y - 1][i] * dif[l] % MOD + MOD) % MOD; n   } n   sort(cx, cx + 26); n   sort(cy, cy + 26); n   for (int i = 0; i < 26; i++) n     if (cx[i] != cy[i]) { n       puts( NO ); n       return; n     } n   puts( YES ); n   return; n } n int main() { n   cin >> N >> M; n   scanf( %s , a + 1); n   dif[0] = 1; n   for (int i = 1; i <= N; i++) { n     dif[i] = (dif[i - 1] * SEED) % MOD; n     for (int j = 0; j < 26; j++) cnt[i][j] = cnt[i - 1][j] * SEED % MOD; n     cnt[i][a[i] -  a ] = (cnt[i][a[i] -  a ] + 1) % MOD; n   } n   while (M--) { n     int x, y, l; n     scanf( %d%d%d , &x, &y, &l); n     solve(x, y, l); n   } n   return 0; n } n