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

#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
using ll = long long;
using ld = long double;
#define repd(i, a, b) for (int i = a; i < int(b); i++)
#define rep(i, n) repd(i, 0, n)
#define VARNAME(v) #v
using P = pair<int, int>;

int main() {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  constexpr int BEAM = 510;
  int d;
  vector<int> c(26);
  cin >> d;
  rep(i, 26) cin >> c[i];
  vector<array<int, 26>> s(d);
  rep(i, d) rep(j, 26) cin >> s[i][j];
  vector<int> curs = {0};
  vector<array<int, 26>> curl(1);
  vector<vector<int>> cura(1);
  rep(i, d) {
    int len = int(curs.size()) * 26, ind = 0;
    vector<P> nxs(len);
    vector<array<int, 26>> nxl(len);
    vector<vector<int>> nxa(len);
    rep(j, curs.size()) {
      rep(t, 26) {
        int score = curs[j];
        array<int, 26> last = curl[j];
        vector<int> ans = cura[j];
        score += s[i][t];
        last[t] = i + 1;
        rep(k, 26) score -= c[k] * (i + 1 - last[k]);
        ans.emplace_back(t);
        nxs[ind] = P(score, ind);
        nxl[ind] = last;
        nxa[ind] = ans;
        ind++;
      }
    }
    sort(nxs.rbegin(), nxs.rend());
    len = min(len, BEAM);
    curs.resize(len);
    curl.resize(len);
    cura.resize(len);
    rep(j, len) {
      curs[j] = nxs[j].first;
      curl[j] = nxl[nxs[j].second];
      cura[j] = nxa[nxs[j].second];
    }
  }
  int ans = 0;
  rep(i, min(int(curs.size()), BEAM)) {
    if (curs[ans] < curs[i])
      ans = i;
  }

  for (auto x : cura[ans]) {
    cout << x + 1 << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
using ll = long long;
using ld = long double;
#define repd(i, a, b) for (int i = a; i < int(b); i++)
#define rep(i, n) repd(i, 0, n)
#define VARNAME(v) #v
using P = pair<int, int>;

int main() {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  constexpr int BEAM = 600;
  int d;
  vector<int> c(26);
  cin >> d;
  rep(i, 26) cin >> c[i];
  vector<array<int, 26>> s(d);
  rep(i, d) rep(j, 26) cin >> s[i][j];
  vector<int> curs = {0};
  vector<array<int, 26>> curl(1);
  vector<vector<int>> cura(1);
  rep(i, d) {
    int len = int(curs.size()) * 26, ind = 0;
    vector<P> nxs(len);
    vector<array<int, 26>> nxl(len);
    vector<vector<int>> nxa(len);
    rep(j, curs.size()) {
      rep(t, 26) {
        int score = curs[j];
        array<int, 26> last = curl[j];
        vector<int> ans = cura[j];
        score += s[i][t];
        last[t] = i + 1;
        rep(k, 26) score -= c[k] * (i + 1 - last[k]);
        ans.emplace_back(t);
        nxs[ind] = P(score, ind);
        nxl[ind] = last;
        nxa[ind] = ans;
        ind++;
      }
    }
    sort(nxs.rbegin(), nxs.rend());
    len = min(len, BEAM);
    curs.resize(len);
    curl.resize(len);
    cura.resize(len);
    rep(j, len) {
      curs[j] = nxs[j].first;
      curl[j] = nxl[nxs[j].second];
      cura[j] = nxa[nxs[j].second];
    }
  }
  int ans = 0;
  rep(i, min(int(curs.size()), BEAM)) {
    if (curs[ans] < curs[i])
      ans = i;
  }

  for (auto x : cura[ans]) {
    cout << x + 1 << endl;
  }
}