atcoder#ABC226C. [ABC226C] Martial artist
[ABC226C] Martial artist
Score : points
Problem Statement
Takahashi is a martial artist. There are moves that a martial artist can learn, called Move , , , . For each , it takes minutes of practice to learn Move . Additionally, at the beginning of that practice, all of the Moves , , , must be already learned. Here, it is guaranteed that for each .
Takahashi has not learned any move at time . He cannot practice for more than one move simultaneously, nor can he stop a practice he has already started. Find the minimum number of minutes needed for Takahashi to learn Move .
Constraints
- , , , are all distinct.
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Output
Print the minimum number of minutes needed for Takahashi to learn Move .
3
3 0
5 1 1
7 1 1
10
Here is one possible plan for Takahashi.
- At time , start practicing for Move to learn Move at time .
- Then, at time , start practicing for Move to learn Move at time .
Here, Takahashi spends minutes to learn Move , which is the fastest possible. Note that he does not need to learn Move to learn Move .
5
1000000000 0
1000000000 0
1000000000 0
1000000000 0
1000000000 4 1 2 3 4
5000000000
Note that the answer may not fit into a -bit integer.