# You may need to purchase (R-S)+B+d <= 99+20+100 = 219 power in a day. # This solution purchases just less than that. # The constants need to be updated if we change the bounds MAX_R_MINUS_S = 99 MAX_BATTERY = 20 MAX_D = 100 MAX_PURCHASE = MAX_R_MINUS_S + MAX_BATTERY + MAX_D n, m, b = tuple(map(int, input().split())) PRS = [tuple(map(int, input().split())) for _ in range(n)] C = [list(map(int, input().split()))+[0] for _ in range(n)] D = [list(map(int, input().split()))+[0] for _ in range(n)] max_purchase_amount = max(max(d) for d in D) DP = [[None for _ in range(b + 1)] for _ in range(n + 1)] def f(day: int, battery: int) -> int: if day == n: return 0 if DP[day][battery] is not None: return DP[day][battery] p, r, s = PRS[day] used_solar = min(r, s) r -= used_solar s -= used_solar excess = battery + s best = f(day + 1, min(b, excess)) + r * p for purchased_power in range(MAX_PURCHASE): # Intentionally 1 less than max needed excess_with_purchase = excess + purchased_power - r if excess_with_purchase < 0: continue for c, d in zip(C[day], D[day]): end_battery = min(b, excess_with_purchase - d) if end_battery < 0: continue best = min(best, f(day + 1, end_battery) - c + purchased_power * p) DP[day][battery] = best return DP[day][battery] print(f(0, 0))