Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#include <bits/stdc++.h>
using namespace std;
bool b[18];
int n;
void get(int a,int n)
{
for ( int i= 0; i < n; i ++)
{
if (a%2==0) b[i]=false;
else b[i]=true;
a/=2;
}
}
int main ()
{
int n,a[18],dn;
__int64 s1=0,s2=0,dx,dk;
cin >> n;
for ( int i = 0; i < n; i++)
{
cin >> a[i];
s1+=a[i];
}
dx=abs(s1-s2);
dn=(1<<n);
s1=0;
for (int i = 1; i < dn; i++)
{
get(i,n);
s1=0;s2=0;
for (int j=0;j<n;j++)
{
if (b[j]) s2=s2+a[j];
else s1=s1+a[j];
}
dk=abs(s1-s2);
dx=min(dx,dk);
}
cout<<dx;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.