#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
using ll = long long;
using P = pair<int,int>;
int main() {
  const ll LINF = 1LL<<62;
  int n,t;
  cin >> n >> t;
  vector<int> a(n);
  rep(i,n) cin >> a[i];
  int l = n/2;
  int r = n-l;
  set<ll> sa;
  for (int s=0; s<(1<<l); s++) {
    ll sum = 0;
    rep(i,l) {
      if ((s>>i)%2 == 1) sum += a[i];
    }
    sa.insert(sum);
  }
  set<ll> sb;
  for (int s=0; s<(1<<r); s++) {
    ll sum = 0;
    rep(i,r) {
      if ((s>>i)%2 == 1) sum += a[l+i];
    }
    sb.insert(sum);
  }
  vector<ll> vb;
  vb.push_back(-LINF);
  for (ll e : sb) vb.push_back(e);
  ll ans = 0;
  for (ll x : sa) {
    int j = upper_bound(vb.begin(), vb.end(), t-x) - vb.begin() - 1;
    ll y = vb[j];
    ans = max(ans, x+y);
  }
  cout << ans << endl;
  return 0;
}

#include<bits/stdc++.h>
namespace my_std{
	using namespace std;
	#define pii pair<int,int>
	#define fir first
	#define sec second
	#define MP make_pair
	#define rep(i,x,y) for (int i=(x);i<=(y);i++)
	#define drep(i,x,y) for (int i=(x);i>=(y);i--)
	#define go(x) for (int i=head[x];i;i=edge[i].nxt)
	#define templ template<typename T>
	#define sz 202020
	typedef long long ll;
	typedef double db;
	mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
	templ inline T rnd(T l,T r) {return uniform_int_distribution<T>(l,r)(rng);}
	templ inline bool chkmax(T &x,T y){return x<y?x=y,1:0;}
	templ inline bool chkmin(T &x,T y){return x>y?x=y,1:0;}
	templ inline void read(T& t)
	{
		t=0;char f=0,ch=getchar();double d=0.1;
		while(ch>'9'||ch<'0') f|=(ch=='-'),ch=getchar();
		while(ch<='9'&&ch>='0') t=t*10+ch-48,ch=getchar();
		if(ch=='.'){ch=getchar();while(ch<='9'&&ch>='0') t+=d*(ch^48),d*=0.1,ch=getchar();}
		t=(f?-t:t);
	}
	template<typename T,typename... Args>inline void read(T& t,Args&... args){read(t); read(args...);}
	char __sr[1<<21],__z[20];int __C=-1,__zz=0;
	inline void Ot(){fwrite(__sr,1,__C+1,stdout),__C=-1;}
	inline void print(int x)
	{
		if(__C>1<<20)Ot();if(x<0)__sr[++__C]='-',x=-x;
		while(__z[++__zz]=x%10+48,x/=10);
		while(__sr[++__C]=__z[__zz],--__zz);__sr[++__C]='\n';
	}
	void file()
	{
		#ifdef NTFOrz
		freopen("a.in","r",stdin);
		#endif
	}
	inline void chktime()
	{
		#ifdef NTFOrz
		cerr<<clock()/1000.0<<'\n';
		#endif
	}
	#ifdef mod
	ll ksm(ll x,int y){ll ret=1;for (;y;y>>=1,x=x*x%mod) if (y&1) ret=ret*x%mod;return ret;}
	ll inv(ll x){return ksm(x,mod-2);}
	#else
	ll ksm(ll x,int y){ll ret=1;for (;y;y>>=1,x=x*x) if (y&1) ret=ret*x;return ret;}
	#endif
//	inline ll mul(ll a,ll b){ll d=(ll)(a*(double)b/mod+0.5);ll ret=a*b-d*mod;if (ret<0) ret+=mod;return ret;}
}
using namespace my_std;

int n;
vector<ll>a[3];

int main()
{
	file();
	map<char,int>mp; mp['R']=0,mp['G']=1,mp['B']=2;
	read(n);
	ll x; char cc;
	rep(i,1,n*2) read(x),cin>>cc,a[mp[cc]].push_back(x);
	rep(i,0,2) sort(a[i].begin(),a[i].end());
	auto qq=[&](int id,ll w)
	{
		if (!a[id].size()) return ll(1e18);
		int t=lower_bound(a[id].begin(),a[id].end(),w)-a[id].begin(); ll res=1e18;
		if (t) chkmin(res,w-a[id][t-1]);
		if (t!=(int)a[id].size()) chkmin(res,a[id][t]-w);
		return res;
	};
	int c=0,p=0; rep(i,0,2) if (a[i].size()%2==0u) ++c,p=i; if (c==3) return puts("0"),0;
	ll ans=1e18;
	rep(i,0,2) if (i!=p) for (auto x:a[i]) chkmin(ans,qq(3-i-p,x));
	int p1=(p==0?1:0),p2=3-p-p1;
	auto wk=[&](){ll mn=1e18;for (auto x:a[p]) chkmin(ans,mn+qq(p2,x)),chkmin(mn,qq(p1,x));};
	wk(); swap(p1,p2); wk();
	cout<<ans;
	return 0;
}