#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <iomanip>
#include <set>
#include <tuple>
#define mkp make_pair
#define mkt make_tuple
#define rep(i,n) for(int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
const ll MOD=1e9+7;

int N;
vector<int> R,A,W,T;

int dp[2020][2020];
const int INF=1e9;

void chmax(int &a,int b){
  if(a<b) a=b;
}

int main(){
  cin>>N;
  R.resize(N);
  A.resize(N);
  W.resize(N);
  T.resize(N);
  rep(i,N) cin>>R[i]>>A[i]>>W[i]>>T[i];

  rep(i,N) T[i]--;
  for(int i=0;i<=N;i++) for(int j=0;j<=2000;j++) dp[i][j]=-INF;

  vector<pair<int,int>> v;
  for(int i=0;i<N;i++) v.push_back(mkp(T[i]+R[i],i));
  sort(v.begin(),v.end());
  reverse(v.begin(),v.end());

  for(int i=0;i<N;i++){
    int tar=v[i].second;
    int ndp[2020];
    for(int j=0;j<=2000;j++) ndp[j]=-INF;
    ndp[T[tar]]=A[tar];
    for(int j=0;j<=2000;j++){
      if(dp[i][j]==-INF) continue;
      dp[i+1][j]=dp[i][j];
      if(j-R[tar]>=0) chmax(ndp[min(j-R[tar],T[tar])],dp[i][j]+A[tar]);
    }

    for(int j=2000;j>=0;j--){
      if(ndp[j]==-INF) continue;
      if(j-R[tar]>=0) chmax(ndp[j-R[tar]],ndp[j]+W[tar]);
    }
    for(int j=0;j<=2000;j++) chmax(dp[i+1][j],ndp[j]);
  }

  int ans=0;
  for(int i=0;i<=2000;i++) chmax(ans,dp[N][i]);
  cout<<ans<<endl;


  return 0;
}

