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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define REP(i, n) for (ll(i) = (0); (i) < (n); ++i)
#define REV(i, n) for (ll(i) = (n)-1; (i) >= 0; --i)
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v, n)                                                           \\
  {                                                                            \\
    REP(WW, n) cerr << v[WW] << ' ';                                           \\
    cerr << endl;                                                              \\
  }
#define SHOW2d(v, WW, HH)                                                      \\
  {                                                                            \\
    REP(W_, WW) {                                                              \\
      REP(H_, HH) cerr << v[W_][H_] << ' ';                                    \\
      cerr << endl;                                                            \\
    }                                                                          \\
  }
#define ALL(v) v.begin(), v.end()
#define Decimal fixed << setprecision(20)
#define INF 1000000000
#define LLINF 1000000000000000000LL
#define MOD 998244353

typedef long long ll;
typedef pair<ll, ll> P;

ll cal_score(vector<ll> &c, vector<vector<ll>> &s, vector<ll> v) {
  ll score = 0;
  ll n = v.size();
  vector<ll> now(26);
  REP(i, n) {
    score += s[i][v[i]];
    REP(j, 26) now[j]++;
    now[v[i]] = 0;
    REP(j, 26) score -= c[j] * now[j];
  }
  return score;
}

int main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);

  ll d;
  cin >> d;
  vector<ll> c(26);
  REP(i, 26) cin >> c[i];
  vector<vector<ll>> s(d, vector<ll>(26));
  REP(i, d) REP(j, 26) cin >> s[i][j];

  vector<pair<ll, vector<ll>>> lis;
  REP(i, 26) lis.EB(cal_score(c, s, {i}), vector<ll>(1, i));

  REP(i, d - 1) {
    sort(ALL(lis), greater<pair<ll, vector<ll>>>());
    lis.resize(min((ll)lis.size(), 5LL));
    vector<pair<ll, vector<ll>>> now;

    for (auto seica : lis) {
      REP(i, 26) {
        vector<ll> tmp = seica.SE;
        tmp.PB(i);
        now.EB(cal_score(c, s, tmp), tmp);
      }
    }

    lis = now;
  }

  sort(ALL(lis), greater<pair<ll, vector<ll>>>());
  REP(i, d) { cout << (lis[0].SE[i] + 1) << endl; }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define REP(i, n) for (ll(i) = (0); (i) < (n); ++i)
#define REV(i, n) for (ll(i) = (n)-1; (i) >= 0; --i)
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v, n)                                                           \\
  {                                                                            \\
    REP(WW, n) cerr << v[WW] << ' ';                                           \\
    cerr << endl;                                                              \\
  }
#define SHOW2d(v, WW, HH)                                                      \\
  {                                                                            \\
    REP(W_, WW) {                                                              \\
      REP(H_, HH) cerr << v[W_][H_] << ' ';                                    \\
      cerr << endl;                                                            \\
    }                                                                          \\
  }
#define ALL(v) v.begin(), v.end()
#define Decimal fixed << setprecision(20)
#define INF 1000000000
#define LLINF 1000000000000000000LL
#define MOD 998244353

typedef long long ll;
typedef pair<ll, ll> P;

ll cal_score(vector<ll> &c, vector<vector<ll>> &s, vector<ll> v) {
  ll score = 0;
  ll n = v.size();
  vector<ll> now(26);
  REP(i, n) {
    score += s[i][v[i]];
    REP(j, 26) now[j]++;
    now[v[i]] = 0;
    REP(j, 26) score -= c[j] * now[j];
  }
  return score;
}

int main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);

  ll d;
  cin >> d;
  vector<ll> c(26);
  REP(i, 26) cin >> c[i];
  vector<vector<ll>> s(d, vector<ll>(26));
  REP(i, d) REP(j, 26) cin >> s[i][j];

  vector<pair<ll, vector<ll>>> lis;
  REP(i, 26) lis.EB(cal_score(c, s, {i}), vector<ll>(1, i));

  REP(i, d - 1) {
    sort(ALL(lis), greater<pair<ll, vector<ll>>>());
    lis.resize(min((ll)lis.size(), 1000LL));
    vector<pair<ll, vector<ll>>> now;

    for (auto seica : lis) {
      REP(i, 26) {
        vector<ll> tmp = seica.SE;
        tmp.PB(i);
        now.EB(cal_score(c, s, tmp), tmp);
      }
    }

    lis = now;
  }

  sort(ALL(lis), greater<pair<ll, vector<ll>>>());
  REP(i, d) { cout << (lis[0].SE[i] + 1) << endl; }

  return 0;
}