#!/usr/bin/env python3 # 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. (n := int(input())), (dp := [[-(10 ** 18)] * (i + 2) for i in range(n + 1)]) dp[0][0], e = 0, 0 for i in range(1, n + 1): last_e, (s, p, e, a) = e, 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. if a <= dp[i - 1][k - 1] + s - last_e: dp[i][k] = max(dp[i][k], dp[i - 1][k - 1] - a + e - p + s - last_e) print(max(k for k in range(n + 1) if dp[n][k] >= 0))