This page is still under construction.

Parts of this page are still being built. What you see may change.

Barking Dogs!

Interview

Time limit2sMemory limit512 MB

Summary
Given dogs with wake-up delays and a directed hearing graph, simulate who barks each second from 0 to T and count each dog's barks.
Level

Medium5 of 10

Topics
Simulation, Graph, Implementation, Queue
Solved
No attempts yet

Problem

You live in a neighbourhood full of dogs. Dogs like dogs, and they like barking even more — but best of all, dogs love to bark when other dogs bark.

Each dog has a set of dogs that can hear it bark. Each dog also has a delay: the time it waits before barking after it hears another dog bark.

Dog 11 always barks first, and this first bark happens during second 00.

Assume that sound travels instantly from one dog's mouth to another dog's ear. Your job is to determine how many times each dog barks during seconds 00 through TT inclusive.

In any given second, each dog is doing exactly one of three things: sleeping, waiting, or barking. If dog ii hears a bark during a second nn while it is sleeping, it wakes up, waits during seconds n+1n+1 through n+wi−1n+w_i-1 inclusive, barks during second n+win+w_i, and then goes back to sleep from second n+wi+1n+w_i+1 onward. If a dog hears a bark during a second in which it is waiting or barking, it ignores that bark.

During second 00, every dog except Dog 11 is sleeping.

Input

The first line contains DD (1≤D≤10001 \le D \le 1000), the number of dogs in the neighbourhood.

Each of the next DD lines contains an integer wiw_i (1≤wi≤10001 \le w_i \le 1000), the time in seconds that dog ii waits before barking after it hears a bark.

The next line contains FF (1≤F≤100001 \le F \le 10000). Each of the next FF lines contains two integers ii and jj, meaning that when dog ii barks, dog jj hears it. It is never the case that i=ji = j.

The last line contains the integer TT (1≤T≤10001 \le T \le 1000), the number of seconds during which you monitor the dogs.

Output

Print one line for each dog, in order from dog 11 to dog DD. On line ii, print the number of seconds nn with 0≤n≤T0 \le n \le T during which dog ii barked.

Examples2

  1. Example 1

    Input
    3
    1
    1
    3
    3
    1 2
    2 3
    3 1
    10
    
    Expected output
    3
    2
    2
    
  2. Example 2

    Input
    3
    3
    1
    3
    3
    1 2
    2 3
    3 1
    10
    
    Expected output
    2
    2
    1