#include <cstdio>
#include <algorithm>
#define INF 2147483647
#define max_n 1000000
using namespace std;
int a[max_n+1];
long long psm[max_n+2],nsm[max_n+2];
long long calc(long long a,long long b,long long c,long long d){
	a>b&&(swap(a,b),1),c>d&&(swap(c,d),1);
	return max(b,d)-min(a,c);
}
template<typename T>void minify(T&x,const T&y){
	y<x&&(x=y);
}
int main(){
	int n,i;
	scanf("%d",&n),psm[0]=0;
	for(int i=0; ++i<=n; psm[i]=psm[i-1]+a[i])
		scanf("%d",a+i);
	nsm[n+1]=0;
	for(int i=n+1; --i; nsm[i]=nsm[i+1]+a[i]);
	long long x,y,ans=INF;
	for(int j=0,i=0,k=0; ++j<=n; minify(ans,calc(psm[i],psm[j]-psm[i],psm[k]-psm[j],psm[n]-psm[k]))){
		for(y=(x=psm[j])>>1; psm[++i]<=y; );
		y-psm[i-1]<psm[i]-y&&(--i);
		for(y=(x=nsm[j+1])>>1; nsm[++k+1]>y; );
		nsm[k]-y<=y-nsm[k+1]&&(--k);
//		printf("*(%d,%d,%d)\n",i,j,k);
	}
	printf("%lld\n",ans);
	return 0;
}
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep1(i,n) for(int i=1;i<=(int)(n);i++)
#define all(c) c.begin(),c.end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
using namespace std;
template<class S,class T> ostream& operator<<(ostream& o,const pair<S,T> &p){return o<<"("<<p.fs<<","<<p.sc<<")";}
template<class T> ostream& operator<<(ostream& o,const vector<T> &vc){o<<"sz = "<<vc.size()<<endl<<"[";for(const T& v:vc) o<<v<<",";o<<"]";return o;}
vector<int> c2is[26];
vector<int> ls,rs;
int N;
string S,T;
int solve(){
	cin>>N;
	cin>>S>>T;
	if(S==T){
		return 0;
	}
	rep(i,N){
		c2is[S[i]-'a'].pb(i);
	}
	int now=N-1;
	int r=N-1;
	for(int i=N-2;i>=-1;i--){
		if(i==-1 || T[i]!=T[i+1]){
			int l=i+1;
			int c = T[i+1] -'a';
			int id = upper_bound(all(c2is[c]),min(now,l))-c2is[c].begin()-1;
			if(id==-1){
				return -1;
			}
			int v = c2is[c][id];
			// show(l);
			// show(r);
			// show(v);
			// puts("");
			ls.pb(v);
			rs.pb(r);

			r=i;
			now=v-1;
		}
	}
	reverse(all(ls));
	reverse(all(rs));
	int K=ls.size();
	// show(ls);
	// show(rs);
	rep(i,K) ls[i]-=i;
	int ans=0;
	rep(i,K){
		int r = rs[i];
		int id = lower_bound(all(ls),r-i)-ls.begin()-i;
//		show(id);
		chmax(ans,id);
	}
	return ans;
}
int main(){
	cout<<solve()<<endl;
}