AtCoder Regular Contest #004 B : 2点間距離の最大と最小 ( Maximum and Minimum )

問題概要

平面上にN個の点があり、dist(p[i], p[i+1])が分かっている。dist(p[0], p[N-1])のとりうる最小値と最大値を求める問題。

解法

最大値は自明に和。最小値は存在しても必ず整数でもっとも長い辺をキャンセルするように作れば良い。

acceptされたコード

import std.stdio, std.math, std.algorithm, std.conv, std.string;

void main(){
	int n = to!int(readln.chomp);
	int[] ds;
	foreach(_; 0..n){
		ds ~= to!int(readln.chomp);
	}

	int maxi = reduce!(max)(int.min, ds);
	int total = reduce!("a + b")(0, ds);

	writeln(total);
	writeln(max(0, maxi-(total-maxi)));
}