Problem:
E: モンスターバスター
問題
AORイカちゃんはモンスターバスターである.
ある日, 道を歩いていると寝ているモンスターに出会った.
闘争心が強いAORイカちゃんは,モンスターに寝起きの一撃をお見舞いすることに決めた.
            しかし, 現在のAORイカちゃんの攻撃力は $0$ であり, このままではまともな攻撃ができない.
モンスターバスターの精進をしているAORイカちゃんは, 実は師匠から特殊な笛を託されていた.
            この笛で特定の曲を吹くと一定時間攻撃力が上がるのである.
修行を積んだAORイカちゃんは $N$ 個の曲を吹くことができる.
            $i$ 番目の曲は演奏に $R_i$ 秒かかり, 演奏終了後に攻撃力が $A_i$ だけ上昇する.
            演奏終了から $T_i$ 秒後にこの演奏の効果は切れ, 演奏前の攻撃力に戻ってしまう.
また, AORイカちゃんは重ね演奏をすることができる.
            演奏の効果時間中に同じ曲を演奏し終えると攻撃力が $A_i$ ではなく $W_i$ 上昇する.
            重ね演奏は何回でもできるが効果時間は延長しない. そのため現在効果中の $i$ 番目の曲の最初にかけた効果が切れると重ね演奏の効果もすべて切れる.
AORイカちゃんの攻撃力の最大値を出力せよ.
            なお, いくら演奏してもモンスターは起きないし, AORイカちゃんは $0.5$ 秒で攻撃できる.
制約
入力値は全て整数である.
$1 \leq N \leq 2000$
$-2000 \leq A_i, W_i \leq 2000$
$1 \leq R_i , T_i \leq 2000$
入力形式
入力は以下の形式で与えられる.
$N$
$R_1\ A_1\ W_1\ T_1$
$\vdots$
$R_N\ A_N\ W_N\ T_N$
出力
AORイカちゃんの攻撃力の最大値を出力せよ. また, 末尾に改行も出力せよ.
サンプル
サンプル入力 1
2
5 10 5 5
4 4 2 2
サンプル出力 1
14
サンプル入力 2
2
5 10 5 11
8 8 2 1
サンプル出力 2
20

Submitted solutions:

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>

using namespace std;
struct song {
    int r, a, w, t;
};

bool cmp(song s1, song s2) {
    return min(s2.t, s1.t - s2.r) > min(s1.t, s2.t - s1.r);
}

int main() {
    int n;
    cin >> n;
    vector<song> p;
    for (int i = 0; i < n; ++i) {
        int r, a, t, w;
        cin >> r >> a >> w >> t;
        p.push_back({r, a, w, t});
    }
    sort(p.begin(), p.end(), cmp);
    vector<vector<int>> dp(n, vector<int>(2001, -100000000));
    vector<vector<int>> pref(n + 1, vector<int>(2001, -100000000));
    vector<vector<int>> possible(n + 1, vector<int>(2001, 0));
    int ans = 0;
    for (int i = 0; i <= 2000; ++i) {
        possible[0][i] = 1;
    }
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j <= 2000; ++j) {
            pref[i + 1][j] = max(pref[i + 1][j], pref[i][j]);
            possible[i + 1][j] = possible[i][j];
        }
        dp[i][p[i].t] = p[i].a;
        possible[i + 1][p[i].t] = 1;
        pref[i + 1][p[i].t] = max(pref[i + 1][p[i].t], dp[i][p[i].t]);
        for (int j = p[i].t; j > 0; --j) {
            if (j != p[i].t) {
                dp[i][j] = dp[i][j + 1];
            }
            if (j + p[i].r > 2000) {
                continue;
            }
            if (dp[i][j + p[i].r] != -100000000) {
                dp[i][j] = max(dp[i][j], dp[i][j + p[i].r] + p[i].w);
            }
            if (possible[i][j + p[i].r]) {
                dp[i][j] = max(dp[i][j], pref[i][j + p[i].r] + p[i].a);
            }
            if (dp[i][j] != -100000000) {
                pref[i + 1][j] = max(pref[i + 1][j], dp[i][j]);
                ans = max(ans, dp[i][j]);
                possible[i + 1][j] = 1;
            }
        }
        for (int j = 2000; j > 0; --j) {
            pref[i + 1][j - 1] = max(pref[i + 1][j - 1], pref[i + 1][j]);
            possible[i + 1][j - 1] = max(possible[i + 1][j], possible[i + 1][j - 1]);
        }
    }
    cout << ans << '\n';
}
accuracy: 1
time_ms: 20
memory_kb: 50340
final_rank: 13

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>

using namespace std;
struct song {
    int r, a, w, t;
};

bool cmp(song s1, song s2) {
    return min(s2.t, s1.t - s2.r) > min(s1.t, s2.t - s1.r);
}

int main() {
    int n;
    cin >> n;
    vector<song> p;
    for (int i = 0; i < n; ++i) {
        int r, a, t, w;
        cin >> r >> a >> w >> t;
        p.push_back({r, a, w, t});
    }
    sort(p.begin(), p.end(), cmp);
    vector<vector<int>> dp(n, vector<int>(2001, -100000000));
    vector<vector<int>> pref(n + 1, vector<int>(2001, -100000000));
    int ans = 0;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j <= 2000; ++j) {
            pref[i + 1][j] = max(pref[i + 1][j], pref[i][j]);
        }
        dp[i][p[i].t] = p[i].a;
        pref[i + 1][p[i].t] = max(pref[i + 1][p[i].t], dp[i][p[i].t]);
        for (int j = p[i].t; j > 0; --j) {
            if (j != p[i].t) {
                dp[i][j] = dp[i][j + 1];
                pref[i+1][j]=max(pref[i+1][j], dp[i][j]);
            }
            if (j + p[i].r > 2000) {
                continue;
            }
            if (dp[i][j + p[i].r] != -100000000) {
                dp[i][j] = max(dp[i][j], dp[i][j + p[i].r] + p[i].w);
            }
            dp[i][j] = max(dp[i][j], pref[i][j + p[i].r] + p[i].a);
            if (dp[i][j] != -100000000) {
                pref[i + 1][j] = max(pref[i + 1][j], dp[i][j]);
                ans = max(ans, dp[i][j]);
            }
        }
    }
    cout << ans << '\n';
}
accuracy: 1
time_ms: 10
memory_kb: 34484
final_rank: 7

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>

using namespace std;
struct song {
    int r, a, w, t;
};

bool cmp(song s1, song s2) {
    return min(s2.t, s1.t - s2.r) > min(s1.t, s2.t - s1.r);
}

int main() {
    int n;
    cin >> n;
    vector<song> p;
    for (int i = 0; i < n; ++i) {
        int r, a, t, w;
        cin >> r >> a >> w >> t;
        p.push_back({r, a, w, t});
    }
    sort(p.begin(), p.end(), cmp);
    vector<vector<int>> dp(n, vector<int>(2001, -100000000));
    vector<vector<int>> pref(n + 1, vector<int>(2001, -100000000));
    vector<vector<int>> possible(n + 1, vector<int>(2001, 0));
    int ans = 0;
    for (int i = 0; i <= 2000; ++i) {
        possible[0][i] = 1;
    }
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j <= 2000; ++j) {
            pref[i + 1][j] = max(pref[i + 1][j], pref[i][j]);
            possible[i + 1][j] = possible[i][j];
        }
        dp[i][p[i].t] = p[i].a;
        possible[i + 1][p[i].t] = 1;
        pref[i + 1][p[i].t] = dp[i][p[i].t];
        for (int j = p[i].t; j > 0; --j) {
            if (j != p[i].t) {
                dp[i][j] = dp[i][j + 1];
            }
            if (j + p[i].r > 2000) {
                continue;
            }
            if (dp[i][j + p[i].r] != -100000000) {
                dp[i][j] = max(dp[i][j], dp[i][j + p[i].r] + p[i].w);
            }
            if (possible[i][j + p[i].r]) {
                dp[i][j] = max(dp[i][j], pref[i][j + p[i].r] + p[i].a);
            }
            if (dp[i][j] != -100000000) {
                pref[i + 1][j] = max(pref[i + 1][j], dp[i][j]);
                ans = max(ans, dp[i][j]);
                possible[i + 1][j] = 1;
            }
        }
        for (int j = 2000; j > 0; --j) {
            pref[i + 1][j - 1] = max(pref[i + 1][j - 1], pref[i + 1][j]);
        }
    }
    cout << ans << '\n';
}
accuracy: 0.265625
time_ms: 20
memory_kb: 47532
final_rank: 19

code_snippet: #include<bits/stdc++.h>
using namespace std;
using Int = long long;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}

//INSERT ABOVE HERE
signed main(){
  Int n;
  cin>>n;
  vector<Int> rs(n),as(n),ws(n),ts(n);
  for(Int i=0;i<n;i++) cin>>rs[i]>>as[i]>>ws[i]>>ts[i];

  Int ans=0;
  for(Int uku=0;uku<8;uku++){
    using T = tuple<Int, Int, Int, Int>;
    using P = pair<Int, T>;
    vector<P> vp;
    for(Int i=0;i<n;i++){
      Int val=0;
      if(uku%4==0) val=rs[i]+ts[i];
      if(uku%4==1) val=rs[i]-ts[i];
      if(uku%4==2) val=rs[i];
      if(uku%4==3) val=ts[i];
      if(uku>=4) val*=-1;
      vp.emplace_back(val,T(rs[i],as[i],ws[i],ts[i]));
    }

    sort(vp.begin(),vp.end());
    for(Int i=0;i<n;i++)
      tie(rs[i],as[i],ws[i],ts[i])=vp[i].second;

    const Int MAX = 2020;
    const Int INF = 1e9;
    vector<Int> dp(MAX,-INF);
    for(Int i=0;i<n;i++){
      vector<Int> nx0(MAX,-INF),nx1(MAX,-INF);
      chmax(nx1[ts[i]],as[i]);
      for(Int j=MAX-1;j>=0;j--){
        chmax(nx0[j],dp[j]);
        if(j<rs[i]) continue;
        chmax(nx1[min(j-rs[i],ts[i])],dp[j]+as[i]);
        chmax(nx1[j-rs[i]],nx1[j]+ws[i]);
      }
      for(Int j=0;j<MAX;j++) chmax(dp[j],nx0[j]);
      for(Int j=0;j<MAX;j++) chmax(dp[j],nx1[j]);
    }
    for(Int j=1;j<MAX;j++) chmax(ans,dp[j]);
  }
  cout<<ans<<endl;
  return 0;
}
accuracy: 1
time_ms: 110
memory_kb: 3248
final_rank: 15

code_snippet: #include<iostream>
#include<vector>
#include<algorithm>
#include<utility>
using namespace std;
#define rep(i,n) for(int i = 0; i < (n); i++)
typedef pair<int,int> pii;
#define INF 1e9
struct data{
    int r,a,w,t;
    bool operator<(const data &another) const
    {
        return r+t < another.r + another.t;
    };
};

int main(){
    int n;
    cin >> n;
    vector<data> v(n);
    rep(i,n){
        cin >> v[i].r >> v[i].a >> v[i].w >> v[i].t;
    }
    sort(v.begin(),v.end());
    reverse(v.begin(),v.end());
    int dp[2][2005][2];
    rep(i,2)rep(j,2005)rep(k,2)dp[i][j][k] = -INF;
    rep(i,n){
        int now = i%2;
        int ne = 1 - now;
        dp[now][v[i].t][1] = max(dp[now][v[i].t][1], v[i].a);
        for(int j = 2000; j >= 0; j--)rep(k,2)if(dp[now][j][k] != -INF){
            int nxt = min(j-v[i].r, v[i].t);
            if(nxt > 0){
                if(k){
                    dp[now][nxt][1] = max(dp[now][nxt][1], dp[now][j][1] + v[i].w);
                }else{
                    dp[now][nxt][1] = max(dp[now][nxt][1], dp[now][j][0] + v[i].a);
                }
            }
            dp[ne][j][0] = max(dp[ne][j][0], dp[now][j][k]);
            dp[now][j][k] = -INF;
        }
    }
    int ans = 0;
    rep(i,2)rep(j,2005)rep(k,2)ans = max(ans, dp[i][j][k]);
    cout << ans << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 3228
final_rank: 2

code_snippet: #include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<vector>
#include<cmath>
#include<algorithm>
#include<map>
#include<queue>
#include<deque>
#include<iomanip>
#include<tuple>
using namespace std;
typedef long long int LL;
typedef pair<int,int> P;
typedef pair<LL,int> LP;
const int INF=1<<30;
const LL MAX=1e9+7;

void array_show(int *array,int array_n,char middle=' '){
    for(int i=0;i<array_n;i++)printf("%d%c",array[i],(i!=array_n-1?middle:'\n'));
}
void array_show(LL *array,int array_n,char middle=' '){
    for(int i=0;i<array_n;i++)printf("%lld%c",array[i],(i!=array_n-1?middle:'\n'));
}

int dp[2005];
int ne[2005];
int t[2005][4];
vector<P> v1;

int main(){
    int n,m;
    int i,j,k;
    int a,b,c;
    int p,q;
    cin>>n;
    for(i=0;i<2005;i++){
        dp[i]=-INF;
    }
    for(i=0;i<n;i++){
        for(j=0;j<4;j++){
            cin>>t[i][j];//r/a/w/t
        }
        v1.push_back(make_pair(t[i][0]+t[i][3],i));
    }
    sort(v1.rbegin(),v1.rend());
    for(i=0;i<n;i++){
        p=v1[i].second;

        for(j=0;j<2005;j++){
            ne[j]=-INF;
        }
        for(j=t[p][0];j<2005;j++){
            if(dp[j]==-INF)continue;
            k=min(j-t[p][0],t[p][3]);
            ne[k]=max(ne[k],dp[j]+t[p][1]);
        }
        ne[t[p][3]]=max(ne[t[p][3]],t[p][1]);
        for(j=2004;j>=0;j--){
            dp[j]=max(dp[j],ne[j]);
            if(j<t[p][0] || ne[j]==-INF)continue;
            k=j-t[p][0];
            ne[k]=max(ne[k],ne[j]+t[p][2]);
        }
    }
    int s=0;
    for(i=1;i<2005;i++){
        s=max(s,dp[i]);
    }
    cout<<s<<endl;
}
accuracy: 1
time_ms: 10
memory_kb: 3188
final_rank: 1

code_snippet: #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define rep(i, n) for(ll (i) = 0; (i) < (n); (i)++)
#define rep1(i, n) for(ll (i) = 1; (i) <= (n); (i)++)
#define rrep(i, n) for(ll (i) = (n) - 1; (i) >= 0; (i)--)
#define rrep1(i, n) for(ll (i) = (n); (i) >= 1; (i)--)
const ll INF = 1145141919;
const ll MOD = 1000000007;
template<class T> void chmax(T &a, const T &b){if(a < b){a = b;}}
template<class T> void chmin(T &a, const T &b){if(a > b){a = b;}}

class node{
    public:
    ll R, A, W, T;
    bool operator < (const node &n)const{
        return (-(R + T) < -(n.R + n.T));
    }
};

ll dp[5000][2];

void update(){
    rrep1(i, 4500){
        chmax(dp[i][0], dp[i][1]);
        chmax(dp[i][0], dp[i + 1][0]);
        dp[i][1] = -INF;
    }
}

int main(){

    ll N;
    cin >> N;
    vector<node>v(N);
    rep(i, N){
        cin >> v[i].R >> v[i].A >> v[i].W >> v[i].T;
    }
    sort(v.begin(), v.end());
    update();
    rep(i, N){
        ll R = v[i].R, A = v[i].A, W = v[i].W, T = v[i].T;
        for(ll from = R + T; from > 1; from--){
            ll to = from - R;
            if(to < 1)break;
            chmax(dp[to][1], dp[from][0] + A);
            chmax(dp[to][1], dp[from][1] + W);
        }
        update();
    }
    cout << dp[1][0] << endl;

    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3252
final_rank: 3

code_snippet: // #define _GLIBCXX_DEBUG // for STL debug (optional)
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
using namespace std;
using ll = long long int;
using int64 = long long int;
 
template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}
 
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const int INF = 1001001001;
const ll LONGINF = 1001001001001001LL;
const ll MOD = 1000000007LL;

int dp[2010][4010];
int main() {
    int N; cin >> N;
    vector< tuple<int, int, int, int> > mus(N);
    for(int i=0; i<N; i++) {
        int R, A, W, T; cin >> R >> A >> W >> T;
        mus[i] = make_tuple(R, A, W, T);
    }

    sort(mus.begin(), mus.end(), [&](auto a, auto b) {
            int AR, AA, AW, AT; tie(AR, AA, AW, AT) = a;
            int BR, BA, BW, BT; tie(BR, BA, BW, BT) = b;
            return AR + AT > BR + BT;
        });

    fill(dp[0], dp[N+1], -INF);
    dp[0][4001] = 0;
    for(int i=0; i<N; i++) {
        int R, A, W, T; tie(R, A, W, T) = mus[i];
        int tmp[4010]; fill(tmp, tmp + 4010, -INF);
        for(int j=0; j<=4005; j++) {
            if(dp[i][j] == -INF) continue;
            // 使わない
            dp[i+1][j] = max(dp[i+1][j], dp[i][j]);

            // 使う
            if(R >= j) continue;
            int t = min(T, j - R);
            tmp[t] = max(tmp[t], dp[i][j] + A);
        }
        for(int j=4005; j>=0; j--) {
            dp[i+1][j] = max(dp[i+1][j], tmp[j]);
            if(tmp[j] == -INF or j - R <= 0) continue;
            tmp[j - R] = max(tmp[j - R], tmp[j] + W);
        }
    }

    cout << *max_element(dp[N], dp[N] + 4005) << endl;
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 34460
final_rank: 10

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 99999999999999999
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


#define SIZE 2005

enum Type{
	NOT,
	USED,
};

struct Info{

	bool operator<(const struct Info &arg) const{

		return R+T > arg.R+arg.T;
	}

	int R,A,W,T;
};

int N;
int dp[SIZE][SIZE][2];
Info info[SIZE];

int main(){

	scanf("%d",&N);

	for(int i = 0; i < N; i++){

		scanf("%d %d %d %d",&info[i].R,&info[i].A,&info[i].W,&info[i].T);
	}

	sort(info,info+N);

	for(int i = 0; i <= N; i++){
		for(int k = 0; k <= 2000; k++){
			dp[i][k][NOT] = -BIG_NUM;
			dp[i][k][USED] = -BIG_NUM;
		}
	}

	dp[0][2000][0] = 0;

	for(int i = 0; i < N; i++){

		dp[i][info[i].T][USED] = max(dp[i][info[i].T][USED],info[i].A);

		for(int rest = 2000; rest > 0; rest--){

			dp[i+1][rest][NOT] = max(dp[i][rest][NOT],dp[i][rest][USED]);

			int next = min(rest-info[i].R,info[i].T);

			if(next >= 0){

				dp[i][next][USED] = max(dp[i][next][USED],max(dp[i][rest][NOT]+info[i].A,dp[i][rest][USED]+info[i].W));
			}
		}
	}

	int ans = 0;
	for(int i = 1; i <= 2000; i++){

		ans = max(ans,dp[N][i][NOT]);
	}

	printf("%d\n",ans);

	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 34600
final_rank: 9

code_snippet: #include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <string>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <stdio.h>
using namespace std;
#define int long long
int MOD = 1000000007;
struct K {
	int R, A, W, T;
	bool operator<(const K &right)const {
		return R + T < right.R + right.T;
	}
};
signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	int N;
	cin >> N;
	vector<K> A(N);
	int res = 0;
	for (int i = 0; i < N; i++) {
		cin >> A[i].R >> A[i].A >> A[i].W >> A[i].T;
	}


	sort(A.rbegin(), A.rend());
	vector<int> dp(4001, 0);
	int INF = (int)1 << 60;
	for (int i = 0; i < N; i++) {
		vector<int> ndp(4001, -INF);
		for (int j = 0; j <= 4000; j++) {
			if (j + A[i].R + A[i].T > 4000 && j + A[i].R <= 4000) {
				ndp[j + A[i].R] = max(ndp[j + A[i].R], dp[j] + A[i].A);
			}
		}

		for (int j = 0; j <= 4000; j++) {
			if(j > 0)dp[j] = max(dp[j], dp[j - 1]);
			if(j - A[i].R >= 0)ndp[j] = max(ndp[j], ndp[j - A[i].R] + A[i].W);
			dp[j] = max(dp[j], ndp[j]);
		}

	}
	cout << dp.back() << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 3348
final_rank: 5

code_snippet: #include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> P;

constexpr int INF = INT_MAX;

void chmax(ll &a, ll b){
    a = max(a, b);
}

ll dp[2005][4005][2];

int main(){
    int n;
    cin >> n;
    vector<P> ls;
    vector<int> id(n), r(n), a(n), w(n), t(n);
    for(int i=0;i<n;i++){
        cin >> r[i] >> a[i] >> w[i] >> t[i];
        ls.push_back(P(r[i]+t[i], i));
    }
    sort(ls.rbegin(), ls.rend());
    for(int i=0;i<n;i++) id[i] = ls[i].second;
    for(int i=0;i<=n;i++){
        for(int j=0;j<=4000;j++){
            dp[i][j][0] = -INF;
            dp[i][j][1] = -INF;
        }
    }
    dp[0][4000][0] = 0;
    for(int i=0;i<n;i++){
        for(int j=4000;j>0;j--){
            for(int k=0;k<2;k++){
                if(dp[i][j][k] == -INF) continue;
                if(j <= r[id[i]]) continue;
                if(k==0){
                    chmax(dp[i][min(j-r[id[i]],t[id[i]])][1], dp[i][j][k] + a[id[i]]);
                }
                else{
                    chmax(dp[i][j-r[id[i]]][1], dp[i][j][k] + w[id[i]]);
                }
            }
        }
        for(int j=0;j<=4000;j++){
            dp[i+1][j][0] = max(dp[i][j][0], dp[i][j][1]);
        }
    }
    ll ans = 0;
    for(int i=0;i<=4000;i++){
        ans = max({ans, dp[n][i][0], dp[n][i][1]});
    }
    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 128412
final_rank: 18

code_snippet: #include <bits/stdc++.h>
#define inf (long long)(1e9)
using namespace std;

struct data {
  long long r, a, w, t;
};

bool asc(const data &l, const data &r) {
  return l.t + l.r > r.t + r.r;
}

long long n;
vector<data> v;
long long dp[2005][4005][2] = {0};

long long solve();

int main() {
  cin >> n;
  v.resize(n);
  for(int i = 0; i < n; ++i)
    cin >> v[i].r >> v[i].a >> v[i].w >> v[i].t;
  cout << solve() << endl;
  return 0;
}

long long solve() {
  sort(v.begin(), v.end(), asc);
  long long ans = 0;
  for(int i = 0; i < n; ++i)
    for(int j = 0; j <= 4000; ++j) dp[i][j][1] = -inf;
  for(int i = 0; i < n; ++i) {
    for(int j = 4000; j >= 0; --j) {
      dp[i + 1][j][0] = max(dp[i][j][0], dp[i][j][1]);
      if(min(v[i].t, j - v[i].r) >= 0) {
        dp[i][min(v[i].t, j - v[i].r)][1] =
            max(dp[i][min(v[i].t, j - v[i].r)][1],
                dp[i][j][0] + v[i].a);
        if(dp[i][j][1] != -inf)
          dp[i][min(v[i].t, j - v[i].r)][1] =
              max(dp[i][min(v[i].t, j - v[i].r)][1],
                  dp[i][j][1] + v[i].w);
      }
      if(j != 0) ans = max({ans, dp[i][j][0], dp[i][j][1]});
    }
  }
  return ans;
}
accuracy: 1
time_ms: 50
memory_kb: 128448
final_rank: 16

code_snippet: #include <bits/stdc++.h>
#define GET_MACRO(_1,_2,_3,_4,_5,_6,_7,_8,NAME,...) NAME
#define pr(...) cerr<< GET_MACRO(__VA_ARGS__,pr8,pr7,pr6,pr5,pr4,pr3,pr2,pr1)(__VA_ARGS__) <<endl
#define pr1(a) (#a)<<"="<<(a)<<" "
#define pr2(a,b) pr1(a)<<pr1(b)
#define pr3(a,b,c) pr1(a)<<pr2(b,c)
#define pr4(a,b,c,d) pr1(a)<<pr3(b,c,d)
#define pr5(a,b,c,d,e) pr1(a)<<pr4(b,c,d,e)
#define pr6(a,b,c,d,e,f) pr1(a)<<pr5(b,c,d,e,f)
#define pr7(a,b,c,d,e,f,g) pr1(a)<<pr6(b,c,d,e,f,g)
#define pr8(a,b,c,d,e,f,g,h) pr1(a)<<pr7(b,c,d,e,f,g,h)
#define prArr(a) {cerr<<(#a)<<"={";int i=0;for(auto t:(a))cerr<<(i++?", ":"")<<t;cerr<<"}"<<endl;}
using namespace std;
using Int = long long;
using _int = int;
using ll = long long;
using Double = long double;
const Int INF = (1LL<<60)+1e9; // ~ 1.15 * 1e18
const Int mod = (1e9)+7;
const Double EPS = 1e-8;
const Double PI = 6.0 * asin((Double)0.5);
using P = pair<Int,Int>;
template<class T> T Max(T &a,T b){return a=max(a,b);}
template<class T> T Min(T &a,T b){return a=min(a,b);}
template<class T1, class T2> ostream& operator<<(ostream& o,pair<T1,T2> p){return o<<"("<<p.first<<","<<p.second<<")";}
template<class T1, class T2, class T3> ostream& operator<<(ostream& o,tuple<T1,T2,T3> t){
  return o<<"("<<get<0>(t)<<","<<get<1>(t)<<","<<get<2>(t)<<")";}
template<class T1, class T2> istream& operator>>(istream& i,pair<T1,T2> &p){return i>>p.first>>p.second;}
template<class T> ostream& operator<<(ostream& o,vector<T> a){Int i=0;for(T t:a)o<<(i++?" ":"")<<t;return o;}
template<class T> istream& operator>>(istream& i,vector<T> &a){for(T &t:a)i>>t;return i;}
//INSERT ABOVE HERE

Int N;
vector<Int> ord;
vector<Int> R, A, W, T;

_int used[2010][5010][2], mem[2010][5010][2];
Int dfs(Int i, Int t, Int flag){
  if(t <= 0) return -(Int)INF;
  if(i == N) return 0;
  if(used[i][t][flag]++) return mem[i][t][flag];

  Int I = ord[i];
  Int a = -INF, b = -INF;
  {//次の曲にいく
    a = dfs(i+1, t, 0);
  }

  {//演奏する
    b = dfs(i, min(T[I], t - R[I]), 1) + (flag == 0? A[I]:W[I]);
  }
  Int res = max(a, b);
  return mem[i][t][flag] = res;
}

signed main(){
  srand((unsigned)time(NULL));
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  cin>>N;
  R.resize(N), A.resize(N), W.resize(N), T.resize(N);
  for(Int i=0;i<N;i++) cin>>R[i]>>A[i]>>W[i]>>T[i];

  ord.resize(N); iota(ord.begin(), ord.end(),0);
  sort(ord.begin(), ord.end(), [&](Int i, Int j){
      return P(R[i] + T[i], -R[i]) > P(R[j] + T[j], -R[j]);
    });


  Int ans = dfs(0, 5000, 0);
  cout<<ans<<endl;
  return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 80920
final_rank: 17

code_snippet: #include<bits/stdc++.h>
using namespace std;

#define N 2000

int r[N], a[N], w[N], t[N];
int dp[N+1][N+1][2];

int main(){
  int n;
  cin >> n;

  for(int i = 0; i < n; i++){
    cin >> r[i] >> a[i] >> w[i] >> t[i];
  }

  vector<int> order(n);
  iota(order.begin(), order.end(), 0);

  sort(order.begin(),order.end(),[&](int i,int j){
    return r[i]+t[i] > r[j]+t[j];
  });

  for(int i = 0; i <= 2000; i++) {
    for(int j = 0; j <= 2000; j++) {
      dp[i][j][0] = dp[i][j][1] = -1e9;
    }
  }

  dp[0][2000][0] = 0;

  for(int i = 0; i < n; i++){
    int k = order[i];

    int to2 = t[k];
    dp[i][to2][1] = max(dp[i][to2][1], a[k]);

    for(int j = 2000; j >= 0; j--){
      int to = min(j - r[k], t[k]);
      if (to >= 0) {
        dp[i][to][1] = max(dp[i][to][1], dp[i][j][0] + a[k]);
        dp[i][to][1] = max(dp[i][to][1], dp[i][j][1] + w[k]);
      }
    }

    for(int j=0; j <= 2000; j++) {
      dp[i+1][j][0] = max(dp[i][j][0], dp[i][j][1]);
    }
  }

  int ans = 0;

  for(int i=1; i<=2000; i++) {
    ans = max(ans, dp[n][i][0]);
  }

  cout << ans << endl;

  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 34484
final_rank: 7

code_snippet: #include <bits/stdc++.h>
using namespace std;

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

int main(){
    int N;
    cin >> N;
    vector<vector<int>> A;
    for(int i=0; i<N; i++){
        vector<int> v(4);
        for(int j=0; j<4; j++) cin >> v[j];
        A.push_back(v);
    }
    sort(A.begin(), A.end(), [](auto a, auto b){ return a[0]+a[3] > b[0]+b[3]; }); 
    static int dp[2001][4001][2];
    for(int i=0; i<=N; i++) for(int j=0; j<=4000; j++) for(int k=0; k<2; k++) dp[i][j][k] = -1e9;
    dp[0][4000][0] = 0;
    for(int i=0; i<N; i++){
        int r = A[i][0], a = A[i][1], w = A[i][2], t = A[i][3];
        for(int j=4000; j>0; j--) for(int k=0; k<2; k++){
            chmax(dp[i+1][j][0], dp[i][j][k]);
            int j2 = min(j-r, t);
            int v2 = dp[i][j][k] + (k ? w : a);
            if(j2 > 0) chmax(dp[i][j2][1], v2);
        }
    }
    int ans = 0;
    for(int j=1; j<=2000; j++) for(int k=0; k<2; k++) chmax(ans, dp[N][j][k]);
    cout << ans << endl;
}
accuracy: 1
time_ms: 40
memory_kb: 65824
final_rank: 14

code_snippet: #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;
}
accuracy: 1
time_ms: 10
memory_kb: 18984
final_rank: 6

code_snippet: #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][4020];
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<=4000;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[4020];
    for(int j=0;j<=4000;j++) ndp[j]=-INF;
    ndp[T[tar]]=A[tar];
    for(int j=0;j<=4000;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=4000;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<=4000;j++) chmax(dp[i+1][j],ndp[j]);
  }

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


  return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 34600
final_rank: 11

code_snippet: #include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#define _USE_MATH_DEFINES
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()

const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007; // 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
/*-------------------------------------------------*/
int main() {
  cin.tie(nullptr); ios::sync_with_stdio(false);
  // freopen("input.txt", "r", stdin);

  int n; cin >> n;
  vector<int> r(n), a(n), w(n), t(n); REP(i, n) cin >> r[i] >> a[i] >> w[i] >> t[i];
  vector<int> idx(n);
  iota(ALL(idx), 0);
  sort(ALL(idx), [&](int a, int b) { return r[a] + t[a] > r[b] + t[b]; });
  vector<vector<int> > dp(n + 1, vector<int>(4000, -INF));
  REP(j, 4000) dp[0][j] = 0;
  REP(i, n) {
    int p = idx[i];
    vector<bool> updated(4000, false);
    for (int j = 3999; j >= 0; --j) {
      if (j - r[p] < 0) break;
      dp[i + 1][min(j - r[p], t[p] - 1)] = max(dp[i + 1][min(j - r[p], t[p] - 1)], dp[i][j] + a[p]);
      updated[min(j - r[p], t[p] - 1)] = true;
    }
    for (int j = 3999; j >= 0; --j) if (updated[j]) {
      if (j - r[p] < 0) break;
      dp[i + 1][j - r[p]] = max(dp[i + 1][j - r[p]], dp[i + 1][j] + w[p]);
      updated[j - r[p]] = true;
    }
    REP(j, 4000) dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
  }
  int ans = 0;
  REP(j, 4000) ans = max(ans, dp[n][j]);
  cout << ans << '\n';
  return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 34252
final_rank: 12

code_snippet: #include <bits/stdc++.h>

using namespace std;
using ll = long long;
// #define int ll
using PII = pair<ll, ll>;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()

template<typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template<typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }
template<typename T> bool IN(T a, T b, T x) { return a<=x&&x<b; }
template<typename T> T ceil(T a, T b) { return a/b + !!(a%b); }

template<typename T> vector<T> make_v(size_t a) { return vector<T>(a); }
template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts) {
    return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}
template<typename T,typename V> typename enable_if<is_class<T>::value==0>::type
fill_v(T &t, const V &v) { t=v; }
template<typename T,typename V> typename enable_if<is_class<T>::value!=0>::type
fill_v(T &t, const V &v ) { for(auto &e:t) fill_v(e,v); }

template<class S,class T>
ostream &operator <<(ostream& out,const pair<S,T>& a) {
    out<<'('<<a.first<<','<<a.second<<')'; return out;
}
template<class T>
ostream &operator <<(ostream& out,const vector<T>& a){
    out<<'[';
    for(const T &i: a) out<<i<<',';
    out<<']';
    return out;
}
template<class T>
ostream &operator <<(ostream& out, const set<T>& a) {
    out<<'{';
    for(const T &i: a) out<<i<<',';
    out<<'}';
    return out;
}
template<class T, class S>
ostream &operator <<(ostream& out, const map<T,S>& a) {
    out<<'{';
    for(auto &i: a) out<<i<<',';
    out<<'}';
    return out;
}

int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0}; // DRUL
const int INF = 1<<30;
const ll LLINF = 1LL<<60;
const ll MOD = 1000000007;

signed main(void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);

    ll n;
    cin >> n;
    vector<ll> r(n), a(n), w(n), t(n);
    REP(i, n) {
        cin >> r[i] >> a[i] >> w[i] >> t[i];
        if(a[i]<=0 && w[i]<=0) {
            i--;
            n--;
        }
    }
    vector<ll> ord(n);
    iota(ALL(ord), 0);
    sort(ALL(ord), [&](ll x, ll y){
        return t[x]+r[x] < t[y]+r[y];
    });

    vector<vector<ll>> dp(5001, vector<ll>(2, -LLINF));
    dp[0][0] = 0;
    for(auto i: ord) {
        FOR(j, r[i], r[i]+t[i]) chmax(dp[j][1], dp[j-r[i]][0]+a[i]);
        FOR(j, r[i], r[i]+t[i]) chmax(dp[j][1], dp[j-r[i]][1]+w[i]);
        REP(j, 5001) {
            chmax(dp[j][0], dp[j][1]);
            dp[j][1] = -LLINF;
        }
    }

    // vector<vector<vector<ll>>> dp(n+1, vector<vector<ll>>(5001, vector<ll>(2)));
    // REP(i, n) REP(j, 2001) dp[i][j][0] = dp[i][j][1] = -LLINF;
    // dp[0][0][0] = 0;
    // REP(ii, n) {
    //     ll i = ord[ii];
    //     FOR(j, r[i], r[i]+t[i]) {
    //         chmax(dp[ii+1][j][1], dp[ii][j-r[i]][0]+a[i]);
    //         chmax(dp[ii+1][j][1], dp[ii+1][j-r[i]][1]+w[i]);
    //     }
    //     REP(j, 5001) {
    //         chmax(dp[ii+1][j][0], dp[ii][j][0]);
    //         chmax(dp[ii+1][j][0], dp[ii+1][j][1]);
    //     }
    // }

    // REP(i, n+1) {
    //     cout << "i=" << ord[i] << endl;
    //     REP(j, 21) cout << j << " " << dp[i][j][0] << " " << dp[i][j][1] << endl;
    //     cout << endl;
    // }

    ll ret = 0;
    REP(i, 5001) chmax(ret, dp[i][0]);
    cout << ret << endl;

    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3348
final_rank: 4

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define R cin>>
#define Z class
#define ll long long
#define ln cout<<'\n'
#define in(a) insert(a)
#define pb(a) push_back(a)
#define pd(a) printf("%.10f\n",a)
#define mem(a) memset(a,0,sizeof(a))
#define all(c) (c).begin(),(c).end()
#define iter(c) __typeof((c).begin())
#define rrep(i,n) for(ll i=(ll)(n)-1;i>=0;i--)
#define REP(i,m,n) for(ll i=(ll)(m);i<(ll)(n);i++)
#define rep(i,n) REP(i,0,n)
#define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++)
template<Z A>void pr(A a){cout<<a;ln;}
template<Z A,Z B>void pr(A a,B b){cout<<a<<' ';pr(b);}
template<Z A,Z B,Z C>void pr(A a,B b,C c){cout<<a<<' ';pr(b,c);}
template<Z A,Z B,Z C,Z D>void pr(A a,B b,C c,D d){cout<<a<<' ';pr(b,c,d);}
template<Z A>void PR(A a,ll n){rep(i,n){if(i)cout<<' ';cout<<a[i];}ln;}
ll check(ll n,ll m,ll x,ll y){return x>=0&&x<n&&y>=0&&y<m;}
const ll MAX=1e9+7,MAXL=1LL<<61,dx[4]={-1,0,1,0},dy[4]={0,1,0,-1};
typedef pair<ll,ll> P;
typedef pair<P,P> PP;

ll dp[2222][4222];
void Main() {
  ll n;
  R n;
  ll a[n],b[n],c[n],d[n];
  rep(i,n) cin >> a[i] >> b[i] >> c[i] >> d[i];
  PP p[n];
  rep(i,n) p[i]=PP(P(d[i],a[i]),P(b[i],c[i]));
  sort(p,p+n,greater<PP>());
  rep(i,2222)rep(j,4222) dp[i][j]=-MAX;
  dp[0][4100]=0;
  rep(i,n) {
    rep(j,4101) dp[i+1][j]=max(dp[i+1][j],dp[i][j]);
    rep(j,4101) {
      if(p[i].F.S>=j) continue;
      ll y=min(j-p[i].F.S,p[i].F.F);
      ll z=(y-1)/p[i].F.S;
      rep(k,min(300LL,z+1)) {
        ll e=z-k;
        ll x=y-e*p[i].F.S;
        dp[i+1][x]=max(dp[i+1][x],dp[i][j]+e*p[i].S.S+p[i].S.F);
      }
      rep(k,min(300LL,z+1)) {
        ll e=k;
        ll x=y-e*p[i].F.S;
        dp[i+1][x]=max(dp[i+1][x],dp[i][j]+e*p[i].S.S+p[i].S.F);
      }
    }
  }
  ll ans=0;
  rep(i,n+1)REP(j,1,4222) ans=max(ans,dp[i][j]);
  pr(ans);
}

int main(){ios::sync_with_stdio(0);cin.tie(0);Main();return 0;}
accuracy: 0.265625
time_ms: 140
memory_kb: 76540
final_rank: 20