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

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <chrono>
#include <cstdio>
#include <ctime>
#include <ctype.h>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <time.h>
#include <unistd.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
using ll = long long;
#define endl '\\n'
#define debug(name) DEBUG(#name, (name))
template <typename T> void DEBUG(string label, T value) {
  cerr << "[" << label << " = " << value << "]\\n";
}

void solve() {
  int n, k;
  cin >> n >> k;
  vector<ll> cost(n);
  vector<int> p(n);
  for (int i = 0; i < n; ++i) {
    cin >> p[i];
    p[i]--;
  }
  for (int i = 0; i < n; ++i) {
    cin >> cost[i];
  }
  vector<ll> sums;
  auto dfs = [&sums, &p, &cost](const auto &lambda, int I, int u,
                                ll ans) -> void {
    // printf("cost[%d] = %lld\\n", 1+u, cost[u]);
    if (u == I) {
      ans += cost[u];
      sums.push_back(ans);
      return;
    }
    ans += cost[u];
    sums.push_back(ans);
    lambda(lambda, I, p[u], ans);
  };
  ll best = 0;
  bool not_set = true;
  for (int i = 0; i < n; ++i) {
    sums.clear();
    dfs(dfs, i, p[i], 0);
    ll ans = sums[0];
    // for(auto kk : sums){
    // cerr << kk << " ";
    //}
    // cerr <<endl;
    if (k > (int)sums.size()) {
      for (int j = 1; j < (int)sums.size(); ++j) {
        ans = max(ans, sums[j]);
      }
      if (sums.back() > 0) {
        int rem = k % (int)sums.size();
        ans = max(ans, 1LL * sums.back() * (k / (int)sums.size()));
        for (int j = 0; j < rem; ++j) {
          ans = max(ans, 1LL * sums.back() * (k / (int)sums.size()) + sums[j]);
        }
        ans =
            max(ans, 1LL * sums.back() * max(0, ((k / (int)sums.size()) - 1)) +
                         *max_element(sums.begin(), sums.end()));
      }
    } else {
      for (int j = 1; j < k; ++j) {
        ans = max(ans, sums[j]);
      }
    }
    if (not_set) {
      not_set = false;
      best = ans;
    }
    best = max(best, ans);
  }
  cout << best << endl;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  solve();
}


Error: 0

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <chrono>
#include <cstdio>
#include <ctime>
#include <ctype.h>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <time.h>
#include <unistd.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
using ll = long long;
#define endl '\\n'
#define debug(name) DEBUG(#name, (name))
template <typename T> void DEBUG(string label, T value) {
  cerr << "[" << label << " = " << value << "]\\n";
}

void solve() {
  int n, k;
  cin >> n >> k;
  vector<int> cost(n), p(n);
  for (int i = 0; i < n; ++i) {
    cin >> p[i];
    p[i]--;
  }
  for (int i = 0; i < n; ++i) {
    cin >> cost[i];
  }
  ll best = 0;
  bool not_set = true;
  auto dfs = [&not_set, &k, &best, &p, &cost](const auto &lambda, int u,
                                              int cnt, ll ans) -> void {
    if (cnt == k) {
      return;
    }
    ans += cost[u];
    if (not_set) {
      not_set = false;
      best = ans;
    }
    best = max(best, ans);
    lambda(lambda, p[u], cnt + 1, ans);
  };
  for (int i = 0; i < n; ++i) {
    dfs(dfs, p[i], 0, 0);
  }
  cout << best << endl;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  solve();
}