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

#BF0000022. Tìm việc làm (Job Hunt)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Tìm việc làm (Job Hunt)

Job Hunt

Source: Luogu

Version: Phuoc Hung OJ Extended

Problem Statement

Bessie starts in city SS among CC cities. She earns DD dollars whenever she works in a city, including the starting city, and may earn DD again after leaving and returning. There are PP free one-way paths and FF one-way flights costing TiT_i. Flights may be paid from future income, so intermediate cash need not be nonnegative. Bessie may retire in any city at any time. Find her maximum possible money, or print -1 if it is unlimited.

Input

The first line contains D,P,C,F,SD,P,C,F,S. Then follow PP lines Ai,BiA_i,B_i for free paths and FF lines Ji,Ki,TiJ_i,K_i,T_i for flights.

Output

Print the maximum achievable money, or -1 if unbounded.

Subtasks

  • Subtask 1 (20 points): 1≤D≤1001\le D\le100; 2≤C≤122\le C\le12; 1≤P≤201\le P\le20; 1≤F≤201\le F\le20; 1≤Ti≤10001\le T_i\le1000; routes are directed and connect distinct cities.

  • Subtask 2 (30 points): 1≤D≤10001\le D\le1000; 2≤C≤602\le C\le60; 1≤P≤901\le P\le90; 1≤F≤1201\le F\le120; 1≤Ti≤500001\le T_i\le50000; routes are directed and connect distinct cities.

  • Subtask 3 (50 points): 1≤D≤10001\le D\le1000; 2≤C≤2202\le C\le220; 1≤P≤1501\le P\le150; 1≤F≤3501\le F\le350; 1≤Ti≤500001\le T_i\le50000; routes are directed and connect distinct cities.

Examples

Input

100 3 5 2 1
1 5
2 3
1 4
5 2 150
2 5 120

Output

250

Explanation

Work in city 11, take the free path to 55, fly to 22 for 150150, then take the free path to 33: 100+100−150+100+100=250100+100-150+100+100=250.