#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
using namespace std;
typedef long long LL;
typedef long double LD;
typedef complex<LD> CMP;
class ICPCBalloons { public:
static const int INF = 0x3fffffff;
int minRepaintings(vector <int> balloonCount, string balloonSize, vector <int> maxAccepted)
{
const int N = balloonCount.size();
const int Q = maxAccepted.size();
vector<int> M, L;
for(int i=0; i<N; ++i)
(balloonSize[i]=='M' ? M : L).push_back(balloonCount[i]);
sort(M.rbegin(), M.rend());
sort(L.rbegin(), L.rend());
sort(maxAccepted.rbegin(), maxAccepted.rend());
int best = INF;
for(int m=0; m<(1<<Q); ++m) {
vector<int> forM, forL;
for(int i=0; i<Q; ++i)
(m & (1<<i) ? forM : forL).push_back(maxAccepted[i]);
best = min(best, cost(M, forM)+cost(L, forL));
}
return best==INF ? -1 : best;
}
int cost(const vector<int>& b, const vector<int> q)
{
int more = 0;
int less = 0;
for(int i=0; i<q.size(); ++i) {
int bi = i<b.size() ? b[i] : 0;
if(bi >= q[i]) {
more += bi - q[i];
} else {
less += q[i] - bi;
}
}
for(int i=q.size(); i<b.size(); ++i)
more += b[i];
return (more>=less ? less : INF);
}
};
// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
{ ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
{ os << "{ ";
for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const int& Expected, const int& Received) {
bool ok = (Expected == Received);
if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl;
cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END verify_case(_, ICPCBalloons().minRepaintings(balloonCount, balloonSize, maxAccepted));}
int main(){
CASE(0)
int balloonCount_[] = {100};
vector <int> balloonCount(balloonCount_, balloonCount_+sizeof(balloonCount_)/sizeof(*balloonCount_));
string balloonSize = "L";
int maxAccepted_[] = {1,2,3,4,5};
vector <int> maxAccepted(maxAccepted_, maxAccepted_+sizeof(maxAccepted_)/sizeof(*maxAccepted_));
int _ = 10;
END
CASE(1)
int balloonCount_[] = {100};
vector <int> balloonCount(balloonCount_, balloonCount_+sizeof(balloonCount_)/sizeof(*balloonCount_));
string balloonSize = "M";
int maxAccepted_[] = {10,20,30,40,50};
vector <int> maxAccepted(maxAccepted_, maxAccepted_+sizeof(maxAccepted_)/sizeof(*maxAccepted_));
int _ = -1;
END
CASE(2)
int balloonCount_[] = {5,6,1,5,6,1,5,6,1};
vector <int> balloonCount(balloonCount_, balloonCount_+sizeof(balloonCount_)/sizeof(*balloonCount_));
string balloonSize = "MLMMLMMLM";
int maxAccepted_[] = {7,7,4,4,7,7};
vector <int> maxAccepted(maxAccepted_, maxAccepted_+sizeof(maxAccepted_)/sizeof(*maxAccepted_));
int _ = 6;
END
CASE(3)
int balloonCount_[] = {100,100};
vector <int> balloonCount(balloonCount_, balloonCount_+sizeof(balloonCount_)/sizeof(*balloonCount_));
string balloonSize = "ML";
int maxAccepted_[] = {50,51,51};
vector <int> maxAccepted(maxAccepted_, maxAccepted_+sizeof(maxAccepted_)/sizeof(*maxAccepted_));
int _ = -1;
END
CASE(4)
int balloonCount_[] = {8,5,1,4,1,1,3,1,3,3,5,4,5,6,9};
vector <int> balloonCount(balloonCount_, balloonCount_+sizeof(balloonCount_)/sizeof(*balloonCount_));
string balloonSize = "MMMLLLMMLLMLMLM";
int maxAccepted_[] = {3,5,3,3,5,6,4,6,4,2,3,7,1,5,2};
vector <int> maxAccepted(maxAccepted_, maxAccepted_+sizeof(maxAccepted_)/sizeof(*maxAccepted_));
int _ = 5;
END
CASE(5)
int balloonCount_[] = {1,18,4,7,19,7,7,1,4,8,10,5,14,13,8,22,6,3,13,5,3,4,2,1,3,15,19,4,5,9,4,11,2,7,12,20,11,26,22,7,2,10,9,20,13,20,2,9,11,9};
vector <int> balloonCount(balloonCount_, balloonCount_+sizeof(balloonCount_)/sizeof(*balloonCount_));
string balloonSize = "LLMLLLLMLLLLLLLLLLLLMLLLLLLLLLLMMLMLLLMLLLLLLLLMLL";
int maxAccepted_[] = {44,59,29,53,16,23,13,14,29,42,13,15,66,4,47};
vector <int> maxAccepted(maxAccepted_, maxAccepted_+sizeof(maxAccepted_)/sizeof(*maxAccepted_));
int _ = 210;
END
/*
CASE(6)
int balloonCount_[] = ;
vector <int> balloonCount(balloonCount_, balloonCount_+sizeof(balloonCount_)/sizeof(*balloonCount_));
string balloonSize = ;
int maxAccepted_[] = ;
vector <int> maxAccepted(maxAccepted_, maxAccepted_+sizeof(maxAccepted_)/sizeof(*maxAccepted_));
int _ = ;
END
CASE(7)
int balloonCount_[] = ;
vector <int> balloonCount(balloonCount_, balloonCount_+sizeof(balloonCount_)/sizeof(*balloonCount_));
string balloonSize = ;
int maxAccepted_[] = ;
vector <int> maxAccepted(maxAccepted_, maxAccepted_+sizeof(maxAccepted_)/sizeof(*maxAccepted_));
int _ = ;
END
*/
}
// END CUT HERE