#define  _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <algorithm>
#include <utility>
#include <functional>
#include <cstring>
#include <queue>
#include <stack>
#include <math.h>
#include <iterator>
#include <vector>
#include <string>
#include <set>
#include <math.h>
#include <iostream>
#include <random>
#include<map>
#include <iomanip>
#include <time.h>
#include <stdlib.h>
#include <list>
#include <typeinfo>
#include <list>
#include <set>
#include <cassert>
#include<fstream>
#include <unordered_map>
#include <cstdlib>
#include <complex>
#include <cctype>
#include <bitset>
using namespace std;
typedef string::const_iterator State;
#define Ma_PI 3.141592653589793
#define eps 1e-5
#define LONG_INF 1e18
#define GOLD 1.61803398874989484820458
#define MAX_MOD 1000000007LL
#define GYAKU 500000004LL
#define MOD 998244353LL
#define seg_size 262144*4
#define REP(a,b) for(long long a = 0;a < b;++a)
long long dp[6000];
long long new_dp[6000] = {};
long long cnter[6000] = {};
int main() {
#define int long long
	int n;
	cin >> n;
	vector<tuple<long long, long long, long long, long long, long long>> gogo;
	REP(i, n) {
		long long r, a, t, w;
		cin >> r >> a >> w >> t;
		long long border = 5000 - (r + t);
		gogo.push_back(make_tuple(border, r, a, t, w));
		/*
		REP(q, r) {
			cnter[q] = -1e9;
		}
		for (int q = border + r; q < 5000; ++q) {
			cnter[q % r] = max(cnter[q % r] + w, dp[q - r] + a);
			new_dp[q] = max(dp[q], cnter[q % r]);
		}
		for(int i = border+r;i < 5000;++i){
			dp[i] = new_dp[i];
		}
		*/
	}
	sort(gogo.begin(), gogo.end());
	REP(i, n) {
		long long r, a, t, w;
		r = get<1>(gogo[i]);
		a = get<2>(gogo[i]);
		t = get<3>(gogo[i]);
		w = get<4>(gogo[i]);
		long long border = get<0>(gogo[i]);
		REP(q, r) {
			cnter[q] = -1e9;
		}
		for (int q = border + r; q < 5000; ++q) {
			cnter[q % r] = max(cnter[q % r] + w, dp[q - r] + a);
			new_dp[q] = max(dp[q], cnter[q % r]);
		}
		for (int i = border + r; i < 5000; ++i) {
			dp[i] = new_dp[i];
		}
	}
	long long ans = 0;
	for (int i = 0; i < 5000; ++i) {
		if (dp[i] == 0) continue;
		ans = max(ans, dp[i]);
	}
	cout << ans << endl;
}
