Problem p02618 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define repl(i, l, r) for (ll i = (l); i < (r); i++)
#define per(i, n) for (ll i = n - 1; i >= 0; i--)
#define perl(i, r, l) for (ll i = r - 1; i >= l; i--)
#define fi first
#define se second
#define pb push_back
#define ins insert
#define pqueue(x) priority_queue<x, vector<x>, greater<x>>
#define all(x) (x).begin(), (x).end()
#define CST(x) cout << fixed << setprecision(x)
#define vtpl(x, y, z) vector<tuple<x, y, z>>
#define rev(x) reverse(x);
using ll = long long;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using pl = pair<ll, ll>;
using vpl = vector<pl>;
using vvpl = vector<vpl>;
const ll MOD = 1000000007;
const ll MOD9 = 998244353;
const int inf = 1e9 + 10;
const ll INF = 4e18;
const ll dy[9] = {1, 0, -1, 0, 1, 1, -1, -1, 0};
const ll dx[9] = {0, -1, 0, 1, 1, -1, 1, -1, 0};
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
int main() {
  ll d;
  cin >> d;
  vl c(26);
  rep(i, 26) cin >> c[i];
  vvl s(d, vl(26));
  rep(i, d) rep(j, 26) cin >> s[i][j];
  vector<set<ll>> ch(26);
  rep(i, 26) ch[i].ins(-1), ch[i].ins(d);
  vl days(d);
  ll pre = 0;
  vl last(26, -1);
  rep(i, d) {
    ll t = 0;
    ll maxi = -INF;
    rep(j, 26) {
      ll p = s[i][j];
      rep(k, 26) {
        if (k == j)
          continue;
        p -= c[k] * (i - last[k]);
      }
      if (chmax(maxi, p))
        t = j;
    }
    days[i] = t;
    last[t] = i;
  }
  rep(i, d) {
    ll t = days[i];
    pre += s[i][t];
    ch[t].ins(i);
    days[i] = t;
  }
  ll score = pre;
  rep(i, 26) {
    ll mini = 0;
    ll pp = -1;
    for (auto a : ch[i]) {
      mini += (a - pp) * (a - pp - 1) / 2;
      pp = a;
    }
    score -= c[i] * mini;
  }
  // rep(i,d)cout << days[i] <<endl;

  ll q = 9000000;
  while (q--) {
    ll x = rand() % d, y = rand() % 26;
    if (days[x] == y)
      y += 1, y %= 26;
    // cout << x <<" " << y <<endl;
    ll old = days[x];
    pre -= s[x][days[x]];
    ch[days[x]].erase(x);
    days[x] = y;
    pre += s[x][y];
    ch[y].ins(x);
    ll p = 0;
    rep(i, 26) {
      ll mini = 0;
      ll pp = -1;
      for (auto a : ch[i]) {
        mini += (a - pp) * (a - pp - 1) / 2;
        pp = a;
      }
      p += c[i] * mini;
    }
    // if(q>299990)cout << score <<endl;
    if (pre - p >= score) {
      score = pre - p;
    } else {
      ch[y].erase(x);
      pre -= s[x][y];
      days[x] = old;
      ch[days[x]].ins(x);
      pre += s[x][old];
    }
  }
  rep(i, d) cout << days[i] + 1 << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define repl(i, l, r) for (ll i = (l); i < (r); i++)
#define per(i, n) for (ll i = n - 1; i >= 0; i--)
#define perl(i, r, l) for (ll i = r - 1; i >= l; i--)
#define fi first
#define se second
#define pb push_back
#define ins insert
#define pqueue(x) priority_queue<x, vector<x>, greater<x>>
#define all(x) (x).begin(), (x).end()
#define CST(x) cout << fixed << setprecision(x)
#define vtpl(x, y, z) vector<tuple<x, y, z>>
#define rev(x) reverse(x);
using ll = long long;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using pl = pair<ll, ll>;
using vpl = vector<pl>;
using vvpl = vector<vpl>;
const ll MOD = 1000000007;
const ll MOD9 = 998244353;
const int inf = 1e9 + 10;
const ll INF = 4e18;
const ll dy[9] = {1, 0, -1, 0, 1, 1, -1, -1, 0};
const ll dx[9] = {0, -1, 0, 1, 1, -1, 1, -1, 0};
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
int main() {
  ll d;
  cin >> d;
  vl c(26);
  rep(i, 26) cin >> c[i];
  vvl s(d, vl(26));
  rep(i, d) rep(j, 26) cin >> s[i][j];
  vector<set<ll>> ch(26);
  rep(i, 26) ch[i].ins(-1), ch[i].ins(d);
  vl days(d);
  ll pre = 0;
  vl last(26, -1);
  rep(i, d) {
    ll t = 0;
    ll maxi = -INF;
    rep(j, 26) {
      ll p = s[i][j];
      rep(k, 26) {
        if (k == j)
          continue;
        p -= c[k] * (i - last[k]);
      }
      if (chmax(maxi, p))
        t = j;
    }
    days[i] = t;
    last[t] = i;
  }
  rep(i, d) {
    ll t = days[i];
    pre += s[i][t];
    ch[t].ins(i);
    days[i] = t;
  }
  ll score = pre;
  rep(i, 26) {
    ll mini = 0;
    ll pp = -1;
    for (auto a : ch[i]) {
      mini += (a - pp) * (a - pp - 1) / 2;
      pp = a;
    }
    score -= c[i] * mini;
  }
  // rep(i,d)cout << days[i] <<endl;

  ll q = 950000;
  while (q--) {
    ll x = rand() % d, y = rand() % 26;
    if (days[x] == y)
      y += 1, y %= 26;
    // cout << x <<" " << y <<endl;
    ll old = days[x];
    pre -= s[x][days[x]];
    ch[days[x]].erase(x);
    days[x] = y;
    pre += s[x][y];
    ch[y].ins(x);
    ll p = 0;
    rep(i, 26) {
      ll mini = 0;
      ll pp = -1;
      for (auto a : ch[i]) {
        mini += (a - pp) * (a - pp - 1) / 2;
        pp = a;
      }
      p += c[i] * mini;
    }
    // if(q>299990)cout << score <<endl;
    if (pre - p >= score) {
      score = pre - p;
    } else {
      ch[y].erase(x);
      pre -= s[x][y];
      days[x] = old;
      ch[days[x]].ins(x);
      pre += s[x][old];
    }
  }
  rep(i, d) cout << days[i] + 1 << endl;
}