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

#include <bits/stdc++.h>
using namespace std;

#define int long long
#define ld long double

#define pb push_back
#define F first
#define S second
#define I insert
#define sz(a) (int)((a).size())
#define all(a) (a).begin(), (a).end()

#define debug1(a) cerr << " " << #a << " : " << a << "\\n"
#define debug2(a, b)                                                           \\
  cerr << " " << #a << " : " << a << " " << #b << " : " << b << "\\n"
#define debug3(a, b, c)                                                        \\
  cerr << " " << #a << " : " << a << " " << #b << " : " << b << " " << #c      \\
       << " : " << c << "\\n"
#define debug4(a, b, c, d)                                                     \\
  cerr << " " << #a << " : " << a << " " << #b << " : " << b << " " << #c      \\
       << " : " << c << " " << #d << " : " << d << "\\n"

#define INF (int)100000000000000000
#define PI (double)3.14159265358979
// #define M 421412341324321
#define MOD 998244353

vector<vector<pair<int, int>>> adj;

vector<int> v;
void dfs(int i, int src) {
  for (auto j : adj[i]) {
    v.pb(j.S);
    if (j.F != src)
      dfs(j.F, src);
  }
}

int mx_sum(int k) {
  int n = sz(v);
  if (n == 0)
    return -INF;

  if (n >= k) {
    int ans = v[0], s = v[0];
    for (int i = 1; i < k; ++i) {
      s += v[i];
      ans = max(ans, s);
    }
    return ans;
  } else {
    int s1 = v[0];
    for (int i = 1; i < n; ++i) {
      s1 += v[i];
    }
    if (s1 > 0) {
      s1 = s1 * ((k / n) - 1);
      int x = k % n + n;
      int ans = 0, s = 0;
      if (x) {
        ans = v[0], s = v[0];
        for (int i = 1; i < x; ++i) {
          s += v[i % n];
          ans = max(ans, s);
        }
      }
      int ans1 = ans + s1;

      return ans + s1;
    } else {
      int ans = v[0], s = v[0];
      for (int i = 1; i < n; ++i) {
        s += v[i];
        ans = max(ans, s);
      }
      return ans;
    }
  }
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int n, k;
  cin >> n >> k;

  int a[n + 1], b[n + 1];
  adj.resize(n + 1);
  for (int i = 0; i < n; ++i) {
    cin >> a[i + 1];
  }
  for (int i = 0; i < n; ++i) {
    cin >> b[i + 1];
  }
  for (int i = 1; i <= n; ++i) {
    adj[i].pb({a[i], b[a[i]]});
    // debug3(i, a[i], b[a[i]]);
  }

  int ans = -INF;
  for (int i = 1; i <= n; ++i) {
    v.clear();
    dfs(i, i);
    // debug1(sz(v));
    // for (auto j:v) debug1(j);
    ans = max(ans, mx_sum(k));
  }

  cout << ans << "\\n";

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define int long long
#define ld long double

#define pb push_back
#define F first
#define S second
#define I insert
#define sz(a) (int)((a).size())
#define all(a) (a).begin(), (a).end()

#define debug1(a) cerr << " " << #a << " : " << a << "\\n"
#define debug2(a, b)                                                           \\
  cerr << " " << #a << " : " << a << " " << #b << " : " << b << "\\n"
#define debug3(a, b, c)                                                        \\
  cerr << " " << #a << " : " << a << " " << #b << " : " << b << " " << #c      \\
       << " : " << c << "\\n"
#define debug4(a, b, c, d)                                                     \\
  cerr << " " << #a << " : " << a << " " << #b << " : " << b << " " << #c      \\
       << " : " << c << " " << #d << " : " << d << "\\n"

#define INF (int)100000000000000000
#define PI (double)3.14159265358979
// #define M 421412341324321
#define MOD 998244353

vector<vector<pair<int, int>>> adj;
vector<int> vis;
vector<int> v;
int dfs(int i) {
  vis[i] = 1;
  for (auto j : adj[i]) {
    v.pb(j.S);
    if (!vis[j.F])
      dfs(j.F);
  }
}

int find(int k) {
  int n = sz(v);
  if (n == 0)
    return -INF;

  if (n >= k) {
    int ans = v[0], s = v[0];
    for (int i = 1; i < k; ++i) {
      s += v[i];
      ans = max(ans, s);
    }
    return ans;
  } else {
    int s1 = v[0];
    for (int i = 1; i < n; ++i) {
      s1 += v[i];
    }
    if (s1 >= 0)
      s1 = s1 * (k / n);
    k %= n;
    int ans = v[0], s = v[0];
    for (int i = 1; i < k; ++i) {
      s += v[i];
      ans = max(ans, s);
    }
    return ans + s1;
  }
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int n, k;
  cin >> n >> k;

  int a[n + 1], b[n + 1];
  adj.resize(n + 1);
  for (int i = 0; i < n; ++i) {
    cin >> a[i + 1];
  }
  for (int i = 0; i < n; ++i) {
    cin >> b[i + 1];
  }
  for (int i = 1; i <= n; ++i) {
    adj[i].pb({a[i], b[a[i]]});
    // debug3(i, a[i], b[a[i]]);
  }

  int ans = -INF;
  for (int i = 1; i <= n; ++i) {
    vis.clear();
    vis.resize(n + 1, 0);
    v.clear();
    dfs(i);
    ans = max(ans, find(k));
  }

  cout << ans << "\\n";

  return 0;
}