Problem p02692 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

typedef unsigned long long ull;
typedef long long ll;

ull N, A, B, C;
vector<string> proc;

vector<char> path;

string follow_path_one(vector<char> &path) {
  string ans;

  ans = "No";
  int i;
  for (i = 0; i < N; i++) {
    if (proc.at(i) == "AB") {
      if (A == 0 && B == 0) {
        break;
      } else if (A == 1) {
        A--, B++;
        path.push_back('B');
      } else {
        A++, B--;
        path.push_back('A');
      }
    } else if (proc.at(i) == "BC") {
      if (B == 0 && C == 0) {
        break;
      } else if (B == 1) {
        B--, C++;
        path.push_back('C');
      } else {
        B++, C--;
        path.push_back('B');
      }
    } else { // "AC"
      if (A == 0 && C == 0) {
        break;
      } else if (A == 1) {
        A--, C++;
        path.push_back('C');
      } else {
        A++, C--;
        path.push_back('A');
      }
    }
  }
  if (i == N)
    ans = "Yes";

  return ans;
}

void check_and_setPath(int i, ull &a, ull &b, ull c, string cond_bc, char for_b,
                       string cond_ac, char for_a, vector<char> &path) {

  if (a == 1 && b == 1 && c == 0 && i < N - 1) {
    if (proc.at(i + 1) == cond_bc) {
      a--, b++;
      path.push_back(for_b);

      return;
    } else if (proc.at(i + 1) == cond_ac) {
      a++, b--;
      path.push_back(for_a);
      return;
    }
  }

  if (a >= b) {
    a--, b++;
    path.push_back(for_b);
    return;
  } else {
    a++, b--;
    path.push_back(for_a);
    return;
  }
}

string follow_path(vector<char> &path) {
  string ans;

  ans = "No";
  int i;

  for (i = 0; i < N; i++) {
    if (proc.at(i) == "AB") {
      if (A == 0 && B == 0) {
        break;
      }
      check_and_setPath(i, A, B, C, "BC", 'B', "AC", 'A', path);
    } else if (proc.at(i) == "BC") {
      if (B == 0 && C == 0) {
        break;
      }
      check_and_setPath(i, B, C, A, "AC", 'C', "AB", 'B', path);
    } else { // "AC"
      if (C == 0 && A == 0) {
        break;
      }
      check_and_setPath(i, C, A, B, "AB", 'A', "BC", 'C', path);
    }
  }
  if (i == N) {
    ans = "Yes";
  }

  return ans;
}

int main() {
  ll i;
  string S;

  cin >> N >> A >> B >> C;

  for (int i = 0; i < N; i++) {
    cin >> S;
    proc.push_back(S);
  }

  vector<char> path;

  string ans;
  ;
  if (A + B + C == 0) {
    ans = "No";
  } else if (A + B + C == 1) {
    ans = follow_path_one(path);
  } else {
    ans = follow_path(path);
  }

  cout << ans << endl;
  if (ans == "Yes") {
    for (auto i : path)
      cout << i << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

typedef unsigned long long ull;
typedef long long ll;

ull N, A, B, C;
vector<int> proc;

vector<char> path;

vector<char> get_path(ull pos, ull a, ull b, ull c, vector<char> old_path) {
  if (pos == N) {
    return old_path;
  }

  vector<char> ans;
  switch (proc.at(pos)) {
  case 1: // AB
    // A->B
    if (a == 0 and b == 0)
      return ans;

    if (a > 0) {
      vector<char> new_path = old_path;
      new_path.push_back('B');
      ans = get_path(pos + 1, a - 1, b + 1, c, new_path);
      if (not ans.empty()) {
        return ans;
      }
    }

    // B->A
    if (b > 0) {
      vector<char> new_path = old_path;
      new_path.push_back('A');
      ans = get_path(pos + 1, a + 1, b - 1, c, new_path);
      if (not ans.empty()) {
        return ans;
      }
    }
    ans.clear();
    return ans;

  case 2: // AC
    // A->C
    if (a == 0 and c == 0)
      return ans;

    if (a > 0) {
      vector<char> new_path = old_path;
      new_path.push_back('C');
      ans = get_path(pos + 1, a - 1, b, c + 1, new_path);
      if (not ans.empty()) {
        return ans;
      }
    }

    // C->A
    if (c > 0) {
      vector<char> new_path = old_path;
      new_path.push_back('A');
      ans = get_path(pos + 1, a + 1, b, c - 1, new_path);
      if (not ans.empty()) {
        return ans;
      }
    }
    ans.clear();
    return ans;

  case 3: // BC
    // B->C
    if (b == 0 and c == 0)
      return ans;

    if (b > 0) {
      vector<char> new_path = old_path;
      new_path.push_back('C');
      ans = get_path(pos + 1, a, b - 1, c + 1, new_path);
      if (not ans.empty()) {
        return ans;
      }
    }

    // C->B
    if (c > 0) {
      vector<char> new_path = old_path;
      new_path.push_back('B');
      ans = get_path(pos + 1, a, b + 1, c - 1, new_path);
      if (not ans.empty()) {
        return ans;
      }
    }
    ans.clear();
    return ans;
  }
}

int main() {
  ll i;
  string S;

  cin >> N >> A >> B >> C;

  for (int i = 0; i < N; i++) {
    cin >> S;
    if (S == "AB") {
      proc.push_back(1);
    } else if (S == "AC") {
      proc.push_back(2);
    } else {
      proc.push_back(3);
    }
  }

  vector<char> null_path;
  path = get_path(0, A, B, C, null_path);

  if (path.empty()) {
    cout << "No" << endl;
  } else {
    cout << "Yes" << endl;
    for (auto i : path)
      cout << i << endl;
  }
}