#K40602. Ship Package Delivery Problem

    ID: 26679 Type: Default 1000ms 256MiB

Ship Package Delivery Problem

Ship Package Delivery Problem

You are given a cargo ship that makes deliveries and pickups at a series of n destinations. At each destination i (0-indexed), the ship needs to deliver a package weighing w[i] and then pick up a package weighing p[i]. The ship has a maximum load capacity of m at any time. Initially, the ship is empty.

The procedure at each destination is as follows:

  1. Check if the ship can load the package for delivery without exceeding its capacity. If current_load + w[i] ≤ m, the package is delivered (which counts as a successful delivery) and the ship’s load increases by w[i]; otherwise, the process terminates.
  2. Immediately after delivery, the delivered package is removed from the ship (simulating a delivery drop-off), and then the ship picks up the package at the same destination. If the ship can pick up the package (i.e. current_load + p[i] ≤ m), the pickup is made; if not, the ship fills up to capacity.

Your task is to compute the maximum number of packages that can be successfully delivered under these constraints. All calculations and decisions must obey the ship’s maximum capacity constraint at every step.

Note: All formulas should be written in LaTeX format. For example, the capacity constraint is given by \(current\_load + w[i] \leq m\).

inputFormat

The input is read from standard input (stdin) and consists of three lines:

  • The first line contains two space-separated integers: \(n\) (the number of destinations) and \(m\) (the maximum load capacity of the ship).
  • The second line contains \(n\) space-separated integers representing the delivery weights \(w[0], w[1], \dots, w[n-1]\).
  • The third line contains \(n\) space-separated integers representing the pickup weights \(p[0], p[1], \dots, p[n-1]\).

outputFormat

Output a single integer to standard output (stdout) indicating the maximum number of packages that the ship can deliver.

## sample
3 50
10 20 30
15 25 35
2