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

#include <bits/stdc++.h>
#define ll long long int
#define MOD 1000000007
#define INF 1000000000000000000

using namespace std;

ll n, k;
vector<ll> visited;
vector<ll> temp;
vector<ll> p, c;

ll ans = -INF;

void init() { visited = vector<ll>(n, 0); }

void dfs(int i) {
  if (visited[i] == 1) {
    ll l = temp.size();

    ll sum = 0;
    for (int i = 0; i < l; i++) {
      sum += temp[i];
    }

    if (sum < 0) {
      vector<vector<ll>> dp(l, vector<ll>(l, 0));
      ll ma = -INF;
      for (int i = 0; i < l; i++) {
        dp[i][0] = temp[i];
        ma = max(ma, dp[i][0]);
      }
      for (int i = 0; i < l; i++) {
        for (int j = 1; j < l; j++) {
          dp[i][j] = dp[i][j - 1] + temp[(i + j) % l];
          ma = max(ma, dp[i][j]);
        }
      }
      ans = max(ans, ma);
    } else {
      ll t = (k / l) * sum;

      ll x = k % l;

      ll ma = -INF;
      ll ma2 = -INF;

      vector<vector<ll>> dp(l, vector<ll>(l, 0));
      for (int i = 0; i < l; i++) {
        dp[i][0] = temp[i];
      }
      for (int i = 0; i < l; i++) {
        for (int j = 1; j < l; j++) {
          dp[i][j] = dp[i][j - 1] + temp[(i + j) % l];
        }
      }
      for (int i = 0; i < l; i++) {
        for (int j = 0; j < l; j++) {
          ma = max(ma, dp[i][j]);
        }
      }
      for (int i = 0; i < l; i++) {
        for (int j = 0; j < x; j++) {
          ma2 = max(ma2, dp[i][j]);
        }
      }
      ans = max(ans, max(t + ma2, t - sum + ma));
    }
  } else {
    visited[i] = 1;
    temp.emplace_back(c[i]);
    dfs(p[i]);
  }
}

int main(void) {
  cin >> n >> k;

  p = vector<ll>(n);
  for (int i = 0; i < n; i++) {
    cin >> p[i];
    p[i]--;
  }
  c = vector<ll>(n);
  for (int i = 0; i < n; i++) {
    cin >> c[i];
  }

  init();

  for (int i = 0; i < n; i++) {
    if (visited[i] == 0) {
      temp = vector<ll>(0);
      dfs(i);
    }
  }

  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long int
#define MOD 1000000007
#define INF 1000000000000000000

using namespace std;

ll n, k;
vector<ll> visited;
vector<ll> temp;
vector<ll> p, c;

ll ans = -INF;

void init() { visited = vector<ll>(n, 0); }

void dfs(int i) {
  if (visited[i] == 1) {
    ll l = temp.size();

    ll sum = 0;
    for (int i = 0; i < l; i++) {
      sum += temp[i];
    }

    if (sum < 0) {
      ll t = -INF;
      for (int i = 0; i < l; i++) {
        t = max(t, temp[i]);
      }
      ans = max(t, ans);
    } else if (sum >= 0) {
      ll t = (k / l) * sum;

      ll x = k % l;

      ll ma = -INF;
      vector<vector<ll>> dp(l, vector<ll>(x, 0));
      for (int i = 0; i < l; i++) {
        dp[i][0] = temp[i];
        ma = max(ma, dp[i][0]);
      }
      for (int i = 0; i < l; i++) {
        for (int j = 1; j < x; j++) {
          dp[i][j] = dp[i][(j - 1 + l) % l] + temp[(i + j) % l];
          ma = max(ma, dp[i][j]);
        }
      }
      ans = max(ans, t + ma);
    }
  } else {
    visited[i] = 1;
    temp.emplace_back(c[i]);
    dfs(p[i]);
  }
}

int main(void) {
  cin >> n >> k;

  p = vector<ll>(n);
  for (int i = 0; i < n; i++) {
    cin >> p[i];
    p[i]--;
  }
  c = vector<ll>(n);
  for (int i = 0; i < n; i++) {
    cin >> c[i];
  }

  init();

  for (int i = 0; i < n; i++) {
    if (visited[i] == 0) {
      temp = vector<ll>(0);
      dfs(i);
    }
  }

  cout << ans << endl;
  return 0;
}