P28474 [洛谷] Ouwen Ou Learns to Drive Again
普及
题目描述

Problem Description

Ouwen Ou practices driving with a rubber tree car. This car has $N$ gears, and Ouwen Ou can increase or decrease the gear by $1$ per second. Initially (at time $0$), the gear is set to $1$.

The speed range of this car is $[L, R]$, with the initial speed being $L$. Each time the gear is increased, the speed resets to $L$; when the gear is decreased, the speed becomes $R$. Ouwen Ou can also press the accelerator each second, increasing the speed by $X$ and then taking the minimum with $R$. If the speed remains at $R$ for $K$ consecutive seconds, the engine will stop working at the moment those $K$ seconds end (even if a gear-down operation occurs during these $K$ seconds, it still counts as this scenario).

These operations are considered to occur instantaneously at the beginning of each second, with the gear shift operation taking place before the accelerator operation. The distance traveled by the car in this second is the product of the speed and the gear.

Given the sequence of operations during Ouwen Ou's practice, you need to calculate the total distance traveled.

Input Format

The first line contains six integers $T, N, L, R, X, K$, where $T$ represents the total time.

The next $T$ lines each contain two integers $x, y$, representing the operation for that second.

Here, $x=0$ means increasing the gear, $x=1$ means decreasing the gear, and $x=2$ means keeping the gear unchanged; $y=0$ means not pressing the accelerator, and $y=1$ means pressing the accelerator.

Output Format

Output a single integer, which is the total distance traveled based on the given sequence of operations.

If Ouwen Ou increases the gear when it is already at $N$, or decreases the gear when it is at $1$, the sequence is invalid, and output $-1$.

Sample Input and Output

Sample Input #1

5 3 1 10 5 100
0 1
0 0
2 1
2 1
1 1

Sample Output #1

83

Sample Input #2

3 1 1 1 1 2
2 0
2 1
2 0

Sample Output #2

2

Sample Input #3

1 2 3 4 5 6
1 0

Sample Output #3

-1

Notes

For the first sample:

  • In the first second, the gear is $2$, and the speed is $6$;
  • In the second second, the gear is $3$, and the speed is $1$;
  • In the third second, the gear is $3$, and the speed is $6$;
  • In the fourth second, the gear is $3$, and the speed is $10$;
  • In the fifth second, the gear is $2$, and the speed is $10$.

For the second sample, the engine stops working after two seconds.

  • For $30\%$ of the data, there are no gear operations (i.e., $x=2$ is guaranteed);
  • For another $30\%$ of the data, there are no accelerator operations (i.e., $y=0$ is guaranteed);
  • For all data, it is guaranteed that $1 \le T, N, L, R, X, K \le 10^6, L \le R$.
输入格式

见题面 Input 部分

输出格式

见题面 Output 部分

样例输入
5 3 1 10 5 100 0 1 0 0 2 1 2 1 1 1
样例输出
83