#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline void read(T &n) { n   T w = 1; n   n = 0; n   char ch = getchar(); n   while (!isdigit(ch) && ch != EOF) { n     if (ch ==  - ) w = -1; n     ch = getchar(); n   } n   while (isdigit(ch) && ch != EOF) { n     n = (n << 3) + (n << 1) + (ch & 15); n     ch = getchar(); n   } n   n *= w; n } n template <typename T> n inline void write(T x) { n   T l = 0; n   unsigned long long y = 0; n   if (!x) { n     putchar(48); n     return; n   } n   if (x < 0) { n     x = -x; n     putchar( - ); n   } n   while (x) { n     y = y * 10 + x % 10; n     x /= 10; n     ++l; n   } n   while (l) { n     putchar(y % 10 + 48); n     y /= 10; n     --l; n   } n } n template <typename T> n inline void writes(T x) { n   write(x); n   putchar(   ); n } n template <typename T> n inline void writeln(T x) { n   write(x); n   puts(  ); n } n template <typename T> n inline void checkmax(T &a, T b) { n   a = a > b ? a : b; n } n template <typename T> n inline void checkmin(T &a, T b) { n   a = a < b ? a : b; n } n const int N = 1e5 + 10, M = 2e5 + 10; n int n, m, cnt, head[N], nxt[M << 1], to[M << 1], vis[N], in[N], dep[N], n     depth[N], depto[N], sum[N], ok[N]; n inline int rd() { n   return int((long long)(rand() % 100000) * (rand() % 100000) % 998244353); n } n inline void addedge(int x, int y) { n   nxt[++cnt] = head[x]; n   to[cnt] = y; n   head[x] = cnt; n } n inline bool check(int x) { n   vis[x] = in[x] = 1; n   bool f = 1; n   for (int i = head[x]; i; i = nxt[i]) { n     if (vis[to[i]] && !in[to[i]]) return 0; n     if (vis[to[i]]) continue; n     f &= check(to[i]); n   } n   in[x] = 0; n   return f; n } n inline void dfs(int x) { n   vis[x] = 1; n   for (int i = head[x]; i; i = nxt[i]) { n     if (vis[to[i]]) { n       if (depth[to[i]] < dep[x]) { n         dep[x] = depth[to[i]]; n         depto[x] = to[i]; n       } n       sum[x]++; n       sum[to[i]]--; n       continue; n     } n     depth[to[i]] = depth[x] + 1; n     dfs(to[i]); n     sum[x] += sum[to[i]]; n     if (dep[x] > dep[to[i]]) { n       dep[x] = dep[to[i]]; n       depto[x] = depto[to[i]]; n     } n   } n } n inline void dfs2(int x) { n   vis[x] = 1; n   if (sum[x] >= 2 || (sum[x] == 1 && ok[depto[x]])) ok[x] = 1; n   for (int i = head[x]; i; i = nxt[i]) { n     if (vis[to[i]]) continue; n     dfs2(to[i]); n   } n } n inline void doit(int rt) { n   for (int i = 1; i <= n; ++i) { n     vis[i] = sum[i] = ok[i] = 0; n     dep[i] = n * 2 + 1; n   } n   depth[rt] = 0; n   dfs(rt); n   for (int i = 1; i <= n; ++i) vis[i] = 0; n   dfs2(rt); n   int ans = 0; n   for (int i = 1; i <= n; ++i) n     if (!ok[i]) ++ans; n   if (ans * 5 < n) { n     puts( -1 ); n     return; n   } n   for (int i = 1; i <= n; ++i) n     if (!ok[i]) writes(i); n   puts(  ); n } n inline void solve() { n   read(n); n   read(m); n   for (int i = 1; i <= n; ++i) head[i] = vis[i] = in[i] = 0; n   cnt = 0; n   for (int i = 1; i <= m; ++i) { n     int x, y; n     read(x); n     read(y); n     addedge(x, y); n   } n   for (int i = 1; i <= 50; ++i) { n     int num = rd() % n + 1; n     for (int i = 1; i <= n; ++i) vis[i] = in[i] = 0; n     if (check(num)) { n       doit(num); n       return; n     } n   } n   puts( -1 ); n } n int main() { n   srand(time(0) + 1432433); n   int t; n   read(t); n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a, b, n, i, p[1001], sz[1001], cnt[1001]; n vector<pair<int, int> > v2; n vector<int> v; n int find_p(int a) { n   if (p[a] == a) return a; n   return p[a] = find_p(p[a]); n } n void unionn(int a, int b) { n   int _a = a, _b = b; n   a = find_p(a); n   b = find_p(b); n   if (a != b) { n     if (sz[a] < sz[b]) swap(a, b); n     p[b] = a; n     sz[a] += sz[b]; n   } else n     v2.push_back({_a, _b}); n } n int main() { n   cin >> n; n   for (i = 1; i <= n; i++) p[i] = i, sz[i] = 1; n   for (i = 1; i < n; i++) { n     cin >> a >> b; n     unionn(a, b); n   } n   cout << v2.size() << endl; n   for (i = 1; i <= n; i++) { n     p[i] = find_p(i); n     if (!cnt[p[i]]) { n       v.push_back(p[i]); n       cnt[p[i]] = 1; n     } n   } n   for (i = 0; i < v2.size(); i++) { n     cout << v2[i].first <<     << v2[i].second <<     << v[i] <<     << v[i + 1] n          << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n auto main() -> int { n   int N, X, Y; n   cin >> N; n   vector<int> C(N); n   for (int i = 0; i < N; i++) cin >> C[i]; n   cin >> X >> Y; n   int l = 0; n   int ret = 0; n   for (int i = 0; i < N; i++) { n     if (l < X) { n       if (l + C[i] <= Y) n         l += C[i]; n       else n         break; n     } else if (l >= X && l <= Y) { n       int t = 0; n       for (int j = i; j < N; j++) t += C[j]; n       if (t >= X && t <= Y) { n         ret = i + 1; n         break; n       } else { n         l += C[i]; n       } n     } n   } n   cout << ret << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N, M, i, j; n int a, b, v[105]; n bool ok = true; n int main() { n   cin >> N >> M; n   for (i = 1; i <= N; i++) { n     cin >> a >> b; n     a++; n     v[a]++; n     v[b + 1]--; n   } n   for (i = 1; i <= M; i++) { n     v[i] += v[i - 1]; n     if (v[i] == 0) ok = false; n   } n   if (ok == true) n     cout <<  YES ; n   else n     cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int OO = 1e9, N = 1e5; n int main() { n   long long n, c = 0, o = 0; n   cin >> n; n   string s; n   cin >> s; n   queue<int> q; n   for (int i = 0; i < n; i++) { n     o += (s[i] ==  ( ); n     c += (s[i] ==  ) ); n   } n   int sma7 = 0; n   for (int i = 0; i < n; i++) { n     if (s[i] ==  )  && !q.empty()) n       q.pop(); n     else if (s[i] ==  ) ) n       sma7++; n     else n       q.push(0); n   } n   if (o == c && sma7 <= 1) n     cout <<  YES ; n   else n     cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[100009], x[100009], h[100009], id[100009], seg[400009]; n void upd(int pos, int l, int r, int pp, int v) { n   if (l == r) { n     seg[pos] = v; n     return; n   } n   int m = (l + r) / 2; n   if (m >= pp) n     upd(2 * pos + 1, l, m, pp, v); n   else n     upd(2 * pos + 2, m + 1, r, pp, v); n   seg[pos] = max(seg[2 * pos + 1], seg[2 * pos + 2]); n } n int qu(int pos, int l, int r, int b, int e) { n   if (b <= l && r <= e) return seg[pos]; n   int m = (l + r) / 2; n   int r1 = 0, r2 = 0; n   if (m >= b) r1 = qu(2 * pos + 1, l, m, b, e); n   if (m < e) r2 = qu(2 * pos + 2, m + 1, r, b, e); n   return max(r1, r2); n } n int ans[100009]; n void bs(int i, int l, int r) { n   int m; n   int o = x[i] + h[i] - 1; n   while (r - l > 1) { n     m = (l + r) / 2; n     if (o < x[m]) n       r = m; n     else n       l = m; n   } n   if (l == i) n     ans[id[i]] = i; n   else n     ans[id[i]] = qu(0, 0, n - 1, i + 1, l); n   upd(0, 0, n - 1, i, ans[id[i]]); n   ans[id[i]] -= i; n   ans[id[i]]++; n } n pair<int, pair<int, int> > p[100009]; n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> x[i] >> h[i]; n     p[i] = {x[i], {h[i], i}}; n   } n   sort(p, p + n); n   for (int i = 0; i < n; i++) { n     x[i] = p[i].first; n     h[i] = p[i].second.first; n     id[i] = p[i].second.second; n   } n   ans[id[n - 1]] = 1; n   upd(0, 0, n - 1, n - 1, n - 1); n   for (int i = n - 2; i >= 0; i--) { n     bs(i, i, n); n   } n   for (int i = 0; i < n; i++) cout << ans[i] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int arr[1005]; n int res[1005]; 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) cin >> arr[i]; n   sort(arr, arr + n); n   int ini = 0, fin = n - 1, idx = 0; n   while (ini <= fin) { n     res[idx] = arr[ini++]; n     res[idx + 1] = arr[fin--]; n     idx += 2; n   } n   for (int i = 0; i < n; ++i) cout << res[i] <<    ; n   cout <<   n ; n   return 0; n } n


using namespace std; n #include <cstdio> n #include <cstring> n #include <algorithm> n #include <ctime> n #include <cstdlib> n #include <set> n #include <map> n #define N 1005 n #define ll long long n ll mul(ll a,ll b,ll mo){ n  a%=mo,b%=mo; n  ll c=a*b-(ll)((long double)a*b/mo)*mo; n  if (c<0) c+=mo; n  if (c>=mo) c-=mo; n  return c; n } n ll qpow(ll x,ll y,ll mo){ n  ll r=1; n  for (;y;y>>=1,x=mul(x,x,mo)) n   if (y&1) n    r=mul(r,x,mo); n  return r; n } n int n,k; n ll a[N]; n ll p[N][100],np[N]; n const int tst[10]={2,3,5,7,11,13,17,19,23,29}; n bool mr(ll x){ n  if (x==1 || x==0) n   return 0; n  for (int i=0;i<10;++i){ n   if (x==tst[i]) n    return 1; n   if (x%tst[i]==0) n    return 0; n  } n  ll z=x-1,y=0; n  while (z&1^1) n   z>>=1,++y; n  for (int i=0;i<10;++i){ n   ll t=qpow(tst[i],z,x); n   if (t==1 || t==x-1) n    continue; n   for (int j=1;j<y;++j){ n    t=mul(t,t,x); n    if (t==x-1) n     break; n   } n   if (t!=x-1) n    return 0; n  } n  return 1; n } n #define irand (rand()*RAND_MAX+rand()) n ll gcd(ll a,ll b){ n  ll k; n  while (b) n   k=a%b,a=b,b=k; n  return a; n } n ll pr(ll n){ n  while (1){ n   ll a=irand%n,c=irand%n,b=a,s=1; n   for (int i=1,k=2;1;++i){ n    a=(mul(a,a,n)+c)%n; n    if (a==b) n     break; n    ll t=mul(s,abs(a-b),n); n    if (t==0) n     return gcd(s,n); n    s=t; n    if (i==k){ n     ll g=gcd(s,n); n     if (g!=1) n      return g; n     b=a; n     k<<=1; n     continue; n    } n    if (!(i&127)){ n     ll g=gcd(s,n); n     if (g!=1) n      return g; n    } n   } n  } n } n void divide(ll x,int id){ n  if (x==1) n   return; n  if (mr(x)){ n   p[id][np[id]++]=x; n   return; n  } n  ll d=pr(x); n  divide(d,id); n  divide(x/d,id); n } n map<int,pair<int,int> > S; n set<int> T; n bool legal[N]; n bool b[N]; n int main(){ n // freopen( in.txt , r ,stdin); n // freopen( out.txt , w ,stdout); n  srand(time(0)* *new(int)); n  scanf( %d%d ,&n,&k); n  for (int i=1;i<=n;++i) n   scanf( %lld ,&a[i]); n  for (int i=1;i<=n;++i){ n   divide(a[i],i); n   sort(p[i],p[i]+np[i]); n   np[i]=unique(p[i],p[i]+np[i])-p[i]; n   if (np[i]==1){ n    auto q=S.find(p[i][0]); n    if (q==S.end()) n     S[p[i][0]].first=i,b[i]=1; n    else if (q->second.second==0) n     q->second.second=i,b[i]=1; n   } n  } n  for (auto q=S.begin();q!=S.end();) n   if (q->second.second==0) n    q=S.erase(q); n   else n    ++q; n  int mn=0; n  np[0]=77114514; n  int ex=0; n  for (int i=1;i<=n;++i){ n   legal[i]=1; n   for (int j=0;j<np[i] && legal[i];++j){ n    auto q=S.find(p[i][j]); n    if (q==S.end()) n     legal[i]=0; n   } n   if (legal[i]){ n    if (!b[i]){ n     ex++; n     if (np[i]<np[mn]) n      mn=i; n    } n   } n   else n    b[i]=0; n  } n  static int ls[N]; n  if (k>=S.size()*2){ n   k-=S.size()*2; n   if (k>ex){ n    printf( 0 n ); n    return 0; n   } n   for (int i=1;i<=n;++i){ n    if (!legal[i]) n     continue; n    if (b[i]) n     printf( %lld  ,a[i]); n    else if (k){ n     printf( %lld  ,a[i]); n     k--;     n    } n   } n  } n  else if (k>=np[mn]*2+(mn!=0)){ n   for (int i=0;i<np[mn];++i){ n    T.insert(p[mn][i]); n    ls[i*2+1]=S[p[mn][i]].first; n    ls[i*2+2]=S[p[mn][i]].second; n   } n   int i=np[mn]*2; n   if (k&1) n    ls[++i]=mn; n   for (auto q=S.begin();q!=S.end() && i<k;++q) n    if (T.find(q->first)==T.end()){ n     ls[i+1]=q->second.first; n     ls[i+2]=q->second.second; n     i+=2; n    } n   sort(ls+1,ls+k+1); n   for (int i=1;i<=k;++i) n    printf( %lld  ,a[ls[i]]); n  } n  else{ n   if (k&1) n    printf( 0 ); n   else{ n    int i=0; n    for (auto q=S.begin();q!=S.end() && i<k;++q,i+=2){ n     ls[i+1]=q->second.first; n     ls[i+2]=q->second.second; n    } n    sort(ls+1,ls+k+1); n    for (int i=1;i<=k;++i) n     printf( %lld  ,a[ls[i]]); n   } n  } n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-8; n const double pi = acos(-1); n const long long inf = 0x3f3f3f3f3f3f3f3f; n const int INF = 0x3f3f3f3f; n const int MAX = 2e5 + 10; n const long long mod = 1e9 + 7; n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n char c[3] = { R ,  G ,  B }; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int dif[3] = {0}; n     int n, k; n     cin >> n >> k; n     string s; n     cin >> s; n     for (int i = (0); i < (3); ++i) { n       for (int j = (0); j < (k); ++j) { n         if (s[j] != c[(j + i) % 3]) dif[i]++; n       } n     } n     int ans = 0; n     ans = min(dif[0], min(dif[1], dif[2])); n     for (int i = 1; i + k - 1 < n; i++) { n       int a0 = dif[0], a1 = dif[1], a2 = dif[2]; n       dif[0] = a2 - (s[i - 1] ==  B  ? 0 : 1) + n                (s[i + k - 1] == c[(k - 1) % 3] ? 0 : 1); n       dif[1] = n           a0 - (s[i - 1] ==  R  ? 0 : 1) + ((s[i + k - 1] == c[k % 3]) ? 0 : 1); n       dif[2] = a1 - (s[i - 1] ==  G  ? 0 : 1) + n                ((s[i + k - 1] == c[(k + 1) % 3]) ? 0 : 1); n       ans = min(ans, min(dif[0], min(dif[1], dif[2]))); n     } n     cout << ans <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 500005, INF = 0x3f3f3f3f; n int ta, tb, n, T; n long long t[maxn], t1[maxn]; n int main(void) { n   cin >> n >> ta >> tb >> T; n   string s; n   char a; n   cin >> a; n   if (a ==  w ) T -= tb; n   T--; n   if (T < 0) { n     cout << 0 << endl; n     return 0; n   } n   cin >> s; n   for (int i = 0; i < n - 1; i++) { n     t[i] = t[i - 1] + 1 + ta; n     if (s[i] ==  w ) t[i] += tb; n   } n   reverse(s.begin(), s.end()); n   for (int i = 0; i < n - 1; i++) { n     t1[i] = t1[i - 1] + 1 + ta; n     if (s[i] ==  w ) t1[i] += tb; n   } n   int res = 0; n   for (int i = 0; i < n; i++) { n     if (t1[i - 1] > T) break; n     int l = 0, r = n - i; n     while (l < r - 1) { n       int mid = (l + r) / 2; n       if (mid > 0 && t1[i - 1] + t[mid - 1] + i * ta <= T) n         l = mid; n       else n         r = mid; n     } n     res = max(res, l + i); n   } n   for (int i = 0; i < n; i++) { n     if (t[i - 1] > T) break; n     int l = 0, r = n - i; n     while (l < r - 1) { n       int mid = (l + r) / 2; n       if (mid > 0 && t[i - 1] + t1[mid - 1] + i * ta <= T) n         l = mid; n       else n         r = mid; n     } n     res = max(res, l + i); n   } n   cout << res + 1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool dif[10]; n int p[1005], n, x, y, ans1, ans2; n inline void chk(int p) { n   int i, sz = 0, res; n   for (i = 1; i <= n; i++) { n     if (i & (1 << p)) { n       sz++; n     } n   } n   if (sz == 0) { n     dif[p] = 0; n     return; n   } n   printf( ? %d  , sz); n   for (i = 1; i <= n; i++) { n     if (i & (1 << p)) { n       printf( %d  , i); n     } n   } n   printf(  n ); n   fflush(stdout); n   scanf( %d , &res); n   if (sz & 1) { n     if (res == x) { n       dif[p] = 0; n       return; n     } n     if (res == y) { n       dif[p] = 1; n       return; n     } n   } else { n     if (res == (x ^ y)) { n       dif[p] = 1; n       return; n     } n     if (res == 0) { n       dif[p] = 0; n       return; n     } n   } n } n inline bool ok(int pr) { n   int res, i; n   printf( ? %d  , pr); n   for (i = 1; i <= pr; i++) { n     printf( %d  , p[i]); n   } n   printf(  n ); n   fflush(stdout); n   scanf( %d , &res); n   if (pr & 1) { n     if (res == x) { n       return 0; n     } n     if (res == y) { n       return 1; n     } n   } else { n     if (res == (x ^ y)) { n       return 1; n     } n     if (res == 0) { n       return 0; n     } n   } n } n inline int get(int x) { n   int i, cnt = 0; n   for (i = 1; i <= n; i++) { n     if (!(i & (1 << x))) { n       continue; n     } n     p[++cnt] = i; n   } n   int l = 1, r = cnt; n   while (l <= r) { n     int mid = (l + r) >> 1; n     if (ok(mid)) { n       r = mid - 1; n     } else { n       l = mid + 1; n     } n   } n   return p[l]; n } n int main() { n   int i; n   scanf( %d%d%d , &n, &x, &y); n   for (i = 9; i >= 0; i--) { n     chk(i); n   } n   for (i = 9; i >= 0; i--) { n     if (dif[i]) { n       ans1 = get(i); n       break; n     } n   } n   ans2 = ans1; n   for (i = 9; i >= 0; i--) { n     if (dif[i]) { n       ans2 ^= (1 << i); n     } n   } n   printf( ! %d %d n , ans2, ans1); n   fflush(stdout); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s; n queue<char> q; n vector<int> sp; n int main() { n   long long n, l = 0, r = 0, k = 0; n   cin >> s; n   if (s[0] !=  a  && s[0] !=  e  && s[0] !=  u  && s[0] !=  o  && s[0] !=  i ) { n     q.push(s[0]); n     k = 1; n   } n   for (int i = 1; i < s.size(); i++) { n     if (s[i] !=  a  && s[i] !=  e  && s[i] !=  u  && s[i] !=  o  && n         s[i] !=  i ) { n       if (s[i] != s[i - 1]) q.push(s[i]); n       k++; n       if (k >= 3 && q.size() > 1) { n         sp.push_back(i); n         n = q.size(); n         while (q.size() > 1) q.pop(); n         k = 1; n       } n     } else { n       while (q.size() != 0) q.pop(); n       k = 0; n     } n   } n   int j = 0; n   sp.push_back(s.size()); n   for (int i = 0; i < s.size(); i++) { n     while (i < sp[j]) { n       cout << s[i]; n       i++; n     } n     j++; n     if (i < s.size()) cout <<     << s[i]; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int arr[100005]; n int brr[100005]; n char s[1234][1234]; n int vis[1234][1234]; n int has[1234][1234]; n int f[1234567]; n int n, m, c, g; n void dfs(int x, int y) { n   if (x < 0 || y < 0 || x == n || y == m || vis[x][y] || s[x][y] ==  * ) return; n   if (s[x][y] ==  . ) { n     vis[x][y] = 1; n     has[x][y] = g; n     f[g]++; n     dfs(x, y + 1); n     dfs(x, y - 1); n     dfs(x - 1, y); n     dfs(x + 1, y); n   } n } n int main() { n   cin >> n >> m; n   int i, j; n   for (i = 0; i < n; i++) { n     for (j = 0; j < m; ++j) { n       cin >> s[i][j]; n     } n   } n   for (i = 0; i < n; ++i) { n     for (j = 0; j < m; ++j) { n       if (s[i][j] ==  .  && vis[i][j] == 0) { n         g++; n         dfs(i, j); n       } n     } n   } n   for (i = 0; i < n; ++i) { n     for (j = 0; j < m; ++j) { n       c = 1; n       set<int> se; n       if (s[i][j] ==  * ) { n         if (j > 0) se.insert(has[i][j - 1]); n         if (i > 0) se.insert(has[i - 1][j]); n         if (j + 1 < m) se.insert(has[i][j + 1]); n         if (i + 1 < n) se.insert(has[i + 1][j]); n         for (set<int>::iterator it = se.begin(); it != se.end(); ++it) { n           c = c + f[*it]; n         } n         cout << c % 10; n       } else n         cout <<  . ; n     } n     cout << endl; n   } n } n


#include <iostream> n #include <string> n #include <vector> n  n constexpr int N = 1000005; n constexpr int p = 1000000007; n  n inline int add(int x, int y) { return (x += y) >= p ? x - p : x; } n inline int sub(int x, int y) { return (x -= y) < 0 ? x + p : x; } n inline int mul(int x, int y) { return (long long)x * y % p; } n inline int power(int x, int y) { int ans = 1; for (; y; y >>= 1) { if (y & 1) ans = mul(ans, x); x = mul(x, x); } return ans; } n  n int n, q, k; n std::string s0, t, w; n int pow2[N], nxt[N]; n int sum[N][26]; n std::vector<std::string> str; n  n int main() { n     std::ios::sync_with_stdio(false), std::cin.tie(nullptr); n     std::cin >> n >> q; n     std::cin >> s0 >> t; n     pow2[0] = 1; n     for (int i = 1; i <= n; ++i) pow2[i] = add(pow2[i - 1], pow2[i - 1]); n     for (int i = 1; i <= n; ++i) { n         for (int j = 0; j < 26; ++j) sum[i][j] = sum[i - 1][j]; n         sum[i][t[i - 1] -  a ] = add(sum[i][t[i - 1] -  a ], power(pow2[i], p - 2)); n     } n     std::string tmp = s0; n     str.push_back(tmp); n     int x = 0; n     while (tmp.length() < N << 1) { n         tmp = tmp + t[x++] + tmp; n         str.push_back(tmp); n     } n     while (q--) { n         std::cin >> k >> w; n         int x = 0; n         while (str[x].length() < w.length() && x < k) ++x; n         nxt[0] = -1; n         int now = -1; n         for (int i = 1; i < w.length(); ++i) { n             while (~now && w[now + 1] != w[i]) now = nxt[now]; n             if (w[now + 1] == w[i]) ++now; n             nxt[i] = now; n         } n         now = -1; n         int cnt0 = 0; n         std::string& s = str[x]; n         for (int i = 0; i < s.length(); ++i) { n             while (~now && w[now + 1] != s[i]) now = nxt[now]; n             if (w[now + 1] == s[i]) ++now; n             if (now == w.length() - 1) { n                 ++cnt0; n                 now = nxt[now]; n             } n         } n         int index = s.length(); n         int ans = mul(cnt0, power(pow2[x], p - 2)); n         for (int i = 0; i < 26; ++i) { n             int cnt = -cnt0 << 1; n             now = -1; n             for (int j = 0; j < str[x + 1].length(); ++j) { n                 char tmp = str[x + 1][j]; n                 if (j == index) tmp =  a  + i; n                 while (~now && w[now + 1] != tmp) now = nxt[now]; n                 if (w[now + 1] == tmp) ++now; n                 if (now == w.length() - 1) { n                     ++cnt; n                     now = nxt[now]; n                 } n             } n             ans = add(ans, mul(cnt, sub(sum[k][i], sum[x][i]))); n         } n         std::cout << mul(ans, pow2[k]) <<   n ; n     } n     return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<string> split(const string& s, char c) { n   if (s.length() == 0) return vector<string>(); n   vector<string> v; n   stringstream ss(s); n   string x; n   while (getline(ss, x, c)) { n     v.emplace_back(x); n   } n   return std::move(v); n } n vector<int> splitInt(const string& s, char c) { n   if (s.length() == 0) return vector<int>(); n   vector<int> v; n   stringstream ss(s); n   int x; n   string str; n   while (getline(ss, str, c)) { n     stringstream si(str); n     si >> x; n     v.emplace_back(x); n   } n   return std::move(v); n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t = 0; n   cin >> t; n   while (t-- > 0) { n     int n; n     cin >> n; n     vector<int> arr(n, 0); n     for (int i = 0; i < n; ++i) cin >> arr[i]; n     vector<long long> seen(3001, 0); n     vector<vector<long long>> left(3001); n     for (int i = 0; i < n; ++i) { n       seen[arr[i]]++; n       left[i] = seen; n     } n     seen = vector<long long>(3001, 0); n     vector<vector<long long>> right(3001); n     for (int i = n - 1; i >= 0; --i) { n       seen[arr[i]]++; n       right[i] = seen; n     } n     long long count = 0; n     for (int j = 1; j < n; ++j) { n       for (int k = j + 1; k + 1 < n; ++k) { n         int aj = arr[j]; n         int ak = arr[k]; n         if (j - 1 >= 0 && k + 1 < n) { n           count += left[j - 1][ak] * right[k + 1][aj]; n         } n       } n     } n     cout << count <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int compare(const void* x1, const void* x2) { return (*(int*)x1 - *(int*)x2); } n int main() { n   int n, a; n   cin >> n >> a; n   int k = n / 2; n   if (a % 2 == 1) n     cout << a / 2 + 1; n   else n     cout << (k - a / 2 + 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, x, y, d, T; n int main() { n   cin >> T; n   while (T--) { n     cin >> n >> x >> y >> d; n     int sum; n     if ((abs(y - x)) % d == 0 || (n - y) % d == 0 || (y - 1) % d == 0) { n       if ((abs(y - x)) % d == 0) n         sum = (abs(y - x)) / d; n       else { n         int sxl, sxr; n         if ((x - 1) % d) n           sxl = (x - 1) / d + 1; n         else n           sxl = (x - 1) / d; n         if ((n - x) % d) n           sxr = (n - x) / d + 1; n         else n           sxr = (n - x) / d; n         int fl = (y - 1) % d, fr = (n - y) % d; n         if (fl == 0 && fr != 0) sum = sxl + (y - 1) / d; n         if (fl != 0 && fr == 0) sum = sxr + (n - y) / d; n         if (fl == 0 && fr == 0) { n           sum = min(sxl + (y - 1) / d, sxr + (n - y) / d); n         } n       } n       cout << sum << endl; n     } else n       cout <<  -1  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   char s[100001]; n   int a[100001], m, l, r; n   scanf( %s%d , s, &m); n   int len = strlen(s); n   a[1] = 0; n   for (int i = 1; i < len; ++i) { n     if (s[i] == s[i - 1]) { n       a[i + 1] = a[i] + 1; n     } else { n       a[i + 1] = a[i]; n     } n   } n   while (m--) { n     scanf( %d%d , &l, &r); n     printf( %d n , a[r] - a[l]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   static char buf[1 << 21], *p1, *p2; n   int f = 0, c = 1; n   char ch = n       (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) n            ? EOF n            : *p1++); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) c = -1; n     ch = n         (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) n              ? EOF n              : *p1++); n   } n   while (ch >=  0  && ch <=  9 ) { n     f = f * 10 + ch -  0 ; n     ch = n         (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) n              ? EOF n              : *p1++); n   } n   return c * f; n } n const int M = 3e5 + 10; n struct node { n   long long k, b; n   inline long long operator()(const long long &x) { return x * k + b; } n   node(long long x = 0, long long y = 0) { k = x, b = y; } n } e, f[M * 4]; n vector<int> v[M]; n int fa[M], siz[M], mx[M], dep[M], belong[M], vis[M]; n long long v1[M], v2[M], st[M], l[M], a[M], s[M]; n int sz, n, x, y, md, top, rt; n long long ans; n void build(int o, int l, int r) { n   f[o] = (node){0, 0}; n   if (l == r) return; n   build((o << 1), l, ((l + r) >> 1)); n   build((o << 1 | 1), ((l + r) >> 1) + 1, r); n } n long long ask(int o, int l, int r, long long x) { n   long long ans = f[o](x); n   if (l == r) return ans; n   if (x <= ((l + r) >> 1)) n     return max(ans, ask((o << 1), l, ((l + r) >> 1), x)); n   else n     return max(ans, ask((o << 1 | 1), ((l + r) >> 1) + 1, r, x)); n } n void insert(int o, int l, int r, node x) { n   if (f[o](((l + r) >> 1)) < x(((l + r) >> 1))) swap(f[o], x); n   if (l == r) return; n   if (x.k < f[o].k) n     insert((o << 1), l, ((l + r) >> 1), x); n   else n     insert((o << 1 | 1), ((l + r) >> 1) + 1, r, x); n } n void getroot(int x, int fa) { n   siz[x] = 1; n   mx[x] = 0; n   for (int i = 0; i < v[x].size(); ++i) { n     int y = v[x][i]; n     if (y == fa || vis[y]) continue; n     getroot(y, x); n     siz[x] += siz[y]; n     mx[x] = max(mx[x], siz[y]); n   } n   mx[x] = max(mx[x], sz - siz[x]); n   if (mx[x] < mx[rt]) rt = x; n } n void dfs(int x, int fa, long long val1, long long val2, long long sum, n          int from) { n   if (x != rt && !from) from = x; n   dep[x] = dep[fa] + 1; n   md = max(md, dep[x]); n   int f = 0; n   siz[x] = 1; n   for (int i = 0; i < v[x].size(); ++i) { n     int y = v[x][i]; n     if (y == fa || vis[y]) continue; n     dfs(y, x, val1 + sum + a[y], val2 + a[y] * dep[x], sum + a[y], from); n     siz[x] += siz[y]; n     f = 1; n   } n   if (!f) { n     st[++top] = x, v1[top] = val1, v2[top] = val2, s[top] = sum - a[rt], n     l[top] = dep[x], belong[top] = from; n   } n } n void dfz(int x) { n   vis[x] = 1; n   top = 0; n   md = 0; n   dfs(x, 0, a[x], 0, a[x], 0); n   st[++top] = x; n   v1[top] = a[x], l[top] = 1, v2[top] = s[top] = belong[top] = 0; n   build(1, 1, md); n   belong[top + 1] = belong[0] = -1; n   for (int i = 1; i <= top;) { n     int j = i; n     while (belong[j] == belong[i]) n       ans = max(ans, ask(1, 1, md, l[j]) + v1[j]), ++j; n     j = i; n     while (belong[j] == belong[i]) insert(1, 1, md, node(s[j], v2[j])), ++j; n     i = j; n   } n   build(1, 1, md); n   for (int i = top; i >= 1;) { n     int j = i; n     while (belong[j] == belong[i]) n       ans = max(ans, ask(1, 1, md, l[j]) + v1[j]), --j; n     j = i; n     while (belong[i] == belong[j]) insert(1, 1, md, node(s[j], v2[j])), --j; n     i = j; n   } n   for (int i = 0; i < v[x].size(); ++i) { n     int y = v[x][i]; n     if (vis[y]) continue; n     sz = siz[y]; n     rt = 0; n     getroot(y, x); n     dfz(rt); n   } n } n int main() { n   n = read(); n   for (int i = 1; i < n; ++i) { n     x = read(); n     y = read(); n     v[x].push_back(y); n     v[y].push_back(x); n   } n   for (int i = 1; i <= n; ++i) a[i] = read(); n   siz[0] = mx[0] = sz = n; n   getroot(1, 0); n   dfz(rt); n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int vocala(char c) { n   if (c ==  a  || c ==  A  || c ==  e  || c ==  E  || c ==  i  || c ==  I  || n       c ==  o  || c ==  O  || c ==  u  || c ==  U ) n     return 1; n   if (c ==  y  || c ==  Y ) return 1; n   return 0; n } n int isprime(long long n) { n   if (n <= 1) return 0; n   for (long long i = 2; i * i <= n; ++i) n     if (n % i == 0) return 0; n   return 1; n } n int isfibo(long long n) { n   long long a = 5 * n * n + 4; n   long long b = a - 8; n   if (sqrt(a) == int(sqrt(a)) || sqrt(b) == int(sqrt(b))) return 1; n   return 0; n } n int gcd(long long a, long long b) { n   long long r; n   while (b) { n     r = a % b; n     a = b; n     b = r; n   } n   return a; n } n ifstream in( input.txt ); n ofstream out( output.txt ); n long long const nrmax = 1e18; n long long v[100005]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long n; n   cin >> n; n   for (int i = 1; i <= n; ++i) { n     cin >> v[i]; n   } n   long long cnt = 3e9; n   sort(v + 1, v + n + 1); n   cnt = min(cnt, v[n] - v[2]); n   cnt = min(cnt, v[n - 1] - v[1]); n   cout << cnt <<   n ; n   in.close(); n   out.close(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n using vv = vector<vector<T>>; n template <class T> n ostream &operator<<(ostream &os, const vector<T> &t) { n   os <<  { ; n   for (int(i) = 0; (i) < (t.size()); ++(i)) { n     os << t[i] <<  , ; n   } n   os <<  }  << endl; n   return os; n } n template <class S, class T> n ostream &operator<<(ostream &os, const pair<S, T> &t) { n   return os <<  (  << t.first <<  ,  << t.second <<  ) ; n } n template <class T> n inline bool MX(T &l, const T &r) { n   return l < r ? l = r, 1 : 0; n } n template <class T> n inline bool MN(T &l, const T &r) { n   return l > r ? l = r, 1 : 0; n } n vector<string> s_p_l_i_t(const string &s, char c) { n   vector<string> v; n   stringstream ss(s); n   string x; n   while (getline(ss, x, c)) v.emplace_back(x); n   return move(v); n } n void e_r_r(vector<string>::iterator it) {} n template <typename T, typename... Args> n void e_r_r(vector<string>::iterator it, T a, Args... args) { n   if (*it ==   1  || *it ==  1 ) n     cerr << endl; n   else n     cerr << it->substr((*it)[0] ==    , it->length()) <<   =   << a <<  ,  ; n   e_r_r(++it, args...); n } n const long long MOD = 1e9 + 7; n int main() { n   ios_base::sync_with_stdio(false); n   cout << fixed << setprecision(0); n   int n; n   cin >> n; n   cout <<  YES  << endl; n   int a, b, c, d; n   for (int(i) = 0; (i) < (n); ++(i)) { n     cin >> a >> b >> c >> d; n     cout << abs(a) % 2 * 2 + abs(b) % 2 + 1 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, p; n   cin >> n >> p; n   string s; n   cin >> s; n   if (p > s.length() / 2) { n     reverse(s.begin(), s.end()); n     p = (n - p) + 1; n   } n   int sum = 0; n   int t[n], l = -1, r = 0; n   for (int i = 0; i < s.length() / 2; i++) { n     if (abs(int(s[i]) - int(s[n - i - 1])) > 13) { n       t[i] = 26 - abs(int(s[i]) - int(s[n - i - 1])); n     } else { n       t[i] = abs(int(s[i]) - int(s[n - i - 1])); n     } n     if (t[i] > 0) { n       r = i; n       if (l < 0) { n         l = i; n       } n     } n     sum += t[i]; n   } n   p--; n   if (l <= p && p <= r && p - l > r - p) { n     sum += r - p + r - l; n   } else if (l <= p && p <= r && p - l <= r - p) { n     sum += p - l + r - l; n   } else if (p <= l) { n     sum += r - p; n   } else if (r <= p) { n     sum += p - l; n   } n   if (l < 0) { n     cout << 0; n   } else { n     cout << sum; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long a, b, c, d, x, y, g, h, i, j, k, n, t, z = 0, l, r, u; n   cin >> t; n   while (t--) { n     cin >> n; n     for (i = 0; i < n; i++) cout << 1 <<    ; n     cout << endl; n   } n } n


#include<bits/stdc++.h> n #define mp make_pair n #define pr pair<int,int> n #define pb push_back n using namespace std; n int k[100005],all[200005]; n vector<int>a[100005]; n struct nd{ n  int val,id; n  nd(){} n  nd(int val,int id):val(val),id(id){} n  bool operator <(const nd &x)const{ n   return val<x.val; n  } n }; n vector<nd>w[200005]; n int prew[200005]; n int vis[200005]; n int main(){ n  int t; n  scanf( %d ,&t); n  while(t--){ n   int n; n   scanf( %d ,&n); n   for(int i=0;i<n;i++)a[i].clear(); n   for(int i=0;i<n;i++){ n    scanf( %d ,&k[i]); n    a[i].resize(k[i]); n    for(int j=0;j<k[i];j++)scanf( %d ,&a[i][j]); n //   for(int j=0;j<k[i];j++){ n //    int x; n //    scanf( %d ,&x); n //    a[i].pb(x); n //   } n    sort(a[i].begin(),a[i].end()); n   } n   int m=0; n   for(int i=0;i<n;i++)for(int j=0;j<k[i];j++)all[m++]=a[i][j]; n   sort(all,all+m); n   int len=unique(all,all+m)-all; n   for(int i=0;i<len;i++)w[i].clear(); n   for(int i=0;i<n;i++)for(int j=0;j<k[i];j++)a[i][j]=lower_bound(all,all+len,a[i][j])-all; n   int D=sqrt(m)/6; n   int flag=0; n   for(int i=0;i<len;i++)vis[i]=0; n   for(int i=0;i<n;i++){ n    if(k[i]>D){ n     for(int j=0;j<k[i];j++)vis[a[i][j]]=1; n     for(int j=0;j<n;j++)if(j!=i){ n      int cnt=0; n      for(int l=0;l<k[j];l++)if(vis[a[j][l]])cnt++; n      if(cnt>=2){ n       flag=1; n       printf( %d %d n ,i+1,j+1); n       break; n      } n     } n     if(flag)break; n     for(int j=0;j<k[i];j++)vis[a[i][j]]=0; n    } n   }  n   if(flag)continue; n //  for(int i=0;i<n;i++)if(k[i]<=D)for(int j=0;j<k[i];j++)for(int l=j+1;l<k[i];l++)prew[a[i][j]]++; n //  for(int i=0;i<len;i++)w[i].resize(prew[i]); n //  for(int i=0;i<len;i++)prew[i]=0; n //  for(int i=0;i<n;i++)if(k[i]<=D)for(int j=0;j<k[i];j++)for(int l=j+1;l<k[i];l++)w[a[i][j]][prew[a[i][j]]++]=mp(a[i][l],i); n   for(int i=0;i<n;i++)if(k[i]<=D)for(int j=0;j<k[i];j++)for(int l=j+1;l<k[i];l++)w[a[i][j]].pb(nd(a[i][l],i)); n   for(int i=0;i<len;i++)sort(w[i].begin(),w[i].end()); n   for(int i=0;i<len;i++){ n    int l=w[i].size(); n    for(int j=0;j<l-1;j++)if(w[i][j].val==w[i][j+1].val && w[i][j].id!=w[i][j+1].id){ n     flag=1; n     printf( %d %d n ,w[i][j].id+1,w[i][j+1].id+1); n     break; n    }  n    if(flag)break; n   } n   if(!flag)printf( -1 n ); n  } n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 100; n vector<int> v[maxn], w[maxn]; n long long a[maxn]; n bool d[maxn]; n long long c[maxn][5]; n long long n, m; n bool mark[maxn][2]; n void dfs1(long long i) { n   c[i][0] = 1; n   mark[i][0] = 1; n   for (long long y = 0; y < v[i].size(); y++) { n     if (!mark[v[i][y]][0]) { n       dfs1(v[i][y]); n     } n   } n } n void dfs2(long long i) { n   mark[i][1] = 1; n   c[i][1] = 1; n   if (a[i] == 1) return; n   for (long long y = 0; y < w[i].size(); y++) { n     if (!mark[w[i][y]][1]) { n       dfs2(w[i][y]); n     } n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin >> n >> m; n   for (long long y = 1; y <= n; y++) { n     cin >> a[y]; n     if (a[y] == 1) { n       v[100000 + 1].push_back(y); n     } n     if (a[y] == 2) { n       w[100000 + 1].push_back(y); n     } n   } n   while (m--) { n     long long i, j; n     cin >> i >> j; n     v[i].push_back(j); n     w[j].push_back(i); n   } n   dfs1(100000 + 1); n   dfs2(100000 + 1); n   for (long long y = 1; y <= n; y++) { n     cout << (c[y][0] && c[y][1]) << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, p1, p2, tot1, tot2; n long long L, W, ans, a[100005], b[100005]; n int main() { n   scanf( %d%lld%lld , &n, &L, &W); n   long long t; n   int v; n   for (int i = 1; i <= n; ++i) { n     scanf( %lld%d , &t, &v); n     if (v == 1) n       a[++tot1] = t; n     else n       b[++tot2] = t; n   } n   sort(a + 1, a + tot1 + 1), sort(b + 1, b + tot2 + 1); n   for (p1 = p2 = 1; p1 <= tot1; ++p1) { n     while (p2 <= tot2 && (b[p2] - a[p1] + L) * W <= abs(b[p2] + a[p1] + L) && n            (p2++, 1)) n       ; n     ans += tot2 - p2 + 1; n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n const long long N = 1e2 + 10; n vector<long long> g[N]; n void dfs(long long node, long long par, long long dd, vector<long long>& fre) { n   fre[dd]++; n   for (auto x : g[node]) n     if (x != par) dfs(x, node, dd + 1, fre); n } n long long n, k; n long long get(vector<long long>& arr) { n   vector<long long> cur(k + 1, 0); n   cur[0] = 1; n   for (auto x : arr) { n     for (long long i = k; i >= 1; i--) { n       cur[i] += cur[i - 1] * x; n       cur[i] %= MOD; n     } n   } n   return cur[k]; n } n long long solve(long long x) { n   long long sz = g[x].size(); n   vector<vector<long long> > go(sz, vector<long long>(n + 1, 0)); n   for (long long i = 0; i < sz; i++) dfs(g[x][i], x, 1, go[i]); n   long long res = 0; n   for (long long i = 1; i <= n; i++) { n     vector<long long> arr; n     for (auto v : go) n       if (v[i] > 0) arr.push_back(v[i]); n     res += get(arr); n     res %= MOD; n   } n   return res; n } n void solve() { n   cin >> n >> k; n   long long u, v; n   for (long long i = 0; i < n - 1; i++) { n     cin >> u >> v; n     g[u].push_back(v); n     g[v].push_back(u); n   } n   long long res = 0; n   if (k == 2) { n     res = (n * (n - 1)) / 2; n     res %= MOD; n   } else { n     for (long long i = 1; i <= n; i++) { n       res += solve(i); n       res %= MOD; n     } n   } n   cout << res <<   n ; n   for (long long i = 1; i <= n; i++) g[i].clear(); n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long t = 1; n   cin >> t; n   while (t--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T1, typename T2> n inline T1 max(T1 a, T2 b) { n   return a < b ? b : a; n } n template <typename T1, typename T2> n inline T1 min(T1 a, T2 b) { n   return a < b ? a : b; n } n const char lf =   n ; n namespace ae86 { n const int bufl = 1 << 15; n char buf[bufl], *s = buf, *t = buf; n inline int fetch() { n   if (s == t) { n     t = (s = buf) + fread(buf, 1, bufl, stdin); n     if (s == t) return EOF; n   } n   return *s++; n } n inline int ty() { n   int a = 0; n   int b = 1, c = fetch(); n   while (!isdigit(c)) b ^= c ==  - , c = fetch(); n   while (isdigit(c)) a = a * 10 + c - 48, c = fetch(); n   return b ? a : -a; n } n }  // namespace ae86 n using ae86::ty; n const int _ = 123, mo = 1000000007; n int n, m, ps, cnts[_] = {0}; n long long ans = 0; n bitset<_> alls; n inline bitset<_> rshl(bitset<_> a, int d) { n   d %= m; n   return ((a << d) | (a >> (m - d))) & alls; n } n inline bitset<_> rshr(bitset<_> a, int d) { n   d %= m; n   return ((a >> d) | (a << (m - d))) & alls; n } n void dfs(bitset<_> now, int las, long long d) { n   ans = (ans + d) % mo; n   for (int i = las; i + i <= m; i++) n     if (cnts[i] && !now[i] && !now[m - i]) n       dfs(now | rshl(now, i) | rshr(now, i), i + 1, d * cnts[i] % mo); n } n int main() { n   ios::sync_with_stdio(0), cout.tie(nullptr); n   n = ty(), m = ty(), ps = ty(); n   for (int i = 1; i <= ps; i++) { n     int a = ty() % m; n     cnts[min(a, m - a)]++; n   } n   bitset<_> temp = 0; n   temp[0] = 1; n   alls = 0; n   for (int i = 0; i < m; i++) alls[i] = 1; n   dfs(temp, 0, 1), cout << ans << lf; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long price[200002]; n long long a, b, x, y, k; n long long gcd(long long aa, long long bb) { n   return (bb != 0) ? gcd(bb, aa % bb) : aa; n } n bool check(long long mid) { n   long long tt = mid / (a * b / gcd(a, b)); n   long long ta = mid / a; n   long long tb = mid / b; n   long long total = 0; n   for (int i = 1; i <= tt; i++) { n     total += price[i] * (x + y); n   } n   for (int i = tt + 1; i <= ta; i++) { n     total += price[i] * x; n   } n   for (int i = ta + 1; i <= ta + tb - tt; i++) { n     total += price[i] * y; n   } n   if (total >= k) { n     return true; n   } n   return false; n } n int main() { n   int q, n; n   cin >> q; n   while (q--) { n     cin >> n; n     for (int i = 0; i < n; i++) { n       cin >> price[i + 1]; n       price[i + 1] /= 100; n     } n     sort(price + 1, price + n + 1, greater<int>()); n     cin >> x >> a >> y >> b; n     cin >> k; n     if (x < y) { n       swap(x, y); n       swap(a, b); n     } n     int l = 1, r = n, res = -1; n     while (l <= r) { n       int mid = (l + r) >> 1; n       if (check(mid)) { n         res = mid; n         r = mid - 1; n       } else { n         l = mid + 1; n       } n     } n     cout << res << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 998244353; n const long long N = 2005; n long long n, K, ans, sum, sum2, f[N][N], g[N]; n long long kuai(long long a, long long b) { n   if (b == 0) return 1ll; n   if (b == 1) return a; n   long long x = kuai(a, b / 2); n   if (b % 2 == 0) return x * x % mod; n   return x * x % mod * a % mod; n } n signed main() { n   scanf( %lld%lld , &n, &K); n   K--; n   for (long long i = 1; i <= n; i++) { n     sum = 2; n     f[i][1] = 2; n     for (long long j = 2; j <= i; j++) { n       f[i][j] = f[i][j - 1] * 2 % mod; n       sum = (sum + f[i][j]) % mod; n     } n     for (long long j = i + 1; j <= n; j++) { n       f[i][j] = sum; n       sum = ((sum - f[i][j - i] + f[i][j]) % mod + mod) % mod; n     } n   } n   for (long long i = 1; i <= n; i++) { n     long long k = K / i; n     k = min(k, n); n     if (k == 0) continue; n     sum2 = 2; n     g[1] = 2; n     for (long long j = 2; j <= k; j++) { n       g[j] = g[j - 1] * 2 % mod; n       sum2 = (sum2 + g[j]) % mod; n     } n     for (long long j = k + 1; j <= n; j++) { n       g[j] = sum2; n       sum2 = ((sum2 - g[j - k] + g[j]) % mod + mod) % mod; n     } n     ans = (ans + g[n] * (f[i][n] - f[i - 1][n] + mod) % mod) % mod; n   } n   cout << ans * kuai(2, mod - 2) % mod; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string ss[100]; n   int n, ans = 0, cur; n   cin >> n; n   for (int i = 0; i < n; i++) cin >> ss[i]; n   for (char a =  a ; a <=  z ; a++) n     for (char b =  a ; b <=  z ; b++) { n       cur = 0; n       for (int i = 0; i < n; i++) { n         int l = ss[i].length(); n         bool ok = true; n         while (l--) { n           if (ss[i][l] != a && ss[i][l] != b) ok = false; n         } n         if (ok) cur += ss[i].length(); n       } n       ans = max(ans, cur); n     } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using namespace std; n int main() { n   int N; n   cin >> N; n   int suma = 0; n   vector<int> a(N); n   for (int i = 0; i < N; ++i) { n     cin >> a[i]; n     suma += a[i]; n   } n   if (suma == 1) { n     cout << -1 << endl; n   } else { n     long long answer = LONG_LONG_MAX; n     for (int tk = 2; tk <= suma; ++tk) { n       if (suma % tk == 0) { n         int currenti = 0; n         long long cans = 0; n         while (currenti < N) { n           int currentsum = 0; n           int median = -1; n           int starti = currenti; n           while (currentsum < tk) { n             currentsum += a[currenti]; n             if (a[currenti] == 1 && currentsum == (tk + 1) / 2) { n               median = currenti; n             } n             currenti++; n           } n           long long steps = 0; n           for (int i = starti; i < currenti; ++i) { n             if (a[i] == 1) { n               steps += abs(median - i); n             } n           } n           cans += steps; n         } n         answer = min(answer, cans); n       } n     } n     cout << answer << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long DIM = 2e5 + 7; n long long n, used[27][DIM], a[DIM], tests; n int in; n int main() { n   in = scanf( %lld , &tests); n   for (long long test = 1; test <= tests; test++) { n     in = scanf( %lld , &n); n     long long result = 0; n     for (long long i = 1; i <= n; i++) { n       for (long long j = 1; j <= 26; j++) { n         used[j][i] = used[j][i - 1]; n       } n       in = scanf( %lld , &a[i]); n       used[a[i]][i]++; n     } n     for (long long i = 1; i <= 26; i++) result = max(result, used[i][n]); n     long long cntin = 0, cntout = 0; n     for (long long l = 1; l <= n; l++) { n       for (long long r = l; r <= n; r++) { n         cntin = 0; n         cntout = 0; n         for (long long x = 1; x <= 26; x++) { n           cntin = max(cntin, used[x][r] - used[x][l - 1]); n         } n         for (long long x = 1; x <= 26; x++) n           cntout = n               max(cntout, min(used[x][l - 1], used[x][n] - used[x][r]) * 2); n         result = max(result, cntout + cntin); n       } n     } n     printf( %lld n , result); n     for (long long i = 1; i <= n; i++) { n       for (long long j = 1; j <= 26; j++) { n         used[j][i] = 0; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = INT_MAX / 3; n const double EPS = 1e-14; n template <typename U, typename T> n void chmin(U &x, T y) { n   x = min(x, y); n } n template <typename U, typename T> n void chmax(U &x, T y) { n   x = max(x, y); n } n const long long int PRIMEMAX = 1e6; n long long int FACTOR[PRIMEMAX]; n vector<long long int> Primes; n void sieve(long long int upb) { n   FACTOR[0] = FACTOR[1] = -1; n   for (long long int n = 2; n < upb; ++n) { n     if (FACTOR[n] != 0) continue; n     Primes.push_back(n); n     long long int kn = n * n; n     while (kn < PRIMEMAX) { n       FACTOR[kn] = n; n       kn += n; n     } n   } n } n bool is_prime(int n) { n   if (PRIMEMAX > n) { n     return FACTOR[n] == 0; n   } else { n     for (int p : Primes) { n       if (n % p == 0) return false; n     } n     return true; n   } n } n int main() { n   cout << fixed << setprecision(12); n   sieve(PRIMEMAX); n   int n; n   cin >> n; n   vector<int> ps; n   ps.push_back(n % 2 == 0 ? 2 : 3); n   n -= ps[0]; n   if (n == 0) { n     cout << 1 << endl; n     cout << ps[0] << endl; n     return 0; n   } n   if (is_prime(n)) { n     cout << 2 << endl; n     cout << ps[0] <<     << n << endl; n     return 0; n   } n   for (int x : Primes) { n     int y = n - x; n     if (is_prime(y)) { n       ps.push_back(x); n       ps.push_back(y); n       break; n     } n   } n   cout << 3 << endl; n   for (int i = 0; i < (3); i++) { n     if (i > 0) cout <<    ; n     cout << ps[i]; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void Get(int &T) { n   char C; n   bool F = 0; n   for (; C = getchar(), C <  0  || C >  9 ;) n     if (C ==  - ) F = 1; n   for (T = C -  0 ; C = getchar(), C >=  0  && C <=  9 ; T = T * 10 + C -  0 ) n     ; n   F && (T = -T); n } n int N, M; n int X[10005]; n int Y[10005]; n int G[1005][1005]; n int A[10005]; n void Debug() { n   for (int i = 1; i <= N; i++) { n     for (int j = 1; j <= N; j++) printf( %d  , G[i][j]); n     puts(  ); n   } n } n void Init() { n   Get(N); n   Get(M); n   for (int i = 2; i < N; i++) Get(A[i]); n   for (int k = 1; k <= N; k++) { n     Get(X[k]); n     X[k] *= M; n     Get(Y[k]); n     Y[k] *= M; n   } n   for (int i = 1; i <= N; i++) n     for (int j = 1; j <= N; j++) n       if (i != j) n         G[i][j] = std::abs(X[i] - X[j]) + std::abs(Y[i] - Y[j]) - A[i]; n } n int Ans; n void Work() { n   for (int k = 1; k <= N; k++) n     for (int i = 1; i <= N; i++) n       for (int j = 1; j <= N; j++) n         if (G[i][j] > G[i][k] + G[k][j]) G[i][j] = G[i][k] + G[k][j]; n   Ans = G[1][N]; n } n void Output() { printf( %d n , Ans); } n int main() { n   Init(); n   Work(); n   Output(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char f[1111][1111]; n bool black[1111][1111]; n bool is_free(int i, int j) { n   return f[i][j] !=  .  && (f[i][j] <  a  || f[i][j] >  d ); n } n bool can_hor(int i, int j) { n   return (is_free(i, j) && is_free(i, j + 1) && is_free(i, j + 2)) && n          !black[i][j] && black[i][j + 1] && !black[i][j + 2]; n } n bool can_vec(int i, int j) { n   return (is_free(i, j) && is_free(i + 1, j) && is_free(i + 2, j)) && n          !black[i][j] && black[i + 1][j] && !black[i + 2][j]; n } n bool place_hor(int i, int j) { n   if (can_hor(i, j)) { n     char fr = 0; n     for (char cur =  a ; cur <=  d ; cur++) { n       bool can = true; n       for (int kj = j; kj <= j + 2; kj++) { n         if (f[i - 1][kj] == cur || f[i + 1][kj] == cur || f[i][j - 1] == cur || n             f[i][j + 3] == cur) { n           can = false; n           break; n         } n       } n       if (can) { n         fr = cur; n         break; n       } n     } n     if (!fr) return false; n     for (int kj = j; kj <= j + 2; kj++) { n       f[i][kj] = fr; n     } n     return true; n   } n   return false; n } n bool place_vec(int i, int j) { n   if (can_vec(i, j)) { n     char fr = 0; n     for (char cur =  a ; cur <=  d ; cur++) { n       bool can = true; n       for (int ki = i; ki <= i + 2; ki++) { n         if (f[ki][j - 1] == cur || f[ki][j + 1] == cur || f[i - 1][j] == cur || n             f[i + 3][j] == cur) { n           can = false; n           break; n         } n       } n       if (can) { n         fr = cur; n         break; n       } n     } n     if (!fr) return false; n     for (int ki = i; ki <= i + 2; ki++) { n       f[ki][j] = fr; n     } n     return true; n   } n   return false; n } n int main() { n   ios_base::sync_with_stdio(false); n   int n, m; n   cin >> n >> m; n   memset(f,  . , sizeof(f)); n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       cin >> f[i][j]; n       if (f[i][j] ==  b ) n         black[i][j] = 1; n       else if (f[i][j] ==  w ) n         black[i][j] = 0; n       if (f[i][j] !=  . ) f[i][j] =    ; n     } n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       if (!is_free(i, j)) continue; n       if (!place_hor(i, j)) { n         if (!place_vec(i, j)) { n           cout <<  NO ; n           return 0; n         } n       } n     } n   } n   cout <<  YES  << endl; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       cout << f[i][j]; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n int n, a[N], to[N]; n vector<int> idx[N]; n int is[N]; n bool ok() { n   for (int i = 0; i < n; ++i) is[a[i]] ^= 1; n   bool prv = 0; n   for (int i = 1; i <= n; ++i) { n     if (!is[i]) continue; n     if (prv) return 0; n     prv = 1; n   } n   return 1; n } n bool plind() { n   for (int i = 0; i * 2 + 1 < n; ++i) n     if (a[i] != a[n - i - 1]) return 0; n   return 1; n } n bool vis[N]; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) { n     scanf( %d , a + i); n     idx[a[i]].push_back(i); n   } n   if (!ok()) return cout << 0, 0; n   if (plind()) return cout << 1ll * n * (n + 1) / 2, 0; n   int indx = n / 2; n   for (int i = 1; i <= n; ++i) { n     for (int j = 0; j * 2 + 1 < idx[i].size(); ++j) { n       to[idx[i][j]] = idx[i][idx[i].size() - j - 1]; n       to[idx[i][idx[i].size() - j - 1]] = idx[i][j]; n     } n     if (idx[i].size() % 2 == 0) continue; n     indx = idx[i][idx[i].size() / 2]; n     to[idx[i][idx[i].size() / 2]] = n / 2; n   } n   for (int i = 0; i < n; ++i) n     if (to[i] == n - i - 1) vis[i] = 1; n   int prel = 0; n   while (a[prel] == a[n - prel - 1]) ++prel; n   int prer = prel; n   if (n % 2 && indx != n / 2) prer = max(prer, indx); n   if (n % 2 && indx == n / 2) vis[n / 2] = 1; n   for (int i = n - 1; i >= 0; --i) n     if (to[i] >= 0 && !vis[i]) { n       prer = max(prer, to[i]); n       vis[to[i]] = 1; n     } n   long long ans = 1ll * (prel + 1) * (n - prer); n   int sufr = n - 1; n   while (a[sufr] == a[n - sufr - 1]) --sufr; n   int sufl = sufr; n   if (n % 2 && indx != n / 2) sufl = min(sufl, indx); n   memset(vis, 0, sizeof vis); n   if (n % 2 && indx == n / 2) vis[n / 2] = 1; n   for (int i = 0; i < n; ++i) n     if (to[i] == n - i - 1) vis[i] = 1; n   for (int i = 0; i < n; ++i) n     if (to[i] >= 0 && !vis[i]) { n       sufl = min(sufl, to[i]); n       vis[to[i]] = 1; n     } n   if (sufl <= prel) return cout << ans, 0; n   ans += 1ll * (n - sufr) * (sufl - prel); n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3e5 + 10, size = 1 << 20, mod = 998244353, inf = 2e9; n const long long INF = 1e15; n template <class o> n void qr(o& x) { n   char c = getchar(); n   x = 0; n   int f = 1; n   while (!isdigit(c)) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (isdigit(c)) x = x * 10 + c -  0 , c = getchar(); n   x *= f; n } n template <class o> n void qw(o x) { n   if (x / 10) qw(x / 10); n   putchar(x % 10 +  0 ); n } n template <class o> n void pr1(o x) { n   if (x < 0) x = -x, putchar( - ); n   qw(x); n   putchar(   ); n } n template <class o> n void pr2(o x) { n   if (x < 0) x = -x, putchar( - ); n   qw(x); n   putchar(  n ); n } n long long gcd(long long a, long long b) { return !a ? b : gcd(b % a, a); } n long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } n long long power(long long a, long long b = mod - 2, long long p = mod) { n   long long c = 1; n   while (b) { n     if (b & 1) c = c * a % p; n     b /= 2; n     a = a * a % p; n   } n   return c; n } n template <class o> n void cmax(o& x, o y) { n   if (x < y) x = y; n } n void cmax(int& x, int y) { n   x = x - y >> 31 ? y : x; n   ; n } n template <class o> n void cmin(o& x, o y) { n   if (x > y) x = y; n } n void cmin(int& x, int y) { x = x - y >> 31 ? x : y; } n template <typename t1, typename t2> n void ad(t1& x, t2 y) { n   x += y; n   if (x >= mod) x -= mod; n } n template <typename t1, typename t2> n void dl(t1& x, t2 y) { n   x -= y; n   if (x < 0) x += mod; n } n int n, m; n int mx[N << 2]; n void change(int x, int l, int r, int pos, int d) { n   if (l == r) { n     mx[x] = d; n     return; n   } n   if (pos <= ((l + r) >> 1)) n     change((x << 1), l, ((l + r) >> 1), pos, d); n   else n     change((x << 1 | 1), ((l + r) >> 1) + 1, r, pos, d); n   cmax(mx[x] = mx[(x << 1)], mx[(x << 1 | 1)]); n } n int ask(int x, int l, int r, int L, int R) { n   if (L <= l && r <= R) return mx[x]; n   int s = 0; n   if (L <= ((l + r) >> 1)) cmax(s, ask((x << 1), l, ((l + r) >> 1), L, R)); n   if (((l + r) >> 1) < R) n     cmax(s, ask((x << 1 | 1), ((l + r) >> 1) + 1, r, L, R)); n   return s; n } n int trie[N][27], tot = 1, pos[N]; n vector<int> name[N]; n void insert(char* s, int now) { n   int p = 1; n   for (int i = 1; s[i]; i++) { n     int c = s[i] -  a ; n     if (!trie[p][c]) trie[p][c] = ++tot; n     p = trie[p][c]; n   } n   name[p].push_back(now); n   pos[now] = p; n } n int fail[N]; n void add(int x, int y); n void bfs() { n   static int q[N], l, r; n   q[l = r = 1] = 1; n   for (int i = 0; i <= 25; i++) trie[0][i] = 1; n   while (l <= r) { n     int p = q[l++], x, y; n     for (int c = 0; c <= 25; c++) { n       x = trie[p][c]; n       if (!x) { n         trie[p][c] = trie[fail[p]][c]; n         continue; n       } n       y = fail[p]; n       if (p == 1) n         fail[x] = 1; n       else n         fail[x] = trie[fail[p]][c]; n       q[++r] = x; n       add(fail[x], x); n     } n   } n } n int fa[N], dep[N], son[N], sz[N], top[N], dfn[N], id; n vector<int> e[N]; n void add(int x, int y) { e[x].push_back(y); } n void dfs1(int x) { n   sz[x] = ((int)name[x].size()); n   son[x] = 0; n   for (int y : e[x]) { n     fa[y] = x; n     dep[y] = dep[x] + 1; n     dfs1(y); n     sz[x] += sz[y]; n     if (sz[y] > sz[son[x]]) son[x] = y; n   } n } n int st[N], ed[N]; n void dfs2(int x, int tp) { n   top[x] = tp; n   st[x] = id + 1; n   for (int y : name[x]) dfn[y] = ++id; n   ed[x] = id; n   if (son[x]) dfs2(son[x], tp); n   for (int y : e[x]) n     if (y ^ son[x]) dfs2(y, y); n } n int Max(int x) { n   int ans = -1; n   while (x) { n     if (st[top[x]] <= ed[x]) cmax(ans, ask(1, 1, n, st[top[x]], ed[x])); n     x = fa[top[x]]; n   } n   return ans; n } n void solve() { n   qr(n); n   qr(m); n   static char s[N]; n   for (int i = 1; i <= n; i++) { n     scanf( %s , s + 1); n     insert(s, i); n   } n   bfs(); n   dfs1(1); n   dfs2(1, 1); n   while (m--) { n     int op, i, x; n     qr(op); n     if (op == 1) { n       qr(i); n       qr(x); n       change(1, 1, n, dfn[i], x); n     } else { n       scanf( %s , s + 1); n       int ans = -1; n       for (int i = 1, p = 1; s[i]; i++) { n         int c = s[i] -  a ; n         p = trie[p][c]; n         cmax(ans, Max(p)); n       } n       pr2(ans); n     } n   } n } n int main() { n   int T = 1; n   while (T--) solve(); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const int maxn = 5e3 + 10; n long long a[maxn], pre[maxn]; n long long dp[maxn][maxn]; n int main() { n   int n, m, k; n   while (~scanf( %d%d%d , &n, &m, &k)) { n     for (int i = 1; i < n + 1; i++) { n       scanf( %lld , &a[i]); n       pre[i] = pre[i - 1] + a[i]; n     } n     for (int i = 1; i <= n; ++i) n       for (int j = 1; j <= i / m; ++j) n         dp[i][j] = max(dp[i - 1][j], dp[i - m][j - 1] + pre[i] - pre[i - m]); n     cout << dp[n][k] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char op[20]; n char s[205]; n int main() { n   int n, m, k; n   scanf( %d%d%d , &n, &m, &k); n   getchar(); n   cin.getline(op, 15); n   int dir; n   if (op[3] ==  h ) { n     dir = 0; n   } else { n     dir = 1; n   } n   scanf( %s , s); n   int dir2 = m < k ? 0 : 1; n   int len = strlen(s); n   bool flag = false; n   int t = 0; n   for (int i = 0; i < len; ++i) { n     ++t; n     int lastk = k; n     if (dir == 0) { n       --k; n       if (k == 0) k = 2, dir = 1; n     } else { n       ++k; n       if (k == n + 1) k = n - 1, dir = 0; n     } n     if (s[i] ==  0 ) { n       if (dir == 0) { n         if (m > lastk && m < n) n           ++m; n         else if (m < lastk && m > 1) n           --m; n       } else { n         if (m < lastk && m > 1) n           --m; n         else if (m > lastk && m < n) n           ++m; n       } n     } else if (i != len - 1) { n       if (dir == 0) n         m = n; n       else n         m = 1; n     } n     if (m == k && i != len - 1) { n       flag = true; n       break; n     } n   } n   if (!flag) { n     puts( Stowaway ); n   } else { n     printf( Controller %d n , t); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n const int Inf = 1e9 + 5; n int main() { n   cin.tie(0); n   ios_base::sync_with_stdio(0); n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     vector<int> a(n); n     ll sum = 0; n     for (int i = 0; i < n; i++) { n       cin >> a[i]; n       sum += a[i]; n     } n     ll mx = 0, s = 0; n     for (int i = 1; i < n; i++) { n       s += a[i]; n       if (s < 0) s = 0; n       mx = max(mx, s); n     } n     bool f1 = 0; n     if (sum > mx) { n       f1 = 1; n     } n     mx = 0; n     s = 0; n     for (int i = 0; i < n - 1; i++) { n       s += a[i]; n       if (s < 0) s = 0; n       mx = max(mx, s); n     } n     if (sum > mx && f1) { n       cout <<  YES  << endl; n     } else { n       cout <<  NO  << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n constexpr int kN = int(2E5 + 10), kInf = int(1E9 + 10), kC = int(1E6 + 10); n struct seg_tree { n   long long int val[kN << 2], flag[kN << 2]; n   void init() { n     memset(val, 0, sizeof(val)); n     memset(flag, 0, sizeof(flag)); n     return; n   } n   void addtag(int n, long long int x) { n     flag[n] += x; n     val[n] += x; n   } n   void push(int n) { n     if (flag[n]) { n       addtag(n << 1, flag[n]); n       addtag(n << 1 | 1, flag[n]); n       flag[n] = 0; n     } n     return; n   } n   void pull(int n) { n     val[n] = max(val[n << 1], val[n << 1 | 1]); n     return; n   } n   void fix(int n, int l, int r, int L, int R, long long int x) { n     if (L <= l && r <= R) n       addtag(n, x); n     else if (!(l > R || L > r)) { n       int mid = (l + r) >> 1; n       push(n); n       fix(n << 1, l, mid, L, R, x); n       fix(n << 1 | 1, mid + 1, r, L, R, x); n       pull(n); n     } n     return; n   } n }; n pair<int, int> wep[kN], eqi[kN]; n pair<pair<int, int>, int> mon[kN]; n int pos[kC]; n seg_tree sg; n int main() { n   int n, m, p, now = 1; n   long long int ans; n   scanf( %d%d%d , &n, &m, &p); n   for (int i = 1; i <= n; i++) scanf( %d%d , &wep[i].first, &wep[i].second); n   for (int i = 1; i <= m; i++) scanf( %d%d , &eqi[i].first, &eqi[i].second); n   for (int i = 1; i <= p; i++) n     scanf( %d%d%d , &mon[i].first.first, &mon[i].first.second, &mon[i].second); n   sort(wep + 1, wep + n + 1); n   sort(eqi + 1, eqi + m + 1); n   sort(mon + 1, mon + p + 1); n   sg.init(); n   for (int i = 1; i <= m; i++) sg.fix(1, 1, m, i, i, -eqi[i].second); n   for (int i = 0; i < kC; i++) { n     while (now <= m) { n       if (eqi[now].first <= i) n         now++; n       else n         break; n     } n     pos[i] = now; n   } n   ans = -wep[1].second - eqi[1].second; n   now = 1; n   for (int i = 1; i <= n; i++) { n     while (now <= p) { n       if (mon[now].first.first < wep[i].first) { n         if (pos[mon[now].first.second] <= m) n           sg.fix(1, 1, m, pos[mon[now].first.second], m, mon[now].second); n         now++; n       } else n         break; n     } n     ans = max(ans, sg.val[1] - wep[i].second); n   } n   printf( %lld n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[25], d[25][25], f[1 << 25], ans = INT_MAX; n int popcnt(int x) { n   int cnt = 0; n   while (x > 0) cnt += (x & 1), x >>= 1; n   return cnt; n } n int main() { n   cin >> n; n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n     for (int j = 0; j < i; ++j) d[i][j] = -1; n     for (int j = 0; j < i; ++j) n       for (int k = j; k < i; ++k) n         if (a[j] + a[k] == a[i]) d[i][j] = k, d[i][k] = j; n   } n   f[1] = true; n   for (int i = 1; i < n; ++i) n     for (int j = 1 << (i - 1); j < (1 << i); ++j) n       if (f[j] == true) n         for (int k = 0; k < i; ++k) n           if (j & (1 << k) && d[i][k] >= 0 && (j & (1 << d[i][k]))) { n             for (int l = 0; l <= i; ++l) f[(j & (~(1 << l))) | (1 << i)] = true; n             break; n           } n   for (int i = 1 << (n - 1); i < (1 << n); ++i) n     if (f[i] == true) ans = min(ans, popcnt(i)); n   if (ans == INT_MAX) n     cout << -1; n   else n     cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f; n const double pi = 3.1415926535897932384626; n inline long long read() { n   long long x = 0, f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = (x << 1) + (x << 3) + (ch ^ 48); n     ch = getchar(); n   } n   return x * f; n } n const int maxn = 1e2 + 5; n int t, n; n char s1[maxn], s2[maxn]; n int main() { n   t = read(); n   while (t--) { n     n = read(); n     scanf( %s%s , s1, s2); n     int minv = inf; n     for (int i = 0; i < n; ++i) { n       int k = i; n       for (int j = 0; j < n; ++j) { n         if (k < n && s1[j] == s2[k]) ++k; n         minv = min(minv, n - k + i); n       } n     } n     sort(s1, s1 + n); n     sort(s2, s2 + n); n     if (strcmp(s1, s2)) n       printf( -1 n ); n     else n       printf( %d n , minv); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int ppl, s = 0, maxn = 0; n   cin >> ppl; n   int wel[ppl]; n   for (int j = 0; j < ppl; j++) { n     cin >> wel[j]; n     if (maxn < wel[j]) { n       maxn = wel[j]; n     } n   } n   for (int i = 0; i < ppl; i++) { n     s = s + maxn - wel[i]; n   } n   cout << s; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 10, size = 1 << 20; n template <class o> n void qr(o &x) { n   char c = getchar(); n   x = 0; n   int f = 1; n   while (!isdigit(c)) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (isdigit(c)) x = x * 10 + c -  0 , c = getchar(); n   x *= f; n } n template <class o> n void qw(o x) { n   if (x / 10) qw(x / 10); n   putchar(x % 10 +  0 ); n } n template <class o> n void pr1(o x) { n   if (x < 0) x = -x, putchar( - ); n   qw(x); n   putchar(   ); n } n template <class o> n void pr2(o x) { n   if (x < 0) x = -x, putchar( - ); n   qw(x); n   puts(  ); n } n int T, n, last, tot, f[N], b[N], c[N]; n char s[N]; n long long ans; n struct node { n   int fa, len, v[26]; n } tr[N]; n void add(int c) { n   int p = last, x = last = ++tot; n   tr[x].len = tr[p].len + 1; n   for (; p && tr[p].v[c] == 0; p = tr[p].fa) tr[p].v[c] = x; n   if (!p) n     tr[x].fa = 1; n   else { n     int q = tr[p].v[c], y; n     if (tr[p].len + 1 == tr[q].len) n       tr[x].fa = q; n     else { n       tr[y = ++tot] = tr[q]; n       tr[y].len = tr[p].len + 1; n       tr[q].fa = tr[x].fa = y; n       for (; p && tr[p].v[c] == q; p = tr[p].fa) tr[p].v[c] = y; n     } n   } n } n void init() { n   memset(tr + 1, 0, tot * 28 << 2); n   memset(f + 1, 0, tot << 2); n   memset(c, 0, n + 1 << 2); n   last = tot = 1; n   ans = n = 0; n } n int main() { n   qr(T); n   while (T--) { n     init(); n     scanf( %s , s + 1); n     for (int i = 1; s[i]; i++) n++, add(s[i] -=  a ), f[last]++; n     for (int i = 1; i <= tot; i++) c[tr[i].len]++; n     for (int i = 1; i <= n; i++) c[i] += c[i - 1]; n     for (int i = tot; i; i--) b[c[tr[i].len]--] = i; n     for (int i = tot, j; i; i--) { n       j = b[i]; n       f[tr[j].fa] += f[j]; n       ans += (long long)f[j] * f[j] * (tr[j].len - tr[tr[j].fa].len); n     } n     pr2(ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n int H[10]; n int main() { n   memset(H, 0, sizeof(H)); n   for (int _n = 6, i = 0; i < _n; i++) { n     int v; n     scanf( %d , &v); n     H[v]++; n   } n   bool n6, n4, n5, n2; n   n6 = n4 = n5 = n2 = false; n   for (int _n = 10, i = 0; i < _n; i++) n     if (H[i] == 4) n       n4 = true; n     else if (H[i] == 6) n       n6 = true; n     else if (H[i] == 5) n       n5 = true; n     else if (H[i] == 2) n       n2 = true; n   if (n6 || (n4 && n2)) n     printf( Elephant n ); n   else if ((n4 && !n2) || n5) n     printf( Bear n ); n   else n     printf( Alien n ); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   int i; n   map<string, string> mp1to2; n   map<string, string> mp2to1; n   map<string, int> mp1p; n   map<string, int> mp2p; n   for (i = 1; i <= m; i++) { n     string w1, w2; n     cin >> w1 >> w2; n     mp1to2[w1] = w2; n     mp2to1[w2] = w1; n     mp1p[w1]++; n     mp2p[w2]++; n   } n   for (i = 1; i <= n; i++) { n     string word; n     cin >> word; n     if (mp1p[word] > 0) { n       int len1 = word.length(); n       int len2 = mp1to2[word].length(); n       if (len1 <= len2) { n         printf( %s  , word.c_str()); n       } else { n         printf( %s  , mp1to2[word].c_str()); n       } n     } else { n       int len1 = mp2to1[word].length(); n       int len2 = word.length(); n       if (len1 <= len2) { n         printf( %s  , mp2to1[word].c_str()); n       } else { n         printf( %s  , word.c_str()); n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long fact(long long n) { n   if (n == 0 || n == 1) n     return 1; n   else n     return n * fact(n - 1); n } n long long gcd(long long a, long long b) { n   if (a < 0) a = -a; n   if (b < 0) b = -b; n   if (a == 1 || b == 1) n     return 1; n   else if (a == 0) n     return b; n   else if (b == 0) n     return a; n   else { n     long long r = a > b ? a % b : b % a; n     if (r == 0) n       return min(a, b); n     else n       return gcd(min(a, b), r); n   } n } n bool Comparator(int a, int b) { return (a >= b); } n long long M = 1e9 + 7; n long long solve() { n   int n; n   cin >> n; n   vector<int> inf(n, 0), tt(n, 0), ans(n, 0); n   for (long long i = 1; i < n; i++) { n     cin >> inf[i]; n     inf[i]--; n   } n   tt[1] = 1; n   for (long long i = 2; i < n; i++) { n     tt[i] = tt[inf[i]] + 1; n   } n   for (long long i = 0; i < n; i++) { n     ans[tt[i]]++; n   } n   long long sum = 0; n   for (long long i = 0; i < n; i++) { n     sum += ans[i] % 2; n   } n   return sum; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t = 1; n   while (t--) { n     cout << solve() <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n, x = 0; n   char arra[10]; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %s , arra); n     if (arra[0] ==  +  || arra[1] ==  + ) n       x = x + 1; n     else if (arra[0] ==  -  || arra[1] ==  - ) n       x = x - 1; n   } n   printf( %d n , x); n } n


#include <bits/stdc++.h> n using namespace std; n int n, k; n vector<pair<long long, long long> > nums; n int main() { n   cin >> n; n   nums.resize(n); n   for (int i = 0; i < n; i++) { n     long long a, b; n     cin >> a >> b; n     nums[i] = make_pair(b, a); n   } n   sort(nums.begin(), nums.end()); n   long long res = 0; n   long long left; n   long long fac = 1; n   long long last_left = 0; n   cin >> k; n   vector<long long> ks(k + 1); n   for (int i = 0; i < k; i++) cin >> ks[i]; n   ks[k] = -1; n   long long k_cnt = 0; n   left = ks[0]; n   for (int i = 0; i < n; i++) { n     if (left == -1) { n       res += fac * nums[i].second * nums[i].first; n       continue; n     } n     if (left <= nums[i].second) { n       res += fac * left * nums[i].first; n       nums[i].second -= left; n       i--; n       k_cnt++; n       if (ks[k_cnt] == -1) n         left = -1; n       else n         left = (ks[k_cnt] - ks[k_cnt - 1]); n       fac++; n     } else { n       res += fac * nums[i].second * nums[i].first; n       left -= nums[i].second; n       nums[i].second = 0; n     } n   } n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 100; n int arr[maxn]; n int cum[maxn], n, m, k; n int dp[2020]; n map<int, int> offer; n int range(int l, int r) { return (cum[r] - cum[l]); } n int solve(int i) { n   if (i == k) return 0; n   int &ret = dp[i]; n   if (ret != -1) return ret; n   ret = 1e9; n   for (int j = i + 1; j <= k; j++) { n     int buy = (j - i); n     int of = offer[buy]; n     int taka = range(i, j); n     int offtaka = range(i, i + of); n     ret = min(ret, taka - offtaka + solve(j)); n   } n   return ret; n } n int main() { n   scanf( %d%d%d , &n, &m, &k); n   memset(dp, -1, sizeof dp); n   for (int i = 1; i <= n; i++) scanf( %d , &arr[i]); n   sort(arr + 1, arr + n + 1); n   for (int i = 1; i <= m; i++) { n     int xi, yi; n     scanf( %d%d , &xi, &yi); n     offer[xi] = max(offer[xi], yi); n   } n   for (int i = 1; i <= k; i++) cum[i] = cum[i - 1] + arr[i]; n   cout << solve(0) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n FILE *in = stdin; n class node { n  public: n   long long sum[5], hnode; n   node() { n     sum[0] = sum[1] = sum[2] = sum[3] = sum[4] = hnode = 0; n     return; n   } n   node(int x) { n     for (int q = 0; q < 5; q++) { n       sum[q] = 0; n     } n     sum[0] = x; n     hnode = 1; n     return; n   } n }; n node merge(node a, node b) { n   node c; n   c.hnode = a.hnode + b.hnode; n   int left = a.hnode % 5; n   for (int q = 0; q < 5; q++) { n     c.sum[q] = a.sum[q] + b.sum[(5 - left + q) % 5]; n   } n   return c; n } n class segment_tree { n  public: n   node tree[4 * 100000]; n   segment_tree() { n     for (int q = 0; q < 4 * 100000; q++) { n       tree[q] = node(); n     } n     return; n   } n   void add(int i, int l, int r, int x, int v, int typ) { n     if (l == r) { n       if (typ == 0) { n         tree[i] = node(v); n       } else { n         tree[i] = node(); n       } n       return; n     } n     int mid = (l + r) >> 1; n     if (x <= mid) n       add(i * 2, l, mid, x, v, typ); n     else n       add(i * 2 + 1, mid + 1, r, x, v, typ); n     tree[i] = merge(tree[i * 2], tree[i * 2 + 1]); n     return; n   } n   long long sum() { return tree[1].sum[2]; } n }; n int N, a[100000][2]; n set<int> heap; n segment_tree seg; n map<int, int> ind; n int main() { n   fscanf(in,  %d , &N); n   for (int q = 0; q < N; q++) { n     char qq[22]; n     int x; n     fscanf(in,  %s , qq); n     if (qq[0] ==  s ) { n       a[q][0] = -1; n     } else { n       fscanf(in,  %d , &x); n       if (qq[0] ==  a ) n         a[q][0] = 0; n       else n         a[q][0] = 1; n       a[q][1] = x; n       heap.insert(x); n     } n   } n   int cind = 0; n   for (set<int>::iterator it = heap.begin(); it != heap.end(); it++) { n     ind[*it] = cind; n     cind++; n   } n   for (int q = 0; q < N; q++) { n     if (a[q][0] == -1) { n       printf( %I64d n , seg.sum()); n     } else { n       seg.add(1, 0, cind - 1, ind[a[q][1]], a[q][1], a[q][0]); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct point { n   double x, y; n   point(double _x = 0, double _y = 0) { n     x = _x; n     y = _y; n   } n   point operator+(point w) { return point(x + w.x, y + w.y); } n   point operator-(point w) { return point(x - w.x, y - w.y); } n   point operator*(double H) { return point(x * H, y * H); } n   double operator/(point w) { n     if (abs(w.x) > 1E-7) return x / w.x; n     return y / w.y; n   } n   bool operator==(point w) { n     return (abs(x - w.x) < 1E-7 && abs(y - w.y) < 1E-7); n   } n }; n double sp(point a, point b) { return (a.x * b.x + a.y * b.y); } n int ORI(point a, point b) { n   double O = a.x * b.y - a.y * b.x; n   return (abs(O) < 1E-7) ? 0 : (O > 0 ? 1 : -1); n } n struct prava { n   double a, b, c; n   prava(point f = point(0, 0), point t = point(1, 1)) { n     a = t.y - f.y; n     b = f.x - t.x; n     c = t.x * f.y - f.x * t.y; n   } n   point IP(prava w) { n     point ret; n     if (abs(a) < 1E-7) { n       ret.y = (-c) / b; n       ret.x = (-w.b * ret.y - w.c) / w.a; n     } else { n       ret.y = (w.a * c - a * w.c) / (a * w.b - w.a * b); n       ret.x = (-b * ret.y - c) / a; n     } n     return ret; n   } n }; n point BEG; n double X1, Y1, Z1, X2, Y2, Z2, D; n int n; n point M[10000]; n point L1, L2; n bool is_in(point w) { n   w = w - BEG; n   if (ORI(L1, L2) == 0) { n     if (!(L1 == point(0, 0)) && ORI(L1, w) == 0 && (w / L1 >= 0)) return true; n     if (!(L2 == point(0, 0)) && ORI(L2, w) == 0 && (w / L2 >= 0)) return true; n     return false; n   } n   return (ORI(L1, L2) * ORI(L1, w) >= 0 && ORI(L2, L1) * ORI(L2, w) >= 0); n } n double t1 = 1E9, t2 = 1E9; n void get_time(point w) { n   if (ORI(L1, L2) != 0) { n     prava B(BEG, BEG + L1); n     double T1, T2, T3; n     point WIND(X2, Y2); n     T3 = (B.a * w.x + B.b * w.y + B.c) / (B.a * WIND.x + B.b * WIND.y); n     if (T3 < 0 && abs(T3) < 1E-7) T3 = 0; n     if (T3 < 0) return; n     T1 = point(w - WIND * T3 - BEG) / point(X1, Y1); n     if (T1 < 0 && abs(T1) < 1E-7) T1 = 0; n     if (T1 < 0) return; n     T2 = (T1 * Z1 + T3 * Z2) / (-D); n     if (T2 < 0 && abs(T2) < 1E-7) T2 = 0; n     if (T2 < 0) return; n     if (t1 > T1 || (abs(t1 - T1) < 1E-7 && t2 > T2)) { n       t1 = T1; n       t2 = T2; n     } n   } else { n     double T1, T2, T3; n     if (!(point(X1, Y1) == point(0, 0)) && point(X2, Y2) / point(X1, Y1) < 0) { n       T1 = (w - BEG) / point(X1, Y1); n       T2 = (-Z1 * T1) / D; n       if (T1 < 0 && abs(T1) < 1E-7) T1 = 0; n       if (T2 < 0 && abs(T2) < 1E-7) T2 = 0; n       if (T1 < 0 || T2 < 0) goto NEXT_TYPE; n       if (t1 > T1 || (abs(t1 - T1) < 1E-7 && t2 > T2)) { n         t1 = T1; n         t2 = T2; n       } n     NEXT_TYPE: n       point SUM = point(X1, Y1) + point(X2, Y2) * (-Z1 / Z2); n       if (SUM == point(0, 0)) return; n       T1 = (w - BEG) / SUM; n       T2 = 0; n       if (T1 < 0 && abs(T1) < 1E-7) T1 = 0; n       if (T1 < 0 || T2 < 0) return; n       if (t1 > T1 || (abs(t1 - T1) < 1E-7 && t2 > T2)) { n         t1 = T1; n         t2 = T2; n       } n     } else { n       point SUM = point(X1, Y1) + point(X2, Y2) * (-Z1 / Z2); n       if (SUM == point(0, 0)) return; n       T1 = (w - BEG) / SUM; n       T2 = 0; n       if (T1 < 0 && abs(T1) < 1E-7) T1 = 0; n       if (T1 < 0 || T2 < 0) return; n       if (t1 > T1 || (abs(t1 - T1) < 1E-7 && t2 > T2)) { n         t1 = T1; n         t2 = T2; n       } n     } n   } n } n int main() { n   int i, j, k, l; n   scanf( %d , &n); n   for (i = 0; i < n; ++i) scanf( %lf%lf , &M[i].x, &M[i].y); n   scanf( %lf%lf , &BEG.x, &BEG.y); n   scanf( %lf%lf%lf%lf%lf%lf%lf , &X1, &Y1, &Z1, &D, &X2, &Y2, &Z2); n   L1 = point(X1, Y1); n   L2 = L1 - point(X2, Y2) * (Z1 / Z2); n   for (i = 0; i < n; ++i) n     if (is_in(M[i])) get_time(M[i]); n   for (i = 0; i < n; ++i) { n     j = i + 1; n     if (j >= n) j -= n; n     if (ORI(L1, M[i] - BEG) * ORI(L1, M[j] - BEG) < 0) { n       point T = prava(BEG, BEG + L1).IP(prava(M[i], M[j])); n       if (is_in(T)) get_time(T); n     } n     if (ORI(L2, M[i] - BEG) * ORI(L2, M[j] - BEG) < 0) { n       point T = prava(BEG, BEG + L2).IP(prava(M[i], M[j])); n       if (is_in(T)) get_time(T); n     } n   } n   if (t1 < 1E9) n     printf( %.12lf %.12lf n , t1, t2); n   else n     printf( -1 -1 n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, nrh, nrt, nod1, nod2, d[100006], x, nr1, nr2, nra; n vector<int> G[100006]; n vector<int> sters; n int uz[100006], uz2[100006]; n int main() { n   int i, j, k; n   cin >> n >> m >> nrh >> nrt; n   for (i = 1; i <= m; i++) { n     cin >> nod1 >> nod2; n     G[nod1].push_back(nod2); n     G[nod2].push_back(nod1); n     d[nod1]++; n     d[nod2]++; n   } n   for (i = 1; i <= n; i++) n     if (d[i] > nrh) uz[i] = 1; n   for (i = 1; i <= n; i++) { n     if (d[i] > nrt) { n       for (j = 0; j < G[i].size(); j++) { n         if (uz[G[i][j]]) { n           nra = nr1 = nr2 = 0; n           x = G[i][j]; n           for (k = 0; k < G[i].size(); k++) { n             if (k != j) { n               uz2[G[i][k]]++; n               nr1++; n               sters.push_back(G[i][k]); n             } n           } n           for (k = 0; k < G[x].size(); k++) { n             if (G[x][k] != i) { n               uz2[G[x][k]]++; n               if (uz2[G[x][k]] == 2) n                 nra++; n               else n                 sters.push_back(G[x][k]); n               nr2++; n             } n           } n           if (nr1 + nr2 - nra < nrt + nrh) { n             for (k = 0; k < sters.size(); k++) uz2[sters[k]] = 0; n             sters.clear(); n             continue; n           } n           cout <<  YES n ; n           cout << G[i][j] <<     << i <<   n ; n           for (k = 0; k < G[x].size() && nrh; k++) { n             if (uz2[G[x][k]] == 1) { n               cout << G[x][k] <<    ; n               nrh--; n             } n           } n           for (k = 0; nrh; k++) { n             if (uz2[G[x][k]] == 2) { n               uz2[G[x][k]] = 0; n               cout << G[x][k] <<    ; n               nrh--; n             } n           } n           cout <<   n ; n           for (k = 0; k < G[i].size() && nrt; k++) { n             if (uz2[G[i][k]]) { n               cout << G[i][k] <<    ; n               nrt--; n             } n           } n           return 0; n         } n       } n     } n   } n   cout <<  NO n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int i, j; n int main() { n   int n, s, d, flag, last; n   cin >> n; n   cin >> s >> d; n   flag = s; n   n--; n   while (n--) { n     cin >> s >> d; n     while (flag >= s) s += d; n     flag = s; n   } n   cout << flag; 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   cout.tie(0); n   int q; n   cin >> q; n   unordered_map<string, string> gr; n   set<string> ending; n   for (int i = 0; i < q; i++) { n     string prev, next; n     cin >> prev >> next; n     gr[prev] = next; n     ending.insert(next); n   } n   vector<pair<string, string>> ans; n   for (auto it : gr) { n     if (ending.count(it.first)) continue; n     string cur = it.first; n     while (gr.count(cur)) { n       cur = gr[cur]; n     } n     ans.push_back({it.first, cur}); n   } n   cout << ans.size() <<   n ; n   for (auto it : ans) cout << it.first <<     << it.second <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 510; n const int MAXS = 110; n const int MAXM = 1E5 + 10; n const double EPS = 1E-8; n int siz, n, m, k, ban[MAXN], pos[MAXN], U[MAXM], V[MAXM], d[MAXN], id[MAXN]; n vector<int> G[MAXN]; n struct Mat { n   double a[MAXS][MAXS]; n   Mat() { memset(a, 0, sizeof a); } n   double* operator[](int x) { return a[x]; } n   const double* operator[](int x) const { return a[x]; } n   Mat operator*(const Mat& rhs) const { n     Mat ret; n     for (int i = 0; i <= siz; i++) n       for (int j = 0; j <= siz; j++) n         for (int k = 0; k <= siz; k++) ret[i][j] += a[i][k] * rhs[k][j]; n     return ret; n   } n } trans; n Mat ksm(Mat a, long long b) { n   Mat ret; n   for (int i = 0; i <= siz; i++) ret[i][i] = 1; n   for (; b; b >>= 1, a = a * a) n     if (b & 1) ret = ret * a; n   return ret; n } n double A[MAXN][MAXN << 1], C[MAXN], X[MAXN]; n void Guass() { n   for (int i = 1; i <= n; i++) { n     int pos = i; n     for (int j = i + 1; j <= n; j++) n       if (abs(A[j][i]) > abs(A[pos][i])) pos = j; n     if (abs(A[pos][i]) < EPS) throw  No Matrix Inverse ; n     for (int j = 1; j <= 2 * n; j++) swap(A[pos][j], A[i][j]); n     for (int j = i + 1; j <= n; j++) { n       if (abs(A[j][i]) < EPS) continue; n       double coef = A[j][i] / A[i][i]; n       for (int k = 1; k <= 2 * n; k++) A[j][k] -= A[i][k] * coef; n     } n   } n   for (int i = n; i; i--) { n     double inv = 1.0 / A[i][i]; n     for (int j = 1; j <= 2 * n; j++) A[i][j] = A[i][j] * inv; n     for (int j = i - 1; j; j--) { n       double coef = A[j][i] / A[i][i]; n       for (int k = 1; k <= 2 * n; k++) A[j][k] -= A[i][k] * coef; n     } n   } n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) swap(A[i][j], A[i][j + n]); n } n void solve() { n   for (int i = 1; i <= n; i++) X[i] = 0; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) X[j] += C[i] * A[j][i]; n } n int main() { n   scanf( %d%d%d , &n, &m, &k); n   pos[++siz] = 1; n   id[1] = 1; n   for (int i = 1; i <= n; i++) { n     scanf( %d , &ban[i]); n     if (ban[i]) pos[++siz] = i, id[i] = siz; n   } n   for (int i = 1; i <= n; i++) A[i][i] = A[i][i + n] = 1; n   for (int i = 1; i <= m; i++) { n     scanf( %d%d , &U[i], &V[i]); n     G[U[i]].push_back(V[i]); n     G[V[i]].push_back(U[i]); n     ++d[U[i]]; n     ++d[V[i]]; n   } n   for (int i = 1; i <= m; i++) { n     int u = U[i], v = V[i]; n     if (!ban[u]) A[v][u] -= 1.0 / d[u]; n     if (!ban[v]) A[u][v] -= 1.0 / d[v]; n   } n   Guass(); n   for (int i = 1; i <= siz; i++) { n     memset(C, 0, sizeof C); n     int x = pos[i]; n     if (x == 1) n       C[1] = 1, solve(); n     else { n       C[x] = 1; n       for (int v : G[x]) C[v] += 1.0 / d[x]; n       solve(); n     } n     double tot = 1; n     for (int j = 1; j <= n; j++) n       if (ban[j] && j != x) trans[i][id[j]] = X[j], tot -= X[j]; n     trans[i][i] = tot; n   } n   trans[siz][0] = 1; n   Mat res; n   res[0][1] = 1; n   res = res * ksm(trans, k); n   printf( %.10lf n , res[0][0]); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 805; n const int INF = 0x3f3f3f3f; n int n, m, k, lim; n struct Edge { n   int from, to, flow, cap; n   Edge(int f, int t, int fl, int c) : from(f), to(t), flow(fl), cap(c) {} n }; n struct EK { n   int n; n   vector<Edge> E; n   vector<int> g[N]; n   int p[N], df[N]; n   bool vis[N]; n   void init(int n) { n     this->n = n; n     for (int i = 1; i <= n; i++) g[i].clear(); n     E.clear(); n   } n   void addEdge(int u, int v, int c) { n     E.push_back(Edge(u, v, 0, c)); n     E.push_back(Edge(v, u, 0, 0)); n     int sz = E.size(); n     g[u].push_back(sz - 2); n     g[v].push_back(sz - 1); n   } n   int bfs(int S, int T) { n     memset(df, 0, sizeof(df)); n     memset(vis, 0, sizeof(vis)); n     queue<int> Q; n     df[S] = 0x3f3f3f3f, vis[S] = 1; n     Q.push(S); n     while (!Q.empty()) { n       int u = Q.front(); n       Q.pop(); n       for (int i = 0; i < ((int)g[u].size()); i++) { n         Edge &e = E[g[u][i]]; n         if (!vis[e.to] && e.cap > e.flow) { n           p[e.to] = g[u][i]; n           vis[e.to] = 1; n           df[e.to] = min(df[e.from], e.cap - e.flow); n           Q.push(e.to); n         } n       } n       if (vis[T]) break; n     } n     for (int u = T; u != S; u = E[p[u]].from) { n       E[p[u]].flow += df[T]; n       E[p[u] ^ 1].flow -= df[T]; n     } n     return df[T]; n   } n   int maxFlow(int S, int T) { n     int F = 0; n     while (1) { n       int D = bfs(S, T); n       if (!D) break; n       F += D; n     } n     return F; n   } n } T; n bool done[N]; n vector<pair<int, int> > g[N]; n int dis[N][N], st[N]; n void dij(int s) { n   memset(dis[s], 0x3f, sizeof(dis[s])); n   memset(done, 0, sizeof(done)); n   priority_queue<pair<int, int>, vector<pair<int, int> >, n                  greater<pair<int, int> > > n       Q; n   Q.push(make_pair(0, s)); n   dis[s][s] = 0; n   while (!Q.empty()) { n     int u = Q.top().second; n     Q.pop(); n     if (done[u]) continue; n     done[u] = 1; n     for (int i = 0; i < ((int)g[u].size()); i++) { n       int v = g[u][i].first, w = g[u][i].second; n       if (dis[s][v] > dis[s][u] + w) { n         dis[s][v] = dis[s][u] + w; n         Q.push(make_pair(dis[s][v], v)); n       } n     } n   } n } n int tot; n bool check(int x) { n   T.init(n + tot + 2); n   int cur = 0; n   for (int i = 1; i <= n; i++) { n     if (st[i]) { n       T.addEdge(n + tot + 1, ++cur, st[i]); n       for (int j = 1; j <= n; j++) n         if (dis[i][j] <= x) { n           T.addEdge(cur, j + tot, 1); n         } n     } n     T.addEdge(i + tot, n + tot + 2, 1); n   } n   return T.maxFlow(n + tot + 1, n + tot + 2) >= lim; n } n int main() { n   scanf( %d%d%d%d , &n, &m, &k, &lim); n   for (int i = 0; i < k; i++) { n     int x; n     scanf( %d , &x); n     st[x]++; n   } n   for (int i = 1; i <= n; i++) n     if (st[i]) tot++; n   while (m--) { n     int u, v, w; n     scanf( %d%d%d , &u, &v, &w); n     g[u].push_back(make_pair(v, w)); n     g[v].push_back(make_pair(u, w)); n   } n   for (int i = 1; i <= n; i++) dij(i); n   int l = 0, r = 1731312; n   while (l + 1 < r) { n     int mid = (l + r) / 2; n     if (check(mid)) n       r = mid; n     else n       l = mid; n   } n   int ans = check(l) ? l : r; n   printf( %d n , ans > 1731311 ? -1 : ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int n, k; n   cin >> n >> k; n   long long a1[n], b1[n]; n   for (int i = 0; i < n; i++) cin >> a1[i] >> b1[i]; n   long long dp[n][k]; n   vector<long long> a, b; n   vector<pair<pair<long long, long long>, long long> > e; n   vector<long long> ans; n   long long mx = 0; n   for (int p = 0; p < n; p++) { n     a.clear(); n     b.clear(); n     e.clear(); n     for (int i = 0; i < n; i++) n       if (i == p) n         continue; n       else n         e.push_back(make_pair(make_pair(b1[i], a1[i]), i)); n     sort(e.begin(), e.end()); n     a.push_back(0); n     b.push_back(0); n     for (int i = 0; i < e.size(); i++) { n       a.push_back(e[i].first.second); n       b.push_back(e[i].first.first); n     } n     for (int i = 0; i < n; i++) n       for (int j = 0; j < k; j++) dp[i][j] = -1e9; n     dp[0][0] = 0; n     for (int i = 1; i < n; i++) n       for (int j = 0; j < k; j++) { n         dp[i][j] = max(dp[i][j], dp[i - 1][j] + b[i] * (k - 1)); n         if (j > 0) n           dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + b[i] * (j - 1) + a[i]); n       } n     if (mx < a1[p] + b1[p] * (k - 1) + dp[n - 1][k - 1]) { n       mx = a1[p] + b1[p] * (k - 1) + dp[n - 1][k - 1]; n       vector<long long> x, bad; n       int i = n - 1, j = k - 1; n       while (dp[i][j] > 0) { n         if (j > 0) { n           if (dp[i - 1][j - 1] + b[i] * (j - 1) + a[i] == dp[i][j]) { n             x.push_back(e[i - 1].second + 1); n             i--; n             j--; n           } else { n             bad.push_back(-e[i - 1].second - 1); n             bad.push_back(e[i - 1].second + 1); n             i--; n           } n         } else { n           bad.push_back(-e[i - 1].second - 1); n           bad.push_back(e[i - 1].second + 1); n           i--; n         } n       } n       reverse(x.begin(), x.end()); n       reverse(bad.begin(), bad.end()); n       for (int i = 0; i < bad.size(); i++) x.push_back(bad[i]); n       x.push_back(p + 1); n       ans = x; n     } n   } n   cout << ans.size() <<   n ; n   for (int i = 0; i < ans.size(); i++) cout << ans[i] <<    ; n   cout <<   n ; n } n int main() { n   ios::sync_with_stdio(0); n   int q; n   cin >> q; n   while (q--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long int> v; n long long int a[120100], n, dd; n map<int, int> mp; n int main() { n   for (long long int i = 1; i <= 2000000000; i *= 2) { n     v.push_back(i); n   } n   cin >> n; n   for (int i = 0; i < n; i++) { n     scanf( %lld , &a[i]); n     mp[a[i]]++; n   } n   sort(a, a + n); n   int f, res = 0; n   for (int i = 0; i < n; i++) { n     f = 0; n     for (int j = 0; j < v.size(); j++) { n       if (v[j] <= a[i]) continue; n       dd = v[j] - a[i]; n       if (dd == a[i]) { n         if (mp[a[i]] >= 2) { n           f = 1; n           break; n         } n         continue; n       } n       if (mp[dd] >= 1) { n         f = 1; n         break; n       } n     } n     if (f == 0) res++; n   } n   cout << res << endl; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   long long a, b, c; n } e[100005]; n int cmp(node a, node b) { return a.c > b.c; } n int main() { n   int n, f, i; n   cin >> n >> f; n   for (i = 1; i <= n; i++) { n     scanf( %lld%lld , &e[i].a, &e[i].b); n     if (e[i].a >= e[i].b) { n       e[i].c = 0; n     } else { n       e[i].c = min(e[i].a * 2, e[i].b) - e[i].a; n     } n   } n   sort(e + 1, e + 1 + n, cmp); n   long long ans = 0; n   for (i = 1; i <= f; i++) { n     ans += min(2 * e[i].a, e[i].b); n   } n   for (; i <= n; i++) { n     ans += min(e[i].a, e[i].b); n   } n   printf( %lld , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MaxN = 100010; n struct adjList { n   int id, nxt; n } Lst[MaxN * 2]; n int LCnt, Head[MaxN]; n int N, w[MaxN], root; n void addEdge(int a, int b) { n   Lst[LCnt].id = b; n   Lst[LCnt].nxt = Head[a]; n   Head[a] = LCnt++; n } n pair<long long, long long> dfs(int id, int pre) { n   vector<pair<long long, long long> > v; n   for (int x = Head[id]; x != -1; x = Lst[x].nxt) { n     if (Lst[x].id != pre) { n       v.push_back(dfs(Lst[x].id, id)); n     } n   } n   sort(v.begin(), v.end()); n   long long sum = 1, rem = 0; n   w[id]--; n   for (int i = (int)(v.size()) - 1; i >= 0; i--) { n     if (w[id] == 0) break; n     w[id]--; n     sum += v[i].first + 1; n     rem += v[i].second; n   } n   if (w[id]) { n     sum += 2 * min((long long)w[id], rem); n     w[id] -= min((long long)w[id], rem); n   } n   return make_pair(sum, w[id]); n } n int main() { n   while (cin >> N) { n     memset(Head, -1, sizeof(Head)); n     LCnt = 0; n     for (int i = 1; i <= N; i++) { n       scanf( %d , w + i); n     } n     for (int i = 1; i < N; i++) { n       int a, b; n       scanf( %d %d , &a, &b); n       addEdge(a, b); n       addEdge(b, a); n     } n     cin >> root; n     w[root]++; n     cout << dfs(root, -1).first - 1 << endl; n     ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1000000000; n map<int, int> mp, mp2; n map<int, int>::iterator it; n int n, m; n int tmp[3] = {2, 3, 5}, flag, t; n void bfs(int val, int cnt) { n   queue<int> q; n   mp[val] = cnt; n   q.push(val); n   while (!q.empty()) { n     int x = q.front(); n     q.pop(); n     for (int i = 0; i < 3; ++i) { n       if (x % tmp[i] == 0 && x != 0) { n         if (mp.count(x / tmp[i]) == 0) { n           mp[x / tmp[i]] = mp[x] + 1; n           q.push(x / tmp[i]); n         } n       } n     } n   } n } n void bfs1(int val, int cnt) { n   if (mp.count(val) != 0) { n     flag = mp[val]; n     return; n   } n   queue<int> q; n   mp2[val] = cnt; n   q.push(val); n   while (!q.empty() && flag == -1) { n     int x = q.front(); n     q.pop(); n     for (int i = 0; i < 3 && flag == -1; ++i) { n       if (x % tmp[i] == 0 && x != 0 && flag == -1) { n         if (mp.count(x / tmp[i]) != 0 && flag == -1) { n           flag = mp[x / tmp[i]] + mp2[x] + 1; n           return; n         } n         if (mp2.count(x / tmp[i]) == 0) { n           mp2[x / tmp[i]] = mp2[x] + 1; n           q.push(x / tmp[i]); n         } n       } n     } n   } n } n int main() { n   while (cin >> n >> m) { n     if (n == m) { n       cout <<  0 n ; n       continue; n     } n     mp.clear(); n     mp2.clear(); n     t = 0; n     bfs(n, 0); n     flag = -1; n     bfs1(m, 0); n     cout << flag << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long n, m, k; n   cin >> n >> m >> k; n   if (n > m) swap(n, m); n   set<long long> vals; n   for (long long i = 1; i <= n; ++i) { n     if (vals.count(n / i)) { n       for (long long j = n / i; j >= 1; --j) { n         vals.insert(j); n       } n       break; n     } n     vals.insert(n / i); n   } n   long long res = -1; n   int diff = 2; n   vals.insert(n); n   vals.insert(m); n   for (long long x : vals) { n     for (long long i = n / x - diff; i <= n / x + diff; ++i) { n       if (i < 0 || i >= n) continue; n       long long j = k - i; n       if (j < 0 || j >= m) continue; n       res = max(res, 1LL * (n / (i + 1)) * (m / (j + 1))); n     } n     for (long long i = x - diff; i <= x + diff; ++i) { n       if (i < 0 || i >= n) continue; n       long long j = k - i; n       if (j < 0 || j >= m) continue; n       res = max(res, 1LL * (n / (i + 1)) * (m / (j + 1))); n     } n   } n   if (k < n) res = max(res, 1LL * m * (n / (k + 1))); n   if (k < m) res = max(res, 1LL * n * (m / (k + 1))); n   cout << res <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 5; n const int Mod = 1000000007; n const int INF = 0x3f3f3f3f; n const long long LL_INF = 0x3f3f3f3f3f3f3f3f; n const double e = exp(1); n const double PI = acos(-1); n const double ERR = 1e-10; n int main() { n   long long n; n   scanf( %lld , &n); n   long long ans = (1LL * (1 + n) * n) / 2; n   if (ans % 2 == 0) n     printf( 0 n ); n   else n     printf( 1 n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 500; n const int M = 1e6 + 500; n const int INF = 0x3f3f3f3f; n const int MOD = 1e9 + 7; n const int BASE = 31337; n const int LOG = 18; n const int OFF = (1 << LOG); n const double EPS = 1e-9; n const double PI = 3.1415926535; n int blok[M], n, bio[M], pref[M], lft[M], q, k; n int get(int l, int r) { n   if (l <= 0) return pref[r]; n   return pref[r] - pref[l - 1]; n } n int check(int x) { n   int sol = 0; n   for (int i = 0; i < n; i++) { n     if (blok[i]) { n       if (lft[i] <= i - x) { n         return -1; n       } else { n         i = lft[i] + x - 1; n         sol++; n       } n     } else { n       i += x - 1; n       sol++; n     } n   } n   return sol; n } n int main() { n   scanf( %d%d%d , &n, &q, &k); n   for (int i = 0; i < q; i++) { n     int x; n     scanf( %d , &x); n     blok[x] = 1; n   } n   if (blok[0]) n     lft[0] = -INF; n   else n     lft[0] = 0; n   for (int i = 1; i < n; i++) { n     if (!blok[i]) n       lft[i] = i; n     else n       lft[i] = lft[i - 1]; n   } n   long long fin = (long long)INF * INF; n   for (int i = 1; i <= k; i++) { n     int x; n     scanf( %d , &x); n     int y = check(i); n     if (y == -1) continue; n     fin = min(fin, (long long)x * y); n   } n   if (fin == (long long)INF * INF) n     printf( -1 n ); n   else n     printf( %I64d n , fin); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long o = 101; n const long long p = 6e4; n struct qwq { n   long long x, y; n } a[o], b[o]; n long long n, m, l, r, f[p + 1], ans = -1; n bool cmp1(qwq u, qwq v) { return u.x == v.x ? u.y > v.y : u.x < v.x; } n bool cmp2(qwq u, qwq v) { return u.x + u.y > v.x + v.y; } n signed main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> m; n   for (long long i = 1, x, y; i <= n; i++) { n     cin >> x >> y; n     if (y >= 0) n       a[++l] = (qwq){x, y}; n     else n       b[++r] = (qwq){x, y}; n   } n   memset(f, -1, sizeof(f)), f[m] = 0; n   sort(a + 1, a + l + 1, cmp1), sort(b + 1, b + r + 1, cmp2); n   for (long long i = 1; i <= l; i++) n     for (long long j = p; j >= a[i].x; j--) n       if (f[j] != -1) f[j + a[i].y] = max(f[j] + 1, f[j + a[i].y]); n   for (long long i = 1; i <= r; i++) n     for (long long j = max(b[i].x, -b[i].y); j <= p; j++) n       if (f[j] != -1) f[j + b[i].y] = max(f[j] + 1, f[j + b[i].y]); n   for (long long i = 0; i <= p; i++) ans = max(f[i], ans); n   cout << ans; n   return 0; n } n