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

#GD0000101. Ít lần tiếp nhiên liệu nhất (Minimum Number of Refueling Stops)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Ít lần tiếp nhiên liệu nhất (Minimum Number of Refueling Stops)

Minimum Number of Refueling Stops

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

A car drives from coordinate 00 to destination DD, initially with FF units of fuel. It consumes one fuel unit per distance unit. There are nn stations listed in increasing order, station ii at pip_i providing fif_i fuel once when chosen. The tank has unlimited capacity. Arriving with zero fuel is allowed. Find the minimum number of refueling stops or -1 if impossible.

Input

Line 1 contains D,F,nD,F,n. The following nn lines contain pi,fip_i,f_i in strictly increasing position order.

Output

Print the fewest stops, or -1 if the destination is unreachable.

Subtasks

  • Subtask 1 (20 points): 1≤D≤1091\le D\le10^9; 0≤F≤1090\le F\le10^9; 0≤n≤120\le n\le 12; 0<p1<⋯<pn<D0<p_1<\cdots<p_n<D; 1≤fi≤1091\le f_i\le10^9.
  • Subtask 2 (30 points): 1≤D≤1091\le D\le10^9; 0≤F≤1090\le F\le10^9; 0≤n≤1000\le n\le 100; 0<p1<⋯<pn<D0<p_1<\cdots<p_n<D; 1≤fi≤1091\le f_i\le10^9.
  • Subtask 3 (50 points): 1≤D≤1091\le D\le10^9; 0≤F≤1090\le F\le10^9; 0≤n≤5000\le n\le 500; 0<p1<⋯<pn<D0<p_1<\cdots<p_n<D; 1≤fi≤1091\le f_i\le10^9.

Examples

Input

100 10 4
10 60
20 30
30 30
60 40

Output

2

Explanation

The car can reach station 1010, take 6060 fuel, then reach station 6060 and take 4040 more to reach 100100. Two stops suffice; no single stop does.