#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 MonstersValley { public:
	int minimumPrice(vector<long long> dread, vector <int> price)
	{
		const int N = dread.size();

		map<int, LL> p2maxd;
		p2maxd[0] = 0;

		for(int i=0; i<N; ++i) {
			map<int,LL> neo;
			for(map<int,LL>::iterator it=p2maxd.begin(); it!=p2maxd.end(); ++it)
			{
				int p = it->first;
				LL md = it->second;
				if( md >= dread[i] )
					neo[p] = max(neo[p], md);
				p += price[i];
				md += dread[i];
				neo[p] = max(neo[p], md);
			}
			p2maxd.swap(neo);
		}

		return p2maxd.begin()->first;
	}
};

// 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(_, MonstersValley().minimumPrice(dread, price));}
int main(){

CASE(0)
	long long dread_[] = {8, 5, 10};
	  vector<long long> dread(dread_, dread_+sizeof(dread_)/sizeof(*dread_)); 
	int price_[] = {1, 1, 2};
	  vector <int> price(price_, price_+sizeof(price_)/sizeof(*price_)); 
	int _ = 2; 
END
CASE(0)
	long long dread_[] = {1, 1};
	  vector<long long> dread(dread_, dread_+sizeof(dread_)/sizeof(*dread_)); 
	int price_[] = {1, 1};
	  vector <int> price(price_, price_+sizeof(price_)/sizeof(*price_)); 
	int _ = 1; 
END
CASE(1)
	long long dread_[] = {1, 2, 4, 1000000000};
	  vector<long long> dread(dread_, dread_+sizeof(dread_)/sizeof(*dread_)); 
	int price_[] = {1, 1, 1, 2};
	  vector <int> price(price_, price_+sizeof(price_)/sizeof(*price_)); 
	int _ = 5; 
END
CASE(2)
	long long dread_[] = {200, 107, 105, 206, 307, 400};
	  vector<long long> dread(dread_, dread_+sizeof(dread_)/sizeof(*dread_)); 
	int price_[] = {1, 2, 1, 1, 1, 2};
	  vector <int> price(price_, price_+sizeof(price_)/sizeof(*price_)); 
	int _ = 2; 
END
CASE(3)
	long long dread_[] = {5216, 12512, 613, 1256, 66, 17202, 30000, 23512, 2125, 33333};
	  vector<long long> dread(dread_, dread_+sizeof(dread_)/sizeof(*dread_)); 
	int price_[] = {2, 2, 1, 1, 1, 1, 2, 1, 2, 1};
	  vector <int> price(price_, price_+sizeof(price_)/sizeof(*price_)); 
	int _ = 5; 
END
/*
CASE(4)
	long long dread_[] = ;
	  vector<long long> dread(dread_, dread_+sizeof(dread_)/sizeof(*dread_)); 
	int price_[] = ;
	  vector <int> price(price_, price_+sizeof(price_)/sizeof(*price_)); 
	int _ = ; 
END
CASE(5)
	long long dread_[] = ;
	  vector<long long> dread(dread_, dread_+sizeof(dread_)/sizeof(*dread_)); 
	int price_[] = ;
	  vector <int> price(price_, price_+sizeof(price_)/sizeof(*price_)); 
	int _ = ; 
END
*/
}
// END CUT HERE
