Store-and-forward across many links

Routers receive a whole packet before they forward it (store and forward). Several packets sent back to back form a pipeline along the path.

What to build

A program that works out how long one packet, and a stream of packets, take to cross a path.

Requirements

  • Print the time for the first packet to arrive: for every link, its transmission time plus its propagation delay, added up.
  • Print the time until the last of the m packets arrives: the first packet's time plus (m − 1) times the largest transmission time on the path.
  • Print both in milliseconds with 2 decimal places.

Input

Line 1: the packet size in bytes L and the number of packets m. Line 2: the number of links n. Then n lines, each with a rate in Mbps and a propagation delay in ms.

Output

Two lines: first X ms and all Y ms.

Constraints

1 ≤ L ≤ 10000; 1 ≤ m ≤ 1000; 1 ≤ n ≤ 10.

Example

Input:

1500 1
2
10 5
10 5

Output:

first 12.40 ms
all 12.40 ms

Why it works

A router cannot forward a packet before it has all of it, so every link adds its transmission and propagation time to the first packet. Once the pipeline is full, packets leave the slowest link one transmission time apart, so each extra packet adds the largest transmission time.

Examples

Example 1 · Input
1500 1
2
10 5
10 5
Expected output
first 12.40 ms
all 12.40 ms
Example 2 · Input
1500 10
2
10 5
10 5
Expected output
first 12.40 ms
all 23.20 ms

Prompts inside input() are ignored when checking, so input("n: ") is fine.

Hints

Hint 1

On link i, one packet needs t_i = bits ÷ (rate × 1000) ms to transmit (rate in Mbps), plus its propagation delay.

Hint 2

The first packet pays sum of (t_i + p_i). Each following packet leaves the slowest link one t_max later.

Hint 3

All packets arrived when the last one has: first_packet_time + (m - 1) × max(t_i).

Solution

Show one correct solution (try it yourself first)
l, m = map(int, input().split())
n = int(input())
first = 0.0
longest = 0.0
for _ in range(n):
    rate, prop = map(float, input().split())
    t = l * 8 / (rate * 1000)
    first += t + prop
    longest = max(longest, t)
print(f"first {first:.2f} ms")
print(f"all {first + (m - 1) * longest:.2f} ms")

▶ Open this solution in the compiler, then press Visualize to watch it run step by step

Lesson: Delay and throughput →