Problem:
Score : 300 points

Problem Statement

You have N bamboos. The lengths (in centimeters) of these are l_1, l_2, ..., l_N, respectively.

Your objective is to use some of these bamboos (possibly all) to obtain three bamboos of length A, B, C. For that, you can use the following three kinds of magics any number:

Extension Magic: Consumes 1 MP (magic point). Choose one bamboo and increase its length by 1.

Shortening Magic: Consumes 1 MP. Choose one bamboo of length at least 2 and decrease its length by 1.

Composition Magic: Consumes 10 MP. Choose two bamboos and combine them into one bamboo. The length of this new bamboo is equal to the sum of the lengths of the two bamboos combined. (Afterwards, further magics can be used on this bamboo.)

At least how much MP is needed to achieve the objective?

Constraints

3 \leq N \leq 8

1 \leq C < B < A \leq 1000

1 \leq l_i \leq 1000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N A B C
l_1
l_2
:
l_N

Output

Print the minimum amount of MP needed to achieve the objective.

Sample Input 1

5 100 90 80
98
40
30
21
80

Sample Output 1

23

We are obtaining three bamboos of lengths 100, 90, 80 from five bamboos 98, 40, 30, 21, 80. We already have a bamboo of length 80, and we can obtain bamboos of lengths 100, 90 by using the magics as follows at the total cost of 23 MP, which is optimal.

Use Extension Magic twice on the bamboo of length 98 to obtain a bamboo of length 100. (MP consumed: 2)

Use Composition Magic on the bamboos of lengths 40, 30 to obtain a bamboo of length 70. (MP consumed: 10)

Use Shortening Magic once on the bamboo of length 21 to obtain a bamboo of length 20. (MP consumed: 1)

Use Composition Magic on the bamboo of length 70 obtained in step 2 and the bamboo of length 20 obtained in step 3 to obtain a bamboo of length 90. (MP consumed: 10)

Sample Input 2

8 100 90 80
100
100
90
90
90
80
80
80

Sample Output 2

0

If we already have all bamboos of the desired lengths, the amount of MP needed is 0. As seen here, we do not necessarily need to use all the bamboos.

Sample Input 3

8 1000 800 100
300
333
400
444
500
555
600
666

Sample Output 3

243

Solution in C# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.IO;
using System.Text.RegularExpressions;
using System.Text;
using System.Diagnostics;
using System.Globalization;
using static System.Math;

namespace A
{
	public class Program
	{
		// variables
		int N;
		int A, B, C;
		int[] l;

		void Run(string[] args)
		{
			// main program
			N = Int;
			A = Int;
			B = Int;
			C = Int;
			l = MakeArray(N, i => Int);
			PrintLine(Solve(0, 0, 0, 0));
		}

		// methods
		int Solve(int n, int a, int b, int c)
		{
			if (n == N)
			{
				if (0 == a || 0 == b || 0 == c) return 123456;

				return Abs(a - A) + Abs(b - B) + Abs(c - C);
			}

			var minMP = Solve(n+1, a, b, c);
			minMP = Min(minMP, (0 == a ? 0 : 10) + Solve(n + 1, a + l[n], b, c));
			minMP = Min(minMP, (0 == b ? 0 : 10) + Solve(n + 1, a, b + l[n], c));
			minMP = Min(minMP, (0 == c ? 0 : 10) + Solve(n + 1, a, b, c + l[n]));
			return minMP;
		}

		// utilities
		static void Main(string[] args)
		{
			new Program().Run(args);
#if DEBUG
			Console.Write("Press any key...");
			Console.ReadKey();
#endif
		}

		/// <summary>
		/// Make an array
		/// </summary>
		/// <typeparam name="T">type of element</typeparam>
		/// <param name="count">number of elements</param>
		/// <param name="input">element generator</param>
		/// <returns>array</returns>
		static T[] MakeArray<T>(int count, Func<int, T> input)
		{
			var array = new T[count];
			for (var i = 0; i < count; ++i)
			{
				array[i] = input(i);
			}
			return array;
		}
		/// <summary>
		/// Make a 2-dimensional array
		/// </summary>
		/// <typeparam name="T">type of element</typeparam>
		/// <param name="count1">size of first dimension</param>
		/// <param name="count2">size of second dimension</param>
		/// <param name="input">element generator</param>
		/// <returns>2-dimensional array</returns>
		static T[,] MakeArray<T>(int count1, int count2, Func<int, int, T> input)
		{
			var array = new T[count1, count2];
			for (var i = 0; i < count1; ++i)
			{
				for (var j = 0; j < count2; ++j)
				{
					array[i, j] = input(i, j);
				}
			}
			return array;
		}
		/// <summary>
		/// Make a 3-dimensional array
		/// </summary>
		/// <typeparam name="T">type of element</typeparam>
		/// <param name="count1">size of first dimension</param>
		/// <param name="count2">size of second dimension</param>
		/// <param name="count3">size of third dimension</param>
		/// <param name="input">element generator</param>
		/// <returns>3-dimensional array</returns>
		static T[,,] MakeArray<T>(int count1, int count2, int count3, Func<int, int, int, T> input)
		{
			var array = new T[count1, count2, count3];
			for (var i = 0; i < count1; ++i)
			{
				for (var j = 0; j < count2; ++j)
				{
					for (var k = 0; k < count3; ++k)
					{
						array[i, j, k] = input(i, j, k);
					}
				}
			}
			return array;
		}

		/// <summary>
		/// Output any value
		/// </summary>
		/// <typeparam name="T">type of value.</typeparam>
		/// <param name="value">value</param>
		[DebuggerNonUserCode]
		static void Print<T>(T value) => Console.Write(value);
		/// <summary>
		/// Output any value on a line
		/// </summary>
		/// <typeparam name="T">type of value</typeparam>
		/// <param name="value">value</param>
		[DebuggerNonUserCode]
		static void PrintLine<T>(T value) => Console.WriteLine(value);
		/// <summary>
		/// Output a line break
		/// </summary>
		[DebuggerNonUserCode]
		static void PrintLine() => Console.WriteLine();

		/// <summary>
		/// Input a word
		/// </summary>
		static string Word
		{
			[DebuggerNonUserCode]
			get
			{
				var builder = new StringBuilder();

				int key;
				while ((key = Console.Read()) != -1)
				{
					char c = (char)key;
					if (!char.IsWhiteSpace(c))
					{
						builder.Append(c);
						break;
					}
				}

				while ((key = Console.Read()) != -1)
				{
					char c = (char)key;
					if (char.IsWhiteSpace(c)) break;

					builder.Append(c);
				}

				return builder.ToString();
			}
		}
		/// <summary>
		/// Input a line
		/// </summary>
		static string Line
		{
			[DebuggerNonUserCode]
			get
			{
				return Console.ReadLine();
			}
		}
		/// <summary>
		/// Input bool
		/// </summary>
		static bool Bool
		{
			[DebuggerNonUserCode]
			get
			{
				return bool.Parse(Word);
			}
		}
		/// <summary>
		/// Input char
		/// </summary>
		static char Char
		{
			[DebuggerNonUserCode]
			get
			{
				return char.Parse(Word);
			}
		}
		/// <summary>
		/// Input byte
		/// </summary>
		static byte Byte
		{
			[DebuggerNonUserCode]
			get
			{
				return byte.Parse(Word);
			}
		}
		/// <summary>
		/// Input ushort
		/// </summary>
		static ushort UShort
		{
			[DebuggerNonUserCode]
			get
			{
				return ushort.Parse(Word);
			}
		}
		/// <summary>
		/// Input uint
		/// </summary>
		static uint UInt
		{
			[DebuggerNonUserCode]
			get
			{
				return uint.Parse(Word);
			}
		}
		/// <summary>
		/// Input ulong
		/// </summary>
		static ulong ULong
		{
			[DebuggerNonUserCode]
			get
			{
				return ulong.Parse(Word);
			}
		}
		/// <summary>
		/// Input sbyte
		/// </summary>
		static sbyte SByte
		{
			[DebuggerNonUserCode]
			get
			{
				return sbyte.Parse(Word);
			}
		}
		/// <summary>
		/// Input short
		/// </summary>
		static short Short
		{
			[DebuggerNonUserCode]
			get
			{
				return short.Parse(Word);
			}
		}
		/// <summary>
		/// Input int
		/// </summary>
		static int Int
		{
			[DebuggerNonUserCode]
			get
			{
				return int.Parse(Word);
			}
		}
		/// <summary>
		/// Input long
		/// </summary>
		static long Long
		{
			[DebuggerNonUserCode]
			get
			{
				return long.Parse(Word);
			}
		}
		/// <summary>
		/// Input decimal
		/// </summary>
		static decimal Decimal
		{
			[DebuggerNonUserCode]
			get
			{
				return decimal.Parse(Word);
			}
		}
		/// <summary>
		/// Input variable length integer
		/// </summary>
		static BigInteger Integer
		{
			[DebuggerNonUserCode]
			get
			{
				return BigInteger.Parse(Word);
			}
		}
		/// <summary>
		/// Input float
		/// </summary>
		static float Float
		{
			[DebuggerNonUserCode]
			get
			{
				return float.Parse(Word);
			}
		}
		/// <summary>
		/// Input double
		/// </summary>
		static double Double
		{
			[DebuggerNonUserCode]
			get
			{
				return double.Parse(Word);
			}
		}

		/// <summary>
		/// Input hex int
		/// </summary>
		static int Hex
		{
			[DebuggerNonUserCode]
			get
			{
				return int.Parse(Word, NumberStyles.HexNumber);
			}
		}
		/// <summary>
		/// Input hex int
		/// </summary>
		static int HexInt
		{
			[DebuggerNonUserCode]
			get
			{
				return Hex;
			}
		}
		/// <summary>
		/// Input hex long
		/// </summary>
		static long HexLong
		{
			[DebuggerNonUserCode]
			get
			{
				return long.Parse(Word, NumberStyles.HexNumber);
			}
		}
		/// <summary>
		/// Input variable length hex integer
		/// </summary>
		static BigInteger HexInteger
		{
			[DebuggerNonUserCode]
			get
			{
				return BigInteger.Parse(Word, NumberStyles.HexNumber);
			}
		}

		/// <summary>
		/// Calculate GCD
		/// </summary>
		/// <typeparam name="T">type of value</typeparam>
		/// <typeparam name="TOp">operator struct of value</typeparam>
		/// <param name="a">first value</param>
		/// <param name="b">second value</param>
		/// <returns>the GCD between <c>a</c> and <c>b</c></returns>
		static T Gcd<T, TOp>(T a, T b)
			where TOp : struct, IDividable<T>
		{
			T x, y;
			return Gcd<T, TOp>(a, b, out x, out y);
		}
		/// <summary>
		/// Calculate GCD
		/// </summary>
		/// <typeparam name="T">type of value</typeparam>
		/// <typeparam name="TOp">operator struct of value</typeparam>
		/// <param name="a">first value</param>
		/// <param name="b">second value</param>
		/// <param name="x">return the coefficient of <c>a</c></param>
		/// <param name="y">return the coefficient of <c>b</c></param>
		/// <returns>the GCD between <c>a</c> and <c>b</c></returns>
		static T Gcd<T, TOp>(T a, T b, out T x, out T y)
			where TOp : struct, IDividable<T>
		{
			T x1 = default(TOp).Zero;
			T x2 = default(TOp).One;
			T y1 = default(TOp).One;
			T y2 = default(TOp).Zero;
			T q = default(TOp).Zero;

			while (0 != default(TOp).Compare(b, default(TOp).Zero))
			{
				var tmp = x2;
				x2 = default(TOp).Subtract(x1, default(TOp).Multiply(q, x2));
				x1 = tmp;
				tmp = y2;
				y2 = default(TOp).Subtract(y2, default(TOp).Multiply(q, y2));
				y1 = tmp;
				q = default(TOp).Divide(a, b);
				tmp = b;
				b = default(TOp).Modulo(a, b);
				a = tmp;
			}

			x = x2;
			y = y2;
			return a;
		}

		/// <summary>
		/// Union-find tree
		/// </summary>
		class UnionFind : UnionFind<Unit, UnitOp>
		{
			/// <param name="count">number of elements</param>
			public UnionFind(int count) : base(count)
			{ }
		}
		/// <summary>
		/// Union-find tree
		/// </summary>
		class UnionFind<TPotential, TPotentialOp>
			where TPotentialOp : struct, IAddable<TPotential>
		{
			/// <summary>
			/// parent indices
			/// </summary>
			readonly int[] Parent;

			/// <summary>
			/// tree rank
			/// </summary>
			readonly int[] Rank;

			/// <summary>
			/// potential
			/// </summary>
			readonly TPotential[] DiffPotential;

			/// <param name="count">number of elements</param>
			public UnionFind(int count)
			{
				Parent = MakeArray(count, i => i);
				Rank = MakeArray(count, i => 0);
				DiffPotential = MakeArray(count, i => default(TPotentialOp).Zero);
			}

			/// <summary>
			/// Get root element
			/// </summary>
			/// <param name="x">element index</param>
			/// <returns>root element index of <c>x</c></returns>
			public int Root(int x)
			{
				if (Parent[x] == x)
				{
					return x;
				}
				else
				{
					var root = Root(Parent[x]);
					DiffPotential[x] = default(TPotentialOp).Add(DiffPotential[x], DiffPotential[Parent[x]]);
					return Parent[x] = root;
				}
			}

			/// <summary>
			/// Get whether two elements belong to the same set.
			/// </summary>
			/// <param name="x">first element index</param>
			/// <param name="y">second element index</param>
			/// <returns>whether two elements belong to the same set</returns>
			public bool Same(int x, int y)
				=> Root(x) == Root(y);


			/// <summary>
			/// Unite two elements
			/// </summary>
			/// <param name="x">first element index</param>
			/// <param name="y">second element index</param>
			public bool Unite(int x, int y)
				=> Unite(x, y, default(TPotentialOp).Zero);

			/// <summary>
			/// Unite two elements
			/// </summary>
			/// <param name="x">first element index</param>
			/// <param name="y">second element index</param>
			/// <param name="potential">potential from <c>x</c> to <c>y</c></param>
			public bool Unite(int x, int y, TPotential potential)
			{
				potential = default(TPotentialOp).Add(potential, Potential(x));
				potential = default(TPotentialOp).Subtract(potential, Potential(y));
				x = Root(x);
				y = Root(y);

				if (x == y) return false;

				if (Rank[x] < Rank[y])
				{
					// Join x to y
					Parent[x] = y;
					DiffPotential[x] = default(TPotentialOp).Negate(potential);
				}
				else
				{
					// Join y to x
					Parent[y] = x;
					if (Rank[x] == Rank[y]) ++Rank[x];
					DiffPotential[y] = potential;
				}
				return true;
			}

			/// <summary>
			/// Get potential difference from root.
			/// </summary>
			/// <param name="x">element index</param>
			/// <returns>potential difference between the element and the root</returns>
			public TPotential Potential(int x)
			{
				Root(x);
				return DiffPotential[x];
			}

			/// <summary>
			/// Get potential difference between two element.
			/// </summary>
			/// <param name="x">first element index</param>
			/// <param name="y">second element index</param>
			/// <returns>potential difference between two element</returns>
			public TPotential Diff(int x, int y)
				=> default(TPotentialOp).Subtract(Potential(y), Potential(x));
		}

		struct Unit
		{ }
		struct UnitOp : IDividable<Unit>
		{
			public Unit Zero => default(Unit);
			public Unit One => default(Unit);
			public Unit Negate(Unit x) => default(Unit);
			public Unit Add(Unit x, Unit y) => default(Unit);
			public Unit Multiply(Unit x, Unit y) => default(Unit);
			public Unit Divide(Unit x, Unit y) => default(Unit);
			public Unit Modulo(Unit x, Unit y) => default(Unit);
			public int Compare(Unit x, Unit y) => 0;
		}
		struct IntOp : IDividable<int>
		{
			public int Zero => 0;
			public int One => 1;
			public int Negate(int x) => -x;
			public int Add(int x, int y) => x + y;
			public int Multiply(int x, int y) => x * y;
			public int Divide(int x, int y) => x / y;
			public int Modulo(int x, int y) => x % y;
			public int Compare(int x, int y) => x.CompareTo(y);
		}
		struct LongOp : IDividable<long>
		{
			public long Zero => 0L;
			public long One => 1L;
			public long Negate(long x) => -x;
			public long Add(long x, long y) => x + y;
			public long Multiply(long x, long y) => x * y;
			public long Divide(long x, long y) => x / y;
			public long Modulo(long x, long y) => x % y;
			public int Compare(long x, long y) => x.CompareTo(y);
		}
		struct FloatOp : IDividable<float>
		{
			public float Zero => 0.0F;
			public float One => 1.0F;
			public float Negate(float x) => -x;
			public float Add(float x, float y) => x + y;
			public float Multiply(float x, float y) => x * y;
			public float Divide(float x, float y) => x / y;
			public float Modulo(float x, float y) => x % y;
			public int Compare(float x, float y) => x.CompareTo(y);
		}
		struct DoubleOp : IDividable<double>
		{
			public double Zero => 0.0;
			public double One => 1.0;
			public double Negate(double x) => -x;
			public double Add(double x, double y) => x + y;
			public double Multiply(double x, double y) => x * y;
			public double Divide(double x, double y) => x / y;
			public double Modulo(double x, double y) => x % y;
			public int Compare(double x, double y) => x.CompareTo(y);
		}
	}

	interface IDividable<T> : IMultipliable<T>
	{
		T Divide(T x, T y);
		T Modulo(T x, T y);
	}
	interface IMultipliable<T> : IAddable<T>
	{
		T One { get; }
		T Multiply(T x, T y);
	}
	interface IAddable<T> : IComparer<T>
	{
		T Zero { get; }
		T Negate(T x);
		T Add(T x, T y);
	}

	static class OperationExtensions
	{
		public static T Subtract<T, TOp>(this TOp op, T x, T y)
			where TOp : struct, IAddable<T>
		{
			return default(TOp).Add(x, default(TOp).Negate(y));
		}
		public static T Sum<T, TOp>(this TOp op, params T[] items)
			where TOp : struct, IAddable<T>
		{
			var sum = default(TOp).Zero;
			foreach (var item in items)
				sum = default(TOp).Add(sum, item);
			return sum;
		}
		public static T Product<T, TOp>(this TOp op, params T[] items)
			where TOp : struct, IMultipliable<T>
		{
			var sum = default(TOp).One;
			foreach (var item in items)
				sum = default(TOp).Multiply(sum, item);
			return sum;
		}
	}
}