Choose for each cow whether to milk it or rent it, matching milk to stores with limited quantity and price, to maximize total cents.
Medium6GreedySortingTwo pointersBinary searchInterviewNo attempts yetTime limit2sMemory limit512 MBFarmer John decided that milk sales alone do not bring in enough money to grow his farm. To earn extra income he started a service that rents his cows out.
Farmer John has N cows, and each cow produces a fixed amount of milk per day. There are M stores near the farm, and each store buys milk up to a fixed amount at a fixed price per gallon. R neighboring farmers each want to rent one cow for a day at a fixed price.
For every cow Farmer John picks one of two options: milk it, or rent it out to a neighbor. Find the largest amount of money he can make in one day.
The first line contains N, M, and R (1≤N≤100,000, 1≤M≤100,000, 1≤R≤100,000).
Each of the next N lines contains one integer ci (1≤ci≤1,000,000), meaning that the ith cow produces ci gallons of milk per day.
Each of the next M lines contains two integers qi and pi (1≤qi,pi≤1,000,000), meaning that the ith store buys up to qi gallons of milk at pi cents per gallon. Farmer John may sell any amount between 0 and qi gallons to that store.
Each of the next R lines contains one integer ri (1≤ri≤1,000,000), meaning that one neighbor pays ri cents to rent one cow for a day.
Print one line with the largest daily profit Farmer John can make by milking or renting out each of his cows. The answer can exceed the range of a 32-bit integer, so use a 64-bit integer type.
In the first example Farmer John milks cow 1 and cow 4 and gets 13 gallons. He fills the order for 10 gallons and earns 250 cents, then sells the remaining 3 gallons at 15 cents per gallon, so milk brings in 295 cents. He rents the other three cows out for 250, 80, and 100 cents, which adds 430 cents. He turns down the 40-cent rental request. The daily profit is 725 cents in total.