#P1672. [USACO05FEB] Feed Accounting S

[USACO05FEB] Feed Accounting S

Description

John wants to know when the previous shipment of feed arrived. Before that shipment arrived, his cows had just finished all the feed that was originally in the barn. He received F1(1F1106)F1(1\le F1\le 10^6) kilograms of feed. Unfortunately, he no longer remembers which day that was. By day D(1D2×103)D(1\le D\le 2\times 10^3), there are still F2(1F2F1)F2(1\le F2\le F1) kilograms of feed left in the barn.

John has C(1C100)C(1\le C\le 100) cows, and each cow eats exactly 11 kilogram of feed per day. Due to various reasons, each cow starts eating at the barn on some day and leaves on some day, so the feed consumption may vary greatly between different days. Each cow eats on both the day it arrives and the day it leaves. Given today’s day DD, write a program to determine when the feed was most recently delivered. Today the cows have already eaten, and on the delivery day the cows had not yet eaten.

If there are multiple possible answers, output the largest one (i.e., the most recent).

Input Format

Line 11: Four integers CC, F1F1, F2F2, DD, separated by spaces.

Lines 22 to C+1C+1: Each line contains two integers separated by spaces, representing the day a cow starts eating at the barn and the day it leaves.

Output Format

A single positive integer: the day when the previous shipment of feed arrived.

3 14 4 10
1 9
5 8
8 12
6

Hint

Sample Explanation

The previous shipment delivered 1414 kilograms of feed, and there are now 44 kilograms left. In the last 1010 days, 33 cows have come to eat.

John received 1414 kilograms of feed on day 66. They ate 22 kilograms on day 66, 22 kilograms on day 77, 33 kilograms on day 88, 22 kilograms on day 99, and 11 kilogram on day 1010, leaving exactly 44 kilograms.

Constraints

1F2F11061\le F2\le F1\le 10^6, 1D2×1031\le D\le 2\times 10^3, 1C1001\le C\le 100.

Translated by ChatGPT 5