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

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

long long calc(int n, const vector<int> &options, const vector<int> &cost,
               const vector<array<int, 26>> &s, int dd, int nt, int ot,
               int total) {
  // long long x(total);
  int before(dd - 1), after(dd + 1);
  while (before >= 0 && options[before] != nt)
    before--;
  while (after < n && options[after] != nt)
    after++;
  int a(dd - before - 1), b(after - dd - 1), c(after - before - 1);
  int diff = c * (c + 1) / 2 - a * (a + 1) / 2 - b * (b + 1) / 2;
  int before2(dd - 1), after2(dd + 1);
  while (before2 >= 0 && options[before2] != ot)
    before2--;
  while (after2 < n && options[after2] != ot)
    after2++;
  int a2(dd - before2 - 1), b2(after2 - dd - 1), c2(after2 - before2 - 1);
  int diff2 = c2 * (c2 + 1) / 2 - a2 * (a2 + 1) / 2 - b2 * (b2 + 1) / 2;
  return total - (long long)diff * cost[nt] + (long long)diff2 * cost[ot] +
         s[dd][nt] - s[dd][ot];
}
int main() {
  int n;
  cin >> n;
  vector<int> c(26);
  vector<array<int, 26>> s(n);

  vector<int> last(26, -1);

  for (int i = 0; i < 26; ++i)
    cin >> c[i];
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < 26; ++j)
      cin >> s[i][j];

  long long total(0);
  vector<int> options(n);

  for (int i = 0; i < n; ++i) {
    int neg(0), test(-1);
    for (int j = 0; j < 26; ++j) {
      neg -= c[j] * (i - last[j]);
    }
    // cout << neg << 'n' << endl;
    int max_score = -1e9;
    for (int j = 0; j < 26; ++j) {
      int ss = s[i][j] + neg + c[j] * (i - last[j]);
      if (ss > max_score) {
        max_score = ss;
        test = j;
      }
    }
    total += max_score;
    // cout << test + 1 << endl;
    options[i] = test;
    // cout << test + 1 << ' ' << max_score << endl;
    last[test] = i;
  }
  // cout << total << endl;
  for (int t = 1; t <= 10; ++t) {
    for (int dd = 0; dd < n; ++dd) {
      for (int tt = 0; tt < 26; ++tt) {
        if (tt == options[dd])
          continue;
        int ot = options[dd];
        long long ns = calc(n, options, c, s, dd, tt, ot, total);
        if (ns > total) {
          total = ns;
        } else {
          options[dd] = ot;
        }
      }
    }
  }
  for (const int &i : options)
    cout << i + 1 << endl;
  // cout << total << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

long long calc(int n, const vector<int> &options, const vector<int> &c,
               const vector<array<int, 26>> &s) {
  long long score(0);
  vector<int> last(26, -1);
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < 26; ++j) {
      score -= c[j] * (i - last[j]);
    }
    score += s[i][options[i]] + c[options[i]] * (i - last[options[i]]);
  }
  return score;
}
int main() {
  int n;
  cin >> n;
  vector<int> c(26);
  vector<array<int, 26>> s(n);

  vector<int> last(26, -1);

  for (int i = 0; i < 26; ++i)
    cin >> c[i];
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < 26; ++j)
      cin >> s[i][j];

  long long total(0);
  vector<int> options(n);

  for (int i = 0; i < n; ++i) {
    int neg(0), test(-1);
    for (int j = 0; j < 26; ++j) {
      neg -= c[j] * (i - last[j]);
    }
    // cout << neg << 'n' << endl;
    int max_score = -1e9;
    for (int j = 0; j < 26; ++j) {
      int ss = s[i][j] + neg + c[j] * (i - last[j]);
      if (ss > max_score) {
        max_score = ss;
        test = j;
      }
    }
    total += max_score;
    // cout << test + 1 << endl;
    options[i] = test;
    // cout << test + 1 << ' ' << max_score << endl;
    last[test] = i;
  }
  // cout << total << endl;
  for (int t = 1; t <= 100; ++t) {
    for (int dd = 0; dd < n; ++dd) {
      for (int tt = 0; tt < 26; ++tt) {
        if (tt == options[dd])
          continue;
        int ot = options[dd];
        long long ns = calc(n, options, c, s);
        if (ns > total) {
          total = ns;
        } else {
          options[dd] = ot;
        }
      }
    }
  }
  for (const int &i : options)
    cout << i + 1 << endl;
  // cout << total << endl;
  return 0;
}