-
Notifications
You must be signed in to change notification settings - Fork 0
/
Apple Division.cpp
108 lines (100 loc) · 2.36 KB
/
Apple Division.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define read freopen("input.txt", "r", stdin)
#define write freopen("output.txt", "w", stdout)
#define pb push_back
#define ll long long
#define ull unsigned long long
#define ld long double
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define mpi map<int, int>
#define si set<int>
#define vt vector
#define getline cout << '\n'
#define all(x) x.begin(), x.end()
#define FOR(i, a, b) for (long long int i = a; (b - i) * (a - i) <= 0 && (b > a ? b != i : b != a); b > a ? i++ : i--)
#define FOREACHR(it, m) for (auto it = m.rbegin(); it != m.rend(); it++)
#define FOREACH(it, m) for (auto it = m.begin(); it != m.end(); it++)
// #define FOREACH(it, m) for (auto it : m)
// #define IOS ios_base::sync_with_stdio(false)
#define scanset(s, n) \
FOR(i, 0, n) \
{ \
int x; \
cin >> x; \
s.insert(x); \
}
template <class T, size_t size>
// scan array
void scan(T (&array)[size])
{
for (size_t i = 0; i < size; ++i)
cin >> array[i];
}
template <class T, size_t size>
// print array
void print(const T (&array)[size])
{
for (size_t i = 0; i < size; ++i)
cout << array[i] << " ";
}
template <class T, size_t size>
void println(const T (&array)[size])
{
print(array);
cout << endl;
}
template <class T>
// scan vector with size
void scan(T &x)
{
for (size_t i = 0; i < x.size(); i++)
{
cin >> x[i];
}
}
template <class T>
// print vector
void print(const T &t)
{
for (size_t i = 0; i < t.size(); i++)
cout << t[i] << endl;
}
template <class T>
void println(const T &t)
{
print(t);
cout << endl;
}
template <class T>
// print vector, set, ...
void __print_collection(T const &container)
{
for (auto pos : container)
cout << pos << " ";
}
long long int recursion(long long int sum, long long int left, int n, int a[])
{
if (n == 0)
return abs(left - (sum - left));
return min(recursion(sum, left + a[n - 1], n - 1, a), recursion(sum, left, n - 1, a));
}
int main()
{
int n;
cin >> n;
int a[n];
long long int sum = 0;
FOR(i, 0, n)
{
cin >> a[i];
sum += a[i];
}
long long int left = 0;
cout << recursion(sum, left, n, a);
}