Problem p02692 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define FAST                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
typedef int ll;
typedef long double ld;
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define mod 1000000007
#define inf 1000000000000000000
#define bpc(x) __builtin_popcountll(x)
#define autoit(x, it) for (auto it = x.begin(); it != x.end(); it++)
#define rep(n) for (ll i = 0; i < n; i++)
#define repi(i, n) for (ll i = 0; i < n; i++)

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define ordered_set                                                            \\
  tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>

using namespace std;

int main() {
  FAST /**/

      ll n;
  cin >> n;

  ll a, b, c;
  cin >> a >> b >> c;

  bool dp[n + 1][5][5][5];
  memset(dp, 0, sizeof(dp));

  ll off = 2;

  dp[0][off][off][off] = 1;
  pair<pair<ll, ll>, ll> prev[n + 1][5][5][5];
  for (ll i = 1; i <= n; i++) {
    string s;
    cin >> s;

    for (ll s1 = -2; s1 < 3; s1++)
      for (ll s2 = -2; s2 < 3; s2++)
        for (ll s3 = -2; s3 < 3; s3++) {
          ll pa = a + s1, pb = b + s2, pc = c + s3;
          if (pa < 0 || pb < 0 || pc < 0)
            continue;
          if (dp[i - 1][s1 + off][s2 + off][s3 + off] == 0)
            continue;
          if (s == "AB" || s == "BA") {
            if (abs(s1 + 1) <= 2 && abs(s2 - 1) <= 2 && a + s1 + 1 > -1 &&
                b + s2 - 1 > -1)
              dp[i][(s1 + 1) + off][(s2 - 1) + off]
                [s3 + off] = 1,
                      prev[i][(s1 + 1) + off][(s2 - 1) + off][s3 + off] =
                          mp(mp(s1, s2), s3);
            if (abs(s1 - 1) <= 2 && abs(s2 + 1) <= 2 && a + s1 - 1 > -1 &&
                b + s2 + 1 > -1)
              dp[i][(s1 - 1) + off][(s2 + 1) + off]
                [s3 + off] = 1,
                      prev[i][(s1 - 1) + off][(s2 + 1) + off][s3 + off] =
                          mp(mp(s1, s2), s3);
          }

          if (s == "BC" || s == "CB") {
            if (abs(s2 + 1) <= 2 && abs(s3 - 1) <= 2 && b + s2 + 1 > -1 &&
                c + s3 - 1 > -1)
              dp[i][(s1 + 0) + off][(s2 + 1) + off]
                [(s3 - 1) + off] = 1,
                            prev[i][(s1 + 0) + off][(s2 + 1) + off]
                                [(s3 - 1) + off] = mp(mp(s1, s2), s3);
            if (abs(s2 - 1) <= 2 && abs(s3 + 1) <= 2 && b + s2 - 1 > -1 &&
                c + s3 + 1 > -1)
              dp[i][(s1 + 0) + off][(s2 - 1) + off]
                [(s3 + 1) + off] = 1,
                            prev[i][(s1 + 0) + off][(s2 - 1) + off]
                                [(s3 + 1) + off] = mp(mp(s1, s2), s3);
          }

          if (s == "AC" || s == "CA") {
            if (abs(s1 + 1) <= 2 && abs(s3 - 1) <= 2 && a + s1 + 1 > -1 &&
                c + s3 - 1 > -1)
              dp[i][(s1 + 1) + off][(s2 + 0) + off]
                [(s3 - 1) + off] = 1,
                            prev[i][(s1 + 1) + off][(s2 + 0) + off]
                                [(s3 - 1) + off] = mp(mp(s1, s2), s3);
            if (abs(s1 - 1) <= 2 && abs(s3 + 1) <= 2 && a + s1 - 1 > -1 &&
                c + s3 + 1 > -1)
              dp[i][(s1 - 1) + off][(s2 - 0) + off]
                [(s3 + 1) + off] = 1,
                            prev[i][(s1 - 1) + off][(s2 + 0) + off]
                                [(s3 + 1) + off] = mp(mp(s1, s2), s3);
          }
        }
  }

  bool fpos = 0;
  pair<pair<ll, ll>, ll> curr;
  for (ll s1 = -2; s1 < 3; s1++)
    for (ll s2 = -2; s2 < 3; s2++)
      for (ll s3 = -2; s3 < 3; s3++)
        if (dp[n][s1 + off][s2 + off][s3 + off])
          fpos = 1, curr = mp(mp(s1, s2), s3);

  if (fpos == 0) {
    cout << "No";
    return 0;
  }

  cout << "Yes\\n";
  stack<char> st;
  for (ll i = n; i > 0; i--) {
    if (abs(curr.ff.ff) > off || abs(curr.ff.ss) > off || abs(curr.ss) > off)
      break;
    auto previ = prev[i][curr.ff.ff + off][curr.ff.ss + off][curr.ss + off];
    if (curr.ff.ff > previ.ff.ff)
      st.push('A');
    else if (curr.ff.ss > previ.ff.ss)
      st.push('B');
    else
      st.push('C');
    curr = previ;
  }

  while (!st.empty()) {
    cout << st.top() << '\\n';
    st.pop();
  }

  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define FAST                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
typedef long long ll;
typedef long double ld;
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define mod 1000000007
#define inf 1000000000000000000
#define bpc(x) __builtin_popcountll(x)
#define autoit(x, it) for (auto it = x.begin(); it != x.end(); it++)
#define rep(n) for (ll i = 0; i < n; i++)
#define repi(i, n) for (ll i = 0; i < n; i++)

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define ordered_set                                                            \\
  tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>

using namespace std;

int main() {
  FAST /**/

      ll n;
  cin >> n;

  ll a, b, c;
  cin >> a >> b >> c;

  ll dp[n + 1][7][7][7];
  rep(n + 1) repi(i1, 7) repi(i2, 7) repi(i3, 7) dp[i][i1][i2][i3] = 0;

  ll off = 2;

  dp[0][off][off][off] = 1;
  // pair<pair<ll,ll>,ll> prev[n+1][5][5][5];
  for (ll i = 1; i <= n; i++) {
    string s;
    cin >> s;

    /*for(ll s1=-2;s1<3;s1++)
             for(ll s2=-2;s2<3;s2++)
                 for(ll s3=-2;s3<3;s3++)
                 {
                         ll pa = a+s1, pb = b+s2, pc = c+s3;
                         if(pa<0 || pb<0 || pc<0)
                             continue;
                         if(dp[i-1][s1+off][s2+off][s3+off] == 0)
                             continue;
                         if(s == "AB" || s == "BA")
                         {
                             if(abs(s1+1)<=2 && abs(s2-1)<=2 && a+s1+1>-1 &&
       b+s2-1>-1) dp[i][(s1+1)+off][(s2-1)+off][s3+off] = 1,
       prev[i][(s1+1)+off][(s2-1)+off][s3+off] = mp(mp(s1,s2),s3);
                             if(abs(s1-1)<=2 && abs(s2+1)<=2 && a+s1-1>-1 &&
       b+s2+1>-1) dp[i][(s1-1)+off][(s2+1)+off][s3+off] = 1,
       prev[i][(s1-1)+off][(s2+1)+off][s3+off] = mp(mp(s1,s2),s3);
                         }

                         if(s == "BC" || s == "CB")
                         {
                             if(abs(s2+1)<=2 && abs(s3-1)<=2 && b+s2+1>-1 &&
       c+s3-1>-1) dp[i][(s1+0)+off][(s2+1)+off][(s3-1)+off] = 1,
       prev[i][(s1+0)+off][(s2+1)+off][(s3-1)+off] = mp(mp(s1,s2),s3);
                             if(abs(s2-1)<=2 && abs(s3+1)<=2 && b+s2-1>-1 &&
       c+s3+1>-1) dp[i][(s1+0)+off][(s2-1)+off][(s3+1)+off] = 1,
       prev[i][(s1+0)+off][(s2-1)+off][(s3+1)+off] = mp(mp(s1,s2),s3);
                         }

                         if(s == "AC" || s == "CA")
                         {
                             if(abs(s1+1)<=2 && abs(s3-1)<=2 && a+s1+1>-1 &&
       c+s3-1>-1) dp[i][(s1+1)+off][(s2+0)+off][(s3-1)+off] = 1,
       prev[i][(s1+1)+off][(s2+0)+off][(s3-1)+off] = mp(mp(s1,s2),s3);
                             if(abs(s1-1)<=2 && abs(s3+1)<=2 && a+s1-1>-1 &&
       c+s3+1>-1) dp[i][(s1-1)+off][(s2-0)+off][(s3+1)+off] = 1,
       prev[i][(s1-1)+off][(s2+0)+off][(s3+1)+off] = mp(mp(s1,s2),s3);
                         }
                 }*/
  }

  cout << "No\\n";

  /*bool fpos = 0;
  pair<pair<ll,ll>,ll> curr;
  for(ll s1=-2;s1<3;s1++)
               for(ll s2=-2;s2<3;s2++)
                   for(ll s3=-2;s3<3;s3++)
                       if(dp[n][s1+off][s2+off][s3+off])
                           fpos = 1, curr = mp(mp(s1,s2),s3);

  if(fpos == 0)
  {
      cout<<"No";
      return 0;
  }

  cout<<"Yes\\n";
  stack<char> st;
  for(ll i=n;i>0;i--)
  {
      if(abs(curr.ff.ff)>off || abs(curr.ff.ss)>off || abs(curr.ss)>off)
           break;
      auto previ = prev[i][curr.ff.ff+off][curr.ff.ss+off][curr.ss+off];
      if(curr.ff.ff>previ.ff.ff)
           st.push('A');
      else if(curr.ff.ss>previ.ff.ss)
           st.push('B');
      else st.push('C');
      curr = previ;
  }

  while(!st.empty())
  {
      cout<<st.top()<<'\\n';
      st.pop();
  }
  */

  return 0;
}