CCCGuide

Problem S1: Crazy Fencing

2021 · S1 · Original problem · DMOJ

Problem Description

You need to paint a wooden fence between your house and your neighbour’s house. You want to determine the area of the fence, in order to determine how much paint you will use.

However, the fence is made out of NN non-uniform pieces of wood, and your neighbour believes that they have an artistic flair. In particular, the pieces of wood may be of various widths. The bottom of each piece of wood will be horizontal, both sides will be vertical, but its top may be cut on an angle. Two such pieces of wood are shown below:

Two right trapezoids.

Thankfully, the fence has been constructed so that adjacent pieces of wood have the same height on the sides where they touch, which makes the fence more visually appealing.

Input Specification

The first line of the input will be a positive integer NN, where N≤10 000N \leq 10\,000.

The second line of input will contain N+1N+1 space-separated integers h1,…,hN+1h_1,\ldots, h_{N+1} (1≤hi≤100,1≤i≤N+1)(1 \le h_i \le 100, 1 \le i \le N+1) describing the left and right heights of each piece of wood. Specifically, the left height of the iith piece of wood is hih_i and the right height of the iith piece of wood is hi+1h_{i+1}.

The third line of input will contain NN space-separated integers wiw_i (1≤wi≤100,1≤i≤N)(1 \le w_i \le 100, 1 \le i \le N) describing the width of the iith piece of wood.

Output Specification

Output the total area of the fence.

Sample Input 1

3
2 3 6 2
4 1 1

Output for Sample Input 1

18.5

Explanation of Output for Sample Input 1

The fence looks like the following:

Three right trapezoids placed side by side, each with their bases placed vertically and the side forming the height placed horizontally at the bottom. The left piece has left base of length 2, right base of length 3, and height of length 4. The middle piece has left base 3, right base 6, and height 1. The right piece has left base 6, right base 2, and height 1. The left and middle pieces share the base of length 3, the right and middle pieces share the base of length 6.

When looking from left to right, the individual areas of the pieces of wood are 10=4⋅(2+3)/210 = 4\cdot (2+3)/2, 4.5=1⋅(3+6)/24.5 = 1\cdot(3+6)/2, and 4=1⋅(6+2)/24 = 1 \cdot (6+2)/2, for a total area of 18.5.

Sample Input 2

4
6 4 9 7 3
5 2 4 1

Output for Sample Input 2

75

Explanation of Output for Sample Input 2

The fence looks like the following:

Four right trapezoids placed side by side, each with bases placed vertically and the edge forming the height placed horizontally at the bottom. The left piece has left base of length 6, right base of length 4, and height of length 5. The middle-left piece has left base 4, right base 9, and height 2. The middle-right piece has left base 9, right base 7, and height 4. The right piece has left base 7, right base 3, and height 1. The left and middle-left pieces share the base of length 4, the middle-left and middle-right pieces share the base of length 9, the middle-right and right pieces share the base of length 7.

When looking from left to right, the individual areas of the pieces of wood are 25, 13, 32, and 5, for a total area of 75.