Stop-and-wait with losses

Stop-and-wait resends a packet when no ACK arrives before the timeout. Losses make the transfer slower.

What to build

A program that computes how long stop-and-wait needs to deliver all packets when some are lost on the way.

Requirements

  • A try that succeeds takes tx + rtt ms (send, then wait for the ACK).
  • A try that is lost takes tx + timeout ms (send, then wait until the timer ends), and the packet is sent again.
  • The second line gives, for each packet, how many times in a row it is lost before it gets through. Print the total time in ms.

Input

Line 1: n (the number of packets), tx, rtt and timeout, all in ms. Line 2: n whole numbers, the number of times each packet is lost.

Output

One line: total T ms, with T written as a whole number.

Constraints

1 ≤ n ≤ 100; all times are whole numbers from 1 to 1000; each loss count is from 0 to 5.

Example

Input:

3 10 50 200
0 1 0

Output:

total 390 ms

Why it works

Every packet costs one successful try, and every loss adds a failed try that ends only when the timer runs out. Adding them up for all packets gives the total, because stop-and-wait never overlaps two packets.

Examples

Example 1 · Input
3 10 50 200
0 1 0
Expected output
total 390 ms
Example 2 · Input
1 5 20 100
0
Expected output
total 25 ms

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

Hints

Hint 1

One try of a packet that gets through takes tx + rtt: send it, then wait for the ACK.

Hint 2

A try that is lost takes tx + timeout: the sender sends, then waits for the timer.

Hint 3

For a packet lost k times, the total is k * (tx + timeout) + tx + rtt. Add this up for all packets.

Solution

Show one correct solution (try it yourself first)
n, tx, rtt, timeout = map(int, input().split())
losses = list(map(int, input().split()))
total = 0
for k in losses:
    total += k * (tx + timeout) + tx + rtt
print("total", total, "ms")

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

Lesson: Reliable delivery: Go-Back-N and Selective Repeat →