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

#BS0000410. Vượt qua sa mạc (Through the Desert)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Vượt qua sa mạc (Through the Desert)

Through the Desert

Source: UVa

Version: Phuoc Hung OJ Extended

Problem Statement

An explorer drives across a desert, starting at position 0 with a full fuel tank. Fuel consumption can change, leaks can appear, and the vehicle may encounter gas stations or mechanics.

The journey is described by the following events:

  • Fuel consumption n: from this position onward the engine uses nn litres per 100 km until the rate changes again.
  • Leak: one additional puncture appears in the tank. Each active puncture loses 1 litre per kilometre; losses from several punctures add together.
  • Gas station: refill the tank completely. Existing leaks are not repaired.
  • Mechanic: repair all active punctures without adding fuel.
  • Goal: the destination is reached and the journey ends.

Fuel is consumed while travelling between positions. Events at the same position are processed in input order. The vehicle may arrive with exactly zero fuel and process the event there. Find the smallest tank capacity allowing the vehicle to complete the journey without running out of fuel between positions.

Input

Each line describes an event: an integer did_i, its distance in kilometres from the start, followed by one of the five event descriptions above. A Fuel consumption n event includes the integer parameter nn. The first line is 0 Fuel consumption n. The last line is d Goal, and there is exactly one Goal event.

Let mm be the number of event lines. The value mm is not supplied separately; read through the Goal line.

Output

Print one line giving the minimum tank capacity in litres with exactly three digits after the decimal point.

Subtasks

  • Subtask 1 (20 points): 2≤m≤502\le m\le50; 0=d1≤d2≤⋯≤dm≤1090=d_1\le d_2\le\cdots\le d_m\le10^9; 1≤n≤301\le n\le30 in every Fuel consumption n event; the first line is 0 Fuel consumption n, the last is d Goal, with no earlier Goal; each line contains exactly one valid event. Only Fuel consumption n and Goal occur.
  • Subtask 2 (30 points): 2≤m≤502\le m\le50; 0=d1≤d2≤⋯≤dm≤1090=d_1\le d_2\le\cdots\le d_m\le10^9; 1≤n≤301\le n\le30 in every Fuel consumption n event; the first line is 0 Fuel consumption n, the last is d Goal, with no earlier Goal; each line contains exactly one valid event. No Gas station event occurs.
  • Subtask 3 (50 points): 2≤m≤502\le m\le50; 0=d1≤d2≤⋯≤dm≤1090=d_1\le d_2\le\cdots\le d_m\le10^9; 1≤n≤301\le n\le30 in every Fuel consumption n event; the first line is 0 Fuel consumption n, the last is d Goal, with no earlier Goal; each line contains exactly one valid event.

Examples

Input

0 Fuel consumption 10
100 Goal

Output

10.000

Explanation

The engine uses 10 litres per 100 km. The trip is 100 km without other events, so exactly 10 litres are needed.

Input

0 Fuel consumption 5
100 Fuel consumption 30
200 Goal

Output

35.000

Explanation

The first 100 km use 5 litres and the next 100 km use 30 litres. With no refill, the capacity must be at least 5+30=355+30=35 litres.

Input

0 Fuel consumption 20
10 Leak
25 Leak
25 Fuel consumption 30
50 Gas station
70 Mechanic
100 Leak
120 Goal

Output

81.000

Explanation

From km 0 to 10 the vehicle uses 2 litres; from 10 to 25 it uses 15(0.2+1)=1815(0.2+1)=18 litres; from 25 to 50 it uses 25(0.3+2)=57.525(0.3+2)=57.5 litres. Total before refilling is 77.5 litres. After refilling, the next three intervals use 46, 9 and 26 litres, respectively, totaling 81. A capacity of 81 litres is necessary and sufficient.