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

#FW0000057. Kinh doanh chênh lệch tiền tệ (Arbitrage)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Kinh doanh chênh lệch tiền tệ (Arbitrage)

Arbitrage

Source: Kattis

Version: Phuoc Hung OJ Extended

Problem Statement

Directed exchange rates between named currencies are given. An arbitrage opportunity exists if some closed sequence of exchanges multiplies one unit of currency into strictly more than one unit. Determine whether such a sequence exists.

Input

First n; then n distinct currency names; then m; then m lines sourceCurrency exchangeRate targetCurrency.

Output

Print exactly YES if an arbitrage cycle exists, otherwise NO.

Subtasks

  • Subtask 1 (40 points): 2≤n≤6, 0≤m≤20, 0.01≤r≤102\le n\le 6,\ 0\le m\le 20,\ 0.01\le r\le 10. Currency names are distinct alphanumeric tokens with no spaces. Rates are positive; self-exchange is allowed.
  • Subtask 2 (60 points): 2≤n≤30, 0≤m≤500, 0.01≤r≤102\le n\le 30,\ 0\le m\le 500,\ 0.01\le r\le 10. Currency names are distinct alphanumeric tokens with no spaces. Rates are positive; self-exchange is allowed.

Examples

Input

3
USD
GBP
FRF
3
USD 0.50000 GBP
GBP 10.00000 FRF
FRF 0.21000 USD

Output

YES

Explanation

USD→GBP→FRF→USD has product 0.5 × 10 × 0.21 = 1.05 > 1, so arbitrage exists.