1472B - Fair Division - CodeForces Solution


dp greedy math *800

Please click on ads to support us..

Python Code:

t = int(input())
for turn in range(t):
    n = int(input())
    a = list(map(int, input().split()))

    c1 = a.count(1)
    c2 = a.count(2)


    if n == 1:
        print("NO")

    elif c1 % 2 == 0 and c2 % 2 == 0:
        print("YES")
    elif c1 % 2 == 0 and c2 == 1:
        print("YES")

    elif c1 % 2 == 0 and c1 != 0:
        print("YES")
    elif c1 == 0 and c2 % 2 == 0:
        print("YES")
    elif c1 % 2 == 0 and c2 == 0:
        print("YES")
    else:
        print("NO")

C++ Code:

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e5+5;

int a[N];

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        for(int i=0;i<n;i++)
          cin>>a[i];
        sort(a,a+n);
        int sum1=0,sum2=0;
        for(int i=n-1;i>=0;i--){
            if(sum1<sum2)
              sum1+=a[i];
            else
              sum2+=a[i];
        }
        if(sum1!=sum2)
           cout<<"NO\n";
        else
           cout<<"YES\n";
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

1044. Longest Duplicate Substring
1032. Stream of Characters
987. Vertical Order Traversal of a Binary Tree
952. Largest Component Size by Common Factor
212. Word Search II
174. Dungeon Game
127. Word Ladder
123. Best Time to Buy and Sell Stock III
85. Maximal Rectangle
84. Largest Rectangle in Histogram
60. Permutation Sequence
42. Trapping Rain Water
32. Longest Valid Parentheses
Cutting a material
Bubble Sort
Number of triangles
AND path in a binary tree
Factorial equations
Removal of vertices
Happy segments
Cyclic shifts
Zoos
Build a graph
Almost correct bracket sequence
Count of integers
Differences of the permutations
Doctor's Secret
Back to School
I am Easy
Teddy and Tweety