Level Up

시간 제한2초메모리 제한256 MB

요약
레벨업 전후로 경험치와 소요 시간이 달라지는 퀘스트들의 수행 순서를 정해 s1과 s2를 최소 시간에 채우는 문제입니다.
난이도

보통10점 중 7점

유형
동적 계획법, 그리디, 정렬
정답자
아직 제출이 없습니다

문제

Being a fan of MMORPGs, Steve was really excited when World of Warcraft Classic was announced. He started playing from the first day and now has only two levels to go till the maximum level. Of course, he doesn’t have as much time as he used to have when the game first appeared, so he really wants to finish these two levels as fast as possible.

In order to pass the first level, Steve needs s1 experience. Only after he gains it, he can move to the second level, in which he needs to get other s2 experience to pass it.

Steve has the list of n available quests. He knows that he may finish two levels with these quests. In order to pass the i-th quest, Steve needs ti minutes. As a result, he will get xi experience for this quest.

When Steve finishes a quest that takes him to the next level, the extra experience overflow is subtracted from the next level’s required experience. Once he levels up, all the quests that are left will offer less experience yi, but they will also require less time ri.

Note that if Steve finishes a quest, he can’t repeat this quest anymore (even in another level).

Given the list of quests, help Steve choose the order in which he will be doing the quests in order to finish the last two levels as fast as possible.

입력

The first line contains three integers n, s1, s2 (1 ≤ n, s1, s2 ≤ 500) — the number of quests, experience required for the first level and experience required for the second level.

Each of the next n lines contains four integers xi, ti, yi, ri (1 ≤ yi < xi ≤ 500, 1 ≤ ri < ti ≤ 109) where xi and yi are the experience you will gain from the i-th quest on the 1-st and 2-nd level respectively and ti and ri are the number of minutes you need to spend in order to pass this quest on the 1-st and 2-nd level respectively,

출력

Print one number, representing the minimum number of minutes that Steve needs to finish two levels, or −1 if there is no way to complete quests to finish both levels.`

예제3

  1. 예제 1

    입력
    2 100 100
    100 100 10 10
    101 11 100 10
    
    예상 출력
    110
    
  2. 예제 2

    입력
    4 20 20
    40 1000 20 20
    6 6 5 5
    10 10 1 1
    10 10 1 1
    
    예상 출력
    40
    
  3. 예제 3

    입력
    2 20 5
    10 10 5 5
    10 10 5 5
    
    예상 출력
    -1