Problem:
﻿Score : 400 points

Problem Statement

In the year 2168, AtCoder Inc., which is much larger than now, is starting a limited express train service called AtCoder Express.

In the plan developed by the president Takahashi, the trains will run as follows:

A train will run for (t_1 + t_2 + t_3 + ... + t_N) seconds.

In the first t_1 seconds, a train must run at a speed of at most v_1 m/s (meters per second). Similarly, in the subsequent t_2 seconds, a train must run at a speed of at most v_2 m/s, and so on.

According to the specifications of the trains, the acceleration of a train must be always within ±1m/s^2. Additionally, a train must stop at the beginning and the end of the run.

Find the maximum possible distance that a train can cover in the run.

Constraints

1 \leq N \leq 100

1 \leq t_i \leq 200

1 \leq v_i \leq 100

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
t_1 t_2 t_3 … t_N
v_1 v_2 v_3 … v_N

Output

Print the maximum possible that a train can cover in the run.

Output is considered correct if its absolute difference from the judge's output is at most 10^{-3}.

Sample Input 1

1
100
30

Sample Output 1

2100.000000000000000

The maximum distance is achieved when a train runs as follows:

In the first 30 seconds, it accelerates at a rate of 1m/s^2, covering 450 meters.

In the subsequent 40 seconds, it maintains the velocity of 30m/s, covering 1200 meters.

In the last 30 seconds, it decelerates at the acceleration of -1m/s^2, covering 450 meters.

The total distance covered is 450 + 1200 + 450 = 2100 meters.

Sample Input 2

2
60 50
34 38

Sample Output 2

2632.000000000000000

The maximum distance is achieved when a train runs as follows:

In the first 34 seconds, it accelerates at a rate of 1m/s^2, covering 578 meters.

In the subsequent 26 seconds, it maintains the velocity of 34m/s, covering 884 meters.

In the subsequent 4 seconds, it accelerates at a rate of 1m/s^2, covering 144 meters.

In the subsequent 8 seconds, it maintains the velocity of 38m/s, covering 304 meters.

In the last 38 seconds, it decelerates at the acceleration of -1m/s^2, covering 722 meters.

The total distance covered is 578 + 884 + 144 + 304 + 722 = 2632 meters.

Sample Input 3

3
12 14 2
6 2 7

Sample Output 3

76.000000000000000

The maximum distance is achieved when a train runs as follows:

In the first 6 seconds, it accelerates at a rate of 1m/s^2, covering 18 meters.

In the subsequent 2 seconds, it maintains the velocity of 6m/s, covering 12 meters.

In the subsequent 4 seconds, it decelerates at the acceleration of -1m/s^2, covering 16 meters.

In the subsequent 14 seconds, it maintains the velocity of 2m/s, covering 28 meters.

In the last 2 seconds, it decelerates at the acceleration of -1m/s^2, covering 2 meters.

The total distance covered is 18 + 12 + 16 + 28 + 2 = 76 meters.

Sample Input 4

1
9
10

Sample Output 4

20.250000000000000000

The maximum distance is achieved when a train runs as follows:

In the first 4.5 seconds, it accelerates at a rate of 1m/s^2, covering 10.125 meters.

In the last 4.5 seconds, it decelerates at the acceleration of -1m/s^2, covering 10.125 meters.

The total distance covered is 10.125 + 10.125 = 20.25 meters.

Sample Input 5

10
64 55 27 35 76 119 7 18 49 100
29 19 31 39 27 48 41 87 55 70

Sample Output 5

20291.000000000000

Solution in Java8 (OpenJDK 1.8.0) (judge verdict: Accepted)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;

public class Main {

	private interface F extends Function<Double, Optional<Double>> {
	}

	private static class LineFunction {
		List<F> list = new ArrayList<>();

		public boolean add(F f) {
			return this.list.add(f);
		}

		public Optional<Double> f(double x) {
			double min = Double.MAX_VALUE;
			for (F f : list) {
				Optional<Double> y = f.apply(x);
				if (y.isPresent()) {
					min = Math.min(min, y.get());
				}
			}
			if (min < Double.MAX_VALUE) {
				return Optional.of(min);
			}
			return Optional.empty();
		}
	}

	private static LineFunction getVMaxLine(int[] t, int[] v) {
		LineFunction vmaxs = new LineFunction();
		int n = t.length;
		int lastTime = 0;
		for (int i = 0; i < n; lastTime += t[i++]) {
			final int now = lastTime;
			final int time = t[i];
			final int vmax = v[i];
			// vmaxにラインを引いておく
			vmaxs.add(x -> {
				double dx = x - now;
				if (0 <= dx && dx <= time) {
					return Optional.of((double) vmax);
				}
				return Optional.empty();
			});
		}
		return vmaxs;

	}

	private static LineFunction getUpLine(int[] t, int[] v) {
		LineFunction up = new LineFunction();
		int n = t.length;
		int lastTime = 0;
		double lastSpeed = 0;
		for (int i = 0; i < n; lastTime += t[i++]) {
			final double speed = lastSpeed;
			final int now = lastTime;
			final int time = t[i];
			final int vmax = v[i];

			final double needUpSpeed = vmax - speed;

			if (needUpSpeed > 0) {
				final double canUpSpeed = Math.min(needUpSpeed, time);
				up.add(x -> {
					double dx = x - now;
					if (0 <= dx) {
						return Optional.of(dx + speed);
					}
					return Optional.empty();
				});
				lastSpeed += canUpSpeed;
			} else {
				lastSpeed = vmax;
			}
		}
		return up;
	}

	private static LineFunction getDownLine(int[] t, int[] v, int T) {
		LineFunction down = new LineFunction();
		final int n = t.length;
		int lastTime = T;
		double lastSpeed = 0;
		for (int i = n - 1; i >= 0; lastTime -= t[i--]) {
			final double speed = lastSpeed;
			final int now = lastTime;
			final int time = t[i];
			final int vmax = v[i];

			final double needUpSpeed = vmax - speed;

			if (needUpSpeed > 0) {
				final double canUpSpeed = Math.min(needUpSpeed, time);
				down.add(x -> {
					double dx = now - x;
					if (0 <= dx) {
						return Optional.of(dx + speed);
					}
					return Optional.empty();
				});
				lastSpeed += canUpSpeed;
			} else {
				lastSpeed = vmax;
			}
		}
		return down;
	}

	public static void main(String[] args) throws IOException {
		try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
			br.readLine();
			int[] t = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
			int[] v = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();

			int T = Arrays.stream(t).sum();

			LineFunction vmaxs = getVMaxLine(t, v);
			LineFunction up = getUpLine(t, v);
			LineFunction down = getDownLine(t, v, T);

			// 0.5ごとにマッピングする
			double sum = 0;
			double last = 0;
			for (int i = 1; i <= 2 * T; i++) {
				double x = i / 2.0;
				double a = up.f(x).orElse(Double.MAX_VALUE);
				double b = down.f(x).orElse(Double.MAX_VALUE);
				double c = vmaxs.f(x).orElse(Double.MAX_VALUE);

				double min = Math.min(a, Math.min(b, c));

				if (min < Double.MAX_VALUE) {
					sum += last + min;
					last = min;
				} else {
					last = 0;
				}
			}
			System.out.println(sum / 4.0);
		}
	}
}