amcbn

Adunscad

Nov 9th, 2021
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.03 KB | None | 0 0
  1. /* [A][M][C][B][N] / [K][R][I][P][6][8] */
  2. #include <bits/stdc++.h>
  3. #include <unordered_map>
  4. using namespace std;
  5. typedef long long ll;
  6. const char sp = ' ', nl = '\n';
  7. const int MOD = 9973;
  8. ifstream fin("adunscad.in");
  9. ofstream fout("adunscad.out");
  10. vector<int> v, curr;
  11. int n, S, found(0);
  12. void back(int idx, int s = 0) {
  13.     if (!found) {
  14.         if (idx == n) {
  15.             if (s == S) {
  16.                 found = true;
  17.                 if (curr[0] == -1) fout << '-';
  18.                 fout << v[0];
  19.                 for (int i = 1; i < n; ++i)
  20.                     fout << (curr[i] < 0 ? '-' : '+') << v[i];
  21.             }
  22.  
  23.         }
  24.         else {
  25.             curr[idx] = 1;
  26.             back(idx + 1, s + v[idx]);
  27.             curr[idx] = -1;
  28.             back(idx + 1, s - v[idx]);
  29.         }
  30.     }
  31. }
  32.  
  33. int main() {
  34.     ios::sync_with_stdio(0);
  35.     cin.tie(0), cout.tie(0);
  36.     fin >> S >> n;
  37.     v.resize(n), curr.resize(n);
  38.     for (int i = 0; i < n; ++i)
  39.         fin >> v[i];
  40.     back(0);
  41.     if (!found) fout << 0;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment