Cheapest items

4.3

10 votes
Problem

You are given N items with their price (Pi) and value (Vi).

You are required to choose a subset of items such that (size of subset + sum of values of all selected elements)N and the sum of prices of all selected elements must be minimum.

Input format

  • The first line contains an integer T denoting the number of test cases.
  • The first line of each test case contains an integer N denoting the number of N items.
  • Each of the next N lines contain two space-separated integers Vi and Pi.

Output format

For each test case, print a single line denoting the minimum value of the sum of prices of selected elements.

Constraints

  • 1T100
  • 1N1000
  • 0ViN
  • 1Pi109
  • Sum of N over all test cases will not exceed 2000
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

We will choose the first and third item: (size of subset (2) + sum of values (0+1))=3 which is >=N(3) and price is 2+1=3 which is optimal

Editor Image

?