Railroad Management
시간 제한40초메모리 제한1024 MB
각 역이 정확히 C_i량의 화차를 역 D_i로 보낼 때, 어떤 순서로든 모든 배송이 가능하도록 하는 최소 초기 화차 총량을 구한다.
문제
You are in charge of the management of a railroad network. The network consists of stations. Each station needs to ship goods to exactly one other station . Station will send exactly one shipment, in a train with exactly railroad cars.
You get all the shipment information well in advance, so you plan on saving on railroad cars by reusing them. If station sends railroad cars to station , then can add those railroad cars to its supply to use for its own shipment if it did not already happen.
Formally, you must give an initial supply of railroad cars to each station (some stations may get ) and provide an order for the shipments so that, by the time station must ship, the number of railroad cars between its initial supply and any previous shipments that arrived at must be at least the number it needs for its own shipment . You cannot send more than cars in a shipment out of station , even if the station has more than available.
For example, suppose that station sends a train carrying exactly railroad cars to station . Now, if station needs cars, it could reuse of the cars it received from station . And if station needs to send cars, it can reuse all cars received from station and add of its own supply. Note that when station needs to send cars, it cannot send all it received from station .
Given the shipment information, what is the minimum number of railroad cars you need to distribute for the stations' initial supplies, such that you can do all shipments in some order?
입력
The first line of the input gives the number of test cases, . test cases follow. Each test case consists of lines. The first line contains a single integer , the number of stations in the network. The second line contains integers and the third and last line contains integers . These represent that station must send a train of exactly railroad cars to station .
출력
For each test case, output one line containing Case #x: y, where is the test case number (starting from 1) and is the minimum number of railroad cars you need to distribute among the stations so that all shipments can be performed.
제한
- .
- , for all .
- , for all .
- , for all .
힌트
In Sample Case #1 one optimal way is to do the shipments in increasing order of departure station. That requires sending cars to station . But after that, each station receives enough cars for its shipment, for a total of overall. Since no cars arrive at station , it definitely needs the initial , so this is also the minimum possible.

In Sample Case #2 one minimal way is to supply car to station and cars each to stations and and , for a total of . Then, we can start with the shipment which gets one additional car to station . This makes station have the cars it needs to ship . Station now has cars which is enough to do with a single car, taking the total at station to cars, enough to do the final shipment . Notice that the shipment cannot bring extra cars to station , even though there are cars available and it would be helpful to do so. There are other ways to do all shipments with initial cars, but no way to do it with less.

In Sample Case #3, one optimal starting number of cars is cars at stations and and cars at stations and .