D. Road to Post Office
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Vasiliy has a car and he wants to get from home to the post office. The distance which he needs to pass equals to d kilometers.

Vasiliy's car is not new — it breaks after driven every k kilometers and Vasiliy needs t seconds to repair it. After repairing his car Vasiliy can drive again (but after k kilometers it will break again, and so on). In the beginning of the trip the car is just from repair station.

To drive one kilometer on car Vasiliy spends a seconds, to walk one kilometer on foot he needs b seconds (a < b).

Your task is to find minimal time after which Vasiliy will be able to reach the post office. Consider that in every moment of time Vasiliy can left his car and start to go on foot.

Input

The first line contains 5 positive integers d, k, a, b, t (1 ≤ d ≤ 1012; 1 ≤ k, a, b, t ≤ 106; a < b), where:

  • d — the distance from home to the post office;
  • k — the distance, which car is able to drive before breaking;
  • a — the time, which Vasiliy spends to drive 1 kilometer on his car;
  • b — the time, which Vasiliy spends to walk 1 kilometer on foot;
  • t — the time, which Vasiliy spends to repair his car.
Output

Print the minimal time after which Vasiliy will be able to reach the post office.

Examples
input
5 2 1 4 10
output
14
input
5 2 1 4 5
output
13
Note

In the first example Vasiliy needs to drive the first 2 kilometers on the car (in 2 seconds) and then to walk on foot 3 kilometers (in 12 seconds). So the answer equals to 14 seconds.

In the second example Vasiliy needs to drive the first 2 kilometers on the car (in 2 seconds), then repair his car (in 5 seconds) and drive 2 kilometers more on the car (in 2 seconds). After that he needs to walk on foot 1 kilometer (in 4 seconds). So the answer equals to 13 seconds.

题意:d,k,a,b,t

  • d — the distance from home to the post office; 从家到邮局的距离
  • k — the distance, which car is able to drive before breaking;车最多能开k
  • a — the time, which Vasiliy spends to drive 1 kilometer on his car;车行驶一公里需要的时间
  • b — the time, which Vasiliy spends to walk 1 kilometer on foot;步行一公里需要的时间
  • t — the time, which Vasiliy spends to repair his car.修车的速度

 求从家到达邮局的最小时间。

题解:题目保证a<b 第一段肯定是开车的  特判处理

之后的路程以k为分段 比较 修车-开车所花的时间( t+a*k)与这一段步行所花的时间(b*k) 取min

最后不足k的一段x 所花的时间为 min(t+a*x,b*x); 求和ans输出.

 1 /******************************
 2 code by drizzle
 3 blog: www.cnblogs.com/hsd-/
 4 ^ ^    ^ ^
 5  O      O
 6 ******************************/
 7 //#include<bits/stdc++.h>
 8 #include<iostream>
 9 #include<cstring>
10 #include<cstdio>
11 #include<map>
12 #include<algorithm>
13 #include<queue>
14 #define ll __int64
15 using namespace std;
16 ll d,k,a,b,t;
17 int main()
18 {
19     scanf("%I64d %I64d %I64d %I64d %I64d",&d,&k,&a,&b,&t);
20     if(d<=k)
21     {
22         printf("%I64d\n",d*a);
23         return 0;
24     }
25     ll ans=k*a;
26     d-=k;
27     ll exm=d/k;
28     ll plu=d%k;
29     ans=ans+exm*min(t+a*k,b*k);
30     ans=ans+min(t+a*plu,b*plu);
31     printf("%I64d\n",ans);
32     return 0;
33 }