GUPTA MECHANICAL

IN THIS WEBSITE I CAN TELL ALL ABOUT TECH. TIPS AND TRICKS APP REVIEWS AND UNBOXINGS ALSO TECH. NEWS .............

Thursday 8 September 2022

[Solution] Red-Black Pepper Codeforces Solution


E. Red-Black Pepper
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Monocarp is going to host a party for his friends. He prepared n 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 i-th dish has two values ai and bi — 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 m shops in his local area. The j-th of them has packages of red pepper sufficient for xj servings and packages of black pepper sufficient for yj 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 x red pepper packages and y black pepper packages, then x and y should be non-negative and xxj+yyj should be equal to n.

Solution Click Below:-  👉CLICK HERE👈
👇👇👇👇👇

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.

Input

The first line contains a single integer n (1n3105) — the number of dishes.

The i-th of the next n lines contains two integers ai and bi (1ai,bi109) — the tastiness of the i-th dish with red pepper added or black pepper added, respectively.

The next line contains a single integer m (1m3105) — the number of shops.

The j-th of the next m lines contains two integers xj and yj (1xj,yjn) — the number of servings the red and the black pepper packages are sufficient for in the j-th shop, respectively.

Output

Print m 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