Problem p02618 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define P pair<int, int>
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(v) v.begin(), v.end()
#define pb push_back
constexpr int INF = 1000000000;
int Random(int mi, int ma) {
  random_device rnd;
  mt19937 mt(rnd()); // 32bit
  //[mi,ma]
  uniform_int_distribution<int> engine(mi, ma);
  return engine(mt);
}
int d, c[26], s[365][26], t[365];
set<int> st[26];
int toscore(int dd, int q) {
  int from = t[dd], to = q, score = s[d][to] - s[d][from];
  int r = *st[from].upper_bound(dd);
  auto it = st[from].lower_bound(dd);
  it--;
  int l = *it;
  score -= (r - dd) * (dd - l);
  r = *st[to].upper_bound(dd);
  it = st[to].lower_bound(dd);
  it--;
  l = *it;
  score += (r - dd) * (dd - l);
  return score;
}
int score = 0;
void init() {
  int last[26];
  fill(last, last + 26, -1);
  rep(i, d) {
    int maidx = -1, ma = -INF;
    rep(j, 26) {
      int score = s[i][j];
      int memo = last[j];
      last[j] = i;
      rep(k, 26) score -= c[k] * (i - last[k]);
      last[j] = memo;
      if (ma < score) {
        ma = score;
        maidx = j;
      }
    }
    last[maidx] = i;
    t[i] = maidx;
    st[t[i]].insert(i);
  }
  rep(i, 26) {
    st[i].insert(-1);
    st[i].insert(d);
  }
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> d;
  rep(i, 26) cin >> c[i];
  rep(i, d) {
    rep(j, 26) { cin >> s[i][j]; }
  }
  init();
  int score = 0, last[26];
  fill(last, last + 26, -1);
  rep(i, d) {
    score += s[i][t[i]];
    last[t[i]] = i;
    rep(j, 26) score -= c[j] * (i - last[j]);
  }
  rep(_, 170000) {
    int dd = Random(0, d - 1), q = Random(0, 24);
    if (t[dd] <= q)
      q++;
    int to = toscore(dd, q);
    if (to > 0) {
      score += to;
      int from = t[dd], to = q;
      t[dd] = to;
      st[from].erase(dd);
      st[to].insert(dd);
    }
  }
  rep(i, d) cout << t[i] + 1 << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define P pair<int, int>
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(v) v.begin(), v.end()
#define pb push_back
constexpr int INF = 1000000000;
int Random(int mi, int ma) {
  random_device rnd;
  mt19937 mt(rnd()); // 32bit
  //[mi,ma]
  uniform_int_distribution<int> engine(mi, ma);
  return engine(mt);
}
int d, c[26], s[365][26], t[365];
set<int> st[26];
int toscore(int dd, int q) {
  int from = t[dd], to = q, score = s[d][to] - s[d][from];
  int r = *st[from].upper_bound(dd);
  auto it = st[from].lower_bound(dd);
  it--;
  int l = *it;
  score -= (r - dd) * (dd - l);
  r = *st[to].upper_bound(dd);
  it = st[to].lower_bound(dd);
  it--;
  l = *it;
  score += (r - dd) * (dd - l);
  return score;
}
int score = 0;
void init() {
  int last[26];
  fill(last, last + 26, -1);
  rep(i, d) {
    int maidx = -1, ma = -INF;
    rep(j, 26) {
      int score = s[i][j];
      int memo = last[j];
      last[j] = i;
      rep(k, 26) score -= c[k] * (i - last[k]);
      last[j] = memo;
      if (ma < score) {
        ma = score;
        maidx = j;
      }
    }
    last[maidx] = i;
    t[i] = maidx;
    st[t[i]].insert(i);
  }
  rep(i, 26) {
    st[i].insert(-1);
    st[i].insert(d);
  }
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> d;
  rep(i, 26) cin >> c[i];
  rep(i, d) {
    rep(j, 26) { cin >> s[i][j]; }
  }
  init();
  int score = 0, last[26];
  fill(last, last + 26, -1);
  rep(i, d) {
    score += s[i][t[i]];
    last[t[i]] = i;
    rep(j, 26) score -= c[j] * (i - last[j]);
  }
  rep(_, 150000) {
    int dd = Random(0, d - 1), q = Random(0, 24);
    if (t[dd] <= q)
      q++;
    int to = toscore(dd, q);
    if (to > 0) {
      score += to;
      int from = t[dd], to = q;
      t[dd] = to;
      st[from].erase(dd);
      st[to].insert(dd);
    }
  }
  rep(i, d) cout << t[i] + 1 << endl;
  return 0;
}