#P1672. Determining the Latest Feed Delivery Day

    ID: 14958 Type: Default 1000ms 256MiB

Determining the Latest Feed Delivery Day

Determining the Latest Feed Delivery Day

John received a shipment of \(F1\) kg of feed (\(1 \le F1 \le 10^6\)). Before this shipment arrived, the cows had exactly finished the previous feed in the warehouse. As of day \(D\) (\(1 \le D \le 2 \times 10^3\)), there remain \(F2\) kg of feed (\(1 \le F2 \le F1\)). John keeps \(C\) cows (\(1 \le C \le 100\)), and every cow eats exactly 1 kg of feed on every day it is present at the warehouse. Note that if a cow comes on a day or leaves on a day, it still consumes feed on that day.

Today, the cows have already eaten their feed, while on the day the shipment arrived, the cows had not yet eaten. Let the shipment arrival day be \(d\) and define \(n = D-d\) (with \(n \ge 1\)). Since the feed available right after delivery was \(F1\) and by day \(D\) there remains \(F2\), the total feed consumed from day \(d+1\) to day \(D\) is \(F1-F2\). Because the cows come and go in continuous intervals (each cow, if present, always consumes on consecutive days), the total consumption over these \(n\) days can be expressed as the sum of contributions of each cow. In particular, if exactly \(k\) cows were active (\(1 \le k \le C\)), then each cow can consume at most \(n\) kg, so the total consumption satisfies the inequality \[ k \le F1-F2 \le k\cdot n.\] Among all possible answers, if there are multiple possible days for \(d\) then the most recent one (i.e. the maximum \(d\)) is desired. Thus, you are asked to determine the largest \(d\) (which is equivalent to finding the smallest valid \(n=D-d\)) such that there exists some integer \(k\) (\(1 \le k \le C\)) satisfying the above inequality.

Input Format: Four space-separated integers \(F1\), \(F2\), \(C\), and \(D\) representing the delivered feed amount, remaining feed amount as of day \(D\), number of cows, and today's day number, respectively.

Output Format: Output an integer representing the most recent day on which the feed was delivered.

inputFormat

Four space-separated integers: F1 F2 C D

outputFormat

An integer indicating the latest possible day d on which the feed delivery occurred.

sample

100 40 10 10
4