PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#BF0000048. Chênh lệch tỷ giá II (Arbitrage II)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

2 lộ trình

Chênh lệch tỷ giá II (Arbitrage II)

Arbitrage II

Source: UVa

Version: Phuoc Hung OJ Extended

Problem Statement

Given named currencies and directed, commission-free exchange rates, decide whether some sequence of exchanges starts and ends in the same currency with a net gain.

Input

First line nn, then nn currency names, then mm and mm lines from rate to.

Output

Print Yes if arbitrage is possible, otherwise No.

Subtasks

  • Subtask 1 (20 points): 1≤n≤51\le n\le5, 0≤m≤150\le m\le15; Currency names contain 1 to 30 non-whitespace characters; every rate r>0r>0.
  • Subtask 2 (30 points): 1≤n≤151\le n\le15, 0≤m≤1200\le m\le120; Currency names contain 1 to 30 non-whitespace characters; every rate r>0r>0.
  • Subtask 3 (50 points): 1≤n≤301\le n\le30, 0≤m≤9000\le m\le900; Currency names contain 1 to 30 non-whitespace characters; every rate r>0r>0.

Examples

Input

3
USDollar
BritishPound
FrenchFranc
3
USDollar 0.5 BritishPound
BritishPound 10.0 FrenchFranc
FrenchFranc 0.21 USDollar

Output

Yes

Explanation

Starting with 1 US dollar, the conversions yield 1×0.5×10×0.21=1.051\times0.5\times10\times0.21=1.05 US dollars. This exceeds the starting amount, so arbitrage is possible and the output is Yes.