This page is still under construction.

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

Door-to-Door Sales (Hard)

Time limit2sMemory limit1024 MB

Summary
A fixed deterministic order (lexicographically smallest topological order) is generated, then find the minimum number of customers to select so their summed x and y meet quotas X and Y, and report the last-selected customer's identity.
Level

Medium7 of 10

Topics
Dynamic programming, Topological sort, Greedy, Graph
Solved
No attempts yet

Problem

SG Group has just released two revolutionary products, X and Y. Leo, the top salesman working as a traveling salesperson in SG Group's sales department, must visit the homes of all NN customers and sell these two products in the given quotas XX and YY. Each customer is numbered from 11 to NN, and if a visit to customer ii's home results in a successful sale, the amounts of products X and Y that can be sold are given as xix_i and yiy_i, respectively. However, some customers may refuse to buy the products even when visited, resulting in a failed sale.

When making door-to-door sales, the customers must be visited in order according to the manual set by the sales department, but Leo, having lost the manual, fell into deep thought because he cannot know the full visit order. However, Leo has a good memory and knows, for MM pairs of distinct customer numbers, which customer must be visited first. So Leo decides to visit all NN customers based on his memory using the following rules.

  1. Among the customers not yet visited, all customers who are currently visitable are chosen as visit candidates.
  2. All customers chosen as candidates are visited in ascending order of customer number, starting from the smallest.
  3. Steps 1 and 2 are repeated until all NN customers have been visited.

In step 1, "visitable" means that there is no customer who must be visited before them, or that all such customers have already been visited. In other words, if there is no customer who must be visited before customer ii, or if all such customers have already been visited, then customer ii becomes a visit candidate.

The car Leo carries while selling is filled with enough quantities of products X and Y so that they never run short.

Leo wonders how few customers he must sell both products X and Y to in this door-to-door sales trip to meet the given quotas. Find the minimum number of customers who must be made to buy the products in order to visit all NN customers in a way that satisfies the visit order and meet the quotas, and the number of the customer who succeeds in the sale last.

Input

The first line gives the number of customers Leo must visit, NN, the number of pieces of information about the customer visit order he remembers, MM, and the quotas of the two products X and Y to be sold, XX and YY, as integers. (1≤N≤4001 \le N \le 400, 0≤M≤N⋅(N−1)0 \le M \le N \cdot (N - 1) , 1≤X,Y≤2001 \le X, Y \le 200)

From the second line, across MM lines, the ordered pairs aia_i, bib_i concerning the visit order Leo remembers are given in order. This means that the customer numbered aia_i must be visited before the customer numbered bib_i. The same ordered pair is not given more than once. (1≤ai,bi≤N1 \le a_i, b_i \le N, ai≠bia_i \ne b_i, 1≤i≤M1 \le i \le M)

From line M+2M + 2, across NN lines, for customers 11 through NN, the amounts xjx_j and yjy_j of the two products X and Y that the customer buys when a visit results in a successful sale are given as integers. (1≤xj,yj≤2001 \le x_j, y_j \le 200, 1≤j≤N1 \le j \le N)

Output

Print the minimum number of customers who must have a successful sale in order to visit all NN customers in a way that satisfies the visit order and meet the quotas. Then, on the next line, print the number of the customer who succeeds in the sale last; if there are multiple possible numbers, print the number of the customer whose position in the visit order is earliest among them.

If the quotas cannot be met even if any customer buys the products, or if the visit order is contradictory and all NN customers cannot be visited so the door-to-door sales cannot be completed, print −1-1.

Examples4

  1. Example 1

    Input
    4 4 8 10
    1 4
    3 4
    2 1
    2 3
    5 2
    3 7
    2 1
    4 8
    
    Expected output
    2
    4
    
  2. Example 2

    Input
    5 5 8 3
    2 1
    1 5
    2 3
    3 4
    3 5
    3 1
    5 2
    3 1
    1 2
    2 2
    
    Expected output
    2
    1
    
  3. Example 3

    Input
    3 3 11 12
    2 1
    2 3
    1 3
    4 9
    2 3
    2 1
    
    Expected output
    -1
    
  4. Example 4

    Input
    3 4 7 7
    1 2
    2 3
    3 1
    3 2
    2 4
    3 2
    5 1
    
    Expected output
    -1