#P1335. Maximizing Achievement in the Game Script

    ID: 14622 Type: Default 1000ms 256MiB

Maximizing Achievement in the Game Script

Maximizing Achievement in the Game Script

You are given a game script that consists of n events numbered from 1 to n. The game state contains only one variable, the achievement (variable 1), which is initially 0. The script’s events are of three types:

  • Ordinary Event (type 1): This event updates a variable by either adding or subtracting a quantity. A quantity can be either a constant or the current value of a variable. The event is described by five items: 1 v op typ val where v is the variable id (for this problem, only v = 1 will appear), op is either '+' or '-' indicating addition or subtraction, typ is 0 if the operand is a constant or 1 if it is a variable reference (which will always be variable 1), and val is the integer constant (if typ = 0) or a positive integer representing the variable id (if typ = 1). After executing an ordinary event, the execution pointer increases by 1.
  • Choice Jump (type 2): This event gives the player a choice between two jump targets. It is described by three integers: 2 a b. When executed, the player may choose either event number a or b as the next event to execute.
  • Conditional Jump (type 3): This event compares two quantities and jumps to one of two targets. It is described by seven numbers: 3 typ1 val1 typ2 val2 x y. Here, for each quantity, if typ is 0 the corresponding value is a constant, and if it is 1 then the quantity is the current value of variable 1. If the first quantity is less than the second one, the execution pointer becomes x; otherwise it becomes y.

The game starts at event 1, and the execution pointer always points to the next event to execute. If the pointer becomes a number that does not correspond to any event (i.e. not in the range 1 to n), then the game ends. Your task is to choose the branches at all choice events so that the final value of the achievement (variable 1) is maximized. If it is possible to make the achievement value arbitrarily large through cycles, output UNBOUNDED.

inputFormat

The first line contains a single integer n (1 ≤ n ≤ 500) representing the number of events. Each of the next n lines describes an event in one of the following formats:

  • Ordinary event: 1 v op typ val
    • v: variable id (will always be 1 in this problem).
    • op: a character, either '+' or '-', that indicates addition or subtraction.
    • typ: an integer, 0 or 1. If 0, then val is a constant; if 1, then val represents a variable id (which will be 1).
  • Choice jump: 2 a b where a and b are the event numbers to jump to.
  • Conditional jump: 3 typ1 val1 typ2 val2 x y
    For each quantity, if the corresponding typ is 0 then the quantity is the constant val; if it is 1 then the quantity is the current value of variable 1. If the first quantity is less than the second, the pointer becomes x; otherwise, it becomes y.

It is guaranteed that all numbers and operations are valid. Variables start at 0, and only variable 1 (the achievement) appears in the input.

outputFormat

Output a single line containing either the maximum final value of variable 1 that can be achieved if the game terminates, or UNBOUNDED if there is a way to make the achievement value arbitrarily large.

sample

3
1 1 + 0 5
1 1 - 0 3
1 1 + 0 4
6