Advertisement
Josif_tepe

Untitled

Mar 30th, 2022
1,009
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.61 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. #include <cmath>
  4. using namespace std;
  5.  
  6. int arr[305];
  7. int main()
  8. {
  9.     int n;
  10.     cin >> n;
  11.     for(int i = 0; i < n; i++) {
  12.         cin >> arr[i];
  13.     }
  14.     int razlika = 2e9;
  15.     for(int bitmask = 0; bitmask < (1 << n); bitmask++) {
  16.         int sum1 = 0, sum2 = 0;
  17.         for(int i = 0; i < n; i++) {
  18.             if(bitmask & (1 << i)) {
  19.                 sum1 += arr[i];
  20.             }
  21.             else {
  22.                 sum2 += arr[i];
  23.             }
  24.         }
  25.         razlika = min(razlika, abs(sum1 - sum2));
  26.     }
  27.     cout << razlika << endl;
  28.     return 0;
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement