This page is still under construction.

Parts of this page are still being built. What you see may change.

Floodgates

Time limit1sMemory limit128 MB

Summary
Each gate drains Fi per hour at fixed cost Ci when opened. For each query (V, T), find the minimum total cost whose combined capacity Fi*T covers V.
Level

Medium5 of 10

Topics
Brute force, Greedy, Sorting, Dynamic programming
Solved
No attempts yet

Problem

A dam has nn floodgates. Each floodgate has its own flow rate and channel, and opening it can cause flood damage to the downstream area.

Opening floodgate GiG_i drains FiF_i m³ of water per hour and incurs a damage cost of CiC_i. This damage cost is a fixed cost that occurs whenever the gate is opened at all, regardless of how long it stays open.

The dam holds VV m³ of water, and all of it must be drained within TT hours. Write a program that finds how to open the floodgates so that the total damage cost is minimized.

Each floodgate operates independently, opening time is measured in whole hours, and a gate can be kept open for at most TT hours. Therefore, opening gate GiG_i can drain at most Fi×TF_i \times T m³ of water.

For example, suppose there are 4 floodgates whose flow rates and damage costs are as follows.

GateG1G_1G2G_2G3G_3G4G_4
Flow (m³/hour)720000500001300001200000
Cost1200006000050000150000

To drain 5000000 m³ within 7 hours, opening only G1G_1 drains 720000×7=5040000720000 \times 7 = 5040000 m³, which is enough, at a cost of 120000.

To drain 5000000 m³ within 30 hours, opening G2G_2 and G3G_3 together drains 180000 m³ per hour, which is more than enough within 30 hours, at a cost of 60000+50000=11000060000 + 50000 = 110000.

Input

The first line contains the number of floodgates nn. (1≤n≤201 \le n \le 20)

Each of the next nn lines contains the flow rate FiF_i (m³/hour) and the damage cost CiC_i of gate GiG_i, separated by a space.

The next line contains the number of queries mm. (1≤m≤501 \le m \le 50)

Each of the following mm lines contains VV and TT for one query, meaning that the stored VV m³ of water must all be drained within TT hours.

(1≤Fi,Ci,V≤1091 \le F_i, C_i, V \le 10^9, 1≤T≤10001 \le T \le 1000)

Output

For each query, print one line in the format Case k: X, where kk is the query number starting from 1 and XX is the minimum damage cost. If the VV m³ of water cannot be drained within TT hours, print IMPOSSIBLE instead of XX.

Examples1

  1. Example 1

    Input
    4
    720000 120000
    50000 60000
    130000 50000
    1200000 150000
    3
    5000000 7
    5000000 30
    63000000 24
    
    Expected output
    Case 1: 120000
    Case 2: 110000
    Case 3: IMPOSSIBLE