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

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

// boost
#include <boost/multiprecision/cpp_int.hpp>
using boost::multiprecision::cpp_int;

using ll = long long;
using ld = long double;

using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vb = vector<bool>;
using vs = vector<string>;
using vld = vector<ld>;
using vvld = vector<vld>;

typedef pair<ll, ll> P;

#define bit(n) (1LL << (n))

// #define int long long

#define all(v) v.begin(), v.end()

#define rep(i, n) for (ll i = 0; i < n; i++)
#define REP(i, n) for (ll i = 1; i < n; i++)

#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define FORm(i, m) for (auto i = m.begin(); i != m.end(); i++)

template <class T> inline void chmax(T &a, T b) { a = std::max(a, b); }
template <class T> inline void chmin(T &a, T b) { a = std::min(a, b); }

#define mod (ll)(1e9 + 7)
// #define mod (998244353ll)

const long long INF = 1LL << 60;

ll d;
vll c(26);
vvll s;

vll create_first_answer() {
  vll ans(d);
  vll last(26, -1);

  rep(i, d) {
    vll cand(26);

    rep(j, 26) { cand[i] = s[i][j] + c[j] * (i - last[j]); }

    ll choice = distance(cand.begin(), max_element(all(cand)));
    ans[i] = choice;
    last[choice] = i;
  }
  return ans;
}

ll update_diff(ll idx, const vll &choice) {
  ll last = -1;
  ll total = 0;
  rep(today, d) {
    if (choice[today] == idx) {
      last = today;
      total += s[today][idx];
    } else {
      total -= c[idx] * (today - last);
    }
  }
  return total;
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);

  cin >> d;
  rep(i, 26) { cin >> c[i]; }

  s = vvll(d, vll(26));
  rep(i, d) {
    rep(j, 26) { cin >> s[i][j]; }
  }

  srand(s[0][0]);

  vll ans(d);

  // ans = create_first_answer();

  vll score_con(26);
  ll score = 0;

  rep(i, 26) {
    score_con[i] = update_diff(i, ans);
    score += score_con[i];
  }

  rep(i, 50000) {
    vll cand = ans;
    vb changed(26);

    rep(j, rand() % 3 + 1) {
      ll tgt = (i + j) % d;
      cand[tgt] = rand() % 26;

      changed[ans[tgt]] = true;
      changed[cand[tgt]] = true;
    }

    ll cs = score;
    vll score_cand = score_con;
    rep(i, 26) {
      if (changed[i]) {
        cs -= score_cand[i];
        score_cand[i] = update_diff(i, cand);
        cs += score_cand[i];
      }
    }
    if (cs > score) {
      ans = cand;
      score_con = score_cand;
    }
  }

  rep(i, d) { cout << ans[i] + 1 << endl; }

  return 0;
}


Error: WA

Fixed submission:

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

// boost
#include <boost/multiprecision/cpp_int.hpp>
using boost::multiprecision::cpp_int;

using ll = long long;
using ld = long double;

using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vb = vector<bool>;
using vs = vector<string>;
using vld = vector<ld>;
using vvld = vector<vld>;

typedef pair<ll, ll> P;

#define bit(n) (1LL << (n))

// #define int long long

#define all(v) v.begin(), v.end()

#define rep(i, n) for (ll i = 0; i < n; i++)
#define REP(i, n) for (ll i = 1; i < n; i++)

#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define FORm(i, m) for (auto i = m.begin(); i != m.end(); i++)

template <class T> inline void chmax(T &a, T b) { a = std::max(a, b); }
template <class T> inline void chmin(T &a, T b) { a = std::min(a, b); }

#define mod (ll)(1e9 + 7)
// #define mod (998244353ll)

const long long INF = 1LL << 60;

ll d;
vll c(26);
vvll s;

vll create_first_answer() {
  vll ans(d);
  vll last(26, -1);

  rep(i, d) {
    vll cand(26);

    rep(j, 26) { cand[i] = s[i][j] + c[j] * (i - last[j]); }

    ll choice = distance(cand.begin(), max_element(all(cand)));
    ans[i] = choice;
    last[choice] = i;
  }
  return ans;
}

ll update_diff(ll idx, vll &diff, const vll &choice) {
  ll last = -1;
  ll total = 0;
  rep(today, d) {
    if (choice[today] == idx) {
      last = today;
      diff[today] = s[today][idx];
    } else {
      diff[today] = -c[idx] * (today - last);
    }
    total += diff[today];
  }
  return total;
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);

  cin >> d;
  rep(i, 26) { cin >> c[i]; }

  s = vvll(d, vll(26));
  rep(i, d) {
    rep(j, 26) { cin >> s[i][j]; }
  }

  srand(s[0][0]);

  vll ans(d);

  ans = create_first_answer();
#if 0
  vvll score_diff(26, vll(d));
  vll score_con(26);
  ll score = 0;

  rep(i, 26) {
    score_con[i] = update_diff(i, score_diff[i], ans);
    score += score_con[i];
  }

  rep(i, 100000) {
    vll cand = ans;
    vb changed(26);

    rep(j, 5) {
      ll tgt = (i + j) % d;
      cand[tgt] = rand() % 26;

      changed[ans[tgt]] = true;
      changed[cand[tgt]] = true;
    }

    ll cs = score;
    vll score_cand = score_con;
    rep(i, 26) {
      if (changed[i]) {
        cs -= score_cand[i];
        score_cand[i] = update_diff(i, score_diff[i], cand);
        cs += score_cand[i];
      }
    }
    if (cs > score) {
      ans = cand;
      score_con = score_cand;
    }
  }
#endif
  rep(i, d) { cout << ans[i] + 1 << endl; }

  return 0;
}