### IOI '04 - Athens, Greece

## Farmer

A farmer has a set of fields, each of which is surrounded by cypress trees. Also, the farmer has a set of strips of land, each of which has a row of cypress trees. In both fields and strips, between every two consecutive cypress trees is a single olive tree. All of the farmer's cypress trees either surround a field or are in a strip and all of the farmer's olive trees are between two consecutive cypress trees in a field or in a strip.

One day the farmer became very ill and he felt that he was going to die. A few days before
he passed away he called his eldest son and told him, "I give you any *Q* cypress trees of
your choice and all the olive trees which are between any two consecutive cypress trees you
have chosen." From each field and from each strip the son can pick any combination of
cypress trees. Since the eldest son loves olives he wants to pick the *Q* cypress trees which
will allow him to inherit as many olive trees as possible.

In Figure 1, assume that the son is given *Q*=17 cypress trees. To maximize his olive
inheritance he should choose all the cypress trees in Field 1 and Field 2, inheriting 17 olive
trees.

You are to write a program which, given the information about the fields and the strips and the number of cypress trees the son can pick, determines the largest possible number of olive trees the son may inherit.

### Input

The first line contains first the integer*Q*(0 ≤

*Q*≤ 150000): the number of cypress trees the son is to select; then the integer

*M*(0 ≤

*M*≤ 2000), the number of fields; and then the integer

*K*(0 ≤

*K*≤ 2000), the number of strips. The second line contains

*M*integers

*N*

_{1},

*N*

_{2},…

*N*

_{M}: the numbers of cypress trees in fields (3 ≤

*N*

_{i}≤ 150). The third line contains

*K*integers

*R*

_{1},

*R*

_{2},…

*R*

_{K}(2 ≤

*R*

_{i}≤ 150): the numbers of cypress trees in strips. The total number of cypress trees is always at least

*Q*.

### Output

The output is to contain one line with one integer: the largest possible number of olive trees the son may inherit.### Sample Input

17 3 3 13 4 8 4 8 6

### Sample Output

17

Note: In 50% of the inputs, *Q* ≤ 1500.

All Submissions

Best Solutions

**Point Value:** 15 (partial)

**Time Limit:** 2.00s

**Memory Limit:** 16M

**Added:** Feb 27, 2010

**Languages Allowed:**

C++03, PAS, C, HASK, ASM, RUBY, PYTH2, JAVA, PHP, SCM, CAML, PERL, C#, C++11, PYTH3

## Comments (Search)

HelloMelloCon Oct 23, 2011 - 10:40:36 pm UTC Invalid ReturnI've tried the first 10 test cases myself and I get the right answers.

Process Explorer says the process takes 15,912k on Windows. Am I getting mem limit exceeded on judge?

bbi5291on Oct 23, 2011 - 11:24:12 pm UTC Re: Invalid Return