Skip to content

Instantly share code, notes, and snippets.

@afishr
Last active September 20, 2022 08:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save afishr/a3d53483eaeb5b73cc5395eaca7c345a to your computer and use it in GitHub Desktop.
Save afishr/a3d53483eaeb5b73cc5395eaca7c345a to your computer and use it in GitHub Desktop.
#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