#!/usr/bin/env python3 n = int(input()) # dp[i][k]: # the maximal amount of leftover studying time up to e_i (the end of exam i), given that we pass k out of i exams. dp = [[-(10**18)] * (i + 2) for i in range(n + 1)] dp[0][0] = 0 last_e = 0 for i in range(1, n + 1): s, p, e, a = map(int, input().split()) # We can pass 0..=i of the first i exams for k in range(0, i + 1): # Skip the current exam. dp[i][k] = max(dp[i][k], dp[i - 1][k] + s - last_e) # Pass the current exam. # NOTE: WA because this should also add `s - last_e`. if a <= dp[i - 1][k - 1]: dp[i][k] = max(dp[i][k], dp[i - 1][k - 1] - a + e - p + s - last_e) last_e = e maxk = 0 for k in range(n + 1): if dp[n][k] >= 0: maxk = k print(maxk)