Page not loading? Try clicking here.
Placeholder

#2259

Melon Field 2s 32MB

Problems

At Taeyang’s uncle’s house in the countryside, there is a large melon field.

Taeyang became curious about how many melons are growing in the field. After thinking carefully, he came up with a good idea:

Eureka!

Count the number of melons growing in 1㎡ of area, then calculate the total area of the field. Using proportional reasoning, he can find the total number of melons.

The melon field is in a hexagonal shape shaped like an “L” or rotated 90°, 180°, or 270° (┏,ㄱ,┛ shapes).
All sides of the field are aligned either horizontally or vertically.

Starting from a corner, he measured the lengths of all six sides while walking along the perimeter of the field in counterclockwise order.

For example, if the field has the following shape:

  • Directions: right = east, left = west, down = south, up = north.

  • Starting from the top-left corner, moving counterclockwise along the perimeter: south 30 m, east 60 m, south 20 m, east 100 m, north 50 m, west 160 m.

The area of this field is 6800㎡. If there are 7 melons per 1㎡, then the total number of melons is 47600.


Input

The first line contains a positive integer K (1 ≤ K ≤ 20), the number of melons per 1㎡.

The next 6 lines each contain two integers representing the direction and length of each side, in order, starting from an arbitrary vertex and moving counterclockwise along the perimeter.

  • Directions are encoded as:

    • 1 = east

    • 2 = west

    • 3 = south

    • 4 = north

  • Lengths are integers between 1 and 500.


Output

Print a single integer: the total number of melons in the field.


Example

7

4 50
2 160
3 30
1 60
3 20
1 100
47600


Source

KOI 본선 2010 초3/중2

You must sign in to write code.