Problem:
Score : 200 points

Problem Statement

Takahashi has N days of summer vacation.

His teacher gave him M summer assignments. It will take A_i days for him to do the i-th assignment.

He cannot do multiple assignments on the same day, or hang out on a day he does an assignment.

What is the maximum number of days Takahashi can hang out during the vacation if he finishes all the assignments during this vacation?

If Takahashi cannot finish all the assignments during the vacation, print -1 instead.

Constraints

1 \leq N \leq 10^6

1 \leq M \leq 10^4

1 \leq A_i \leq 10^4

Input

Input is given from Standard Input in the following format:

N M
A_1 ... A_M

Output

Print the maximum number of days Takahashi can hang out during the vacation, or -1.

Sample Input 1

41 2
5 6

Sample Output 1

30

For example, he can do the first assignment on the first 5 days, hang out on the next 30 days, and do the second assignment on the last 6 days of the vacation. In this way, he can safely spend 30 days hanging out.

Sample Input 2

10 2
5 6

Sample Output 2

-1

He cannot finish his assignments.

Sample Input 3

11 2
5 6

Sample Output 3

0

He can finish his assignments, but he will have no time to hang out.

Sample Input 4

314 15
9 26 5 35 8 9 79 3 23 8 46 2 6 43 3

Sample Output 4

9

Solution in C# (.NET Core 3.1.201) (judge verdict: Accepted)

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using static System.Console;

namespace AtCoder
{
	public static class Program
	{
		public static void Main(string[] args)
		{
			var n = ReadLine().SplitToIntList()[0];
			ReadLine()
				.SplitToIntList()
				.Sum()
				.Chain(a => n - a)
				.Chain(a => a < 0 ? -1 : a)
				.Chain(WriteLine);
		}

		#region Utilities

		public static T ToVariable<T>(this T source, out T variable)
		{
			variable = source;
			return source;
		}

		public static IEnumerable<IList<T>> Buffer<T>(
			this IEnumerable<T> source,
			int count)
		{
			return Buffer(source, count, count);
		}

		public static IEnumerable<IList<T>> Buffer<T>(
			this IEnumerable<T> source,
			int count,
			int skip)
		{
			if (source == null)
				throw new ArgumentNullException(nameof(source));
			if (count < 1)
				throw new ArgumentOutOfRangeException(nameof(count));
			if (skip < 1)
				throw new ArgumentOutOfRangeException(nameof(skip));

			var queue = new Queue<List<T>>();
			int step = skip;
			foreach (var item in source)
			{
				if (step == skip)
				{
					step = 0;
					queue.Enqueue(new List<T>(count));
				}
				foreach (var list in queue)
					list.Add(item);
				while (queue.Count > 0 && queue.Peek().Count == count)
					yield return queue.Dequeue();
				step++;
			}
			while (queue.Count > 0) yield return queue.Dequeue();
		}

		public static IEnumerable<IEnumerable<T>> Combination<T>(
			this IEnumerable<T> source,
			int count)
		{
			var stack = new Stack<List<T>[]>();
			stack.Push(new[]
			{
				new List<T>(),
				source.ToList()
			});
			while (stack.Count > 0)
			{
				var item = stack.Pop();
				if (item[0].Count() == count)
				{
					yield return item[0];
				}
				else
				{
					var list = item[1];
					list
						.Select((a, i) => new[]
						{
							item[0].Concat(new[]{ a }).ToList(),
							list.Skip(i + 1).ToList(),
						})
						.TakeWhile(a => a[0].Count + a[1].Count >= count)
						.ForEach(a => stack.Push(a));
				}
			}
		}

		public static IEnumerable<IEnumerable<T>> Permutation<T>(
			this IEnumerable<T> source,
			int count)
		{
			var stack = new Stack<IEnumerable<T>[]>();
			stack.Push(new[]
			{
				Enumerable.Empty<T>(),
				source.ToList()
			});
			while (stack.Count > 0)
			{
				var item = stack.Pop();
				if (item[0].Count() == count)
				{
					yield return item[0];
				}
				else
				{
					var list = item[1];
					list
						.Select((a, i) => new[]
						{
							item[0].Concat(new[]{ a }),
							list.Where((b, j) => j != i),
						})
						.ForEach(a => stack.Push(a));
				}
			}
		}

		public static IEnumerable<string> ReadAllLines()
		{
			return Repeat(ReadLine)
				.TakeWhile(a => a != null);
		}

		public static List<int> SplitToIntList(this string source)
		{
			return source.Split().Select(a => int.Parse(a)).ToList();
		}

		public static List<double> SplitToDoubleList(this string source)
		{
			return source.Split().Select(a => double.Parse(a)).ToList();
		}

		public static IEnumerable<T> Return<T>(params T[] source)
		{
			return source;
		}

		public static string JoinWithSpace<T>(this IEnumerable<T> source)
		{
			return string.Join(" ", source);
		}

		public static IEnumerable<T> Repeat<T>(this T source)
		{
			return Repeat(() => source);
		}

		public static IEnumerable<T> Repeat<T>(Func<T> selector)
		{
			while (true) yield return selector();
		}

		public static IEnumerable<T> Recur<T>(
			this T source,
			Func<T, T> selector)
		{
			var p = source;
			while (true)
			{
				yield return p;
				p = selector(p);
			}
		}

		public static IEnumerable<T> Scan<T>(
			this IEnumerable<T> source,
			Func<T, T, T> selector)
		{
			return Scan(source, default(T), selector);
		}

		public static IEnumerable<T> Scan<T>(
			this IEnumerable<T> source,
			T seed,
			Func<T, T, T> selector)
		{
			var last = seed;
			foreach (var item in source)
			{
				last = selector(last, item);
				yield return last;
			}
		}

		public static void ForEach<T>(
			this IEnumerable<T> source,
			Action<T> unlessEmpty,
			Action ifEmpty = null)
		{
			using (var e = source.GetEnumerator())
			{
				if (e.MoveNext())
				{
					do unlessEmpty?.Invoke(e.Current);
					while (e.MoveNext());
				}
				else
				{
					ifEmpty?.Invoke();
				}
			}
		}

		public static TResult Chain<TSource, TResult>(
			this TSource source,
			Func<TSource, TResult> selector)
		{
			return selector(source);
		}

		public static T Chain<T>(
			this T source,
			Action<T> action)
		{
			action(source);
			return source;
		}

		public static IEnumerable<T> Do<T>(
			this IEnumerable<T> source,
			Action<T> action)
		{
			foreach (var a in source)
			{
				action(a);
				yield return a;
			}
		}

		#endregion

		#region D2

		class D2<T> : IEnumerable<Location>
		{
			public D2(int width, int height, IEnumerable<T> items = null)
			{
				Width = width;
				Height = height;
				var size = Width * Height;
				Items = items?.ToArray() ?? new T[size];
				if (Items.Length != size) throw new Exception();
			}

			public int Width { get; private set; }
			public int Height { get; private set; }

			public T[] Items { get; private set; }

			public T this[int index]
			{
				get { return Items[index]; }
				set { Items[index] = value; }
			}

			public T this[int x, int y]
			{
				get { return Items[y * Width + x]; }
				set { Items[y * Width + x] = value; }
			}

			public T this[Location location]
			{
				get { return this[location.X, location.Y]; }
				set { this[location.X, location.Y] = value; }
			}

			public bool Contains(Location location)
			{
				return 0 <= location.X
					&& 0 <= location.Y
					&& location.X < Width
					&& location.Y < Height;
			}

			public D2<T> Clone()
			{
				return new D2<T>(Width, Height, Items);
			}

			public IEnumerable<IEnumerable<Location>> EnumerateVertical()
			{
				return Enumerable
					.Range(0, Height)
					.Select(y => Enumerable
						.Range(0, Width)
						.Select(x => new Location(x, y)));
			}

			public IEnumerable<IEnumerable<Location>> EnumerateHorizontal()
			{
				return Enumerable
					.Range(0, Width)
					.Select(x => Enumerable
						.Range(0, Height)
						.Select(y => new Location(x, y)));
			}

			public IEnumerator<Location> GetEnumerator()
			{
				foreach (var item in EnumerateVertical().SelectMany(a => a))
				{
					yield return item;
				}
			}

			IEnumerator IEnumerable.GetEnumerator()
			{
				return GetEnumerator();
			}
		}

		#endregion

		#region Location

		struct Location
		{
			public Location(int x, int y)
			{
				X = x;
				Y = y;
			}

			public int X { get; private set; }
			public int Y { get; private set; }

			public override bool Equals(object obj)
			{
				if (!(obj is Location)) return false;
				var other = (Location)obj;
				return other != null &&
					   X == other.X &&
					   Y == other.Y;
			}

			public override int GetHashCode()
			{
				return X << 16 | Y | X >> 16;
			}

			public static bool operator ==(Location left, Location right)
			{
				return EqualityComparer<Location>.Default.Equals(left, right);
			}

			public static bool operator !=(Location left, Location right)
			{
				return !(left == right);
			}

			public static Location operator +(Location left, Location right)
			{
				return new Location(left.X + right.X, left.Y + right.Y);
			}

			public static Location operator -(Location left, Location right)
			{
				return new Location(left.X - right.X, left.Y - right.Y);
			}
		}

		#endregion
	}
}