#BS0000300. Hai nhóm khỉ và những quả dừa (Svada)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Hai nhóm khỉ và những quả dừa (Svada)
Svada
Source: Kattis
Version: Phuoc Hung OJ Extended
Problem Statement
An unknown positive number of coconuts is present. In the first group, monkey i picks its first coconut seconds after entry and then one every seconds. As soon as all coconuts are picked, the first group leaves and the second enters. In the second group, monkey j opens its first coconut after seconds and then one every seconds. The second group leaves immediately when all coconuts are opened. Several monkeys may finish at the same second, but stop when no coconuts remain. The total duration of both groups is consistent with some actual coconut count. Find the second group entry time measured from the first group entry.
Input
Line 1: . Line 2: . Next lines: . Next line: . Last lines: .
Output
Print the elapsed seconds until the second group enters.
Subtasks
- Subtask 1 (20 points): ; ; .
- Subtask 2 (30 points): ; ; .
- Subtask 3 (50 points): ; ; .
Examples
Input 1
20
2
3 2
1 3
3
3 1
4 1
5 1
Output 1
13
Explanation
Group 1 can pick 9 by second 12 and 11 by second 13. Group 2 can open 9 by second 6 and 12 by second 7. Either 10 or 11 coconuts gives transition time 13.