Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Online C++ compiler to run C++ program online
- #include <bits/stdc++.h>
- using namespace std;
- bool func(vector<int> & arr, int n, int k){
- vector<bool> prev(k+1, 0), cur(k+1, 0);
- prev[0] = cur[0] = true;
- if(arr[0] <= k) prev[arr[0]] = true;
- for(int ind=1; ind<n; ind++){
- for(int target=1; target<=k; target++){
- bool notTake = prev[target];
- bool take = false;
- if(arr[ind] <= target) take = prev[target-arr[ind]];
- cur[target] = take | notTake;
- }
- prev = cur;
- }
- return prev[k];
- }
- int main() {
- // Write C++ code here
- int n, sum{0};
- cin>>n;
- vector<int> v(n);
- for(int i = 0; i < n; i++){
- cin>>v[i];
- sum += v[i];
- }
- if(sum%2) { cout<<"NO"; return 0; }
- if(func(v, n, sum/2)){
- cout<<"YES";
- }
- else{
- cout<<"NO";
- }
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment