Where do the accesses go

With several cache levels, only some of the accesses reach each level. Find out how many are served at every level and what the average access costs.

What to build

A program that prints the share of all accesses served by each level and the AMAT.

Requirements

  • Each cache level has a name, a hit time and a miss rate. The miss rate counts only the accesses that reach that level. Main memory always hits and takes T.
  • For each level print name served X%, the share of all accesses that are hits in that level, with 2 decimals. Then print memory served X% for the share that reaches main memory.
  • Finally print AMAT Y with the average access time to 2 decimals, computed as h1 + m1 × (h2 + m2 × (… + T)).

Input

A line with n, then n lines name hit miss, then a line with T.

Output

n + 2 lines.

Constraints

1 ≤ n ≤ 5; times are decimals from 0 to 10000; miss rates are decimals from 0 to 1.

Example

Input:

2
L1 1 0.1
L2 10 0.5
100

Output:

L1 served 90.00%
L2 served 5.00%
memory served 5.00%
AMAT 7.00

Why it works

A fraction miss of the accesses falls through each level, so the share reaching a level is the product of the miss rates above it. AMAT is the same nesting written from the bottom up.

Examples

Example 1 · Input
2
L1 1 0.1
L2 10 0.5
100
Expected output
L1 served 90.00%
L2 served 5.00%
memory served 5.00%
AMAT 7.00
Example 2 · Input
1
cache 2 0.25
80
Expected output
cache served 75.00%
memory served 25.00%
AMAT 22.00

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

Hints

Hint 1

Keep the fraction of accesses that reach each level: reach starts at 1; a level serves reach × (1 − miss); then reach becomes reach × miss.

Hint 2

Main memory serves whatever reach is left. For AMAT work from the last cache level up: cost = memory; for each level from the bottom, cost = hit + miss × cost.

Hint 3

Print percentages as f"{x * 100:.2f}%".

Solution

Show one correct solution (try it yourself first)
n = int(input())
levels = []
for _ in range(n):
    name, h, m = input().split()
    levels.append((name, float(h), float(m)))
t = float(input())
reach = 1.0
for name, h, m in levels:
    print(f"{name} served {reach * (1 - m) * 100:.2f}%")
    reach *= m
print(f"memory served {reach * 100:.2f}%")
cost = t
for name, h, m in reversed(levels):
    cost = h + m * cost
print(f"AMAT {cost:.2f}")

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

Lesson: The memory hierarchy →