CF1728E.Red-Black Pepper

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Monocarp is going to host a party for his friends. He prepared nn 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 ii -th dish has two values aia_i and bib_i — 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 mm shops in his local area. The jj -th of them has packages of red pepper sufficient for xjx_j servings and packages of black pepper sufficient for yjy_j 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 xx red pepper packages and yy black pepper packages, then xx and yy should be non-negative and xxj+yyjx \cdot x_j + y \cdot y_j should be equal to nn .

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 nn ( 1n31051 \le n \le 3 \cdot 10^5 ) — the number of dishes.

The ii -th of the next nn lines contains two integers aia_i and bib_i ( 1ai,bi1091 \le a_i, b_i \le 10^9 ) — the tastiness of the ii -th dish with red pepper added or black pepper added, respectively.

The next line contains a single integer mm ( 1m31051 \le m \le 3 \cdot 10^5 ) — the number of shops.

The jj -th of the next mm lines contains two integers xjx_j and yjy_j ( 1xj,yjn1 \le x_j, y_j \le n ) — the number of servings the red and the black pepper packages are sufficient for in the jj -th shop, respectively.

输出格式

Print mm 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.

输入输出样例

  • 输入#1

    3
    5 10
    100 50
    2 2
    4
    2 3
    1 1
    3 2
    2 2

    输出#1

    62
    112
    107
    -1
  • 输入#2

    10
    3 1
    2 3
    1 1
    2 1
    6 3
    1 4
    4 3
    1 3
    5 3
    5 4
    10
    8 10
    9 3
    1 4
    2 5
    8 3
    3 5
    1 6
    7 2
    6 7
    3 1

    输出#2

    26
    -1
    36
    30
    -1
    26
    34
    26
    -1
    36

说明/提示

Consider the first example.

In the first shop, Monocarp can only buy 00 red pepper packages and 11 black pepper package. Black pepper added to all dishes will sum up to 10+50+2=6210 + 50 + 2 = 62 .

In the second shop, Monocarp can buy any number of red and black pepper packages: 00 and 33 , 11 and 22 , 22 and 11 or 33 and 00 . The optimal choice turns out to be either 11 and 22 or 22 and 11 . Monocarp can add black pepper to the first dish, red pepper to the second dish and any pepper to the third dish, the total is 10+100+2=11210 + 100 + 2 = 112 .

In the third shop, Monocarp can only buy 11 red pepper package and 00 black pepper packages. Red pepper added to all dishes will sum up to 5+100+2=1075 + 100 + 2 = 107 .

In the fourth shop, Monocarp can only buy an even total number of packages. Since nn is odd, it's impossible to get exactly nn packages. Thus, the answer is 1-1 .

首页