QOJ.ac

QOJ

Time Limit: 1 s Memory Limit: 32 MB Total points: 100

#477. Schools

Statistics

There are $n$ schools in Byteotia, and each has its number $m$, $1 ≤ m ≤ n$, just as every school tends to. However, the previous king of Byteotia paid little attention to the school numbering, and generously allowed every new school to choose any number from the range $1…n$. There is thus no guarantee that the numbers are unique, in fact it seems highly unlikely. Clearly some schools may have chosen the same number, and as a consequence some numbers from the range $1…n$ might be not used at all. A perfect numbering would be a permutation, i.e. a one that assigns each number from the specified range to exactly one school.

The newly crowned king of Byteotia wants to reform the numbering system, so that every number would be used exactly once. It is, however, not an easy task, since most schools are reluctant to change the numbers they have once chosen.

The king has sent his most trusted informants to all the schools to inquire which numbers each school would accept. Since every school would like to keep its current number (or at least a number close to it), each school director has specified an interval containing the school's current number. These intervals are called the tolerance intervals. Furthermore, each school has stated the cost $c$ of changing its number by $1$. Hence the total cost of changing a school's number equals $c \cdot |m-m'|$, where $m$ denotes the old school number, and $m'$ the new one. Of course $m'$ must lie in the tolerance interval previously specified by the school.

Now, all the information gathered, the king would like to know, if it's possible to introduce a perfect numbering (while complying with every school's tolerance interval), and if so, what is the minimal cost of such reenumaration. He has asked you - the royal computer scientist - to carefully study the information he has provided you with and give the answer in return.

Write a programme which:

  • reads from the standard input the current numbers of Byteotian schools, their tolerance intervals and the costs of changing their numbers by ,
  • checks if it's possible to perfectly reenumerate the schools complying with all previously described conditions, and if so, determines the minimal cost of such reenumeration,
  • writes the result to the standard output.

Input

The first line of the standard input contains one integer $n$ ($1 ≤ n ≤ 200$), denoting the number of schools in Byteotia. In the following $n$ lines there are descriptions of the schools themselves. The line no. $i+1$ ($1 ≤ i ≤ n$) contains four integers $m_i$, $a_i$, $b_i$ and $k_i$ ($1 ≤ a_i ≤ m_i ≤ b_i ≤ n$, $1 ≤ k_i ≤ 1\,000$), separated by single spaces. These denote respectively: the current number of the $i$’th school, the left and right endpoint of the $i$’th school's tolerance interval regarding the change of number (it is a closed interval, therefore the new i’th school's number mi’ must satisfy the inequality $a_i ≤ m_i' ≤ b_i$) and the cost of changing the number of $i$’th school by 1.

Output

If a reenumeration satisfying the above conditions is possible, the programme should write one integer $k$ denoting the minimum possible cost of performing the operation. If it's not possible, it should write the word NIE, which means no in Polish.

Example

Input

5
1 1 2 3
1 1 5 1
3 2 5 5
4 1 5 10
3 3 3 1

Output

9

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.