#include <iostream>
#include <cmath>
using namespace std;

const int INF = -100;
int k, r, c;
int a[8] = { INF, INF, INF, INF, INF, INF, INF, INF };
bool b[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };

bool check() {
	for (int i = 0; i < 7; i++) {
		for (int j = i + 1; j < 8; j++)
			if (a[i] == a[j] || j - i == abs(a[i] - a[j]))
				return false;
	}
	return true;
}

void set(int i) {
	if (i == 8) {
		if (check()) {
			for (int i = 0; i < 8; i++) {
				for (int j = 0; j < 8; j++) {
					if (a[i] == j)
						cout << "Q";
					else
						cout << ".";
				}
				cout << endl;
			}
		}
		return;
	}

	if (b[i])
		set(i + 1);
	else {
		for (int j = 0; j < 8; j++) {
			a[i] = j;
			set(i + 1);
		}
	}
}

int main()
{
	cin >> k;

	for (int i = 0; i < k; i++) {
		cin >> r >> c;
		a[r] = c;
		b[r] = 1;
	}

	set(0);

	return 0;
}

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>

#define mp       make_pair
#define pb       push_back
#define all(x)   (x).begin(),(x).end()
#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

typedef    long long          ll;
typedef    unsigned long long ull;
typedef    vector<bool>       vb;
typedef    vector<int>        vi;
typedef    vector<vb>         vvb;
typedef    vector<vi>         vvi;
typedef    pair<int,int>      pii;

const int INF=1<<29;
const double EPS=1e-9;

const int dx[]={1,0,-1,0},dy[]={0,-1,0,1};

bool ok(int x, int y) {
    if (x < 0 || x >= 8 || y < 0 || y >= 8) return false;
    return true;
}

bool check(int nowmap[8][8]) {
    int count = 0;
    for (int i = 0; i < 8; i++) {
        for (int j = 0; j < 8; j++) {
            if (nowmap[i][j] == 1) {
                count++;
            }
        }
    }
    if (count >= 9) {
        return false;
    }
    for (int i = 0; i < 8; i++) {
        for (int j = 0; j < 8; j++) {
            if (nowmap[i][j] == 1) {
                int dx[] = {1,1,-1,-1};
                int dy[] = {1,-1,-1,1};
                for (int k = 0; k < 4; k++) {
                    int px = j;
                    int py = i;
                    while(ok(px + dx[k], py + dy[k])) {
                        px += dx[k];
                        py += dy[k];
                        if (nowmap[py][px] == 1) {
                            return false;
                        }
                    }
                }
            }
        }
    }
    return true;
}

void print(int nowmap[8][8]) {
    for (int i = 0; i < 8; i++) {
        for (int j = 0; j < 8; j++) {
            if (nowmap[i][j] == 1) {
                cout << "Q";
            } else {
                cout << ".";
            }
        }
        cout << endl;
    }
}

int main(int argc, char const *argv[]) {
    int N;
    cin >> N;
    int map[8][8] = {{0}};
    for (int i = 0; i < N; i++) {
        int a, b;
        cin >> a >> b;
        map[a][b] = 1;
    }

    std::vector<int> perm;
    for (int i = 0; i < 8; i++) {
        perm.push_back(i);
    }

    do {
        int nowmap[8][8] = {{0}};
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                nowmap[i][j] = map[i][j];
            }
        }
        for (int i = 0; i < 8; i++) {
            int x = i;
            int y = perm[i];

            nowmap[y][x] = 1;

        }
        if (check(nowmap)) {
            print(nowmap);
            return 0;
        }
    } while(next_permutation(perm.begin(), perm.end()));

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL, LL> PII;

// chmax, chmin
template <class T>
inline bool chmax(T &a, T b)
{
  if (a < b)
  {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
inline bool chmin(T &a, T b)
{
  if (a > b)
  {
    a = b;
    return 1;
  }
  return 0;
}

template <typename T>
function<T(T, T)> op_max = [](T a, T b) -> T { return max(a, b); };
template <typename T>
function<T(T, T)> op_min = [](T a, T b) -> T { return min(a, b); };

template <typename T>
function<T(T, T)> op_sum = [](T a, T b) -> T { return a + b; };

using namespace std;
//********************  dumps ************************//

template <typename T>
void dump(const T &data, vector<int> &iter)
{
  cout << data << " ";
}
template <typename T>
void dump(const vector<T> &data, vector<int> &iter)
{
  for (auto elem : data)
    dump(elem, iter);
  cout << endl;
}

template <typename T>
void dump(const vector<vector<T>> &data, vector<int> &iter)
{
  for (auto elem : iter)
  {
    cout << "[" << elem << "]";
  }
  cout << endl;
  for (int i = 0; i < data.size(); i++)
  {
    iter.push_back(i);
    dump(data[i], iter);
    iter.pop_back();
  }
  cout << endl;
}

template <typename T>
void dump(const vector<T> &data, int dummy)
{
  for (int i = 0; i < data.size(); i++)
  {
    cout << "[" << i << "] " << data[i] << endl;
  }
}
template <typename T>
void dump(const T &data)
{ // T : data, U = base type val.
  vector<int> iter;
  dump(data, iter);
}
///////////////////////////////////////////////

void solve(long long K, std::string S)
{

  int n = S.size();
  if (K < n)
  {
    S = S.substr(0, K);
    S += "...";
  }

  cout << S << endl;
}

int main()
{
  cout << setprecision(10);
  long long K;
  scanf("%lld", &K);
  std::string S;
  std::cin >> S;
  solve(K, S);
  return 0;
}

#include<iostream>
#include<string>
using namespace std;
int main(){
  int k;
  cin>>k;
  string s,S;
  cin>>s;
  if(s.length()<=k)
    cout<<s;
  else{
    for(int i=0;i<k;i++){
    	cout<<s[i];
	}
  cout<<"...";
}
}


#include <bits/stdc++.h>
#define ll long long
#define N 1000100
ll n,s,ans,x[N],p[N];
using namespace std;
int main(){
    scanf("%lld%lld",&n,&s);
    for (ll i=1;i<=n;i++) scanf("%lld%lld",&x[i],&p[i]);
    for (ll l=1,r=n;l<=r;){
        if (x[l]>=s){ans+=x[r]-s; break;}//所有人都在一边直接开过去
        if (x[r]<=s){ans+=s-x[l]; break;}
        ans+=x[r]-x[l]; //每次来回开一趟
        if (p[l]>=p[r])
            while (l<r && p[l]>=p[r]) p[l]+=p[r],r--;//相同往左开
        else while (l<r && p[l]<p[r]) p[r]+=p[l],l++;
    }
    printf("%lld\n",ans);
    return 0;
}

#include <cstdio>

using namespace std;
typedef long long int LL;
const int Max_N(100050);

namespace io
{
	const int SIZE = (1 << 21) + 1;
	char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c, qu[55]; int f, qr;
	#define gc() (iS == iT ? (iT = (iS = ibuf) + fread (ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++)
	inline void flush (){fwrite (obuf, 1, oS - obuf, stdout);oS = obuf;}
	inline void putc (char x){*oS ++ = x;if (oS == oT) flush ();}
	template <class I>
	inline void gi (I &x) {for (f = 1, c = gc(); c < '0' || c > '9'; c = gc()) if (c == '-') f = -1;
		for (x = 0; c <= '9' && c >= '0'; c = gc()) x = x * 10 + (c & 15); x *= f;}
	template <class I>
	inline void print (I x){
		if (!x) putc ('0'); if (x < 0) putc ('-'), x = -x;while(x) qu[++ qr] = x % 10 + '0',  x /= 10;while (qr) putc (qu[qr--]);}
	struct Flusher_ {~Flusher_(){flush();}}io_flusher_;
}
using io::gi;
using io::putc;
using io::print;

int N, S, X[Max_N], V[Max_N];
LL P[Max_N], Ans;

void solve(int l, int r)
{
	if (l == r)
	{
		V[++V[0]] = X[l], V[++V[0]] = S;
		return;
	}
	if (S < X[l])
	{
		V[++V[0]] = X[r], solve(l, r - 1);
		return;
	}
	if (S > X[r])
	{
		V[++V[0]] = X[l], solve(l + 1, r);
		return;
	}
	if (P[l] >= P[r])
		V[++V[0]] = X[r], P[l] += P[r], solve(l, r - 1);
	else
		V[++V[0]] = X[l], P[r] += P[l], solve(l + 1, r);
}

constexpr int Abs(int x)
{
	return x >= 0 ? x : -x;
}

int main()
{
	gi(N), gi(S);
	for (int i = 1;i <= N;++i)
		gi(X[i]), gi(P[i]);
	solve(1, N);
	for (int i = V[0] - 1;i >= 1;--i)
		Ans += Abs(V[i + 1] - V[i]);
	print(Ans);
	return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <math.h>
#include <cassert>
#define rep(i,n) for(int i = 0; i < n; ++i )
using namespace std;
using ll = long long;
using P = pair<int,int>;

int main() {
  int n,q;
  cin >> n >> q;

  set<P> ds;
  rep(i,n){
    int s,t,x;
    cin >> s >> t >> x;
    ds.emplace(s-x,x);
    ds.emplace(t-x,-x);
  }
  set<int> s; auto itr = ds.begin();
  rep(i,q){
    int d;
    cin >> d;
    while(itr!=ds.end() && itr->first<=d){
      if(itr->second>0) s.insert(itr->second);
      else s.erase(-itr->second);
      ++itr;
    }
    cout << (s.empty() ? -1 : *s.begin()) << endl;
  }
}

#include<iostream>
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define IO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
int n, q, inf=2e9, cnt;
vector<int> par ;
vector<pair<int,pair<int,int> > > v;
map<int,int> id ;
int tree[3000006];
void update(int node, int s, int e, int l, int r, int val)
{
    if(s>r||e<l)
        return ;
    if(s>=l && e<=r)
    {
        tree[node]=min(tree[node],val);
        return ;
    }
    int m=(s+e)/2;
    update(2*node,s,m,l,r,val);
    update(2*node+1,m+1,e,l,r,val);
}
int solve(int node, int s,int e, int pos)
{
    if(s==e)
        return tree[node];
    int m=(s+e)/2;
    tree[2*node]=min(tree[node],tree[2*node]);
    tree[2*node+1]=min(tree[node],tree[2*node+1]);
    return pos<=m ? solve(2*node,s,m,pos) : solve(2*node+1,m+1,e,pos);
}
int main()
{
    IO
    cin>>n>>q;
    for(int i=1; i<=3e6; i++)
        tree[i]=inf;
    for(int i=1; i<=n; i++)
    {
        int s, t, x;
        cin>>s>>t>>x;
        v.push_back({x,{s-x,t-x-1}});
        id[s-x]++;
        id[t-x-1]++;
    }
    for(int i=1; i<=q; i++)
    {
        int d;
        cin>>d;
        id[d]++;
        par.push_back(d);
    }
    for(auto i : id)
        id[i.first]=++cnt;
    for(int i=0; i<n; i++)
    {
        int l=id[v[i].second.first], r=id[v[i].second.second];
        update(1,1,cnt,l,r,v[i].first);
    }
    for(int i=0; i<q; i++)
    {
        int ans=solve(1,1,cnt,id[par[i]]);
        if(ans==inf)
            ans=-1;
        cout<<ans<<"\n";
    }
}


#include <stdio.h>
int main(){
		long long int s,max,min,k;
		int n;
		scanf("%d",&n);
		max = -1000000;
		min = 1000000;
		s = 0;
		for (int i=0; i<n ; ++i){
			scanf("%lld",&k);
				if(k>max) max = k;
				if(k<min) min = k;
				s =s+k;
		};
		printf("%lld %lld %lld\n",min ,max, s);
	    return 0;
}

#include<iostream>
#include<cstdio>
using namespace std;
int main(){
	long long Min,Max,sum=0,n,m;
	scanf("%lld%lld",&n,&Max);
	Min=sum=Max;
	for(int i=1;i<n;i++){
		scanf("%lld",&m);
		if(m>Max) Max=m;
		if(m<Min) Min=m;
		sum+=m;
	}
	printf("%lld %lld %lld\n",Min,Max,sum);
	return 0;
}


#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define P pair<int,int>
using ll=int64_t;
using namespace std;
#define ketasuu(n) fixed<<setprecision(n)
#define btoe(p) p.begin(),p.end()
#define etob(p) p.rbegin(),p.rend()

int main(){
    int n,m,x,y;
    cin>>n>>m>>x>>y;
    vector<int> p(n), q(m);
    rep(i,n) cin>>p[i];
    rep(i,m) cin>>q[i];
    p.push_back(x); q.push_back(y);
    sort(etob(p)); sort(btoe(q));
    int a=p[0], b=q[0];
    if(a<b) cout<<"No War"<<endl;
    else cout<<"War"<<endl;
}

#include <bits/stdc++.h>
#define rep(i,x,n) for(int i=x; i<(int)(n); i++)
using namespace std;

bool hantei( vector<int>& x, vector<int>& y ) {
  rep( Z, -100, 100+1 ) {
    bool flag = true;
    rep( i, 0, x.size() ) {
      if ( x.at(i) >= Z ) flag = false;
    }

    rep( i, 0, y.size() ) {
      if ( y.at(i) < Z ) flag = false;
    }
    if (flag) return false;
  }

  return true;
}

int main() {
  int N, M;
  cin >> N >> M;

  vector<int> x(N+1), y(M+1);
  cin >> x.at(0) >> y.at(0);
  rep(i, 1, N+1) cin >> x.at(i);
  rep(i, 1, M+1) cin >> y.at(i);

  cout << ( hantei(x, y) ? "War" : "No War" ) << endl;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair< ll, ll > Pi;
#define rep(i,n) for(int i=0;i<(n);i++)
#define rep2(i,n) for(int i=1;i<=(n);i++)
#define rep3(i,i0,n) for(int i=i0;i<(n);i++)
#define pb push_back
#define mod 1000000007
const ll INF = 1LL << 60;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
ll gcd(ll a, ll b) {return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b) {return a/gcd(a,b)*b;}
#define all(x) x.begin(), x.end()
#define mp make_pair
bool compare(Pi a, Pi b) {
    if(a.first != b.first){
        return a.first < b.first;

    }else{
        return a.second < b.second;
    }
}

bool In_map(ll y,ll x,ll h,ll w){
    if(y<0 || x<0 || y>=h || x>=w){
        return 0;
    }else{
        return 1;
    }
}
const vector<ll> dx{1,0,-1,0};
const vector<ll> dy{0,1,0,-1};

int main() {
    ll N,Q;
    string S;
    cin >>N>>Q>>S;
    vector<ll> l(Q);
    vector<ll> r(Q);
    rep(i,Q){
        cin>>l[i]>>r[i];
    }

    vector<ll> sum(N);
    sum[0]=0;
    rep(i,N-1){
        if(S[i]=='A' && S[i+1]=='C'){
            sum[i+1] = sum[i]+1;
        }else{
            sum[i+1] = sum[i];
        }

    }

    rep(i,Q){
        cout<<sum[r[i]-1]-sum[l[i]-1]<<endl;;
    }

    return 0;

}

#include <bits/stdc++.h>
#include <limits>
#include <math.h>
#include <cmath>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int> ;
using vi = vector<int> ;
using vvi = vector<vector<int>> ;
using vll = vector<long long>;
using vvll = vector<vector<long long >>;
#define PI 3.141592653
#define rep(i, n) for(int i = 0; i < n; i++)

int main(){
  int n; cin >> n;
  string s; cin >> s;
  bool fin;
  if(s[0] == 'o'){fin = true;}
  else{fin = false;}
  bool ans = true;

  vector<char> s1(n+1);
  s1[0] = 'W'; s1[1] = 'S';
  vector<char> s2(n+1);
  s2[0] = 'W'; s2[1] = 'W';
  vector<char> s3(n+1);
  s3[0] = 'S'; s3[1] = 'S';
  vector<char> s4(n+1);
  s4[0] = 'S'; s4[1] = 'W';

  while(true){
  bool f;
  for(int i = 2; i <= n; i++){
    f = true;
    if(s1[i-2] == 'W'){f = !f;}
    if(s1[i-1] == 'W'){f = !f;}
    if(s[i-1] == 'x'){f = !f;}
    if(f){s1[i] = 'S';}
    else{s1[i] = 'W';}
  }
  if(fin){if(s1[n-1] == 'W' && s1[0] == s1[n])  {rep(i, n){cout << s1[i];}ans = false; break;}}
  else{if(s1[n-1] == 'S' && s1[0] == s1[n])  {rep(i, n){cout << s1[i];}ans = false; break;}}
  for(int i = 2; i <= n; i++){
    f = true;
    if(s2[i-2] == 'W'){f = !f;}
    if(s2[i-1] == 'W'){f = !f;}
    if(s[i-1] == 'x'){f = !f;}
    if(f){s2[i] = 'S';}
    else{s2[i] = 'W';}
  }
  if(fin){if(s2[n-1] == 'S' && s2[0] == s2[n] ) {rep(i, n){cout << s2[i];}ans = false; break;}}
  else{if(s2[n-1] == 'W' && s2[0] == s2[n])  {rep(i, n){cout << s2[i];}ans = false; break;}}

  for(int i = 2; i <= n; i++){
    f = true;
    if(s3[i-2] == 'W'){f = !f;}
    if(s3[i-1] == 'W'){f = !f;}
    if(s[i-1] == 'x'){f = !f;}
    if(f){s3[i] = 'S';}
    else{s3[i] = 'W';}
  }
  if(fin){if(s3[n-1] == 'S' && s3[0] == s3[n]) {rep(i, n){cout << s3[i];} ans = false; break;}}
  else{if(s3[n-1] == 'W' && s3[0] == s3[n])  {rep(i, n){cout << s3[i];}ans = false;  break;}}

  for(int i = 2; i <= n; i++){
    f = true;
    if(s4[i-2] == 'W'){f = !f;}
    if(s4[i-1] == 'W'){f = !f;}
    if(s[i-1] == 'x'){f = !f;}
    if(f){s4[i] = 'S';}
    else{s4[i] = 'W';}
  }
  if(fin){if(s4[n-1] == 'W'  && s4[0] == s4[n])  {rep(i, n){cout << s4[i];} ans = false;break;}}
  else{if(s4[n-1] == 'S' && s4[0] == s4[n]) { rep(i, n){cout << s4[i];}ans = false; break;}}

  cout << -1;
  break;
  }
  cout << endl;
}


#pragma GCC optimize ("O3")
#include <iostream>
#include <iomanip>
#include <istream>
#include <ostream>
#include <sstream>
#include <iterator>
#include <vector>
#include <algorithm>
#include <queue>
#include <deque>
#include <list>
#include <stack>
#include <map>
#include <unordered_map>
#include <set>
#include <bitset>
#include <utility>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <string>
#include <ctime>
#include <cctype>
#include <cstdlib>
#include <numeric>
#define IINF 1000000000
#define INF 3223372036854775807
#define MOD 1000000007
#define mod 998244353
#define INT_MAX_ 2147483647
#define EPS (1e-10)
#define REP(i, a, n) fo-r (ll i = a; i < (ll)(n); i++)
#define REPE(i, a, n) for (ll i = a; i <= (ll)(n); i++)
//#define rep(i,n)for (ll i = 0; i < (ll)(n); i++)
#define rep(i,l,r)for(ll i=(l);i<(r);i++)
#define rep1(i,n) for(int i=1;i<=(int)(n);i++)
#define Endl endl
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define eb emplace_back
#define mmax(x,y)(x>y?x:y)
#define mmin(x,y)(x<y?x:y)
#define chmax(x,y) x=mmax(x,y)
#define chmin(x,y) x=mmin(x,y)
#define all(x) (x).begin(),(x).end()
#define siz(x) (ll)(x).size()
#define PI acos(-1.0)
#define me memset
#define bit(n,k) ((n>>k)&1)
#define lg length()
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;
typedef pair<int,int>Pin;
typedef pair<ll,ll>Pll;
template<class T> using V=vector<T>;
template<typename T> using min_priority_queue = priority_queue<T, vector<T>, greater<T> >;
long long GCD(long long a, long long b) {return b?GCD(b,a%b):a;}
long long LCM(long long a, long long b) {return a/GCD(a,b)*b;}
ll pom(ll a,ll n,int m){ll x=1;for(a%=m;n;n/=2)n&1?x=x*a%m:0,a=a*a%m;return x;}
#define invp(a,p)pom(a,p-2,p)
int dx[4]={-1,0,1,0};
int dy[4]={0,-1,0,1};
int ddx[8]={-1,0,1,0,1,1,-1,-1};
int ddy[8]={0,-1,0,1,1,-1,1,-1};
ll cmp1(pair<Pll,ll> a,pair<Pll,ll> b){
        return a.fi.se>b.fi.se;
}
ll cmp2(pair<ll,ll> a,pair<ll,ll> b){
    if(a.se!=b.se)
        return a.se<b.se;
    else
        return a.fi<b.fi;
}
//----------------------------------------------------------------------
bool dp[80][80][2*160*80+1];
//----------------------------------------------------------------------
int main(int argc, char * argv[]){
    cin.tie(0);
    ios::sync_with_stdio(false);
    //-------------------------------
    //ll begin_t=clock();
    //freopen("big.txt", "r", stdin);
    //freopen("out3.txt", "w", stdout);
    //-----------------------------
    ll h,w;cin>>h>>w;
    ll a[h+1][w+1];
    ll b[h+1][w+1];
    for(ll i=0;i<h;i++){
        for(ll j=0;j<w;j++){
            cin>>a[i][j];
        }
    }
    for(ll i=0;i<h;i++){
        for(ll j=0;j<w;j++){
            cin>>b[i][j];
        }
    }
    dp[0][0][a[0][0]-b[0][0]+160*80]=1;
    dp[0][0][b[0][0]-a[0][0]+160*80]=1;
    for(ll i=0;i<h;i++){
        for(ll j=0;j<w;j++){
            if(i>0){
                for(ll k=0;k<=2*160*80;k++){
                    if(dp[i-1][j][k]){
                        dp[i][j][k+a[i][j]-b[i][j]]=1;
                        dp[i][j][k+b[i][j]-a[i][j]]=1;
                    }
                }
            }
            if(j>0){
                for(ll k=0;k<=2*160*80;k++){
                    if(dp[i][j-1][k]){
                        dp[i][j][k+a[i][j]-b[i][j]]=1;
                        dp[i][j][k+b[i][j]-a[i][j]]=1;
                    }
                }
            }
        }
    }
    ll ans=INF;
    for(ll k=0;k<=2*160*80;k++){
        if(dp[h-1][w-1][k]){
            chmin(ans,abs(k-160*80));
        }
    }
    cout<<ans<<endl;
    //------------------------------
    //fclose(stdin);
    //fclose(stdout);
    //ll end_t=clock();cout<<"time="<<end_t-begin_t<<"ms"<<endl;
    //-------------------------------
    return 0;
}
//----------------------------------------------------------------------

#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef vector<int> vi;

int main(){
  int k,n;
  cin >> k >> n;
  vi a(n);
  rep(i,n) cin >> a[i];
  sort(a.rbegin(),a.rend());
  cout << max(2*a[0]-k-1,0);
  return 0;
}


#include<iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

int main() {

	char str[20];
	int i, len=0;

	cin >> str;

	while (str[len])
		len++;

	for (i = len - 1; i >= 0; i--)
		cout << str[i];

	putchar('\n');

	return 0;

}


#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define rep1(i, n) for(int i = 1; i <= (int)(n); i++)
#define showmap(is, js, x) {rep(i, is){rep(j, js){cout << x[i][j] << " ";}cout << endl;}}
#define show(x) {for(auto i: x){cout << i << " ";} cout<<endl;}
#define showm(m) {for(auto i: m){cout << m.x << " ";} cout<<endl;}
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> llP;
ll gcd(int x, int y){ return y?gcd(y, x%y):x;}
ll lcm(ll x, ll y){ return (x*y)/gcd(x,y);}

/*
正面衝突の場合 U-D, L-R
斜め衝突の場合 U-L, L-D, D-R, R-U

*/

int x_in[200050], y_in[200050];
char c_in[200050];
vector<pair<int, char>> ur[400020];
vector<pair<int, char>> ud[400020];

int n;

int solve_UD(){
    rep(i, 400020){
        ud[i].clear();
    }
    rep(i, n){
        if (c_in[i] != 'U' && c_in[i] != 'D') continue;
        ud[x_in[i]].push_back(make_pair(y_in[i], c_in[i]));
    }
    int tmin = 2000001;
    rep(i, 400020){
        sort(ud[i].begin(), ud[i].end());
        rep(j, ud[i].size()-1){
            if (ud[i][j].second == 'U' && ud[i][j+1].second == 'D'){
                tmin = min(tmin, ud[i][j+1].first - ud[i][j].first);
            }
        }
    }
    return tmin * 5;
}

int solve_UR(){
    rep(i, 400020){
        ur[i].clear();
    }
    rep(i, n){
        if (c_in[i] != 'U' && c_in[i] != 'R') continue;
        ur[x_in[i]+y_in[i]].push_back(make_pair(x_in[i], c_in[i]));
    }
    int tmin = 2000001;
    rep(i, 400020){
        sort(ur[i].begin(), ur[i].end());
        rep(j, ur[i].size()-1){
            if (ur[i][j+1].second == 'U' && ur[i][j].second == 'R')
            tmin = min(tmin, ur[i][j+1].first - ur[i][j].first);
        }
    }
    return tmin*10;
}

void rotate(){
    int tmp;
    rep(i, n){
        tmp = x_in[i];
        x_in[i] = 200000-y_in[i];
        y_in[i] = tmp;
        if (c_in[i] == 'U') c_in[i] = 'L';
        else if (c_in[i] == 'L') c_in[i] = 'D';
        else if (c_in[i] == 'D') c_in[i] = 'R';
        else c_in[i] = 'U';
    }

}

int main()
{
    cin >> n;
    rep(i, n){
        cin >> x_in[i] >> y_in[i] >> c_in[i];
    }

    int t_time = 2000001;
    rep(t, 4){
        t_time = min(t_time, solve_UD());
        t_time = min(t_time, solve_UR());
        rotate();
    }

    if (t_time == 2000001) cout << "SAFE" << endl;
    else cout << t_time << endl;

}

#include <iostream>
#include <iomanip>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <cstdio>
#include <utility>
#include <string>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <numeric>

using namespace std;

typedef uint64_t u64;
typedef int64_t s64;
typedef uint32_t u32;
typedef int32_t s32;
typedef vector<s32> vs32;
typedef vector<u32> vu32;
typedef vector<s64> vs64;
typedef vector<u64> vu64;

const double PI=3.14159265358979323846;

#define MAX(x, y) ((x) < (y) ? (y) : (x))
#define MIN(x, y) ((x) > (y) ? (y) : (x))

#define rep(i, N) for(int i = 0; i < N; ++i)

#define CEIL(x, y) (((x) + (y) - 1) / (y))
#define MOD 1000000007ULL

#define IN(l, r, x) ((l) <= (x) && (x) < (r))

vector< vs32 > g;
vs32 color;

const int WHITE = 0;
const int GRAY = 1;
const int BLACK = 2;

inline int enc(int i, int j)
{
    if (i < j) swap(i, j);
    return i * (i - 1) / 2 + j + 1;
}

int d[1000 * 1000];
int dfs(int u)
{
    if (color[u] == BLACK) return d[u];
    color[u] = GRAY;

    bool found = false;
    for (auto v : g[u])
    {
        if (color[v] == GRAY)
        {
            return -1;
        }
        int temp = dfs(v);
        if (temp == -1)
        {
            return -1;
        }
        found = true;
        d[u] = max(d[u], temp + 1);
    }
    color[u] = BLACK;
    return d[u];
}

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);

    int n;
    cin >> n;

    int a;;
    color.resize(n * (n - 1) / 2 + 1, WHITE);
    g.resize(n * (n - 1) / 2 + 1);
    rep (i, n)
    {
        int u = 0;
        rep (j, n - 1)
        {
            cin >> a;
            --a;
            int v = enc(i, a);
            g[u].push_back(v);
            u = v;
        }
    }

    cout << dfs(0) << "\n";
    return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <stack>
#include <map>
#include <queue>
#include <cmath>
#include <iomanip>
#include <set>
#include <functional>
#include <list>

#define MOD (1000000000 + 7)

using namespace std;

typedef long long ll;
typedef pair<ll, ll> tup;
const int MAX = 3000000;

long long fac[MAX], finv[MAX], inv[MAX];

long long modinv(long long a, long long m) {
    long long b = m, u = 1, v = 0;
    while (b) {
        long long t = a / b;
        a -= t * b; swap(a, b);
        u -= t * v; swap(u, v);
    }
    u %= m;
    if (u < 0) u += m;
    return u;
}

ll modpow(ll a, ll n, ll mod) {
    ll res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}

ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b;}
// テーブルを作る前処理
void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i % MOD;
        inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}

// 二項係数計算
long long COM(int n, int k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

map<ll,ll> prime;//素因数分解でそれぞれの素数がいくつ出てきたかを保存するmap

//O(√n)
//整列済み(mapはkeyで自動で整列される)
void prime_factorize(ll n){
    if(n<=1) return;
    ll l=sqrt(n);
   for(ll i=2;i<=(ll)(l);i++) { //
        if(n%i==0){
            prime_factorize(i);prime_factorize(ll(n/i));return;
        }
    }
    //mapでは存在しないkeyの場合も自動で構築される
    prime[n]++;return;
}

vector<ll> divisor;
void list_divisor(ll n) {
    divisor.clear();
    for(int i = 1; i*i <= n; i++) {
        if(n % i == 0) {
            divisor.push_back(i);
            if(i*i != n) divisor.push_back(n/i);
        }
    }
    sort(divisor.begin(), divisor.end());
}

int main() {
    ll N;
    cin >> N;

    vector<ll> D(N, 0);
    map<ll, ll> m;
    ll maxv = 0;
    for(ll i = 0; i < N; i++) {
        cin >> D[i];
        m[D[i]]++;
        maxv = max(maxv, D[i]);
    }

    if(D[0] != 0 || m[0] >= 2) {
        cout << 0 << endl;
        return 0;
    }

    ll ret = 1;
    for(ll i = 1; i <= maxv; i++) {
        ret = (ret % 998244353) *  modpow(m[i-1], m[i], 998244353);
    }

    cout << ret%998244353 << endl;
}

#include<bits/stdc++.h>

#define fast ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define int long long
#define endl '\n'

using namespace std;

const int N = 2e5 + 77 ;
int mod = 998244353;
int cnt[N];
int32_t main(){
    fast;
    int n ; cin >> n ;
    int in = 0;
    for(int i = 0 ; i < n ; ++i){
        int a ; cin >> a;
        cnt[a]++;
        if(i==0) in = a;
    }
    int ans = (int)((cnt[0] == 1) && (in == 0));
    for(int i = 1 ; i <= 1e5 ;++i){
        for(int j = 0 ;j < cnt[i] ; ++j){
            ans = (ans*cnt[i-1]) % mod;
        }
    }
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main() {
  int N,x;
  cin >> N >> x;
  vector<int> data(N);
  for (int i=0; i<N; i++){
    cin >> data[i];
  }
  sort(data.begin(),data.end());
  for (int i=0; i<N; i++) {
    x = x - data[i];
    if (x == 0) {
      cout << i+1 <<endl;
      return 0;
    }
    if (x < 0){
      cout << i << endl;
      return 0;
    }

  }
  cout << N-1 << endl;
}

#include <iostream>
#include <vector>
#include <cstdio>
#include <algorithm>
#include <set>
#include <map>
#include <cassert>
#include <numeric>
#include <string>
#include <cstring>
#include <cmath>
using namespace std;

#ifdef LOCAL
	#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
	#define eprintf(...) 42
#endif

typedef long long int int64;

const int N = (int) 1e5 + 100;

vector <int> ch[N];
int k;

struct Ans
{
    vector <int> v;

    Ans() : v({0}) {}

    void eprint()
    {
        for (int i = 0; i < min(k + 1, (int) v.size()); i++)
            eprintf("[%d] %d, ", i, v[(int) v.size() - 1 - i]);
        eprintf("\n");
    }
};

Ans dp[N];

int merge(int a, int b)
{
    if ((int) dp[a].v.size() < (int) dp[b].v.size())
        swap(a, b);

    for (int i = 0; i < (int) dp[b].v.size(); i++)
        dp[a].v[(int) dp[a].v.size() - 1 - i] += dp[b].v[(int) dp[b].v.size() - 1 - i];

    return a;
}

int goUp(int a)
{
    dp[a].v.push_back(1);
    if ((int) dp[a].v.size() >= k + 1)
        dp[a].v.back() += dp[a].v[(int) dp[a].v.size() - k - 1];

    int i = 2;
    while (i <= (int) dp[a].v.size() && dp[a].v[(int) dp[a].v.size() - i] > dp[a].v.back())
    {
        dp[a].v[(int) dp[a].v.size() - i] = dp[a].v.back();
        i++;
    }

    return a;
}

int solve(int v)
{
    int cur = v;
    for (int to : ch[v])
    {
        int nx = solve(to);
        cur = merge(cur, nx);
    }
    //eprintf("(%d): ", v); dp[cur].eprint();
    if (v != 0)
        cur = goUp(cur);
    //eprintf("(%d'): ", v); dp[cur].eprint();
    return cur;
}

int a[N];

int main(int,  char **)
{
#ifdef LOCAL
	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);
#endif

    int n;
    scanf("%d%d", &n, &k);

    for (int i = 0; i < n; i++)
    {
        scanf("%d", &a[i]);
        a[i]--;
    }

    for (int i = 1; i < n; i++)
        ch[a[i]].push_back(i);

    int id = solve(0);

    int ans = dp[id].v[max(0, (int) dp[id].v.size() - k - 1)];

    if (a[0] != 0)
        ans++;

    printf("%d\n", ans);

	return 0;
}


#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <vector>

using namespace std;
typedef long long ll;

#define rep(i, n) for (ll i = 0; i < (n); i++)
#define reps(i, f, n) for (ll i = (f); i < (n); i++)
#define repr(i, n) for (ll i = n; i >= 0; i--)
#define repv(v) for (auto it = (v).begin(); it != (v).end(); it++)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define pb push_back
#define INIT    \
    cin.tie(0); \
    ios::sync_with_stdio(false);

template <class T>
inline bool chmax(T& a, T b) {
    return a = (a < b) ? b : a;
}
template <class T>
inline bool chmin(T& a, T b) {
    return a = (a > b) ? b : a;
}

ll const INF = 1LL << 60;
ll const MOD = 1000000007;

// 約数の列挙 O(√n)
vector<ll> divisor(ll n) {
    vector<ll> res;

    for (ll i = 1; i * i <= n; i++) {
        if (n % i == 0) {
            res.push_back(i);
            if (i != n / i) res.push_back(n / i);
        }
    }

    return res;
}

int main() {
    INIT;

    ll N;
    cin >> N;
    vector<ll> a(N + 1, 0);
    reps(i, 1, N + 1) cin >> a[i];

    vector<ll> b;
    vector<ll> sum(N + 1, 0);
    ll ans = 0;

    for (ll i = N; i > 0; i--) {
        if (sum[i] % 2 != a[i]) {
            b.pb(i);
            vector<ll> divs = divisor(i);
            rep(j, SZ(divs)) { sum[divs[j]]++; }
        }

        // cout << i << ": ";
        // repv(sum) cout << *it << " ";
        // cout << endl;
    }

    sort(ALL(b));
    cout << SZ(b) << endl;
    if (SZ(b) > 0) {
        repv(b) { cout << *it << " "; }
        cout << endl;
    }

    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define rep(i,N) for(int i=0;i<int(N);++i)
using ll = long long;
const int INF = 2147483647;
const ll MOD = 1e9+7;
// const ll INF = 1000000000000000000LL;

 //使い方
 int main() {
	 ll n;
	 cin >> n;
	 vector<int> a(n);
	 vector<int> ans(n);
	 rep(i,n) cin >> a[i];
	 for(int i=n-1;i>=0;i--){
		 int num = i+1;
		 ans[i] = a[i];
		 if(ans[i]==1){
			for(int j=1;j*j<=num;j++){
					if(num % j ==0){
						int x = j;
						int y = num / j;
						a[x-1] = 1 - a[x-1];
						if(x!=y)
							a[y-1] = 1 - a[y-1];
					}
				}
		 }
	 }
	 int cnt = 0;
	 vector<int> ans_vec;
	 rep(i,n){
		 if(ans[i]){
			  cnt++;
				ans_vec.push_back(i+1);
		 }
	 }
	 cout << cnt << endl;
	 if(cnt==0) return 0;
	 rep(i,ans_vec.size()){
		 cout << ans_vec[i] << ' ';
	 }
	 cout << endl;
 }


#include <bits/stdc++.h>
using namespace std;

#define IOS cin.sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define rep(i, n) for (int i = 0; i < n; i++)
#define repn(i, a, b) for (int i = a;i<=b;i++)
#define ll long double
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define mem(x,val) memset(x, val, sizeof(x))
#define ritr(it, a) for (auto it = a.begin(); it != a.end(); it++)
#define PI 3.1415926535897932384626
#define pai pair < int, int> ;
#define pal pair < ll, ll> ;
#define vi vector <int> ;
#define vl vector < ll > ;
#define vpai vector < pii > ;
#define prec(val,x) cout<<fixed<<setprecision(x)<<val;

const int mod = 1e9+7;
const int INF = INT_MAX;
const int sze = 305;
int n;

ll dp[sze][sze][sze];

ll calc(int one,int two,int three){
    if(one<0||two<0||three<0) return 0;
    if(one==0&&two==0&&three==0) return 0;
    if(dp[one][two][three]>0) return dp[one][two][three];
    int rem=one+two+three;
    ll exp=n+one*calc(one-1,two,three)+two*calc(one+1,two-1,three)+
            three*calc(one,two+1,three-1);
    return dp[one][two][three]=exp/rem;
}

void solve() {
    //int n;
    cin>>n;
    int arr[n];
    int x=0,y=0,z=0;
    rep(i,n) {
        cin>>arr[i];
        if(arr[i]==1) x++;
        if(arr[i]==2) y++;
        if(arr[i]==3) z++;
    }
    prec(calc(x,y,z),14);
    cout<<endl;

}

int main() {
    IOS;

    int t = 1;
    //cin >> t;
    while (t--) {
        mem(dp,-1);
        solve();
    }

  return 0;
}

#include <bits/stdc++.h>
using namespace std;

int main() {
  int num;
  int tmpCnt1,tmpCnt2;
  int Max1 = 0, Max2 = 0;
  string str1, str2;
  cin >> num >> str1 >> str2;

  if(str1 == str2){
    cout << num;
    return 0;
  }

  for(int i = 0; i < num; i++){
    tmpCnt1 = 0;

    if(str1.at(i) == str2.at(0)){
      for(int j = 0; j < num - i; j++){
        if(str1.at(i+j) == str2.at(j)) tmpCnt1++;
        else break;
      }
      Max1 = max(Max1, tmpCnt1);
    }
  }

  for(int i = 0; i < num; i++){
    tmpCnt2 = 0;

    if(str2.at(i) == str1.at(0)){
      for(int j = 0; j < num - i; j++){
        if(str2.at(i+j) == str1.at(j)) tmpCnt2++;
        else break;
      }
      Max2 = max(Max2, tmpCnt2);
    }
  }

  if(Max1 == 0 && Max2 == 0){
    cout << num * 2;
  }else if(Max1 >= Max2){
    cout << num + (num - Max1);
  }else{
    cout << num + (num - Max2);
  }
}


#include<vector>
#include<iostream>
using namespace std;

string solve(){
  vector<int> a;
  for(int i=0;i<3;i++){
    int tmp;
    cin>>tmp;
    a.push_back(tmp);
  }

  for(int i=0;i<a.size();i++){
    if(a[i]==100)return "A";
  }
  if(a[0]+a[1]>=90*2)return "A";
  int ave = (a[0]+a[1]+a[2])/3;
  if(ave>=80)return "A";
  if(ave>=70)return "B";
  if(ave>=50)if(a[0]>=80||a[1]>=80)return "B";
  return "C";
}

int main(){
  int n;
  while(cin>>n,n!=0){
    for(int i=0;i<n;i++){
      cout<<solve()<<endl;
    }
  }
}

#include <iostream>
using namespace std;

char judge(int pm, int pe, int pj)
{
    if(pm == 100 || pe == 100 || pj == 100 || pm + pe >= 180 || pm + pe + pj >= 240) return 'A';
    else if(pm + pe + pj >= 210 || pm + pe + pj >= 150 && (pm >= 80 || pe >= 80)) return 'B';
    return 'C';
}

int main()
{
    int n, pm, pe, pj;
    while(cin >> n, n){
        for(int i = 0; i < n; ++i){
            cin >> pm >> pe >> pj;
            cout << judge(pm,pe,pj) << endl;
        }
    }
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long
const int N = 2005;
const int mod = 1e9+7;
int fac[N],combi[N][N];

int add(int x,int y) {
   int ret = ((x+y)%mod + mod)%mod;
   return ret;
}
int mul(int x,int y) {
   ll ret = (ll)x*y;
   ret%=mod;
   return ret;
}

int num[2];
int group[2];
int dp[N][N];
int inv[N];

int invFac[N];

int solveDp(int x,int rem) {
   if(rem==0) {
      return 1;
   }
   if(x>num[1]) {
      return 0;
   }
   int &ret = dp[x][rem];
   if(ret != -1) {
      return ret;
   }
   ret = solveDp(x+1,rem);
   int cur = 1;
   for(int j = 0;j<group[0];++j) {
      cur = mul(cur, invFac[x]);
   }
   for(int f = group[0];f<=group[1] && f*x<=rem;++f) {
      int tot = f*x;
      int m = fac[tot];
      int t = cur;
      t = mul(t, invFac[f]);
      m = mul(t,m);
      m = mul(combi[rem][tot],m);
      ret = add(ret, mul(m,solveDp(x+1,rem-tot)));
      cur = mul(cur,invFac[x]);
   }
   return ret;
}

int myPow(int x,int c) {
   int ret = 1;
   while(c>0) {
      if(c&1) {
         ret = mul(ret,x);
      }
      x = mul(x,x);
      c/=2;
   }
   return ret;
}
void solve() {
   int n;
   memset(dp,-1,sizeof(dp));
   scanf("%d",&n);
   for(int i=0;i<2;++i) {
      scanf("%d",&num[i]);
   }
   for(int i=0;i<2;++i) {
      scanf("%d",&group[i]);
   }
   fac[0] = 1;
   inv[0] = 1;
   invFac[0] = 1;
   for(int i=1;i<N;++i) {
      fac[i] = mul(fac[i-1],i);
      inv[i] = myPow(i,mod-2);
      invFac[i] = myPow(fac[i],mod-2);
   }
   for(int i=0;i<N;++i) {
      combi[i][i] = combi[i][0] = 1;
      for(int j = 1;j<i;++j) {
         combi[i][j] = add(combi[i-1][j],combi[i-1][j-1]);
      }
   }
   printf("%d\n", solveDp(num[0],n));
}

int main()
{
   //freopen("input.txt","r",stdin);
   solve();
}

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>
using namespace std;

#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()

const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
/*----------------------------------------*/
const int MOD = 1000000007;

vector<long long> inv_init(int num) {
  vector<long long> inv(num + 1, 0);
  inv[1] = 1;
  FOR(i, 2, num + 1) inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
  return inv;
}

pair<long long, long long> ext_gcd(long long a, long long b) {
  if (b == 0) return {1, 0};
  pair<long long, long long> pr = ext_gcd(b, a % b);
  return {pr.second, pr.first - a / b * pr.second};
}

long long mod_inv(long long a) {
  if (__gcd(a, (long long)MOD) != 1) return 0;
  pair<long long, long long> pr = ext_gcd(a, MOD);
  return (pr.first + MOD) % MOD;
}

const int MAX = 10000000;
long long fact[MAX + 1], fact_inv[MAX + 1];
void nCk_init(int num = MAX) {
  fact[0] = 1;
  FOR(i, 1, MAX + 1) fact[i] = fact[i - 1] * i % MOD;
  fact_inv[MAX] = mod_inv(fact[MAX]);
  for (int i = MAX; i > 0; --i) fact_inv[i - 1] = fact_inv[i] * i % MOD;
}

inline long long nCk(int n, int k) {
  if (n < 0 || n < k || k < 0) return 0;
  return fact[n] * (fact_inv[k] * fact_inv[n - k] % MOD) % MOD;
}

int main() {
  cin.tie(0); ios::sync_with_stdio(false);
  // freopen("input.txt", "r", stdin);

  int n, a, b, c, d; cin >> n >> a >> b >> c >> d;
  nCk_init(n);
  vector<long long> inv = inv_init(d);
  // dp[i][j] = 既に i 人グループに含まれており,
  //            各グループの最大人数が j 人
  vector<vector<long long> > dp(n+1, vector<long long>(b+1, 0));
  dp[0][a-1] = 1; // 高々 j 人なので1人もグループ分けされていなくてもよい
  REP(i, n+1) FOR(j, a-1, b) {
    // 既に i 人グループに含まれており, 各グループの最大人数が j 人のとき
    // j+1 人のグループを k (k = 0, c,...,d) グループ作る場合
    // 残りの人々の選び方の総数は
    // C(n-i, j+1) * C(n-i-(j+1), j+1) * ... * C(n-i-(k-1)*(j+1), j+1) / k!
    (dp[i][j+1] += dp[i][j]) %= MOD; // k = 0
    long long tmp = dp[i][j];
    FOR(k, 1, d+1) {
      if (i+k*(j+1) > n) break;
      (tmp *= nCk(n-i-(k-1)*(j+1), j+1) * inv[k] % MOD) %= MOD;
      if (k >= c) (dp[i+k*(j+1)][j+1] += tmp) %= MOD;
    }
  }
  cout << dp[n][b] << '\n';
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define _   int v, int tl, int tr, int l, int r
#define tm  (tl + tr >> 1)
#define sol v+v, tl, tm, l, r
#define sag v+v+1, tm+1, tr, l, r
#define pb push_back
#define mp make_pair
#define st first
#define nd second
typedef pair < int , int > pp;
const int mod = 1e9 + 7;
const int N   = 2e3 + 3;

char s[N][N];
int sam[N],dif[N];

signed main(){
    int m,n;
    cin >> m >> n;
    for(int i=1; i<=m; i++)
        scanf(" %s", s[i]+1);

    int ans = max(m,n);
    for(int j=1; j<=n; j++){
        stack < pp > S;
        S.push(mp(-1,1));
        for(int i=2; i<=m+1; i++){
            int x = 0;
            if(s[i][j] == s[i-1][j]){
        		x = ++sam[i];
                dif[i] = 0;
            }
            else{
                x = ++dif[i];
               	sam[i] = 0;
            }
            if(i == m+1) x = 0;

            for(; S.top().st >= x;){
                int t = S.top().st;
                S.pop();
                ans = max(ans , t * (i-S.top().nd));
            }
            S.push(mp(x,i));
        }
    }

    cout << ans;
    return 0;
}

#include <iostream>
#include <string>
#include <queue>
#include <vector>
#include <algorithm>
#include <utility>

#define INF 1000000000

using namespace std;

typedef long long int lli;
typedef pair<int, int> P;

struct edge{
	int to, cost;
};

int V, E;
vector<edge> e[10000];
int mincost[10000];
bool used[10000];

int main(void){
	cin >> V >> E;

	for(int i = 0;i < V;i++){
		mincost[i] = INF;
		used[i] = false;
	}

	for(int i = 0;i < E;i++){
		int a, b, c;
		cin >> a >> b >> c;
		edge d;
		d.to = b;
		d.cost = c;
		e[a].push_back(d);
		d.to = a;
		e[b].push_back(d);
	}

	mincost[0] = 0;
	long long int res = 0;

	while(true){
		int v = -1;
		for(int u = 0;u < V;u++){
			if(!used[u] && (v == -1 || mincost[u] < mincost[v])) v = u;
		}

		if(v == -1) break;
		used[v] = true;
		res += mincost[v];

		for(int u = 0;u < e[v].size();u++){
			mincost[e[v][u].to] = min(mincost[e[v][u].to], e[v][u].cost);
		}
	}
	cout << res << endl;
}


#include "bits/stdc++.h"
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

#define ub              upper_bound
#define lb              lower_bound
#define isrt            insert
#define clr             clear
#define rsz             resize
#define ff              first
#define ss              second
#define lli             long long int
#define pb              push_back
#define pf              push_front
#define mkp              make_pair
#define pii             pair<int,int>
#define vi              vector<int>
#define mii             map<lli,lli>
#define pqb             priority_queue<int>
#define pqs             priority_queue<int,vi,greater<int> >
#define setbits(x)      __builtin_popcountll(x)
#define zrobits(x)      __builtin_ctzll(x)
#define mod             1000000007
#define INF             1e9
#define ps(x,y)         fixed<<setprecision(y)<<x
#define mk(arr,n,type)  type *arr=new type[n];
#define w(x)            int x; cin>>x; while(x--)
#define all(v)          v.begin(),v.end()
#define endl            "\n"

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;

lli maxm(lli a, lli b)
{
    return (a >= b ? a : b);
}

lli minm(lli a, lli b)
{
    return (a <= b ? a : b);
}

lli power(lli x, lli y, lli p)
{
    lli res = 1;

    x = x % p;
    if (x == 0)
        return 0;

    while (y > 0)
    {

        if (y & 1)
            res = (res * x) % p;

        y = y >> 1;
        x = (x * x) % p;
    }
    return res;
}

lli nCrmodp(lli n, lli r, lli p)
{
    if (r == 0)
        return 1;
    if (r == 1)
        return n;

    lli pro = 1, k = n;
    while (k > r)
    {
        pro *= k;
        pro %= p;
        --k;
    }

    k = 2;
    while (k <= (n - r))
    {
        pro *= power(k, (p - 2), p);
        pro %= p;
        ++k;
    }

    return pro;
}

string num_str(lli n, lli k)
{
    string str = "";
    lli x = n;
    while (x != 0)
    {
        string s = to_string(x % k);
        str = s + str;
        x /= k;
    }

    return str;
}

lli first_big(lli n, lli k, lli arr[])
{
    lli l = 0;
    lli r = n - 1;

    lli leftGreater = n;

    while (l <= r) {
        lli m = l + (r - l) / 2;

        if (arr[m] > k) {
            leftGreater = m;
            r = m - 1;
        }

        else
            l = m + 1;
    }

    return leftGreater;
}

void c_p_c()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
}

double dp[302][302][302], n;
double neg = -1.00000000, zer = 0.00000000;

double exp_val(int one, int two, int three)
{

    if (one < 0 || two < 0 || three < 0)
        return 0;

    if (dp[one][two][three] == neg)
    {
        if (one == 0 && two == 0 && three == 0)
            dp[one][two][three] = zer;

        else
        {
            double x = (n + one * exp_val(one - 1, two, three) + two * exp_val(one + 1, two - 1, three) + three * exp_val(one, two + 1, three - 1));
            x /= (one + two + three);

            dp[one][two][three] = x;
        }
    }

    return dp[one][two][three];

}

int main()
{
    c_p_c();
    cin >> n;
    for (int i = 0; i < 302; ++i)
        for (int j = 0; j < 302; ++j)
            for (int k = 0; k < 302; ++k)
                dp[i][j][k] = neg;

    int x, one = 0, two = 0, three = 0;

    for (int i = 0; i < n; ++i)
    {
        cin >> x;
        if (x == 1)
            ++one;
        if (x == 2)
            ++two;

        if (x == 3)
            ++three;
    }

    cout << ps(exp_val(one, two, three), 16) << endl;

    return 0;
}


#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

int main()
{

  int n;
  while(cin>>n){
    if( n == 0 ) break;

    int score[n];
    for(int i = 0; i < n; ++i){
      score[i] = 0;
    }
    for(int i = 0; i < n*(n-1)/2; ++i){
      int a,b,c,d;
      cin >> a >> b >> c >> d;
      --a;
      --b;

      score[a] += c>d?3:c==d?1:0;
      score[b] += d>c?3:c==d?1:0;
    }

    vector<pair<int,int> > v;
    for(int i = 0; i < n; ++i){
      v.push_back( make_pair(score[i], i) );
    }
    sort(v.begin(), v.end());

    int rank[n];

    /*
    for(int i = 0; i < n; ++i){
      cout << score[i] << endl;
    }
    */

    for(int i = n-1; i >= 0; --i){
      rank[ v[i].second ] = n-1-i+1;
      if( i<n-1 && v[i].first == v[i+1].first){
        rank[ v[i].second ] = rank[ v[i+1].second ];
      }
    }

    for(int i = 0; i < n; ++i){
      cout << rank[i] << endl;
    }

  }
  return 0;
}


#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>

#define INF 2001
#define MAX 101

using namespace std;

int MAT[MAX][MAX];
int N;

int prim(){
    int sum;
    bool complete[MAX];
    int dist[MAX];
    int parent[MAX];

    for (int i = 0; i < N; i++) {
        complete[i] = false;
        dist[i] = INF;
        parent[i] = -1;
    }

    dist[0] = 0;

    while (1) {
        int u = -1;
        int min = INF;

        for (int i = 0; i < N; i++) {
            if (min > dist[i] && !complete[i]) {
                u = i;
                min = dist[i];
            }
        }

        if (u == -1) break;

        complete[u] = true;

        for (int v = 0; v < N; v++) {
            if (!complete[v] && MAT[u][v] != INF) {
                if (dist[v] > MAT[u][v]) {
                    dist[v] = MAT[u][v];
                    parent[v] = u;
                }
            }
        }
    }

    sum = 0;

    for (int i = 1; i < N; i++) {
        sum += MAT[parent[i]][i];
    }

    return sum;
}

int main(){
    cin >> N;
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            scanf("%d",&MAT[i][j]);
            if (MAT[i][j] == -1) MAT[i][j] = INF;
        }
    }

    cout << prim() << endl;

    return 0;
}

#include <bits/stdc++.h>
using namespace std;

#define REP(i, m, n) for (int i = (m); i < (int)(n); i++)
#define REPS(i, m, n) for (int i = (m); i <= (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define reps(i, n) for (int i = 0; i <= (int)(n); i++)
#define rrep(i, x) for (int i = ((int)(x)-1); i >= 0; i--)
#define rreps(i, x) for (int i = (int)(x); i >= 0; i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pb push_back
#define fi first
#define se second
typedef long long ll;
typedef pair<int, int> P;
const int inf = INT_MAX;
const ll INF = 1LL << 60;
const ll mod = 1e9 + 7;
const double EPS = 1e-10;
const double PI = acos(-1.0);
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<typename A, size_t N, typename T> void Fill(A (&array)[N], const T &val) { fill( (T*)array, (T*)(array+N), val ); }
template <class T1, class T2, class Pred = std::less<T2> >
struct sort_pair_second {
    bool operator()(const std::pair<T1,T2>&left, const std::pair<T1,T2>&right) {
        Pred p;
        return p(left.second, right.second);
    }
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    int N; cin >> N;
    vector<ll> a(N);
    rep(i, N) cin >> a[i];
    ll s = 0;
    rep(i, N) s ^= a[i];
    ll ans = s;
    rep(i, N) {
      // s で１が立っていたビットを強制的にa[i]を0にビットを変換（考慮しない）
      a[i] &= ~s;
    }
    int r = 0;
    vector<int> top(N);
    for (int i = 60; i >= 0; i--) {
      int j = r;
      for (; j < N; j++) {
        if (a[j] >> i & 1) {
          swap(a[r], a[j]);
          break;
        }
      }
      if (j == N) continue;
      top[r] = i;
      rep(k, N) {
        if (r == k) continue;
        if (a[k] >> i & 1) a[k] ^= a[r];
      }
      r++;
    }

    ll x = 0;
    for (int i = 60; i >= 0; i--) {
      ll nx = x | (1ll << i);
      ll b = nx;
      rep(j, r) {
        if (b >> top[j] & 1) b ^= a[j];
      }
      if ((b & nx) == 0) {
        x = nx;
      }
    }
    ans += x * 2;
    cout << ans << endl;
    return 0;

}


#pragma GCC optimize(2)
#include <bits/stdc++.h>
using namespace std;
template<class t> inline t read(t &x){
	x=0;char c=getchar();bool f=0;
	while(!isdigit(c)) f|=c=='-',c=getchar();
	while(isdigit(c)) x=(x<<1)+(x<<3)+(c^48),c=getchar();
	if(f) x=-x;return  x;
}
template<class t> inline void write(t x){
	if(x<0){putchar('-'),write(-x);}
	else{if(x>9)write(x/10);putchar('0'+x%10);}
}

const int N=2005;
int n,m,q,a[N][N],s[N][N],sx[N][N],sy[N][N];

void doit(){
	int x,y,xx,yy;
	read(x);read(y);read(xx);read(yy);
	int n=s[xx][yy]-s[xx][y-1]-s[x-1][yy]+s[x-1][y-1];
	int m=(sx[xx][yy-1]-sx[xx][y-1]-sx[x-1][yy-1]+sx[x-1][y-1])+(sy[xx-1][yy]-sy[x-1][yy]-sy[xx-1][y-1]+sy[x-1][y-1]);
	write(n-m);puts("");
}

signed main(){
	read(n);read(m);read(q);
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++) scanf("%1d",&a[i][j]);
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++){
			s[i][j]=s[i-1][j]+s[i][j-1]-s[i-1][j-1]+a[i][j];
			sx[i][j]=sx[i-1][j]+sx[i][j-1]-sx[i-1][j-1]+(a[i][j]+a[i][j+1]==2);
			sy[i][j]=sy[i-1][j]+sy[i][j-1]-sy[i-1][j-1]+(a[i][j]+a[i+1][j]==2);
		}
	while(q--) doit();
}

#include <bits/stdc++.h>
// #include <atcoder/all>
using namespace std;
// using namespace atcoder;

// 型定義
typedef long long ll;
typedef pair<ll, ll> P;

// forループ
#define REP(i,n) for(ll i=0; i<(ll)(n); ++i)

// 定数宣言
const int INF = 1e9;
const int MOD = 1e9+7;
const ll LINF = 1e18;

// グラフ表現
using Graph = vector<vector<int>>;

// グラフの辺表現
using Edge = map<pair<int,int>,int>;

// n次元配列の初期化。第２引数の型のサイズごとに初期化していく。
template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val){
    std::fill( (T*)array, (T*)(array+N), val );
}

// 最大公約数
ll gcd(ll a,ll b){
   if (a%b == 0) return(b);
   else return(gcd(b, a%b));
}

// 最小公倍数
ll lcm(ll a, ll b){
    return a/gcd(a, b) * b;
}

ll dp[85][85][14000];

int main()
{
    cout << fixed << setprecision(15);
    ll H, W;
    cin >> H >> W;

    vector<vector<ll>> A(H+1, vector<ll>(W+1, 0));
    vector<vector<ll>> B(H+1, vector<ll>(W+1, 0));

    for(ll i=1; i<=H; i++){
        for(ll j=1; j<=W; j++){
            cin >> A[i][j];
        }
    }

    for(ll i=1; i<=H; i++){
        for(ll j=1; j<=W; j++){
            cin >> B[i][j];
        }
    }

    Fill(dp, 0);

    ll a = A[1][1];
    ll b = B[1][1];

    dp[1][1][abs(a-b)] = 1;

    for(ll i=1; i<=H; i++){
        for(ll j=1; j<=W; j++){
            REP(k, 13000){
                a = A[i][j];
                b = B[i][j];

                dp[i][j][abs(k+abs(a-b))] = max(dp[i][j][abs(k+abs(a-b))], dp[i-1][j][k]);
                dp[i][j][abs(k-abs(a-b))] = max(dp[i][j][abs(k-abs(a-b))], dp[i-1][j][k]);

                dp[i][j][abs(k+abs(a-b))] = max(dp[i][j][abs(k+abs(a-b))], dp[i][j-1][k]);
                dp[i][j][abs(k-abs(a-b))] = max(dp[i][j][abs(k-abs(a-b))], dp[i][j-1][k]);
            }
        }
    }

    // REP(i, H+1){
    //     REP(j, W+1){
    //         cout << dp[i][j][0];
    //     }
    //     cout << endl;
    // }
    REP(k, 13000){
        // cout << dp[H][W][k] << endl;
        if(dp[H][W][k]){
            cout << k << endl;
            return 0;
        }
    }
    return 0;
}


#include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <cstring>
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <cmath>
#include <numeric>
#include <float.h>
#include <math.h>
#include <sstream>
#include <iterator>
using namespace std;
using Graph = vector<vector<int>>;
// repマクロの定義
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
// allマクロの定義
#define all(v) v.begin(), v.end()
// 大きい順
#define rall(v) v.rbegin(), v.rend()
#define vi vector<int>
#define vl vector<ll>
const  long long INF = 1LL << 60;
typedef  long long ll;
template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

int ctoi(char c) {
 	switch (c) {
		case '0': return 0;
		case '1': return 1;
		case '2': return 2;
		case '3': return 3;
		case '4': return 4;
		case '5': return 5;
		case '6': return 6;
		case '7': return 7;
		case '8': return 8;
		case '9': return 9;
		default: return 0;
	}
}

bool is_prime(const unsigned n){
    switch(n){
        case 0: // fall-through
        case 1: return false;
        case 2: // fall-through
        case 3: return true;
    } // n > 3 が保証された

    if(n % 2 == 0 || n % 3 == 0) return false;
    // n は 2 と 3 のいずれの倍数でもないことが保証された
    // これより n は (6の倍数)-1 か (6の倍数)+1 である

    // 6の倍数前後の数を使って試し割りをする
    for(unsigned i = 5; i * i <= n; i += 6) {
        if(n % i     == 0) return false; // (6の倍数)-1
        if(n % (i+2) == 0) return false; // (6の倍数)+1
    }

    return true;
}

map<int,int> mp;

int primeFactorization(int a){ //素因数分解を出力するプログラム
    int i,sq;
    if(a%2==0){ //偶数の場合
        if(!mp.count(2)){
            mp[2] = 1;
        }
        else{
            mp[2] +=1;
        }
        return primeFactorization(a/2); //2で割った値で再帰
    }
    sq = sqrt(a);
    for(i=3;i<=sq;i+=2){ //3以上√a以下の奇数の場合
        if(a%i==0){
            if(!mp.count(i)){
                mp[i] = 1;
            }
            else{
                mp[i] += 1;
            }
            return primeFactorization(a/i); //割れた値で再帰
        }
    }
    //偶数でも3以上√a以下の奇数の場合でも割り切れない場合
    if(a!=1){ //aが1でないなら、a自身は素数
        if(!mp.count(a))
        mp[a] = 1;
        else{
            mp[a] += 1;
        }
    }
    return 0;
}

ll kaizyou(ll n){
    if(n == 1){
        return 1;
    }
    else{
        return (n * kaizyou(n-1)) % 1000000007;
    }
}

int main(){
int A,B,M; cin >> A >> B >> M;
vi a(A);
vi b(B);
int p,q;
p = q = 100000000;
rep(i,A){
 cin >> a[i];
 chmin(p,a[i]);
}
rep(i,B) {
    cin >> b[i];
    chmin(q,b[i]);
}
int mi = p + q;
rep(i,M){
    int x,y,c;cin >> x >> y >> c;
    x--;
    y--;
    chmin(mi,a[x]+b[y]-c);
}
cout << mi << endl;
}

#include <bits/stdc++.h>
using namespace std;

int main() {
    int A,B,M;
    cin >> A>>B>>M;
    vector<int>veca(A);
    vector<int>vecb(B);
    for(int i=0;i<A;i++){
        cin>>veca.at(i);
    }
    for(int i=0;i<B;i++){
        cin>>vecb.at(i);
    }
    int countera=999999;
    int counterb=999999;
    for(int i=0;i<A;i++){
   countera=min(countera,veca.at(i));
    }
    for(int i=0;i<B;i++){
   counterb=min(counterb,vecb.at(i));
    }
    int counter=countera+counterb;
    vector<vector<int>>vec(M,vector<int>(3));
   for(int i=0;i<M;i++){
    for(int j=0;j<3;j++){
        cin >> vec.at(i).at(j);
    }
   }
      for(int i=0;i<M;i++){
     counter= min(veca.at(vec.at(i).at(0)-1)+vecb.at(vec.at(i).at(1)-1)-vec.at(i).at(2),counter);
    }
    cout <<counter <<endl;
   }


// M
#include<bits/stdc++.h>
using namespace std;
const int N = 303, Mod = 998244353;
int n, k, C[N], dp[N][N][N];
inline void Add(int &a, int b)
{
	a += b;
	if (a >= Mod)
		a -= Mod;
}
int main()
{
	string S;
	cin >> S >> k;
	S = "0" + S;
	for (int i = 0; i < (int)S.size();)
	{
		int r = i + 1;
		while (r < (int)S.size() && S[r] == '1')
			r ++;
		C[n ++] = r - i - 1;
		i = r;
	}
	k = min(k, (int)S.size());
	dp[n][0][0] = 1;
	int sm = 0;
	for (int i = n - 1; ~ i; sm += C[i], i --)
		for (int j = sm; j >= 0; j --)
		{
			for (int h = 0; h <= k; h ++)
				Add(dp[i][j][h], dp[i][j + 1][h]),
				Add(dp[i][j][h], dp[i + 1][j][h]);
			for (int h = 0; h <= k; h ++)
				for (int df = 1; df <= C[i]; df ++)
					Add(dp[i][j + df][h + df], dp[i + 1][j][h]);
		}
	int tot = 0;
	for (int h = 0; h <= k; h ++)
		Add(tot, dp[0][0][h]);
	return !printf("%d\n", tot);
}


#include <iostream>
#include <queue>
using namespace std;
typedef long long ll;
typedef pair<ll, int> P;

int main()
{
    int n;
    ll d;
    cin >> n >> d;
    ll a[200005];
    priority_queue<P, vector<P>, greater<P>> que;
    for(int i = 0; i < n; i++){
        cin >> a[i];
        que.push(P(a[i], i));
    }
    bool used[200005]{0};
    ll ans = 0;
    while(que.size()){
        P p = que.top();
        que.pop();
        int u = p.second;
        if(u > 0 && !used[u]){
            ans += d + a[u] + a[u - 1];
            if(d + a[u] < a[u - 1]){
                a[u - 1] = d + a[u];
                que.push(P(a[u - 1], u - 1));
            }
            used[u] = true;
        }
        if(u < n - 1 && !used[u + 1]){
            ans += d + a[u] + a[u + 1];
            if(d + a[u] < a[u + 1]){
                a[u + 1] = d + a[u];
                que.push(P(a[u + 1], u + 1));
            }
            used[u + 1] = true;
        }
    }
    cout << ans << endl;
}

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
#define f(i,a,b) for (int i = a; i < b; i++)
#define fr(i,a,b) for (int i = b-1; i >= a; i--)

const int mod = 998244353;
const int mxn = 1e5;
int n,K;

void add(ll &a, ll b) { if (b>=mod) b%=mod; a+=b; if (a>=mod) a-=mod; }
ll modpow(ll a, ll b) {
	return b?modpow(a*a%mod,b/2)*(b&1?a:1)%mod:1;
}
struct Comb {
	ll fac[mxn], inv[mxn], twopow[mxn];
	Comb() {
		f(i,0,mxn) {
			fac[i]=i?fac[i-1]*i%mod:1;
			twopow[i]=i?twopow[i-1]*2%mod:1;
		}
		inv[mxn-1]=modpow(fac[mxn-1],mod-2);
		fr(i,0,mxn-1) {
			inv[i]=inv[i+1]*(i+1)%mod;
		}
	}
	ll choose(int n, int r) {
		if (n<0||n<r||r<0) return 0;
		return fac[n]*inv[r]%mod*inv[n-r]%mod;
	}
	ll multichoose(int n, int k) {
		if (!n&&!k) return 1;
		return choose(n+k-1,k);
	}
	ll pow2(int n) {
		if (n<0) return 0;
		return twopow[n];
	}
} C;

ll solve(int x) {
	ll ans = 0;
	int a = 0, b = 0;
	f(i,1,K+1) {
		if (i+i==x) b++;
		if (i<=K && x-i<=K && x-i>0) a++;
	}
	a/=2;
	f(na,0,a+1) {
		f(nb,0,b+1) {
			add(ans,C.pow2(na)*C.choose(a,na)%mod*C.multichoose(na+K-2*a-b,n-na-nb));
		}
	}
	return ans;
}

const int DEBUG = 1;

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
#ifdef LOCAL
	if (DEBUG) freopen("input.txt", "r", stdin);
	if (DEBUG) freopen("output.txt", "w", stdout);
#endif

	cin >> K >> n;
	for (int x = 2; x <= 2*K; x++)
		cout << solve(x) << endl;
	cout << endl;

	return 0;
}


#include <cstdio>
#include <vector>
#define rep(i,n) for(int i = 0; i < n; i++)
using namespace std;

int n, m;
bool used[500];
vector<int> e[500];

int main(){
	while(scanf("%d%d", &n, &m), n||m){
		rep(i,500) e[i].clear();
		rep(i,500) used[i] = false;
		rep(i,m){
			int x, y; scanf("%d%d", &x, &y);
			x--; y--;
			e[x].push_back(y);
			e[y].push_back(x);
		}
		used[0] = true;
		rep(i,e[0].size()){
			used[e[0][i]] = true;
			rep(j,e[e[0][i]].size()){
				used[e[e[0][i]][j]] = true;
			}
		}
		int ans = -1;
		rep(i,500) ans += used[i];
		printf("%d\n", ans);
	}
}


#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define repLRE(i, l, r) for (ll i = (l); i <= (r); ++i)
#define rrepLRE(i, l, r) for (ll i = (l); i >= (r); --i)
#define Sort(v) sort(v.begin(), v.end())
#define rSort(v) sort(v.rbegin(), v.rend())
#define Reverse(v) reverse(v.begin(), v.end())
#define Lower_bound(v, x) \
  distance(v.begin(), lower_bound(v.begin(), v.end(), x))
#define Upper_bound(v, x) \
  distance(v.begin(), upper_bound(v.begin(), v.end(), x))

using ll = long long;
using ull = unsigned long long;
using P = pair<ll, ll>;
using T = tuple<ll, ll, ll>;
using vll = vector<ll>;
using vP = vector<P>;
using vT = vector<T>;
using vvll = vector<vector<ll>>;
using vvP = vector<vector<P>>;
using dqll = deque<ll>;

ll dx[9] = {-1, 1, 0, 0, -1, -1, 1, 1, 0};
ll dy[9] = {0, 0, -1, 1, -1, 1, -1, 1, 0};

/* Macros reg. ends here */

const ll INF = 1LL << 50;

int main() {
  // ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cout << fixed << setprecision(15);

  ll n, g;
  cin >> n >> g;
  g /= 100;

  vll ps(n), cs(n);
  rep(i,n) {
    cin >> ps[i] >> cs[i];
    cs[i] /= 100;
  }

  ll ans = INF;
  rep(s, 1 << n){
    ll tmp = 0;
    ll scr = 0;
    rep(j, n){
      if((s >> j) & 1) {
        tmp += ps[j];
        scr += (j+1)*ps[j]+cs[j];
      }
    }
    ll cur = n - 1;
    ll curn = 0;
    bool ok = true;
    while(g > scr){
      if(cur < 0){
        ok = false;
        break;
      }
      if((s >> cur) & 1){
        cur--;
        curn = 0;
        continue;
      }
      if(curn == ps[cur]-1){
        cur--;
        tmp += curn-1;
        curn = 0;
      }
      scr += cur + 1;
      curn++;
    }
    tmp += curn;
    if(ok) chmin(ans, tmp);
  }

  cout << ans << endl;

  return 0;
}

#include<cstdio>
#include<math.h>
#include<algorithm>
#include<vector>
#include<queue>
#include<string>
#include<set>
#include<cstring>
#include<map>

using namespace std;
#define int long long int
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1001001001
#define LLINF 1001001001001001001
#define mp make_pair
#define pb push_back
#define mod 1000000007
//#define mod 998244353

int D,G;
int p[15],c[15];
int dp[15][1500];
signed main(){
    scanf("%lld %lld",&D,&G);
    int maxp=0;
    rep(i,D){
        scanf("%lld %lld",&p[i],&c[i]);
        maxp=max(maxp,p[i]);
    }
    rep(i,D+1)rep(j,D*maxp+1)dp[i][j]=-INF;
    rep(i,D){
        rep(j,D*maxp+1){
            if(i==0&&j==0)dp[i][j]=0;
            rep(p_num,p[i]+1){
                if(p_num!=p[i])dp[i+1][j+p_num]=max(dp[i+1][j+p_num],dp[i][j]+p_num*(i+1)*100);
                else dp[i+1][j+p_num]=max(dp[i+1][j+p_num],dp[i][j]+p_num*(i+1)*100+c[i]);
            }
        }
    }
    int ans=-1;
    rep(j,D*maxp+1){
        //printf("%lld/",dp[D][j]);
        if(dp[D][j]>=G){ans=j;break;}
    }
    printf("%lld\n",ans);
}


#include<bits/stdc++.h>
//#include<atcoder/all>
// Begin Header {{{
//using namespace atcoder;
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
#define rep(i,n) for(ll i=0; i<n; i++)
#define loop(i, j, n) for(ll i=j; i<n; i++)
#define all(x) (x).begin(), (x).end()
#define UNIQUE(v) v.erase(unique(all(v)), v.end());
#define Sp(p) cout<<setprecision(25)<< fixed<<p<<endl;
constexpr int INF  = 0x3f3f3f3f;
const long long mod=1e9+7;
const long double PI = acos(-1);
template<class T, class S> inline bool chmax(T &a, const S &b) { if (a<b) { a = b; return 1; } return 0; }
template<class T, class S> inline bool chmin(T &a, const S &b) { if (a>b) { a = b; return 1; } return 0; }
#define INT(...)                                                                                \
    int __VA_ARGS__;                                                                                \
    IN(__VA_ARGS__)
#define LL(...)                                                                                \
    ll __VA_ARGS__;                                                                                \
    IN(__VA_ARGS__)
#define STR(...)                                                                                \
    string __VA_ARGS__;                                                                                \
    IN(__VA_ARGS__)
#define CHR(...)                                                                                \
    char __VA_ARGS__;                                                                                \
    IN(__VA_ARGS__)
#define DBL(...)                                                                                \
    double __VA_ARGS__;                                                                                \
    IN(__VA_ARGS__)
template<class T> void scan(T &a) { cin >> a; }
void IN() {}
template<class Head, class... Tail> void IN(Head &head, Tail &... tail){ scan(head); IN(tail...); }
// }}} End Header

int main() {
	LL(n);
	STR(s);
	ll west_cnt = 0;
	ll east_cnt = 0;

	loop(i, 1, n) if(s[i] == 'E') east_cnt++;
	ll min = east_cnt;
	loop(i, 1, n){
		if(s[i-1] == 'W') west_cnt++;
		if(s[i] == 'E') east_cnt--;
		chmin(min, west_cnt+east_cnt);
	}
	cout << min << endl;
	return 0;
}

#include <bits/stdc++.h>
#include <iostream>
#include <string>
#include <string.h>
#include <ctype.h>
#include <algorithm>

using namespace std;

int main() {
  int n,ans,W[310000] = {},E[310000] = {};
  string s;
  cin >> n >> s;

  for(int i = 1; i <= n-1; i++) {
    if(s[i-1] == 'W')
      W[i] = W[i-1] + 1;
    else W[i] = W[i-1];
  }
  for(int i = n-2; i >= 0; i--) {
    if(s[i+1] == 'E')
      E[i] = E[i+1] + 1;
    else E[i] = E[i+1];
  }

  int m = n;
  for(int i = 0; i < n; i++)
    m = min(m,W[i]+E[i]);

  cout << m << endl;
}


#include <bits/stdc++.h>
using namespace std;

#define rep(i,n) for(i = 0;i < n;++i)
#define all(v) v.begin(), v.end()
using ll = int;

int main()
{
    ll i,j,k;
    ll h,w;
    cin >> h >> w;
    ll inf = 160*80;
    vector<vector<ll>> a(h,vector<ll>(w,0));
    vector<vector<ll>> b(h,vector<ll>(w,0));
    for(i = 0;i < h;++i){
        rep(j,w) cin >> a.at(i).at(j);
    }
    for(i = 0;i < h;++i){
        rep(j,w) cin >> b.at(i).at(j);
    }
    vector<vector<vector<ll>>> dp(h, vector<vector<ll>>(w, vector<ll>(2*inf+1,0)));
    ll tmp = a.at(0).at(0) - b.at(0).at(0);
    dp.at(0).at(0).at(inf + tmp) = 1;
    dp.at(0).at(0).at(inf - tmp) = 1;
    for(i = 0;i < h;++i){
        rep(j,w){
            ll ni = i + 1;
            ll nj = j + 1;
            ll t1, t2;
            if(ni < h) t1 = a.at(ni).at(j) - b.at(ni).at(j);
            if(nj < w) t2 = a.at(i).at(nj) - b.at(i).at(nj);
            rep(k, 2*inf+1){
                if(dp.at(i).at(j).at(k)){
                    if(ni < h){
                        dp.at(ni).at(j).at(k + t1) = 1;
                        dp.at(ni).at(j).at(k - t1) = 1;
                    }
                    if(nj < w){
                        dp.at(i).at(nj).at(k + t2) = 1;
                        dp.at(i).at(nj).at(k - t2) = 1;
                    }
                }
            }
        }
    }
    ll ans = 1e9;
    for(i = 0;i < 2*inf + 1;++i){
        if(dp.at(h-1).at(w-1).at(i)){
            ans = min(ans, abs(i - inf));
        }
    }
    cout << ans << endl;

    return 0;
}

#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#include <map>
#include <algorithm>
#include <iomanip>
#include <math.h>
#include <string.h>
#include <cstdio>
#include <tuple>
using namespace std; using ll = long long; using pll = pair<ll, ll>;
using vl = vector<ll>; using vll = vector<vl>; using vpll = vector<pll>;
//**デバッグ準備**//
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
void debug_out() { cerr << endl; }
template<typename Head, typename... Tail>
void debug_out(Head h, Tail... t) {
	cerr << " " << h;
	if (sizeof...(t) > 0) cerr << " :";
	debug_out(t...);
}
template <typename T>
ostream& operator<<(ostream& os, vector<T> vec) {
	for (size_t i = 0; i < vec.size(); i++)os << vec[i] << (i + 1 == vec.size() ? "" : " ");
	return os;
}
ll ctoi(char c) {
	switch (c) {
	case '0': return 0; case '1': return 1; case '2': return 2;
	case '3': return 3; case '4': return 4; case '5': return 5;
	case '6': return 6; case '7': return 7; case '8': return 8;
	case '9': return 9;  default: return 0;
	}
}
bool pairCompare(const pll firstElof, pll secondElof)
{
	return firstElof.first > secondElof.first;
}
//**Snippetリスト**//
//rep, vin, all, iteLoop//
//gcdlcm, isPrime, eratos, Npow, divisor, modinv, bitSearch, bitList, dfs, bfs//
//dijkstra, WarshallFloyd, BellmanFord, UnionFind, COM, digitDP ,RMQ//
ll i, j, k, l; ll N, M, K, H, W, L, X, Y, Z;
ll MOD = 1000000007; ll INF = 1LL << 60; ll ans = 0; ll z = 0, o = 1;
vl flag, color, D; vll path;
//***********//
int main() {
	cin >> H >> W;
	vll A(H, vl(W, 0));
	vll B(H, vl(W, 0));
	vll D(H+1, vl(W+1, 0));
	for (i = 0; i < H; i++) {
		for (j = 0; j < W; j++) {
			cin >> A[i][j];
		}
	}
	for (i = 0; i < H; i++) {
		for (j = 0; j < W; j++) {
			cin >> B[i][j];
			D[i+1][j+1] = abs(A[i][j] - B[i][j]);
		}
	}
	ll M = (H + W) * 80;
	vector<vll> dp(H+1, vll(W+1, vl(M, 0)));
	dp[1][1][D[1][1]] = 1;
	for (i = 1; i <= H; i++) {
		for (j = 1; j <= W; j++) {
			for (k = 0; k < M; k++) {
				if (i == H && j == W) continue;
				if (i != H) {
					if (k + D[i + 1][j] < M) dp[i + 1][j][k + D[i + 1][j]] |= dp[i][j][k];
					if (abs(k - D[i + 1][j]) < M) dp[i + 1][j][abs(k - D[i + 1][j])] |= dp[i][j][k];
				}
				if (j != W) {
					if (k + D[i][j + 1] < M) dp[i][j + 1][k + D[i][j + 1]] |= dp[i][j][k];
					if (abs(k - D[i][j + 1]) < M) dp[i][j + 1][abs(k - D[i][j + 1])] |= dp[i][j][k];
				}
			}
		}
	}
	for (i = 1; i <= H; i++) {
		for (j = 1; j <= W; j++) {
			for (k = 0; k < M; k++) {
				//if(dp[i][j][k]) debug(i, j, k, dp[i][j][k]);
			}
		}
	}
	for (k = 0; k < M; k++) {
		if (dp[H][W][k]) {
			cout << k;
			exit(0);
		}
	}
}


#pragma region header
#include <bitset>
#include <tuple>
#include <cstdint>
#include <cstdio>
#include <cctype>
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <limits>
#include <map>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#include <math.h>
using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define rev(i, n) for(int i = (int)(n - 1); i >= 0; i--)
#define rev1(i, n) for(int i = (int)(n); i > 0; i--)
#define pb push_back
#define ts to_string
#define all(v) (v).begin(), (v).end()
#define vi vector<int>
#define vvi vector<vector<int>>
#define vc vector<char>
#define vvc vector<vector<char>>
#define vs vector <string>
#define vvs vector <vector<string>>
#define pq  priority_queue<int>
using ll = long long;
/* ----------------よく使う数字や配列----------------- */
int dx[] = { 1,0,-1,0 };
int dy[] = { 0,1,0,-1 };
const int INF = 2000000000;
const int MINF=-2000000000;
constexpr ll mod = 1e9 + 7;
constexpr long double pi = 3.141592653589793238462643383279;
/* ----------------------end----------------------- */
/* --------------------ライブラリ-------------------- */
ll fact(int i) {       //階乗
    if (i == 0) return 1;
    return (fact(i - 1)) * i % mod;
}
ll gcd(ll a, ll b) {        //最大公約数
    if (b == 0) return a;
    return gcd(b, a % b);
}
ll lcm(ll a, ll b) {      //最小公倍数
    return a * b / gcd(a, b);
}
bool isprime(ll a)//素数判定、primeならtrue,違うならfalse
{
    if (a < 2) return false;
    else if (a == 2) return true;
    else if (a % 2 == 0) return false;
    double m = sqrt(a);
    for (int i = 3; i <= m; i += 2)
    {
        if (a % i == 0)
        {
            return false;
        }
    }
    // 素数である
    return true;
}
ll ketasum(ll n) {    //各桁の和
    ll sum = 0;
    while (n != 0) {
        sum += n % 10;
        n /= 10;
    }
    return sum;
}
ll kind(vi SYURUI) {//vector<int>型の配列の要素の種類数
    int size = SYURUI.size();
    sort(all(SYURUI));
    int cnt = 1;
    rep(i, size-1) {
        if (SYURUI[i] != SYURUI[i + 1])cnt++;
    }
    return cnt;
}
/* ----------------------end----------------------- */
#pragma endregion
signed main() {
    int A, B, C, D, K; cin >> A >> B >> C >> D >> K;
    cout << (60 * C + D) - (60 * A + B) - K << endl;
}

#include <bits/stdc++.h>

using namespace std;

#define SZ(x) (int)(x.size())

using ll = long long;
using ld = long double;
using P = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
const double eps = 1e-10;
const int MOD = 1000000007;
const int INF = 1000000000;
const ll LINF = 1ll<<50;

template<typename T>
void printv(const vector<T>& s) {
  for(int i=0;i<(int)(s.size());++i) {
    cout << s[i];
    if(i == (int)(s.size())-1) cout << endl;
    else cout << " ";
  }
}

vvll mem;
int ng = 0;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);

  ll n, x, y, z; cin >> n >> x >> y >> z;

  ll MAX = 1ll << (x + y + z - 1);

  const ll NG_BITS = (1ll<<(x+y+z-1)) | (1ll<<(y+z-1)) | (1ll<<(z-1));

  vvll dp(n+1, vll(MAX, 0));
  dp[0][0] = 1;

  for(int i=0;i<n;++i) {
    for(ll j=1;j<=10;++j) {
      for(ll k=0;k<MAX;++k) {
        ll t = (k << j) | (1ll << (j - 1));

        if((NG_BITS & t) != NG_BITS) {
          t &= (MAX - 1);
          dp[i+1][t] += dp[i][k];
          dp[i+1][t] %= MOD;
        }
      }
    }
  }

  ll ans = 1;
  for(int i=0;i<n;++i) {
    ans *= 10;
    ans %= MOD;
  }

  for(int i=0;i<MAX;++i) {
    ans += MOD - dp[n][i];
    ans %= MOD;
  }
  cout << ans << endl;

}


#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<vector>
#include<functional>
#include<algorithm>
#include<stdlib.h>
#include<string>
#include<string.h>
#define _USE_MATH_DEFINES
#include<math.h>
#include<deque>
#include<set>
#include<map>
#include<queue>
#include<list>
#include<iostream>
#include <bitset>
using namespace std;
typedef long long ll;
#define rep(i,a,b) for(auto i=a;i<b;i++)
#define rep2(i, a)for(auto i : a)
#define all(_x) _x.begin(), _x.end()
#define r_sort(_x) sort(_x, std::greater<int>())
#define vec_cnt(_a, _n) (upper_bound(all(_a), _n) - lower_bound(all(_a), _n))
#define vec_unique(_a) _a.erase(std::unique(all(_a)), _a.end());
#define vvec vector<vector<ll>>
ll gcd(ll a, ll b) { return a % b == 0 ? b : gcd(b, a % b); }
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }
#define INF 1 << 30
const int mod = 1000000007;
ll power(ll x, ll p) { ll a = 1; while (p > 0) { if (p % 2 == 0) { x *= x; p /= 2; } else { a *= x; p--; } }return a; }
ll mpower(ll x, ll p) { ll a = 1; while (p > 0) { if (p % 2 == 0) { x = x * x % mod; p /= 2; } else { a = a * x % mod; p--; } }return a; }
ll co(ll n, ll k) { ll a = 1; rep(i, 1, k) { a *= n - i + 1; a /= i; }return a; }
ll mc(ll n, ll m) { ll k = 1, l = 1; rep(i, n - m + 1, n + 1) k = k * i % mod; rep(i, 1, m + 1) l = l * i % mod; l = mpower(l, mod - 2); return k * l % mod; }

int main()
{
	int n;
	string s, t, str[4] = {"SS", "SW", "WS", "WW"};
	cin >> n >> s;
	t.resize(s.size());
	//t[s.size()] = 0;
	rep(_i, 0, 4) {
		t[0] = str[_i][0];
		t[1] = str[_i][1];
		rep(i, 1, s.size() + 1) {
			t[(i+1) % s.size()] = ((s[i % s.size()] == 'o') ^ (t[(i-1) % s.size()] == 'S') ^ (t[i % s.size()] == 'S')) ? 'S' : 'W';
		}
		if (t[0] == str[_i][0] && t[1] == str[_i][1]) {
			puts(t.c_str());
			return 0;
		}
	}
	puts("-1");
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 100005;

int n;
string s;

bool val = false;
bitset<N> ans;

int main() {
    cin.sync_with_stdio(0); cin.tie(0);
    cin >> n >> s;
    int variations[4][2] = {{0, 1}, {0, 0}, {1, 0}, {1, 1}};

    for (int v = 0; v < 4; ++v) {
        ans.reset();
        ans[0] = variations[v][0];
        ans[1] = variations[v][1];

        for (int i = 1; i < n - 1; ++i) {
            if (!ans[i]) {
                if (s[i] == 'o') ans[i + 1] = ans[i - 1];
                else ans[i + 1] = !ans[i - 1];
            } else {
                if (s[i] == 'o') ans[i + 1] = !ans[i - 1];
                else ans[i + 1] = ans[i - 1];
            }
        }

        bool good = true;
        if (ans[n - 1]) {
            if (s[n - 1] == 'o' && ans[n - 2] == ans[0]) {
                good = false;
            } else if (s[n - 1] == 'x' && ans[n - 2] != ans[0]) {
                good = false;
            }
        } else {
            if (s[n - 1] == 'o' && ans[n - 2] != ans[0]) {
                good = false;
            } else if (s[n - 1] == 'x' && ans[n - 2] == ans[0]) {
                good = false;
            }
        }

        if (ans[0]) {
            if (s[0] == 'o' && ans[1] == ans[n - 1]) {
                good = false;
            } else if (s[0] == 'x' && ans[1] != ans[n - 1]) {
                good = false;
            }
        } else {
            if (s[0] == 'o' && ans[1] != ans[n - 1]) {
                good = false;
            } else if (s[0] == 'x' && ans[1] == ans[n - 1]) {
                good = false;
            }
        }

        if (good) {
            val = true;
            break;
        }
    }

    if (!val) {
        cout << -1;
        return 0;
    }

    for (int i = 0; i < n; ++i) {
        if (!ans[i]) cout << 'S';
        else cout << 'W';
    }
}


#include <bits/stdc++.h>
#define FOR(i, begin, end) for(int i=(begin);i<(end);i++)
#define REP(i, n) FOR(i,0,n)
#define IFOR(i, begin, end) for(int i=(end)-1;i>=(begin);i--)
#define IREP(i, n) IFOR(i,0,n)
#define SORT(a) sort(a.begin(), a.end())
#define REVERSE(a) reverse(a.begin(), a.end())
#define Lower_bound(v, x) distance(v.begin(), lower_bound(v.begin(), v.end(), x))
#define Upper_bound(v, x) distance(v.begin(), upper_bound(v.begin(), v.end(), x))
#define int long long
#define INF 1000000000000000000
using namespace std;

#define ANS(f) if(f) cout << "YES" << endl; else cout << "NO" << endl;

typedef vector<int> vec;
typedef vector<vec> mat;
typedef pair<int, int> Pii;

template<typename T>
void readv(vector<T> &a){ REP(i, a.size()) cin >> a[i]; }
void readi(vector<int> &a){ REP(i, a.size()){cin >> a[i]; a[i]--;} }
void debug(mat m){REP(i, m.size()){ REP(j, m[i].size()){ cout << m[i][j] << ","; } cout << endl; }}

int modpow(int x, int n, int m){
    int a = 1;
    IREP(i, 64){
        a = (a * a) % m;
        if(((n >> i) & 1) == 1) a = (a * x) % m;
    }
    return a;
}

class Combination
{
public:

    vec fact, invfact;
    int MAX_N, mod;

    Combination(int MAX_N, int mod): MAX_N(MAX_N), mod(mod) {
        initialize();
    }

    void initialize(){
        fact = vec(MAX_N + 1);
        invfact = vec(MAX_N + 1);
        fact[0] = 1;
        FOR(i, 1, MAX_N + 1){
            fact[i] = (fact[i - 1] * i) % mod;
        }
        invfact[MAX_N] = modpow(fact[MAX_N], mod - 2, mod);
        IREP(i, MAX_N){
            invfact[i] = (invfact[i + 1] * (i + 1)) % mod;
        }
    }

    int nCr(int n, int r){
        if(r > n || r < 0 || n < 0) return 0;
        if(n > MAX_N){
            MAX_N = n;
            initialize();
        }
        int a = fact[n];
        a = (a * invfact[r]) % mod;
        a = (a * invfact[n - r]) % mod;
        return a;
    }
};

signed main(){

    int K, N; cin >> K >> N;
    int mod = 998244353;
    Combination comb(N + K, mod);

    FOR(i, 2, 2 * K + 1){
        if(i % 2 == 1){
            int p = min(i, 2 * K + 2 - i) / 2;
            int ans = 0;
            REP(j, p + 1){
                int k = K - 2 * p + j;
                int n = N - j;
                ans += ((comb.nCr(n + k - 1, n) * modpow(2, j, mod)) % mod) * comb.nCr(p, j);
                ans %= mod;
            }
            cout << ans << endl;
        }else{
            int p = min(i, 2 * K + 2 - i) / 2 - 1;
            int ans = 0;
            REP(j, p + 1){
                int k = K - 1 - 2 * p + j;
                int n = N - j;
                ans += ((comb.nCr(n + k - 1, n) * modpow(2, j, mod)) % mod) * comb.nCr(p, j);
                ans %= mod;
                n = N - j - 1;
                ans += ((comb.nCr(n + k - 1, n) * modpow(2, j, mod)) % mod) * comb.nCr(p, j);
                ans %= mod;
            }
            cout << ans << endl;
        }
    }

    return 0;
}

#include <bits/stdc++.h>

using namespace std;

const int maxn = 2e5 + 5;
int n, a[maxn];
long long sum[maxn];

long long F(int left, int m, int right) {
	return abs(sum[right]-2*sum[m]+sum[left]);
}

int find(int l, int r) {
	int left = l-1, right = r+1;
	while (r-l >= 3) {
		int m1 = l + (r-l)/3;
		int m2 = r - (r-l)/3;
		if (F(left, m1, right) < F(left, m2, right)) r = m2;
		else l = m1;
	}
	if (l == r) return l;
	else if (l == r+1) {
		if (F(left, l, right) < F(left, l+1, right)) return l;
		else return l+1;
	}
	else {
		if (F(left, l, right) < F(left, l+1, right) && F(left, l+2, right))
			return l;
		else if (F(left, l+1, right) < F(left, l+2, right))
			return l+1;
		else return l+2;
	}
}

int main() {
	cin >> n;
	for (int i = 1; i <= n; ++i)
		cin >> a[i];
	sum[0] = 0;
	for (int i = 1; i <= n; ++i)
		sum[i] = sum[i-1] + a[i];
	long long ans = 1e17;
	for (int i = 2; i <= n-2; ++i) {
		int j = find(1, i-1);
		int k = find(i+1, n-1);
		long long maxt = max(sum[j], max(sum[i]-sum[j], max(sum[k]-sum[i], sum[n]-sum[k])));
		long long mint = min(sum[j], min(sum[i]-sum[j], min(sum[k]-sum[i], sum[n]-sum[k])));
		ans = min(ans, maxt-mint);
	}
	cout << ans << endl;
	return 0;
}


#include <bits/stdc++.h>

const int mod = 998244353;
typedef long long LL;
void reduce(int & x) { x += x >> 31 & mod; }
int mul(int a, int b) { return (LL) a * b % mod; }
int pow(int a, int b, int res = 1) {
	for (; b; b >>= 1, a = mul(a, a)) if (b & 1) res = mul(res, a);
	return res;
}
const int MAXS = 1000010;
int fac[MAXS], inv[MAXS];
int C(int a, int b) { return a < b ? 0 : (LL) fac[a] * inv[b] % mod * inv[a - b] % mod; }
int main() {
	fac[0] = fac[1] = inv[0] = inv[1] = 1;
	for (int i = 2; i != MAXS; ++i) {
		fac[i] = mul(fac[i - 1], i);
		inv[i] = mul(inv[mod % i], mod - mod / i);
	}
	for (int i = 2; i != MAXS; ++i) inv[i] = mul(inv[i - 1], inv[i]);
	std::ios_base::sync_with_stdio(false), std::cin.tie(0);
	int n, m; std::cin >> n >> m;
	int ans = std::max(n, m), a2 = 0;
	for (int i = std::min(n, m); i; --i)
		reduce(a2 += mul(C(i * 2, i), C(n + m - i * 2, n - i)) - mod);
	a2 = pow(mul(C(n + m, n), 2), mod - 2, a2);
	reduce(ans += a2 - mod);
	std::cout << ans << std::endl;
	return 0;
}

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int,int> pii;

typedef int _loop_int;
#define REP(i,n) for(_loop_int i=0;i<(_loop_int)(n);++i)
#define FOR(i,a,b) for(_loop_int i=(_loop_int)(a);i<(_loop_int)(b);++i)
#define FORR(i,a,b) for(_loop_int i=(_loop_int)(b)-1;i>=(_loop_int)(a);--i)

#define VIN(v) for(auto&elem_: (v) )cin>>elem_
#define VOUT(v, sep) for(_loop_int idx=0; idx<(_loop_int)v.size(); idx++) {cout<<v[idx]; if(idx<(_loop_int)v.size()-1) cout<<sep;} cout<<endl
#define ALL(a) (a).begin(),(a).end()

#define DEBUG(x) cout<<#x<<": "<<x<<endl
#define DEBUG2(x,y) cout<<#x<<": "<<x<<" "<<#y<<": "<<y<<endl
#define DEBUG_VEC(v) cout<<#v<<":";REP(debugidx,v.size())cout<<" "<<v[debugidx];cout<<endl
#define DEBUG_ARR(v,n) cout<<#v<<":";REP(debugidx,n)cout<<" "<<v[debugidx];cout<<endl

const ll MOD = 1000000007ll;
const int IINF = numeric_limits<int>::max()/2-1;
const ll LINF = numeric_limits<ll>::max()/2-1;

template<class T> inline bool chmin(T& a, const T b) { if (a>b) { a=b; return true; } return false; }
template<class T> inline bool chmax(T& a, const T b) { if (a<b) { a=b; return true; } return false; }

int main() {
    ios::sync_with_stdio(false); cin.tie(0);
    int n,y;
    cin>>n>>y;
    y/=1000;

    FOR(i,0,n+1){
        FOR(j,0,n+1-i){
            int k = n-i-j;
            if(i + j*5 + k*10 == y){
                printf("%d %d %d", k,j,i);
                return 0;
            }
        }
    }
    cout << "-1 -1 -1" << endl;
    return 0;
}


#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <fstream>
#include <unordered_map>

using namespace std;

ifstream fin("../data.txt");

int n, x, a1, a2, eat;
long long ans;

int main() {

  cin >> n >> x >> a1;
  n--;
  while (n--) {
    cin >> a2;
    if (a1 + a2 > x) {
      eat = a1 + a2 - x;
      ans += eat;
      if (eat > a2) {
        a2 = 0;
      } else {
        a2 -= eat;
      }
    }
    a1 = a2;
  }
  cout << ans;
  return 0;
}

#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <vector>

using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define reps(i, f, n) for (ll i = (f); i < (n); i++)
#define repv(v) for (auto it = (v).begin(); it != (v).end(); it++)
ll const INF = 1LL << 60;
ll const MAX_N = 510000;
ll const MOD = 1000000007;

// in
// 6 3
// 4 3 1 0 9 8
// out
// 18

int main() {
    ll N, x;
    cin >> N >> x;
    vector<ll> a(N);
    rep(i, N) cin >> a[i];

    ll ans = 0;
    for (ll i = 0; i < N - 1; i++) {
        if (a[i] + a[i + 1] > x) {
            ans += a[i] + a[i + 1] - x;
            a[i + 1] -= min(a[i + 1], (a[i] + a[i + 1] - x));
        }

        // repv(a) cout << *it << " ";
        // cout << endl;
    }
    cout << ans << endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ld = long double;
using pii = pair<int, int>;

#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define all(x) (x).begin(),(x).end()
#define m0(x) memset(x,0,sizeof(x))

int dx4[4] = {1,0,-1,0}, dy4[4] = {0,1,0,-1};

int main()
{
    int x[2],ans=0;
    for (int i = 0; i < 2; i++)
    {
        cin>>x[i];
        if(x[i]==1) ans+=300000;
        if(x[i]==2) ans+=200000;
        if(x[i]==3) ans+=100000;
    }
    if(x[0]==1&&x[1]==1) ans+=400000;
    cout<<ans<<endl;

    return 0;
}

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int INF = 1001001001;
const ll LINF = 1001001001001001;
const int MOD = 1000000007;

template <typename T>
void print(const T &v);

int main()
{
    int x, y;
    cin >> x >> y;
    int ans = 0;
    if (x == 1)
    {
        ans += 300000;
    }
    else if (x == 2)
    {
        ans += 200000;
    }
    else if (x == 3)
    {
        ans += 100000;
    }
    if (y == 1)
    {
        ans += 300000;
    }
    else if (y == 2)
    {
        ans += 200000;
    }
    else if (y == 3)
    {
        ans += 100000;
    }
    if (ans == 600000)
    {
        ans += 400000;
    }
    cout << ans << endl;
    return 0;
}

// Use For Debug
template <typename T>
void print(T const &v)
{
    for (int i = 0; i < v.size(); i++)
    {
        if (i)
            cout << ' ';
        cout << v[i];
    }
    cout << endl;
}


#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;

/*2進数配列+1*/
vector<int> twoadd(vector<int> v, int N){
    v[N-1]+=1;
    int ind = N-1;
    int j=N-1;
    for(j=N-1;j>=1;j--){
        if(v[j]>1){
            v[j-1]+=1;
            v[j]=0;
        }
    }
    return v;
}

/*フィボナッチ*/
long long fibonatti(long long d){
    long long count = 0;
    long long f1 = 1;
    long long f2 = 1;/*ここを変える*/
    long long temp;
    if(d == 1){
        count = f1;
    }else if(d == 2){
        count = f2;
    }else if(d==0){
        count = 1;
    }else{
        for(int i=0;i<d-2;i++){
            temp = f1+f2;
            f1 = f2;
            f2 = temp;
        }
        count = temp;
    }
    return count;
}

/*最大公約数*/
unsigned long long GCD(long long L,long long R){
    if(L>R){
        long long temp=R;
        R = L;
        L = temp;
    }
    unsigned long long pp=0,ppt=0;
    unsigned long long ans=0;
    if(R%L==0){
        ans = L;
    }else{
        while(true){
            ppt = pp;
            pp=R%L;
            if(pp == 0){
                ans = ppt;
                break;
            }
            R = L;
            L = pp;
        }
    }
    return ans;
}

/*最小公倍数*/
unsigned long long LCM(long long L,long long R){
    unsigned long long ans;
    unsigned long long gcd = GCD(L,R);
    ans = (L/gcd)*R;
    return ans;
}

/*Combination set*/
#define mod 1000000007
#define maxcomb 3000/*大きいものを求めるときはここを変える*/
vector<long long> fc(maxcomb+1);
vector<long long> ifc(maxcomb+1);
long long modpow(long long x,long long n){
    long long ans = 1;
    while(n > 0){
        if(n & 1) {
            ans = ans*x%mod;
        }
        x = x*x%mod;
        n >>= 1;
    }
    return ans;
}
void Conb(){
    fc[0]= 1;
    ifc[0]=1;
    for(long long i=0;i<maxcomb;i++){
        fc[i+1] = fc[i]*(i+1)%mod;//n!(mod)
        ifc[i+1] = ifc[i]*modpow(i+1,mod-2)%mod;//k!^{M-2} (mod)
    }
}
unsigned long long Combination(long long L,long long R){
    unsigned long long up=1,ans;
    Conb();
    if(L==0&&R==0){
        return 1;
    }else if(L<R||L<0){
        return 0;
    }else{
        long long t = ifc[L-R]*ifc[R]%mod;
        ans = t*fc[L]%mod;
    }
    return ans;
}
/*Combination set ここまで*/

/*ここから*/
int main() {
    int K,X;
    cin >> K >> X;
    for(int i=X-K+1;i<X+K;i++){
        cout << i ;
        if(i!=X+K-1){
            cout << " ";
        }else{
            cout << endl;
        }
    }
    return 0;
}

#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
typedef long long ll;
int read()
{
	int out = 0, sgn = 1;
	char jp = getchar();
	while (jp != '-' && (jp < '0' || jp > '9'))
		jp = getchar();
	if (jp == '-')
		sgn = -1, jp = getchar();
	while (jp >= '0' && jp <= '9')
		out = out * 10 + jp - '0', jp = getchar();
	return out * sgn;
}
const int P = 1e9 + 7;
int add(int a, int b)
{
	return a + b >= P ? a + b - P : a + b;
}
void inc(int &a, int b)
{
	a = add(a, b);
}
const int N = 3e5 + 10;
char s[N];
int trans[9][2] = {{1, 2}, {3, 0}, {4, 5}, {1, 1}, {6, 2}, {7, 5}, {4, 4}, {8, 5}, {7, 7}};
int n, dp[N][9];
int main()
{
	scanf("%s", s + 1);
	n = strlen(s + 1);
	dp[0][0] = 1;
	for (int i = 0; i < n; ++i)
		for (int j = 0; j < 9; ++j) if (dp[i][j])
		{
			if (s[i + 1] != '0')
				inc(dp[i + 1][trans[j][1]], dp[i][j]);
			if (s[i + 1] != '1')
				inc(dp[i + 1][trans[j][0]], dp[i][j]);
		}
	int ans = 0;
	for (int j : {2, 5, 7, 8})
		inc(ans, dp[n][j]);
	printf("%d\n", ans);
	return 0;
}


#include <iostream>
#include <string>
#include <iomanip>
#include <math.h>
#include <utility>
#include <vector>
#include <cstdlib>
#include <algorithm>
#include <climits>
#include <queue>
using namespace std;

#define rep(i,n) for(int i=0;i<n;i++)

int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
string table[12];

void dfs(int x,int y){
    table[x][y]='0';
    for(int i=0;i<4;i++){
        int nx=x+dx[i];
        int ny=y+dy[i];
        if(nx>=0&&nx<12&&ny>=0&&ny<12&&table[nx][ny]=='1'){
            dfs(nx,ny);
        }
    }
}

int main(){
    while(cin >> table[0]){
        for(int i=1;i<12;i++) cin >> table[i];
        int ans=0;
        rep(i,12){
            rep(j,12){
                if(table[i][j]=='1'){
                    dfs(i,j);
                    ans++;
                }
            }
        }
        cout << ans << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main(){
  int n;
  cin >> n;
  vector<ll> takahashi(n);
  ll sum = 0;
  for(int i = 0; i < n; i++){
    ll a, b;
    cin >> a >> b;
    takahashi[i] = a+b;
    sum-=b;
  }
  sort(takahashi.begin(), takahashi.end(), greater<long long>());
  for(int i = 0; i < n; i++){
    if(i%2 == 0){
    sum += takahashi[i];
    }
  }
  cout << sum;
}

#include <cstdio>
#include <iostream>
#include <string>
#include <cstdlib>
#include <vector>
#include <algorithm>
#include <functional>
#include <iomanip>
#include <stdlib.h>
using namespace std;

int main()
{
	int n;
	cin >> n;
	vector<int> a(n);
	int min = 0;
	int max = 0;
	long long sum = 0;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	sort(a.begin(), a.end());
	min = a[0];
	sort(a.rbegin(), a.rend());
	max = a[0];
	for (int i = 0; i < n; i++) {
		sum += a[i];
	}
	cout << min << " " << max << " " << sum << endl;
}


#include <iostream>
#include <regex>
#include <stdio.h>
#include <string>

using namespace std;

int main() {
  string S;
  cin >> S;
  regex rx(R"(^A[a-z][a-z]*?C[a-z]*?[a-z]$)");
  if (regex_match(S, rx)) {
    printf("AC\n");
  } else {
    printf("WA\n");
  }
}

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef double dd;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int,int> pii;

const int mx=1e6+11;
const double PI = acos(-1);
#define MOD 1000000007

#define nl '\n'
#define pb push_back
#define F first
#define S second
#define I insert
#define mp make_pair

#define sz(x) x.size()
#define tt int t; cin>>t; while(t--)
#define lo(i,a,b) for(int i=a;i<b;i++)
#define rlo(i,n) for(int i=n-1;i>=0;i--)
#define be(a) (a).begin(),(a).end()
#define rbe(a) (a).rbegin(),(a).rend()
#define mem(a,b) memset(a,b,sizeof(a))
#define pr(a) {for(auto x:a)cout<<x<<" ";cout<<nl;}
#define ac cout<<"AC"<<nl
#define wa cout<<"WA"<<nl
#define su ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define file freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#define frac cout.unsetf(ios::floatfield); cout.precision(10); cout.setf(ios::fixed,ios::floatfield);

#define dbg(args...) do {cerr << #args << " : "; faltu(args); } while(0)
void faltu () {            cerr << endl;}
template < typename T, typename ... hello>void faltu( T arg, const hello &... rest) {cerr << arg << ' ';faltu(rest...);}

void f()
{
  string s;
  cin>>s;
  int n=sz(s),c=0,C=0,a=0;
  if(s[0]=='A')
  {
    for(int i=1;i<n;i++)
    {
      if(s[i]=='C')
        c++;
    }
    if(c>1||c==0)
      wa;
    else
    {
      for(int i=2;i<n-1;i++)
      {
        if(s[i]=='C')
          C++;
      }
      if(C==1)
      {
        for(int i=1;i<n;i++)
        {
          if(s[i]!='C'&&s[i]>='a'&&s[i]<='z')
            a++;
        }
        if(2+a==n)
          ac;
        else
          wa;
      }
      else
        wa;
    }
  }
  else
    wa;
}

int main()
{
    su;
    ///tt
    f();
    return 0;
}