#P5269. 欧稳欧再次学车

欧稳欧再次学车

Description

When Owen-O practices driving, he often uses an “oak car” for training. This oak car has a total of NN gears. Each second, Owen-O can increase or decrease the gear by 11. Initially (at time 00), the gear is 11.

The car’s RPM range is [L,R][L,R], and initially the RPM is LL. Each time he upshifts, the RPM becomes LL; each time he downshifts, the RPM becomes RR. Each second, Owen-O can also step on the accelerator to increase the RPM by XX, then take min\text{min} with RR. If the RPM stays continuously at =R=R for KK seconds, then the engine will stop working and the car will stop at the instant those KK seconds end (even if downshifts happen during those KK seconds, it is still counted as this situation).

We consider all operations to happen instantly at the beginning of each second, and the gear-change operation happens before the acceleration operation. The distance the car travels during that second is RPM ×\times gear.

Now you are given Owen-O’s sequence of operations during practice. You need to compute the total distance traveled.

Input Format

The first line contains six integers T,N,L,R,X,KT,N,L,R,X,K, where TT is the total time.

The next TT lines each contain two integers x,yx,y, describing the operation in this second.

Here, x=0x=0 means upshift, x=1x=1 means downshift, x=2x=2 means the gear stays the same; y=0y=0 means do not step on the accelerator, y=1y=1 means step on the accelerator. (Do not ask why there is no brake.)

Output Format

Output one integer in one line, the total distance traveled under the given operation sequence.

If Owen-O upshifts while the gear is NN, or downshifts while the gear is 11, then the given sequence is invalid; output 1-1.

5 3 1 10 5 100
0 1
0 0
2 1
2 1
1 1
83
3 1 1 1 1 2
2 0
2 1
2 0
2
1 2 3 4 5 6
1 0
-1

Hint

For Sample 1:

In the first second, the gear is 22 and the RPM is 66;
in the second second, the gear is 33 and the RPM is 11;
in the third second, the gear is 33 and the RPM is 66;
in the fourth second, the gear is 33 and the RPM is 1010;
in the fifth second, the gear is 22 and the RPM is 1010.

For Sample 2, the engine stops working after moving for two seconds.

For 30%30\% of the testdata, there are no gear operations (i.e. guaranteed x=2x=2);

for another 30%30\% of the testdata, there are no acceleration operations (i.e. guaranteed y=0y=0);

for all testdata, it is guaranteed that 1T,N,L,R,X,K1061\le T,N,L,R,X,K\le 10^6 and LRL\le R.

Translated by ChatGPT 5