#K60702. Identify Branch with Lowest Taxable Income
Identify Branch with Lowest Taxable Income
Identify Branch with Lowest Taxable Income
You are given n branches and m sectors. Each branch provides two values per sector: its income and its expenditure.
For each branch, the taxable income for a set of sectors S is calculated as:
$$T = \sum_{j \in S}\Big(\text{income}_j - \text{expenditure}_j\Big)$$
You will be given several queries. Each query consists of an integer x followed by x sector indices. For each query, calculate the taxable income for every branch considering only the specified sectors. The branch having the lowest taxable income should be selected. In case of ties, choose the branch with the smaller branch ID (branches are 1-indexed).
Your task is to output the branch ID for each query that meets these criteria.
inputFormat
The input is read from standard input (stdin) and is structured as follows:
- First line contains two integers
n
andm
wheren
is the number of branches andm
is the number of sectors. - The next
n
lines each contain2*m
integers. For each branch, the first two numbers correspond to income and expenditure of sector 0, the next two numbers for sector 1, and so on. - The next line contains an integer
q
indicating the number of queries. - Each of the following
q
lines starts with an integerx
(the number of sectors in the query) followed byx
space-separated sector indices.
outputFormat
For each query, output a single line with the branch ID (1-indexed) that has the lowest taxable income based on the given sectors.
## sample3 3
500 100 300 50 200 80
400 200 300 100 200 150
600 200 400 200 100 70
2
2 0 1
3 0 1 2
2
2
</p>