Music Festival
Time limit1sMemory limit1024 MB
Given albums whose tracks play in fixed order, find the album order that maximizes how many tracks are louder than every track heard before them.
- Level
Medium6 of 10
- Topics
- Dynamic programming, Segment tree, Sorting
- Solved
- No attempts yet
Problem
Vitya is a boy who loves listening to music. He follows the news of his favorite bands closely, so he knows that albums come out this Friday, and the -th album has tracks. As a devoted fan, Vitya has already listened to every track that will be released soon. He knows that the coolness of the -th track of the -th album is . Vitya has a friend, Masha, whom he wants to invite to a music festival where his favorite bands perform. Before she agrees, she must listen to the new releases. Each time Masha hears a track that is cooler than all the tracks before it, she gets 1 point of impression. Albums can only be played in full, and the tracks inside an album cannot be reordered. Find an order of the albums that gives Masha the largest possible impression, so that she is sure to go to the festival with him.
Input
The first line contains a single integer (), the number of albums. Each album is described by two lines. The first line contains a single integer (), the number of tracks in the -th album. The next line contains integers (), the coolness of the tracks in the -th album. It is guaranteed that the sum of all does not exceed 200000.
Output
Print a single number, the maximum impression that Masha can get.
Hint
In the first sample, the optimal order is to listen to albums 4, 2, 3, and 1. In that case Masha hears the tracks in this order: 1; 7; 8, 6; 4, 9, 4, 6, 8, and gets 4 points of impression. In the second sample, you must listen to album 1 first, then album 4, and albums 2 and 3 in any order. In that case Masha gets points for every track in albums 1 and 4, and nothing for albums 2 and 3.