#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 TomekPhone { public:
int minKeystrokes(vector <int> fr, vector <int> keySizes)
{
vector<int> cand;
for(int i=0; i<keySizes.size(); ++i)
for(int k=1; k<=keySizes[i]; ++k)
cand.push_back(k);
sort(cand.begin(), cand.end());
if(cand.size() < fr.size())
return -1;
int total = 0;
sort(fr.rbegin(), fr.rend());
for(int i=0; i<fr.size(); ++i)
total += fr[i]*cand[i];
return total;
}
};
// 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(_, TomekPhone().minKeystrokes(frequencies, keySizes));}
int main(){
CASE(0)
int frequencies_[] = {7,3,4,1};
vector <int> frequencies(frequencies_, frequencies_+sizeof(frequencies_)/sizeof(*frequencies_));
int keySizes_[] = {2,2};
vector <int> keySizes(keySizes_, keySizes_+sizeof(keySizes_)/sizeof(*keySizes_));
int _ = 19;
END
CASE(1)
int frequencies_[] = {13,7,4,20};
vector <int> frequencies(frequencies_, frequencies_+sizeof(frequencies_)/sizeof(*frequencies_));
int keySizes_[] = {2,1};
vector <int> keySizes(keySizes_, keySizes_+sizeof(keySizes_)/sizeof(*keySizes_));
int _ = -1;
END
CASE(2)
int frequencies_[] = {11,23,4,50,1000,7,18};
vector <int> frequencies(frequencies_, frequencies_+sizeof(frequencies_)/sizeof(*frequencies_));
int keySizes_[] = {3,1,4};
vector <int> keySizes(keySizes_, keySizes_+sizeof(keySizes_)/sizeof(*keySizes_));
int _ = 1164;
END
CASE(3)
int frequencies_[] = {100,1000,1,10};
vector <int> frequencies(frequencies_, frequencies_+sizeof(frequencies_)/sizeof(*frequencies_));
int keySizes_[] = {50};
vector <int> keySizes(keySizes_, keySizes_+sizeof(keySizes_)/sizeof(*keySizes_));
int _ = 1234;
END
CASE(4)
int frequencies_[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50};
vector <int> frequencies(frequencies_, frequencies_+sizeof(frequencies_)/sizeof(*frequencies_));
int keySizes_[] = {10,10,10,10,10,10,10,10};
vector <int> keySizes(keySizes_, keySizes_+sizeof(keySizes_)/sizeof(*keySizes_));
int _ = 3353;
END
/*
CASE(5)
int frequencies_[] = ;
vector <int> frequencies(frequencies_, frequencies_+sizeof(frequencies_)/sizeof(*frequencies_));
int keySizes_[] = ;
vector <int> keySizes(keySizes_, keySizes_+sizeof(keySizes_)/sizeof(*keySizes_));
int _ = ;
END
CASE(6)
int frequencies_[] = ;
vector <int> frequencies(frequencies_, frequencies_+sizeof(frequencies_)/sizeof(*frequencies_));
int keySizes_[] = ;
vector <int> keySizes(keySizes_, keySizes_+sizeof(keySizes_)/sizeof(*keySizes_));
int _ = ;
END
*/
}
// END CUT HERE