codeforce 534b covered path (贪心)no.4

Covered Path

传送
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 v2 meters 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](注意是瞬间改变速度,牛顿的棺材板要压不住了)问最远距离

做法:

先一个劲的加速 加速的时候要注意能不能回到v2

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

#include<cstdio>
using namespace std;
int v1,v2,t,d,ans;
int (){
int res=0,w=1;char ch=getchar();
while(!isdigit(ch)&&ch!='-') ch=getchar();
if(ch=='-') w=-1,ch=getchar();
while(isdigit(ch)) res=res*10+ch-'0',ch=getchar();
return res*w;
}
int main(){
v1=read();
v2=read();
t=read();
d=read();
for(int i=0;i<=t-1;i++){
ans+=min(v1+i*d,v2+(t-i-1)*d);
}
printf("%dn",ans);
return 0;
}