summaryrefslogtreecommitdiff
path: root/introductory_problems/apple_division.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'introductory_problems/apple_division.cpp')
-rw-r--r--introductory_problems/apple_division.cpp23
1 files changed, 23 insertions, 0 deletions
diff --git a/introductory_problems/apple_division.cpp b/introductory_problems/apple_division.cpp
new file mode 100644
index 0000000..11a0450
--- /dev/null
+++ b/introductory_problems/apple_division.cpp
@@ -0,0 +1,23 @@
+// 1623. Apple Division
+#include <bits/stdc++.h>
+using namespace std;
+
+int main() {
+ const long long max = INT_MAX;
+ static int n, p[20];
+ long long a, b, best = max;
+ scanf("%d", &n);
+ for (int i = 0; i < n; i++)
+ scanf("%d", &p[i]);
+ for (int i = 0; i < (1 << n); i++) {
+ a = b = 0;
+ for (int j = 0; j < n; j++) {
+ if (i & (1 << j))
+ a += p[j];
+ else
+ b += p[j];
+ }
+ best = min(best, abs(a - b));
+ }
+ printf("%lld\n", best);
+}