[Solution] Red-Black Pepper Codeforces Solution
Monocarp is going to host a party for his friends. He prepared dishes and is about to serve them. First, he has to add some powdered pepper to each of them — otherwise, the dishes will be pretty tasteless.
The -th dish has two values and — its tastiness with red pepper added or black pepper added, respectively. Monocarp won't add both peppers to any dish, won't add any pepper multiple times, and won't leave any dish without the pepper added.
Before adding the pepper, Monocarp should first purchase the said pepper in some shop. There are shops in his local area. The -th of them has packages of red pepper sufficient for servings and packages of black pepper sufficient for servings.
Monocarp goes to exactly one shop, purchases multiple (possibly, zero) packages of each pepper in such a way that each dish will get the pepper added once, and no pepper is left. More formally, if he purchases red pepper packages and black pepper packages, then and should be non-negative and should be equal to .
For each shop, determine the maximum total tastiness of the dishes after Monocarp buys pepper packages only in this shop and adds the pepper to the dishes. If it's impossible to purchase the packages in the said way, print -1.
The first line contains a single integer () — the number of dishes.
The -th of the next lines contains two integers and () — the tastiness of the -th dish with red pepper added or black pepper added, respectively.
The next line contains a single integer () — the number of shops.
The -th of the next lines contains two integers and () — the number of servings the red and the black pepper packages are sufficient for in the -th shop, respectively.
Print integers. For each shop, print the maximum total tastiness of the dishes after Monocarp buys pepper packages only in this shop and adds the pepper to the dishes. If it's impossible to purchase the packages so that each dish will get the pepper added once and no pepper is left, print -1.
No comments:
Post a Comment