斯特潘是一位经验丰富的奥运选手。他有n杯物理奥林匹克和m杯信息学奥林匹克。每个杯子都有两个参数——其重要性ci和宽度wi。
Stepan决定将他的一些杯子放在宽度为d的架子上,这样:
·货架上有至少一个物理学杯和至少一个信息学杯,
·暴露的杯的总宽度不超过d,
·从每个科目(物理和信息学)中,一些最重要的杯子被暴露出来(即,如果某个科目的杯子具有显著性x,那么该科目的所有杯子都必须具有显著性大于x)。
考虑到上述所有规则,你的任务是确定最大可能的总体意义,当Stepan将杯子放在宽度为d的架子上时,他可以得到最大可能的总意义。总意义是所有暴露杯子的意义之和。
Stepan is a very experienced olympiad participant. He has
n cups for Physics olympiads and
m cups for Informatics olympiads. Each cup is characterized by two parameters − its significance
ci and width
wi.
Stepan decided to expose some of his cups on a shelf with width
d in such a way, that:
-
there is at least one Physics cup and at least one Informatics cup on the shelf,
-
the total width of the exposed cups does not exceed d,
-
from each subjects (Physics and Informatics) some of the most significant cups are exposed (i. e. if a cup for some subject with significance x is exposed, then all the cups for this subject with significance greater than x must be exposed too).
Your task is to determine the maximum possible total significance, which Stepan can get when he exposes cups on the shelf with width
d, considering all the rules described above. The total significance is the sum of significances of all the exposed cups.
Output
Print the maximum possible total significance, which Stepan can get exposing cups on the shelf with width
d, considering all the rules described in the statement.
If there is no way to expose cups on the shelf, then print
0.
Note
In the first example Stepan has only one Informatics cup which must be exposed on the shelf. Its significance equals
3 and width equals
2, so after Stepan exposes it, the width of free space on the shelf becomes equal to
6. Also, Stepan must expose the second Physics cup (which has width
5), because it is the most significant cup for Physics (its significance equals
5). After that Stepan can not expose more cups on the shelf, because there is no enough free space. Thus, the maximum total significance of exposed cups equals to
8.