Waiting for Good Data
Marcus is a LLM power user, in particular he has become quite adept with setting up and managing agentic workflows to replace his lazy teammates. Marcus sets up intricate chains of AI processes, where each process takes time to produce some data, and then spawns potentially multiple subprocesses to perform even more work.
Marcus has designed such a workflow to make promotional posters for an upcoming event. He has setup each process and knows how long each one is going to relate, and also which processes are children of others. Marcus's computer can run as many processes in parallel as it likes, but it cannot start running a process until it's parent process has completed. While his agents work, Marcus is going to go for a megawalk outside, but he wants to know how long he should go out for.
Can you calculate how long it will be until Marcus's entire agentic workflow is complete?
Input
The first input contains a single integer (
), the number of processes which comprise Marcus's agentic workflow. Each process is labelled
through
.
The next lines each contain information about one of these processes. The
th line contains two integers
(
,
), the parent process label and the duration of the
th process. Note that
is possible and indicates that a process has no parent. Due to perfect parallelism, this means that the process may be started instantly. Each other process may only be started once its parent is complete.
Output
Output a single integer , the total amount of time required for all processes to complete.
Example
Input 1
6
1 3
1 1
2 2
1 5
5 1
5 2
Output 1
8
The processes form a structure that can be seen in this image:

After seconds, process
will be complete. Then,
seconds after, process
will be complete. At this stage, all other processes will already have been completed if we start them instantly after their parent completes (and start all parentless processes immediately at the start). Thus
seconds is needed.
Input 2
5
1 2
1 1
2 3
3 2
4 1
Output 2
9
In this case, all processes depend on the previous process, so we cannot parallelise everything. We must wait for each process to complete in order, which takes seconds.
Comments