#P9668. Cave Exploration with Torches

    ID: 22815 Type: Default 1000ms 256MiB

Cave Exploration with Torches

Cave Exploration with Torches

Two professors, Prof. Pang and Prof. Shou, explore a cave. Prof. Pang always walks in front while Prof. Shou follows strictly — he must remain at least 1 unit behind Prof. Pang at all times.

Each professor carries a torch that needs fuel. When refueled, Prof. Pang’s torch burns for \(a_1\) seconds and then takes \(b_1\) seconds to refuel. Similarly, Prof. Shou’s torch burns for \(a_2\) seconds and then takes \(b_2\) seconds to refuel. A professor can walk forward (1 unit per second) only while his torch is burning. Importantly, a professor cannot refuel his torch until its fuel has completely burned out. Also note: while refueling, a professor cannot walk.

Every second, the actions occur in the following order:

  • Prof. Pang moves first if his torch is burning; otherwise, he is refueling.
  • Then Prof. Shou moves if his torch is burning and his move will not cause him to catch up with or surpass Prof. Pang (i.e. his new position must be at least 1 unit behind Prof. Pang); otherwise, he does not move.

Initially (time 0), both professors have just refueled their torches. Prof. Pang starts at position 0 while Prof. Shou starts at position -1, so that the distance Prof. Shou has traveled from his starting point is initially 0.

You are given n queries. For the i-th query, determine the total distance (in units) that Prof. Shou has moved forward from his original position (position -1) by time \(q_i\). (Note that the distance moved equals the current position + 1.)

The burning and refueling process for each professor is cyclic. For example, if a professor’s torch burns for \(a\) seconds, he will move (if allowed) for those \(a\) consecutive seconds and then must spend \(b\) seconds refueling, during which he cannot move. After refueling, his torch is lit again for \(a\) seconds, and so on.

inputFormat

The first line contains five integers: (a_1), (b_1), (a_2), (b_2), and (n) representing the burning time and refueling time for Prof. Pang and Prof. Shou respectively, and the number of queries. The second line contains (n) non-negative integers (q_i), each representing a time in seconds.

outputFormat

Output (n) lines, where the (i)-th line contains a single integer representing the number of units Prof. Shou has moved forward from his starting point by time (q_i).

sample

1 1 2 1 4
0 1 2 4
0

1 1 2

</p>