Keep Fighting

시간 제한0.5초메모리 제한2048 MB

요약
곱하기, 더하기, 공격 카드로 이루어진 덱을 끝없이 순환시키며 몬스터의 체력을 0 이하로 만드는 최소 턴 수를 구하거나 불가능하면 *를 출력한다.
난이도

어려움10점 중 8점

유형
그리디, 동적 계획법, 수학, 시뮬레이션
정답자
아직 제출이 없습니다

문제

Bob is playing a card game where he must defeat a monster. Before the game starts, Bob’s power is set to PP, the monster’s health is set to HH, and Bob receives a deck of NN cards in his hands.

Each card in the deck belongs to one of the following types:

  • Multiply: a card of this type has a number XX written on it. Playing it multiplies Bob’s current power by XX.
  • Add: a card of this type also has a number YY written on it. Playing it increases Bob’s current power by YY.
  • Attack: a card of this type allows Bob to attack the monster. Playing it reduces the monster’s current health by Bob’s current power.

The game is played in turns. In each turn, Bob must play exactly one card from his hand, after which the card is moved to a discard pile. If Bob has no cards left in his hand at the end of a turn, he retrieves all cards from the discard pile and can use them again in any order.

The monster is beaten as soon as its health reaches 00 or less. Is it possible for Bob to beat the monster? If so, what is the minimum number of turns required?

입력

The first line contains three integers NN (1≤N≤501 ≤ N ≤ 50), PP (0≤P≤1090 ≤ P ≤ 10^9) and HH (1≤H≤1091 ≤ H ≤ 10^9), indicating respectively the number of cards in the deck, Bob’s initial power and the monster’s initial health.

Each of the next NN lines describes a card. The content of the line depends on the type of the card, as follows:

  • Multiply: the line contains the character “*” (asterisk) and an integer XX (1≤X≤1091 ≤ X ≤ 10^9), representing the multiply factor provided by the card.
  • Add: the line contains the character “+” (plus sign) and an integer YY (1≤Y≤1091 ≤ Y ≤ 10^9), indicating the increment provided by the card.
  • Attack: the line contains the character “!” (exclamation mark).

출력

Output a single line with an integer indicating the minimum number of turns required to beat the monster, or the character “*” (asterisk) if it is impossible to beat the monster.

예제3

  1. 예제 1

    입력
    3 0 20
    * 2
    !
    + 5
    
    예상 출력
    4
    
  2. 예제 2

    입력
    1 0 1
    !
    
    예상 출력
    *
    
  3. 예제 3

    입력
    1 1 1
    + 1
    
    예상 출력
    *