## COCI '13 Contest 1 #4 Lopov

View as PDF

Points: 15 (partial)
Time limit: 0.6s
Memory limit: 32M

Problem types

The difficult economic situation in the country and reductions in government agricultural subsidy funding have caused Mirko to change his career again, this time to a thief. His first professional endeavour is a jewellery store heist.

The store contains pieces of jewellery, and each piece has some mass and value . Mirko has bags to store his loot, and each bag can hold some maximum mass . He plans to store all his loot in these bags, but at most one jewellery piece in each bag, in order to reduce the likelihood of damage during the escape.

Find the maximum total jewellery value that Mirko can "liberate".

#### Input

The first line of input contains two numbers, and .

Each of the following lines contains a pair of numbers, and .

Each of the following lines contains a number, .

All numbers in the input are positive integers.

#### Output

The first and only line of output must contain the maximum possible total jewellery value.

#### Scoring

In test data worth at least 50% of total points, and will be less than .

#### Sample Input 1

2 1
5 10
100 100
11

#### Sample Output 1

10

#### Sample Input 2

3 2
1 65
5 23
2 99
10
2

#### Sample Output 2

164

#### Explanation

Mirko stores the first piece of jewellery into the second bag and the third piece into the first bag.