# 1033 To Fill or Not to Fill （25 分）

With highways available, driving a car from Hangzhou to any other city is easy. But since the tank capacity（油罐容量） of a car is limited, we have to find gas stations on the way from time to time. Different gas station may give different price. You are asked to carefully design the cheapest route to go.

### Input Specification:

Each input file contains one test case. For each case, the first line contains 4 positive numbers: C​max​​ (≤ 100), the maximum capacity（容纳） of the tank; D (≤30000), the distance between Hangzhou and the destination city; D​avg​​ (≤20), the average distance per unit gas that the car can run; and N (≤ 500), the total number of gas stations. Then N lines follow, each contains a pair of non-negative numbers: P​i​​, the unit gas price, and D​i​​ (≤D), the distance between this station and Hangzhou, for i=1,⋯,N. All the numbers in a line are separated by a space.

### Output Specification:

For each test case, print the cheapest price in a line, accurate up to 2 decimal（小数） places. It is assumed that the tank is empty at the beginning. If it is impossible to reach the destination, print The maximum travel distance = X where X is the maximum possible distance the car can run, accurate up to 2 decimal places.

### Sample Input 1:

50 1300 12 8
6.00 1250
7.00 600
7.00 150
7.10 0
7.20 200
7.50 400
7.30 1000
6.85 300


### Sample Output 1:

749.17


### Sample Input 2:

50 1300 12 2
7.10 0
7.00 600


### Sample Output 2:

The maximum travel distance = 1200.00

#include<cstdio>
#include<algorithm>
#include<climits>

using namespace std;

struct Station{
double price;
double distance;
}sta[510];

bool cmp(Station a, Station b){
return a.distance < b.distance;
}

int main(){
double gasMax, goal, run;
int n;
scanf("%lf%lf%lf%d", &gasMax, &goal, &run, &n);
for (int i = 0; i < n; ++i) {
scanf("%lf%lf", &sta[i].price, &sta[i].distance);
}
sta[n].distance = goal;//最后放终点
sta[n].price = 0;
sort(sta, sta + n, cmp);

if(sta[0].distance){
printf("The maximum travel distance = 0.00\n");
}else{
double ans = 0, nowTake = 0, maxDistance = gasMax * run;
int now = 0;       // 当前所处加油站编号
while(now < n){        //每次循环选出下一个需要到的加油站
int k = -1;          //最低油价的加油站编号
double priceMin = LONG_MAX; // 最低油价
for (int i = now + 1; i <= n && sta[i].distance - sta[now].distance <= maxDistance; ++i) {
if(sta[i].price < priceMin){//如果没有油价低于当前油价的，选择最低的
priceMin = sta[i].price;
k = i;
if(sta[i].price < sta[now].price){//找到第一个比当前油价低的油站，直接中断循环
break;
}
}
}
if(k == -1) break;//如果不能满油到达下一个加油站，退出循环
//下面会可能到达的加油站k，并计算转移费
//need为now到k需要的油量
double need = (sta[k].distance - sta[now].distance) / run;
if(priceMin < sta[now].price){
if(nowTake < need){                          //当前油量不够达到下一个加油站
ans += (need - nowTake) * sta[now].price;//补至need油量
nowTake = 0;                            //到达加油站后油量为0
}else{
nowTake -= need;
}
}else{
ans += (gasMax - nowTake) * sta[now].price;     //直接把油加满
nowTake = gasMax - need;                                //到达加油站k后剩余的油量
}
now = k;//进入下一层循环
}
if(now == n){
printf("%.2f\n", ans);
}else{
printf("The maximum travel distance = %.2f", sta[now].distance + maxDistance);
}
}

return 0;
}