PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#GD0000116. Bạn bè và chi phí di chuyển (Friends and Travel costs)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Bạn bè và chi phí di chuyển (Friends and Travel costs)
Friends and Travel costs
Source: AtCoder
Version: Phuoc Hung OJ Extended
Problem Statement
Taro starts at village with coins. Moving from village to village costs exactly one coin. Friend lives in village and gives him coins upon arrival. Multiple friends may share a village. Determine the farthest village he can reach.
Input
The first line contains . Each of the following lines contains .
Output
Print one integer: the farthest reachable village.
Subtasks
- Subtask 1 (20 points): ; ; ; for all .
- Subtask 2 (30 points): ; ; ; for all .
- Subtask 3 (50 points): ; ; ; for all .
Examples
Input
2 3
2 1
5 10
Output
4
Explanation
He spends two coins reaching village , has one coin remaining, receives one additional coin, and then reaches village . He cannot reach village , so the answer is .