## JOI '14 Open P5 - Pinball

View as PDF

Points: 25 (partial)
Time limit: 0.6s
Memory limit: 512M

Problem types
##### Contest Day 2 - JOI Open Contest

Alice likes a video game named Pinball. The rule of Pinball is as follows.

The board of Pinball is a grid of squares with rows and columns. The first row of the board is the top of the board, and the row of the board is the bottom of the board. The square in the row and the column is denoted by .

A ball appears at one of the squares on the first row of the board, and it falls down vertically towards the bottom of the board. That is to say, if a ball appears in the square , it will pass through , and it will arrive at in the bottom. Alice will get a score when she hits back the ball successfully.

One day, Alice noticed that it is difficult to hit back the ball because a ball can arrive at any squares in the bottom. Alice decided to put some of the following devices on the board appropriately so that there is only one square in the bottom where a ball can possibly arrive.

There are devices numbered from to . Each device is parallel to the rows of the board. The device sits in squares from to . Hence it covers squares in total. When a ball arrives at some square on this device, the ball will be transferred to the square . After that, the transferred ball will move vertically along the column . One device will not interact with a ball more than once.

She needs to pay yen in the game to put the device on the board. She will choose some of the devices and put them on the board so that there is only one square in the bottom where a ball can possibly arrive. She would like to minimize the total cost by putting devices efficiently.

Figure: An example of the board of Pinball. , . A ball appears in the square in the first row. Then, the ball will move to , and it will be transferred to by the device . It finally arrives at the square in the bottom.

Write a program which, given the size of the board and the information of the devices, calculates the minimum of the total cost needed to put devices on the board so that there is only one square in the bottom where a ball can possibly arrive.

#### Input Specification

Read the following data from the standard input.

• The first line of input contains two space separated integers , . This means the board has rows and columns and the number of devices is .
• The line of the following lines contains four space separated integers , , , . This means the device sits in squares from to . The device covers squares in total. The device will transfer a ball arrived at one of these squares to the square . The cost to put the device is yen.

#### Output Specification

Write one line to the standard output. The output should contain one integer which denotes the minimum of the total cost needed to put devices on the board so that there is only one square in the bottom where a ball can possibly arrive. If it is not possible to put devices satisfying these conditions, write -1.

#### Constraints

All input data satisfy the following conditions.

• .
• .
• .
• .

The following conditions are satisfied:

• .
• .

The following condition is satisfied:

• .

The following condition is satisfied:

• .

#### Sample Input 1

5 6
2 4 3 5
1 2 2 8
3 6 5 2
4 6 4 7
2 4 3 10

#### Sample Output 1

25

#### Explanation for Sample 1

The board and the positions of the devices are as in the following figure. The number written above each device denotes the cost needed to put it on the board.

If three devices 2; 4; 5 among five devices are put, the board becomes as in the following figure.

Then, a ball which appears in any square in the first row will arrive at the square in the bottom. The total cost to put these devices is 25 yen. You should output 25 because it is impossible to put devices on the board so that the total cost is less than 25 yen and there is only one square in the bottom where a ball can possibly arrive.

#### Sample Input 2

3 5
2 4 3 10
1 3 1 20
2 5 4 30

#### Sample Output 2

-1

#### Explanation for Sample 2

It is impossible to put devices on the board so that there is only one square in the bottom where a ball can possibly arrive.