Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace DividingPresents
- {
- class Program
- {
- static void Main(string[] args)
- {
- int[] input = Console.ReadLine()
- .Split(" ")
- .Select(int.Parse)
- .ToArray();
- int[] presents = input
- .OrderBy(e => e)
- .ToArray();
- List<int> firstHalfe = new List<int>();
- List<int> secondHalfe = new List<int>();
- int firstSum = 0;
- int secondSum = 0;
- for (int present = presents.Length - 1; present >= 0; present--)
- {
- if (firstSum <= secondSum)
- {
- firstSum += presents[present];
- firstHalfe.Add(presents[present]);
- }
- else
- {
- secondSum += presents[present];
- secondHalfe.Add(presents[present]);
- }
- }
- int alan = 0;
- int bob = 0;
- List<int> alans;
- List<int> bobs;
- if (firstSum <= secondSum)
- {
- alan = firstSum;
- bob = secondSum;
- alans = RestoreOrder(firstHalfe, input);
- bobs = secondHalfe;
- }
- else
- {
- alan = secondSum;
- bob = firstSum;
- alans = RestoreOrder(secondHalfe, input);
- bobs = firstHalfe;
- }
- StringBuilder stringBuilder = new StringBuilder();
- stringBuilder.AppendLine($"Difference: {Math.Abs(firstSum - secondSum)}");
- stringBuilder.AppendLine($"Alan:{alan} Bob:{bob}");
- stringBuilder.AppendLine($"Alan takes: {string.Join(" ", alans)}");
- stringBuilder.AppendLine("Bob takes the rest.");
- Console.WriteLine(stringBuilder.ToString().TrimEnd());
- }
- private static List<int> RestoreOrder(List<int> sorted, int[] input)
- {
- List<int> originalOrder = new List<int>();
- foreach (int item in input)
- {
- if (sorted.Contains(item))
- {
- originalOrder.Add(item);
- sorted.Remove(item);
- }
- }
- originalOrder.Reverse();
- return originalOrder;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement