In this problem, you need to simulate the execution of
n service programs
P1,
P2, ...,
Pn. Each program is described with sequence of integers:
T I in1 in2 ... inI O out1 out2 ... outO, that means it takes
T unit time to execute, needs
I input variables (i.e.
in1 in2 ... inI), and sets
O output variables (i.e.
out1 out2 ... outO) when it finishes running. A program can be started if and only if all these
T input variables are ready (initially available, or set by some other programs).
Imagine you have a super-computer which can execute as many programs in parallel as you like, and every variable can be read and written simultaneously by multiple programs. Your task is to calculate a particular "target" variable, as soon as possible.
Assume there are 4 programs, shown in the table below:
The quickest time to get
X5 is 7, if only
X1 is available at startup.
You also need to construct an expression that shows
how to execute the programs to achieve the minimal time. The grammar of the expression is recursive:
- Single Program: Px, where 1 <= x <= n. (i.e. P2, P499, etc). Meaning: execute the program immediately. Then end of this program marks the end of this expression.
- Execute in serial: (S1S2...Sk), where every Si is an expression. Note that the outermost pair of parentheses is mandatory. Meaning: execute expression S1, then S2 immediately after S1 ends, then S3 immediately after S2 ends, ..., and finally Sk immediately after Sk-1 ends. Then end of expression Sk marks the end of the whole expression.
- Execute in parallel: (S1|S2|...|Sk), where every Si is an expression. Note that the outermost pair of parentheses is mandatory. Meaning: execute expressions S1, S2, ..., and Sk simultaneously. The end of last finished expression marks the end of the whole expression.
One of the possible expressions for the example above is (((P1P3)|P2)P4). (P1P2P3P4)is not acceptable, since
X5 is available at time 10 in that expression, later than the optimal time 7.