QOJ.ac

QOJ

Time Limit: 13 s Memory Limit: 1024 MB Total points: 100

#3011. Knight of the Tarot Cards

統計

Problem Description

Consider an infinite chessboard and a special knight whose move types can change given powerups. The knight is trying to reach square $(0, 0)$.

Some of the squares of the infinite chessboard have tarot cards on them. If the knight lands on some position $(r, c)$ on the infinite chessboard with a tarot card on it, then the knight has the option of buying that card at that position. Each tarot card will have a price, and will have two integer values written on it. The tarot card with values $a$ and $b$ written on it allow the knight to make relative jumps:

$$ (−a, −b) (a, −b) (−a, b) (a, b) (b, a) (−b, a) (b, −a) (−b, −a) $$

The knight retains all cards he purchases and can make relative moves from any of the cards he owns any number of times. The knight must only pay when obtaining cards and can perform jumps at no additional cost. For example, if he buys a card with $3$ and $2$ and another card with $8$ and $4$, he may jump by $(−2, 3)$, and from there jump by $(8, 4)$, and later jump by $(−3, 2)$. Of course, he cannot make a jump $(a, b)$ until after he arrives at a square with a tarot card with $a$ and $b$ on it, and purchases that card.

Given positions of the tarot cards on the board and their prices, find the least amount that the knight must pay to reach square $(0, 0)$.

Input

Each test case will begin with a line containing a single integer $n$ ($1 \leq n \leq 1,000$), which is the number of tarot cards on the chessboard.

Each of the next $n$ lines contains a description of a tarot card in five space-separated integers: $$ r\;c\;a\;b\;p $$ ($−10^9 \leq r, c \leq 10^9, 1 \leq a, b, p \leq 10^9$), where $(r, c)$ is the location of the tarot card, $a$ and $b$ are the offset values, and $p$ is the price of the card.

The first tarot card is the initial position of the knight. Multiple tarot cards may exist at the same location. The knight must have a tarot card to move.

Output

Output a single integer, which is the minimum cost for the knight to reach the goal at $(0, 0)$. Output $−1$ if it is not possible to reach the goal.

Samples

Sample Input 1

2
3 3 2 2 100
1 1 1 1 500

Sample Output 1

600

Sample Input 2

2
2 0 2 1 100
6 0 8 1 1

Sample Output 2

100

Sample Input 3

3
1 0 100 50 100
1 50 50 25 100
26 0 20 30 123

Sample Output 3

-1

Discussions

About Discussions

The discussion section is only for posting: Editorials, General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues. Submitting multiple issues may cause your account to be banned.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.