#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>
#include <cstring>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

class TheProgrammingContestDivOne { public:
	struct Problem
	{
		LL M;
		LL D;
		LL R;
		bool operator<(const Problem& rhs) const {
			if(R != rhs.R) return R < rhs.R;
			if(D != rhs.D) return D > rhs.D;
			return M > rhs.M;
		}
	};
	int find(int T, vector <int> maxPoints, vector <int> pointsPerMinute, vector <int> requiredTime) 
	{
		const int N = maxPoints.size();
		vector<Problem> ps;
		for(int i=0; i<N; ++i) {
			Problem p = {maxPoints[i], pointsPerMinute[i], requiredTime[i]};
			ps.push_back(p);
		}
		sort(ps.begin(), ps.end());

		LL best = 0;

		map<LL, LL> st;
		st[0] = 0;
		for(int i=0; i<N; ++i)
		{
			map<LL, LL> st2 = st;
			for(map<LL, LL>::iterator it=st.begin(); it!=st.end(); ++it)
			{
				LL s = it->first;
				LL t = it->second;
				LL t2 = t + ps[i].R;
				if( t2 <= T ) {
					LL s2 = s + ps[i].M - ps[i].D * t2;
					best = max(best, s2);
					if( s2 > s ) {
						LL& entry = st2[s2];
						if( entry==0 || entry>t2 )
							entry = t2;
					}
				}
			}
			st.swap(st2);
		}
		return int(best);
	}
};

// 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(_, TheProgrammingContestDivOne().find(T, maxPoints, pointsPerMinute, requiredTime));}
int main(){

CASE(0)
	int T = 74; 
	int maxPoints_[] = {502};
	  vector <int> maxPoints(maxPoints_, maxPoints_+sizeof(maxPoints_)/sizeof(*maxPoints_)); 
	int pointsPerMinute_[] = {2};
	  vector <int> pointsPerMinute(pointsPerMinute_, pointsPerMinute_+sizeof(pointsPerMinute_)/sizeof(*pointsPerMinute_)); 
	int requiredTime_[] = {47};
	  vector <int> requiredTime(requiredTime_, requiredTime_+sizeof(requiredTime_)/sizeof(*requiredTime_)); 
	int _ = 408; 
END
CASE(1)
	int T = 40000; 
	int maxPoints_[] = {100000, 100000};
	  vector <int> maxPoints(maxPoints_, maxPoints_+sizeof(maxPoints_)/sizeof(*maxPoints_)); 
	int pointsPerMinute_[] = {1, 100000};
	  vector <int> pointsPerMinute(pointsPerMinute_, pointsPerMinute_+sizeof(pointsPerMinute_)/sizeof(*pointsPerMinute_)); 
	int requiredTime_[] = {50000, 30000};
	  vector <int> requiredTime(requiredTime_, requiredTime_+sizeof(requiredTime_)/sizeof(*requiredTime_)); 
	int _ = 0; 
END
CASE(2)
	int T = 75; 
	int maxPoints_[] = {250, 500, 1000};
	  vector <int> maxPoints(maxPoints_, maxPoints_+sizeof(maxPoints_)/sizeof(*maxPoints_)); 
	int pointsPerMinute_[] = {2, 4, 8};
	  vector <int> pointsPerMinute(pointsPerMinute_, pointsPerMinute_+sizeof(pointsPerMinute_)/sizeof(*pointsPerMinute_)); 
	int requiredTime_[] = {25, 25, 25};
	  vector <int> requiredTime(requiredTime_, requiredTime_+sizeof(requiredTime_)/sizeof(*requiredTime_)); 
	int _ = 1200; 
END
CASE(3)
	int T = 30; 
	int maxPoints_[] = {100, 100, 100000};
	  vector <int> maxPoints(maxPoints_, maxPoints_+sizeof(maxPoints_)/sizeof(*maxPoints_)); 
	int pointsPerMinute_[] = {1, 1, 100};
	  vector <int> pointsPerMinute(pointsPerMinute_, pointsPerMinute_+sizeof(pointsPerMinute_)/sizeof(*pointsPerMinute_)); 
	int requiredTime_[] = {15, 15, 30};
	  vector <int> requiredTime(requiredTime_, requiredTime_+sizeof(requiredTime_)/sizeof(*requiredTime_)); 
	int _ = 97000; 
END
CASE(4)
	int T = 100000; 
	int maxPoints_[] = {100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,};
	  vector <int> maxPoints(maxPoints_, maxPoints_+sizeof(maxPoints_)/sizeof(*maxPoints_)); 
	  int pointsPerMinute_[] = {10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,};
	  vector <int> pointsPerMinute(pointsPerMinute_, pointsPerMinute_+sizeof(pointsPerMinute_)/sizeof(*pointsPerMinute_)); 
	int requiredTime_[] = {1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536,1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536,1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536,4,4};
	  vector <int> requiredTime(requiredTime_, requiredTime_+sizeof(requiredTime_)/sizeof(*requiredTime_)); 
	int _ = -1; 
END
CASE(5)
	int T = 100000; 
	int maxPoints_[] = {1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,};
	  vector <int> maxPoints(maxPoints_, maxPoints_+sizeof(maxPoints_)/sizeof(*maxPoints_)); 
	  int pointsPerMinute_[] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
	  vector <int> pointsPerMinute(pointsPerMinute_, pointsPerMinute_+sizeof(pointsPerMinute_)/sizeof(*pointsPerMinute_)); 
	int requiredTime_[] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
	  vector <int> requiredTime(requiredTime_, requiredTime_+sizeof(requiredTime_)/sizeof(*requiredTime_)); 
	int _ = -1; 
END
/*
CASE(5)
	int T = ; 
	int maxPoints_[] = ;
	  vector <int> maxPoints(maxPoints_, maxPoints_+sizeof(maxPoints_)/sizeof(*maxPoints_)); 
	int pointsPerMinute_[] = ;
	  vector <int> pointsPerMinute(pointsPerMinute_, pointsPerMinute_+sizeof(pointsPerMinute_)/sizeof(*pointsPerMinute_)); 
	int requiredTime_[] = ;
	  vector <int> requiredTime(requiredTime_, requiredTime_+sizeof(requiredTime_)/sizeof(*requiredTime_)); 
	int _ = ; 
END
*/
}
// END CUT HERE
