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

// 雪花飄飄北風嘯嘯
// 天地一片蒼茫

#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
#define ll long long
#define ii pair<ll, ll>
#define iii pair<ii, ll>
#define fi first
#define se second
#define endl '\\n'
#define debug(x) cout << #x << " is " << x << endl;

#define rep(x, start, end)                                                     \\
  for (auto x = (start) - ((start) > (end)); x != (end) - ((start) > (end));   \\
       ((start) < (end) ? x++ : x--))
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()

ll MAX(ll a) { return a; }
ll MIN(ll a) { return a; }
template <typename... Args> ll MAX(ll a, Args... args) {
  return max(a, MAX(args...));
}
template <typename... Args> ll MIN(ll a, Args... args) {
  return min(a, MIN(args...));
}

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

mt19937 rng(chrono::system_clock::now().time_since_epoch().count());

int n;
ll c[26];
ll d[400][26];

ll calc(vector<int> &contest) {
  ll last[26];

  memset(last, -1, sizeof(last));

  ll res = 0;
  rep(x, 0, n) {
    res += d[x][contest[x]];
    last[contest[x]] = x;

    rep(y, 0, 26) res -= c[y] * (x - last[y]);
  }

  return res;
}

// store curr best
ll best;
vector<int> ans;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  // freopen("sample2.in","r",stdin);

  auto begin_time = chrono::steady_clock::now().time_since_epoch().count();

  cin >> n;
  rep(x, 0, 26) cin >> c[x];

  rep(x, 0, n) { rep(y, 0, 26) cin >> d[x][y]; }

  vector<int> curr;
  rep(x, 0, n) { curr.push_back(0); }

  ll val = calc(curr);

  best = val;
  ans = curr;

  while (chrono::steady_clock::now().time_since_epoch().count() - begin_time <
         1900000000) {
    rep(zzz, 0, 375) { // try a few times to go to a smaller place
      int idx = rng() % n;
      int type = curr[idx];

      curr[idx] = rng() % 26;

      ll temp = calc(curr);
      if (temp > val) {
        val = temp;

        if (best < val) {
          best = val;
          ans = curr;
        }

        goto ok;
      } else {
        curr[idx] = type;
      }
    }

    // cout<<"reached "<<val<<endl;

    // almost maxima. screw  around
    rep(x, 0, 2) {
      curr[rng() % n] = rng() % 26;
      val = calc(curr);
    }

  ok:;
  }

  // pls uncomment later
  rep(x, 0, n) cout << ans[x] + 1 << endl;

  // cout<<"debug: "<<best<<endl;
}


Error: -8

Fixed submission:

// 雪花飄飄北風嘯嘯
// 天地一片蒼茫

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
#define ll long long
#define ii pair<ll, ll>
#define iii pair<ii, ll>
#define fi first
#define se second
#define endl '\\n'
#define debug(x) cout << #x << " is " << x << endl;

#define rep(x, start, end)                                                     \\
  for (auto x = (start) - ((start) > (end)); x != (end) - ((start) > (end));   \\
       ((start) < (end) ? x++ : x--))
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()

ll MAX(ll a) { return a; }
ll MIN(ll a) { return a; }
template <typename... Args> ll MAX(ll a, Args... args) {
  return max(a, MAX(args...));
}
template <typename... Args> ll MIN(ll a, Args... args) {
  return min(a, MIN(args...));
}

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

mt19937 rng(chrono::system_clock::now().time_since_epoch().count());

int n;
ll c[26];
ll d[400][26];

ll calc(vector<int> &contest) {
  ll last[26];

  memset(last, -1, sizeof(last));

  ll res = 0;
  rep(x, 0, n) {
    res += d[x][contest[x]];
    last[contest[x]] = x;

    rep(y, 0, 26) res -= c[y] * (x - last[y]);
  }

  return res;
}

// store curr best
ll best;
vector<int> ans;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  freopen("sample2.in", "r", stdin);

  auto begin_time = chrono::steady_clock::now().time_since_epoch().count();

  cin >> n;
  rep(x, 0, 26) cin >> c[x];

  rep(x, 0, n) { rep(y, 0, 26) cin >> d[x][y]; }

  vector<int> curr;
  rep(x, 0, n) { curr.push_back(0); }

  ll val = calc(curr);

  best = val;
  ans = curr;

  while (chrono::steady_clock::now().time_since_epoch().count() - begin_time <
         1900000000) {
    rep(zzz, 0, 250) { // try a few times to go to a smaller place
      int idx = rng() % n;
      int type = curr[idx];

      curr[idx] = rng() % 26;

      ll temp = calc(curr);
      if (temp > val) {
        val = temp;

        if (best < val) {
          best = val;
          ans = curr;
        }

        goto ok;
      } else {
        curr[idx] = type;
      }
    }

    // cout<<"reached "<<val<<endl;

    // almost maxima. screw around
    rep(x, 0, 3) {
      curr[rng() % n] = rng() % 26;
      val = calc(curr);
    }

  ok:;
  }

  // pls uncomment later
  // rep(x,0,n) cout<<ans[x]+1<<endl;

  cout << "debug: " << best << endl;
}