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

// triedodin
#include <bits/stdc++.h>
using namespace std;
#define mem(a, b) memset(a, (b), sizeof(a))
#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.end(), cont.begin()
#define mp make_pair
#define pb push_back
#define PI 3.1415926535897932384626433832795
#define mod1 1000000007
#define mod2 998244353
#define ll long long int
#define ld long double
#define inf LLONG_MAX
#define endl "\\n"
#define f first
#define s second
#define inp(a)                                                                 \\
  int a;                                                                       \\
  cin >> a;
#define inpa(a, n)                                                             \\
  vector<int> a(n);                                                            \\
  fo(i, 0, n, 1) cin >> a[i];
const double pi = acos(-1.0);
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<pii> vp;
typedef vector<vi> vvi;
typedef map<int, int> mpii;
typedef set<int> seti;
typedef multiset<int> mseti;
#define printclock                                                             \\
  cerr << "Time : " << 1000 * (ld)clock() / (ld)CLOCKS_PER_SEC << "ms\\n";
#define sz(c) (int)c.size()
#define debug1(x) cout << #x << "=" << x << endl;
#define debug2(x, y) cout << #x << "=" << x << " " << #y << "=" << y << endl;
#define debug3(x, y, z)                                                        \\
  cout << #x << "=" << x << " " << #y << "=" << y << " " << #z << "=" << z     \\
       << endl;
#define debug4(x, y, z, w)                                                     \\
  cout << #x << "=" << x << " " << #y << "=" << y << " " << #z << "=" << z     \\
       << " " << #w << "=" << w << endl;
#define debugarray(arr, j, n)                                                  \\
  for (int i = j; i < n; i++) {                                                \\
    cout << "i=" << i << " arr[i]=" << arr[i] << "\\n";                         \\
  }

const ll MODA = 1000000007;
// const  int MODA =                     998244353 ;
vector<ll> primefactors; // used by generatePrimeFactors() // ARRAYS/VECTORS
vector<ll> factors;      // used by generateFactors()
ll fact[1025] = {};      // used by generateFactorial() & ncr()
bool isPowerTwo(ll x) { return (x && !(x & (x - 1))); } // FUNCTIONS
ll modmul(ll a, ll b) { return ((a % MODA) * (b % MODA)) % MODA; }
ll modadd(ll a, ll b) { return ((a % MODA) + (b % MODA) + MODA) % MODA; }
ll modsub(ll a, ll b) { return ((a % MODA) - (b % MODA) + MODA) % MODA; }
bool isSubstring(string s1, string s2) {
  if (s1.find(s2) != string::npos)
    return true;
  else
    return false;
}
void generateFactorial(ll n) {
  fact[0] = 1;
  for (ll i = 1; i <= n; i++)
    fact[i] = (i * 1ll * fact[i - 1]);
}
void generateFactorialMOD(ll n) {
  fact[0] = 1;
  for (ll i = 1; i <= n; i++)
    fact[i] = (i * 1ll * fact[i - 1]) % MODA;
}
bool isPrime(ll n) {
  if (n < 2)
    return false;
  for (ll i = 2; i * i <= n; i++)
    if (n % i == 0)
      return false;
  return true;
} // O(sqrt(n))
ll power(ll a, ll b) {
  ll res = 1;
  while (b) {
    if (b & 1)
      res = (res * a);
    a = (a * a);
    b >>= 1;
  }
  return res;
}
ll powermod(ll a, ll b) {
  ll res = 1;
  while (b) {
    if (b & 1)
      res = (res * a) % MODA;
    a = (a * a) % MODA;
    b >>= 1;
  }
  return res;
}
ll modi(ll a) {
  ll m = MODA, s = 1, p = 0;
  while (a > 1) {
    ll q = a / m, t = m;
    m = a % m;
    a = t;
    t = p;
    p = s - q * p;
    s = t;
  }
  return s >= 0 ? s : s + MODA;
}
void generatePrimeFactors(ll n) {
  primefactors.clear();
  for (ll i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      primefactors.pb(i);
      while (n % i == 0)
        n = n / i;
    }
  }
  if (n != 1)
    primefactors.pb(n);
}
ll ncr(ll n, ll r) {
  if (r == 0)
    return 1;
  fact[0] = 1;
  for (int i = 1; i <= n; i++)
    fact[i] = fact[i - 1] * i % MODA;
  return (fact[n] * modi(fact[r]) % MODA * modi(fact[n - r]) % MODA) % MODA;
}
void generateFactors(ll n) {
  factors.clear();
  for (ll i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      factors.pb(i);
      if (n / i != i)
        factors.pb(n / i);
    }
  }
  factors.pb(1);
  factors.pb(n);
  sort(factors.begin(), factors.end());
}
ld Logn(ld n, ld r) { return log(n) / log(r); }

///////////////////////////////////
const int N = 2000005;

ll dp[3001][3001][4];
ll r, c, k;
////////////////////////////////////

ll rec(int cx, int cy, int mx, vector<vector<int>> &value) {

  if (dp[cx][cy][mx] != -1) {
    return dp[cx][cy][mx];
  }

  // debug4(cx, cy, value[cx][cy], mx);
  if (cx == r && cy == c) {
    if (mx >= 1) {
      return value[cx][cy];
    } else {
      return 0;
    }
  }

  ll ans1 = -1;
  ll ans2 = -1;
  ll ans3 = -1;
  ll ans4 = -1;
  if (cx + 1 <= r) {

    if (dp[cx + 1][cy][mx] != -1) {
      ans1 = dp[cx + 1][cy][3];
    } else {
      dp[cx + 1][cy][3] = rec(cx + 1, cy, 3, value);
      ans1 = dp[cx + 1][cy][3];
    }

    if (mx >= 1 && value[cx][cy] != 0) {
      if (dp[cx + 1][cy][3] != -1) {
        ans2 = value[cx][cy] + dp[cx + 1][cy][3];
      } else {
        // cout << "He" << endl;
        dp[cx + 1][cy][3] = rec(cx + 1, cy, 3, value);
        ans2 = value[cx][cy] + dp[cx + 1][cy][3];
      }
    }
  }

  if (cy + 1 <= c) {

    if (dp[cx][cy + 1][mx] != -1) {
      ans3 = dp[cx][cy + 1][mx];
    } else {
      dp[cx][cy + 1][mx] = rec(cx, cy + 1, mx, value);
      ans3 = dp[cx][cy + 1][mx];
    }

    if (mx >= 1 && value[cx][cy] != 0) {

      if (dp[cx][cy + 1][mx - 1] != -1) {
        ans4 = value[cx][cy] + dp[cx][cy + 1][mx - 1];
      } else {
        dp[cx][cy + 1][mx - 1] = rec(cx, cy + 1, mx - 1, value);
        ans4 = value[cx][cy] + dp[cx][cy + 1][mx - 1];
      }
    }
  }

  return dp[cx][cy][mx] = max(ans1, max(ans2, max(ans3, ans4)));
}

void solve() {

  mem(dp, -1);
  cin >> r >> c >> k;
  vector<vector<int>> value(r + 1, vector<int>(c + 1, 0));

  for (int i = 0; i < k; i++) {
    int x, y, v;
    cin >> x >> y >> v;
    value[x][y] = v;
  }

  ll ans = rec(1, 1, 3, value);

  cout << ans << endl;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  int t = 1;
  // cin >> t;

  while (t--) {
    solve();
  }

  printclock;
}

Error: WA

Fixed submission:

// triedodin
/////////////////////////////
// try to bring as much symmetry as possible
// just check for integer overflow
// add MOD value when you substract two numbers

/////////////////////////////

#include <bits/stdc++.h>
using namespace std;
#define mem(a, b) memset(a, (b), sizeof(a))
#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.end(), cont.begin()
#define mp make_pair
#define pb push_back
#define PI 3.1415926535897932384626433832795
#define mod1 1000000007
#define mod2 998244353
#define ll long long int
#define ld long double
#define inf LLONG_MAX
#define endl "\\n"
#define F first
#define S second
#define inp(a)                                                                 \\
  int a;                                                                       \\
  cin >> a;
#define inpa(a, n)                                                             \\
  vector<int> a(n);                                                            \\
  fo(i, 0, n, 1) cin >> a[i];
const double pi = acos(-1.0);
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<pii> vp;
typedef vector<vi> vvi;
typedef map<int, int> mpii;
typedef set<int> seti;
typedef multiset<int> mseti;
#define printclock                                                             \\
  cerr << "Time : " << 1000 * (ld)clock() / (ld)CLOCKS_PER_SEC << "ms\\n";
#define sz(c) (int)c.size()
#define d1(x) cout << #x << "=" << x << endl;
#define d2(x, y) cout << #x << "=" << x << " " << #y << "=" << y << endl;
#define d3(x, y, z)                                                            \\
  cout << #x << "=" << x << " " << #y << "=" << y << " " << #z << "=" << z     \\
       << endl;
#define d4(x, y, z, w)                                                         \\
  cout << #x << "=" << x << " " << #y << "=" << y << " " << #z << "=" << z     \\
       << " " << #w << "=" << w << endl;
#define da(arr, j, n)                                                          \\
  for (int i = j; i < n; i++) {                                                \\
    cout << "i=" << i << " arr[i]=" << arr[i] << "\\n";                         \\
  }

const ll MODA = 1000000007;
// const  int MODA =                     998244353 ;
vector<ll> primefactors; // used by generatePrimeFactors() // ARRAYS/VECTORS
vector<ll> factors;      // used by generateFactors()
ll fact[1000010] = {};   // used by generateFactorial() & ncr()
bool isPowerTwo(ll x) { return (x && !(x & (x - 1))); } // FUNCTIONS
ll modmul(ll a, ll b) { return ((a % MODA) * (b % MODA)) % MODA; }
ll modadd(ll a, ll b) { return ((a % MODA) + (b % MODA) + MODA) % MODA; }
ll modsub(ll a, ll b) { return ((a % MODA) - (b % MODA) + MODA) % MODA; }
bool isSubstring(string s1, string s2) {
  if (s1.find(s2) != string::npos)
    return true;
  else
    return false;
}
void generateFactorial(ll n) {
  fact[0] = 1;
  for (ll i = 1; i <= n; i++)
    fact[i] = (i * 1ll * fact[i - 1]);
}
void generateFactorialMOD(ll n) {
  fact[0] = 1;
  for (ll i = 1; i <= n; i++)
    fact[i] = (i * 1ll * fact[i - 1]) % MODA;
}
bool isPrime(ll n) {
  if (n < 2)
    return false;
  for (ll i = 2; i * i <= n; i++)
    if (n % i == 0)
      return false;
  return true;
} // O(sq1rt(n))
ll power(ll a, ll b) {
  ll res = 1;
  while (b) {
    if (b & 1)
      res = (res * a);
    a = (a * a);
    b >>= 1;
  }
  return res;
}
ll powermod(ll a, ll b) {
  ll res = 1;
  while (b) {
    if (b & 1)
      res = (res * a) % MODA;
    a = (a * a) % MODA;
    b >>= 1;
  }
  return res;
}
ll modi(ll a) {
  ll m = MODA, s = 1, p = 0;
  while (a > 1) {
    ll q = a / m, t = m;
    m = a % m;
    a = t;
    t = p;
    p = s - q * p;
    s = t;
  }
  return s >= 0 ? s : s + MODA;
}
void generatePrimeFactors(ll n) {
  primefactors.clear();
  for (ll i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      primefactors.pb(i);
      while (n % i == 0)
        n = n / i;
    }
  }
  if (n != 1)
    primefactors.pb(n);
}
ll ncr(ll n, ll r) {
  if (r == 0)
    return 1;
  fact[0] = 1;
  for (int i = 1; i <= n; i++)
    fact[i] = fact[i - 1] * i % MODA;
  return (fact[n] * modi(fact[r]) % MODA * modi(fact[n - r]) % MODA) % MODA;
}
void generateFactors(ll n) {
  factors.clear();
  for (ll i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      factors.pb(i);
      if (n / i != i)
        factors.pb(n / i);
    }
  }
  factors.pb(1);
  factors.pb(n);
  sort(factors.begin(), factors.end());
}
ld Logn(ld n, ld r) { return log(n) / log(r); }

///////////////////////////////////
const ll Nmax = 1000005;

ll rm, bm, gm;
vector<ll> ra, ba, ga;
ll dp[201][201][201];

////////////////////////////////////

void solve() {

  ll n, k;
  cin >> n >> k;
  vector<int> v(n + 1);
  vector<bool> vis(n + 1, false);
  vector<int> idv(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> v[i];
  }
  for (int i = 1; i < n + 1; i++) {
    cin >> idv[i];
  }
  vector<vector<int>> cycle;

  for (int i = 1; i <= n; i++) {
    if (vis[i] == false) {

      int j = i;
      vector<int> temp;
      while (vis[j] == false) {
        temp.pb(j);
        vis[j] = true;
        d2(j, v[j]);
        j = v[j];
      }
      //  cout << endl;
    }
  }
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  int t = 1;
  // cin >> t;

  while (t--) {
    solve();
  }

  printclock;
}