1. 程式人生 > >[VJ][暴力列舉]Covered Path

[VJ][暴力列舉]Covered Path

Covered Path

Description

The on-board computer on Polycarp's car measured that the car speed at the beginning of some section of the path equals v1 meters per second, and in the end it is v2meters per second. We know that this section of the route took exactly t seconds to pass.

Assuming that at each of the seconds the speed is constant, and between seconds the speed can change at most by d meters per second in absolute value (i.e., the difference in the speed of any two adjacent seconds does not exceed d in absolute value), find the maximum possible length of the path section in meters.

Input

The first line contains two integers v1 and v2 (1 ≤ v1, v2 ≤ 100) — the speeds in meters per second at the beginning of the segment and at the end of the segment, respectively.

The second line contains two integers t (2 ≤ t ≤ 100) — the time when the car moves along the segment in seconds, d

 (0 ≤ d ≤ 10) — the maximum value of the speed change between adjacent seconds.

It is guaranteed that there is a way to complete the segment so that:

  • the speed in the first second equals v1,
  • the speed in the last second equals v2,
  • the absolute value of difference of speeds between any two adjacent seconds doesn't exceed d.

Output

Print the maximum possible length of the path segment in meters.

Examples

Input

5 6
4 2

Output

26

Input

10 10
10 0

Output

100

Note

In the first sample the sequence of speeds of Polycarpus' car can look as follows: 5, 7, 8, 6. Thus, the total path is 5 + 7 + 8 + 6 = 26 meters.

In the second sample, as d = 0, the car covers the whole segment at constant speed v = 10. In t = 10 seconds it covers the distance of 100 meters.

 

描述:

一個小車起始速度為v1,末速度為v2,給出時間t,和最大加速度(-d--d)

求小車在 t 時間內的最大路程。

正確解法:

時間從 t==2 開始列舉,從d— -d開始列舉加速度。

當滿足 現在的速度v 加上加速度 再減去剩餘時間*d <=v2 時,此時的加速度就是目前所能加的最大加速度。

萬一小車加速,要保證它在剩餘時間內可以減速到 v2

(第一次見到列舉加速度,要好好記下來)

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<algorithm>
 5 #include<string>
 6 #include<cstring>
 7 using namespace std;    
 8 int b[1000010] = {0};
 9 int main()
10 {
11     int v1, v2,t,d,v;
12     cin >> v1 >> v2 >> t >> d;
13     long long ans = v1;
14     v = v1;    t=t-1;
15     while (t) {
16         for (int i = d; i >= -d; i--)
17             if (v + i - (t - 1)*d <= v2)
18             {
19                 ans += v + i;
20                 t--;
21                 v = v + i;
22                 break;
23             }
24     }
25     cout << ans << endl;
26     return 0;
27 }
View Code