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

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

inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<long long> VL;
typedef vector<vector<long long>> VVL;
typedef vector<string> VS;
typedef pair<int, int> P;
typedef tuple<int, int, int> tpl;

#define ALL(a) (a).begin(), (a).end()
#define SORT(c) sort((c).begin(), (c).end())
#define REVERSE(c) reverse((c).begin(), (c).end())
#define EXIST(m, v) (m).find((v)) != (m).end()
#define LB(a, x) lower_bound((a).begin(), (a).end(), x) - (a).begin()
#define UB(a, x) upper_bound((a).begin(), (a).end(), x) - (a).begin()

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define RFOR(i, a, b) for (int i = (a)-1; i >= (b); --i)
#define RREP(i, n) RFOR(i, n, 0)

#define en "\\n"

constexpr double EPS = 1e-9;
constexpr double PI = 3.1415926535897932;
constexpr int INF = 2147483647;
constexpr long long LINF = 1LL << 60;
constexpr long long MOD = 1000000007; // 998244353;

#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <typename Iterator>
inline bool next_combination(const Iterator first, Iterator k,
                             const Iterator last) {
  /* Credits: Thomas Draper */
  if ((first == last) || (first == k) || (last == k))
    return false;
  Iterator itr1 = first;
  Iterator itr2 = last;
  ++itr1;
  if (last == itr1)
    return false;
  itr1 = last;
  --itr1;
  itr1 = k;
  --itr2;
  while (first != itr1) {
    if (*--itr1 < *itr2) {
      Iterator j = k;
      while (!(*itr1 < *j))
        ++j;
      iter_swap(itr1, j);
      ++itr1;
      ++j;
      itr2 = k;
      rotate(itr1, j, last);
      while (last != j) {
        ++j;
        ++itr2;
      }
      rotate(k, itr2, last);
      return true;
    }
  }
  rotate(first, k, last);
  return false;
}

void Main() {
  int D;
  cin >> D;
  VI c(26);
  REP(i, 26) cin >> c[i];
  int s[D][26];
  REP(i, D) REP(j, 26) cin >> s[i][j];
  VI last(26, 0);
  VI ans(D);

  int day = 3;

  REP(i, D) {
    int maxscore = -INF, type = -1;

    REP(j, 26) if (chmax(maxscore, c[j] * (i + 1 - last[j]))) type = j;
    last[type] = i + 1;
    ans[i] = type + 1;
  }

  REP(i, D) cout << ans[i] << en;
  return;
}

int main(void) {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(15);
  int t = 1; // cin>>t;
  REP(_, t) Main();
  return 0;
}

Error: TLE

Fixed submission:

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

inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<long long> VL;
typedef vector<vector<long long>> VVL;
typedef vector<string> VS;
typedef pair<int, int> P;
typedef tuple<int, int, int> tpl;

#define ALL(a) (a).begin(), (a).end()
#define SORT(c) sort((c).begin(), (c).end())
#define REVERSE(c) reverse((c).begin(), (c).end())
#define EXIST(m, v) (m).find((v)) != (m).end()
#define LB(a, x) lower_bound((a).begin(), (a).end(), x) - (a).begin()
#define UB(a, x) upper_bound((a).begin(), (a).end(), x) - (a).begin()

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define RFOR(i, a, b) for (int i = (a)-1; i >= (b); --i)
#define RREP(i, n) RFOR(i, n, 0)

#define en "\\n"

constexpr double EPS = 1e-9;
constexpr double PI = 3.1415926535897932;
constexpr int INF = 2147483647;
constexpr long long LINF = 1LL << 60;
constexpr long long MOD = 1000000007; // 998244353;

#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <typename Iterator>
inline bool next_combination(const Iterator first, Iterator k,
                             const Iterator last) {
  /* Credits: Thomas Draper */
  if ((first == last) || (first == k) || (last == k))
    return false;
  Iterator itr1 = first;
  Iterator itr2 = last;
  ++itr1;
  if (last == itr1)
    return false;
  itr1 = last;
  --itr1;
  itr1 = k;
  --itr2;
  while (first != itr1) {
    if (*--itr1 < *itr2) {
      Iterator j = k;
      while (!(*itr1 < *j))
        ++j;
      iter_swap(itr1, j);
      ++itr1;
      ++j;
      itr2 = k;
      rotate(itr1, j, last);
      while (last != j) {
        ++j;
        ++itr2;
      }
      rotate(k, itr2, last);
      return true;
    }
  }
  rotate(first, k, last);
  return false;
}

void Main() {
  int D;
  cin >> D;
  VI c(26);
  REP(i, 26) cin >> c[i];
  int s[D][26];
  REP(i, D) REP(j, 26) cin >> s[i][j];
  VI last(26, 0);
  VI ans(D);

  int day = 4;

  REP(i, D) {
    int maxscore = -INF;
    VI type(day, -1);

    VI p(26);
    REP(j, 26) p[j] = j;
    do {
      VI q(day);
      REP(j, day) q[j] = p[j];
      bool flag = false;
      REP(j, day) if (i >= day && i + j - last[q[j]] < day) flag = true;
      if (flag)
        continue;
      do {
        int tmp = 0;
        VI prev_last(day);
        REP(j, day) prev_last[j] = last[q[j]];
        REP(j, day) {
          tmp += s[i + j][q[j]];
          last[q[j]] = i + 1 + j;
          REP(k, 26) tmp -= c[k] * (i + 1 + j - last[k]);
        }
        REP(j, day) last[q[j]] = prev_last[j];
        if (chmax(maxscore, tmp)) {
          REP(j, day) type[j] = q[j];
        }
      } while (next_permutation(ALL(q)));
    } while (next_combination(p.begin(), p.begin() + day, p.end()));
    REP(j, 1) {
      ans[i + j] = type[j] + 1;
      last[type[j]] = i + 1 + j;
    }
    // i += day-1;
  }

  REP(i, D) cout << ans[i] << en;
  return;
}

int main(void) {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(15);
  int t = 1; // cin>>t;
  REP(_, t) Main();
  return 0;
}