Problem:
Score : 400 points

Problem Statement

There are N mountains in a circle, called Mountain 1, Mountain 2, ..., Mountain N in clockwise order. N is an odd number.

Between these mountains, there are N dams, called Dam 1, Dam 2, ..., Dam N. Dam i (1 \leq i \leq N) is located between Mountain i and i+1 (Mountain N+1 is Mountain 1).

When Mountain i (1 \leq i \leq N) receives 2x liters of rain, Dam i-1 and Dam i each accumulates x liters of water (Dam 0 is Dam N).

One day, each of the mountains received a non-negative even number of liters of rain.

As a result, Dam i (1 \leq i \leq N) accumulated a total of A_i liters of water.

Find the amount of rain each of the mountains received. We can prove that the solution is unique under the constraints of this problem.

Constraints

All values in input are integers.

3 \leq N \leq 10^5-1

N is an odd number.

0 \leq A_i \leq 10^9

The situation represented by input can occur when each of the mountains receives a non-negative even number of liters of rain.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print N integers representing the number of liters of rain Mountain 1, Mountain 2, ..., Mountain N received, in this order.

Sample Input 1

3
2 2 4

Sample Output 1

4 0 4

If we assume Mountain 1, 2, and 3 received 4, 0, and 4 liters of rain, respectively, it is consistent with this input, as follows:

Dam 1 should have accumulated \frac{4}{2} + \frac{0}{2} = 2 liters of water.

Dam 2 should have accumulated \frac{0}{2} + \frac{4}{2} = 2 liters of water.

Dam 3 should have accumulated \frac{4}{2} + \frac{4}{2} = 4 liters of water.

Sample Input 2

5
3 8 7 5 5

Sample Output 2

2 4 12 2 8

Sample Input 3

3
1000000000 1000000000 0

Sample Output 3

0 2000000000 0

Solution in Rust (1.15.1) (judge verdict: Accepted)

#[allow(unused_imports)]
use std::cmp::{max, min};
#[allow(unused_imports)]
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque};
#[allow(unused_imports)]
use std::io::prelude::BufRead;
#[allow(unused_imports)]
use std::io::{stdin, stdout, BufReader, BufWriter, Write};
#[allow(unused_imports)]
use std::mem::swap;
#[allow(unused_imports)]
use std::str::FromStr;

fn main() {
	let n = read::<usize>();
	let arr = read_vector::<usize>();
	let mut ans = vec![0; n];
	if calculate(0, &arr, &mut ans) == arr[arr.len() / 2] {
		print(&ans);
		return;
	}
	if calculate(2000000000, &arr, &mut ans) == arr[arr.len() / 2] {
		print(&ans);
		return;
	}
	let (mut l, mut r) = (0, 2000000000);
	while r - l > 2 {
		let mut mid = (l + r) / 2;
		if mid % 2 == 1 {
			mid += 1;
		}
		if calculate(mid, &arr, &mut ans) > arr[arr.len() / 2] {
			if (arr.len() - 1) % 4 == 0 {
				r = mid;
			} else {
				l = mid;
			}
		} else {
			if (arr.len() - 1) % 4 == 0 {
				l = mid;
			} else {
				r = mid;
			}
		}
	}
	if (arr.len() - 1) % 4 == 0 {
		let _ = calculate(l, &arr, &mut ans);
	} else {
		let _ = calculate(r, &arr, &mut ans);
	}
	print(&ans);
}

fn calculate(x: usize, arr: &[usize], ans: &mut Vec<usize>) -> usize {
	ans[0] = x;
	let mut r = 1;
	let mut l = arr.len() - 1;
	for i in 0..(arr.len() / 2) {
		if ans[r - 1] / 2 > arr[r - 1] || ans[(l + 1) % arr.len()] / 2 > arr[l]{
			if i % 2 == 0 {
				if (arr.len() - 1) % 4 == 0 {
					return std::usize::MAX;
				} else {
					return 0;
				}
			} else {
				if (arr.len() - 1) % 4 == 0 {
					return 0;
				} else {
					return std::usize::MAX;
				}
			}
		}
		ans[r] = (arr[r - 1] - ans[r - 1] / 2) * 2;
		ans[l] = (arr[l] - ans[(l + 1) % arr.len()] / 2) * 2;
		r += 1;
		l -= 1;
	}
	ans[r] / 2 + ans[l] / 2
}

fn print(ans: &[usize]) {
	for i in 0..ans.len() {
		if i < ans.len() - 1 {
			print!("{} ", ans[i]);
		} else {
			println!("{}", ans[i]);
		}
	}
}

#[allow(dead_code)]
fn read<T>() -> T where T: std::str::FromStr, T::Err: std::fmt::Debug {
    let mut buf = String::new();
    stdin().read_line(&mut buf).unwrap();
    return buf
        .trim()
        .parse()
        .unwrap();
}

#[allow(dead_code)]
fn read_vector<T>() -> Vec<T> where T: std::str::FromStr, T::Err: std::fmt::Debug {
    let mut buf = String::new();
    stdin().read_line(&mut buf).unwrap();
    return buf
        .split_whitespace()
        .map(|s| s.parse().unwrap())
        .collect();
}

#[allow(dead_code)]
fn read_matrix<T>() -> Vec<Vec<T>> where T: std::str::FromStr, T::Err: std::fmt::Debug {
    use std::io::prelude::*;
    let stdin = stdin();
    return stdin
        .lock()
        .lines()
        .map(|line| line
            .unwrap()
            .split_whitespace()
            .map(|s| s.parse().unwrap())
            .collect())
        .collect();
}