Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using System.Collections.Generic;
- class ThreeFriends
- {
- static void Main()
- {
- int n = int.Parse(Console.ReadLine());
- for (int i = 0; i < n; i++)
- {
- int[] nums = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
- Console.WriteLine(Check3Partitions(nums) ? "Yes" : "No");
- }
- }
- static bool Check3Partitions(int[] nums)
- {
- int totalSum = nums.Sum();
- if (totalSum % 3 != 0)
- {
- return false;
- }
- int targetSum = totalSum / 3;
- var sumReached = new bool[targetSum + 1, targetSum + 1];
- sumReached[0, 0] = true;
- foreach (short num in nums)
- {
- for (int s1 = targetSum; s1 >= 0; s1--)
- {
- for (int s2 = targetSum; s2 >= 0; s2--)
- {
- if (sumReached[s1, s2])
- {
- if (s1 + num <= targetSum && !sumReached[s1 + num, s2])
- {
- sumReached[s1 + num, s2] = true;
- }
- if (s2 + num <= targetSum && !sumReached[s1, s2 + num])
- {
- sumReached[s1, s2 + num] = true;
- }
- }
- }
- }
- }
- bool possible = sumReached[targetSum, targetSum];
- return possible;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement