Advertisement
Diogo03

Subconjuntos de mesma soma

Nov 14th, 2024 (edited)
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.88 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4.  
  5. bool solve(const vector<int>& arr) {
  6.    
  7.     unordered_map<int, int> sum_counts;
  8.     sum_counts[0] = 1;
  9.  
  10.     for (int num : arr) {
  11.         unordered_map<int, int> current_sums = sum_counts;
  12.         for (auto [sum, count] : current_sums) {
  13.             int new_sum = sum + num;
  14.             sum_counts[new_sum]++;
  15.             if (sum_counts[new_sum] > 1) {
  16.                 return true;
  17.             }
  18.         }
  19.     }
  20.     return false;
  21.    
  22. }
  23.  
  24. signed main() {
  25.     ios_base::sync_with_stdio(false);
  26.     cin.tie(nullptr);
  27.  
  28.     int n;
  29.     cin >> n;
  30.  
  31.     if(n > 18) {
  32.         cout << "S\n";
  33.         return 0;
  34.     }
  35.     vector<int> arr(n);
  36.     for (int i = 0; i < n; i++) cin >> arr[i];
  37.  
  38.     if (solve(arr)) {
  39.         cout << "S\n";
  40.     } else {
  41.         cout << "N\n";
  42.     }
  43.     return 0;
  44. }
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement