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

#include <bits/stdc++.h>
#define ft first
#define sc second
#define pt(sth) cout << sth << "\\n"
#define moca(a, s, b) a = ((a)s(b) + MOD) % MOD
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
static const ll INF = 1e18;
static const ll MAX = 101010;
static const ll MOD = 1e9 + 7;

// for(i=0; i<N; i++) cin >> a[i];

ll D;
ll c[26];
ll s[366][26];

ll calcScore(vector<ll> sche) {
  ll score = 0;
  ll i, j, k;
  ll p[26];
  for (i = 0; i < 26; i++)
    p[i] = -1;

  for (ll d = 0; d < sche.size(); d++) {
    score += s[d][sche[d]];

    p[sche[d]] = d;
    for (i = 0; i < 26; i++)
      score -= c[i] * (d - p[i]);
  }

  return score;
}

int main(void) {
  ll i, j, k;

  cin >> D;
  for (i = 0; i < 26; i++)
    cin >> c[i];
  for (i = 0; i < D; i++) {
    for (j = 0; j < 26; j++) {
      cin >> s[i][j];
    }
  }

  ll width = 3;
  priority_queue<pair<ll, vector<ll>>> candidates;
  vector<vector<ll>> tops;
  tops.push_back({});

  for (ll d = 0; d < D; d++) {

    for (k = 0; k < tops.size(); k++) {
      for (i = 0; i < 26; i++) {
        tops[k].push_back(i);
        candidates.push({calcScore(tops[k]), tops[k]});
        tops[k].pop_back();
      }
    }

    tops.clear();
    for (i = 0; i < min(width, (ll)candidates.size()); i++) {
      tops.push_back(candidates.top().sc);
      candidates.pop();
    }
  }

  for (i = 0; i < D; i++) {
    ll t = tops[0][i] + 1;
    if (1 <= t && t <= 26)
      pt(t);
    else
      pt(1);
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ft first
#define sc second
#define pt(sth) cout << sth << "\\n"
#define moca(a, s, b) a = ((a)s(b) + MOD) % MOD
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
static const ll INF = 1e18;
static const ll MAX = 101010;
static const ll MOD = 1e9 + 7;

// for(i=0; i<N; i++) cin >> a[i];

ll D;
ll c[26];
ll s[366][26];

ll calcScore(vector<ll> sche) {
  ll score = 0;
  ll i, j, k;
  ll p[26];
  for (i = 0; i < 26; i++)
    p[i] = -1;

  for (ll d = 0; d < sche.size(); d++) {
    score += s[d][sche[d]];

    p[sche[d]] = d;
    for (i = 0; i < 26; i++)
      score -= c[i] * (d - p[i]);
  }

  return score;
}

int main(void) {
  ll i, j, k;

  cin >> D;
  for (i = 0; i < 26; i++)
    cin >> c[i];
  for (i = 0; i < D; i++) {
    for (j = 0; j < 26; j++) {
      cin >> s[i][j];
    }
  }

  vector<ll> res;
  ll score = 0;
  ll width = 1000;

  priority_queue<pair<ll, vector<ll>>> candidates;
  vector<vector<ll>> tops;
  tops.push_back({});

  for (ll d = 0; d < D; d++) {

    for (k = 0; k < tops.size(); k++) {
      for (i = 0; i < 26; i++) {
        tops[k].push_back(i);
        candidates.push({calcScore(tops[k]), tops[k]});
        tops[k].pop_back();
      }
    }

    tops.clear();
    for (i = 0; i < min(width, (ll)candidates.size()); i++) {
      tops.push_back(candidates.top().sc);
      candidates.pop();
    }
  }

  for (auto r : tops[0])
    pt(r + 1);
}