#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
#define int         int64_t
#define ii          pair<int,int>
#define dd          pair<double,double>
#define vi          vector<int>
#define pb          push_back
#define all(x)      (x).begin(), (x).end()
#define checkBit(v, p)      (v&(1LL << p))
#define loop(i,a,b) for(int i = a;i < int(b);i++)
#define x           first
#define y           second
#define chkmin(a,b) (a) = min((a), (b))
#define chkmax(a,b) (a) = max((a), (b))
using namespace std;
int inf = 1e9, mod = 1e9;
vi ps;
inline int sum(int l, int r)
{
    return ps[r + 1] - ps[l];
}

inline int diff(int l, int mid, int r)
{
    return abs(sum(mid, r) - sum(l, mid - 1));
}

int32_t main()
{
    ios_base::sync_with_stdio(false); cout << fixed <<setprecision(0);
    int n; cin >> n;
    ps.resize(n + 1);
    ps[0] = 0;
    vi arr(n);
    for (int i=0;i<n;i++) cin >> arr[i], ps[i + 1] = ps[i] + arr[i];
    vi l(n, -1),r(n, -1);
    l[1] = 1;
    r[n - 2] = n - 1;
    for(int i = 2,ind = 1, s; i < n;i++)
    {
        s = diff(0, ind, i);
        while(ind <= i && s > diff(0, ind + 1, i)) s = diff(0, ++ind, i);
        l[i] = ind;
    }

    for(int i = n - 3,ind = n - 1, s; i >= 0;i--)
    {
        s = diff(i, ind, n - 1);
        while(ind > i && s > diff(i, ind - 1, n - 1)) s = diff(i, --ind, n - 1);
        r[i] = ind;
    }
    int mini = 2e18;
    for(int i = 2; i < n - 1;i++)
    {
        vi vals(4);
        vals[0] = sum(0, l[i - 1] - 1);
        vals[1] = sum(l[i - 1], i - 1);
        vals[2] = sum(i, r[i] - 1);
        vals[3] = sum(r[i], n - 1);
        mini = min(mini, *max_element(all(vals)) - *min_element(all(vals)));
    }
    cout << mini << endl;
    return 0;
}

#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<cmath>
#include<bitset>
#include<deque>
#include<functional>
#include<iterator>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<utility>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
#define a first
#define b second
#define sz(x) (ll)((x).size())
#define pb push_back
#define mp make_pair
#define bg begin()
#define ed end()
#define all(x) (x).bg,(x).ed
#define rep(i,n) for(ll i=0;i<(n);i++)
#define rep1(i,n) for(ll i=1;i<=(n);i++)
#define rrep(i,n) for(ll i=(n)-1;i>=0;i--)
#define rrep1(i,n) for(ll i=(n);i>=1;i--)
#define FOR(i,a,b) for(ll i=(a);i<(b);i++)
const ll MOD=1000000007;
const ll INF=1000000000000000;
template<class T> inline bool chmin(T& a, T b){if(a>b){a=b;return true;}return false;}
template<class T> inline bool chmax(T& a, T b){if(a<b){a=b;return true;}return false;}
ll maxx(ll x,ll y,ll z){return max(max(x,y),z);}
ll minn(ll x,ll y,ll z){return min(min(x,y),z);}
ll gcd(ll x,ll y){if(x%y==0) return y;else return gcd(y,x%y);}
ll lcm(ll x,ll y){return x*(y/gcd(x,y));}
ll digsz(ll x){if(x==0) return 1;else{ll ans=0;while(x){x/=10;ans++;}return ans;}}
ll digsum(ll x){ll sum=0;while(x){sum+=x%10;x/=10;}return sum;}
vector<ll> pw2(62,1);vector<ll> pw10(19,1);


int main(){
    {rep1(i,61) pw2[i]=2*pw2[i-1];}
    {rep1(i,18) pw10[i]=10*pw10[i-1];}
    
    ll N; cin>>N;
    ll ans=INF;
    string s; cin>>s;
    vector<ll> L(sz(s),0);
    vector<ll> R(sz(s),0);
    // 0 1 2 3 ...  sz(s)-1
    rep1(i,sz(s)-1){
        L[i]=L[i-1];
        if(s[i-1]=='W') L[i]++;
    }
    rrep(i,sz(s)-1){
        R[i]=R[i+1];
        if(s[i+1]=='E') R[i]++;
    }
    
    rep(i,sz(s)){
        chmin(ans,L[i]+R[i]);
    }
    cout<<ans<<endl;
}

