Restaurant Recommendations

Restaurants recommend each other through a favorite digraph; find the cheapest cost to visit exactly k restaurants for every k, where each step's price depends on whether the recommender is a favorite of the current restaurant.

Hard8GraphDynamic programmingShortest pathGreedyNo attempts yetTime limit3sMemory limit128 MB

Problem

Luka's town has NN restaurants, numbered from 1 to NN, and everyone can find something to like there. The restaurant owners also have favorite restaurants that they like to visit. If you ask a restaurant owner for a recommendation, the owner recommends their own restaurant and their favorite restaurants, and also every restaurant that the owners of those favorites would recommend.

In other words, the owner of restaurant ii recommends restaurant ii itself and every restaurant that can be reached from ii by following favorites one or more times.

The table below shows an example with four restaurants.

Restaurant ownerFavorite restaurantsRecommended restaurants
121, 2, 3, 4
232, 3, 4
32, 42, 3, 4
4none4

Luka plans to visit several restaurants as follows:

  • He chooses the first restaurant freely.
  • To choose each next restaurant, he asks the owner of the current restaurant for a recommendation and picks one of the recommended restaurants that he has not visited yet.
  • Luka can end the tour at any moment.

Each restaurant AA has two prices for its main menu, XAX_A and YAY_A. When Luka enters a restaurant, its owner asks him who recommended the restaurant. If that person is the owner of restaurant BB, Luka pays:

  • XAX_A kuna if the owner of restaurant AA recommends restaurant BB,
  • YAY_A kuna otherwise. Luka also pays this amount at the first restaurant.

Let KK be the largest number of restaurants Luka can visit this way. For every kk from 11 to KK, find the minimum number of kuna Luka needs to visit exactly kk restaurants.

Input

The first line contains an integer NN (1N10001 \le N \le 1000), the number of restaurants.

Each of the next NN lines contains several integers.

The first two numbers on the ii-th line are the main menu prices XiX_i and YiY_i (1Xi,Yi100001 \le X_i, Y_i \le 10000). The third number OiO_i (0Oi<N0 \le O_i < N) is the number of favorite restaurants of the owner of restaurant ii. The remaining OiO_i numbers are the labels of those favorite restaurants. The labels are pairwise distinct, and none of them equals ii.

Output

If KK is the largest number of restaurants Luka can visit, print KK lines. On the kk-th line, print the minimum number of kuna Luka must pay to visit exactly kk restaurants.

Hint

In the first example, the cheapest way to visit one restaurant is to visit restaurant 1 (200 kuna).

The cheapest way to visit two restaurants is to visit restaurant 3 (250 kuna) and then restaurant 2 (200 kuna).

The cheapest way to visit three restaurants is to visit restaurant 1 (200 kuna), restaurant 3 (250 kuna), and finally restaurant 2 (200 kuna).

The cheapest way to visit four restaurants is to visit restaurant 1 (200 kuna), restaurant 3 (250 kuna), restaurant 2 (200 kuna), and finally restaurant 4 (300 kuna).