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

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef pair<ll, ll> P;
typedef vector<P> vp;
typedef vector<string> vs;
typedef vector<bool> vb;

const ll mod = 1e9 + 7;
const ll inf = 1e16;

#define rep(i, m, n) for (ll i = m; i < n; i++)
#define repr(i, m, n) for (ll i = m - 1; i >= n; i--)
#define fi first
#define se second
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define eb(x) emplace_back(x)
#define all(x) x.begin(), x.end()
#define sum(x) accumulate(all(x), 0)
#define gll greater<ll>()

void solve() {
  ll d;
  cin >> d;
  vll c(26);
  rep(i, 0, 26) cin >> c[i];

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

  vll t(d);
  rep(i, 0, d) t[i] = (i % 26);

  vector<set<ll>> v(26);
  rep(j, 0, 26) {
    v[j].insert(-1);
    v[j].insert(d);
  }

  ll ans = 0;
  rep(i, 0, d) {
    ans += s[i][t[i]];
    v[t[i]].insert(i);
    rep(j, 0, 26) ans -= c[j] * (i - *v[j].rbegin());
  }

  rep(k, 0, 1000) {
    rep(i, 0, d) {
      rep(j, 1, 26) {
        ll cost = ans;
        auto prev = v[t[i]].find(i);
        auto next = prev;
        prev--;
        next++;
        cost = cost - c[t[i]] * (i - *prev) * (*next - i);
        v[t[i]].erase(i);
        t[i] = (t[i] + j) % 26;

        v[t[i]].insert(i);
        prev = v[t[i]].find(i);
        next = prev;
        prev--;
        next++;
        cost = cost + c[t[i]] * (i - *prev) * (*next - i);

        if (cost > ans) {
          ans = cost;
        } else {
          v[t[i]].erase(i);
          t[i] = (t[i] - j + 26) % 26;
          v[t[i]].insert(i);
        }
      }
    }
  }

  rep(i, 0, d) cout << t[i] + 1 << endl;
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  solve();
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef pair<ll, ll> P;
typedef vector<P> vp;
typedef vector<string> vs;
typedef vector<bool> vb;

const ll mod = 1e9 + 7;
const ll inf = 1e16;

#define rep(i, m, n) for (ll i = m; i < n; i++)
#define repr(i, m, n) for (ll i = m - 1; i >= n; i--)
#define fi first
#define se second
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define eb(x) emplace_back(x)
#define all(x) x.begin(), x.end()
#define sum(x) accumulate(all(x), 0)
#define gll greater<ll>()

void solve() {
  ll d;
  cin >> d;
  vll c(26);
  rep(i, 0, 26) cin >> c[i];

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

  vll t(d);
  rep(i, 0, d) t[i] = (i % 26);

  vector<set<ll>> v(26);
  rep(j, 0, 26) {
    v[j].insert(-1);
    v[j].insert(d);
  }

  ll ans = 0;
  rep(i, 0, d) {
    ans += s[i][t[i]];
    v[t[i]].insert(i);
    rep(j, 0, 26) ans -= c[j] * (i - *v[j].rbegin());
  }

  rep(k, 0, 100000) {
    rep(i, 0, d) {
      ll cost = ans;
      auto prev = v[t[i]].find(i);
      auto next = prev;
      prev--;
      next++;
      cost = cost - c[t[i]] * (i - *prev) * (*next - i);
      v[t[i]].erase(i);
      t[i] = (t[i] + 1) % 26;

      v[t[i]].insert(i);
      prev = v[t[i]].find(i);
      next = prev;
      prev--;
      next++;
      cost = cost + c[t[i]] * (i - *prev) * (*next - i);

      if (cost > ans) {
        ans = cost;
      } else {
        v[t[i]].erase(i);
        t[i] = (t[i] + 25) % 26;
        v[t[i]].insert(i);
      }
      //

      cost = ans;
      prev = v[t[i]].find(i);
      next = prev;
      prev--;
      next++;
      cost = cost - c[t[i]] * (i - *prev) * (*next - i);
      v[t[i]].erase(i);
      t[i] = (t[i] + 25) % 26;

      v[t[i]].insert(i);
      prev = v[t[i]].find(i);
      next = prev;
      prev--;
      next++;
      cost = cost + c[t[i]] * (i - *prev) * (*next - i);

      if (cost > ans) {
        ans = cost;
      } else {
        v[t[i]].erase(i);
        t[i] = (t[i] + 1) % 26;
        v[t[i]].insert(i);
      }
    }
  }

  rep(i, 0, d) cout << t[i] << endl;
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  solve();
}