Problem p02618 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef long long ll;

ll INF = 1e10;

int main() {

  cin.tie(0);
  ios::sync_with_stdio(false);

  int D;
  cin >> D;
  int E = min(10, D);

  vector<ll> c(27);
  for (int i = 1; i <= 26; ++i)
    cin >> c[i];

  vector<vector<ll>> s(D + 1, vector<ll>(27, 0));
  for (int i = 1; i <= D; ++i) {
    for (int j = 1; j <= 26; ++j) {
      cin >> s[i][j];
    }
  }

  vector<ll> t(D + 1);
  vector<set<ll>> next(27);
  vector<ll> test(D + 1);

  ll ans = 0;
  ll val = -INF;

  for (int cnt = 0; cnt < 3000; ++cnt) {

    vector<set<ll>> nest(27);
    for (int i = 1; i <= 26; ++i) {
      nest[i].insert(0);
      nest[i].insert(D + 1);
    }

    for (int i = 1; i <= E; ++i) {
      test[i] = rand() % 26;
      ++test[i];
      nest[test[i]].insert(i);
    }

    for (int i = E + 1; i <= D; ++i) {
      ll ind = 1;
      ll sum = -INF;
      for (int j = 1; j <= 26; ++j) {
        auto k = nest[j].lower_bound(i);
        --k;
        ll l = *k;
        ll cmp = s[i][j] + c[j] * (i - l);
        if (cmp > sum) {
          sum = cmp;
          ind = j;
        }
      }
      test[i] = ind;
      nest[ind].insert(i);
    }

    ll ch = 0;
    for (int i = 1; i <= D; ++i) {
      ch += s[i][test[i]];
      for (int j = 1; j <= 26; ++j) {
        auto k = nest[j].upper_bound(i);
        --k;
        ll l = *k;
        ch -= c[j] * (i - l);
      }
    }

    if (ch > val) {
      val = ch;
      ans = val;
      for (int i = 1; i <= D; ++i)
        t[i] = test[i];
    }
  }

  for (int i = 1; i <= D; ++i) {
    next[t[i]].insert(i);
  }
  for (int i = 1; i <= 26; ++i) {
    next[i].insert(0);
    next[i].insert(D + 1);
  }

  for (int i = 0; i < 3000000; ++i) {

    int d = rand() % D;
    ++d;
    int q = rand() % 26;
    ++q;
    ll diff = 0;

    // up
    ll l, r;
    auto j = next[q].lower_bound(d);
    r = *j;
    --j;
    l = *j;
    diff += s[d][q] + ((r - d) * (d - l)) * c[q];

    // down
    next[t[d]].erase(d);
    auto k = next[t[d]].lower_bound(d);
    r = *k;
    --k;
    l = *k;
    diff -= s[d][t[d]] + ((r - d) * (d - l)) * c[t[d]];

    // update
    if (diff > 0) {
      next[q].insert(d);
      t[d] = q;
      ans += diff;
    } else {
      next[t[d]].insert(d);
    }
  }

  for (int i = 1; i <= D; ++i)
    cout << t[i] << "\\n";

  // cout<<ans<<"\\n";
}

Error: TLE

Fixed submission:

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

typedef long long ll;

int main() {

  cin.tie(0);
  ios::sync_with_stdio(false);

  int D;
  cin >> D;

  vector<ll> c(27);
  for (int i = 1; i <= 26; ++i)
    cin >> c[i];
  vector<vector<ll>> s(D + 1, vector<ll>(27, 0));
  for (int i = 1; i <= D; ++i) {
    for (int j = 1; j <= 26; ++j) {
      cin >> s[i][j];
    }
  }

  vector<ll> t(D + 1, 1);
  vector<set<ll>> next(27);
  vector<ll> test(D + 1);

  ll ans = 0;
  ll val = -1e10;

  for (int cnt = 0; cnt < 3000; ++cnt) {

    vector<set<ll>> nest(27);

    for (int i = 1; i <= D; ++i) {
      test[i] = rand() % 26;
      ++test[i];
    }

    for (int i = 1; i <= D; ++i) {
      nest[test[i]].insert(i);
    }
    for (int i = 1; i <= 26; ++i) {
      nest[i].insert(0);
      nest[i].insert(D + 1);
    }

    ll ch = 0;
    for (int i = 1; i <= D; ++i) {
      ch += s[i][test[i]];
      for (int j = 1; j <= 26; ++j) {
        auto k = nest[j].upper_bound(i);
        --k;
        ll l = *k;
        ch -= c[j] * (i - l);
      }
    }
    if (ch > val) {
      val = ch;
      ans = ch;
      for (int i = 1; i <= D; ++i)
        t[i] = test[i];
    }
  }

  for (int i = 1; i <= D; ++i) {
    next[t[i]].insert(i);
  }
  for (int i = 1; i <= 26; ++i) {
    next[i].insert(0);
    next[i].insert(D + 1);
  }

  for (int i = 0; i < 10000000; ++i) {

    int d = rand() % D;
    ++d;
    int q = rand() % 26;
    ++q;
    ll diff = 0;

    // up
    ll l, r;
    auto j = next[q].lower_bound(d);
    r = *j;
    --j;
    l = *j;
    diff += s[d][q] + ((r - d) * (d - l)) * c[q];

    // down
    next[t[d]].erase(d);
    auto k = next[t[d]].lower_bound(d);
    r = *k;
    --k;
    l = *k;
    diff -= s[d][t[d]] + ((r - d) * (d - l)) * c[t[d]];

    // update
    if (diff > 0) {
      next[q].insert(d);
      t[d] = q;
      ans += diff;
    } else {
      next[t[d]].insert(d);
    }
  }

  for (int i = 1; i <= D; ++i)
    cout << t[i] << "\\n";

  // cout<<ans<<"\\n";
}