Smallest price to pay for all heaters.

Hi. guys. Here is the task.
Suppose you hava a house, which requires to install a heater in every room.
All rooms are different in space and each room needs to have a heater with greater or equal power.
You have some heater models with their prices.
You need to pick heaters that they can heat all the rooms and total price would be minimal.
Input data: first line, number of rooms. Second line, min power needed to heat the room for each room. Third line number of heater models.
Next lines - heaters with power and price.
Examples:
Input:
1
400
1
400 1000
Output:1000

Input:
3
2 1 3
4
1 15
1 7
2 3
10 7

output:
13

So I've sorted the array of heaters by price and then iterate through it untill I get the right power. Add the price to total price.
This is kinda brute force thing, I think there should be a better way. Thanks.

Comments (5)