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

#BS0000385. Nhà sư leo núi (Traveling Monk)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Nhà sư leo núi (Traveling Monk)

Traveling Monk

Source: Kattis

Version: Phuoc Hung OJ Extended

Problem Statement

A monk climbs a mountain one morning and descends along the same path on another morning. Both trips start at sunrise, defined as time 0. Each trip consists of consecutive segments with constant elevation change per unit time; resting segments have zero elevation change. Total elevation gained equals total elevation lost. After finishing a trip, the monk remains at its destination. Find the earliest elapsed time at which the elevations along both trips coincide.

Input

The first line contains integers a,da,d, the numbers of ascent and descent segments. Each of the next aa lines contains integers h,th,t, elevation gain and duration of an ascent segment. Each of the final dd lines contains integers h,th,t, elevation loss and duration of a descent segment. Segments are listed in travel order.

Output

Print the earliest elapsed time. The absolute or relative error must not exceed 10−510^{-5}.

Subtasks

  • Subtask 1 (20 points): 1≤a,d≤11\le a,d\le 1; 0≤h≤10000\le h\le1000; 1≤t≤1001\le t\le100 for every segment; total elevation gained equals total elevation lost.
  • Subtask 2 (30 points): 1≤a,d≤501\le a,d\le 50; 0≤h≤10000\le h\le1000; 1≤t≤1001\le t\le100 for every segment; total elevation gained equals total elevation lost.
  • Subtask 3 (50 points): 1≤a,d≤50001\le a,d\le 5000; 0≤h≤10000\le h\le1000; 1≤t≤1001\le t\le100 for every segment; total elevation gained equals total elevation lost.

Examples

Input

1 1
10 11
10 10

Output

5.238095

Explanation

The ascent gains 10 in 11 time units, while the descent loses 10 in 10 units. Their elevations are 10t/1110t/11 and 10−t10-t. Equating them yields t=110/21t=110/21.

Input

3 1
4 2
0 3
6 3
10 7

Output

4.200000

Explanation

The ascending monk reaches elevation 4 at time 2 and rests until time 5. The descending elevation is 10−10t/710-10t/7. At time 4.2 it equals 4, and earlier it is higher than the ascending elevation.

Input

3 3
2 3
0 5
3 1
3 4
0 2
2 2

Output

4.000000

Explanation

The mountain height is 5. The ascending monk reaches elevation 2 at time 3 and rests until time 8. The descending monk loses 3 by time 4 and rests until time 6. The elevations coincide throughout [4,6], so the earliest time is 4.