Problem p00182 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
const ld eps = 1e-9;

//// < "d:\d_download\visual studio
///2015\projects\programing_contest_c++\debug\a.txt" > "d:\d_download\visual
///studio 2015\projects\programing_contest_c++\debug\b.txt"

bool getans(vector<int> &used, vector<int> &vs, int rest) {
  if (rest) {
    bool flag = false;
    for (int i = 0; i < used.size(); ++i) {
      if (used[i] == 0) {
        used[i] = 2;
        if (rest >= vs[i]) {

          if (getans(used, vs, rest - vs[i])) {
            used[i] = 0;
            return true;
          }
        }
        used[i] = 0;
        // if (!flag)return false;
      } else if (used[i] == 2) {
        flag = true;
      }
    }
    return false;
  } else {
    if (all_of(used.begin(), used.end(), [](const int n) { return n; })) {
      return true;
    }
    bool flag = false;
    for (int i = 0; i < used.size(); ++i) {
      if (used[i] == 2) {
        used[i] = 1;
        bool ok = getans(used, vs, vs[i]);
        used[i] = 2;
        if (ok)
          return true;
      } else if (used[i] == 0) {
        return false;
      }
    }
    return false;
  }
}
int main() {
  while (1) {
    int N;
    cin >> N;
    if (!N)
      break;
    vector<int> vs(N), used(N);
    for (int i = 0; i < N; ++i) {
      cin >> vs[i];
    }
    sort(vs.begin(), vs.end(), greater<int>());
    used[0] = 2;
    bool ans = getans(used, vs, 0);
    if (ans)
      cout << "YES" << endl;
    else
      cout << "NO" << endl;
  }
  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
const ld eps = 1e-9;

//// < "d:\d_download\visual studio
///2015\projects\programing_contest_c++\debug\a.txt" > "d:\d_download\visual
///studio 2015\projects\programing_contest_c++\debug\b.txt"

bool getans(vector<int> &used, vector<int> &vs, int rest) {
  if (rest) {
    bool flag = false;
    for (int i = 0; i < used.size(); ++i) {
      if (used[i] == 0) {
        used[i] = 2;
        if (rest >= vs[i]) {

          if (getans(used, vs, rest - vs[i])) {
            used[i] = 0;
            return true;
          }
        }
        used[i] = 0;
        if (!flag)
          return false;
      } else if (used[i] == 2) {
        flag = true;
      }
    }
    return false;
  } else {
    if (all_of(used.begin(), used.end(), [](const int n) { return n; })) {
      return true;
    }
    bool flag = false;
    for (int i = 0; i < used.size(); ++i) {
      if (used[i] == 2) {
        used[i] = 1;
        bool ok = getans(used, vs, vs[i]);
        used[i] = 2;
        if (ok)
          return true;
      } else if (used[i] == 0) {
        return false;
      }
    }
    return false;
  }
}
int main() {
  while (1) {
    int N;
    cin >> N;
    if (!N)
      break;
    vector<int> vs(N), used(N);
    for (int i = 0; i < N; ++i) {
      cin >> vs[i];
    }
    sort(vs.begin(), vs.end(), greater<int>());
    used[0] = 2;
    bool ans = getans(used, vs, 0);
    if (ans)
      cout << "YES" << endl;
    else
      cout << "NO" << endl;
  }
  return 0;
}