Gourmet Grazers
InterviewTime limit1sMemory limit128 MB
Assign each cow a distinct grass type whose price and greenness both meet her minimums, minimizing total price; print -1 if impossible.
- Level
Medium6 of 10
- Topics
- Greedy, Sorting, Binary search, Implementation
- Solved
- No attempts yet
Problem
Like so many others, the cows have developed very haughty tastes and will no longer graze on just any grass. Instead, Farmer John must purchase gourmet organic grass for each of his () cows.
Each cow demands grass whose price is at least () and whose greenness score is at least (). The store has () different types of grass available; each grass has a price () and a greenness score (). Of course, no cow would sacrifice her individuality, so no two cows can be given the same kind of grass (each type of grass may be assigned to at most one cow).
Help Farmer John satisfy the cows' expensive gourmet tastes while spending as little money as necessary.
Input
- Line 1: Two space-separated integers and .
- Next lines: line contains two space-separated integers and .
- Next lines: line contains two space-separated integers and .
Output
- A single integer: the minimum cost to satisfy all the cows. If it is impossible, output .
Hint
- In the first example, cow 1 eats a grass costing 2, cow 2 a grass costing 4, cow 3 a grass costing 2, and cow 4 a grass costing 4, for a total cost of .