746C.md (2942B)
1 Source: https://codeforces.com/problemset/problem/746/C 2 3 C. Tram 4 5 time limit per test 6 7 1 second 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 The tram in Berland goes along a straight line from the point 0 to the point *s* and back, passing 1 meter per *t*1 seconds in both directions. It means that the tram is always in the state of uniform rectilinear motion, instantly turning around at points *x* = 0 and *x* = *s*. 22 23 Igor is at the point *x*1. He should reach the point *x*2. Igor passes 1 meter per *t*2 seconds. 24 25 Your task is to determine the minimum time Igor needs to get from the point *x*1 to the point *x*2, if it is known where the tram is and in what direction it goes at the moment Igor comes to the point *x*1. 26 27 Igor can enter the tram unlimited number of times at any moment when his and the tram's positions coincide. It is not obligatory that points in which Igor enter and exit the tram are integers. Assume that any boarding and unboarding happens instantly. Igor can move arbitrary along the line (but not faster than 1 meter per *t*2 seconds). He can also stand at some point for some time. 28 29 Input 30 31 The first line contains three integers *s*, *x*1 and *x*2 (2 ≤ *s* ≤ 1000, 0 ≤ *x*1, *x*2 ≤ *s*, *x*1 ≠ *x*2) — the maximum coordinate of the point to which the tram goes, the point Igor is at, and the point he should come to. 32 33 The second line contains two integers *t*1 and *t*2 (1 ≤ *t*1, *t*2 ≤ 1000) — the time in seconds in which the tram passes 1 meter and the time in seconds in which Igor passes 1 meter. 34 35 The third line contains two integers *p* and *d* (1 ≤ *p* ≤ *s* - 1, *d* is either 1 or ) — the position of the tram in the moment Igor came to the point *x*1 and the direction of the tram at this moment. If , the tram goes in the direction from the point *s* to the point 0. If *d* = 1, the tram goes in the direction from the point 0 to the point *s*. 36 37 Output 38 39 Print the minimum time in seconds which Igor needs to get from the point *x*1 to the point *x*2. 40 41 Examples 42 43 Input 44 45 ``` 46 4 2 4 47 3 4 48 1 1 49 ``` 50 51 Output 52 53 ``` 54 8 55 ``` 56 57 Input 58 59 ``` 60 5 4 0 61 1 2 62 3 1 63 ``` 64 65 Output 66 67 ``` 68 7 69 ``` 70 71 Note 72 73 In the first example it is profitable for Igor to go by foot and not to wait the tram. Thus, he has to pass 2 meters and it takes 8 seconds in total, because he passes 1 meter per 4 seconds. 74 75 In the second example Igor can, for example, go towards the point *x*2 and get to the point 1 in 6 seconds (because he has to pass 3 meters, but he passes 1 meters per 2 seconds). At that moment the tram will be at the point 1, so Igor can enter the tram and pass 1 meter in 1 second. Thus, Igor will reach the point *x*2 in 7 seconds in total.