eolymp
bolt
Попробуйте наш новый интерфейс для отправки задач
Задачи

Machine Works

Machine Works

You are the director of Arbitrarily Complex Machines (ACM for short), a company producing advanced machinery using even more advanced machinery. The old production machinery has broken down, so you need to buy new production machines for the company. Your goal is to make as much money as possible during the restructuring period. During this period you will be able to buy and sell machines and operate them for profit while ACM owns them. Due to space restrictions, ACM can own at most one machine at a time. During the restructuring period, there will be several machines for sale. Being an expert in the advanced machines market, you already know the price \textbf{P_i} and the availability day \textbf{D_i} for each machines \textbf{M_i}. Note that if you do not buy machine \textbf{M_i} on day \textbf{D_i}, then somebody else will buy it and it will not be available later. Needless to say, you cannot buy a machine if ACM has less money than the price of the machine. If you buy a machine \textbf{M_i} on day \textbf{D_i}, then ACM can operate it starting on day \textbf{D_i + 1}. Each day that the machine operates, it produces a profit of \textbf{G_i} dollars for the company. You may decide to sell a machine to reclaim a part of its purchase price any day after you've bought it. Each machine has a resale price \textbf{R_i} for which it may be resold to the market. You cannot operate a machine on the day that you sell it, but you may sell a machine and use the proceeds to buy a new machine on the same day. Once the restructuring period ends, ACM will sell any machine that it still owns. Your task is to maximize the amount of money that ACM makes during the restructuring. \InputFile The input consists of several test cases. Each test case starts with a line containing three positive integers \textbf{N}, \textbf{C}, and \textbf{D}. \textbf{N} is the number of machines for sale (\textbf{N} ≤ \textbf{10^5}), \textbf{C} is the number of dollars with which the company begins the restructuring (\textbf{C} ≤ \textbf{10^9}), and \textbf{D} is the number of days that the restructuring lasts (\textbf{D} ≤ \textbf{10^9}). Each of the next \textbf{N} lines describes a single machine for sale. Each line contains four integers \textbf{D_i}, \textbf{P_i}, \textbf{R_i} and \textbf{C_i}, denoting (respectively) the day on which the machine is for sale, the dollar price for which it may be bought, the dollar price for which it may be resold and the daily profit generated by operating the machine. These numbers satisfy \textbf{1} ≤ \textbf{D_i} ≤ \textbf{D}, \textbf{1} ≤ \textbf{R_i} < \textbf{P_i} ≤ \textbf{10^9} and \textbf{1} ≤ \textbf{G_i} ≤ \textbf{10^9}. The last test case is followed by a line containing three zeros. \OutputFile For each test case, display its case number followed by the largest number of dollars that ACM can have at the end of day \textbf{D+1}. Follow the format of the sample output.
Лимит времени 20 секунд
Лимит использования памяти 256 MiB
Входные данные #1
6 10 20
6 12 1 3
1 9 1 2
3 2 1 2
8 20 5 4
4 11 7 4
2 10 9 1
0 0 0
Выходные данные #1
Case 1: 44
Источник ICPC 2011 World Finals