Advertisement
Guest User

Sums

a guest
Jan 18th, 2018
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.10 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Linq;
  4.  
  5. public class Program
  6. {
  7.     public static void Main()
  8.     {
  9.         int[] numbers = Console.ReadLine()
  10.             .Split()
  11.             .Select(a => Math.Abs(int.Parse(a)))
  12.             .Where(n => n != 0)
  13.             .ToArray();
  14.  
  15.         int max = numbers.Sum();
  16.  
  17.         bool result = max >= 13;
  18.         int total = max * 2 + 1;
  19.         BitArray prev = new BitArray(total);
  20.         BitArray current = new BitArray(total);
  21.         prev[max] = true;// zero
  22.         foreach (var currentNum in numbers)
  23.         {
  24.             current.SetAll(false);
  25.             for (int i = 0; i < prev.Length; i++)
  26.             {
  27.                 if (prev.Get(i))
  28.                 {
  29.                     current.Set(i - currentNum, true);
  30.                     current.Set(i + currentNum, true);
  31.                 }
  32.             }
  33.  
  34.             prev.SetAll(false);
  35.             prev.Or(current);
  36.         }
  37.  
  38.         result = result && prev.Get(max + 13); // short circuits to false if sum less than 13
  39.         Console.WriteLine(result ? "Yes" : "No");
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement