#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<unordered_map>
#include<vector>
#include<queue>
#include<numeric>
#include<cmath>
#include<set>
#include<map>
#include<stack>
#include<bitset>

#if DEBUG && !ONLINE_JUDGE
	#include "debugger.cpp"
#else
    #define debug(args...)
	#define printMatrix(args...)
    #define printArray(args...)
    #define printSegmentTree(args...)
#endif

#define pb push_back
#define mp make_pair
#define so sizeof
#define FOR(i,a,b) for(int i=a;i<b;++i)
#define REP(i,n) FOR(i,0,n)
#define all(x) (x).begin(), (x).end()
#define pii pair<int,int>
#define ord(x) x - 'a'

#define lli long long int
#define MOD 1000000007
#define PI 3.14159265358979323846
using namespace std;
const int MAXN = 1e5+5;
const int MAXR = 1;
const int MAXC = 1;
const int MAXBITS = 64;
const int INF = (int)1e9;
int dx[] = {-1,0,1,0};
int dy[] = {0,1,0,-1};
priority_queue<int, vector<int>, greater<int>> q1;
priority_queue<int, vector<int>, less<int>> q2;
void l(){
	debug("****************************************");
}
int arr[MAXN];
int carr[MAXN];
int ccarr[MAXN];
int n;

int getSum(){
	int sum =0;
	FOR(i,0,n){
		sum+=arr[i]*ccarr[i];
	}
	return sum;
}

int solve(){
	FOR(i,0,n){
		ccarr[i] = carr[i];
	}
	int s1 = getSum();

	FOR(i,0,n){
		ccarr[i] = carr[i];
	}
	bool f1=false;
	for(int i = n-1;i>=0;i--){
		if(f1){
			ccarr[i]=1;
		}else{
			if(carr[i]==1){
				f1=true;
				ccarr[i]=0;
			}
		}
	}
	int s2 = getSum();

	FOR(i,0,n){
		ccarr[i] = carr[i];
	}
	for(int i = 0;i<n;i++){
		if(ccarr[i]==1){
			ccarr[i]=0;
			break;
		}else{
			ccarr[i]=1;
		}
	}
	int s3 = getSum();

	debug("S1:", s1, "S2:", s2, "S3:", s3);
	return max(s1, max(s2, s3));

}

int main(void){
	#ifndef ONLINE_JUDGE
        freopen("input.txt", "r+", stdin);
        //freopen("output.txt", "w+", stdout);
    #endif
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cin>>n;
    char c;
    FOR(i,0,n){
    	cin>>arr[i];
    }
    FOR(i,0,n){
    	cin>>c;
    	if(c=='1'){
    		carr[i]=1;
    	}else{
    		carr[i]=0;
    	}
    }
    int ans;
    ans = solve();
    cout<<ans;
	return 0;
}


#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<unordered_map>
#include<vector>
#include<queue>
#include<numeric>
#include<cmath>
#include<set>
#include<map>
#include<stack>
#include<bitset>

#if DEBUG && !ONLINE_JUDGE
	#include "debugger.cpp"
#else
    #define debug(args...)
	#define printMatrix(args...)
    #define printArray(args...)
    #define printSegmentTree(args...)
#endif

#define pb push_back
#define mp make_pair
#define so sizeof
#define FOR(i,a,b) for(int i=a;i<b;++i)
#define REP(i,n) FOR(i,0,n)
#define all(x) (x).begin(), (x).end()
#define pii pair<int,int>
#define ord(x) x - 'a'

#define lli long long int
#define MOD 1000000007
#define PI 3.14159265358979323846
using namespace std;
const int MAXN = 1e5+5;
const int MAXR = 1;
const int MAXC = 1;
const int MAXBITS = 64;
const int INF = (int)1e9;
int dx[] = {-1,0,1,0};
int dy[] = {0,1,0,-1};
priority_queue<int, vector<int>, greater<int>> q1;
priority_queue<int, vector<int>, less<int>> q2;
void l(){
	debug("****************************************");
}
int arr[MAXN];
int carr[MAXN];
int ccarr[MAXN];
int n;

int getSum(){
	l();
	int sum =0;
	printArray(ccarr, n);
	FOR(i,0,n){
		sum+=arr[i]*ccarr[i];
	}
	return sum;
}

int solve(){
	FOR(i,0,n){
		ccarr[i] = carr[i];
	}
	int s1 = getSum();

	FOR(i,0,n){
		ccarr[i] = carr[i];
	}
	bool f1=false;
	for(int i = n-1;i>=0;i--){
		if(f1){
			ccarr[i]=1;
		}else{
			if(carr[i]==1){
				f1=true;
				ccarr[i]=0;
			}
		}
	}
	int s2 = getSum();

	FOR(i,0,n){
		ccarr[i] = carr[i];
	}
	for(int i = 0;i<n;i++){
		if(ccarr[i]==1){
			ccarr[i]=0;
			break;
		}else{
			ccarr[i]=1;
		}
	}
	int s3 = getSum();

	FOR(i,0,n){
		ccarr[i] = carr[i];
	}
	f1 = false;
	bool f2 = false;
	for(int i = n-1;i>=0;i--){
		if(f2){
			ccarr[i]=1;
		}else if(f1){
			if(carr[i]==1){
				f2=true;
				ccarr[i]=0;
			}
		}else{
			if(carr[i]==1){
				f1=true;
			}
		}
	}
	int s4 = getSum();

	debug("S1:", s1, "S2:", s2, "S3:", s3, "S4:", s4);
	return max(max(s1,s2), max(s3,s4));

}

int main(void){
	#ifndef ONLINE_JUDGE
        freopen("input.txt", "r+", stdin);
        //freopen("output.txt", "w+", stdout);
    #endif
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cin>>n;
    char c;
    FOR(i,0,n){
    	cin>>arr[i];
    }
    FOR(i,0,n){
    	cin>>c;
    	if(c=='1'){
    		carr[i]=1;
    	}else{
    		carr[i]=0;
    	}
    }
    int ans;
    ans = solve();
    cout<<ans;
	return 0;
}


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

int main() {

     int n;
         long long int sum=0,max=0;

     cin>>n;
     int a[n],b[n],c[n];
     for(int i=0;i<n;i++)
     {cin>>a[i];}
     b[0]=a[0];
     for(int i=1;i<n;i++)
     b[i]=b[i-1]+a[i];
     string s;
     cin>>s;
     int index;
    for(int i=n-1;i>=0;i--)
    {
        if(s[i]=='1')
        {
            index=i;
            break;
        }
    }
    if(index==0)
    {
        cout<<a[0];
        return 0;
    }
    for(int i=0;i<=index;i++)
    sum+=a[i];
    long long sum1=0;
    for(int i=index;i>=0;i--)
    {
        sum1=0;
        if(s[i]=='1')
        {
            sum1=sum-a[i];
        }
         if(s[i]=='0')
        {
            sum-=a[i];
        }
        if(sum1>max)
        max=sum1;

    }
    cout<<max;
	return 0;
}


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

int main() {

     int n;
         long long int sum=0,max=0;

     cin>>n;
     int a[n],b[n],c[n];
     for(int i=0;i<n;i++)
     {cin>>a[i];}
     b[0]=a[0];
     for(int i=1;i<n;i++)
     b[i]=b[i-1]+a[i];
     string s;
     cin>>s;
     int index=-1,index2=-1;
    for(int i=n-1;i>=0;i--)
    {
        if(s[i]=='1')
        {
            index=i;
            break;
        }
    }
     for(int i=0;i<=index;i++)
    sum+=a[i];
    for(int i=index;i>=0;i--)
    {
        if(s[i]=='0')
        {
            index2=i;
            break;
        }
    }
    if(index2==-1)
    {
        cout<<sum;
        return 0;
    }
    if(index==-1)
    {
        cout<<"0";
        return 0;
    }

    long long sum1=0;
    for(int i=index;i>=0;i--)
    {
        sum1=0;
        if(s[i]=='1')
        {
            sum1=sum-a[i];
        }
         if(s[i]=='0')
        {
            sum-=a[i];
        }
        if(sum1>max)
        max=sum1;

    }
   cout<<max;
	return 0;
}


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

int main() {

     int n;
     cin>>n;
     int a[n],b[n],c[n];
     for(int i=0;i<n;i++)
     cin>>a[i];
     b[0]=a[0];
     for(int i=1;i<n;i++)
     b[i]=b[i-1]+a[i];
     string s;
     cin>>s;
     int index;
    for(int i=n-1;i>=0;i--)
    {
        if(s[i]=='1')
        {
            index=i;
            break;
        }
    }
    long long int sum=0,max=0;
    if(index-1>=0)
    max=b[index-1];
    int index2=-1;
    for(int j=index;j>=0;j--)
    {
        if(s[j]=='0')
        {
            index2=j;
            break;
        }
    }

    for(int i=0;i<=index;i++)
    {
        if(i!=index2)
        sum+=a[i];
    }
    if(sum>max)
    cout<<sum;
    else
    cout<<max;
	return 0;
}


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

int main() {

     int n;
         long long int sum=0,max=0;

     cin>>n;
     int a[n],b[n],c[n];
     for(int i=0;i<n;i++)
     {cin>>a[i];}
     b[0]=a[0];
     for(int i=1;i<n;i++)
     b[i]=b[i-1]+a[i];
     string s;
     cin>>s;
     int index=-1;
    for(int i=n-1;i>=0;i--)
    {
        if(s[i]=='1')
        {
            index=i;
            break;
        }
    }
    if(index==0)
    {
        cout<<a[0];
        return 0;
    }
    if(index==-1)
    {
        cout<<"0";
        return 0;
    }
    for(int i=0;i<=index;i++)
    sum+=a[i];
    long long sum1=0;
    for(int i=index;i>=0;i--)
    {
        sum1=0;
        if(s[i]=='1')
        {
            sum1=sum-a[i];
        }
         if(s[i]=='0')
        {
            sum-=a[i];
        }
        if(sum1>max)
        max=sum1;

    }
    cout<<max;
	return 0;
}


/*
 * Author : underdog_eagle
 */
#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define sz(o) ((int)o.size())
#define eb(o) emplace_back(o)
#define pb(o) push_back(o)
#define all(o) o.begin(), o.end()
#define rep(i, a, b) for(int i = (a); i <= (b); i++)
#define repr(i, a, b) for(int i = (a); i >= (b); i--)
#define repo(i, o) for(auto i = o.begin(); i != o.end(); i++)

typedef long long int ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;

void solve(){
    int n;
    cin >> n;
    vi a(n + 1), b(n + 1);
    rep(i, 0, n - 1) cin >> a[i];
    string s;
    cin >> s;
    //reverse(all(s));
    b[0] = a[0];
    rep(i, 1, n - 1) b[i] = a[i] + b[i - 1];
    ll ans = 0, foo = 0;
    rep(i, 0, n - 1) if(s[i] == '1') ans += a[i];
    repr(i, n - 1, 1){
        if(s[i] == '1') {
            ans = max(ans, (ll) b[i - 1] + foo);
            foo += a[i];
        }
    }
    cout << ans;
}

int main() {
    #ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    #endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cout << setprecision(8);
    clock_t b = clock();
    solve();
    clock_t e = clock();
    cerr << (double(e - b) / CLOCKS_PER_SEC) << " sec";
    return 0;
}


/*
 * Author : underdog_eagle
 */
#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define sz(o) ((int)o.size())
#define eb(o) emplace_back(o)
#define pb(o) push_back(o)
#define all(o) o.begin(), o.end()
#define rep(i, a, b) for(int i = (a); i <= (b); i++)
#define repr(i, a, b) for(int i = (a); i >= (b); i--)
#define repo(i, o) for(auto i = o.begin(); i != o.end(); i++)

typedef long long int ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;

void solve(){
    int n;
    cin >> n;
    vi a(n + 1), b(n + 1);
    rep(i, 0, n - 1) cin >> a[i];
    string s;
    cin >> s;
    //reverse(all(s));
    b[0] = a[0];
    rep(i, 1, n - 1) b[i] = a[i] + b[i - 1];
    ll ans = 0;
    rep(i, 0, n - 1) if(s[i] == '1') ans += a[i];
    repr(i, n - 1, 1){
        if(s[i] == '1')
            ans = max(ans, (ll)b[i - 1]);
    }
    cout << ans;
}

int main() {
    #ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    #endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cout << setprecision(8);
    clock_t b = clock();
    solve();
    clock_t e = clock();
    cerr << (double(e - b) / CLOCKS_PER_SEC) << " sec";
    return 0;
}


/*
 * Author : underdog_eagle
 */
#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define sz(o) ((int)o.size())
#define eb(o) emplace_back(o)
#define pb(o) push_back(o)
#define all(o) o.begin(), o.end()
#define rep(i, a, b) for(int i = (a); i <= (b); i++)
#define repr(i, a, b) for(int i = (a); i >= (b); i--)
#define repo(i, o) for(auto i = o.begin(); i != o.end(); i++)

typedef long long int ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;

void solve(){
    int n;
    cin >> n;
    vi a(n + 1), b(n + 1);
    rep(i, 0, n - 1) cin >> a[i];
    string s;
    cin >> s;
    b[0] = a[0];
    rep(i, 1, n - 1) b[i] = a[i] + b[i - 1];
    ll ans = 0, foo = 0;
    rep(i, 0, n - 1) if(s[i] == '1') ans += a[i];
    repr(i, n - 1, 1){
        if(s[i] == '1') {
            ans = max(ans, (ll) b[i - 1] + foo);
            foo += a[i];
        }
    }
    cout << ans;
}

int main() {
    #ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    #endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cout << setprecision(8);
    clock_t b = clock();
    solve();
    clock_t e = clock();
    cerr << (double(e - b) / CLOCKS_PER_SEC) << " sec";
    return 0;
}


#include<bits/stdc++.h>

using namespace std;
#define ll long long int
ll const mod=1e9;
vector<ll>v1[100001];
vector<ll>v2(100001,0);

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

int main()
{
    ll n;
    cin>>n;
    ll a[n];
    for(int i=0;i<n;i++)
        cin>>a[i];
    string s;
    cin>>s;
    ll i=n-1;
    while(s[i]=='0' && i>=0)
    {
        i--;
    }
    if(i<0)
    {
        cout<<0;
        return 0;
    }
    ll b[i+1]={0};
    for(int j=0;j<i;j++)
    {
        b[j+1]=b[j]+a[j];
    }
    ll ans=b[i],sum=0;
    for(int j=i;j>=0;j--)
    {
        ans=max(ans,max(sum+a[j],sum+b[j]));
        if(s[j]=='1')
            sum+=a[j];
    }
    cout<<ans;
}


#include<bits/stdc++.h>

using namespace std;
#define ll long long int
ll const mod=1e9;
vector<ll>v1[100001];
vector<ll>v2(100001,0);

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

int main()
{
    ll n;
    cin>>n;
    ll a[n];
    for(int i=0;i<n;i++)
        cin>>a[i];
    string s;
    cin>>s;
    ll i=n-1;
    while(s[i]=='0' && i>=0)
    {
        i--;
    }
    if(i<0)
    {
        cout<<0;
        return 0;
    }
    ll x=-a[i],y=0;
    for(;i>=0;i--)
    {
        x+=a[i];
        if(s[i]=='1')
            y+=a[i];
    }
    cout<<max(x,y);
}


#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <iterator>
#include <map>
#include <unordered_map>
#include <numeric>
#include <set>
#include <string>
#include <cmath>
#include <stack>
#include <queue>

using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<long long> vll;
typedef vector<set<int>> graph;

template <typename T>
istream &operator>>(istream &is, vector<T> &arr);
template <typename T>
ostream &operator<<(ostream &os, vector<T> &arr);

#define setup() (ios_base::sync_with_stdio(false), cin.tie(0))
int INF = 1e9 + 5;

bool valid(int i, int j, int n)
{
    return i >= 0 && i < n && j >= 0 && j < n;
}

void flip(char &c)
{
    c = (c == '1' ? '0' : '1');
}

int main()
{
    setup();
    int n;
    cin >> n;
    vi a(n);
    cin >> a;
    string s;
    cin >> s;
    ll sum = 0;
    ll lastLen = 0;
    ll greedySum = 0; //taking everything up to the last 1
    for (int i = 0; i < n; i++)
    {
        if (s[i] == '1')
        {
            lastLen = i;
            sum += a[i];
        }
    }
    for (int i = 0; i < lastLen; i++)
        greedySum += a[i];
    cout << max(greedySum, sum) << endl;
}

/* generic functions */

template <typename T>
istream &operator>>(istream &is, vector<T> &arr)
{
    typename vector<T>::iterator a;
    for (a = arr.begin(); a != arr.end(); a++)
    {
        is >> *a;
    }
    return is;
}

template <typename T>
ostream &operator<<(ostream &os, vector<T> &arr)
{
    typename vector<T>::iterator a;
    for (a = arr.begin(); a != arr.end(); a++)
    {
        os << *a << " ";
    }
    os << endl;
    return os;
}


#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <iterator>
#include <map>
#include <unordered_map>
#include <numeric>
#include <set>
#include <string>
#include <cmath>
#include <stack>
#include <queue>

using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<long long> vll;
typedef vector<set<int>> graph;

template <typename T>
istream &operator>>(istream &is, vector<T> &arr);
template <typename T>
ostream &operator<<(ostream &os, vector<T> &arr);

#define setup() (ios_base::sync_with_stdio(false), cin.tie(0))
int INF = 1e9 + 5;

bool valid(int i, int j, int n)
{
    return i >= 0 && i < n && j >= 0 && j < n;
}

void flip(char &c)
{
    c = (c == '1' ? '0' : '1');
}

int main()
{
    setup();
    int n;
    cin >> n;
    vi a(n);
    cin >> a;
    string s;
    cin >> s;
    ll sum = 0;
    ll lastLen = 0;
    ll Len = 0;
    ll greedySum = 0; //taking everything up to the last 1
    for (int i = 0; i < n; i++)
    {
        if (s[i] == '1')
        {
            lastLen = i;
            sum += a[i];
        }
    }
    while (lastLen > 0 && a[lastLen] == '1')
        lastLen--;
    for (int i = 0; i < lastLen; i++)
        greedySum += a[i];
    cout << max(greedySum, sum) << endl;
}

/* generic functions */

template <typename T>
istream &operator>>(istream &is, vector<T> &arr)
{
    typename vector<T>::iterator a;
    for (a = arr.begin(); a != arr.end(); a++)
    {
        is >> *a;
    }
    return is;
}

template <typename T>
ostream &operator<<(ostream &os, vector<T> &arr)
{
    typename vector<T>::iterator a;
    for (a = arr.begin(); a != arr.end(); a++)
    {
        os << *a << " ";
    }
    os << endl;
    return os;
}


#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <iterator>
#include <map>
#include <unordered_map>
#include <numeric>
#include <set>
#include <string>
#include <cmath>
#include <stack>
#include <queue>

using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<long long> vll;
typedef vector<set<int>> graph;

template <typename T>
istream &operator>>(istream &is, vector<T> &arr);
template <typename T>
ostream &operator<<(ostream &os, vector<T> &arr);

#define setup() (ios_base::sync_with_stdio(false), cin.tie(0))
int INF = 1e9 + 5;

bool valid(int i, int j, int n)
{
    return i >= 0 && i < n && j >= 0 && j < n;
}

void flip(char &c)
{
    c = (c == '1' ? '0' : '1');
}

int main()
{
    setup();
    int n;
    cin >> n;
    vi a(n);
    string s;
    cin >> a >> s;
    vi sum(n);
    sum[0] = a[0];
    int lastLen = 0;
    for (int i = 1; i < n; i++)
        sum[i] = sum[i - 1] + a[i];
    for (int i = 0; i < n; i++)
        if (s[i] == '1')
            lastLen = i;

    vi dp(lastLen + 1);
    if (s[0] == '1')
        dp[0] = a[0];
    for (int i = 1; i <= lastLen; i++)
    {
        if (s[i] == '1')
        {
            dp[i] = max(dp[i - 1] + a[i], sum[i - 1]);
        }
        else
            dp[i] = dp[i - 1];
    }
    cout << dp[lastLen] << endl;
}

/* generic functions */

template <typename T>
istream &operator>>(istream &is, vector<T> &arr)
{
    typename vector<T>::iterator a;
    for (a = arr.begin(); a != arr.end(); a++)
    {
        is >> *a;
    }
    return is;
}

template <typename T>
ostream &operator<<(ostream &os, vector<T> &arr)
{
    typename vector<T>::iterator a;
    for (a = arr.begin(); a != arr.end(); a++)
    {
        os << *a << " ";
    }
    os << endl;
    return os;
}


#include<bits/stdc++.h>
using namespace std;
#define sc(x) scanf("%d",&x);
#define sc2(x,y) scanf("%d%d",&x,&y);
int n;
char ch[10202];
int a[10202];
int cumu[10020];
int getsum(int i, int j)
{
	return cumu[j];
}
int dp[1020][3];
int func(int i, int tight)
{
	if(i<0)
		return 0;
	else
	{
		int &ans=dp[i][tight];
		if(ans!=-1)
			return ans;

		if(tight==1)
		{
			if(ch[i]=='0')
			{
				return ans= func(i-1,tight);
			}
			else
			{
				return ans=max(func(i-1,0),a[i]+func(i-1,1));
			}
		}
		else
		{
			//debug2(i,getsum(0,i));
			return ans=getsum(0,i);
		}
	}
}
int main()
{
	int i,j,t;
	memset(dp,-1,sizeof(dp));
	sc(n);
	for(i=0;i<n;i++)
		sc(a[i]);
	cumu[0]=a[0];
	for(i=1;i<n;i++)
		cumu[i]=cumu[i-1]+a[i];
	scanf("%s",ch);
	int ans=func(n-1,1);
	printf("%d\n",ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define scl(x) scanf("%lld",&x);
#define sc2l(x,y) scanf("%d%d",&x,&y);
#define int long long int
int n;
char ch[100202];
int a[100202];
int cumu[100200];
int getsum(int i, int j)
{
	return cumu[j];
}
int dp[102000][3];
int func(int i, int tight)
{
	if(i<0)
		return 0;
	else
	{
		int &ans=dp[i][tight];
		if(ans!=-1)
			return ans;

		if(tight==1)
		{
			if(ch[i]=='0')
			{
				return ans= func(i-1,tight);
			}
			else
			{
				return ans=max(func(i-1,0),a[i]+func(i-1,1));
			}
		}
		else
		{
			//debug2(i,getsum(0,i));
			return ans=cumu[i];
		}
	}
}
main()
{
	int i,j,t;
	memset(dp,-1,sizeof(dp));
	scl(n);
	for(i=0;i<n;i++)
		scl(a[i]);
	cumu[0]=a[0];
	for(i=1;i<n;i++)
		cumu[i]=cumu[i-1]+a[i];
	scanf("%s",ch);
	int ans=func(n-1,1);
	printf("%lld\n",ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define scl(x) scanf("%lld",&x);
#define sc2l(x,y) scanf("%d%d",&x,&y);
#define int long long int
int n;
char ch[100202];
int a[10202];
int cumu[10020];
int getsum(int i, int j)
{
	return cumu[j];
}
int dp[102000][3];
int func(int i, int tight)
{
	if(i<0)
		return 0;
	else
	{
		int &ans=dp[i][tight];
		if(ans!=-1)
			return ans;

		if(tight==1)
		{
			if(ch[i]=='0')
			{
				return ans= func(i-1,tight);
			}
			else
			{
				return ans=max(func(i-1,0),a[i]+func(i-1,1));
			}
		}
		else
		{
			//debug2(i,getsum(0,i));
			return ans=getsum(0,i);
		}
	}
}
main()
{
	int i,j,t;
	memset(dp,-1,sizeof(dp));
	scl(n);
	for(i=0;i<n;i++)
		scl(a[i]);
	cumu[0]=a[0];
	for(i=1;i<n;i++)
		cumu[i]=cumu[i-1]+a[i];
	scanf("%s",ch);
	int ans=func(n-1,1);
	printf("%lld\n",ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define sc(x) scanf("%d",&x);
#define sc2(x,y) scanf("%d%d",&x,&y);
int n;
char ch[100202];
int a[10202];
int cumu[10020];
int getsum(int i, int j)
{
	return cumu[j];
}
int dp[102000][3];
int func(int i, int tight)
{
	if(i<0)
		return 0;
	else
	{
		int &ans=dp[i][tight];
		if(ans!=-1)
			return ans;

		if(tight==1)
		{
			if(ch[i]=='0')
			{
				return ans= func(i-1,tight);
			}
			else
			{
				return ans=max(func(i-1,0),a[i]+func(i-1,1));
			}
		}
		else
		{
			//debug2(i,getsum(0,i));
			return ans=getsum(0,i);
		}
	}
}
int main()
{
	int i,j,t;
	memset(dp,-1,sizeof(dp));
	sc(n);
	for(i=0;i<n;i++)
		sc(a[i]);
	cumu[0]=a[0];
	for(i=1;i<n;i++)
		cumu[i]=cumu[i-1]+a[i];
	scanf("%s",ch);
	int ans=func(n-1,1);
	printf("%d\n",ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define sc(x) scanf("%d",&x);
#define sc2(x,y) scanf("%d%d",&x,&y);
int n;
char ch[10202];
int a[10202];
int cumu[10020];
int getsum(int i, int j)
{
	return cumu[j];
}
int dp[102000][3];
int func(int i, int tight)
{
	if(i<0)
		return 0;
	else
	{
		int &ans=dp[i][tight];
		if(ans!=-1)
			return ans;

		if(tight==1)
		{
			if(ch[i]=='0')
			{
				return ans= func(i-1,tight);
			}
			else
			{
				return ans=max(func(i-1,0),a[i]+func(i-1,1));
			}
		}
		else
		{
			//debug2(i,getsum(0,i));
			return ans=getsum(0,i);
		}
	}
}
int main()
{
	int i,j,t;
	memset(dp,-1,sizeof(dp));
	sc(n);
	for(i=0;i<n;i++)
		sc(a[i]);
	cumu[0]=a[0];
	for(i=1;i<n;i++)
		cumu[i]=cumu[i-1]+a[i];
	scanf("%s",ch);
	int ans=func(n-1,1);
	printf("%d\n",ans);
	return 0;
}


	#include <bits/stdc++.h>
	#include <cstdio>
	#include <cstring>
	#include <cmath>
	#include <cstring>
	#include <chrono>
	#include <complex>
	#define endl "\n"
	#define ll long long int
	#define vi vector<int>
	#define vll vector<ll>
	#define vvi vector < vi >
	#define pii pair<int,int>
	#define pll pair<long long, long long>
	#define mod 1000000007
	#define inf 1000000000000000001;
	#define all(c) c.begin(),c.end()
	#define mp(x,y) make_pair(x,y)
	#define mem(a,val) memset(a,val,sizeof(a))
	#define eb emplace_back
	#define f first
	#define s second

	using namespace std;
	int main()
	{
		std::ios::sync_with_stdio(false);
		ll n;
		cin >> n;
		vll arr(n);
		for(ll i = 0; i < n ; i++)
		cin >> arr[i];
		string s;
		cin >> s;
		vll cumarr(n);
		for(ll i = 0 ; i < n ; i++){
			if(i == 0)
				cumarr[i] = arr[i];
			else
				cumarr[i] = cumarr[i-1] + arr[i];
		}
		ll dp[n];
		for(ll i = 0 ; i < n ; i++){
			if(i == 0){
				if(s[i] == '1')
					dp[i] = arr[i];
				else
					dp[i] = 0;
			}
			else{
				if(s[i] == '0')
					dp[i] = dp[i-1];
				else
					dp[i] = max(dp[i-1]+arr[i], cumarr[i-1]);
			}
		}
		cout << dp[n-1] << endl;

		return 0;
	}


	#include <bits/stdc++.h>
	#include <cstdio>
	#include <cstring>
	#include <cmath>
	#include <cstring>
	#include <chrono>
	#include <complex>
	#define endl "\n"
	#define ll long long int
	#define vi vector<int>
	#define vll vector<ll>
	#define vvi vector < vi >
	#define pii pair<int,int>
	#define pll pair<long long, long long>
	#define mod 1000000007
	#define inf 1000000000000000001;
	#define all(c) c.begin(),c.end()
	#define mp(x,y) make_pair(x,y)
	#define mem(a,val) memset(a,val,sizeof(a))
	#define eb emplace_back
	#define f first
	#define s second

	using namespace std;
	int main()
	{
		std::ios::sync_with_stdio(false);
		ll n;
		cin >> n;
		vll arr(n);
		for(ll i = 0; i < n ; i++)
		cin >> arr[i];
		string s;
		cin >> s;
		ll s1 = 0, s2 = 0, s3 = 0;
		ll ans = 0;
		for(ll i = 0 ; i < n ; i++){
			if(s[i] == '1'){
				s1+=arr[i];
				s3 = s2;
			}
			s2+=arr[i];

		}
		cout << max(s1,s3) << endl;

		return 0;
	}


	#include <bits/stdc++.h>
	#include <cstdio>
	#include <cstring>
	#include <cmath>
	#include <cstring>
	#include <chrono>
	#include <complex>
	#define endl "\n"
	#define ll long long int
	#define vi vector<int>
	#define vll vector<ll>
	#define vvi vector < vi >
	#define pii pair<int,int>
	#define pll pair<long long, long long>
	#define mod 1000000007
	#define inf 1000000000000000001;
	#define all(c) c.begin(),c.end()
	#define mp(x,y) make_pair(x,y)
	#define mem(a,val) memset(a,val,sizeof(a))
	#define eb emplace_back
	#define f first
	#define s second

	using namespace std;
	int main()
	{
		std::ios::sync_with_stdio(false);
		ll n;
		cin >> n;
		vll arr(n);
		for(ll i = 0; i < n ; i++)
		cin >> arr[i];
		string s;
		cin >> s;
		vll cumarr(n);
		for(ll i = 0 ; i < n ; i++){
			if(i == 0)
				cumarr[i] = arr[i];
			else
				cumarr[i] = cumarr[i-1] + arr[i];
		}
		ll dp;
		for(ll i = 0 ; i < n ; i++){
			if(i == 0){
				if(s[i] == '1')
					dp = arr[i];
				else
					dp = 0;
			}
			else{
				if(s[i] == '1')
					dp = max(dp+arr[i], cumarr[i-1]);
			}
		}
		cout << dp << endl;

		return 0;
	}


#include <bits/stdc++.h>

using namespace std;

typedef complex<int> point;
#define dp(a, b) ((conj(a) * b).real())
#define cp(a, b) ((conj(a) * b).imag())
#define angle(a) (atan2(a.imag(), a.real()))
#define rotate0(a, angle) (a * exp(point(0, angle)))
#define length(a) (hypot(a.imag(), a.real()))
#define rotateAr(a, angle, around) (rotate0(((a) - (around)),(angle)) + around)
#define reflect(a, m) (conj((a)/(m)) * (m))
#define normalize(a) ((a) / length(a));
typedef long long ll;
#define rep(i, start, end) for(int i = start; i < end; ++i)
#define per(i, start, end) for(int i = (int)start-1; i >= end; --i)
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define clr(d, v) memset(d, v, sizeof(d))
const double PI = 3.14159265358979323846;
const double eps = (1e-8);
const int MOD = 1000000007;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
//    freopen("facebook.txt", "w", stdout);
#endif
    int n;
    cin >> n;
    vector<int> a(n);
    rep(i,0,n)
        cin >> a[i];
    string s;
    cin >> s;
    reverse(all(s));
    reverse(all(a));
    int start = 0;
    rep(i,0,sz(s))
    {
        if (s[i] == '1')
        {
            start = 1;
            break;
        }
    }
    int cur = 1;
    int sumOne = 0, sumZero = 0;
    int ans = 0;
    for (int i = start; i < n; ++i)
    {
        if (s[i] == '1' && cur == 1)
        {
            sumOne += a[i];
        }
        else if (s[i] == '0')
        {
            cur = 0;
            sumZero += a[i];
        }

        else if (s[i] == '0' && cur == 0)
        {
            sumZero += a[i];
        }
        else if (s[i] == '1')
        {
            cur = 1;
            if (sumOne >= sumZero)
            {
                ans += sumOne;
                sumOne = 0;
                sumZero = 0;
            }
            else
            {
                ans += sumZero;
                sumOne = sumZero;
                sumZero = 0;
            }
            sumOne += a[i];
        }
    }
    if (s[n-1] == '1')
    {
        for (int i = n - 1; i >= 0; --i)
        {
            if (s[i] == '0')
                break;
            ans += a[i];
        }
    }
    else
    {
        ans += max(sumOne, sumZero);
    }
    cout << ans << '\n';
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef complex<int> point;
#define dp(a, b) ((conj(a) * b).real())
#define cp(a, b) ((conj(a) * b).imag())
#define angle(a) (atan2(a.imag(), a.real()))
#define rotate0(a, angle) (a * exp(point(0, angle)))
#define length(a) (hypot(a.imag(), a.real()))
#define rotateAr(a, angle, around) (rotate0(((a) - (around)),(angle)) + around)
#define reflect(a, m) (conj((a)/(m)) * (m))
#define normalize(a) ((a) / length(a));
typedef long long ll;
#define rep(i, start, end) for(int i = start; i < end; ++i)
#define per(i, start, end) for(int i = (int)start-1; i >= end; --i)
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define clr(d, v) memset(d, v, sizeof(d))
const double PI = 3.14159265358979323846;
const double eps = (1e-8);
const int MOD = 1000000007;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
//    freopen("facebook.txt", "w", stdout);
#endif
    int n;
    cin >> n;
    vector<int> a(n);
    rep(i, 0, n)
    {
        cin >> a[i];
    }
    string s;
    cin >> s;
    reverse(all(s));
    reverse(all(a));
    vector<int> postAll(n), postCur(n);
    postAll[n - 1] = a[n - 1];
    if (s[n - 1] == '1')
        postCur[n - 1] = a[n - 1];
    for (int i = n - 2; i >= 0; --i)
    {
        postAll[i] = postAll[i + 1] + a[i];
        if (s[i] == '1')
        {
            postCur[i] = postCur[i + 1] + a[i];
        }
        else
        {
            postCur[i] = postCur[i + 1];
        }
    }
    int out = postCur[0];
    rep(i, 0, n - 1)
    {
        if (s[i] == '1')
        {
            int ans = postAll[i + 1];
            ans += postCur[0] - postCur[i];
            out = max(ans, out);
        }
    }
    cout << out << endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef complex<int> point;
#define dp(a, b) ((conj(a) * b).real())
#define cp(a, b) ((conj(a) * b).imag())
#define angle(a) (atan2(a.imag(), a.real()))
#define rotate0(a, angle) (a * exp(point(0, angle)))
#define length(a) (hypot(a.imag(), a.real()))
#define rotateAr(a, angle, around) (rotate0(((a) - (around)),(angle)) + around)
#define reflect(a, m) (conj((a)/(m)) * (m))
#define normalize(a) ((a) / length(a));
typedef long long ll;
#define rep(i, start, end) for(int i = start; i < end; ++i)
#define per(i, start, end) for(int i = (int)start-1; i >= end; --i)
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define clr(d, v) memset(d, v, sizeof(d))
const double PI = 3.14159265358979323846;
const double eps = (1e-8);
const int MOD = 1000000007;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
//    freopen("facebook.txt", "w", stdout);
#endif
    int n;
    cin >> n;
    vector<int> a(n);
    rep(i,0,n)
        cin >> a[i];
    string s;
    cin >> s;
    reverse(all(s));
    reverse(all(a));
    int start = n;
    rep(i,0,sz(s))
    {
        if (s[i] == '1')
        {
            start = i;
            break;
        }
    }
    int cur = 1;
    int sumOne = 0, sumZero = 0;
    int ans = 0;
    for (int i = start; i < n; ++i)
    {
        if (s[i] == '1' && cur == 1)
        {
            sumOne += a[i];
        }
        else if (s[i] == '0')
        {
            cur = 0;
            sumZero += a[i];
        }

        else if (s[i] == '0' && cur == 0)
        {
            sumZero += a[i];
        }
        else if (s[i] == '1')
        {
            cur = 1;
            if (sumOne >= sumZero)
            {
                ans += sumOne;
                sumOne = 0;
                sumZero = 0;
            }
            else
            {
                ans += sumZero;
                sumOne = sumZero;
                sumZero = 0;
            }
            sumOne += a[i];
        }
    }
    if (s[n-1] == '1')
    {
        for (int i = n - 1; i >= 0; --i)
        {
            if (s[i] == '0')
                break;
            ans += a[i];
        }
    }
    else
    {
        ans += max(sumOne, sumZero);
    }
    cout << ans << '\n';
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<double> vd;

int const oo = INT_MAX;
int const mod = 1000000007;

int main() {
//	 freopen("input.txt","r",stdin);
//	 freopen("output.txt","w",stdout);
	int n;
	cin >> n;
	int arr[n];
	for(int i=0;i<n;i++) cin>>arr[i];
	string s;
	cin>>s;
	int sum=0,mx=0;
	for (int i = 0; i < n; ++i) {
		if(s[i]=='1')
			mx=max(mx+arr[i],sum);
		sum+=arr[i];
	}
	cout<<mx;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<double> vd;

int const oo = INT_MAX;
int const mod = 1000000007;

int main() {
//	 freopen("input.txt","r",stdin);
//	 freopen("output.txt","w",stdout);
	int n;
	cin >> n;
	int arr[n], s[n + 1];
	s[0] = 0;
	for (int i = 0; i < n; i++) {
		cin >> arr[i];
		s[i + 1] = arr[i] + s[i];
	}
	string bin;
	cin >> bin;
	int sum = 0;
	for (int i = 0; i < n; ++i) {
		if (bin[i] == '1') {
			sum += arr[i];
			arr[i] = sum;
		}
	}
	int temp;
	for (int i = 0; i < n; ++i) {
		if (bin[i] == '1') {
			temp = sum - arr[i] + s[i];
			sum = max(sum, temp);
		}
	}
	cout << sum;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<double> vd;

int const oo = INT_MAX;
int const mod = 1000000007;

int main() {
//	 freopen("input.txt","r",stdin);
//	 freopen("output.txt","w",stdout);
	int n;
	cin >> n;
	int arr[n], s[n + 1];
	s[0] = 0;
	for (int i = 0; i < n; i++) {
		cin >> arr[i];
		s[i + 1] = arr[i] + s[i];
	}
	string bin;
	cin >> bin;
	int sum = 0;
	for (int i = 0; i < n; ++i) {
		if (bin[i] == '1') {
			sum += arr[i];
			arr[i] = sum;
		}
	}
	int temp=0;
	for (int i = 0; i < n; ++i)
		if (bin[i] == '1')
			temp = max(sum - arr[i] + s[i],temp);

	sum = max(temp,sum);

	cout << sum;
}


#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<set>
#include<map>
#include<iterator>
#include<stack>
#include<queue>
#include<utility>
#include<functional>
#include<bitset>
#include<cstring>
using namespace std;

const int N = 1e5 + 5;
int n, sum = 0, ans = -1;
vector< int > data( N ), dp( N );
string s;

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

	cin >> n;
	for( int i = 0; i <= n - 1; ++i )
	{
		cin >> data[ i ];
	}
	cin >> s;
	dp[ 0 ] = data[ 0 ];
	for( int i = 1; i <= n - 1; ++i )
	{
		dp[ i ] = dp[ i - 1 ] + data[ i ];
	}
	for( int i = n - 1; i >= 1; --i )
	{
		if( s[ i ] == '1' )
		{
			ans = max( ans, dp[ i - 1 ] + sum );
			sum += data[ i ];
		}
	}
	ans = max( ans, dp[ 0 ] + sum );
	cout << ans << endl;
	return 0;
}


#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<set>
#include<map>
#include<iterator>
#include<stack>
#include<queue>
#include<utility>
#include<functional>
#include<bitset>
#include<cstring>
using namespace std;

const int N = 1e5 + 5;
int n, sum = 0, ans = -1;
vector< int > data( N ), dp( N );
string s;

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

	cin >> n;
	for( int i = 0; i <= n - 1; ++i )
	{
		cin >> data[ i ];
	}
	cin >> s;
	dp[ 0 ] = data[ 0 ];
	for( int i = 1; i <= n - 1; ++i )
	{
		dp[ i ] = dp[ i - 1 ] + data[ i ];
	}
	for( int i = n - 1; i >= 1; --i )
	{
		if( s[ i ] == '1' )
		{
			ans = max( ans, dp[ i - 1 ] + sum );
			sum += data[ i ];
		}
	}
	if( s[ 0 ] == '1' )
	{
		ans = max( ans, dp[ 0 ] + sum );
	}
	cout << ans << endl;
	return 0;
}


#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<set>
#include<map>
#include<iterator>
#include<stack>
#include<queue>
#include<utility>
#include<functional>
#include<bitset>
#include<cstring>
using namespace std;

const int N = 1e5 + 5;
int n, sum = 0, ans = 0;
vector< int > data( N ), dp( N );
string s;

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

	cin >> n;
	for( int i = 0; i <= n - 1; ++i )
	{
		cin >> data[ i ];
	}
	cin >> s;
	dp[ 0 ] = data[ 0 ];
	for( int i = 1; i <= n - 1; ++i )
	{
		dp[ i ] = dp[ i - 1 ] + data[ i ];
	}
	for( int i = n - 1; i >= 1; --i )
	{
		if( s[ i ] == '1' )
		{
			ans = max( ans, dp[ i - 1 ] + sum );
			sum += data[ i ];
		}
	}
	if( s[ 0 ] == '1' )
	{
		ans = max( ans, dp[ 0 ] + sum );
	}
	cout << ans << endl;
	return 0;
}


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

#define int long long
#define pb push_back
#define inf (int)1e18
#define pi pair<int,int>
#define F first
#define S second
#define N 300005
#define M 1000000007
#define ld  long double

string s;
int a[N];
 main()
{

    int n,i,j,a1=0,a2=0;

    cin>>n;

    for(i=0;i<n;i++)
        {
            cin>>a[i];
            if(i)
                pre[i]=pre[i-1]+a[i];
            else
                pre[i]=a[i];
        }
    cin>>s;

    for(i=n-1;i>=0;i--)
    {
      if(s[i]=='1')
      {
          a1=max(a1,pre[i-1]+x);
          x+=a[i];
      }
    }

    for(i=0;i<s.size();i++)
        a2+=(s[i]=='1')*a[i];
    cout<<max(a1,a2);
    return 0;
}


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

#define int long long
#define pb push_back
#define inf (int)1e18
#define pi pair<int,int>
#define F first
#define S second
#define N 300005
#define M 1000000007
#define ld  long double

string s;
int a[N],pre[N];
 main()
{

    int n,i,j,a1=0,a2=0;

    cin>>n;

    for(i=0;i<n;i++)
        {
            cin>>a[i];
            if(i)
                pre[i]=pre[i-1]+a[i];
            else
                pre[i]=a[i];
        }
    cin>>s;
int x=0;
    for(i=n-1;i>=0;i--)
    {
      if(s[i]=='1')
      {
          a1=max(a1,pre[i-1]+x);
          x+=a[i];
      }
    }

    for(i=0;i<s.size();i++)
        a2+=(s[i]=='1')*a[i];
    cout<<max(a1,a2);
    return 0;
}


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

#define int long long
#define pb push_back
#define inf (int)1e18
#define pi pair<int,int>
#define F first
#define S second
#define N 300005
#define M 1000000007
#define ld  long double

string s;
int a[N];
 main()
{

    int n,i,j,a1=0,a2=0;

    cin>>n;

    for(i=0;i<n;i++)
        cin>>a[i];
    cin>>s;

    for(i=n-1;i>=0;i--)
    {
        if(s[i]=='1')
        {
            j=i;
            break;
        }
    }

    for(i=0;i<j;i++)
        a1+=a[i];

    for(i=0;i<s.size();i++)
        a2+=(s[i]=='1')*a[i];
    cout<<max(a1,a2);
    return 0;
}


#include <bits/stdc++.h>
#define ll long long

using namespace std;

int f[100005],n,a[100005],sum[100005];
string s,s1;
int dp(int i)
{
    if (i>n)
        return 0;
    if (f[i]!=-1)
        return f[i];
    if (s[i]=='0')
        return f[i]=dp(i+1);
    return f[i]=max(dp(i+1)+a[i],sum[n-i]);
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    memset(f,-1,sizeof f);
    cin>>n;
    for (int i=n;i>=1;i--)
    {
        cin>>a[i];
        sum[n-i+1]=sum[n-i]+a[i];
    }

    cin>>s1;
    for (int i=s1.length()-1;i>=0;i--)
        s=s+s1[i];
    s='0'+s;
    cout<<dp(1);
}


#include <bits/stdc++.h>
#define ll long long

using namespace std;

ll f[100005][2],n,a[100005];
string s,s1;
ll dp(int i,int ok)
{
    if (i>n)
        return 0;
    if (f[i][ok]!=-1)
        return f[i][ok];
    ll res=0;
    if (ok)
        for (int j=0;j<=s[i]-'0';j++)
            res=max(res,dp(i+1,ok & (j==s[i]-'0'))+a[i]*j);
    else
        for (int j=0;j<=1;j++)
            res=max(res,dp(i+1,0)+a[i]*j);
    return f[i][ok]=res;
}

int main()
{
    memset(f,-1,sizeof f);
    cin>>n;
    for (int i=n;i>=1;i--)
        cin>>a[i];
    cin>>s1;
    for (int i=s1.length()-1;i>=0;i--)
        s=s+s1[i];
    s='0'+s;
    cout<<dp(1,1);
}


#include <bits/stdc++.h>
#define ll long long

using namespace std;

int f[100005],n,a[100005],sum[100005];
string s,s1;
int dp(int i)
{
    if (i>n)
        return 0;
    if (f[i]!=-1)
        return f[i];
    if (s[i]=='0')
        return f[i]=dp(i+1);
    int res=max(dp(i+1)+a[i],sum[n-i]);
    return f[i]=res;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    memset(f,-1,sizeof f);
    cin>>n;
    for (int i=n;i>=1;i--)
    {
        cin>>a[i];
        sum[n-i+1]=sum[n-i]+a[i];
    }

    cin>>s1;
    for (int i=s1.length()-1;i>=0;i--)
        s=s+s1[i];
    s='0'+s;
    cout<<dp(1);
}


#include<bits/stdc++.h>
using namespace std;
#define fp(i,l,r) for(register int (i)=(l);i<=(r);++(i))
#define fd(i,l,r) for(register int (i)=(l);i>=(r);--(i))
#define fe(i,u) for(register int (i)=front[(u)];(i);(i)=e[(i)].next)
#define mem(a) memset((a),0,sizeof (a))
#define int long long
inline int read(){
    int x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return x*f;
}
void wr(int x){
	if(x<0)putchar('-'),x=-x;
	if(x>=10)wr(x/10);
	putchar('0'+x%10);
}
const int MAXN=2e5+10;
vector<int>E[MAXN],Es[MAXN];
int n,m,ind[MAXN],buc[MAXN],tot,res1[MAXN],res2[MAXN],outd[MAXN];
bool vis[MAXN];
queue<int>q;
inline void adde(int u,int v){
	E[u].push_back(v);Es[v].push_back(u);++ind[v];++outd[u];
}
main(){
	n=read();m=read();
	fp(i,1,m){
		int u=read(),v=read();adde(u,v);
	}
	fp(i,1,n)res1[i]=res2[i]=i;
	fp(i,1,n)if(!ind[i])buc[++tot]=i,q.push(i);
	while(!q.empty()){
		int u=q.front();q.pop();
		for(auto v:E[u]){
			if(!(--ind[v]))q.push(v),buc[++tot]=v;
			res1[v]=min(res1[v],res1[u]);
		}
	}
	if(tot!=n){puts("-1");return 0;}
	fd(i,n,1){
		int u=buc[i];
		for(auto v:Es[u])res2[v]=min(res2[u],res2[v]);
	}
	int ans=0;
	fp(i,1,n)res2[i]=min(res2[i],res1[i]);
	fp(i,1,n)if(res2[i]==i)++ans;
	printf("%lld\n",ans);
	fp(i,1,n)
	if(res2[i]==i)putchar('A');
	else putchar('E');puts("");
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define fp(i,l,r) for(register int (i)=(l);i<=(r);++(i))
#define fd(i,l,r) for(register int (i)=(l);i>=(r);--(i))
#define fe(i,u) for(register int (i)=front[(u)];(i);(i)=e[(i)].next)
#define mem(a) memset((a),0,sizeof (a))
#define int long long
inline int read(){
    int x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return x*f;
}
void wr(int x){
	if(x<0)putchar('-'),x=-x;
	if(x>=10)wr(x/10);
	putchar('0'+x%10);
}
const int MAXN=2e5+10;
vector<int>E[MAXN],Es[MAXN];
int n,m,ind[MAXN],buc[MAXN],tot,res1[MAXN],res2[MAXN],outd[MAXN];
bool vis[MAXN];
queue<int>q;
inline void adde(int u,int v){
	E[u].push_back(v);Es[v].push_back(u);++ind[v];++outd[u];
}
main(){
	n=read();m=read();
	fp(i,1,m){
		int u=read(),v=read();adde(u,v);
	}
	fp(i,1,n)res1[i]=res2[i]=i;
	fp(i,1,n)if(!ind[i])buc[++tot]=i,q.push(i);
	while(!q.empty()){
		int u=q.front();q.pop();
		for(auto v:E[u]){
			if(!(--ind[v]))q.push(v),buc[++tot]=v,res1[v]=min(res1[v],res1[u]);
		}
	}
	if(tot!=n){puts("-1");return 0;}
	fp(i,1,n){
		int u=buc[i];
		for(auto v:Es[u])res2[u]=min(res2[u],res2[v]);
	}
	int ans=0;
	fp(i,1,n)res2[i]=min(res2[i],res1[i]);
	fp(i,1,n)if(res2[i]==i)++ans;
	printf("%lld\n",ans);
	fp(i,1,n)
	if(res2[i]==i)putchar('A');
	else putchar('E');puts("");
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define fp(i,l,r) for(register int (i)=(l);i<=(r);++(i))
#define fd(i,l,r) for(register int (i)=(l);i>=(r);--(i))
#define fe(i,u) for(register int (i)=front[(u)];(i);(i)=e[(i)].next)
#define mem(a) memset((a),0,sizeof (a))
#define int long long
inline int read(){
    int x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return x*f;
}
void wr(int x){
	if(x<0)putchar('-'),x=-x;
	if(x>=10)wr(x/10);
	putchar('0'+x%10);
}
const int MAXN=2e5+10;
vector<int>E[MAXN],Es[MAXN];
int n,m,ind[MAXN],buc[MAXN],tot,res1[MAXN],res2[MAXN],outd[MAXN];
bool vis[MAXN];
queue<int>q;
inline void adde(int u,int v){
	E[u].push_back(v);Es[v].push_back(u);++ind[v];++outd[u];
}
main(){
	n=read();m=read();
	fp(i,1,m){
		int u=read(),v=read();adde(u,v);
	}
	fp(i,1,n)res1[i]=res2[i]=i;
	fp(i,1,n)if(!ind[i])buc[++tot]=i,q.push(i);
	while(!q.empty()){
		int u=q.front();q.pop();
		for(auto v:E[u]){
			if(!(--ind[v]))q.push(v),buc[++tot]=v;
		}
	}
	if(tot!=n){puts("-1");return 0;}
	fd(i,n,1){
		int u=buc[i];
		for(auto v:E[u])res1[u]=min(res1[u],res1[v]);
	}
	fp(i,1,n){
		int u=buc[i];
		for(auto v:Es[u])res2[u]=min(res2[u],res2[v]);
	}
	int ans=0;
	fp(i,1,n)res2[i]=min(res2[i],res1[i]);
	fp(i,1,n)if(res2[i]==i)++ans;
	printf("%lld\n",ans);
	fp(i,1,n)
	if(res2[i]==i)putchar('A');
	else putchar('E');puts("");
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define fp(i,l,r) for(register int (i)=(l);i<=(r);++(i))
#define fd(i,l,r) for(register int (i)=(l);i>=(r);--(i))
#define fe(i,u) for(register int (i)=front[(u)];(i);(i)=e[(i)].next)
#define mem(a) memset((a),0,sizeof (a))
#define int long long
inline int read(){
    int x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return x*f;
}
void wr(int x){
	if(x<0)putchar('-'),x=-x;
	if(x>=10)wr(x/10);
	putchar('0'+x%10);
}
const int MAXN=2e5+10;
vector<int>E[MAXN],Es[MAXN];
int n,m,ind[MAXN],buc[MAXN],tot,res1[MAXN],res2[MAXN],outd[MAXN];
bool vis[MAXN];
queue<int>q;
inline void adde(int u,int v){
	E[u].push_back(v);Es[v].push_back(u);++ind[v];++outd[u];
}
main(){
	n=read();m=read();
	fp(i,1,m){
		int u=read(),v=read();adde(u,v);
	}
	fp(i,1,n)res1[i]=res2[i]=i;
	fp(i,1,n)if(!ind[i])buc[++tot]=i,q.push(i);
	while(!q.empty()){
		int u=q.front();q.pop();
		for(auto v:E[u]){
			if(!(--ind[v]))q.push(v),buc[++tot]=v,res1[v]=min(res1[v],res1[u]);
		}
	}
	if(tot!=n){puts("-1");return 0;}
	fd(i,n,1){
		int u=buc[i];
		for(auto v:E[u])res2[u]=min(res2[u],res2[v]);
	}
	int ans=0;
	fp(i,1,n)res2[i]=min(res2[i],res1[i]);
	fp(i,1,n)if(res2[i]==i)++ans;
	printf("%lld\n",ans);
	fp(i,1,n)
	if(res2[i]==i)putchar('A');
	else putchar('E');puts("");
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define fp(i,l,r) for(register int (i)=(l);i<=(r);++(i))
#define fd(i,l,r) for(register int (i)=(l);i>=(r);--(i))
#define fe(i,u) for(register int (i)=front[(u)];(i);(i)=e[(i)].next)
#define mem(a) memset((a),0,sizeof (a))
#define int long long
inline int read(){
    int x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return x*f;
}
void wr(int x){
	if(x<0)putchar('-'),x=-x;
	if(x>=10)wr(x/10);
	putchar('0'+x%10);
}
const int MAXN=2e5+10;
vector<int>E[MAXN],Es[MAXN];
int n,m,ind[MAXN],buc[MAXN],tot,res1[MAXN],res2[MAXN],outd[MAXN];
bool vis[MAXN];
queue<int>q;
inline void adde(int u,int v){
	E[u].push_back(v);Es[v].push_back(u);++ind[v];++outd[u];
}
main(){
	n=read();m=read();
	fp(i,1,m){
		int u=read(),v=read();adde(u,v);
	}
	fp(i,1,n)res1[i]=res2[i]=i;
	fp(i,1,n)if(!ind[i])buc[++tot]=i,q.push(i);
	while(!q.empty()){
		int u=q.front();q.pop();
		for(auto v:E[u]){
			if(!(--ind[v]))buc[++tot]=v,q.push(v),res1[v]=min(res1[v],res1[u]);
		}
	}
	if(tot!=n){puts("-1");return 0;}
	fd(i,n,1){
		int u=buc[i];
		for(auto v:Es[u])res2[v]=min(res2[v],res2[u]);
	}
	int ans=0;
	fp(i,1,n)res2[i]=min(res2[i],res1[i]);
	fp(i,1,n)if(res2[i]>=i)++ans;
	printf("%lld\n",ans);
	fp(i,1,n)
	if(res2[i]>=i)putchar('A');
	else putchar('E');puts("");
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define fp(i,l,r) for(register int (i)=(l);i<=(r);++(i))
#define fd(i,l,r) for(register int (i)=(l);i>=(r);--(i))
#define fe(i,u) for(register int (i)=front[(u)];(i);(i)=e[(i)].next)
#define mem(a) memset((a),0,sizeof (a))
#define int long long
inline int read(){
    int x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return x*f;
}
void wr(int x){
	if(x<0)putchar('-'),x=-x;
	if(x>=10)wr(x/10);
	putchar('0'+x%10);
}
const int MAXN=2e5+10;
vector<int>E[MAXN],Es[MAXN];
int n,m,ind[MAXN],buc[MAXN],tot,res1[MAXN],res2[MAXN],outd[MAXN];
bool vis[MAXN];
queue<int>q;
inline void adde(int u,int v){
	E[u].push_back(v);Es[v].push_back(u);++ind[v];++outd[u];
}
main(){
	n=read();m=read();
	fp(i,1,m){
		int u=read(),v=read();adde(u,v);
	}
	fp(i,1,n)res1[i]=res2[i]=i;
	fp(i,1,n)if(!ind[i])q.push(i);
	while(!q.empty()){
		int u=q.front();q.pop();buc[++tot]=u;
		for(auto v:E[u]){
			if(!(--ind[v]))q.push(v),res1[v]=min(res1[v],res1[u]);
		}
	}
	if(tot!=n){puts("-1");return 0;}
	fd(i,n,1){
		int u=buc[i];
		for(auto v:Es[u])res2[v]=min(res2[v],res2[u]);
	}
	int ans=0;
	fp(i,1,n)res2[i]=min(res2[i],res1[i]);
	fp(i,1,n)if(res2[i]==i)++ans;
	printf("%lld\n",ans);
	fp(i,1,n)
	if(res2[i]==i)putchar('A');
	else putchar('E');puts("");
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int MAXN=200010;
int n,m,x,y,eg,l[MAXN],hd[MAXN],nx[2*MAXN],ver[2*MAXN],in[MAXN],dp[2][MAXN];
void add_edge (int x,int y) {
	ver[++eg]=y;
	nx[eg]=hd[x];
	hd[x]=eg;
	in[y]++;
	return;
}
bool topo (int k) {
	int cnt=0;
	queue <int> q;
	for (int i=1;i<=n;i++) {
		dp[k][i]=i;
		if (!in[i]) {q.push(i);}
	}
	while (!q.empty()) {
		int a=q.front();
		q.pop();
		cnt++;
		for (int i=hd[a];i;i=nx[i]) {
			dp[k][ver[i]]=min(dp[k][ver[i]],dp[k][a]);
			if (--in[ver[i]]==0) {q.push(ver[i]);}
		}
	}
	return (cnt==n);
}
int main () {
	scanf("%d%d",&n,&m);
	for (int i=1;i<=m;i++) {
		scanf("%d%d",&x,&y);
		l[max(x,y)]=1;
		add_edge(x,y);
	}
	if (!topo(0)) {
		printf("-1\n");
	} else {
		topo(1);
		int ans=0;
		for (int i=1;i<=n;i++) {
			if (min(dp[0][i],dp[1][i])==i) {ans++;}
		}
		printf("%d\n",ans);
		for (int i=1;i<=n;i++) {
			printf("%c",min(dp[0][i],dp[1][i])==i?'A':'E');
		}
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int MAXN=200010;
int n,m,x[MAXN],y[MAXN],eg,l[MAXN],hd[MAXN],nx[2*MAXN],ver[2*MAXN],in[MAXN],dp[2][MAXN];
void add_edge (int x,int y) {
	ver[++eg]=y;
	nx[eg]=hd[x];
	hd[x]=eg;
	in[y]++;
	return;
}
bool topo (int k) {
	int cnt=0;
	queue <int> q;
	for (int i=1;i<=n;i++) {
		dp[k][i]=i;
		if (!in[i]) {q.push(i);}
	}
	while (!q.empty()) {
		int a=q.front();
		q.pop();
		cnt++;
		for (int i=hd[a];i;i=nx[i]) {
			dp[k][ver[i]]=min(dp[k][ver[i]],dp[k][a]);
			if (--in[ver[i]]==0) {q.push(ver[i]);}
		}
	}
	return (cnt==n);
}
int main () {
	scanf("%d%d",&n,&m);
	for (int i=1;i<=m;i++) {
		scanf("%d%d",&x[i],&y[i]);
		l[max(x[i],y[i])]=1;
		add_edge(x[i],y[i]);
	}
	if (!topo(0)) {
		printf("-1\n");
	} else {
		eg=0;
		memset(hd,0,sizeof(hd));
		for (int i=1;i<=m;i++) {
			add_edge(y[i],x[i]);
		}
		topo(1);
		int ans=0;
		for (int i=1;i<=n;i++) {
			if (min(dp[0][i],dp[1][i])==i) {ans++;}
		}
		printf("%d\n",ans);
		for (int i=1;i<=n;i++) {
			printf("%c",min(dp[0][i],dp[1][i])==i?'A':'E');
		}
	}
	return 0;
}


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

template < typename Tp >
void read(Tp &x) {
	x = 0; int fh = 1; char ch = 1;
	while(ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
	if(ch == '-') fh = -1, ch = getchar();
	while(ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
	x *= fh;
}

template < typename Tp >
void biread(Tp &x) {
	x = 0; int fh = 1; char ch = 1;
	while(ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
	if(ch == '-') fh = -1, ch = getchar();
	while(ch >= '0' && ch <= '9') x = x * 2 + ch - '0', ch = getchar();
	x *= fh;
}

const int maxn = 200000 + 7;
const int maxm = 200000 + 7;

struct Graph {
	int Head[maxn], to[maxm], tot, Next[maxm];
	void add(int x, int y) {
		to[++tot] = y, Next[tot] = Head[x], Head[x] = tot;
	}
}G1, G2;

int n, m;
int deg[maxn];

void Init(void) {
	read(n); read(m);
	for(int i = 1, x, y; i <= m; i++) {
		read(x); read(y);
		deg[y]++;
		G1.add(x, y); G2.add(y, x);
	}
}

bool vis[maxn], vis2[maxn];
bool ins[maxn];

void toposort() {
	queue <int> Q; int cnt = 0;
	for(int i = 1; i <= n; i++) {
		if(deg[i] == 0) {
			ins[i] = 0;
			Q.push(i); ++cnt;
		}
	}
	while(!Q.empty()) {
		int x = Q.front(); Q.pop();
		for(int i = G1.Head[x]; i; i = G1.Next[i]) {
			int y = G1.to[i];
			if(ins[y]) continue;
			--deg[y];
			if(deg[y] == 0) {
				ins[y] = true;
				++cnt; Q.push(y);
			}
		}
	}
	if(cnt != n) {
		puts("-1");
		exit(0);
	}
}

void bfs(int S) {
	vis[S] = vis2[S] = true;
	queue <int> Q;
	Q.push(S);
	while(!Q.empty()) {
		int x = Q.front(); Q.pop();
		for(int i = G1.Head[x]; i; i = G1.Next[i]) {
			int y = G1.to[i];
			if(vis[y]) continue;
			vis[y] = true;
			Q.push(y);
		}
	}
	Q.push(S);
	while(!Q.empty()) {
		int x = Q.front(); Q.pop();
		for(int i = G2.Head[x]; i; i = G2.Next[i]) {
			int y = G2.to[i];
			if(vis2[y]) continue;
			vis2[y] = true;
			Q.push(y);
		}
	}
}

bool func[maxn];
int ans;

void Work(void) {
	toposort();
	for(int i = 1;i <= n; i++) {
		if(vis[i] || vis2[i]) func[i] = false;
		else func[i] = true, ans++;
		bfs(i);
	}
	printf("%d\n", ans);
	for(int i = 1; i <= n; i++) {
		if(func[i]) putchar('A');
		else putchar('E');
	}
}

int main(void) {
	Init();
	Work();
	return 0;
}


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

template < typename Tp >
void read(Tp &x) {
	x = 0; int fh = 1; char ch = 1;
	while(ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
	if(ch == '-') fh = -1, ch = getchar();
	while(ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
	x *= fh;
}

template < typename Tp >
void biread(Tp &x) {
	x = 0; int fh = 1; char ch = 1;
	while(ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
	if(ch == '-') fh = -1, ch = getchar();
	while(ch >= '0' && ch <= '9') x = x * 2 + ch - '0', ch = getchar();
	x *= fh;
}

const int maxn = 200000 + 7;
const int maxm = 200000 + 7;

struct Graph {
	int Head[maxn], to[maxm], tot, Next[maxm];
	void add(int x, int y) {
		to[++tot] = y, Next[tot] = Head[x], Head[x] = tot;
	}
}G1, G2;

int n, m;
int deg[maxn];

void Init(void) {
	read(n); read(m);
	for(int i = 1, x, y; i <= m; i++) {
		read(x); read(y);
		deg[y]++;
		G1.add(x, y); G2.add(y, x);
	}
}

bool vis[maxn], vis2[maxn];
bool ins[maxn];

void toposort() {
	queue <int> Q; int cnt = 0;
	for(int i = 1; i <= n; i++) {
		if(deg[i] == 0) {
			ins[i] = 0;
			Q.push(i); ++cnt;
		}
	}
	while(!Q.empty()) {
		int x = Q.front(); Q.pop();
		for(int i = G1.Head[x]; i; i = G1.Next[i]) {
			int y = G1.to[i];
			if(ins[y]) continue;
			--deg[y];
			if(deg[y] == 0) {
				ins[y] = true;
				++cnt; Q.push(y);
			}
		}
	}
	if(cnt != n) {
		puts("-1");
		exit(0);
	}
}

void bfs(int S) {
	vis[S] = vis2[S] = true;
	queue <int> Q;
	Q.push(S);
	while(!Q.empty()) {
		int x = Q.front(); Q.pop();
		for(int i = G1.Head[x]; i; i = G1.Next[i]) {
			int y = G1.to[i];
			if(vis[y]) continue;
			vis[y] = true;
			Q.push(y);
		}
	}
	Q.push(S);
	while(!Q.empty()) {
		int x = Q.front(); Q.pop();
		for(int i = G2.Head[x]; i; i = G2.Next[i]) {
			int y = G2.to[i];
			if(vis2[y]) continue;
			vis[y] = true;
			Q.push(y);
		}
	}
}

bool func[maxn];
int ans;

void Work(void) {
	toposort();
	for(int i = 1;i <= n; i++) {
		if(vis[i] || vis2[i]) func[i] = false;
		else func[i] = true, ans++;
		bfs(i);
	}
	printf("%d\n", ans);
	for(int i = 1; i <= n; i++) {
		if(func[i]) putchar('A');
		else putchar('E');
	}
}

int main(void) {
	Init();
	Work();
	return 0;
}


#include <cstdio>
#include <cstring>
#include <cctype>
#include <iostream>
#include <vector>
#define MAXN 200005
using namespace std;
vector<int> e[MAXN],f[MAXN];
bool vis[MAXN],ins[MAXN],lop;
char s[MAXN];
void dfs(int u)
{
	vis[u]=ins[u]=1;
	for (int i=0;i<(int)e[u].size();i++)
		if (!vis[e[u][i]]) dfs(e[u][i]);
		else
			if (ins[e[u][i]])
				lop=1;
	ins[u]=0;
}
bool mark[2][MAXN];
void dfs(int u,int type)
{
	if (mark[type][u]) return;
	mark[type][u]=true;
	if (type==0)
		for (int i=0;i<(int)e[u].size();i++)
			dfs(e[u][i],type);
	else
		for (int i=0;i<(int)f[u].size();i++)
			dfs(f[u][i],type);
}
int main()
{
	int n,m;
	scanf("%d%d",&n,&m);
	for (int i=1;i<=m;i++)
	{
		int u,v;
		scanf("%d%d",&u,&v);
		e[u].push_back(v),f[v].push_back(u);
	}
	for (int i=1;i<=n;i++)
		if (!vis[i])
			dfs(i);
	if (lop) return puts("-1"),0;
	int cnt=0;
	for (int i=1;i<=n;i++)
	{
		if (mark[0][i]||mark[1][i]) s[i]='E';
		else s[i]='A',++cnt,dfs(i,0),dfs(i,1);
	}
	printf("%d\n",cnt);
	puts(s+1);
	return 0;
}


#include <cstdio>
#include <cstring>
#include <cctype>
#include <iostream>
#include <vector>
#define MAXN 200005
using namespace std;
vector<int> e[MAXN],f[MAXN];
bool vis[MAXN],ins[MAXN],lop;
char s[MAXN];
void dfs(int u)
{
	vis[u]=ins[u]=1;
	for (int i=0;i<(int)e[u].size();i++)
		if (!vis[e[u][i]]) dfs(e[u][i]);
		else
			if (ins[e[u][i]])
				lop=1;
	ins[u]=0;
}
bool mark[2][MAXN];
void dfs(int u,int type)
{
	if (mark[type][u]) return;
	mark[type][u]=true;
	if (type==0)
		for (int i=0;i<(int)e[u].size();i++)
			dfs(e[u][i],type);
	else
		for (int i=0;i<(int)f[u].size();i++)
			dfs(f[u][i],type);
}
int main()
{
	int n,m;
	scanf("%d%d",&n,&m);
	for (int i=1;i<=m;i++)
	{
		int u,v;
		scanf("%d%d",&u,&v);
		e[u].push_back(v),f[v].push_back(u);
	}
	for (int i=1;i<=n;i++)
		if (!vis[i])
			dfs(i);
	if (lop) return puts("-1"),0;
	int cnt=0;
	for (int i=1;i<=n;i++)
	{
		if (mark[0][i]||mark[1][i]) s[i]='E';
		else s[i]='A',++cnt;
		dfs(i,0),dfs(i,1);
	}
	printf("%d\n",cnt);
	puts(s+1);
	return 0;
}


#include<bits/stdc++.h>
#define maxn 400010
using namespace std;
template<typename T> inline void read(T &x)
{
    x=0;char c=getchar();bool flag=false;
    while(!isdigit(c)){if(c=='-')flag=true;c=getchar();}
    while(isdigit(c)){x=(x<<1)+(x<<3)+(c^48);c=getchar();}
    if(flag)x=-x;
}
int n,m,ans;
char s[maxn];
struct node
{
    int deg[maxn],f[maxn];
    struct edge
    {
        int to,nxt;
    }e[maxn];
    int head[maxn],edge_cnt;
    void add(int from,int to)
    {
        e[++edge_cnt]={to,head[from]};
        head[from]=edge_cnt,deg[to]++;
    }
    bool topo()
    {
        queue<int> q;
        for(int i=1;i<=n;++i)
            if(!deg[i])
                q.push(i);
        while(!q.empty())
        {
            int x=q.front();
            q.pop();
            for(int i=head[x];i;i=e[i].nxt)
            {
                int y=e[i].to;
                f[x]=min(f[x],f[y]);
                if(--deg[y]==0) q.push(y);
            }
        }
        for(int i=1;i<=n;++i)
            if(deg[i])
                return false;
        return true;
    }
}A,B;
int main()
{
    read(n),read(m);
    for(int i=1;i<=n;++i) A.f[i]=B.f[i]=i;
    for(int i=1;i<=m;++i)
    {
        int x,y;
        read(x),read(y);
        A.add(x,y),B.add(y,x);
    }
    if(!A.topo())
    {
        puts("-1");
        return 0;
    }
    B.topo();
    for(int i=1;i<=n;++i)
    {
        if(A.f[i]<i||B.f[i]<i) s[i]='E';
        else s[i]='A',ans++;
    }
    printf("%d\n%s",ans,s+1);
    return 0;
}


#include<bits/stdc++.h>
#define maxn 400010
using namespace std;
template<typename T> inline void read(T &x)
{
    x=0;char c=getchar();bool flag=false;
    while(!isdigit(c)){if(c=='-')flag=true;c=getchar();}
    while(isdigit(c)){x=(x<<1)+(x<<3)+(c^48);c=getchar();}
    if(flag)x=-x;
}
int n,m,ans;
char s[maxn];
struct node
{
    int deg[maxn],f[maxn];
    struct edge
    {
        int to,nxt;
    }e[maxn];
    int head[maxn],edge_cnt;
    void add(int from,int to)
    {
        e[++edge_cnt]={to,head[from]};
        head[from]=edge_cnt,deg[to]++;
    }
    bool topo()
    {
        queue<int> q;
        for(int i=1;i<=n;++i)
            if(!deg[i])
                q.push(i);
        while(!q.empty())
        {
            int x=q.front();
            q.pop();
            for(int i=head[x];i;i=e[i].nxt)
            {
                int y=e[i].to;
                f[y]=min(f[y],f[x]);
                if(--deg[y]==0) q.push(y);
            }
        }
        for(int i=1;i<=n;++i)
            if(deg[i])
                return false;
        return true;
    }
}A,B;
int main()
{
    read(n),read(m);
    for(int i=1;i<=n;++i) A.f[i]=B.f[i]=i;
    for(int i=1;i<=m;++i)
    {
        int x,y;
        read(x),read(y);
        A.add(x,y),B.add(y,x);
    }
    if(!A.topo())
    {
        puts("-1");
        return 0;
    }
    B.topo();
    for(int i=1;i<=n;++i)
    {
        if(A.f[i]<i||B.f[i]<i) s[i]='E';
        else s[i]='A',ans++;
    }
    printf("%d\n%s",ans,s+1);
    return 0;
}


#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;
typedef long long ll;
const int maxn=2e5+5;
const int mod=1000007;
vector<int> G[maxn],RG[maxn];
queue<int> Top;
char result[maxn];
int id[maxn],od[maxn],pre[maxn],lst[maxn];
int n,m;

int vis[maxn];
bool dfs(int u){
	vis[u]=1;
	for(int i=0,len=G[u].size();i<len;++i){
		if(vis[G[u][i]]==1||vis[u]!=2&&dfs(G[u][i])==false)
			return false;
	}
	vis[u]=2;
	return true;
}
int tps[maxn];
void topsort(){
	for(int i=1;i<=n;++i) if(od[i]==0) Top.push(i);
	int pos=0,cur;
	while(!Top.empty()){
		cur= Top.front();
		Top.pop();
		tps[++pos]=cur;
		for(int j=0,len=RG[cur].size();j<len;++j){
			if(--od[RG[cur][j]]==0){
				Top.push(RG[cur][j]);
			}
		}
	}
}

int main(){
	scanf("%d%d",&n,&m);
	for(int i=1,u,v;i<=m;++i){
		scanf("%d%d",&u,&v);
		G[v].emplace_back(u);
		RG[u].emplace_back(v);
		++id[u];
		++od[v];
	}
	bool flag=true;
	for(int i=1;i<=n;++i){
		if(!vis[i]){
			if(dfs(i)==false){
				flag=false;
				break;
			}
		}
	}
	if(!flag){
		printf("-1");
		return 0;
	}
	topsort();
	for(int _=1,i,j;_<=n;++_){
		i=tps[_];
		pre[i]=i;
		for(j=0;j<G[i].size();++j){
			pre[i]=min(pre[i],pre[G[i][j]]);
		}
	}
	int ans=0;
	for(int _=n,i,j;_>=1;--_){
		i=tps[_];
		lst[i]=i;
		for(j=0;j<RG[i].size();++j){
			lst[i]=min(lst[i],lst[RG[i][j]]);
		}
		if(pre[i]==lst[i]) ans++;
	}
	printf("%d\n",ans);
	for(int i=1;i<=n;++i){
		if(pre[i]==lst[i]) printf("A");
		else printf("E");
	}
}

/*
4 4
1 4
2 4
3 1
3 2

dag?
self break
son's breaks
2 1
1 2
1
AE

4 3
1 2
2 3
3 1
-1

3 2
1 3
2 3
2
AAE

5 4
1 2
1 3
1 4
1 5

100 50
55 13
84 2
22 63
100 91
2 18
98 64
1 86
93 11
17 6
24 97
14 35
24 74
22 3
42 5
63 79
31 89
81 22
86 88
77 51
81 34
19 55
41 54
34 57
45 9
55 72
67 61
41 84
39 32
51 89
58 74
32 79
65 6
86 64
63 42
100 57
46 39
100 9
23 58
26 81
61 49
71 83
66 2
79 74
30 27
44 52
50 49
88 11
94 89
2 35
80 94

AAAAAAAAAAAAAAAAEEAAAEAAAAAAAEAAAAEAAAEAAEAAEEAAAEAEAEEAEEAAEAEAEEEAAAAEAEAAEAEAEAEEAEAEEAAAEEAAEEAE
AAAAAAAAAAEAAAAAEEEAAEAAAEAAAEAAAEEAAAEAEEAAEEAAAEAEAEEAEEAAEAEEEEEAAAAEAEAAEAEAEAEEAEAEEAAAEEAAEEAE
*/


#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;
typedef long long ll;
const int maxn=2e5+5;
const int mod=1000007;
vector<int> G[maxn],RG[maxn];
queue<int> Top;
char result[maxn];
int id[maxn],od[maxn],pre[maxn],lst[maxn];
int n,m;

int vis[maxn];
bool dfs(int u){
	vis[u]=1;
	for(int i=0,len=G[u].size();i<len;++i){
		if(vis[G[u][i]]==1||vis[u]!=2&&dfs(G[u][i])==false)
			return false;
	}
	vis[u]=2;
	return true;
}
int tps[maxn];
void topsort(){
	for(int i=1;i<=n;++i) if(od[i]==0) Top.push(i);
	int pos=0,cur;
	while(!Top.empty()){
		cur= Top.front();
		Top.pop();
		tps[++pos]=cur;
		for(int j=0,len=RG[cur].size();j<len;++j){
			if(--od[RG[cur][j]]==0){
				Top.push(RG[cur][j]);
			}
		}
	}
}

int main(){
	scanf("%d%d",&n,&m);
	for(int i=1,u,v;i<=m;++i){
		scanf("%d%d",&u,&v);
		G[v].emplace_back(u);
		RG[u].emplace_back(v);
		++id[u];
		++od[v];
	}
	bool flag=true;
	// for(int i=1;i<=n;++i){
	// 	if(!vis[i]){
	// 		if(dfs(i)==false){
	// 			flag=false;
	// 			break;
	// 		}
	// 	}
	// }
	if(!flag){
		printf("-1");
		return 0;
	}
	topsort();
	for(int _=1,i,j;_<=n;++_){
		i=tps[_];
		pre[i]=i;
		for(j=0;j<G[i].size();++j){
			pre[i]=min(pre[i],pre[G[i][j]]);
		}
	}
	int ans=0;
	for(int _=n,i,j;_>=1;--_){
		i=tps[_];
		lst[i]=i;
		for(j=0;j<RG[i].size();++j){
			lst[i]=min(lst[i],lst[RG[i][j]]);
		}
		if(pre[i]==lst[i]) ans++;
	}
	printf("%d\n",ans);
	string output="";
	for(int i=1;i<=n;++i){
		if(pre[i]==lst[i]) output+='A';
		else output+='E';
	}
	cout<<output;
}

/*
4 4
1 4
2 4
3 1
3 2

dag?
self break
son's breaks
2 1
1 2
1
AE

4 3
1 2
2 3
3 1
-1

3 2
1 3
2 3
2
AAE

5 4
1 2
1 3
1 4
1 5

100 50
55 13
84 2
22 63
100 91
2 18
98 64
1 86
93 11
17 6
24 97
14 35
24 74
22 3
42 5
63 79
31 89
81 22
86 88
77 51
81 34
19 55
41 54
34 57
45 9
55 72
67 61
41 84
39 32
51 89
58 74
32 79
65 6
86 64
63 42
100 57
46 39
100 9
23 58
26 81
61 49
71 83
66 2
79 74
30 27
44 52
50 49
88 11
94 89
2 35
80 94

AAAAAAAAAAAAAAAAEEAAAEAAAAAAAEAAAAEAAAEAAEAAEEAAAEAEAEEAEEAAEAEAEEEAAAAEAEAAEAEAEAEEAEAEEAAAEEAAEEAE
AAAAAAAAAAEAAAAAEEEAAEAAAEAAAEAAAEEAAAEAEEAAEEAAAEAEAEEAEEAAEAEEEEEAAAAEAEAAEAEAEAEEAEAEEAAAEEAAEEAE
*/


#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;
typedef long long ll;
const int maxn=2e2+5;
const int mod=1000007;
vector<int> G[maxn],RG[maxn];
queue<int> Top;
char result[maxn];
int id[maxn],od[maxn],tod[maxn],dp[maxn],dv[maxn];
int n,m;

int fa[maxn];
int F(int x){return x==fa[x]?x:fa[x]=F(fa[x]);}
int vis[maxn];
bool dfs(int u){
	vis[u]=1;
	for(int i=0,len=G[u].size();i<len;++i){
		if(vis[G[u][i]]==1||vis[u]!=2&&dfs(G[u][i])==false)
			return false;
	}
	vis[u]=2;
	return true;
}
int tps[maxn];
void topsort(){
	for(int i=1;i<=n;++i) if(od[i]==0) Top.push(i);
	int pos=0,cur;
	while(!Top.empty()){
		cur= Top.front();
		Top.pop();
		tps[++pos]=cur;
		for(int j=0,len=RG[cur].size();j<len;++j){
			if(--tod[RG[cur][j]]==0){
				Top.push(RG[cur][j]);
			}
		}
	}
}

int main(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;++i) fa[i]=i;
	for(int i=1,u,v;i<=m;++i){
		scanf("%d%d",&u,&v);
		G[v].emplace_back(u);
		RG[u].emplace_back(v);
		++id[u];
		++od[v],++tod[v];
	}
	bool flag=true;
	for(int i=1;i<=n;++i){
		if(!vis[i]){
			if(dfs(i)==false){
				flag=false;
				break;
			}
		}
	}
	if(!flag){
		printf("-1");
		return 0;
	}
	topsort();
	for(int _=1,i,ff;_<=n;++_){
		i=tps[_];
		map<int,pair<int,int> > mp;
		for(int j=0;j<G[i].size();++j){
			ff=F(G[i][j]);
			mp[ff]=max(mp[ff],{dp[G[i][j]],G[i][j]});
		}
		for(auto x:mp){
			dp[i]+=x.second.first;
			dv[x.second.second]=x.second.first;
		}
		dp[i]=max(dp[i],1);
		for(int j=0;j<G[i].size();++j){
			ff=F(G[i][j]);
			fa[ff]=i;
		}
	}
	int ans=0;
	for(int i=1;i<=n;++i) if(id[i]==0) ans+=dp[i],dv[i]=dp[i];
	printf("%d\n",ans);
	if(ans!=1){
		for(int _=n,i;_>=1;--_){
			i=tps[_];
			for(int j=0;j<G[i].size();++j){
				if(dv[G[i][j]]==1&&(dv[i]>dv[G[i][j]]||od[G[i][j]]==0)){
					result[G[i][j]]='A';
				}
			}
		}
	}else{
		result[tps[1]]='A';
	}
	for(int i=1;i<=n;++i){
		if(result[i]=='A') printf("%c",result[i]);
		else printf("E");
	}
}

/*
dag?
self break
son's breaks
2 1
1 2
1
AE

4 3
1 2
2 3
3 1
-1

3 2
1 3
2 3
2
AAE

*/


#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;
typedef long long ll;
const int maxn=2e2+5;
const int mod=1000007;
vector<int> G[maxn],RG[maxn];
queue<int> Top;
char result[maxn];
int id[maxn],od[maxn],tod[maxn],pre[maxn],lst[maxn];
int n,m;

int fa[maxn];
int F(int x){return x==fa[x]?x:fa[x]=F(fa[x]);}
int vis[maxn];
bool dfs(int u){
	vis[u]=1;
	for(int i=0,len=G[u].size();i<len;++i){
		if(vis[G[u][i]]==1||vis[u]!=2&&dfs(G[u][i])==false)
			return false;
	}
	vis[u]=2;
	return true;
}
int tps[maxn];
void topsort(){
	for(int i=1;i<=n;++i) if(od[i]==0) Top.push(i);
	int pos=0,cur;
	while(!Top.empty()){
		cur= Top.front();
		Top.pop();
		tps[++pos]=cur;
		for(int j=0,len=RG[cur].size();j<len;++j){
			if(--tod[RG[cur][j]]==0){
				Top.push(RG[cur][j]);
			}
		}
	}
}

int main(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;++i) fa[i]=i;
	for(int i=1,u,v;i<=m;++i){
		scanf("%d%d",&u,&v);
		G[v].emplace_back(u);
		RG[u].emplace_back(v);
		++id[u];
		++od[v],++tod[v];
	}
	bool flag=true;
	for(int i=1;i<=n;++i){
		if(!vis[i]){
			if(dfs(i)==false){
				flag=false;
				break;
			}
		}
	}
	if(!flag){
		printf("-1");
		return 0;
	}
	topsort();
	for(int _=1,i,j;_<=n;++_){
		i=tps[_];
		pre[i]=i;
		for(j=0;j<G[i].size();++j){
			pre[i]=min(pre[i],G[i][j]);
		}
	}
	int ans=0;
	for(int _=n,i,j;_>=1;--_){
		i=tps[_];
		lst[i]=i;
		for(j=0;j<RG[i].size();++j){
			lst[i]=min(lst[i],RG[i][j]);
		}
		if(pre[i]==lst[i]) ans++;
	}
	printf("%d\n",ans);
	for(int i=1;i<=n;++i){
		if(pre[i]==lst[i]) printf("A");
		else printf("E");
	}
}

/*
dag?
self break
son's breaks
2 1
1 2
1
AE

4 3
1 2
2 3
3 1
-1

3 2
1 3
2 3
2
AAE

5 4
1 2
1 3
1 4
1 5

*/


#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;
typedef long long ll;
const int maxn=2e2+5;
const int mod=1000007;
vector<int> G[maxn],RG[maxn];
queue<int> Top;
char result[maxn];
int id[maxn],od[maxn],tod[maxn],dp[maxn],dv[maxn];
int n,m;

int fa[maxn];
int F(int x){return x==fa[x]?x:fa[x]=F(fa[x]);}
int vis[maxn];
bool dfs(int u){
	vis[u]=1;
	for(int i=0,len=G[u].size();i<len;++i){
		if(vis[G[u][i]]==1||vis[u]!=2&&dfs(G[u][i])==false)
			return false;
	}
	vis[u]=2;
	return true;
}
int tps[maxn];
void topsort(){
	for(int i=1;i<=n;++i) if(od[i]==0) Top.push(i);
	int pos=0,cur;
	while(!Top.empty()){
		cur= Top.front();
		Top.pop();
		tps[++pos]=cur;
		for(int j=0,len=RG[cur].size();j<len;++j){
			if(--tod[RG[cur][j]]==0){
				Top.push(RG[cur][j]);
			}
		}
	}
}

int main(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;++i) fa[i]=i;
	for(int i=1,u,v;i<=m;++i){
		scanf("%d%d",&u,&v);
		G[v].emplace_back(u);
		RG[u].emplace_back(v);
		++id[u];
		++od[v],++tod[v];
	}
	bool flag=true;
	for(int i=1;i<=n;++i){
		if(!vis[i]){
			if(dfs(i)==false){
				flag=false;
				break;
			}
		}
	}
	if(!flag){
		printf("-1");
		return 0;
	}
	topsort();
	for(int _=1,i,ff;_<=n;++_){
		i=tps[_];
		map<int,pair<int,int> > mp;
		for(int j=0;j<G[i].size();++j){
			ff=F(G[i][j]);
			mp[ff]=max(mp[ff],{dp[G[i][j]],G[i][j]});
		}
		for(auto x:mp){
			dp[i]+=x.second.first;
			dv[x.second.second]=x.second.first;
		}
		dp[i]=max(dp[i],1);
		for(int j=0;j<G[i].size();++j){
			ff=F(G[i][j]);
			fa[ff]=i;
		}
	}
	int ans=0;
	for(int i=1;i<=n;++i) if(id[i]==0) ans+=dp[i],dv[i]=dp[i];
	printf("%d\n",ans);
	if(ans!=1){
		for(int _=n,i;_>=1;--_){
			i=tps[_];
			for(int j=0;j<G[i].size();++j){
				if(dv[G[i][j]]==1&&dv[i]>dv[G[i][j]]){
					result[G[i][j]]='A';
				}
			}
		}
	}else{
		result[tps[1]]='A';
	}
	for(int i=1;i<=n;++i){
		if(result[i]=='A') printf("%c",result[i]);
		else printf("E");
	}
}

/*
dag?
self break
son's breaks
2 1
1 2
1
AE

4 3
1 2
2 3
3 1
-1

3 2
1 3
2 3
2
AAE

*/


#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;
typedef long long ll;
const int maxn=2e5+5;
const int mod=1000007;
vector<int> G[maxn],RG[maxn];
queue<int> Top;
char result[maxn];
int id[maxn],od[maxn],pre[maxn],lst[maxn];
int n,m;

int vis[maxn];
bool dfs(int u){
	vis[u]=1;
	for(int i=0,len=G[u].size();i<len;++i){
		if(vis[G[u][i]]==1||vis[u]!=2&&dfs(G[u][i])==false)
			return false;
	}
	vis[u]=2;
	return true;
}
int tps[maxn];
void topsort(){
	for(int i=1;i<=n;++i) if(od[i]==0) Top.push(i);
	int pos=0,cur;
	while(!Top.empty()){
		cur= Top.front();
		Top.pop();
		tps[++pos]=cur;
		for(int j=0,len=RG[cur].size();j<len;++j){
			if(--od[RG[cur][j]]==0){
				Top.push(RG[cur][j]);
			}
		}
	}
}

int main(){
	scanf("%d%d",&n,&m);
	for(int i=1,u,v;i<=m;++i){
		scanf("%d%d",&u,&v);
		G[v].emplace_back(u);
		RG[u].emplace_back(v);
		++id[u];
		++od[v];
	}
	bool flag=true;
	for(int i=1;i<=n;++i){
		if(!vis[i]){
			if(dfs(i)==false){
				flag=false;
				break;
			}
		}
	}
	if(!flag){
		printf("-1");
		return 0;
	}
	topsort();
	for(int _=1,i,j;_<=n;++_){
		i=tps[_];
		pre[i]=i;
		for(j=0;j<G[i].size();++j){
			pre[i]=min(pre[i],pre[G[i][j]]);
		}
	}
	int ans=0;
	for(int _=n,i,j;_>=1;--_){
		i=tps[_];
		lst[i]=i;
		for(j=0;j<RG[i].size();++j){
			lst[i]=min(lst[i],lst[RG[i][j]]);
		}
		if(pre[i]==lst[i]) ans++;
	}
	printf("%d\n",ans);
	string ans="";
	for(int i=1;i<=n;++i){
		if(pre[i]==lst[i]) ans+='A';
		else ans+='E';
	}
	cout<<ans;
}

/*
4 4
1 4
2 4
3 1
3 2

dag?
self break
son's breaks
2 1
1 2
1
AE

4 3
1 2
2 3
3 1
-1

3 2
1 3
2 3
2
AAE

5 4
1 2
1 3
1 4
1 5

100 50
55 13
84 2
22 63
100 91
2 18
98 64
1 86
93 11
17 6
24 97
14 35
24 74
22 3
42 5
63 79
31 89
81 22
86 88
77 51
81 34
19 55
41 54
34 57
45 9
55 72
67 61
41 84
39 32
51 89
58 74
32 79
65 6
86 64
63 42
100 57
46 39
100 9
23 58
26 81
61 49
71 83
66 2
79 74
30 27
44 52
50 49
88 11
94 89
2 35
80 94

AAAAAAAAAAAAAAAAEEAAAEAAAAAAAEAAAAEAAAEAAEAAEEAAAEAEAEEAEEAAEAEAEEEAAAAEAEAAEAEAEAEEAEAEEAAAEEAAEEAE
AAAAAAAAAAEAAAAAEEEAAEAAAEAAAEAAAEEAAAEAEEAAEEAAAEAEAEEAEEAAEAEEEEEAAAAEAEAAEAEAEAEEAEAEEAAAEEAAEEAE
*/


#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;
typedef long long ll;
const int maxn=2e5+5;
const int mod=1000007;
vector<int> G[maxn],RG[maxn];
queue<int> Top;
char result[maxn];
int id[maxn],od[maxn],pre[maxn],lst[maxn];
int n,m;

int vis[maxn];
bool dfs(int u){
	vis[u]=1;
	for(int i=0,len=G[u].size();i<len;++i){
		if(vis[G[u][i]]==1||vis[u]!=2&&dfs(G[u][i])==false)
			return false;
	}
	vis[u]=2;
	return true;
}
int tps[maxn];
void topsort(){
	for(int i=1;i<=n;++i) if(od[i]==0) Top.push(i);
	int pos=0,cur;
	while(!Top.empty()){
		cur= Top.front();
		Top.pop();
		tps[++pos]=cur;
		for(int j=0,len=RG[cur].size();j<len;++j){
			if(--od[RG[cur][j]]==0){
				Top.push(RG[cur][j]);
			}
		}
	}
}

int main(){
	scanf("%d%d",&n,&m);
	for(int i=1,u,v;i<=m;++i){
		scanf("%d%d",&u,&v);
		G[v].emplace_back(u);
		RG[u].emplace_back(v);
		++id[u];
		++od[v];
	}
	bool flag=true;
	for(int i=1;i<=n;++i){
		if(!vis[i]){
			if(dfs(i)==false){
				flag=false;
				break;
			}
		}
	}
	if(!flag){
		printf("-1");
		return 0;
	}
	topsort();
	for(int _=1,i,j;_<=n;++_){
		i=tps[_];
		pre[i]=i;
		for(j=0;j<G[i].size();++j){
			pre[i]=min(pre[i],pre[G[i][j]]);
		}
	}
	int ans=0;
	for(int _=n,i,j;_>=1;--_){
		i=tps[_];
		lst[i]=i;
		for(j=0;j<RG[i].size();++j){
			lst[i]=min(lst[i],lst[RG[i][j]]);
		}
		if(pre[i]==lst[i]) ans++;
	}
	printf("%d\n",ans);//?
	for(int i=1;i<=n;++i){
		if(pre[i]==lst[i]) printf("A");
		else printf("E");
	}
}

/*
4 4
1 4
2 4
3 1
3 2

dag?
self break
son's breaks
2 1
1 2
1
AE

4 3
1 2
2 3
3 1
-1

3 2
1 3
2 3
2
AAE

5 4
1 2
1 3
1 4
1 5

100 50
55 13
84 2
22 63
100 91
2 18
98 64
1 86
93 11
17 6
24 97
14 35
24 74
22 3
42 5
63 79
31 89
81 22
86 88
77 51
81 34
19 55
41 54
34 57
45 9
55 72
67 61
41 84
39 32
51 89
58 74
32 79
65 6
86 64
63 42
100 57
46 39
100 9
23 58
26 81
61 49
71 83
66 2
79 74
30 27
44 52
50 49
88 11
94 89
2 35
80 94

AAAAAAAAAAAAAAAAEEAAAEAAAAAAAEAAAAEAAAEAAEAAEEAAAEAEAEEAEEAAEAEAEEEAAAAEAEAAEAEAEAEEAEAEEAAAEEAAEEAE
AAAAAAAAAAEAAAAAEEEAAEAAAEAAAEAAAEEAAAEAEEAAEEAAAEAEAEEAEEAAEAEEEEEAAAAEAEAAEAEAEAEEAEAEEAAAEEAAEEAE
*/


#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;
typedef long long ll;
const int maxn=2e2+5;
const int mod=1000007;
vector<int> G[maxn],RG[maxn];
queue<int> Top;
char result[maxn];
int id[maxn],od[maxn],tod[maxn],dp[maxn],dv[maxn];
int n,m;

int fa[maxn];
int F(int x){return x==fa[x]?x:fa[x]=F(fa[x]);}
int vis[maxn];
bool dfs(int u){
	vis[u]=1;
	for(int i=0,len=G[u].size();i<len;++i){
		if(vis[G[u][i]]==1||vis[u]!=2&&dfs(G[u][i])==false)
			return false;
	}
	vis[u]=2;
	return true;
}
int tps[maxn];
void topsort(){
	for(int i=1;i<=n;++i) if(od[i]==0) Top.push(i);
	int pos=0,cur;
	while(!Top.empty()){
		cur= Top.front();
		Top.pop();
		tps[++pos]=cur;
		for(int j=0,len=RG[cur].size();j<len;++j){
			if(--tod[RG[cur][j]]==0){
				Top.push(RG[cur][j]);
			}
		}
	}
}

int main(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;++i) fa[i]=i;
	for(int i=1,u,v;i<=m;++i){
		scanf("%d%d",&u,&v);
		G[v].emplace_back(u);
		RG[u].emplace_back(v);
		++id[u];
		++od[v],++tod[v];
	}
	bool flag=true;
	for(int i=1;i<=n;++i){
		if(!vis[i]){
			if(dfs(i)==false){
				flag=false;
				break;
			}
		}
	}
	if(!flag){
		printf("-1");
		return 0;
	}
	topsort();
	for(int _=1,i,ff;_<=n;++_){
		i=tps[_];
		map<int,pair<int,int> > mp;
		for(int j=0;j<G[i].size();++j){
			ff=F(G[i][j]);
			mp[ff]=max(mp[ff],{dp[G[i][j]],G[i][j]});
		}
		for(auto x:mp){
			dp[i]+=x.second.first;
			dv[x.second.second]=x.second.first;
		}
		dp[i]=max(dp[i],1);
		for(int j=0;j<G[i].size();++j){
			ff=F(G[i][j]);
			fa[ff]=i;
		}
	}
	int ans=0;
	for(int i=1;i<=n;++i) if(id[i]==0) ans+=dp[i],dv[i]=dp[i];
	printf("%d\n",ans);
	for(int _=n,i;_>=1;--_){
		i=tps[_];
		if(dv[i]==1){
			result[i]='A';
		}
		for(int j=0;j<G[i].size();++j){
			if(dv[G[i][j]]==1){
				if(dv[G[i][j]]<dv[i])
					result[G[i][j]]='A';
				else
					dv[G[i][j]]=0;
			}
		}
	}

	for(int i=1;i<=n;++i){
		if(result[i]=='A') printf("%c",result[i]);
		else printf("E");
	}
}

/*
dag?
self break
son's breaks
2 1
1 2
1
AE

4 3
1 2
2 3
3 1
-1

3 2
1 3
2 3
2
AAE

5 4
1 2
1 3
1 4
1 5

*/


#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;
typedef long long ll;
const int maxn=2e5+5;
const int mod=1000007;
vector<int> G[maxn],RG[maxn];
queue<int> Top;
char result[maxn];
int id[maxn],od[maxn],pre[maxn],lst[maxn];
int n,m;

// int vis[maxn];
// bool dfs(int u){
// 	vis[u]=1;
// 	for(int i=0,len=G[u].size();i<len;++i){
// 		if(vis[G[u][i]]==1||vis[u]!=2&&dfs(G[u][i])==false)
// 			return false;
// 	}
// 	vis[u]=2;
// 	return true;
// }
int tps[maxn];
bool topsort(){
	for(int i=1;i<=n;++i) if(od[i]==0) Top.push(i);
	int pos=0,cur;
	while(!Top.empty()){
		cur= Top.front();
		Top.pop();
		tps[++pos]=cur;
		for(int j=0,len=RG[cur].size();j<len;++j){
			if(--od[RG[cur][j]]==0){
				Top.push(RG[cur][j]);
			}
		}
	}
	return pos==n;
}

int main(){
	scanf("%d%d",&n,&m);
	for(int i=1,u,v;i<=m;++i){
		scanf("%d%d",&u,&v);
		G[v].emplace_back(u);
		RG[u].emplace_back(v);
		++id[u];
		++od[v];
	}
	bool flag=true;
	flag=topsort();
	// for(int i=1;i<=n;++i){
	// 	if(!vis[i]){
	// 		if(dfs(i)==false){
	// 			flag=false;
	// 			break;
	// 		}
	// 	}
	// }
	if(!flag){
		printf("-1");
		return 0;
	}
	// topsort();
	for(int _=1,i,j;_<=n;++_){
		i=tps[_];
		pre[i]=i;
		for(j=0;j<G[i].size();++j){
			pre[i]=min(pre[i],pre[G[i][j]]);
		}
	}
	int ans=0;
	for(int _=n,i,j;_>=1;--_){
		i=tps[_];
		lst[i]=i;
		for(j=0;j<RG[i].size();++j){
			lst[i]=min(lst[i],lst[RG[i][j]]);
		}
		if(pre[i]==lst[i]) ans++;
	}
	printf("%d\n",ans);//?
	for(int i=1;i<=n;++i){
		if(pre[i]==lst[i]) printf("A");
		else printf("E");
	}
}

/*
4 4
1 4
2 4
3 1
3 2

dag?
self break
son's breaks
2 1
1 2
1
AE

4 3
1 2
2 3
3 1
-1

3 2
1 3
2 3
2
AAE

5 4
1 2
1 3
1 4
1 5

100 50
55 13
84 2
22 63
100 91
2 18
98 64
1 86
93 11
17 6
24 97
14 35
24 74
22 3
42 5
63 79
31 89
81 22
86 88
77 51
81 34
19 55
41 54
34 57
45 9
55 72
67 61
41 84
39 32
51 89
58 74
32 79
65 6
86 64
63 42
100 57
46 39
100 9
23 58
26 81
61 49
71 83
66 2
79 74
30 27
44 52
50 49
88 11
94 89
2 35
80 94

AAAAAAAAAAAAAAAAEEAAAEAAAAAAAEAAAAEAAAEAAEAAEEAAAEAEAEEAEEAAEAEAEEEAAAAEAEAAEAEAEAEEAEAEEAAAEEAAEEAE
AAAAAAAAAAEAAAAAEEEAAEAAAEAAAEAAAEEAAAEAEEAAEEAAAEAEAEEAEEAAEAEEEEEAAAAEAEAAEAEAEAEEAEAEEAAAEEAAEEAE
*/


#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;
typedef long long ll;
const int maxn=2e5+5;
const int mod=1000007;
vector<int> G[maxn],RG[maxn];
queue<int> Top;
char result[maxn];
int id[maxn],od[maxn],pre[maxn],lst[maxn];
int n,m;

int vis[maxn];
bool dfs(int u){
	vis[u]=1;
	for(int i=0,len=G[u].size();i<len;++i){
		if(vis[G[u][i]]==1||vis[u]!=2&&dfs(G[u][i])==false)
			return false;
	}
	vis[u]=2;
	return true;
}
int tps[maxn];
void topsort(){
	for(int i=1;i<=n;++i) if(od[i]==0) Top.push(i);
	int pos=0,cur;
	while(!Top.empty()){
		cur= Top.front();
		Top.pop();
		tps[++pos]=cur;
		for(int j=0,len=RG[cur].size();j<len;++j){
			if(--od[RG[cur][j]]==0){
				Top.push(RG[cur][j]);
			}
		}
	}
}

int main(){
	scanf("%d%d",&n,&m);
	for(int i=1,u,v;i<=m;++i){
		scanf("%d%d",&u,&v);
		G[v].emplace_back(u);
		RG[u].emplace_back(v);
		++id[u];
		++od[v];
	}
	bool flag=true;
	for(int i=1;i<=n;++i){
		if(!vis[i]){
			if(dfs(i)==false){
				flag=false;
				break;
			}
		}
	}
	if(!flag){
		printf("-1");
		return 0;
	}
	topsort();
	for(int _=1,i,j;_<=n;++_){
		i=tps[_];
		pre[i]=i;
		for(j=0;j<G[i].size();++j){
			pre[i]=min(pre[i],pre[G[i][j]]);
		}
	}
	int ans=0;
	for(int _=n,i,j;_>=1;--_){
		i=tps[_];
		lst[i]=i;
		for(j=0;j<G[i].size();++j){
			lst[i]=min(lst[i],lst[G[i][j]]);
		}
		if(pre[i]==lst[i]) ans++;
	}
	printf("%d\n",ans);
	for(int i=1;i<=n;++i){
		if(pre[i]==lst[i]) printf("A");
		else printf("E");
	}
}

/*
4 4
1 4
2 4
3 1
3 2

dag?
self break
son's breaks
2 1
1 2
1
AE

4 3
1 2
2 3
3 1
-1

3 2
1 3
2 3
2
AAE

5 4
1 2
1 3
1 4
1 5

100 50
55 13
84 2
22 63
100 91
2 18
98 64
1 86
93 11
17 6
24 97
14 35
24 74
22 3
42 5
63 79
31 89
81 22
86 88
77 51
81 34
19 55
41 54
34 57
45 9
55 72
67 61
41 84
39 32
51 89
58 74
32 79
65 6
86 64
63 42
100 57
46 39
100 9
23 58
26 81
61 49
71 83
66 2
79 74
30 27
44 52
50 49
88 11
94 89
2 35
80 94

AAAAAAAAAAAAAAAAEEAAAEAAAAAAAEAAAAEAAAEAAEAAEEAAAEAEAEEAEEAAEAEAEEEAAAAEAEAAEAEAEAEEAEAEEAAAEEAAEEAE
AAAAAAAAAAEAAAAAEEEAAEAAAEAAAEAAAEEAAAEAEEAAEEAAAEAEAEEAEEAAEAEEEEEAAAAEAEAAEAEAEAEEAEAEEAAAEEAAEEAE
*/


#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;
typedef long long ll;
const int maxn=2e5+5;
const int mod=1000007;
vector<int> G[maxn],RG[maxn];
queue<int> Top;
char result[maxn];
int id[maxn],od[maxn],pre[maxn],lst[maxn];
int n,m;

int vis[maxn];
bool dfs(int u){
	vis[u]=1;
	for(int i=0,len=G[u].size();i<len;++i){
		if(vis[G[u][i]]==1||vis[u]!=2&&dfs(G[u][i])==false)
			return false;
	}
	vis[u]=2;
	return true;
}
int tps[maxn];
void topsort(){
	for(int i=1;i<=n;++i) if(od[i]==0) Top.push(i);
	int pos=0,cur;
	while(!Top.empty()){
		cur= Top.front();
		Top.pop();
		tps[++pos]=cur;
		for(int j=0,len=RG[cur].size();j<len;++j){
			if(--od[RG[cur][j]]==0){
				Top.push(RG[cur][j]);
			}
		}
	}
}

int main(){
	scanf("%d%d",&n,&m);
	for(int i=1,u,v;i<=m;++i){
		scanf("%d%d",&u,&v);
		G[v].emplace_back(u);
		RG[u].emplace_back(v);
		++id[u];
		++od[v];
	}
	bool flag=true;
	for(int i=1;i<=n;++i){
		if(!vis[i]){
			if(dfs(i)==false){
				flag=false;
				break;
			}
		}
	}
	if(!flag){
		printf("-1");
		return 0;
	}
	topsort();
	for(int _=1,i,j;_<=n;++_){
		i=tps[_];
		pre[i]=i;
		for(j=0;j<G[i].size();++j){
			pre[i]=min(pre[i],pre[G[i][j]]);
		}
	}
	int ans=0;
	for(int _=n,i,j;_>=1;--_){
		i=tps[_];
		lst[i]=i;
		for(j=0;j<G[i].size();++j){
			lst[i]=min(lst[i],lst[G[i][j]]);
		}
		if(pre[i]==lst[i]) ans++;
	}
	printf("%d\n",ans);
	string ans="";
	for(int i=1;i<=n;++i){
		if(pre[i]==lst[i]) printf("A");
		else printf("E");
	}
}

/*
4 4
1 4
2 4
3 1
3 2

dag?
self break
son's breaks
2 1
1 2
1
AE

4 3
1 2
2 3
3 1
-1

3 2
1 3
2 3
2
AAE

5 4
1 2
1 3
1 4
1 5

100 50
55 13
84 2
22 63
100 91
2 18
98 64
1 86
93 11
17 6
24 97
14 35
24 74
22 3
42 5
63 79
31 89
81 22
86 88
77 51
81 34
19 55
41 54
34 57
45 9
55 72
67 61
41 84
39 32
51 89
58 74
32 79
65 6
86 64
63 42
100 57
46 39
100 9
23 58
26 81
61 49
71 83
66 2
79 74
30 27
44 52
50 49
88 11
94 89
2 35
80 94

AAAAAAAAAAAAAAAAEEAAAEAAAAAAAEAAAAEAAAEAAEAAEEAAAEAEAEEAEEAAEAEAEEEAAAAEAEAAEAEAEAEEAEAEEAAAEEAAEEAE
AAAAAAAAAAEAAAAAEEEAAEAAAEAAAEAAAEEAAAEAEEAAEEAAAEAEAEEAEEAAEAEEEEEAAAAEAEAAEAEAEAEEAEAEEAAAEEAAEEAE
*/


#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;
typedef long long ll;
const int maxn=2e5+5;
const int mod=1000007;
vector<int> G[maxn],RG[maxn];
queue<int> Top;
char result[maxn];
int id[maxn],od[maxn],pre[maxn],lst[maxn];
int n,m;

int vis[maxn];
bool dfs(int u){
	vis[u]=1;
	for(int i=0,len=G[u].size();i<len;++i){
		if(vis[G[u][i]]==1||vis[u]!=2&&dfs(G[u][i])==false)
			return false;
	}
	vis[u]=2;
	return true;
}
int tps[maxn];
void topsort(){
	for(int i=1;i<=n;++i) if(od[i]==0) Top.push(i);
	int pos=0,cur;
	while(!Top.empty()){
		cur= Top.front();
		Top.pop();
		tps[++pos]=cur;
		for(int j=0,len=RG[cur].size();j<len;++j){
			if(--od[RG[cur][j]]==0){
				Top.push(RG[cur][j]);
			}
		}
	}
}

int main(){
	scanf("%d%d",&n,&m);
	for(int i=1,u,v;i<=m;++i){
		scanf("%d%d",&u,&v);
		G[v].emplace_back(u);
		RG[u].emplace_back(v);
		++id[u];
		++od[v];
	}
	bool flag=true;
	for(int i=1;i<=n;++i){
		if(!vis[i]){
			if(dfs(i)==false){
				flag=false;
				break;
			}
		}
	}
	if(!flag){
		printf("-1");
		return 0;
	}
	topsort();
	for(int _=1,i,j;_<=n;++_){
		i=tps[_];
		pre[i]=i;
		for(j=0;j<G[i].size();++j){
			pre[i]=min(pre[i],pre[G[i][j]]);
		}
	}
	int ans=0;
	for(int _=n,i,j;_>=1;--_){
		i=tps[_];
		lst[i]=i;
		for(j=0;j<RG[i].size();++j){
			lst[i]=min(lst[i],lst[RG[i][j]]);
		}
		if(pre[i]==lst[i]) ans++;
	}
	printf("%d\n",ans);
	string output="";
	for(int i=1;i<=n;++i){
		if(pre[i]==lst[i]) output+='A';
		else output+='E';
	}
	cout<<output;
}

/*
4 4
1 4
2 4
3 1
3 2

dag?
self break
son's breaks
2 1
1 2
1
AE

4 3
1 2
2 3
3 1
-1

3 2
1 3
2 3
2
AAE

5 4
1 2
1 3
1 4
1 5

100 50
55 13
84 2
22 63
100 91
2 18
98 64
1 86
93 11
17 6
24 97
14 35
24 74
22 3
42 5
63 79
31 89
81 22
86 88
77 51
81 34
19 55
41 54
34 57
45 9
55 72
67 61
41 84
39 32
51 89
58 74
32 79
65 6
86 64
63 42
100 57
46 39
100 9
23 58
26 81
61 49
71 83
66 2
79 74
30 27
44 52
50 49
88 11
94 89
2 35
80 94

AAAAAAAAAAAAAAAAEEAAAEAAAAAAAEAAAAEAAAEAAEAAEEAAAEAEAEEAEEAAEAEAEEEAAAAEAEAAEAEAEAEEAEAEEAAAEEAAEEAE
AAAAAAAAAAEAAAAAEEEAAEAAAEAAAEAAAEEAAAEAEEAAEEAAAEAEAEEAEEAAEAEEEEEAAAAEAEAAEAEAEAEEAEAEEAAAEEAAEEAE
*/


#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;
typedef long long ll;
const int maxn=2e5+5;
const int mod=1000007;
vector<int> G[maxn],RG[maxn];
queue<int> Top;
char result[maxn];
int id[maxn],od[maxn],pre[maxn],lst[maxn];
int n,m;

int vis[maxn];
bool dfs(int u){
	vis[u]=1;
	for(int i=0,len=G[u].size();i<len;++i){
		if(vis[G[u][i]]==1||vis[u]!=2&&dfs(G[u][i])==false)
			return false;
	}
	vis[u]=2;
	return true;
}
int tps[maxn];
void topsort(){
	for(int i=1;i<=n;++i) if(od[i]==0) Top.push(i);
	int pos=0,cur;
	while(!Top.empty()){
		cur= Top.front();
		Top.pop();
		tps[++pos]=cur;
		for(int j=0,len=RG[cur].size();j<len;++j){
			if(--od[RG[cur][j]]==0){
				Top.push(RG[cur][j]);
			}
		}
	}
}

int main(){
	scanf("%d%d",&n,&m);
	for(int i=1,u,v;i<=m;++i){
		scanf("%d%d",&u,&v);
		G[v].emplace_back(u);
		RG[u].emplace_back(v);
		++id[u];
		++od[v];
	}
	bool flag=true;
	for(int i=1;i<=n;++i){
		if(!vis[i]){
			if(dfs(i)==false){
				flag=false;
				break;
			}
		}
	}
	if(!flag){
		printf("-1");
		return 0;
	}
	topsort();
	for(int _=1,i,j;_<=n;++_){
		i=tps[_];
		pre[i]=i;
		for(j=0;j<G[i].size();++j){
			pre[i]=min(pre[i],pre[G[i][j]]);
		}
	}
	int ans=0;
	for(int _=n,i,j;_>=1;--_){
		i=tps[_];
		lst[i]=i;
		for(j=0;j<RG[i].size();++j){
			lst[i]=min(lst[i],lst[RG[i][j]]);
		}
		if(pre[i]==lst[i]) ans++;
	}
	printf("%d\n",ans);
	string ans="";
	for(int i=1;i<=n;++i){
		if(pre[i]==lst[i]) ans+="A";
		else ans+="E";
	}
	cout<<ans;
}

/*
4 4
1 4
2 4
3 1
3 2

dag?
self break
son's breaks
2 1
1 2
1
AE

4 3
1 2
2 3
3 1
-1

3 2
1 3
2 3
2
AAE

5 4
1 2
1 3
1 4
1 5

100 50
55 13
84 2
22 63
100 91
2 18
98 64
1 86
93 11
17 6
24 97
14 35
24 74
22 3
42 5
63 79
31 89
81 22
86 88
77 51
81 34
19 55
41 54
34 57
45 9
55 72
67 61
41 84
39 32
51 89
58 74
32 79
65 6
86 64
63 42
100 57
46 39
100 9
23 58
26 81
61 49
71 83
66 2
79 74
30 27
44 52
50 49
88 11
94 89
2 35
80 94

AAAAAAAAAAAAAAAAEEAAAEAAAAAAAEAAAAEAAAEAAEAAEEAAAEAEAEEAEEAAEAEAEEEAAAAEAEAAEAEAEAEEAEAEEAAAEEAAEEAE
AAAAAAAAAAEAAAAAEEEAAEAAAEAAAEAAAEEAAAEAEEAAEEAAAEAEAEEAEEAAEAEEEEEAAAAEAEAAEAEAEAEEAEAEEAAAEEAAEEAE
*/


#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;
typedef long long ll;
const int maxn=2e5+5;
const int mod=1000007;
vector<int> G[maxn],RG[maxn];
queue<int> Top;
char result[maxn];
int id[maxn],od[maxn],pre[maxn],lst[maxn];
int n,m;

int vis[maxn];
bool dfs(int u){
	vis[u]=1;
	for(int i=0,len=G[u].size();i<len;++i){
		if(vis[G[u][i]]==1||vis[G[u][i]]!=2&&dfs(G[u][i])==false)
			return false;
	}
	vis[u]=2;
	return true;
}
int tps[maxn];
bool topsort(){
	for(int i=1;i<=n;++i) if(od[i]==0) Top.push(i);
	int pos=0,cur;
	while(!Top.empty()){
		cur= Top.front();
		Top.pop();
		tps[++pos]=cur;
		for(int j=0,len=RG[cur].size();j<len;++j){
			if(--od[RG[cur][j]]==0){
				Top.push(RG[cur][j]);
			}
		}
	}
	return pos==n;
}

int main(){
	scanf("%d%d",&n,&m);
	for(int i=1,u,v;i<=m;++i){
		scanf("%d%d",&u,&v);
		G[v].emplace_back(u);
		RG[u].emplace_back(v);
		++id[u];
		++od[v];
	}
	bool flag=true;
	// flag=topsort();
	for(int i=1;i<=n;++i){
		if(!vis[i]){
			if(dfs(i)==false){
				flag=false;
				break;
			}
		}
	}
	if(!flag){
		printf("-1");
		return 0;
	}
	topsort();
	for(int _=1,i,j;_<=n;++_){
		i=tps[_];
		pre[i]=i;
		for(j=0;j<G[i].size();++j){
			pre[i]=min(pre[i],pre[G[i][j]]);
		}
	}
	int ans=0;
	for(int _=n,i,j;_>=1;--_){
		i=tps[_];
		lst[i]=i;
		for(j=0;j<RG[i].size();++j){
			lst[i]=min(lst[i],lst[RG[i][j]]);
		}
		if(pre[i]==lst[i]) ans++;
	}
	printf("%d\n",ans);//?
	for(int i=1;i<=n;++i){
		if(pre[i]==lst[i]) printf("A");
		else printf("E");
	}
}

/*
4 4
1 4
2 4
3 1
3 2

dag?
self break
son's breaks
2 1
1 2
1
AE

4 3
1 2
2 3
3 1
-1

3 2
1 3
2 3
2
AAE

5 4
1 2
1 3
1 4
1 5

100 50
55 13
84 2
22 63
100 91
2 18
98 64
1 86
93 11
17 6
24 97
14 35
24 74
22 3
42 5
63 79
31 89
81 22
86 88
77 51
81 34
19 55
41 54
34 57
45 9
55 72
67 61
41 84
39 32
51 89
58 74
32 79
65 6
86 64
63 42
100 57
46 39
100 9
23 58
26 81
61 49
71 83
66 2
79 74
30 27
44 52
50 49
88 11
94 89
2 35
80 94

AAAAAAAAAAAAAAAAEEAAAEAAAAAAAEAAAAEAAAEAAEAAEEAAAEAEAEEAEEAAEAEAEEEAAAAEAEAAEAEAEAEEAEAEEAAAEEAAEEAE
AAAAAAAAAAEAAAAAEEEAAEAAAEAAAEAAAEEAAAEAEEAAEEAAAEAEAEEAEEAAEAEEEEEAAAAEAEAAEAEAEAEEAEAEEAAAEEAAEEAE
*/


#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;
typedef long long ll;
const int maxn=2e5+5;
const int mod=1000007;
vector<int> G[maxn],RG[maxn];
queue<int> Top;
char result[maxn];
int id[maxn],od[maxn],pre[maxn],lst[maxn];
int n,m;

int vis[maxn];
bool dfs(int u){
	vis[u]=1;
	for(int i=0,len=G[u].size();i<len;++i){
		if(vis[G[u][i]]==1||vis[G[u][i]]!=2&&dfs(G[u][i])==false)
			return false;
	}
	vis[u]=2;
	return true;
}
int tps[maxn];
bool topsort(){
	for(int i=1;i<=n;++i) if(od[i]==0) Top.push(i);
	int pos=0,cur;
	while(!Top.empty()){
		cur= Top.front();
		Top.pop();
		tps[++pos]=cur;
		for(int j=0,len=RG[cur].size();j<len;++j){
			if(--od[RG[cur][j]]==0){
				Top.push(RG[cur][j]);
			}
		}
	}
	return pos==n;
}

int main(){
	scanf("%d%d",&n,&m);
	for(int i=1,u,v;i<=m;++i){
		scanf("%d%d",&u,&v);
		G[v].emplace_back(u);
		RG[u].emplace_back(v);
		++id[u];
		++od[v];
	}
	bool flag=true;
	// flag=topsort();
	for(int i=1;i<=n;++i){
		if(!vis[i]){
			if(dfs(i)==false){
				flag=false;
				break;
			}
		}
	}
	if(!flag){
		printf("-1");
		return 0;
	}
	topsort();
	for(int _=1,i,j;_<=n;++_){
		i=tps[_];
		pre[i]=i;
		for(j=0;j<G[i].size();++j){
			pre[i]=min(pre[i],pre[G[i][j]]);
		}
	}
	int ans=0;
	for(int _=n,i,j;_>=1;--_){
		i=tps[_];
		lst[i]=i;
		for(j=0;j<RG[i].size();++j){
			lst[i]=min(lst[i],lst[RG[i][j]]);
		}
		if(pre[i]==lst[i]) ans++;
	}
	printf("%d\n",ans);//?
	for(int i=1;i<=n;++i){
		if(pre[i]==lst[i]) printf("A");
		else printf("E");
	}
}

/*
4 4
1 4
2 4
3 1
3 2

dag?
self break
son's breaks
2 1
1 2
1
AE

4 3
1 2
2 3
3 1
-1

3 2
1 3
2 3
2
AAE

5 4
1 2
1 3
1 4
1 5

100 50
55 13
84 2
22 63
100 91
2 18
98 64
1 86
93 11
17 6
24 97
14 35
24 74
22 3
42 5
63 79
31 89
81 22
86 88
77 51
81 34
19 55
41 54
34 57
45 9
55 72
67 61
41 84
39 32
51 89
58 74
32 79
65 6
86 64
63 42
100 57
46 39
100 9
23 58
26 81
61 49

80 94

AAAAAAAAAAAAAAAAEEAAAEAAAAAAAEAAAAEAAAEAAEAAEEAAAEAEAEEAEEAAEAEAEEEAAAAEAEAAEAEAEAEEAEAEEAAAEEAAEEAE
AAAAAAAAAAEAAAAAEEEAAEAAAEAAAEAAAEEAAAEAEEAAEEAAAEAEAEEAEEAAEAEEEEEAAAAEAEAAEAEAEAEEAEAEEAAAEEAAEEAE
*/


#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;
typedef long long ll;
const int maxn=2e5+5;
const int mod=1000007;
vector<int> G[maxn],RG[maxn];
queue<int> Top;
char result[maxn];
int id[maxn],od[maxn],pre[maxn],lst[maxn];
int n,m;

int vis[maxn];
bool dfs(int u){
	vis[u]=1;
	for(int i=0,len=G[u].size();i<len;++i){
		if(vis[G[u][i]]==1||vis[u]!=2&&dfs(G[u][i])==false)
			return false;
	}
	vis[u]=2;
	return true;
}
int tps[maxn];
void topsort(){
	for(int i=1;i<=n;++i) if(od[i]==0) Top.push(i);
	int pos=0,cur;
	while(!Top.empty()){
		cur= Top.front();
		Top.pop();
		tps[++pos]=cur;
		for(int j=0,len=RG[cur].size();j<len;++j){
			if(--od[RG[cur][j]]==0){
				Top.push(RG[cur][j]);
			}
		}
	}
}

int main(){
	scanf("%d%d",&n,&m);
	for(int i=1,u,v;i<=m;++i){
		scanf("%d%d",&u,&v);
		G[v].push_back(u);
		RG[u].push_back(v);
		++id[u];
		++od[v];
	}
	bool flag=true;
	for(int i=1;i<=n;++i){
		if(!vis[i]){
			if(dfs(i)==false){
				flag=false;
				break;
			}
		}
	}
	if(!flag){
		printf("-1");
		return 0;
	}
	topsort();
	for(int _=1,i,j;_<=n;++_){
		i=tps[_];
		pre[i]=i;
		for(j=0;j<G[i].size();++j){
			pre[i]=min(pre[i],pre[G[i][j]]);
		}
	}
	int ans=0;
	for(int _=n,i,j;_>=1;--_){
		i=tps[_];
		lst[i]=i;
		for(j=0;j<RG[i].size();++j){
			lst[i]=min(lst[i],lst[RG[i][j]]);
		}
		if(pre[i]==lst[i]) ans++;
	}
	printf("%d\n",ans);//?
	for(int i=1;i<=n;++i){
		if(pre[i]==lst[i]) printf("A");
		else printf("E");
	}
}

/*
4 4
1 4
2 4
3 1
3 2

dag?
self break
son's breaks
2 1
1 2
1
AE

4 3
1 2
2 3
3 1
-1

3 2
1 3
2 3
2
AAE

5 4
1 2
1 3
1 4
1 5

100 50
55 13
84 2
22 63
100 91
2 18
98 64
1 86
93 11
17 6
24 97
14 35
24 74
22 3
42 5
63 79
31 89
81 22
86 88
77 51
81 34
19 55
41 54
34 57
45 9
55 72
67 61
41 84
39 32
51 89
58 74
32 79
65 6
86 64
63 42
100 57
46 39
100 9
23 58
26 81
61 49
71 83
66 2
79 74
30 27
44 52
50 49
88 11
94 89
2 35
80 94

AAAAAAAAAAAAAAAAEEAAAEAAAAAAAEAAAAEAAAEAAEAAEEAAAEAEAEEAEEAAEAEAEEEAAAAEAEAAEAEAEAEEAEAEEAAAEEAAEEAE
AAAAAAAAAAEAAAAAEEEAAEAAAEAAAEAAAEEAAAEAEEAAEEAAAEAEAEEAEEAAEAEEEEEAAAAEAEAAEAEAEAEEAEAEEAAAEEAAEEAE
*/


#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
int n,m,a[200005],b[200005],ans;
bool vis[200005],in[200005],f[200005],flag;
vector<int> ed[200005];
void dfs(int x,int fa)
{
	vis[x]=in[x]=true;
	for(int i=0;i<ed[x].size();i++)
	{
		if(ed[x][i]==fa) continue;
		if(in[ed[x][i]])
		{
			flag=true;
			break;
		}
		if(!vis[ed[x][i]]) dfs(ed[x][i],x);
	}
	in[x]=false;
}
int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=m;i++)
	{
		scanf("%d%d",&a[i],&b[i]);
		ed[a[i]].push_back(b[i]);
	}
	for(int i=1;i<=n;i++) if(!vis[i] && !flag) dfs(i,0);
	if(flag)
	{
		printf("-1\n");
		return 0;
	}
	for(int i=1;i<=m;i++) f[max(a[i],b[i])]=true;
	for(int i=1;i<=n;i++) if(!f[i]) ans++;
	printf("%d\n",ans);
	for(int i=1;i<=n;i++)
	{
		if(f[i]) printf("E");
		else printf("A");
	}
	return 0;
}


#include<cstdio>
#include<algorithm>
#include<cstring>
#include<map>
using namespace std;
struct node
{
	int x,y;
	int dir;//0:N 1:S 2:W 3:E
};
bool operator < (node x,node y)
{
	if(x.x!=y.x) return x.x<y.x;
	if(x.y!=y.y) return x.y<y.y;
	if(x.dir!=y.dir) return x.dir<y.dir;
	return false;
}
int n,len,x,y,ans;
char s[200005];
map<node,bool> mp;
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		mp.clear();
		ans=x=y=0;
		scanf("%s",s+1);
		len=strlen(s+1);
		for(int j=1;j<=len;j++)
		{
			ans+=5;
			if(s[j]=='N')
			{
				if(mp[(node){x,y,0}]) ans-=4;
				mp[(node){x,y,0}]=true;
				mp[(node){x+1,y,1}]=true;
				x++;
			}
			if(s[j]=='S')
			{
				if(mp[(node){x,y,1}]) ans-=4;
				mp[(node){x,y,1}]=true;
				mp[(node){x-1,y,0}]=true;
				x--;
			}
			if(s[j]=='W')
			{
				if(mp[(node){x,y,3}]) ans-=4;
				mp[(node){x,y,3}]=true;
				mp[(node){x,y+1,4}]=true;
				y++;
			}
			if(s[j]=='E')
			{
				if(mp[(node){x,y,4}]) ans-=4;
				mp[(node){x,y,4}]=true;
				mp[(node){x,y-1,3}]=true;
				y--;
			}
		}
		printf("%d\n",ans);
	}
}


#include<cstdio>
#include<vector>
#include<algorithm>
#include<cstring>
using namespace std;
int n,m,a[200005],b[200005],ans;
bool vis[200005],in[200005],f[200005],flag;
vector<int> ed1[200005],ed2[200005];
void dfs(int x)
{
	vis[x]=in[x]=true;
	for(int i=0;i<ed1[x].size();i++)
	{
		if(in[ed1[x][i]])
		{
			flag=true;
			break;
		}
		if(!vis[ed1[x][i]]) dfs(ed1[x][i]);
	}
	in[x]=false;
}
bool vis1[200005],vis2[200005];
void dfs1(int x)
{
	vis1[x]=true;
	f[x]=true;
	for(int i=0;i<ed1[x].size();i++)
	{
		if(!vis1[ed1[x][i]]) dfs1(ed1[x][i]);
	}
}
void dfs2(int x)
{
	vis2[x]=true;
	f[x]=true;
	for(int i=0;i<ed2[x].size();i++)
	{
		if(!vis2[ed2[x][i]]) dfs2(ed2[x][i]);
	}
}
int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=m;i++)
	{
		scanf("%d%d",&a[i],&b[i]);
		ed1[a[i]].push_back(b[i]);
		ed2[b[i]].push_back(a[i]);
	}
	for(int i=1;i<=n;i++) if(!vis[i] && !flag) dfs(i);
	if(flag)
	{
		printf("-1\n");
		return 0;
	}
	memset(vis,false,sizeof(vis));
	for(int i=1;i<=n;i++)
	{
		bool tmp=!f[i];
		if(!f[i]) ans++;
		dfs1(i);
		dfs2(i);
		if(tmp) f[i]=false;
	}
	printf("%d\n",ans);
	for(int i=1;i<=n;i++)
	{
		if(f[i]) printf("E");
		else printf("A");
	}
	return 0;
}


#include<cstdio>
#include<vector>
#include<algorithm>
#include<cstring>
using namespace std;
int n,m,a[200005],b[200005],ans;
bool vis[200005],in[200005],f[200005],flag;
vector<int> ed1[200005],ed2[200005];
void dfs(int x)
{
	vis[x]=in[x]=true;
	for(int i=0;i<ed1[x].size();i++)
	{
		if(in[ed1[x][i]])
		{
			flag=true;
			break;
		}
		if(!vis[ed1[x][i]]) dfs(ed1[x][i]);
	}
	in[x]=false;
}
bool vis1[200005],vis2[200005];
void dfs1(int x)
{
	vis1[x]=true;
	f[x]=true;
	for(int i=0;i<ed1[x].size();i++)
	{
		if(!vis1[ed1[x][i]]) dfs(ed1[x][i]);
	}
}
void dfs2(int x)
{
	vis2[x]=true;
	f[x]=true;
	for(int i=0;i<ed2[x].size();i++)
	{
		if(!vis2[ed2[x][i]]) dfs(ed2[x][i]);
	}
}
int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=m;i++)
	{
		scanf("%d%d",&a[i],&b[i]);
		ed1[a[i]].push_back(b[i]);
	}
	for(int i=1;i<=n;i++) if(!vis[i] && !flag) dfs(i);
	if(flag)
	{
		printf("-1\n");
		return 0;
	}
	for(int i=1;i<=m;i++) f[max(a[i],b[i])]=true;
	memset(vis,false,sizeof(vis));
	for(int i=1;i<=n;i++)
	{
		bool tmp=!f[i];
		if(!f[i]) ans++;
		dfs1(i);
		dfs2(i);
		if(tmp) f[i]=false;
	}
	printf("%d\n",ans);
	for(int i=1;i<=n;i++)
	{
		if(f[i]) printf("E");
		else printf("A");
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<vector>
using namespace std;
const int N=200005;
int n,m;
int in[N];
int f[N],g[N];
vector<int> E[N],G[N];
vector<int> topo()
{
	vector<int>q;
	q.clear();
	for(int i=1;i<=n;i++)
		if(in[i]==0) q.push_back(i);
	for(int u:q)
		for(int v:E[u])
		{
			in[v]--;
			if(in[v]==0) q.push_back(v);
		}
	if(q.size()!=n)
	{
		printf("-1");
		exit(0);
	}
	return q;
}
int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=m;i++)
	{
		int x,y;
		scanf("%d%d",&x,&y);
		E[x].push_back(y);
		G[y].push_back(x);
		in[y]++;
	}
	vector<int> s=topo();
	for(int i=1;i<=n;i++)
		f[i]=g[i]=i;
	for(int u:s)
		for(int v:G[u]) f[u]=min(f[u],f[v]);
	for(int i=s.size()-1;i>=0;i--)
	{
		int u=s[i];
		for(int v:E[u]) g[u]=min(g[u],g[v]);
	}
	int cnt=0;
	for(int i=1;i<=n;i++)
		if(min(f[i],g[i])==i) cnt++;
	printf("%d\n",cnt);
	for(int i=1;i<=n;i++)
		if(min(f[i],g[i])==i) printf("A");
		else printf("E");
	return 0;
}