Advertisement
VisualPaul

Hell on the markets

Nov 30th, 2014
470
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.89 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int maxn = 100000;
  5.  
  6. int a[maxn];
  7. int s[maxn + 1];
  8. int b[maxn];
  9. int n;
  10.  
  11. void find_result(int pos, int target) {
  12.     if (pos == -1) {
  13.         assert(target == 0);
  14.         return;
  15.     }
  16.     if (s[pos] >= abs(target - a[pos])) {
  17.         b[pos] =  1;
  18.         find_result(pos - 1, target - a[pos]);
  19.     } else {
  20.         b[pos] = -1;
  21.         find_result(pos - 1, target + a[pos]);
  22.     }
  23. }
  24.  
  25. int main() {
  26.     ios_base::sync_with_stdio(false);
  27.     ifstream in("hell.in");
  28.     ofstream out("hell.out");
  29.     in >> n;
  30.     for (int i = 0; i < n; ++i) {
  31.         in >> a[i];
  32.     }
  33.     partial_sum(a, a + n, s + 1);
  34.     if (accumulate(a, a + n, 0) % 2 == 0) {
  35.         find_result(n - 1, 0);
  36.         out << "Yes\n";
  37.         for (int i = 0; i < n; ++i) {
  38.             out << b[i] << ' ';
  39.         }
  40.     } else {
  41.         out << "No\n";
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement