F - Safety System Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 77

注意

この問題に対する言及は、2021/4/24 18:00 JST まで禁止されています。言及がなされた場合、賠償が請求される可能性があります。 試験後に総合得点や認定級を公表するのは構いませんが、どの問題が解けたかなどの情報は発信しないようにお願いします。

問題文

あなたは画期的なコンピュータを発明し、これを使って NN 個のタスクを処理しようとしています。
ii 番目のタスクはこのコンピュータで AiA_i 秒で処理されます。処理時間とは別に各タスクには「負荷」という数値が定まっており、ii 番目のタスクの負荷は BiB_i です。タスクは 11 個目から番号順に間隔を開けず処理します。
このコンピュータは負荷 LL 以上の処理が連続 TT 秒間に達した瞬間、安全装置によって XX 秒間だけ処理を停止した後、再開します。処理停止時にあるタスクの処理途中だった場合、そのタスクの初めに戻って再開します。
有限の時間で全てのタスクを処理し終わるかを判定し、処理し終わる場合は全部のタスクを終わらせるのにかかる秒数を求めてください。最後のタスクを終わらせた瞬間に安全装置が作動する場合、その安全装置による処理停止が終わる瞬間までの秒数を求めるものとします。

制約

  • 1N1001 \le N \le 100
  • 1L10001 \le L \le 1000
  • 1T10001 \le T \le 1000
  • 1X10001 \le X \le 1000
  • 1Ai10001 \le A_i \le 1000
  • 1Bi10001 \le B_i \le 1000
  • 入力は全て整数

入力

入力は以下の形式で標準入力から与えられる。

NN LL TT XX
A1A_1 B1B_1
A2A_2 B2B_2
A3A_3 B3B_3
\hspace{14pt} \vdots
ANA_N BNB_N

出力

有限の時間で全てのタスクを処理し終わらない場合 forever を出力せよ。
そうでない場合、全てのタスクを終わらせるのにかかる秒数を出力せよ。


入力例 1Copy

Copy
4 10 3 5
2 15
2 10
2 20
2 5

出力例 1Copy

Copy
20

1,21, 2 番目のタスクの負荷が両方 1010 以上なので、11 番目のタスクを完了し 22 番目のタスクを 11 秒実行した瞬間、負荷 LL 以上の処理が 33 秒に達しコンピュータは 55 秒停止します。
その後、22 番目のタスクの最初に戻って再開します。33 番目のタスクの負荷も 1010 以上なので同様に 33 番目のタスクを 11 秒実行した瞬間に再び安全装置が作動して 55 秒停止します。
同様に再開時は 33 番目のタスクの初めからになり、44 番目のタスクは負荷が 1010 未満なので、この後は 3,43, 4 番目のタスクに 22 秒ずつ使って全てのタスクを完了します。
11 回目の停止から再開した時点で開始から 88 秒、22 回目の停止からの再開時で 1616 秒経っており、最終的に 2020 秒で全てのタスクを処理し終わります。


入力例 2Copy

Copy
1 1 1 1
100 100

出力例 2Copy

Copy
forever

最初のタスクの途中で安全装置が作動して最初に戻る、を繰り返して永遠に終了しません。


入力例 3Copy

Copy
4 10 5 10
3 5
5 20
3 10
2 10

出力例 3Copy

Copy
33

22 番目のタスクが終了した瞬間に安全装置が 11 度作動します。停止したのはタスクの処理途中ではないので、再開時は 33 番目のタスクから処理を開始します。
そして、最後のタスクを処理し終わった瞬間に安全装置が再び作動します。
問題文の最後の文で注意されている通り、この場合安全装置による停止の時間も含めて計算します。


入力例 4Copy

Copy
3 10 5 10
3 10
3 9
3 10

出力例 4Copy

Copy
9

負荷 1010 以上の仕事が 55 秒以上連続することはないので、安全装置が作動することはありません。

Score : 77 points

Warning

Do not make any mention of this problem until April 24, 2021, 6:00 p.m. JST. In case of violation, compensation may be demanded. After the examination, you can reveal your total score and grade to others, but nothing more (for example, which problems you solved).

Problem Statement

You have invented a new epoch-making computer, which you will use to process NN tasks.
The computer takes AiA_i seconds to process the ii-th task. Additionally, each task has a value called the load; the load of the ii-th task is BiB_i. The computer will process the tasks in the given order without pause.
At the moment when the computer has processed tasks of load LL or higher for TT consecutive seconds, the safety system halts the processing for XX seconds, and then the computer restarts processing. Here, if the computer was in the middle of processing a task when interrupted, the computer has to process that task again from the beginning.
Determine whether the computer completes all tasks in finite time, and find that time needed if it is finite. If the safety system activates at the moment when the final task is finished, include that halt in the answer.

Constraints

  • 1N1001 \le N \le 100
  • 1L10001 \le L \le 1000
  • 1T10001 \le T \le 1000
  • 1X10001 \le X \le 1000
  • 1Ai10001 \le A_i \le 1000
  • 1Bi10001 \le B_i \le 1000
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN LL TT XX
A1A_1 B1B_1
A2A_2 B2B_2
A3A_3 B3B_3
\hspace{14pt} \vdots
ANA_N BNB_N

Output

If the computer does not complete all tasks in finite time, print forever.
Otherwise, print the number of seconds needed to complete all tasks.


Sample Input 1Copy

Copy
4 10 3 5
2 15
2 10
2 20
2 5

Sample Output 1Copy

Copy
20

Since the 11-st and 22-nd tasks both have loads 1010 or higher, at the moment when the computer has processed the 22-nd for 11 second after finishing the 11-st, it has processed tasks of load LL or higher for 33 seconds, so it halts for 55 seconds.
Then, the computer starts processing the 22-nd task again from the beginning. The 33-rd task also has a load not lower than 1010, so at the moment when the computer has processed it for 11 second, the safety system activates again and makes the computer halt for 55 seconds.
Then, the computer starts processing the 33-nd task again from the beginning. The 44-th task has a load lower than 1010, so now the computer takes 22 seconds for each of the 33-rd and 44-th tasks, and all tasks are done.
At the end of the 11-st halt, 88 seconds have passed; at the end of the 22-nd halt, 1616 seconds have passed; at the end of processing all tasks, 2020 seconds have passed.


Sample Input 2Copy

Copy
1 1 1 1
100 100

Sample Output 2Copy

Copy
forever

The safety system activates in the middle of processing the first task, making the computer start over, and it repeats forever.


Sample Input 3Copy

Copy
4 10 5 10
3 5
5 20
3 10
2 10

Sample Output 3Copy

Copy
33

At the moment when the 22-nd task is finished, the safety system activates once. Since the computer was not in the middle of processing a task when interrupted, it starts processing the 33-rd task after the halt.
Then, at the moment when the final task is finished, the safety system activates again.
As noted at the end of Problem Statement, we include this second halt in the answer.


Sample Input 4Copy

Copy
3 10 5 10
3 10
3 9
3 10

Sample Output 4Copy

Copy
9

There is no moment when tasks of loads 1010 or higher continue for 55 or more consecutive seconds, so the safety system never activates.



2025-04-05 (Sat)
05:31:21 +00:00