This page is still under construction.

Parts of this page are still being built. What you see may change.

Jedi Academy

Time limit2sMemory limit512 MB

Summary
Given a DAG of skill prerequisites and two buildings, find the shortest total time to learn all skills, counting travel and learning time.
Level

Medium7 of 10

Topics
Graph, Dynamic programming, Topological sort, Greedy
Solved
No attempts yet

Problem

Becoming a Jedi requires mastering many theoretical and practical skills. At the Jedi Academy you can learn everything you need, provided you have the talent.

The academy's new student Phil is very talented and studies individually. He is free to build his own schedule. Phil is curious, so he devotes all his time to studying. The only moments Phil is not studying are when he moves from one academy building to the other or walks from the dormitory to one of the buildings. The academy has two buildings: one teaches theoretical skills and the other teaches practical skills. Traveling from one building to the other takes exactly aa minutes. Learning any skill takes exactly bb minutes. Initially Phil is in the dormitory, and the trip from the dormitory to either building also takes aa minutes.

Naturally, skills cannot be learned in an arbitrary order. For example, before mastering a lightsaber you must first learn the basics of optics and the art of unarmed combat. Phil has to take this into account when building his schedule.

Phil wants to become a Jedi as soon as possible, but to do so he must learn all the necessary skills. Before starting his studies, he wants to know the minimum number of minutes in which he can learn all the skills. Help him find out. After finishing his studies Phil immediately sets off to fight evil, so he does not need to return to the dormitory.

Input

The first line of the input contains an integer nn, the number of skills to be learned at the academy (1≤n≤1051 \le n \le 10^5). All skills are numbered from 1 to nn.

The next nn lines describe the requirements for learning each skill. At the start of the ii-th of these lines there is the number 1 or 2, indicating in which building the ii-th skill can be learned. Then comes the number kk, the number of skills required to learn skill ii. Then, on the same line, come kk integers, the skills required to learn skill ii. The sum of the numbers kk over all skills does not exceed 10510^5.

The next line contains two integers: aa, the time in minutes to travel from one building to the other or from the dormitory to a building, and bb, the time to learn one skill (1≤a,b≤1041 \le a, b \le 10^4).

There exists an order of learning all the skills such that whenever a skill is learned, all the skills required for it have already been learned.

Output

In a single line of the output, print the minimum time in minutes Phil needs to become a Jedi.

Examples1

  1. Example 1

    Input
    6
    1 3 3 4 5
    2 1 4
    2 2 5 6
    1 1 6
    1 0
    1 0
    15 40
    
    Expected output
    285