0%

Codeforces 1341E - Nastya and Unexpected Guest

原文

If the girl doesn’t go to Denis, then Denis will go to the girl. Using this rule, the young man left home, bought flowers and went to Nastya.

On the way from Denis’s house to the girl’s house is a road of nn lines. This road can’t be always crossed in one green light. Foreseeing this, the good mayor decided to place safety islands in some parts of the road. Each safety island is located after a line, as well as at the beginning and at the end of the road. Pedestrians can relax on them, gain strength and wait for a green light.

Denis came to the edge of the road exactly at the moment when the green light turned on. The boy knows that the traffic light first lights up gg seconds green, and then rr seconds red, then again gg seconds green and so on.

Formally, the road can be represented as a segment [0,n][0,n]. Initially, Denis is at point 00. His task is to get to point nn in the shortest possible time.

He knows many different integers d1,d2,,dmd_1,d_2,\ldots,d_m, where 0din0\le d_i\le n — are the coordinates of points, in which the safety islands are located. Only at one of these points, the boy can be at a time when the red light is on.

Unfortunately, Denis isn’t always able to control himself because of the excitement, so some restrictions are imposed:

  • He must always move while the green light is on because it’s difficult to stand when so beautiful girl is waiting for you. Denis can change his position by ±1\pm 1 in 11 second. While doing so, he must always stay inside the segment [0,n][0,n]
  • He can change his direction only on the safety islands (because it is safe). This means that if in the previous second the boy changed his position by +1+ 1 and he walked on a safety island, then he can change his position by ±1\pm 1. Otherwise, he can change his position only by +1+1. Similarly, if in the previous second he changed his position by 1-1, on a safety island he can change position by ±1\pm 1, and at any other point by 1- 1.
  • At the moment when the red light is on, the boy must be on one of the safety islands. He can continue moving in any direction when the green light is on.

Denis has crossed the road as soon as his coordinate becomes equal to nn.

This task was not so simple, because it’s possible that it is impossible to cross the road. Since Denis has all thoughts about his love, he couldn’t solve this problem and asked us to help him. Find the minimal possible time for which he can cross the road according to these rules, or find that it is impossible to do.

Input

The first line contains two integers nn and mm, (1n106,2mmin(n+1,104))(1\le n\le 10^6,2\le m\le \min(n+1,104)) — road width and the number of safety islands.

The second line contains mm distinct integers d1,d2,,dm (0din)d_1,d_2,\ldots,d_m\ (0\le d_i\le n) — the points where the safety islands are located. It is guaranteed that there are 00 and nn among them.

The third line contains two integers g,r (1g,r1000)g,r\ (1\le g,r\le 1000) — the time that the green light stays on and the time that the red light stays on.

Output

Output a single integer — the minimum time for which Denis can cross the road with obeying all the rules.

If it is impossible to cross the road output −1.

Examples

input

1
2
3
15 5
0 3 7 14 15
11 11

output

1
45

input

1
2
3
13 4
0 3 7 13
9 9

output

1
-1

Note

In the first test, the optimal route is:

  • for the first green light, go to 7 and return to 3. In this case, we will change the direction of movement at the point 7, which is allowed, since there is a safety island at this point. In the end, we will be at the point of 3, where there is also a safety island. The next 11 seconds we have to wait for the red light.
  • for the second green light reaches 14. Wait for the red light again.
  • for 11 second go to 15. As a result, Denis is at the end of the road.

In total, 45 seconds are obtained.

In the second test, it is impossible to cross the road according to all the rules.

大致意思

当绿灯亮起的时候可以不受限制的移动,但是绿灯结束的时候必须要停留在安全岛。只有在安全岛上可以进行方向的转换,问到达终点(第m个节点)所需要的最短时间是多少,如果不存在则输出-1。

思路

01-BFS,在同一个绿灯时间内的移动看做图上的边权重为0,利用push_front()否则的话认为边权重为1,利用push_back()。对于同一节点,距离绿灯开始时间相同的时刻,认为是图上相同节点。

总的时间复杂度为O(gm)O(g*m)

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <algorithm>
#include <cmath>
#include <map>
#include <queue>
#include <vector>
#include <stack>
#include <deque>
#include <set>
#include <cstring>
#include <cstdlib>
#include <cstdio>
using namespace std;

#define ll long long

int n, m;
int dist[10010];
int g, r;
int dp[10010][1010];

int main(){
ios::sync_with_stdio(0);
cin>>n>>m;
for(int i=0;i<m;++i)
cin>>dist[i];
cin>>g>>r;
sort(dist, dist+m);

deque<pair<int,int> > q;
q.push_back(make_pair(0,0));
dp[0][0] = 1;
int ans = 0x7fffffff;
while(!q.empty()){
pair<int,int> temp = q.front();
int idx = temp.first;
int val = temp.second;
q.pop_front();
for(int i=-1;i<=1;i+=2){
int new_idx = idx+i;
if(new_idx<0 || new_idx>=m)
continue;
else if(new_idx == m-1){
int d = abs(dist[idx]-dist[new_idx]);
if(val + d <= g){
ans = min(ans, (dp[idx][val]-1)*(g+r)+val+d);
}
}
else{
int d = abs(dist[idx]-dist[new_idx]);
if(val + d == g && dp[new_idx][0] == 0){
dp[new_idx][0] = dp[idx][val] + 1;
q.push_back(make_pair(new_idx, 0));
}
else if(val + d < g && dp[new_idx][val+d] == 0){
dp[new_idx][val+d] = dp[idx][val];
q.push_front(make_pair(new_idx, val+d));
}
}
}
}
cout<<((ans == 0x7fffffff)? -1: ans)<<endl;
}