vs = read_line.split.map(&.to_i64) es = [] of Array(Float64) 5.times do |i| e = [0.0] * 6 e[5] = vs[i].to_f64 e[i - 1] = e[i] = e[(i + 1) % 5] = 1i64 es << e e = [0.0] * 6 e[i] = 1 es << e end ans = 0i64 (1 << 10).times do |i| next if i.popcount != 5 mat = [] of Array(Float64) 10.times do |j| if (i & (1 << j)) != 0 mat << es[j].clone end end ans = {ans, solve(vs, mat)}.max end puts ans def solve(vs, mat) used = Array.new(5, false) 5.times do |i| pivot = -1 5.times do |j| next if used[j] next if mat[j][i].abs < 1e-8 if pivot == -1 || mat[pivot][i].abs < mat[j][i].abs pivot = j end end return 0i64 if pivot == -1 (i + 1).upto(5) do |j| mat[pivot][j] /= mat[pivot][i] end mat[pivot][i] = 1.0 5.times do |j| next if j == pivot next if mat[j][i] == 0 (i + 1).upto(5) do |k| mat[j][k] -= mat[j][i] * mat[pivot][k] end mat[j][i] = 0 end used[pivot] = true end 5.times do |i| return 0i64 if mat[i][5] < -1e-8 end cs = mat.map { |row| row[5].to_i64 } ret = 0i64 {0, cs[0] - 1}.max.upto(cs[0]) do |c0| {0, cs[1] - 1}.max.upto(cs[1]) do |c1| {0, cs[2] - 1}.max.upto(cs[2]) do |c2| {0, cs[3] - 1}.max.upto(cs[3]) do |c3| {0, cs[4] - 1}.max.upto(cs[4]) do |c4| next if c4 + c0 + c1 > vs[0] next if c0 + c1 + c2 > vs[1] next if c1 + c2 + c3 > vs[2] next if c2 + c3 + c4 > vs[3] next if c3 + c4 + c0 > vs[4] ret = {ret, c0 + c1 + c2 + c3 + c4}.max end end end end end return ret end