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

void solve(long long N, std::string S, long long Q, std::vector<long long> k)
{

    for (ll i = 0; i < Q; i++) {
        ll D = 0;
        ll M = 0;
        ll DM = 0;
        ll DMC = 0;
        ll ans = 0;
        for (ll j = 0; j < N; j++) {
            if (j - k[i] >= 0) {
                if (S[j - k[i]] == 'D') {
                    D--;
                    DM -= M;
                }
                if (S[j - k[i]] == 'M') {
                    M--;
                }
            }
            if (S[j] == 'D') {
                D++;
            }
            if (S[j] == 'M') {
                M++;
                DM += D;
            }
            if (S[j] == 'C') {
                DMC += DM;
            }
            ans = max(ans, DMC);
        }
        cout << ans << "\n";
    }
}

// Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
int main()
{
    long long N;
    scanf("%lld", &N);
    std::string S;
    std::cin >> S;
    long long Q;
    scanf("%lld", &Q);
    std::vector<long long> k(Q - 1 - 0 + 1);
    for (int i = 0; i < Q - 1 - 0 + 1; i++) {
        scanf("%lld", &k[i]);
    }
    solve(N, S, Q, std::move(k));
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define rep2(i, m, n) for(int i=int(m); i<int(n); ++i)
#define rep(i, n) rep2(i, 0, n)
using ll = long long;
using V = vector<int>;
using Vll = vector<ll>;
using VV = vector<V>;
using VVll = vector<Vll>;

char s[1000000];
ll t[3][1000001];

int main() {
  int n, q;
  scanf("%d%s%d", &n, s, &q);

  VV pos(3, V());
  char dmc[3] = {'D', 'M', 'C'};

  rep(i, n) rep(j, 3) {
    t[j][i+1] = t[j][i];
    if (s[i] == dmc[j]) {
      ++t[j][i+1];
      pos[j].push_back(i);
    }
  }

  rep(_, q) {
    int k;
    scanf("%d", &k);
    ll ans = 0;
    for (auto &c : pos[2]) ans += t[1][c] * (t[0][c] - t[0][max(c-k+1, 0)]);
    for (auto &a : pos[0]) ans -= t[1][a+1] * (t[2][min(a+k, n)] - t[2][a+1]);
    printf("%lld\n", ans);
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
typedef long long ll;
#define ALL(v) v.begin(), v.end()
#define vmax(v) *max_element(ALL(v))
#define vmin(v) *min_element(ALL(v))
#define chmax(x, a) x = max(x, a)
#define chmin(x, a) x = min(x, a)

int main() {
    int H, W, A, B; cin >> H >> W >> A >> B;
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            if (j < A && i < B) cout << 1;
            else if (j >= A && i >= B) cout << 1;
            else cout << 0;
        }
        cout << endl;
    }
}

#include <bits/stdc++.h>
using namespace std;
typedef pair<char,int> P;
char S[1000001];
vector<P> V;
int main() {
    scanf("%s",S);
    char ch=S[0];
    int cnt=0;
    for(int i=0;S[i]!='\0';i++) {
        if(ch==S[i]) {
            cnt++;
        }else {
            V.push_back(P(ch,cnt));
            cnt=1;
            ch=S[i];
        }
    }
    V.push_back(P(ch,cnt));
    int ans=0;
    for(int i=0;i+2<V.size();i++) {
        if(V[i].first!='J'||V[i+1].first!='O'||V[i+2].first!='I') continue;
        if(V[i].second>=V[i+1].second&&V[i+2].second>=V[i+1].second) {
            ans=max(ans,V[i+1].second);
        }
    }
    printf("%d\n",ans);
}


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

#define ALL(obj) (obj).begin(),(obj).end()
#define SPEED cin.tie(0);ios::sync_with_stdio(false);

template<class T> using PQ = priority_queue<T>;
template<class T> using PQR = priority_queue<T,vector<T>,greater<T>>;

constexpr long long MOD = (long long)1e9 + 7;
constexpr long long MOD2 = 998244353;
constexpr long long HIGHINF = (long long)1e18;
constexpr long long LOWINF = (long long)1e15;
constexpr long double PI = 3.1415926535897932384626433;

template <class T> vector<T> multivector(size_t N,T init){return vector<T>(N,init);}
template <class... T> auto multivector(size_t N,T... t){return vector<decltype(multivector(t...))>(N,multivector(t...));}
template <class T> void corner(bool flg, T hoge) {if (flg) {cout << hoge << endl; exit(0);}}
template <class T, class U>ostream &operator<<(ostream &o, const map<T, U>&obj) {o << "{"; for (auto &x : obj) o << " {" << x.first << " : " << x.second << "}" << ","; o << " }"; return o;}
template <class T>ostream &operator<<(ostream &o, const set<T>&obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr) o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;}
template <class T>ostream &operator<<(ostream &o, const multiset<T>&obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr) o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;}
template <class T>ostream &operator<<(ostream &o, const vector<T>&obj) {o << "{"; for (int i = 0; i < (int)obj.size(); ++i)o << (i > 0 ? ", " : "") << obj[i]; o << "}"; return o;}
template <class T, class U>ostream &operator<<(ostream &o, const pair<T, U>&obj) {o << "{" << obj.first << ", " << obj.second << "}"; return o;}
void print(void) {cout << endl;}
template <class Head> void print(Head&& head) {cout << head;print();}
template <class Head, class... Tail> void print(Head&& head, Tail&&... tail) {cout << head << " ";print(forward<Tail>(tail)...);}
template <class T> void chmax(T& a, const T b){a=max(a,b);}
template <class T> void chmin(T& a, const T b){a=min(a,b);}
void YN(bool flg) {cout << (flg ? "YES" : "NO") << endl;}
void Yn(bool flg) {cout << (flg ? "Yes" : "No") << endl;}
void yn(bool flg) {cout << (flg ? "yes" : "no") << endl;}

int main() {
	SPEED
	string S; cin >> S;
	int N = S.size();
	int K; cin >> K;
	auto R = multivector(N, 26,-1);
	auto L = multivector(N, 26,-1);
	vector<int> cnt(26,-1);
	for(int i = 0; i < N; ++i) {
		L[i] = cnt;
		cnt[S[i]-'a'] = i;
	}
	for(int a = 0; a < 26; ++a){
		cnt[a] = -1;
	}
	for(int i = N-1; 0 <= i; --i) {
		R[i] = cnt;
		cnt[S[i]-'a'] = i;
	}
	auto dp = multivector((K+2)*N*N,0);
	for(int i = 0; i < N; ++i) {
		for(int k = 0; k <= K; ++k) {
			dp[k*N*N+i*N+i] = 1;
		}
	}
	for(int i = 0; i+1 < N; ++i) {
		for(int k = 0; k <= K; ++k) {
			if(S[i]==S[i+1]) dp[k*N*N+i*N+i+1] = 2;
			else dp[k*N*N+i*N+i+1] = (k?2:0);
		}
	}
	for(int i = 0; i < N; ++i) {
		for(int j = 0; j + i < N; ++j){
			int l = j, r = j + i;
			for(int k = 0; k <= K; ++k) {
				//コストが発生しないとき
				for(int a = 0; a < 26; ++a){
					int x = L[l][a],y = R[r][a];
					if(x==-1||y==-1) continue;
					chmax(dp[k*N*N+x*N+y],dp[k*N*N+l*N+r]+2);
				}
				//コストが発生するとき
				if(0<=l-1&&r+1<N) chmax(dp[(k+1)*N*N+(l-1)*N+r+1],dp[k*N*N+l*N+r]+2);
			}
		}
	}
	int ans = 0;
	for(int i = 0; i < N; ++i) {
		for(int j = 0; j < N; ++j){
			for(int k = 0; k <= K; ++k) {
				chmax(ans,dp[k*N*N+i*N+j]);
			}
		}
	}
	cout << ans << endl;
    return 0;
}

#include <cstdio>
typedef long long ll;
inline ll rd(){
    ll x=0,p=1;
    char a=getchar();
    while((a<48||a>57)&&a!='-')a=getchar();
    if(a=='-')p=-p,a=getchar();
    while(a>47&&a<58)x=(x<<1)+(x<<3)+(a&15),a=getchar();
    return x*p;
}
const int N=2002;
const ll mod=1e9+7;
inline ll fpow(ll b,ll p,ll mod){
	ll ans=1,tmp=b;
	while(p){
		if(p&1)ans=ans*tmp%mod;
		tmp=tmp*tmp%mod;
		p>>=1;
	}
	return ans;
}
int n,k;
ll fac[N*N],inv[N*N],f[N][N];
inline ll C(int n,int m){
	return fac[n]*inv[m]%mod*inv[n-m]%mod;
}
int main(){
	fac[0]=1;
	n=rd(),k=rd();
	if(k==1)return puts("1"),0;
	for(int i=1;i<=4000000;i++)fac[i]=fac[i-1]*i%mod;
	f[0][0]=1;
	inv[4000000]=fpow(fac[4000000],mod-2,mod);
	for(int i=3999999;i>=0;i--)inv[i]=(inv[i+1]*(i+1))%mod;
	for(int i=1;i<=n;i++)
		for(int j=0;j<=i;j++){
			f[i][j]=f[i-1][j];
			if(j)f[i][j]=(f[i][j]+f[i][j-1]*(n-j+1)%mod*C(n-i+(n-j+1)*(k-1)-1,k-2)%mod)%mod;
		}
	printf("%lld\n",f[n][n]);
    return 0;
}


#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define sz(x) int(x.size())
#define show(x) {for(auto i: x){cout << i << " ";} cout << endl;}
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
    int N;
    cin >> N;
    vector<int> A(N);
    rep(i, N) cin >> A[i];
    vector<int> ans;
    for (int i = 0; i < N; i++) {
        auto it = lower_bound(ans.rbegin(), ans.rend(), A[i]);
        if (it == ans.rbegin()) ans.push_back(A[i]);
        else *(it-1) = A[i];
    }
    cout << sz(ans) << '\n';
    return 0;
}

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

int main () {
  int N;
  cin >> N;
  vector<int> A(N), B(N), C(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  for (int i = 0; i < N; i++) {
    cin >> B.at(i);
  }
  for (int i = 0; i < N; i++) {
    cin >> C.at(i);
  }
  sort(A.begin(), A.end());
  sort(B.begin(), B.end());
  sort(C.begin(), C.end());
  vector<int64_t> M(N);
  for (int i = 0; i < N; i++) {
    int l = -1;
    int r = N;
    while (r - l > 1) {
      int mid = (l + r) / 2;
      if (A.at(mid) < B.at(i)) {
        l = mid;
      }
      else {
        r = mid;
      }
    }
    M.at(i) = (l + 1);
  }
  int64_t ans = 0;
  for (int i = 0; i < N; i++) {
    int l = -1;
    int r = N;
    while (r - l > 1) {
      int mid = (l + r) / 2;
      if (C.at(mid) > B.at(i)) {
        r = mid;
      }
      else {
        l = mid;
      }
    }
    ans += (M.at(i) * (N - r));
  }
  cout << ans << endl;
}


#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

int main(){

    while(true){
    int n,m;
    cin >> n ;
    if(cin.eof()) break;
    cin >> m;

    vector<int> a(1000);
    vector<int> b(1000);
    vector<int> c(1000);
    vector<bool> u(1000);
    vector<int> ans(1000);
    fill(u.begin(),u.end(),true);
    for(int i=0;i<n;i++){
	cin >> a[i];
    };
    for(int i=0;i<m;i++){
	cin >> b[i];
    }
    for(int i=0;i<n;i++){
	for(int j=0;j<m;j++){
	    if(u[j] && a[i] <= b[j]){
		u[j] = false;
		ans[i]++;
	    }
	}
    }
    int res = -1,ansnum = 0;;
    for(int i=0;i<n;i++){
	if(res < ans[i]){
	    res = ans[i];
	    ansnum = i + 1;
	}
    }

    cout << ansnum << endl;
    }

    return 0;
}

#include <bits/stdc++.h>
#include <string>
#include <vector>

using namespace std;

int main(){

  int n,m,r,q,w=0;
  vector<int> a;
  vector<int> c;

    cin>>n>>m;

    for(int i=0; i<n; ++i){
      cin>>q;
      a.push_back(q);
      c.push_back(0);
    }

    for(int i=0; i<m; ++i){
      cin>>q;
      for(int k=0; k<n; ++k)
	if(a[k]<=q){
	  ++c[k];
	  break;
	}
    }
    q=0;
    for(int i=0; i<n; ++i){
      if(q < c[i]) q=c[i],w=i+1;
    }
    cout << w <<endl;

    //    cout<<endl;
    //   for(int i=0; i<n; ++i){
    //     cout<<a[i]<<"  "<<c[i]<<endl;
    //      }

  return 0;
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using pint = pair<int, int>;

int main() {
	string S;
	cin >> S;

	string ans = "WA";

	vector<int> up;
	for (int i = 0; i < S.size(); ++i) {
		if ('a' > S[i] || S[i] > 'z') {
			up.push_back(i);
		}
	}

	if (up.size() == 2 && up[0] == 0 && S[up[0]] == 'A') {
		if (2 <= up[1] && up[1] <= S.size() - 2 && S[up[1]] == 'C') {
			ans = "AC";
		}
	}

	cout << ans << endl;
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
int i,j,k,count1=0,count2=0;

int main(void){
    int ans=0;
    string s;
    cin>>s;
    if(s.length()%2==0){
        for(i=0;i<s.length();i+=2){
            if(s[i]=='h'&&s[i+1]=='i')count1++;
        }
    }
    if(count1==s.length()/2)ans=1;
    if(s.length()==1)ans=0;
    cout<<(ans? "Yes" : "No")<<endl;
}


#include <bits/stdc++.h>
// iostream is too mainstream
#include <cstdio>
// bitch please
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <cmath>
#include <iomanip>
#include <time.h>
#define dibs reserve
#define OVER9000 1234567890
#define ALL_THE(CAKE,LIE) for(auto LIE =CAKE.begin(); LIE != CAKE.end(); LIE++)
#define tisic 47
#define soclose 1e-8
#define chocolate win
// so much chocolate
#define patkan 9
#define ff first
#define ss second
#define abs(x) ((x < 0)?-(x):x)
#define uint unsigned int
#define dbl long double
#define pi 3.14159265358979323846
using namespace std;
// mylittledoge

typedef long long cat;

#ifdef DONLINE_JUDGE
	// palindromic tree is better than splay tree!
	#define lld I64d
#endif

void add(map<string, int> &M, string s) {
	for(int i = 0; i <= (int)s.length(); i++) M[s.substr(0, i)] = 0;
}

void add_edges(map<string, int> &M, vector< vector<int> > &G, string end) {
	ALL_THE(M, it) if(it->ff != end && G[it->ss].empty())
		for(int k = 0; k < 2; k++) G[it->ss].push_back(M[it->ff+(char)('0'+k)]);
}

int main() {
	cin.sync_with_stdio(0);
	cin.tie(0);
	cout << fixed << setprecision(10);
	string S;
	cin >> S;
	int N = S.length();
	map<string, int> M;
	add(M, "01");
	add(M, "000");
	add(M, "0011");
	add(M, "0010");
	add(M, "101");
	add(M, "1000");
	add(M, "1001");
	add(M, "111");
	add(M, "11000");
	add(M, "11001");
	add(M, "1101");
	int m = 0;
	ALL_THE(M, it) it->ss = m++;
	vector< vector<int> > G(m);
	G[M["01"]].push_back(M[""]);
	G[M["000"]].push_back(M["0"]);
	G[M["0011"]].push_back(M[""]);
	G[M["0010"]].push_back(M["00"]);
	G[M["101"]].push_back(M["1"]);
	G[M["1000"]].push_back(M["10"]);
	G[M["1001"]].push_back(M["10"]);
	G[M["11000"]].push_back(M["110"]);
	G[M["11001"]].push_back(M["110"]);
	G[M["1101"]].push_back(M["11"]);
	add_edges(M, G, "111");
	vector<string> V(m);
	ALL_THE(M, it) V[it->ss] = it->ff;

	cat mod = 1000000007;
	vector< vector<cat> > ans(N+1, vector<cat>(m, 0));
	ans[0][0] = 1;
	for(int i = 0; i < N; i++) for(int j = 0; j < m; j++) if(G[j].size() == 2)
		for(int k = 0; k < 2; k++) if(S[i] == '?' || S[i] == '0'+k) {
			string s = V[j] + (char)('0'+k);
			int id = M[s];
			while(G[id].size() == 1) id = G[id][0];
			ans[i+1][id] += ans[i][j];
			if(ans[i+1][id] >= mod) ans[i+1][id] -= mod;
		}
	cat ansF = 0, pw2 = 1;
	for(int i = N-1; i >= 0; i--) {
		if(S[i] == '?') pw2 = (pw2 * 2) % mod;
		ansF += ans[i][M["111"]] * pw2 % mod;
	}
	for(int i = 0; i < m; i++) if(G[i].size() != 1) {
		string s = V[i];
		int sum = 0;
		for(int j = 0; j < (int)s.length(); j++) sum += (s[j] == '1') ? 1 : -1;
		if(sum > 0) ansF += ans[N][i];
	}
	ansF %= mod;
	if(ansF < 0) ansF += mod;
	cout << ansF << "\n";

	// 01 -> .
	// 000 -> 0
	// 0011 -> .
	// 0010 -> 00
	// 101 -> 1
	// 1000 -> 10
	// 1001 -> 10
	// 111 -> WIN
	// 11000 -> 110
	// 11001 -> 110
	// 1101 -> 11
	return 0;}

// look at my code
// my code is amazing


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod=1e9+7;
ll cnt[66][2];
int main(){
	int n;cin>>n;
	for(int i=0;i<n;i++){
		ll a;cin>>a;
		int bit=0;
		while(a){
			if(a&1){
				cnt[bit][1]++;
			}else cnt[bit][0]++;
			a/=2;
			bit++;
		}
		for(int j=bit;j<=64;j++)cnt[j][0]++;
	}
	ll ans=0;
	for(int i=0;i<=62;i++){
		ll p=(1LL<<i);p%=mod;
		ll hano=cnt[i][0]*cnt[i][1];
		hano%=mod;
		ans+=(p*hano);
		ans%=mod;
	}
	cout<<ans;
	return 0;
}

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

typedef long long ll;
const int N = 2000006;
const ll MOD = 1000000007;

ll a[N];
int bts[66];
int stb[66];

int main(){
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

	int n;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}

	ll ans = 0;

	for(int i=n;i>0;i--){
		for(int j=0;j<61;j++){
			ll t = (1ll<<j);
			t %= MOD;
			if((a[i])&(1ll<<j)){
				ll b = stb[j];
				bts[j]++;
				ans += t*b;
				ans %= MOD;
			}else{
				ll b = bts[j];
				stb[j]++;
				ans += t*b;
				ans %= MOD;
			}
		}
	}

	cout<<ans;

	return 0;
}


#include<bits/stdc++.h>
#define rep(i,N) for(int i=0;i<(N);i++)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 1e12;
const int inf = 1e9;
typedef long long ll;
typedef pair<ll,int> P;
typedef set<int> S;
int main(){
    int n;
    cin>>n;
    int cnt = 0;
    if(n<3){
        cout<<0<<endl;
        return 0;
    }
    cnt = 0;
    while(n>=3){
        cnt++;
        n-=3;
    }
    cout<<cnt<<endl;
    return 0;
}

#include<iostream>
#include<algorithm>
#include<functional>
#include<cmath>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<deque>
using namespace std;
#define ll long long
const int mod = 1000000007;
const ll INF = 1000000000000000000;
const double PI = 3.14159265358979323846264338327950288;

int main()
{
	int N;
	cin >> N;
	cout << N / 3 << endl;
}


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

int main() {
  int x, y, z;
  cin >> x >> y >> z;
  printf("%d %d %d", z, x, y);
  cout << endl;

  return 0;
}

#include <bits/stdc++.h>

#define INF 0x3f3f3f3f
#define EPS 1e-6
using namespace std;
typedef long long LL;
const int maxn = 1e5 + 10;

int T;

int main()
{
    int a, b, c;
    while(cin >> a >> b >> c){
        cout << c << " " << a << " " << b << endl;
    }
    return 0;
}


#include <iostream>

using namespace std;

int order[4000];

bool search(int);

class Member {
public:
	Member() {sum = 0;}

	void add(long price, long num) {
		sum += (long long int)price*num;
	}

	bool milion()
	{
		if (sum >= 1000000) return true;
		else return false;
	}

private:
	long long int sum;
	int id;
};

bool search(int num)
{
	for (int i=0; i<4000; i++)
		if (order[i] == num ) return true;

	return false;

}

int main()
{
	int n, idx, memNum;
	long price, num;
	bool nothing;

	ios::sync_with_stdio();

	while (1) {
		Member *member = new Member[4000];
		memNum=0;
		nothing =true;

		for (int i=0; i<4000; i++) order[i] = -1;

		if (cin >> n, !n) break;
		for (int i=0; i<n; i++) {
			scanf("%d %ld %ld", &idx, &price, &num);
			member[idx].add(price, num);
			if (!search(idx)) order[memNum++] = idx;
		}

		for (int i=0; i<4000 && order[i] > 0; i++)
			if (member[order[i]].milion()) { cout << order[i] << endl; nothing = false; }
		if (nothing) cout << "NA" << endl;
	}
}

#include <iostream>
using namespace std;
typedef long long ll;
//vector<pair<bool, ll>> v(4001);
unsigned long long int a[4001];

int main(){
	int n, num, p, q;
	while(cin >> n, n){
		//fill(a, a+4001, 0);
		int f = 1;
		for(int i = 0;i < 4001;i++)a[i] = 0;
		for(int i = 0;i < n;i++){
			cin >> num >> p >> q;
			a[num] += p * q;
			if(a[num]>999999){
                cout << num << endl;
                f = 0;
            }
		}
		if(f)cout << "NA" <<endl;
		/*int f = 1;
		for(int i = 1;i <= 4000;i++)if(a[i] >= 1000000)cout << i << endl, f = 0;
		if(f)cout << "NA" << endl;*/
	}
	return 0;
}

/*
4
1001 2000 520
1002 1800 450
1003 1600 625
1001 200 1220
*/


#include<cstdio>
#include<iostream>
#include<cstring>
#define RG register
#define si short int
using namespace std;
const int N=101;
si n,m,x,y,ans,dp[N][N][N][N],sum[N][N];
char g[N][N];
template<typename I> inline void read(I &ot){
	I ch=getchar(), x=0, f=1;
	while(ch<'0' || ch>'9'){if(ch=='-') f=-1; ch=getchar();	}
	while(ch>='0' && ch<='9'){x=x*10+ch-'0'; ch=getchar();	}
	ot=x*f;}
template<typename I, typename... U> inline void read(I &x,U&... y){read(x); read(y...);}
template<typename I>inline I mx(const I&a,const I&b){return a>b ? a : b;}
template<typename I>inline I mi(const I&a,const I&b){return a<b ? a : b;}
template<typename I>inline void swp(I&a,I&b){I t=a; a=b; b=t;}
inline void cmx(si &a,si b){if(b>a) a=b;}
inline si s(int a,int b,int c,int d){
	if(a>c) swp(a,c); if(b>d) swp(b,d);
	return sum[c][d]-sum[c][b-1]-sum[a-1][d]+sum[a-1][b-1];
}
int main()
{
//	freopen("Salvage Robots.in","r",stdin);
	read(n,m);
	for(RG int i=1;i<=n;i++)
	{
		scanf("%s",g[i]+1);
		for(RG int j=1;j<=m;j++)
		{
			if(g[i][j]=='o') sum[i][j]=1;
			else if(g[i][j]=='E') x=i, y=j;
			sum[i][j]+=sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1];
		}
	}
	for(int i=x;i;i--)
	 for(int j=y;j;j--)
	  for(int k=x;k<=n;k++)
	   for(int l=y;l<=m;l++)
	   {
			ans=mx(ans,dp[i][j][k][l]);
			int li=l-y+1, ri=m-y+j, us=k-x+1, ds=n-x+i;
			if(i-1>=us) cmx(dp[i-1][j][k][l],dp[i][j][k][l]+s(i-1,mx(li,j),i-1,mi(ri,l)));
			if(j-1>=li) cmx(dp[i][j-1][k][l],dp[i][j][k][l]+s(mx(i,us),j-1,mi(k,ds),j-1));
			if(k+1<=ds) cmx(dp[i][j][k+1][l],dp[i][j][k][l]+s(k+1,mx(li,j),k+1,mi(ri,l)));
			if(l+1<=ri) cmx(dp[i][j][k][l+1],dp[i][j][k][l]+s(mx(i,us),l+1,mi(k,ds),l+1));
	   }
	cout<<ans<<endl;
	return 0;
}

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

#define MAX 102

int h;
int w;

char buf[MAX][MAX];

short dp[MAX][MAX][MAX][MAX];
pair<int, int> E;
int im[MAX][MAX];
int gt(int a, int b){
	if (a < 0 || b < 0){
		return 0;
	}
	return im[a][b];
}
int rect(int x1, int x2, int y1, int y2){
	return gt(x2, y2) + gt(x1 - 1, y1 - 1) - gt(x2, y1 - 1) - gt(x1 - 1, y2);
}
int dfs(int a, int b, int c, int d){
	if (a > b || c > d)return 0;
	return dp[a][b][c][d];
}
int main(){
	cin >> h >> w;
	for (int i = 0; i < h; i++){
		scanf("%s", buf[i]);
	}
	for (int i = 0; i < h; i++){
		for (int j = 0; j < w; j++){
			if (buf[i][j] == 'E'){
				E = make_pair(i, j);
			}
			if (buf[i][j] == 'o'){
				im[i][j]++;
			}
		}
	}
	for (int i = 0; i < h; i++){
		for (int j = 1; j < w; j++){
			im[i][j] += im[i][j - 1];
		}
	}
	for (int j = 0; j < w; j++){
		for (int i = 1; i < h; i++){
			im[i][j] += im[i - 1][j];
		}
	}
	int a;
	int b;
	int c;
	int d;
	short int can;
	short int add;
	for (int rng = 1; rng <= h; rng++){
		for (int rng2 = 1; rng2 <= w; rng2++){
			for (int i = 0; i + rng <= h; i++){
				for (int j = 0; j + rng2 <= w; j++){
					a = i;
				 b = a + rng - 1;
					c = j;
					d = j + rng2 - 1;
					{
						can = E.first + a + 1;
						if (a + 1 <= can&&can <= b){
							add = rect(can, can, max(c, E.second - (w - d) + 1), min(d, E.second + c));
							add += dfs(a + 1, b, c, d);
							dp[a][b][c][d] = add;
						}
					}

					//choose b
					{
					 can = E.first - (h - b);
						if (a <= can&&can <= b - 1){
						 add = rect(can, can, max(c, E.second - (w - d) + 1), min(d, E.second + c));
							add += dfs(a, b - 1, c, d);
							dp[a][b][c][d] = max(dp[a][b][c][d], add);
						}
					}
					//choose c
					{
						can = E.second + c + 1;
						if (c + 1 <= can&&can <= d){
							add = rect(max(a, E.first - (h - b) + 1), min(b, E.first + a), can, can);
							add += dfs(a, b, c + 1, d);
							dp[a][b][c][d] = max(dp[a][b][c][d], add);
						}
					}
					//choose d
					{
						can = E.second - (w - d);
						if (c <= can&&can <= d - 1){
							add = rect(max(a, E.first - (h - b) + 1), min(b, E.first + a), can, can);
							add += dfs(a, b, c, d - 1);
							dp[a][b][c][d] = max(dp[a][b][c][d], add);
						}
					}
				}
			}
		}
	}

	int ans=dfs(0, h - 1, 0, w - 1);
	cout << ans << endl;
	return 0;
}


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

int main(){
  int n;
  cin >> n;
  vector<int> a(n),b(n),asum(n+1,0),bsum(n+1,0);
  rep(i,n){
    cin >> a[i];
    asum[a[i]]++;
  }
  rep(i,n){
    cin >> b[i];
    bsum[b[i]]++;
  }
  rep(i,n+1){
    if(asum[i] + bsum[i] > n){
      cout << "No\n";
      return 0;
    }
  }
  rep(i,n){
    asum[i+1] += asum[i];
    bsum[i+1] += bsum[i];
  }
  int d = 0;
  rep(i,n+1){
    d = max(asum[i]-bsum[i-1],d);
  }
  cout << "Yes\n";
  rep(i,n){
    cout << b[(n+i-d)%n] << " ";
  }
  return 0;
}

#include <bits/stdc++.h>
#define maxn 200086

using namespace std;

int n;
int a[maxn], b[maxn];

int main(){
	scanf("%d", &n);
	for(int i = 1;i <= n;i++) scanf("%d", &a[i]);
	for(int i = 1;i <= n;i++) scanf("%d", &b[i]);
	reverse(b + 1, b + 1 + n);
	int l = n, r = 1;
	for(int i = 1;i <= n;i++){
		if(a[i] == b[i]) l = min(l, i), r = max(r, i);
	}
	for(int i = 1;i <= n && l <= r;i++){
		if(a[i] != a[l] && b[i] != a[l]) swap(b[i], b[l++]);
	}
	if(l <= r) return printf("No"), 0;
	puts("Yes");
	for(int i = 1;i <= n;i++) printf("%d ", b[i]);
}


#include<iostream>
#include<vector>
#include<set>
#include<map>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
#include<climits>
#include<string.h>
using namespace std;

int main(){
    int m,n1,n2;
    while(cin>>m>>n1>>n2,m){
        int p[202];
        vector<pair<int,int> >v;
        for(int i=1;i<=m;i++){
            cin>>p[i];
            if(i-1>=n1&&i-1<=n2){
                v.push_back(make_pair(p[i-1]-p[i],i-1));
            }
        }
        sort(v.begin(),v.end());
        cout<<v[v.size()-1].second<<endl;
    }
}

#include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;

int main()
{
    for(;;){
        char c;
        vector<int> len(10);
        for(int i=0; i<10; ++i){
            if(!(cin >> len[i] >> c))
                return 0;
        }
        int v1, v2;
        cin >> v1 >> c >> v2;

        int sumLen = accumulate(len.begin(), len.end(), 0);
        int tmp = 0;
        for(int i=0; i<10; ++i){
            tmp += len[i];
            if(sumLen * v1 <= tmp * (v1 + v2)){
                cout << (i+1) << endl;
                break;
            }
        }
    }
}


#include <bit>
#include <bitset>
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <sstream>
#include <iostream>
#include <set>
#include <map>
#include <vector>
#include <math.h>
#include <stdlib.h>
#include <queue>
#include <stack>

typedef long double ld;
typedef long long ll;

using namespace std;

#define MAXN (1 << 20)

int a[MAXN], b[MAXN], c[MAXN];

int main() {
	int na, nb, m;
	cin >> na >> nb >> m;
	for (int i = 1; i <= na; i++) {
		cin >> a[i];
	}
	for (int i = 1; i <= nb; i++) {
		cin >> b[i];
	}
	int ret = MAXN;
	for (int i = 1; i <= m; i++) {
		int x, y, c;
		cin >> x >> y >> c;
		int cur = a[x] + b[y] - c;
		ret = min(ret, cur);
	}
	sort(a + 1, a + na + 1);
	sort(b + 1, b + nb + 1);
	ret = min(ret, a[1] + b[1]);
	cout << ret << endl;
	return 0;
}

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=100005;
int n,m,v,p,a[maxn],ans;
ll b[maxn];
inline int cmp1(int x,int y){
	return x>y;
}
inline int read(){
	int res=0,f_f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-') f_f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9') res=(res<<3)+(res<<1)-'0'+ch,ch=getchar();
	return res*f_f;
}
int main(){
	n=read(),m=read(),v=read(),p=read(),ans=p;
	for (int i=1;i<=n;i++){
		a[i]=read();
	}
	sort(a+1,a+n+1,cmp1);
	for (int i=1;i<=n;i++){
		b[i]=b[i-1]+a[i];
	}
	for (int i=p+1;i<=n;i++){
		if(a[i]+m<a[p]) continue;
		ll delta=1ll*max(0,v-(p-1)-(n-i+1))*m;
		if(delta==0){
			ans++;
			continue;
		}
		ll w=1ll*m*(i-p)-(b[i-1]-b[p-1]-1ll*a[i]*(i-p));
		if(w>=delta) ans++;
	}
	printf("%d\n",ans);
	return 0;
}


// time-limit: 2000
// 23
#include <bits/stdc++.h>
using namespace std;
#define coutc "\033[48;5;196m\033[38;5;15m"
#define endc "\033[0m"
#define endl '\n'
#define M(_1, _2, _3, _4, NAME, ...) NAME
#define rep(...) \
  M(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)
#define rep4(_, x, n, s) \
  for (int _ = x; (s < 0) ? _ > n : _ < n; _ += s)
#define rep3(_, x, n) rep4(_, x, n, (x < n ? 1 : -1))
#define rep2(_, n) rep3(_, 0, n)
#define rep1(n) rep2(i, n)
#define fi first
#define se second
#define pb push_back
#define pii pair<int, int>
#define all(x) x.begin(), x.end()
#define len(x) int(x.size())
#define lli __int128_t
#define li long long int
#define ld long double
#ifdef DEBUG
// 3
ostream &operator<<(ostream &o, pair<auto, auto> p) {
  return o << "(" << p.fi << ", " << p.se << ")";
}
// 4
ostream &operator<<(ostream &o, vector<auto> v) {
  rep(i, len(v)) o << setw(7) << right << v[i];
  return o << endc << endl << coutc;
}
// 4
ostream &operator<<(ostream &o, map<auto, auto> m) {
  for (auto &kv : m) o << kv;
  return o;
}
// 7
void debug(const auto &e, const auto &... r) {
  cout << coutc << e;
  ((cout << " " << r), ..., (cout << endc << endl));
}
#else
#define debug(...)
#endif

int sign(li x) { return x < 0 ? -1 : x > 0; }

//         a / b
li ceil(li a, li b) {
  if (sign(a) == sign(b) && a % b) return a / b + 1;
  else return a / b;
}

void _main(int tc) {
    int a, b;
    cin >> a >> b;

    // b = a + (a - 1) * X
    // X = (b - a) / (a - 1)
    // is ceiling because we need to assure that with X pipes we complete "b - a" lines.
    // we add one because after fullfilling "b - a" we still need to complete with "a" lines.
    cout << ceil(b - a, a - 1) + 1 << endl;
}
// 7
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  _main(0), exit(0);
  int tc;
  cin >> tc;
  rep(i, tc) _main(i + 1);
}

#include <bits/stdc++.h>
#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 a, b;
    cin >> a >> b;
    int count = 0;
    int ans = 1;
    for (int i = 0; b > ans; i++)
    {
        ans += (a - 1);
        count++;
    }
    cout << count << endl;
    return 0;
}


#include <iostream>
using namespace std;

int main()
{
	int n, a, b, count = 0, pointX = 0, pointY = 0;
	cin >> n;
	do {
		cin >> a >> b;
		if (a > b) {
			pointX += (a + b);
		}
		else if (a < b) {
			pointY += (a + b);
		}
		else {
			pointX += a;
			pointY += b;
		}
		count++;
		if (count == n) {
			cout << pointX << " " << pointY << endl;
			pointX = pointY = count = 0;
			cin >> n;
		}
	} while (n != 0);
	return 0;
}

#include<stdio.h>
#include<iostream>
using namespace std;
int main(){
int card[20000],i,j,a=0,b=0,c=1,n;
cin>>n;
while(n!=0){
for(i=0;i<2*n;i++){
cin>>card[i];
}
for(i=0;i<2*n;i=i+2){
if(card[i]>card[c]){
a=a+card[i]+card[c];
}
else if (card[i]==card[c]){
a=a+card[i];
b=b+card[c];
}
else{
b=b+card[i]+card[c];
}
c=c+2;
}
cout<<a<<" "<<b<<endl;
c=1;
a=0;
b=0;
cin>>n;
}
return 0;
}


#include<bits/stdc++.h>
using namespace std;
struct UnionFind{
  vector<int> r,p;
  UnionFind(){}
  UnionFind(int size){init(size);}
  void init(int size){
    r.resize(size,0);
    p.resize(size,0);
    for(int i=0;i<size;i++) r[i]=1,p[i]=i;
  }
  int find(int x){
    return (x==p[x]?x:p[x]=find(p[x]));
  }
  bool same(int x,int y){
    return find(x)==find(y);
  }
  void unite(int x,int y){
    x=find(x);y=find(y);
    if(x==y) return;
    if(r[x]<r[y]) swap(x,y);
    r[x]+=r[y];
    p[y]=x;
  }
};
struct edge{
  int from,to,cost;
  edge(){}
  edge(int from,int to,int cost):from(from),to(to),cost(cost){}
  bool operator<(const edge& e) const{
    return cost<e.cost;
  }
};
int kruskal(int N,vector<edge> edges){
  int res=0;
  sort(edges.begin(),edges.end());
  UnionFind uf(N+1);
  for(int i=0;i<(int)edges.size();i++){
    edge e=edges[i];
    if(!uf.same(e.from,e.to)){
      res+=e.cost;
      uf.unite(e.from,e.to);
    }
  }
  return res;
}
int main(){
  int N,M;
  cin>>N>>M;
  vector<edge> edges(M);
  for(int i=0;i<M;i++)
    cin>>edges[i].from>>edges[i].to>>edges[i].cost;
  cout<<kruskal(N,edges)<<endl;
  return 0;
}

#include "bits/stdc++.h"

#define REP(i,num) for(int i=0;i<(num);++i)
#define ALL(c) c.begin(),c.end()
#define LOOP(i) while(i--)
#define PRINTALL(c) for(auto& x:c){cout<<x<<' ';}cout<<endl;
#define PAIRCOMP(c,comp) [](const pair<ll,ll>& lhs,const pair<ll,ll>& rhs){return lhs.c comp rhs.c;}

using namespace std;
using ll = long long;

constexpr ll atcoder_mod = 1e9+7;

template<typename T=int>
T in(){T x; cin >> x; return (x);}
template<typename T=int,typename C=vector<T>>
C vecin(int N){C x(N);REP(i,N){x[i]=in<T>();}return move(x);}

void vout(){cout << endl;}
template<typename Head,typename... Tail>
void vout(Head&& h,Tail&&... t){cout << ' ' << h;vout(forward<Tail>(t)...);}
void out(){cout << endl;}
template<typename Head,typename... Tail>
void out(Head&& h,Tail&&... t){cout << h;vout(forward<Tail>(t)...);}

class ConnectNodeInfo{
	vector<vector<pair<ll,ll>>> graph;
public:
	ConnectNodeInfo(int node_num){
		graph.resize(node_num);
	}
	void AddNonDirectionalConnection(ll u,ll v,ll w){
		graph[u].emplace_back(v,w);
		graph[v].emplace_back(u,w);
	}
	void AddDirectionalConnection(ll u,ll v,ll w){
		graph[u].emplace_back(v,w);
	}
	vector<pair<ll,ll>>& operator[](ll index){
		return graph[index];
	}
	size_t size(){return graph.size();}
};

class RootedNode{
public:
	vector<ll> parent;
	ll depth,dist;
	RootedNode():depth(0),dist(0){}
};

class RootedTree{
	int parent_size;
	vector<int> pre,low;
	int pre_count{0};

	void DFS(ConnectNodeInfo& connection,ll child,ll parent,ll dep,ll dist){
		pre[child] = low[child] = pre_count++;

		node[child].parent[0] = parent;
		node[child].depth = dep;
		node[child].dist = dist;

		bool isArticulation = false;
		int ct = 0;

		for(int i = 0;i < connection[child].size();i++){
			ll next_child = connection[child][i].first;
			if(pre[next_child]<0){
				ct++;
				DFS(connection,next_child,child,dep+1,dist+connection[child][i].second);
				low[child] = min(low[next_child],low[child]);
				if (~parent && pre[child] <= low[next_child]) isArticulation = true;
				if (pre[child]<low[next_child]) bridge.emplace_back(min(child,next_child),max(child,next_child));
			}
			else if(next_child != parent){
				low[child] = min(low[child],pre[next_child]);
			}
		}

		if(parent==-1 && ct>1) isArticulation = true;
		if(isArticulation) articulation.push_back(child);
	}
public:
	vector<RootedNode> node;
	vector<pair<ll,ll>> bridge;
	vector<ll> articulation;

	RootedTree(ConnectNodeInfo& connection){
		auto connection_size = connection.size();
		parent_size = 1;
		while((1LL<<parent_size)<connection_size) parent_size++;

		node.resize(connection_size);
		pre.resize(connection_size,-1);
		low.resize(connection_size);
		for(auto& x:node) x.parent.resize(parent_size,-1);
		DFS(connection,0,-1,0,0);
		for(int k=0;k<parent_size-1;k++){
			for(int v=0,ev=connection_size;v<ev;v++){
				if(node[v].parent[k] < 0) node[v].parent[k+1] = -1;
				else node[v].parent[k+1] = node[node[v].parent[k]].parent[k];
			}
		}
	}
	ll FindLCA(ll u,ll v){
		if(node[u].depth > node[v].depth) swap(u,v);
		for(int k=0;k<parent_size;k++){
			if((node[v].depth - node[u].depth) & (1LL<<k)){
				v = node[v].parent[k];
			}
		}
		if(u==v) return u;
		for(int k=parent_size-1;k>=0;k--){
			if(node[u].parent[k] != node[v].parent[k]){
				u = node[u].parent[k];
				v = node[v].parent[k];
			}
		}
		return node[u].parent[0];
	}
};

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

	int N=in(),M=in();
	ConnectNodeInfo connect(N);
	REP(i,M){
		int u=in(),v=in();
		connect.AddNonDirectionalConnection(u,v,1);
	}
	RootedTree tree(connect);
	sort(ALL(tree.articulation));
	for(auto& x:tree.articulation){
		out(x);
	}
	return 0;
}


#include<iostream>
#include<algorithm>
#include<functional>
#include<cmath>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<deque>
using namespace std;
#define ll long long
const int mod = 1000000007;
const int MOD = 998244353;
const ll INF = 1000000000000000000;

int p[200010];
double sum[200010];
int main()
{
	int N, K;
	cin >> N >> K;
	for (int i = 0; i < N; i++) cin >> p[i];
	for (int i = 0; i < N; i++) {
		sum[i + 1] = sum[i] + (double)(p[i] * (p[i] + 1) / 2) / p[i];
	}
	double ans = 0.0;
	for (int i = 0; i < N - K + 1; i++) {
		ans = max(ans, sum[i + K] - sum[i]);
	}
	printf("%.10f\n", ans);
}

#include <bits/stdc++.h>
using namespace std;
#define pr(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
#define all(v) (v).begin(), (v).end()
#define sz(v) (int)(v).size()
#define mod 1000000007 // ((a- b)% mod+ mod)% mod

void solve() {
        int n, k; scanf("%d%d", &n, &k);
        vector<int> v, csum(n+ 1);

        for (int i= 0; i< n; i++) {
                int x; scanf("%d", &x);
                x++;
                v.push_back(x);
        }

        for (int i= 1; i<= n; i++) csum[i]= csum[i- 1]+ v[i- 1];

        double an= 0;

        for (int i= 1; i<= n- k+ 1; i++) {
                an= max(an, (double)csum[i+ k- 1]- csum[i- 1]);
        }
        an/= 2;
        printf("%0.10f", an);
}

int32_t main(){
//ios::sync_with_stdio(0), cin.tie(NULL), cout.tie(NULL);
        int T= 1;
        //cin >> T;
        while ( T-- ) {
                solve();
        }
return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 300 + 10;
typedef long long ll;
const ll mod = 998244353;
int tot, K;
char s[maxn];
int a[maxn];
ll f[maxn][maxn][maxn];
ll ans;
inline ll norm(const ll & x) { return x >= mod ? x - mod : x;}
int main()
{
	scanf("%s%d", s + 1, &K);
	K = min(K, 300);
	int len = strlen(s + 1);
	reverse(s + 1, s + len + 1);
	int cur = 0;
	for(int i = 1; i <= len; ++i)
		if(s[i] == '0')
		{
			a[++tot] = cur;
			cur = 0;
		}
		else
			++cur;
	a[++tot] = cur;
	f[0][0][0] = 1;
	for(int i = 1; i <= tot; ++i)
		for(int k = 0; k <= K; ++k)
		{
			for(int j = k; ~j; --j)
				f[i][j][k] = norm(f[i - 1][j][k] + f[i][j + 1][k]);
			for(int j = 0; j <= k; ++j)
			{
				/*
				for(int jd = j; jd <= k; ++jd)
					f[i][j][k] = (f[i][j][k] + f[i - 1][jd][k]) % mod;
				*/
				int lim = min(j, a[i]);
				for(int jd = 1; jd <= lim; ++jd)
					f[i][j][k] = norm(f[i][j][k] + f[i - 1][j - jd][k - jd]);
			}
		}
	for(int k = 0; k <= K; ++k)
		ans = norm(ans + f[tot][0][k]);
	cout << ans << endl;
	return 0;
}

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

int main() {
  int N,L;
int result = 0;
  int mini = 10000;
  cin >> N >> L;
  for(int a = 0;a < N;a++){
    result += a + L;
    if(abs(mini) > abs(a + L))mini = a + L;

  }
  cout << result - mini << endl;
}


#include <iostream>
#include <algorithm>
#include <iomanip>
#include <string>
#include <vector>
#include <math.h>
#include <queue>
#include <deque>
#include <stack>
#include <map>
const int MOD = 1e9+7;
#define PI 3.14159265359
typedef long long ll;
using namespace std;

int main(){
	int n;
	string s;
	cin >> n >> s;
	vector<int> sumE(n+1), sumW(n+1);
	sumE[0] = 0, sumW[0] = 0;

	for (int i = 1; i <= n; i++){
		sumE[i] = sumE[i-1];
		sumW[i] = sumW[i-1];
		if (s[i-1] == 'E'){
			sumE[i] += 1;
		}else{
			sumW[i] += 1;
		}
	}
	int ans = MOD;
	for (int i = 1; i <= n; i++){
		int temp1 = sumW[i-1];
		int temp2 = sumE[n] - sumE[i];
		ans = min(ans, temp1 + temp2);
	}

	cout << ans << endl;
	return 0;
}

#include<bits/stdc++.h>
#define ford(i,a,b) for(int i=(a);i>=b;i--)
#define rep1(i,a,b) for(int i=(a);(i)<=(b);(i)++)
#define rep(i,a,b) for(int i=(a);(i)<(b);(i)++)
#define ll long long
#define N 300005
#define pql priority_queue<ll>
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define rev(a,n) reverse(a,a+n)
using namespace std;
int n,ans=300005,l[N],r[N];
char s[N];
int main() {
	cin>>n;
	rep1(i,1,n)cin>>s[i];
	rep1(i,2,n){
		l[i]=l[i-1]+(s[i-1]=='W');
	}
	ford(i,n-1,1){
		r[i]=r[i+1]+(s[i+1]=='E');
	}
	rep1(i,1,n){
		if(ans>l[i]+r[i]){
			ans=l[i]+r[i];
		}
	}
	cout<<ans;
	return 0;
}


#include<iostream>
#include<cstdio>
using namespace std;
bool three(int x){
  if(x%3==0)return true;

  while(x>0){

    if(x%10==3)return true;
    x/=10;
  }
  return false;
}
int main(){

  int n;

  cin>>n;

  for(int i=1;i<=n;i++){

    if(three(i))cout<<" "<<i;

  }
cout<<endl;

  return 0;
}

#include <iostream>
#include <string>

using namespace std;

string S;
int N;

int main(void)
{
	cin >> S;
	N = S.length();
	int offset = 0;
	int result = 0;
	while(offset < N)
	{
		if(S[offset] == 'O')
		{
			int of = offset;
			int c_O = 0;
			while(S[of] == 'O')
			{
				of++;
				c_O++;
			}
			bool OK = true;
			for(int i = offset - 1;i >= offset - c_O;i--)
			{
				if(i >= 0)
				{
					if(S[i] != 'J')
					{
						OK = false;
						break;
					}
				}
				else
				{
					OK = false;
					break;
				}
			}
			for(int i = of;i < of + c_O;i++)
			{
				if(i < N)
				{
					if(S[i] != 'I')
					{
						OK = false;
						break;
					}
				}
				else
				{
					OK = false;
					break;
				}
			}
			if(OK)
			{
				result = max(result,c_O);
			}
			offset = of;
		}
		else
			offset++;
	}
	cout << result << endl;
	return 0;
}


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

typedef vector <bool> VB;
typedef vector <VB> VVB;

int main( void )
{
	int n, m;
	while ( cin >> n >> m && n && m )
	{
		VVB G( n+1, VB( n+1, false ) );
		for ( int i = 0; i < m; i++ )
		{
			int a, b; // a-bツづ債友ツ達
			cin >> a >> b;
			G[a][b] = G[b][a] = true;
		}

		VB F( n+1, false );
		for ( int i = 0; i <= n; i++ )
		{
			if ( G[1][i] )
			{
				F[i] = true;
			}
		}

		VB R( n+1, false );
		for ( int i = 0; i <= n; i++ )
		{
			if ( F[i] == false ) continue;
			for ( int j = 0; j <= n; j++ )
			{
				if ( j == 1 ) continue;
				if ( G[i][j] )
				{
					R[j] = true;
				}
			}
		}

		int answer = 0;
		for ( int i = 0; i <= n; i++ )
		{
			if ( F[i] || R[i] )
			{
				answer++;
			}
		}

		cout << answer << endl;

	}
	return 0;
}

#include<iostream>
#include<algorithm>

using namespace std;

int main()
{
	int friends[500];
	int n,m;
	pair<int, int> student[10000];
	int result[100];
	int I = 0;
	for (int i = 0;; i++)
	{
		cin >> n;
		cin >> m;
		if (m == 0 && n == 0)
		{
			break;
		}

		for (int a = 0; a < 500; a++)
		{
			friends[a] = 0;
			student[a].first = 0;
			student[a].second = 0;
		}

		for (int a = 0; a < m; a++)
		{
			cin >> student[a].first >> student[a].second;
			student[a].first--;
			student[a].second--;
		}

		sort(student, student + m);

		friends[0] = 1;

		for (int a = 0; a < m; a++)
		{
			if (student[a].first == 0)
			{
				for (int b = 0; b < m; b++)
				{
					if (student[b].first == student[a].second)
					{
						friends[student[b].second] = 1;
					}
					if (student[b].second == student[a].second)
					{
						friends[student[b].first] = 1;
					}
				}
				friends[student[a].second] = 1;
			}
		}

		result[i] = -1;
		for (int a = 0; a < n; a++)
		{
			if (friends[a] == 1)
			{
				result[i]++;
			}
		}
		I++;
	}

	for (int a = 0; a < I;a++)
	{
		cout << result[a] << endl;
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll MOD=1000000007;
const long double PI=3.1415926535;

int main() {
  ll N;
  cin>>N;
  ll a[N];
  for(int i=0;i<N;i++){
    cin>>a[i];
  }
  ll ans=0,cnt=0,c=1;
  if(a[0]!=0){
    c=0;
  }
  for(int i=0;i<N;i++){
    if(i==N-1){
      ans+=a[i];
    }
    else if(a[i]+1==a[i+1]){
      cnt++;
    }
    else if(a[i]+1<a[i+1]){
      c=0;
      break;
    }
    else{
      ans+=a[i];
    }
  }
  if(c){
    cout<<ans;
  }
  else{
    cout<<-1;
  }
}

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

#define V(a) a.begin(), a.end()
typedef vector<int> vi;

int main()
{
	vi a[2], C;

	for (auto &i : a) {
		int n;
		cin >> n;
		i = vi(n);
		for (int &j : i)
			cin >> j;
		sort(V(i));
	}

	set_intersection(V(a[0]), V(a[1]), inserter(C, C.end()));

	cout << C.size() << endl;
}


#include <bits/stdc++.h>

using namespace std;

typedef unsigned int ui;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef long double ld;

const int inf = 1e9;
const ll inf64 = 1e18;

const int mod = 998244353;

inline int _mul(int a, int b) {
    return (1ll * a * b) % mod;
}

inline int _sum(int a, int b) {
    return a + b < mod ? a + b : a + b - mod;
}

inline int _sub(int a, int b) {
    return a >= b ? a - b : a - b + mod;
}

inline void _inc(int &x, int y) {
    if ((x += y) >= mod) {
        x -= mod;
    }
}

inline void _dec(int &x, int y) {
    if ((x -= y) < 0) {
        x += mod;
    }
}

inline int _binpow(int x, int p) {
    int res = 1;
    int tmp = x;
    while (p > 0) {
        if (p & 1) {
            res = _mul(res, tmp);
        }
        tmp = _mul(tmp, tmp);
        p >>= 1;
    }
    return res;
}

const int N = 3e6 + 5;

int fact[N];
int rfact[N];

void precalc() {
    fact[0] = 1;
    for (int i = 1; i < N; i++) {
        fact[i] = _mul(fact[i - 1], i);
    }
    rfact[N - 1] = _binpow(fact[N - 1], mod - 2);
    for (int i = N - 2; i >= 0; i--) {
        rfact[i] = _mul(rfact[i + 1], i + 1);
    }
}

inline int CNK(int n, int k) {
    if (n < k) {
        return 0;
    }
    return _mul(fact[n], _mul(rfact[n - k], rfact[k]));
}

inline int F(int n, int k) {
    return CNK(n + k - 1, k);
}

int main() {

    precalc();

#ifdef debug
    freopen("input.txt", "r", stdin);
#endif

    int n, m;

    cin >> n >> m;

    int res = 0;

    for (int i = m & 1; i <= m; i += 2) {
        _inc(
            res,
            _mul(
                CNK(n, i),
                F(n, (3 * m - i) / 2)
            )
        );
    }

    _dec(
        res,
        _mul(
            n,
            F(n, m - 1)
        )
    );

    cout << res << "\n";

    return 0;
}

#include <bits/stdc++.h>

#define debug(...) fprintf(stderr, __VA_ARGS__)

#ifndef AT_HOME
#define getchar() IO::myGetchar()
#define putchar(x) IO::myPutchar(x)
#endif

namespace IO {
	static const int IN_BUF = 1 << 23, OUT_BUF = 1 << 23;

	inline char myGetchar() {
		static char buf[IN_BUF], *ps = buf, *pt = buf;
		if (ps == pt) {
			ps = buf, pt = buf + fread(buf, 1, IN_BUF, stdin);
		}
		return ps == pt ? EOF : *ps++;
	}

	template<typename T>
	inline bool read(T &x) {
		bool op = 0;
		char ch = getchar();
		x = 0;
		for (; !isdigit(ch) && ch != EOF; ch = getchar()) {
			op ^= (ch == '-');
		}
		if (ch == EOF) {
			return false;
		}
		for (; isdigit(ch); ch = getchar()) {
			x = x * 10 + (ch ^ '0');
		}
		if (op) {
			x = -x;
		}
		return true;
	}

	inline int readStr(char *s) {
		int n = 0;
		char ch = getchar();
		for (; isspace(ch) && ch != EOF; ch = getchar())
			;
		for (; !isspace(ch) && ch != EOF; ch = getchar()) {
			s[n++] = ch;
		}
		s[n] = '\0';
		return n;
	}

	inline void myPutchar(char x) {
		static char pbuf[OUT_BUF], *pp = pbuf;
		struct _flusher {
			~_flusher() {
				fwrite(pbuf, 1, pp - pbuf, stdout);
			}
		};
		static _flusher outputFlusher;
		if (pp == pbuf + OUT_BUF) {
			fwrite(pbuf, 1, OUT_BUF, stdout);
			pp = pbuf;
		}
		*pp++ = x;
	}

	template<typename T>
	inline void print_(T x) {
		if (x == 0) {
			putchar('0');
			return;
		}
		static int num[40];
		if (x < 0) {
			putchar('-');
			x = -x;
		}
		for (*num = 0; x; x /= 10) {
			num[++*num] = x % 10;
		}
		while (*num){
			putchar(num[*num] ^ '0');
			--*num;
		}
	}

	template<typename T>
	inline void print(T x, char ch = '\n') {
		print_(x);
		putchar(ch);
	}

	inline void printStr_(const char *s, int n = -1) {
		if (n == -1) {
			n = strlen(s);
		}
		for (int i = 0; i < n; ++i) {
			putchar(s[i]);
		}
	}

	inline void printStr(const char *s, int n = -1, char ch = '\n') {
		printStr_(s, n);
		putchar(ch);
	}
}
using namespace IO;

const int N = 2500005, P = 998244353;

int n, m;
int fac[N], inv[N];

int qpow(int a, int b) {
	int s = 1;
	for (; b; b >>= 1) {
		if (b & 1) {
			s = 1ll * s * a % P;
		}
		a = 1ll * a * a % P;
	}
	return s;
}

void init(int n) {
	fac[0] = 1;
	for (int i = 1; i <= n; ++i) {
		fac[i] = 1ll * fac[i - 1] * i % P;
	}
	inv[n] = qpow(fac[n], P - 2);
	for (int i = n; i; --i) {
		inv[i - 1] = 1ll * inv[i] * i % P;
	}
}

int C(int n, int m) {
	if (m < 0 || m > n) {
		return 0;
	}
	return 1ll * fac[n] * inv[m] % P * inv[n - m] % P;
}

int main() {
	read(n), read(m);
	init(n + 3 * m);
	int ans = 0;
	for (int i = m & 1; i <= n && i <= m; i += 2) {
		int S = (3 * m - i) >> 1, s = 0;
		for (int j = 0; j <= i && m * j <= S; ++j) {
			for (int k = 0; k <= n - i && m * j + (m + 1) * k <= S; ++k) {
				int t = 1ll * C(i, j) * C(n - i, k) % P * C(S - m * j - (m + 1) * k + n - 1, n - 1) % P;
				if ((j + k) & 1) {
					s = (s + P - t) % P;
				} else {
					s = (s + t) % P;
				}
			}
		}
		ans = (ans + 1ll * s * C(n, i)) % P;
	}
	print(ans);
}


/*
ID: skipian1
PROB:
LANG: C++11
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF = 2000000000
#define sz(a) int((a).size())
#define pb push_back
#define mp make_pair
#define all(c) (c).begin(),(c).end()
#define X first
#define Y second

ll MOD = 1e9+7;

int main() {
  ll n;
  cin >> n;
  string str;
  cin >> str;
  bool paren[2*n];
  for (ll i = 0; i < 2*n; i++) {
    if (i == 0) {
      if (str.at(i) == 'W') {
	cout << 0 << "\n";
	return 0;
      }
      paren[0] = 0;
    } else {
      if (str.at(i) != str.at(i-1)) {
	paren[i] = paren[i-1];
      } else {
	paren[i] = !paren[i-1];
      }
    }
  }
  ll parens = 0;
  for (ll i = 0; i < 2*n; i++) {
    if (paren[i]) parens--;
    else parens++;
    if (parens < 0) {
      cout << 0 << "\n";
      return 0;
    }
  }
  if (parens != 0) {
    cout << 0 <<  "\n";
    return 0;
  }
  parens = 0;
  ll ans = 1;
  ll ind = 0;
  for (ll i = 2*n-1; i >= 0; i--) {
    if (paren[i]) parens++;
    else {
      ans *= parens-ind;
      ind++;
      ans %= MOD;
    }
  }
  for (ll i = 1; i <= n; i++) {
    ans *= i;
    ans %= MOD;
  }
  cout << ans << "\n";
}

#include <bits/stdc++.h>
#define rep(i, n) for(long long int i = 0; i < n; i++)
#define _rep(i, m, n) for(long long int i = m; i < n; i++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const ll mod = 1000000007;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

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);
}
using Graph = vector<vector<int>>;
Graph G;

int main(){
    int d; cin >> d;
    ll g; cin >> g;
    vector<ll> p(d);
    vector<ll> c(d);
    rep(i, d) cin >> p[d - 1 - i] >> c[d - 1 - i];

    int min_count = 100000000;
    int count, sum;
    bool flag;
    for(int bit = 0; bit < (1 << d); bit++){
        flag = false;
        count = 0;
        sum = 0;
        for(int i = 0; i < d; i++){
            if(bit & (1 << i)){
                int num;
                if((g - sum) > (d - i) * 100 * p[i] + c[i]){
                    sum += (d - i) * 100 * p[i] + c[i];
                    count += p[i];
                }else{
                    num = min(((g - sum) + ((d - i) * 100) - 1) / ((d - i) * 100), p[i]);
                    count += num;
                    flag = true;
                }
            }
            if(flag) break;
        }
        //cout << count << endl;
        if(flag) min_count = min(count, min_count);
    }
    cout << min_count << endl;
}


#include <bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, x ,n) for(int i = x; i < n; i++)
#define repr(i, n) for(int i = n; i >= 0; i--)
#define INF 2e9
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
int main(){
    int n;
    cin >> n;
    int ans = 111;
    rep(i,n){
        if(ans >= n){
            cout << ans;
            return 0;
        }
        ans +=111;
    }
}

//#pragma GCC optimize "trapv"
#include<bits/stdc++.h>
#define  all(v)            v.begin(), v.end()
#define  sz(x)             (int)x.size()
#define  debug(x)         cout << '>' << #x << ':' << x << endl;
#define  rep(i,n)         for(int i = 0; i < n; i++)
#define  reps(i,n)        for(int i = 1; i<=n ; i++)
#define  make_unique(v)   sort(all(v));v.erase(unique(all(v)), v.end());
#define  rot(v)           rotate(v.begin(), v.begin() + k, v.end());
#define  rmv(v,a)         v.erase(remove(v.begin(),v.end(), a), v.end());
#define  max(x,y)         ((x>y)?x:y)
#define  min(x,y)         ((x<y)?x:y)
#define  pb               push_back
#define  eb               emplace_back
#define  mp               make_pair
#define  endl             '\n'
#define  fi               first
#define  se               second
#define  ajs(a)           (a<0)?(-1*a):a
using namespace std;
typedef long long          ll;
// use count() instead of find() in sets.
const ll MOD = 1e9 + 7;
const double PI  = acos(-1.0);
const double EPS = (1e-10);
const int dx4[4] = {0, 1, 0, -1};
const int dy4[4] = { -1, 0, 1, 0};
const int dx8[8] = { -1, -1, -1, 0, 1, 1, 1, 0};
const int dy8[8] = { -1, 0, 1, 1, 1, 0, -1, -1};
bool cmp(int X, int Y) {return X > Y;}
int dcp(double A, double B){return fabs(A-B)<=EPS ? 0 : A<B ? -1:1;}

ll gcd(ll a, ll b) {return !b ? a : gcd(b, a % b);}
ll lcm(ll a, ll b) {return (a / gcd(a, b)) * b;}
ll modpow(ll a, ll b, ll m ){a %= m; ll res = 1; while (b) {if (b & 1)res = (res * a) % m; a = (a * a) % m; b >>= 1;} return res;}
ll bpow(ll a, ll b){ll res = 1; while (b) {if (b & 1)res = res * a; a = a * a; b >>= 1;} return res;}
ll modinv(ll a, ll m = MOD) {return modpow(a, m - 2, m);}

int main()
{ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
ll n;cin>>n;
ll c = 2*n;
ll a[c];
rep(i,c){cin>>a[i];}

sort(a,a+c);
ll sum = 0;
for(ll i = 0; i < 2*n ; i = i+2){
    sum+=a[i];
}
cout<<sum;
}


#include<bits/stdc++.h>
using namespace std;
using lli = long long;
#define rep(i,n) for(int i=0;i<n;i++)
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; }

lli n, k;

int main(void){
    cin >> n >> k;
    vector<bool> r(10);
    rep(i, k){
        lli d;
        cin >> d;
        r[d] = true;
    }
    for(int i = n; i <= 10*n; i++){
        string s = to_string(i);
        bool ok = true;
        rep(j, s.size()){
            if(r[s[j]-'0']) ok = false;
        }
        if(ok){
            cout << i << endl;
            return 0;
        }
    }
    return 0;
}

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

vector<bool> d(10, true);

bool isvalid(int n)
{
	int dec = n % 10;
	if(!d[dec]) return false;
	if(n/10 != 0) return isvalid(n/10);
	return true;
}

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

	for (int i = 0; i < k; i++){
		int tmp; cin >> tmp;
		d[tmp] = false;
	}

	while(!isvalid(n)) n++;
	cout << n << '\n';
	return 0;
}


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

int main() {
    string s;
    cin >> s;

    int d = INT_MAX;
    for (int i = 0, n = s.length() - 3; i <= n; i++)
        d = min(d, abs(atoi(s.substr(i, 3).c_str()) - 753));

    cout << d << endl;
}

#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
#define all(x) (x).begin(),(x).end()
#define cyes cout<<"YES"<<endl
#define cno cout<<"NO"<<endl
#define sp <<" "<<
#define cst(x) cout<<fixed<<setprecision(x)
#define pi 3.14159265359
#define mod 1000000007
using namespace std;
using ll = long long;
using ld = long double;
using Graph = vector<vector<int>>;
using que_a = priority_queue<int, vector<int>, greater<int> >;
using que_d = priority_queue<int>;
using pint = pair<int,int>;

vector<long long> divisor(long long n) {
    vector<long long> ret;
    for (long long i = 1; i * i <= n; i++) {
        if (n % i == 0) {
            ret.push_back(i);
            if (i * i != n) ret.push_back(n / i);
        }
    }
    sort(ret.begin(), ret.end()); // 昇順に並べる
    return ret;
}

int main(){

    int n; cin >> n;
    int cnt = 0;
    for(int i = 2; i <= 55555; i++){
        if(divisor(i).size() == 2 && i%5 == 1){
            cout << i;
            cnt++;
            if(cnt == n) break;
            cout << " ";
        }
    }
    cout << endl;
    return 0;
}


#include<stdio.h>

int GCD(int a, int b) {
	int A, B, buff;
	// A >= B
	if (b > a) {
		A = b; B = a;
	}
	else { A = a; B = b; }
	while (A%B) {
		buff = B;
		B = A % B;
		A = buff;
	}
	return B;
}

int main() {
	int N, a, b, c;
	int G;
	int GC[1000000];
	scanf("%d", &N);
	if (!(N - 2)) {
		scanf("%d %d", &a, &b);
		G = GCD(a, b);
		for (int i = 1; i <= G; i++) {
			if (a%i == 0 && b%i == 0) {
				GC[i] = i;
			}
			else GC[i] = -i;
		}
		for (int j = 1; j <= G; j++) {
			if (GC[j] < 0)continue;
			printf("%d\n", GC[j]);
		}
	}
	else {
		scanf("%d %d %d", &a, &b, &c);
		G = GCD(c, GCD(a, b));
		for (int i = 1; i <= G; i++) {
			if (a%i == 0 && b%i == 0 && c%i == 0) {
				GC[i] = i;
			}
			else GC[i] = -i;
		}
		for (int j = 1; j <= G; j++) {
			if (GC[j] < 0)continue;
			printf("%d\n", GC[j]);
		}
	}

	return 0;
}

#include<cstdio>
#include<algorithm>
#define N 100000
#define MAX (1<<31)-1
using namespace std;
int A[N], T[4*N], lazy[4*N];

void build(int l, int r, int k) {
    if (l == r) {
        T[k] = A[l];
        return;
    }
    int mid = (l + r) / 2;
    build(l, mid, k * 2);
    build(mid + 1, r, k * 2 + 1);
    T[k] = min(T[2 * k], T[2 * k + 1]);
}
void pushdown(int k) {
    if (lazy[k] != -1) {
        lazy[2*k] = lazy[k];
        lazy[2*k+1] = lazy[k];
        T[2*k] =  lazy[k];
        T[2*k+1] = lazy[k];
        lazy[k] = -1;
    }
}
void updata(int L, int R, int data ,int l, int r ,int k) {
    if (L <= l && r <= R) {
        T[k] = data;
        lazy[k] = data;
        return;
    }
    pushdown(k);
    int mid = (l + r) / 2;
    if (mid >= L) updata(L, R, data, l, mid, 2*k);
    if (mid < R) updata(L, R, data, mid+1, r, 2*k+1);
    T[k] = min(T[2 * k], T[2 * k + 1]);
}
int find(int L, int R, int l, int r, int k) {
    if (L <= l && r <= R) return T[k];
    pushdown(k);
    int mid = (l + r) / 2;
    int ans = MAX;
    if (mid >= L) ans = min(ans, find(L, R, l, mid, k * 2));
    if (mid < R) ans = min(ans, find(L, R, mid + 1, r, k * 2 + 1));
    return ans;
}
int main() {
    int n, q;
    scanf("%d%d", &n, &q);
    for (int i = 0; i < n; i++)
        A[i] = MAX;
    for (int i = 0; i < 4*n; i++)
        lazy[i] = -1;
    build(0, n - 1, 1);
    int com, s, t, x, y;
    while (q--) {
        scanf("%d", &com);
        if (com == 0) {
            scanf("%d%d%d", &s, &t, &x);
            updata(s, t, x, 0, n - 1, 1);
        }
        else {
            scanf("%d", &x);
            printf("%d\n", find(x, x, 0, n - 1, 1));
        }
    }
    return 0;
}


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

int main() {
  string s;
  cin >> s;
  long n=s.size();
  vector<int>a(26);
  rep(i,n){
    a.at(s.at(i)-'a')++;
  }
  long ans=0;
  rep(i,n){
    ans+=n-a.at(s.at(i)-'a');
  }

    cout<<ans/2+1<<endl;
}

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

template<typename T, typename U>
using pv=vector<pair<T,U>>;
template<typename T>
using matrix=vector<vector<T>>;
template<typename T>
using pque=priority_queue<T>;
template<typename T>
using lpque=priority_queue<T,vector<T>,greater<T>>;

using ll=long long;
using intpair=pair<int,int>;
using llpair=pair<ll,ll>;
using ilpair=pair<int,ll>;
using lipair=pair<ll,int>;
using intvec=vector<int>;
using llvec=vector<ll>;
using intq=queue<int>;
using llq=queue<ll>;
using intmat=vector<intvec>;
using llmat=vector<llvec>;

#define PI 3.141592653589793
#define INTINF 1<<30
#define LLINF 1LL<<60
#define MPRIME 1000000007
#define MPRIME9 998244353
#define MMPRIME (1ll<<61)-1

#define len length()
#define pushb push_back
#define fi first
#define se second

#define setpr fixed<<setprecision(15)
#define all(name) name.begin(),name.end()
#define rall(name) name.rbegin(),name.rend()
#define gsort(vbeg,vend) sort(vbeg,vend,greater<>())

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;
}

template<class T>
inline void outmat(T& a, int y, int x) {
	for(int i=0; i<y; i++) {
		for(int j=0; j<x; j++) cout<<a[i][j]<<" ";
		cout<<endl;
	}
}

template<class T>
inline void init(T& v) {
	for(auto &a: v) cin>>a;
}
template<class T, class U>
inline void init(vector<pair<T,U>>& v) {
	for(auto &a: v) cin>>a.first>>a.second;
}
template<class T, class N>
inline void init(T& v, N n) {
	v.resize(n);
	for(auto &a: v) cin>>a;
}
template<class T, class U, class N>
inline void init(vector<pair<T,U>>& v, N n) {
	v.resize(n);
	for(auto &a: v) cin>>a.first>>a.second;
}

template<class N>
void resiz(N n) {
	//empty
}
template<class N, class T, class... U>
void resiz(N n, T&& hd, U&&... tl) {
	hd.resize(n);
	resiz(n,forward<U>(tl)...);
}

ll binpow(ll a, ll ex, ll p) { //繰り返し二乗法
	ll result=1;
	while(ex>0) {
		if(ex&1) result=result*a%p;
		ex>>=1;
		a=a*a%p;
	}
	return result;
}

string S;
int alp[26];

void input() {
	cin>>S;
}

void solve() {
	ll res=(ll)S.len*(S.len-1)/2+1;
	for(int i=0; i<S.len; i++) {
		res-=alp[S[i]-'a'];
		alp[S[i]-'a']++;
	}
	cout<<res<<endl;
}

int main() {
	int t=1;
	while(t) {
		input();
		solve();
		t--;
	}
}


#include <bits/stdc++.h>
#define name "bai3"
#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define fto(i,a,b) for(int i=a; i<=b; ++i)
#define maxn 100009
#define ll long long
using namespace std;
const int mod=1e9+7;
int n,a[maxn*2],xd[maxn*2];
ll p[maxn];
//ll q[maxn*2];
/*ll mu(int a, int b){
    if(b==0) return 1;
    ll tg=mu(a,b/2);
    tg=(tg*tg)%mod;
    if(b%2)tg=(tg*a)%mod;
    return tg;
}
ll c(int a, int b){
    ///return b!/(a!*(b-a)!)
    if(a==0)return 0;
    if(a>b)return 0;
    return (((p[b]*q[a])%mod)*q[b-a])%mod;
}*/
int main()
{
    cin >> n;
    int s=0;
    p[0]=1;
    fto(i,1,n)p[i]=(p[i-1]*i)%mod;
    /*q[0]=1;
    q[1]=1;
    fto(i,2,n*2) q[i]=mu(p[i],mod-2);*/
    fto(i,1,n*2){
        char c;
        cin >> c;
        if(c=='B')a[i]=1;
        else a[i]=0;
        if(a[i]==s){
            xd[i]=-1;
        }
        else{
            xd[i]=1;
        }
        s^=1;
    }
    int s1=0,s2=0;
    ll res=1;
    fto(i,1,n*2)
    if(xd[i]==1)++s1;
    else{
        if(s1>s2)res=(res*(s1-s2))%mod;
        else res=0;
        if(res==0)break;
        ++s2;
    }
    if(s1!=s2)res=0;
    cout << res*p[n]%mod;
    return 0;
}
/*
BWWB
WWWW

*/

typedef long long ll;
typedef long double ld;
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
int main() {
    ll n;
    string s;
    std::cin >> n>>s;

    ll rc = 0;

    string c = "L";
    for (int i = 1; i < n*2; i++) {
        if(s[i]!=s[i-1]){
            if(c[i-1]=='R'){
                c += 'R';
                rc++;
            }else{
                c += 'L';
            }
        }else{
            if(c[i-1]=='R'){
                c += 'L';
            }else{
                c += 'R';
                rc++;
            }
        }
    }
    if(s[0]=='W'){
        std::cout << 0 << std::endl;
        return 0;
    }

    if(rc!=n){
        std::cout << 0 << std::endl;
        return 0;
    }
    if(c[2*n-1]=='L'){
        std::cout << 0 << std::endl;
        return 0;
    }
    rc = 0;
    ll ans = 1;

    for (int i = 2*n-1; i >=0; i--) {
        if(c[i]=='R'){
            rc++;
        }else{
            if(rc==0){
                std::cout << 0 << std::endl;
                return 0;
            }
            ans *= rc;
            rc--;
            ans %= MOD;
        }
    }

    for (int i = 0; i < n; i++) {
        ans *= i+1;
        ans %= MOD;
    }
    std::cout << ans << std::endl;
}


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

int main() {
    // ll N, A, B, C, D, E;
    // cin >> N >> A >> B >> C >> D >> E;
    ll N, A[5];
    cin >> N;
    rep(i, 5) cin >> A[i];
    sort(A, A + 5);

    ll mn = A[0];

    cout << (N + mn - 1) / mn + 4 << endl;

}

#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(ri,n) for(int ri = (int)(n-1); ri >= 0; ri--)
#define rep2(i,x,n) for(int i = (int)(x); i < (int)(n); i++)
#define rrep2(ri,x,n) for(int ri = (int)(n-1); ri >= (int)(x); ri--)
#define repit(itr,x) for(auto itr = x.begin(); itr != x.end(); itr++)
#define rrepit(ritr,x) for(auto ritr = x.rbegin(); ritr != x.rend(); ritr++)
#define ALL(x) x.begin(), x.end()
using ll = long long;
using namespace std;

int main(){
  ll n, a, b, c, d, e;
  cin >> n >> a >> b >> c >> d >> e;
  ll mn = min({a, b, c, d, e});
  ll m = (n+mn-1)/mn;
  ll ans = m + 4;
  cout << ans << endl;
  return 0;
}


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

#define ll long long
#define ull unsigned long long
#define db long double
#define pb push_back
#define ppb pop_back
#define F first
#define S second
#define mp make_pair
#define all(x) (x).begin(), (x).end()

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;

const int N = 1e5 + 123;
int n;
ll a[N];
vector <ll> basis;

void add(ll x) {
	for (ll i : basis) {
		x = min(x, x ^ i);
	}
	if (x > 0) {
		basis.pb(x);
	}
}

int f(ll x) {
	for (int i = 59; i >= 0; i--) {
		if (x >> i & 1) {
			return i;
		}
	}
	return -1;
}

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

	cin >> n;
	ll t = 0;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
		t ^= a[i];
	}
	ll ans = t, rt = t ^ ((1ll << 60) - 1);
	for (int i = 1; i <= n; i++) {
		a[i] &= rt;
		add(a[i]);
	}
	sort(all(basis));
	reverse(all(basis));
	ll res = 0;
	for (ll i : basis) {
		if (res >> f(i) & 1) {
			continue;
		}
		res ^= i;
	}
	ans += 2 * res;
	cout << ans;
}

//include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>

#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>

#include <stdexcept>

using namespace std;

// type alias
using ll = long long;
using ull = unsigned long long;

//conversion
//------------------------------------------
inline int toint(string s) { int v; istringstream sin(s); sin >> v; return v; }
template<class t> inline string tostring(t x) { ostringstream sout; sout << x; return sout.str(); }

//debug
#define DUMP(x)  cerr << #x << " = " << (x) << endl
#define DEBUG(x) cerr << #x << " = " << (x) << " (l" << __line__ << ")" << " " << __file__ << endl

inline ll floor(ll a, ull b)
{
	ll llb = b;
	if (a >= 0)
	{
		return a / llb;
	}
	else
	{
		return - ( (-a + llb - 1) / llb);
	}
}

inline ll ceil(ll a, ull b)
{
	ll llb = b;
	if (a >= 0)
	{
		return (a + llb - 1) / llb;
	}
	else
	{
		return - (-a / llb);
	}
}

inline ll getLowestOneBit(ll a)
{
	return a & (-a);
}

inline ll getHighestOneBit(ll a)
{
	while ((a & (a - 1)) != 0)
	{
		a = a & (a - 1);
	}
	return a;
}

inline bool isPower2(ull a)
{
	if (a == 0)
	{
		return false;
	}
	return !(a & (a - 1));
}

inline ll getSpecifiedBit(ll a, unsigned bit)
{
	return (a & (1LL << bit)) >> bit;
}

// computational complexity: o(log(max(a, b)))
inline ull getGcd(ull a, ull b)
{
	if (b == 0)
	{
		return a;
	}
	return getGcd(b, a % b);
}

template<class Integer>
inline Integer getPower(Integer base, ull exponential)
{
	Integer result = 1;
	while (exponential >= 1)
	{
		if (exponential & 1)
		{
			result = result * base;
		}
		base = base * base;
		exponential >>= 1;
	}

	return result;
}

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

	ll N;
	cin >> N;

	vector<vector<ll>> As(N, vector<ll>(60));
	vector<ll> bit_count(60);
	ll a;
	for (ll i = 0; i < N; ++i)
	{
		cin >> a;
		for (ll j = 0; j < 60; ++j)
		{
			As[i][j] = (a & (1LL << j)) >> j;
			bit_count[j] += (a & (1LL << j)) >> j;
		}
	}

	ll res = 0;
	for (ll j = 0; j < 60; ++j)
	{
		if (bit_count[j] % 2 != 0)
		{
			res += 1LL << j;
			for (ll i = 0; i < N; ++i)
			{
				As[i][j] = 0;
			}
		}
	}

	ll row = 0;
	for (ll j = 59; j >= 0; --j)
	{
		if (bit_count[j] % 2 != 0) continue;
		for (ll i = row; i < N; ++i)
		{
			if (As[i][j] == 0) continue;
			vector<ll> temp = As[row];
			As[row] = As[i];
			As[i] = temp;
			for (ll k = row + 1; k < N; ++k)
			{
				if (As[k][j] == 0) continue;
				for (ll l = 0; l < 60; ++l)
				{
					As[k][l] = (As[k][l] - As[row][l] + 2LL) % 2LL;
				}
			}
			++row;
			break;
		}
	}

	vector<ll> f2_vec(60);
	row = 0;
	for (ll j = 59; j >= 0; --j)
	{
		if (As[row][j] == 0) continue;
		if (f2_vec[j] == 0)
		{
			for (ll k = 0; k < 60; ++k)
			{
				f2_vec[k] = (f2_vec[k] + As[row][k] + 2LL) % 2;
			}
		}
		++row;
	}

	for (ll j = 0; j < 60; ++j)
	{
		if (f2_vec[j] == 1)
		{
			res += (1LL << j) * 2;
		}
	}

	cout << res << endl;

	return 0;
}


#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
ull B = 100000007;
ll mod = 1000000007;
int INF = 0x3f3f3f3f;

int main() {
    int n, m;
    cin >> n >> m;
    vector<int> v(n+1);

    int u1 = 1+m;
    int d1 = 1;
    int u2=  n;
    int d2 = n - m + 1;

    int c = 0;

    while (u1 > d1 && c < m) {
        cout << u1 << " " << d1 << endl;
        d1++; u1--;
        c++;
    }
    while (u2 > d2 && c < m) {
        cout << u2 << " " << d2 << endl;
        d2++; u2--;
        c++;
    }
}

#include <iostream>
#include <algorithm>

using namespace std;

void solve() {
    int n, k;
    cin >> n >> k;
    int l[n];
    for (int i=0; i<n; ++i)
        cin >> l[i];
    sort(l, l+n);
    int ans = 0;
    for (int i=0; i<k; ++i)
        ans += l[n-1-i];
    cout << ans;

}

int main() {

	solve();
    return 0;
}


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

#ifdef _DEBUG
inline void dump() { cerr << endl; }
template<typename Head> void dump(Head&& head) { cerr << head; dump(); }
template<typename Head, typename... Tail> void dump(Head&& head, Tail&&... tail) { cerr << head << ", "; dump(forward<Tail>(tail)...); }
#define debug(...) do {cerr << __LINE__ << ":\t" << #__VA_ARGS__ << " = "; dump(__VA_ARGS__); } while (false)
#else
#define dump(...)
#define debug(...)
#endif

template<typename T>
struct edge {
  int src, to;
  T cost;
  edge(int to, T cost) : src(-1), to(to), cost(cost) {}
  edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}
  edge &operator=(const int &x) {
    to = x;
    return *this;
  }
  operator int() const { return to; }
};
template<typename T>
using Edges = vector<edge<T>>;
template<typename T>
using WeightedGraph = vector<Edges<T>>;
using UnWeightedGraph = vector<vector<int>>;
template<typename T>
using Matrix = vector<vector<T>>;

/////////////////////////////////////////////////////////////////////

const ll inf = 1LL<<60;

typedef pair<ll, ll> P;

int main()
{
  ll n; cin>>n;
  vector<ll> a(n);
  vector<ll> b(n);
  vector<P> c(n);
  rep(i, n) {
    cin>>a[i]>>b[i];
    c[i] = make_pair(a[i]+b[i], i);
  }
  sort(c.rbegin(),c.rend());

  ll x=0, y=0;
  rep(i, n) {
    if (i%2) y += b[c[i].second];
    else x += a[c[i].second];
  }

  cout << x-y << endl;

  return 0;
}

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

#define N 8

bool col[N],hi[2*N-1],mi[2*N+1];
int row[N];
void point(int y,int x){

  row[y] = x;
  col[x] = false;
  hi[x+y] = false;
  mi[y-x+N-1] = false;

}

void del(int y, int x){

  row[y] = -1;
  col[x] = true;
  hi[x+y] = true;
  mi[y-x+N-1] = true;

}

void print_ans(){
  for(int i=0; i<N; i++){
    for(int j=0; j<N; j++){
      if(row[i] == j) cout << "Q";
      else cout << ".";
    }
    cout << endl;
  }
}

bool is_ok(int y,int x){
  return row[y] == -1 && col[x] == true && hi[x+y] == true && mi[y-x+N-1];
}

void dfs(int r){
  if(r == 8){//??´??????
    print_ans();
    return ;
  }
  if(row[r] != -1){
    dfs(r+1);
  }
  else{
    for(int i=0; i<8; i++){
      if(is_ok(r,i) == true){
	point(r,i);
	dfs(r+1);
	del(r,i);
      }
    }
  }
  return;
}

int main(){

  int k; cin >> k;

  memset(row,-1,sizeof(row));
  memset(col,true,sizeof(col));
  memset(hi,true,sizeof(hi));
  memset(mi,true,sizeof(mi));

  for(int i=0; i<k; i++){
    int x,y; cin >> y >> x;
    row[y] = x;
    col[x] = false;
    hi[x+y] = false;
    mi[y-x+N-1] = false;
  }

  dfs(0);

}


#include<bits/stdc++.h>
using namespace std;
///Welcome to Nasif's Code
#define bug printf("bug\n");
#define bug2(var) cout<<#var<<" "<<var<<endl;
#define co(q) cout<<q<<endl;
#define all(q) (q).begin(),(q).end()
typedef long long int ll;
typedef unsigned long long int ull;
const int MOD = (int)1e9+7;
const int MAX = 1e6;
#define pi acos(-1)
#define inf 1000000000000000LL
#define FastRead    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
map<int,int>mp;
bool check(int n)
{
    while(n>0){
        int cur=n%10;
        n/=10;
        if(mp[cur])
            return 0;
    }

return 1;
}
int main()
{
    FastRead
    //freopen("output.txt", "w", stdout);
    int n,k;
    cin>>n>>k;
    for(int i=0; i<k; i++)
    {
        int a;
        cin>>a;
        mp[a]++;
    }
    for(int i=n; i<=MAX; i++)
    {
        if(check(i))
        {
            cout<<i<<endl;
            return 0;
        }
    }
    return 0;
}

#include <bits/stdc++.h>
#include <string>
#include <vector>
#include <algorithm>
#include <math.h>
#include <iostream>
#include <numeric>
#define rep(i,n) for (int i = 0;i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int,int>;
#define chmax(x,y) x = max(x,y)
int INF = 1e9;

int main(){
	int n,K;cin >> n >> K;
	vector<int> num;
	rep(i,10) num.push_back(i);
	rep(i,K) {
		int a;cin >> a;
		rep(j,num.size()){
			if(num[j]==a) num.erase(num.begin()+j);
		}
	}
	//rep(i,num.size()) cout << num[i]<< endl;
	int ans = 100000;
	for(int a:num)for(int b:num)for(int c:num)for(int d:num)for(int e:num){
		int temp = 10000*a+1000*b+100*c+10*d+e;
		int temp2 = 1000*b+100*c+10*d+e;
		int temp3 = 100*c+10*d+e;
		int temp4 = 10*d+e;
		int temp5 = e;
		if(temp >= n) ans = min(ans,temp);
		if(temp2 >= n) ans = min(ans,temp2);
		if(temp3 >= n) ans = min(ans,temp3);
		if(temp4 >= n) ans = min(ans,temp4);
		if(temp5 >= n) ans = min(ans,temp5);
	}
	cout << ans << endl;

}


#include <iostream>
#include<vector>
#include<array>
#include<cmath>
#include <algorithm>

using namespace std;

int main() {
	// 初期処理(iostream軽くする)
	cin.tie(0);
	cout.tie(0);
	ios::sync_with_stdio(false);

	string n;
	cin >> n;

	if (n.find("7") != string::npos) {
		cout << "Yes";
	}
	else {
		cout << "No";
	}

	return 0;
}

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

vector<pair<int,int> >bara,koma;

bool comp1(const pair<ll,ll> &a, const pair<ll,ll> &b)
{
    return (a.second<b.second);
}

bool comp2(const pair<ll,ll> &a, const pair<ll,ll> &b){
    return (a.first>b.first);
}

int main()
{
    int t;
    cin>>t;
    int open,close;
    while(t--)
    {
        string s;
        cin>>s;
        open=0;
        close=0;
        for(char c : s)
        {
            if(c=='(')
            {
                open++;
            }
            else
            {
                if(open)
                    open--;
                else
                    close++;
            }
        }
        if(open>=close)
            bara.push_back({open,close});
        else
            koma.push_back({open,close});
    }

    sort(bara.begin(), bara.end(),comp1);
    sort(koma.begin(),koma.end(),comp2);

    bara.insert(bara.end(),koma.begin(),koma.end());

    int ans=0;
    for(auto x : bara)
    {
        ans-=x.second;
        if(ans<0)
        {
            cout<<"No"<<endl;
            return 0;
        }
        ans+=x.first;
    }
    if(ans==0)
        cout<<"Yes"<<endl;
    else
        cout<<"No"<<endl;

    return 0;
}


#include <bits/stdc++.h>

#define F first
#define S second
#define PB push_back

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
typedef priority_queue<int> HEAP;
typedef priority_queue<int, vector<int>, greater<int> > RHEAP;

const int N = 100010, M = 1010;

int n;
int q[N];

int main()
{
	scanf("%d", &n);

	int cnt = 0;
	for (int i = 1; i <= n; i ++ )
	{
		int x;
		scanf("%d", &x);

		int l = 1, r = cnt;
		while (l < r)
		{
			int mid = l + r >> 1;
			if (q[mid] < x) r = mid;
			else l = mid + 1;
		}

		if (!cnt || q[cnt] >= x) q[ ++ cnt] = x;
		else q[r] = x;
	}

	printf("%d\n", cnt);
    return 0;
}

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <tuple>
#include <utility>
#include <vector>
#define ll long long
#define itn int
#define endl '\n'
#define co(ans) cout<<ans<<endl
#define COYE cout<<"YES"<<endl
#define COYe cout<<"Yes"<<endl
#define COye cout<<"yes"<<endl
#define CONO cout<<"NO"<<endl
#define CONo cout<<"No"<<endl
#define COno cout<<"no"<<endl
#define FORE(i,a) for(auto &i:a)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define FFOR(i,a,b) for(int i=(a);i<=(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define RREP(i,n)  FFOR(i,1,n)
#define PB push_back
#define MP make_pair
#define ALL(V) (V).begin(),(V).end()
#define SORT(V) sort((V).begin(),(V).end())
#define REVERSE(V) reverse((V).begin(),(V).end())
#define EACH(V,i) for(typeof((V).begin()) i=(V).begin();i!=(V).end();++i)
#define equals(a,b) (fabs((a)-(b))<EPS)
#define INF ((1LL<<62)-(1LL<<31))
#define EPS 1e-10
#define PI 3.141592653589793238
#define MOD 1000000007
#define MAX 5100000
using namespace std;
using Edge=pair<int,int>;
using Graph=vector<vector<int>>;
inline int toInt(string s){int v;istringstream sin(s);sin>>v;return v;}
template<class T>inline string toString(T x){ostringstream sout;sout<<x;return sout.str();}
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;}
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
const int dx[4]={0,1,0,-1},dy[4]={1,0,-1,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;}

int main(){
  int N;
  cin>>N;
  int A[N];

  multiset<int> M;
  REP(i,N){
    cin>>A[i];
    auto it=M.lower_bound(A[i]);
    if(it!=M.begin()) M.erase(--it);
    M.insert(A[i]);
  }

  co(M.size());
}


/* Simplicity and Goodness */

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less<int>, rb_tree_tag,
            tree_order_statistics_node_update> indexed_set;

void my_dbg() { cout << endl; }
template<typename Arg, typename... Args> void my_dbg(Arg A, Args... B)
{ cout << ' ' << A; my_dbg(B...); }
#define dbg(...)  cout << "(" << #__VA_ARGS__ << "):", my_dbg(__VA_ARGS__)

#define scn(n) scanf("%d", &n)
#define lscn(n) scanf("%lld", &n)
#define pri(n) printf("%d ", (int)(n))
#define prin(n) printf("%d\n", (int)(n))
#define lpri(n) printf("%lld ", n)
#define lprin(n) printf("%lld\n", n)
#define rep(i,a,b) for(int i=(int)(a); i<(int)(b); i++)
#define pb push_back
#define mp make_pair
#define F first
#define S second

using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;

const int inf = INT_MAX;
const int ninf = INT_MIN;
const int mod = 1e9+7;
const int N = 2e5+2;

void solve()
{
    string st;
    cin >> st;

    int s = 0, t = 0, rm = 0;

    rep(i, 0, st.size()) {
        if(st[i] == 'S')
            s ++, t = 0;
        else
            t ++;

        if(st[i] == 'T') {
            int here = min(s, t);
            s -= here;
            t -= here;
            rm += here;
        }
    }

    prin(st.size() - rm * 2);
}

int main()
{
    int t = 1;
    // scn(t);

    while(t --) {
        solve();
    }
    return 0;
}

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

#define int long long
#define rep(i,n) for(int i=0;i<n;i++)
#define pii pair<int,int>

string S;

signed main()
{
    cin >> S;

    int tmp = 0;
    int ans = S.size();
    for(auto e : S)
    {
        if(e == 'T' && tmp)
        {
            tmp--;
            ans -= 2;
        }
        if(e == 'S')tmp++;
    }

    cout << ans << endl;

    return 0;
}


#include<bits/stdc++.h>
#define fo(i,a,b) for(register int (i)=(a);(i)<=(b);++(i))
#define rd read()
#define maxn 200001
#define gc getchar()
#define mem(a,b) memset(a,b,sizeof(a))
#define mp make_pair
#define ll long long
#define ull unsigned ll
#define pb push_back
#define db double
#define pu puts(" ")
using namespace std;
const int p=998244353;
int read()
{
	int x=0,f=1;
	char ch=gc;
	while(!isdigit(ch))
	{
		if(ch=='-')f=-1;
		ch=gc;
	}
	while(isdigit(ch))x=x*10+ch-48,ch=gc;
	return x*f;
}
void print(int x)
{
	if(x<0)putchar('-'),x=-x;
	if(x>9)print(x/10);
	putchar(x%10+48);
}
int qp(int a,int b)
{
	int ans=1;
	while(b)
	{
		if(b&1)ans=(ll)ans*a%p;
		a=(ll)a*a%p;

		b>>=1;
	}
	return ans%p;
}

int main()
{
	int a=rd,b=rd,k=rd;
	fo(i,1,k)
	{
		if(i%2==1)
		{
		if(a%2==1)a--;a/=2,b+=a;}
		else{if(b%2==1)b--;b/=2,a+=b;}
	}
	printf("%d %d\n",a,b);
	return 0;
}

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <math.h>
#include <utility>
#include <algorithm>
#include <functional>
#include <vector>
#include <numeric>
#include <bits/stdc++.h>

#define int long long

struct edge {
    int to;
    int cost;
};

using namespace std;
using vi = vector<int>;
using vii = vector<vector<int>>;
using vb = vector<bool>;
using qi = queue<int>;
using P = pair<int, int>;
using graph = vector<vector<edge>>;
// using graph = vector<vector<int>>;

#define FOR(i, a, b) for (int i = a; i < (b); i++)
#define REP(i,n) for(int i=0;i<n;i++)
#define delim(i, n) cout << (i == n-1 ? "\n" : " ");

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

const int inf = 1LL << 60;
// const int inf = 100000000;
const int mod = 1000000007;

const int maxn = 100000;
int n, m;
vi a(maxn);
vi table(maxn);

vi dx = {-1, 0, 0, 1};
vi dy = {0, -1, 1, 0};

int lds() {
    table.resize(n);
    REP(i, n) {
        table[i] = -1;
    }
    REP(i, n) {
        auto iter = upper_bound(table.begin(), table.end(), a[i], [](const int& a, const int& b) { return a > b; });
        *iter = a[i];
    }
    auto iter = lower_bound(table.begin(), table.end(), -1, [](const int& a, const int& b) { return a > b; });
    return (int) (iter - table.begin());
}

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

    // https://onlinejudge.u-aizu.ac.jp/problems/DPL_1_D
    cin >> n;
    REP(i, n) {
        cin >> a[i];
    }

    int ret = lds();
    cout << std::fixed << std::setprecision(10) << ret << "\n";
}


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

const long long MOD = 1e9 + 7;

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

    int n;
    cin >> n;

    vector<long long> a(n);
    for (int i = 0; i < n; i++)
        cin >> a[i];

    long long ans = 0, mul = a[0];
    for (int i = 1; i < n; i++){
        ans += (mul * a[i]) % MOD;
        ans %= MOD;
        mul += a[i];
        mul %= MOD;
    }

    cout << ans << '\n';

    return 0;
}

#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define gc getchar_unlocked
#define fo(i,n) for(int i=0;i<n;i++)
#define Fo(i,k,n) for(int i=k;k<n?i<n:i>n;k<n?i+=1:i-=1)
#define ll long long int
#define s(x)	cin>>x
#define p(x)	cout<<x<<"\n"
#define goog(x, y)  cout<<"Case #"<<x<<": "<<y<<"\n"
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)
#define PI 3.1415926535897932384626
#define INF 100000000000
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
typedef pair<int, int>	pii;
typedef pair<ll, ll>	pl;
typedef vector<int>		vi;
typedef vector<ll>		vl;
typedef vector<pii>		vpi;
typedef vector<pl>		vpl;
typedef vector<vi>		vvi;
typedef vector<vl>		vvl;
typedef long double ld;

mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
int rng(int lim) {
	uniform_int_distribution<int> uid(0,lim-1);
	return uid(rang);
}
int mpow(int base, int exp);
void ipgraph(int n, int m);
void dfs(int u, int par);
int gcd(int a, int b) ;

const int mod = 1'000'000'007;
const int N = 3e5, M = N;
//=======================

vi g[N];
int a[N];

void solve() {
  int n;cin>>n;
  ll a[n];
  fo(i,n) cin>>a[i];
  ll cont[n];
  cont[0]=a[0];
  Fo(i,1,n)
  cont[i]=(cont[i-1]+a[i])%mod;
  ll res[n];
  res[0]=0;
  Fo(i,1,n)
  { res[i]=(res[i-1]+a[i]*cont[i-1])%mod;}
  p(res[n-1]);
}

int main() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    srand(chrono::high_resolution_clock::now().time_since_epoch().count());

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

    return 0;
}

int mpow(int base, int exp) {
  base %= mod;
  int result = 1;
  while (exp > 0) {
    if (exp & 1) result = ((ll)result * base) % mod;
    base = ((ll)base * base) % mod;
    exp >>= 1;
  }
  return result;
}

void ipgraph(int n, int m){
	int i, u, v;
	while(m--){
		cin>>u>>v;
    //u--, v--;
		g[u].pb(v);
		g[v].pb(u);
	}
}

void dfs(int u, int par){
	for(int v:g[u]){
		if (v == par) continue;
		dfs(v, u);
	}
}

int gcd(int a, int b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}


#include <bits/stdc++.h>
using namespace std;
// Macros for easier access
#define ll long long
#define endl '\n'
#define F first
#define S second
#define umap unordered_map
#define uset unordered_set
#define test ll cases; cin>>cases; for(ll testCase = 1; testCase <= cases; testCase++)   //test cases
#define fill(name, val) memset(name, val, sizeof(name));
#define mop(a, op, b)    (a%mod op b%mod)%mod
#define error(x) fixed<<setprecision(x) //cout<<error(5)<<someDouble    -> example - 3.14159
#define vll vector<ll>
#define vvll vector<vll>
#define pll pair<ll, ll>
#define get(a, n)    for(ll it = 0; it < n; it++)    cin>>a[it];
#define boost ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define debug(x) cerr << #x << " : " << (x) << endl
// Constants
#define MX 100001
#define mod 1000000007LL
#define inf 1000000000000000000LL
void init(){
	boost;
	#ifndef ONLINE_JUDGE
		freopen("input.txt", "r", stdin);
		freopen("output.txt", "w", stdout);
	#endif
}

/*	Author: 🆉🅴🅽🅾🅻🆄🆂	*/

string k;
ll dp[10001][2][101], d;
ll count(ll i, bool tight, ll sum){
	if(i == k.size())	return (sum == 0);
	if(dp[i][tight][sum] != -1)	return dp[i][tight][sum];
	ll ans = 0, high = tight ? k[i]-'0' : 9;
	for(ll j = 0; j <= high; j++)
		ans = mop(ans, +, count(i+1, tight & (j == high), (sum+j)%d));
	return dp[i][tight][sum] = ans;
}
int main(){
	init();	fill(dp, -1);
	cin>>k>>d;
	cout<<(count(0, 1, 0) + mod - 1) % mod;
}

#include <bits/stdc++.h>

#include <iostream>
//#include <algorithm>
// #include <iomanip>
#define ll long long
#define map unordered_map
#define set unordered_set
#define pll pair<ll, ll>
#define vll vector<ll>
#define mll map<ll, ll>

using namespace std;

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

ll m[100][100];

void show_table(ll I, ll J) {
  for (ll i = 0; i <= I; i++) {
    for (ll j = 0; j <= J; j++) {
      cout << m[i][j] << " ";
    }
    cout << "" << endl;
  }
  cout << "" << endl;
}

int main() {
  // std::cout << std::fixed << std::setprecision(10);
  ll H, W;
  scanf("%lld %lld", &H, &W);

  for (ll y = 0; y < H; y++) {
    for (ll x = 0; x < W; x++) {
      m[y][x] = -1;
    }
  }

  ll spaces = 0;

  for (ll y = 0; y < H; y++) {
    string s;
    cin >> s;

    for (ll x = 0; x < W; x++) {
      if (s[x] == '.') {
        m[y][x] = INF;
        spaces++;
      }
    }
  }

  queue<pll> q;
  q.push(make_pair(0, 0));
  m[0][0] = 0;

  for (;;) {
    if (q.empty()) {
      break;
    }
    auto pos = q.front();
    ll y = pos.first;
    ll x = pos.second;

    ll c = m[y][x];

    if (y >= 1 && m[y - 1][x] > c + 1) {
      m[y - 1][x] = c + 1;
      q.push(make_pair(y - 1, x));
    }
    if (y < H - 1 && m[y + 1][x] > c + 1) {
      m[y + 1][x] = c + 1;
      q.push(make_pair(y + 1, x));
    }
    if (x >= 1 && m[y][x - 1] > c + 1) {
      m[y][x - 1] = c + 1;
      q.push(make_pair(y, x - 1));
    }
    if (x < W - 1 && m[y][x + 1] > c + 1) {
      m[y][x + 1] = c + 1;
      q.push(make_pair(y, x + 1));
    }

    q.pop();
  }

  // show_table(3, 3);

  ll a = m[H - 1][W - 1];

  if (a == INF) {
    cout << -1 << endl;
    return 0;
  }
  // cout << "a:" << a << endl;

  ll ans = spaces - a - 1;

  cout << ans << endl;
}


#include <bits/stdc++.h>
#define rep(i, a) for (int i = (int)0; i < (int)a; ++i)
#define rrep(i, a) for (int i = (int)a - 1; i >= 0; --i)
#define REP(i, a, b) for (int i = (int)a; i < (int)b; ++i)
#define RREP(i, a, b) for (int i = (int)a - 1; i >= b; --i)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define popcount __builtin_popcount
using ll = long long;
constexpr ll mod = 1e9 + 7;
constexpr ll INF = 1LL << 60;

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;
}

ll gcd(ll n, ll m)
{
	ll tmp;
	while (m != 0)
	{
		tmp = n % m;
		n = m;
		m = tmp;
	}
	return n;
}

ll lcm(ll n, ll m)
{
	return abs(n) / gcd(n, m) * abs(m); //gl=xy
}

using namespace std;

void solve(){
	int n;
	cin>>n;
	string s;
	cin>>s;
	int q;
	cin>>q;
	vector<ll>cnt_m(n+1);
	rep(i,n){
		cnt_m[i+1]=cnt_m[i]+ll(s[i]=='M');
	}
	while(q--){
		int k;
		cin>>k;
		vector<vector<ll>>dp(2,vector<ll>(4));
		dp[0][0]=1;
		rep(i,k){
			rep(j,4){
				dp[(i+1)%2][j]+=dp[i%2][j];
			}
			if(s[i]=='D'){
				dp[(i+1)%2][1]+=dp[i%2][0];
			}
			else if(s[i]=='M'){
				dp[(i+1)%2][2]+=dp[i%2][1];
			}
			else if(s[i]=='C'){
				dp[(i+1)%2][3]+=dp[i%2][2];
			}
			rep(j,4)dp[i%2][j]=0;
		}
		for(int i=k;i<n;++i){
			if(s[i-k]=='D'){
				dp[i%2][2]-=(cnt_m[i]-cnt_m[i+1-k]);
				dp[i%2][1]--;
			}
			rep(j,4){
				dp[(i+1)%2][j]+=dp[i%2][j];
			}
			if(s[i]=='D'){
				dp[(i+1)%2][1]+=dp[i%2][0];
			}
			else if(s[i]=='M'){
				dp[(i+1)%2][2]+=dp[i%2][1];
			}
			else if(s[i]=='C'){
				dp[(i+1)%2][3]+=dp[i%2][2];
			}
			rep(j,4)dp[i%2][j]=0;
		}
		cout<<dp[n%2][3]<<"\n";
	}

}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout << fixed << setprecision(15);
	solve();
	return 0;
}


#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef string str;
typedef vector<ll> vel;
typedef vector<str> ves;
#define REP(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, b) REP(i, 0, b)
const ll mod = 1000000007;
const double pi = 3.14159265358979;

ll expo(ll a, ll b){
    ll ex = 1;
    rep(i, b){
        ex *= a;
    }
    return ex;
}

ll fact(ll a){
    ll fc = 1;
    REP(i, 1, a+1){
        fc *= i;
    }
    return fc;
}

int main() {
    ll a, b;
    cin >> a >> b;
    cout << max(a+b, max(a-b, a*b)) << endl;
}

#include <iostream>

using namespace std;

int main()
{
    int A, B;
    cin >> A >> B;
    int X = A*B;
    int Y = A+B;
    int Z = A-B;
    if((X>=Y)&&(X>=Z))
    {
        cout << X;

    }
    else if((Y>=X)&&(Y>=Z))
    {
        cout << Y;
    }
    else if((Z>=Y)&&(Z>=X))
    {
        cout << Z;
    }

    return 0;

}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;

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

    int a, b, c, d;
    cin >> a >> b >> c >> d;
    if (a <= b && c <= d) {
        cout << a + c << "\n";
    } else if (a <= b && c >= d) {
        cout << a + d << "\n";
    } else if (a >= b && c >= d) {
        cout << b + d << "\n";
    } else if (a >= b && c <= d) {
        cout << b + c << "\n";
    }

}

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int a,b,c,d,ans;
    while(cin >> a >> b >> c >> d){
        ans = 0;
        ans+=min(a,b);
        ans+=min(c,d);
        cout << ans << endl;
    }
}


// Graph: Adjacency list
#include<vector>
typedef std::vector<std::vector<int>> Graph;
// Utility
const Graph reverse(const Graph& g) {
  Graph result(g.size());
  for(int u = 0; u < g.size(); ++u) for(auto v: g.at(u)) result.at(v).push_back(u);
  return result;
}
void postorder_dfs(const Graph& g, int current, std::vector<bool>& visited, std::vector<int>& result) {
  visited.at(current) = true;
  for(auto next: g.at(current)) if(!visited.at(next)) postorder_dfs(g, next, visited, result);
  result.push_back(current);
}
std::vector<int> postorder(const Graph& g) {
  std::vector<bool> visited(g.size(), false);
  std::vector<int> result;
  for(int v = 0; v < g.size(); ++v) if(!visited[v]) postorder_dfs(g, v, visited, result);
  return result;
}
// Strongly Connected Components: Kosaraju's algorithm O(V + E)
void scc_dfs(const Graph& g, int current, int group, std::vector<bool>& visited, std::vector<int>& result) {
  visited.at(current) = true;
  result.at(current) = group;
  for(auto next: g.at(current)) if(!visited.at(next)) scc_dfs(g, next, group, visited, result);
}
std::vector<int> scc(const Graph& g) {
  auto order = postorder(g);
  auto rg = reverse(g);
  std::vector<bool> visited(g.size(), false);
  std::vector<int> result(g.size());
  int group = 0;
  for(auto v = order.rbegin(); v != order.rend(); ++v) if(!visited.at(*v)) scc_dfs(rg, *v, group++, visited, result);
  return result;
}

#include<bits/stdc++.h>
using namespace std;
int main() {
  int V, E;
  cin >> V >> E;

  Graph g(V);
  for(int i = 0; i < E; ++i) {
    int s, t;
    cin >> s >> t;
    g[s].push_back(t);
  }

  auto group = scc(g);

  int Q;
  cin >> Q;
  for(int i = 0; i < Q; ++i) {
    int u, v;
    cin >> u >> v;
    cout << (group[u] == group[v] ? 1 : 0) << endl;
  }
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
template<class T> using vc = vector<T>;
template<class T> using vvc = vector<vector<T>>;

const ll MOD = 1e9 + 7;
const ll INF = 1e16;
const ld EPS = 1e-11;
const ld PI = acos(-1.0L);
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, n) for (ll i = (n)-1; i >= 0; i--)
#define repe(i, l, r) for (ll i = (l); i < (r); i++)
#define reper(i, l, r) for (ll i = (r)-1; i >= (l); i--)

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; }

void init() {cin.tie(0);ios::sync_with_stdio(false);cout << fixed << setprecision(15);}
//--------------------------------------------------------------------------------//

int main() {
    init();
    ll N, M;
    cin >> N >> M;
    vvc<ll> E(N), R(N);
    vc<ll> inn(N); //根なら0のまま
    rep(i,N-1+M){
        ll a, b;
        cin >> a >> b;
        a--, b--;
        E[a].eb(b);
        inn[b]++;
    }
    ll root;
    rep(i, N) if (inn[i]==0){
        root = i;
        break;
    }

    vl dep(N), ans(N), cnt(N);
    queue<ll> q;
    q.emplace(root);
    while(!q.empty()){
        ll now = q.front();
        q.pop();
        cnt[now]++;
        if (cnt[now] < inn[now]) continue; //親から辿られきられていないなら飛ばす
        for(auto to:E[now]){
            dep[to] = dep[now] + 1;
            ans[to] = now+1;
            q.emplace(to);
        }
    }
    rep(i, N) cout << ans[i] << "\n";
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
    string a,b,c; cin>>a>>b>>c;
    int l=a.length(),r=b.length(),p=c.length();
    if(a[l-1]==b[0] && b[r-1]==c[0])cout<<"YES";
    else cout<<"NO";
}

#include<bits/stdc++.h>
#define forr(i, a, b) for(int i = (a); i < (b); i++)
#define rep(i,n) for(int i = 0; i < (n); i++)
#define rrep(i,n) for(int i = 1; i <= (n); i++)
#define ALL(a) (a.begin()),(a.end())
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<ll, ll> LP;
const ll LINF = 1LL<<60;
const int INF = 1001001001;
const int MOD = 1000000007;

/* --------------------------------------------------- */

int main() {
    int n;
    string s, t;
    cin >> n >> s >> t;
    int ans = s.size() + t.size();
    int tot = 0;
    for(int i = n - 1; i >= 0; i--) {
        bool check = true;
        for(int j = i; j < n; j++) {
            if(s[j] != t[j - i]) check = false;
        }
        if(check) tot = n - i;
    }
    cout << ans - tot << endl;

    return 0;
}


#include<iostream>
#include<string>
#include<vector>
#include<numeric>
#include<math.h>
#include<algorithm>
#include<iomanip>
#include<list>
#include<bitset>
#include<sstream>
using namespace std;

int main() {

	vector<string> S(3, "");

	for (long long i = 0; i < S.size(); i++) {
		cin >> S[i];
	}

	vector<list<long long>> v(3, list<long long>());

	for (long long i = 0; i < v.size(); i++) {
		for (long long j = 0; j < S[i].length(); j++) {
			if (S[i][j] == 'a') {
				v[i].push_back(0);
			}
			else if (S[i][j] == 'b') {
				v[i].push_back(1);
			}
			else {
				v[i].push_back(2);
			}
		}
		v[i].push_back(-1);
	}

	long long val = 0;
	long long dval = 0;
	while (v[0].size() * v[1].size() * v[2].size() != 0) {
		dval = v[val].front();
		v[val].pop_front();
		val = dval;
	}

	for (long long i = 0; i < v.size(); i++) {
		if (v[i].size() == 0) {
			if (i == 0) {
				cout << "A" << endl;
				break;
			}
			else if (i == 1) {
				cout << "B" << endl;
				break;
			}
			else {
				cout << "C" << endl;
				break;
			}
		}
	}

	return 0;
}

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, s, n) for (int i = s; i < (int)(n); i++)
#define out(x) cout << x << endl;
#define INF 1ll<<60;
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; }

using namespace std;
using ll = long long;
typedef pair<int, int> P;

int main(){
    vector<string> s(3);
    rep(i, 3) cin >> s[i];

    int turn = 0;

    while(true){
        if(s[turn].empty()){
            cout << char('A'+turn) << endl;
            return 0;
        }
        int n_turn = s[turn][0] - 'a';
        s[turn].erase(0, 1);
        turn = n_turn;
    }

    return 0;
}


#include <cstdio>
#include <algorithm>
using namespace std;

class Dice {
	int num[6];
public:
	void set(int n, int s) {
		num[s] = n;
	}

	void move(char c) {
		if (c == 'E') {
			swap(num[0], num[3]);
			swap(num[3], num[5]);
			swap(num[5], num[2]);
		}
		else if (c == 'N') {
			swap(num[0], num[1]);
			swap(num[1], num[5]);
			swap(num[5], num[4]);
		}
		else if (c == 'S') {
			swap(num[0], num[4]);
			swap(num[4], num[5]);
			swap(num[5], num[1]);
		}
		else if (c == 'W') {
			swap(num[0], num[2]);
			swap(num[2], num[5]);
			swap(num[5], num[3]);
		}
	}

	int up() {
		return num[0];
	}
};

int main() {
	Dice d;
	int n;
	for (int i = 0; i < 6; i++) {
		scanf("%d", &n);
		d.set(n, i);
	}
	getchar();
	char c;
	while (scanf("%c", &c), c != '\n') {
		d.move(c);
	}
	printf("%d\n", d.up());
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
int a,b;
string s;
int main(void){
    cin>>a>>b>>s;
    for(int i=0;i<a+b+1;i++){
        if(i==a){
            if(s[i]!='-'){
                cout<<"No"<<endl;
                return 0;
            }
        }else{
            if(s[i]=='-'){
                cout<<"No"<<endl;
                return 0;
            }
        }
    }
    cout<<"Yes"<<endl;

}