Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define d(x) cout << #x << " = " << (x) << endl;
- #define fr freopen("in.txt", "r", stdin);
- #define fw freopen("out.txt", "w", stdout);
- #define mem(x) memset((x), 0, sizeof((x)));
- #define pb push_back
- #define LL long long
- #define fastIO ios_base::sync_with_stdio(false)
- #define sf scanf
- #define pf printf
- #define SQR(x) ((x)*(x))
- #define sc1(x) scanf("%d", &x)
- #define sc2(x, y) scanf("%d %d", &x, &y)
- #define sc3(x, y, z) scanf("%d %d %d", &x, &y, &z)
- #define FOR(i, x, y) for(int i=int(x); i<int(y); i++)
- #define ROF(i, x, y) for(int i=int(x-1); i>=int(y); i--)
- #define all(c) (c.begin(), c.end())
- #define unq(v) sort(all(v)), (v).erase(unique(all(v)),v.end())
- #define siz 100000
- #define EPSILON (1.0E-8)
- int n;
- int whole_sum = 0;
- int mini = INT_MAX;
- int ara[siz];
- int ans[siz];
- int used[siz];
- int bt(int pos, int j, int x){
- if(pos == x){
- int sum = 0;
- for(int i = 0; i < x; i++) sum += ans[i];
- int temp = abs((whole_sum-sum)-sum);
- if(temp < mini) mini = temp;
- // for(int i = 0; i < x; i++) cout << ans[i] << " ";
- // cout << endl;
- return 0;
- }
- for(int i = j; i < n; i++){
- if(used[i] == 0){
- used[i] = 1;
- ans[pos] = ara[i];
- bt(pos+1, i, x);
- used[i] = 0;
- }
- }
- }
- int main(){
- #ifndef ONLINE_JUDGE
- clock_t tStart = clock();
- freopen("in.txt", "r", stdin);
- freopen("out.txt", "w", stdout);
- #endif
- cin >> n;
- for(int i = 0; i < n; i++){
- cin >> ara[i];
- whole_sum += ara[i];
- }
- for(int i = 1; i <= n; i++){
- mem(ans);
- mem(used);
- bt(0, 0, i);
- }
- cout << mini << endl;
- #ifndef ONLINE_JUDGE
- printf("\n>>Time taken: %.10fs\n", (double) (clock() - tStart) / CLOCKS_PER_SEC);
- #endif
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement