Worst Reporter 4
Time limit2sMemory limit512 MB
- Level
Not classified yet
- Solved
- No attempts yet
Problem
Bitaro is a professional reporter who writes reports on competitive programming contests. An international contest will be held in a few days, and Bitaro plans to write a report on it.
There are N contestants, numbered from 1 to N. Each contestant has an integer rating that measures their strength in competitive programming. A rating is an integer between 1 and 1,000,000,000, inclusive.
Bitaro interviewed the contestants and obtained the following information.
The rating of contestant i (1 ≤ i ≤ N) is greater than or equal to the rating of contestant Ai (1 ≤ Ai ≤ N). It is possible that Ai = i.
After the interviews, Bitaro received a list of contestant ratings from the company that manages the rating system. The list contains the following information.
The rating of contestant i (1 ≤ i ≤ N) is equal to Hi.
The list may contain errors. Bitaro has no time to obtain a correct list before the deadline, so he decided to change some ratings in the list so that the list does not contradict the interview information. Changing the rating of contestant i costs Ci. Bitaro can change the rating of contestant i to any integer between 1 and 1,000,000,000, inclusive, by paying Ci. He wants to minimize the total cost of the changes.
Input
The input is given in the following format.
N
A1 H1 C1
.
.
.
AN HN CN
Output
Print the minimum total cost on one line.
Constraints
- 2 ≤ N ≤ 200,000
- 1 ≤ Ai ≤ N (1 ≤ i ≤ N)
- 1 ≤ Hi ≤ 1,000,000,000 (1 ≤ i ≤ N)
- 1 ≤ Ci ≤ 1,000,000,000 (1 ≤ i ≤ N)