#P10541. Maximizing Profit with Technology and Product Dependencies

    ID: 12560 Type: Default 1000ms 256MiB

Maximizing Profit with Technology and Product Dependencies

Maximizing Profit with Technology and Product Dependencies

You are given a market scenario where launching certain products will yield profit, but each product requires a set of technologies. There are two ways to acquire a technology:

  1. Purchase it directly at a cost of fjf_j. This method does not require any prerequisites.

  2. Develop (R&D) it at a cost of hjh_j, but developing technology jj is only possible if all of its prerequisite technologies have already been acquired (by either method).

In addition, there are dependencies between products and technologies. There are mm products; launching the iith product earns a profit of gig_i, but it can only be launched if all its prerequisite technologies are acquired. You are given pp pairs (u,v)(u,v) meaning that technology uu is required before launching product vv.

Furthermore, there are nn technologies and qq pairs (a,b)(a,b) representing technology–technology dependency: technology aa is a prerequisite for technology bb for the purpose of R&D acquisition. Note that the technology–technology dependency forms a directed acyclic graph (DAG), so no cycles occur.

A plan consists of selecting some products to launch (and hence acquiring the necessary technologies) and deciding for each required technology whether to purchase it or develop it. If a technology is acquired by development, all of its R&D prerequisites (according to the qq relations) must also be acquired by development. However, you may always opt to purchase a technology regardless of whether its prerequisites have been acquired.

The total profit is defined as the sum of the profits of the launched products minus the total cost of the technologies used. When a technology is required by more than one product, its cost is only paid once.

Your task is to output the maximum achievable profit.

Formally, let SS be the set of launched products and let TT be the set of required technologies (which must at least cover the prerequisites for every product in SS). For each technology jTj \in T, you may acquire it by purchase at cost fjf_j, or by R&D at cost hjh_j provided that every technology aa with a dependency (a,j)(a, j) is also acquired by R&D. The goal is to maximize: [ \text{Profit} = \sum_{i\in S} g_i - \sum_{j\in T} \text{cost}(j), ] where ( \text{cost}(j) ) is chosen as described.

A common way to solve this problem is to reduce it to a maximum closure (or minimum cut) problem in a directed graph. In one typical formulation, you construct a graph where:

  • Each product ii is represented with a profit (positive weight) gig_i, and an edge is added from the source to product ii with capacity gig_i.
  • Each technology jj is represented with a cost (negative weight) of fj-f_j, and an edge is added from technology jj to the sink with capacity fjf_j.
  • For every product–technology dependency (u,v)(u,v) (meaning technology uu is required for product vv), add an edge from product vv to technology uu with infinite capacity.
  • For every technology–technology dependency (a,b)(a, b) (meaning technology aa is a prerequisite for developing technology bb), add an edge from technology bb to technology aa with capacity max(0,fbhb)\max(0, f_b - h_b). This edge effectively models the benefit (discount) of using R&D for technology bb when its prerequisite aa is also acquired by R&D.

The maximum closure value of this graph is equal to the maximum achievable profit. (Note that all formulas involving costs and profits must be written using LaTeX formatting.)

inputFormat

The input begins with four integers mm, nn, pp, and qq, where:

  • mm is the number of products.
  • nn is the number of technologies.
  • pp is the number of product dependencies.
  • qq is the number of technology dependencies.

The second line contains mm integers, g1,g2,,gmg_1, g_2, \ldots, g_m, representing the profit of each product.

The third line contains nn integers, f1,f2,,fnf_1, f_2, \ldots, f_n, representing the purchase cost for each technology.

The fourth line contains nn integers, h1,h2,,hnh_1, h_2, \ldots, h_n, representing the R&D cost for each technology.

Then follow pp lines, each containing two integers uu and vv, indicating that technology uu is a prerequisite for product vv.

Finally, there are qq lines, each containing two integers aa and bb, indicating that technology aa is a prerequisite (for R&D) for technology bb.

All indices are 1-indexed.

outputFormat

Output a single integer: the maximum profit achievable.

sample

1 1 1 0
10
5
3
1 1
5