N = gets.to_i c = gets.split.map(&:to_i).sort dp = Array.new(N+1) { |l| x = []; x[l] = [0, 0]; x } (1 .. N).each do |s| (0 .. N-s).each do |l| r = l + s b1, a1 = dp[l][r-1] b2, a2 = dp[l+1][r] a1 += c[r-1] a2 += c[l] dp[l][r] = [ [a1, b1], [a2, b2], ].max_by { |(a, b)| a.abs - b.abs } end end a, b = dp[0][N] puts a.abs - b.abs