## COCI '08 Contest 2 #4 Svada

View as PDF

Points: 10
Time limit: 0.6s
Memory limit: 32M

Problem type
Allowed languages
Ada, Assembly, Awk, Brain****, C, C#, C++, COBOL, CommonLisp, D, Dart, F#, Forth, Fortran, Go, Groovy, Haskell, Intercal, Java, JS, Kotlin, Lisp, Lua, Nim, ObjC, OCaml, Octave, Pascal, Perl, PHP, Pike, Prolog, Python, Racket, Ruby, Rust, Scala, Scheme, Sed, Swift, TCL, Text, Turing, VB, Zig

The local zoo has acquired a large open garden in which animals may freely move as in their natural habitats and entertain visitors with their usual shenanigans.

The most popular animals are monkeys. With their climbing and jumping and other skills, they delight old and young visitors alike.

One species of monkey has specialized in climbing tall trees and picking off coconuts. Another species has specialized in breaking them open.

There are monkeys of the first type (numbered through ) and monkeys of the second type (numbered through ).

Monkey of the first type takes seconds to find a good spot on the tree, after which it picks off its first coconut. After that the monkey produces a new coconut every seconds.

Monkey of the second type takes seconds to find a good tool for opening the coconuts, after which it opens its first coconut. After that the monkey opens another coconut every seconds.

Unfortunately, the second type of monkey is extremely aggressive so the two types may not be in the garden at the same time. Therefore, zoo keepers will chase away the first type of monkeys as soon as they have picked off all the coconuts. Similarly, if monkeys of the same type stay too long after opening all the coconuts, fights will ensue. Because of that, zoo keepers will send them away as soon as they have opened all the coconuts.

The zoo keepers first arrive immediately after all coconuts have been picked, and again immediately after the monkeys open them all. The time needed for monkeys to enter or leave the garden is also negligibly small.

Tomislav especially likes the second type of monkey, but can never guess when to arrive in order to see them. Help him calculate the time when the second type arrives if he knows the total time that monkeys spent in the garden, but does not know the number of coconuts in the garden.

#### Input Specification

The first line contains the integer , the total time that monkeys spent in the garden, in seconds.

The next line contains the integer , the number of monkeys of the first type. Each of the following lines contains two integers and , how fast monkey of the first type is.

The next line contains the integer , the number of monkeys of the second type.

Each of the following lines contains two integers and , how fast monkey of the second type is.

#### Output Specification

Output the number of seconds between the arrival of the first type of monkeys and the arrival of the second type.

#### Sample Input 1

12
1
3 1
1
5 1

#### Sample Output 1

5

#### Sample Input 2

20
2
3 2
1 3
3
3 1
4 1
5 1

#### Sample Output 2

13

In the first example, it turns out there are three coconuts in the garden:

• The monkey of the first type picks off the first coconut seconds after the garden was opened.
• The monkey picks off the second coconut seconds after the garden is opened.
• The monkey picks off the third coconut seconds after the garden is opened.
• Zoo keepers come in and escort the monkey out. The monkey of the second type arrives. The output is because this is when Tomislav wants to arrive.
• The monkey of the second type opens the first coconut seconds after the garden was opened.
• The monkey opens the second coconut seconds after the garden was opened.
• The monkey opens the third coconut seconds after the garden was opened.
• Zoo keepers come in and escort the monkey out.

## Comments

There are no comments at the moment.