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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <ciso646>
#include <cmath>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;

// #define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
constexpr ll mod = 1000000007;
const ll INF = mod * mod;
typedef pair<int, int> P;
#define stop                                                                   \\
  char nyaa;                                                                   \\
  cin >> nyaa;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per1(i, n) for (int i = n; i >= 1; i--)
#define Rep1(i, sta, n) for (int i = sta; i <= n; i++)
#define all(v) (v).begin(), (v).end()
typedef pair<ll, ll> LP;
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-12;
const ld pi = acos(-1.0);

int d;
int s[365][26];
int c[26];

int calc(vector<int> &v) {
  int sum = 0;
  int las[26];
  rep(i, 26) las[i] = 0;

  int dec = 0;
  rep(j, 26) dec += c[j];

  int aldec = 0;

  rep(i, d) {
    aldec -= c[v[i]] * (i - las[v[i]]);

    sum += s[i][v[i]];
    las[v[i]] = i;

    sum -= aldec;
    aldec += dec;
  }
  return max(sum, 0);
}

void query(vector<int> v) {
  rep(i, d) { cout << v[i] + 1 << "\\n"; }
}
void solve() {
  cin >> d;
  rep(i, 26) cin >> c[i];
  rep(i, d) rep(j, 26) cin >> s[i][j];

  // init_answer
  vector<int> v;
  int las[26];
  rep(j, 26) las[j] = 0;
  int sum = 0;

  rep(i, d) {
    int ma = -mod;
    int chk = -1;
    rep(j, 26) {
      int ad = s[i][j];
      ad += c[j] * (i - las[j]);
      if (ma < ad) {
        ma = ad;
        chk = j;
      }
    }
    v.push_back(chk);
    las[chk] = i;

    sum += s[i][chk];
    rep(j, 26) { sum -= c[j] * (i - las[j]); }
  }

  random_device rnd;
  mt19937 mt(rnd());
  uniform_int_distribution<> ud(0, d - 1);
  uniform_real_distribution<> rd(0.0, 1.0);
  vector<int> alt = v;

  int cur = sum;

  rep(i, 30000) {
    int id = ud(mt);
    rep(j, 26) {
      if (j == v[id])
        continue;
      alt[id] = j;
      int val = calc(alt);
      if (cur < val) {
        v = alt;
        cur = val;
      }
    }
    alt = v;
  }

  query(v);
}

signed main() {
  solve();
  return 0;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <ciso646>
#include <cmath>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;

// #define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
constexpr ll mod = 1000000007;
const ll INF = mod * mod;
typedef pair<int, int> P;
#define stop                                                                   \\
  char nyaa;                                                                   \\
  cin >> nyaa;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per1(i, n) for (int i = n; i >= 1; i--)
#define Rep1(i, sta, n) for (int i = sta; i <= n; i++)
#define all(v) (v).begin(), (v).end()
typedef pair<ll, ll> LP;
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-12;
const ld pi = acos(-1.0);

int d;
int s[365][26];
int c[26];

int calc2(vector<int> &v) {
  int sum = 0;
  int las[26];
  rep(i, 26) las[i] = -1;
  rep(i, d) {
    sum += s[i][v[i]];
    las[v[i]] = i;
    rep(j, 26) if (las[j] >= 0) { sum -= (i - las[j]) * c[j]; }
  }
  return max(sum, 0);
}

int calc(vector<int> &v) {
  int sum = 0;
  int las[26];
  rep(i, 26) las[i] = -1;

  int dec = 0;

  int aldec = 0;

  rep(i, d) {
    if (las[v[i]] >= 0) {
      aldec -= c[v[i]] * (i - las[v[i]]);
    } else {
      dec += c[v[i]];
    }

    sum += s[i][v[i]];
    las[v[i]] = i;

    sum -= aldec;
    aldec += dec;
  }
  return max(sum, 0);
}

void query(vector<int> v) {
  rep(i, d) { cout << v[i] + 1 << "\\n"; }
}
void solve() {
  cin >> d;
  rep(i, 26) cin >> c[i];
  rep(i, d) rep(j, 26) cin >> s[i][j];

  // init_answer
  vector<int> v;
  int las[26];
  rep(j, 26) las[j] = -1;
  int sum = 0;

  rep(i, d) {
    int ma = -mod;
    int chk = -1;
    rep(j, 26) {
      int ad = s[i][j];
      if (las[j] >= 0) {
        ad += c[j] * (i - las[j]);
      }
      if (ma < ad) {
        ma = ad;
        chk = j;
      }
    }
    v.push_back(chk);
    las[chk] = i;

    sum += s[i][chk];
    rep(j, 26) if (las[j] >= 0) { sum -= c[j] * (i - las[j]); }
  }

  random_device rnd;
  mt19937 mt(rnd());
  uniform_int_distribution<> ud(0, d - 1);
  vector<int> alt = v;

  int cur = sum;
  assert(sum == calc(v));
  rep(i, 5000) {
    int id = ud(mt);
    rep(j, 26) {
      if (j == v[id])
        continue;
      alt[id] = j;
      int val = calc2(alt);
      if (cur < val) {
        v = alt;
        cur = val;
      }
    }
    alt = v;
  }

  query(v);
}

signed main() {
  solve();
  return 0;
}