It's that time of year again where programmers solve copious numbers of competitive programming problems! Of course, Alice's favorite category of programming problems is Dynamic Programming, and she wants to solve as many Dynamic Programming problems as she can in a given amount of time.
Alice is in a contest where she has minutes to solve
problems (all Dynamic Programming problems) numbered from
to
. Using her magical future-predicting skills, she has predicted a number
for each problem which is how many seconds the problem will take to solve. Of course, being a very skilled programmer, she already knows how to solve the easier problems, so she will be satisfied if she just solves the
th problem.
However, Mallory hates Dynamic Programming! Using her magical contest-tampering skills, she modifies the online judge such that some problems are locked behind other problems! That is, if problem is locked behind problems
,
, and
, at least one of problems
have to be solved before problem
is available. If a problem isn't locked behind anything, then there's no way to solve "at least one" dependency, so the problem becomes unsolvable! Gosh darn it, Mallory!
Realizing this, Alice uses her magical anti-contest-tampering skills to revert what Mallory has done. However, magic is tiring work, so she can only use magic to revert one problem; that is, she can make any one problem solvable without the need to solve one of its dependencies.
Alice wants to know the number of unique problems she can choose to apply magic to such that she can finish solving problem
within
minutes.
Constraints
For 30% of the problem points, .
Input specification
The first line will contain two space-separated integers and
.
The second line will contain
space-separated integers
for
, the number of seconds problem
will take to solve.
The third line will contain one integer
, the number of dependency relations there are.
The next
lines will contain two space-separated integers
, which means that solving problem
will allow problem
to be solved.
Output specification
The first and only line of output will contain one integer, the number of problems Alice can choose to "unlock" so that she can solve problem within
minutes.
Sample Input 1
1 5
1 10 5 15 40
3
2 4
3 4
4 5
Sample Output 1
3
Sample Explanation 1
Alice can unlock problems ,
, or
:
- If she unlocks
, she can solve problems
in
seconds.
- If she unlocks
, she can solve problems
in
seconds.
- If she unlocks
, she can solve it in
seconds.
However:
- If she unlocks
, there's no way to solve problem
.
- If she unlocks
, The fastest (and only) way to solve problem
is to solve problems
in
seconds, which is over the time limit of 1 minute.
Sample Input 2
1 5
5 4 3 60 1
6
5 4
4 3
3 5
1 2
2 1
3 3
Sample Output 2
2
Comments