Train Ticket Allocation
Time limit1sMemory limit256 MB
Decide how many tickets to sell for each station pair so paid and free riders fit capacity P on every segment and total income is maximal.
- Level
Medium7 of 10
- Topics
- Graph, Shortest path, Intervals
- Solved
- No attempts yet
Problem
A train starts at station 1, passes the stations in increasing order and stops at station . A ticket from station to station can be sold whenever .
The law fixes the price of a ticket from station to station in advance, and the exact demand for that pair is known before the trip, so you may sell anywhere from 0 to tickets for it. The government also sets aside free tickets from station to station . A passenger holding one of those takes a seat but brings no income.
The train carries passengers at most. On every segment between two adjacent stations the number of passengers on board, counting the ones with government tickets, must not exceed . Selling beyond the capacity is not allowed.
Find the largest income the trip can produce.
Input
The first line contains the number of test cases .
Each test case begins with a line holding the number of stations and the train capacity . The next lines give the ticket prices. Line of that block holds numbers, and its -th number is , the price of a ticket from station to station . The next lines give the demands in the same format, and the lines after those give the number of free government tickets , again in the same format.
- The government tickets alone never exceed the capacity.
Output
For each test case, print the maximum possible income on its own line.