cses

solution to cses exercise problems
git clone git://git.superpozycja.net/cses
Log | Files | Refs | README

apple_division.cpp (738B)


      1 #include <bits/stdc++.h>
      2 
      3 using namespace std;
      4 
      5 using ui = unsigned int;
      6 using l = long;
      7 using ul = unsigned long;
      8 using ll = long long;
      9 using ull = unsigned long long;
     10 
     11 using vi = vector<int>;
     12 using vui = vector<ui>;
     13 using vl = vector<l>;
     14 using vul = vector<ul>;
     15 using vll = vector<ll>;
     16 using vull = vector<ull>;
     17 
     18 vll res;
     19 
     20 void solve()
     21 {
     22 	int n;
     23 	cin >> n;
     24 	vi a;
     25 	ll s = 0;
     26 	for (int i = 0; i < n; i++) {
     27 		l tmp;
     28 		cin >> tmp;
     29 		s += tmp;
     30 		a.push_back(tmp);
     31 	}
     32 	ll res = s;
     33 
     34 	for (int i = 0; i < 1 << n; i++) {
     35 		ll s2 = s;
     36 		int j = 0;
     37 		int nn = i;
     38 		while (nn) {
     39 			s2 -= a[j] * (nn % 2);
     40 			nn /= 2;
     41 			j++;
     42 		}
     43 
     44 		ll diff = abs(s - s2 - s2);
     45 		res = diff < res ? diff : res;
     46 	}
     47 	cout << res << "\n";
     48 }
     49 
     50 int main()
     51 {
     52 	solve();
     53 }