Problem p02618 (C++)

Buggy submission (status: Time Limit Exceeded):
/*
    Author:zeke

    pass System Test!
    GET AC!!
*/
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using ll = long long;
using ld = long double;
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define rep3(var, min, max) for (ll(var) = (min); (var) < (max); ++(var))
#define repi3(var, min, max) for (ll(var) = (max)-1; (var) + 1 > (min); --(var))
#define Mp(a, b) make_pair((a), (b))
#define F first
#define S second
#define Icin(s)                                                                \
  ll(s);                                                                       \
  cin >> (s);
#define Scin(s)                                                                \
  ll(s);                                                                       \
  cin >> (s);
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;
}
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef vector<V> VV;
typedef vector<P> VP;
ll mod = 1e9 + 7;
ll MOD = 1e9 + 7;
ll INF = 1e18;
// cout << "Case #" << index << " :IMPOSSIBLE";
ll evaluate(VV &S, ll d, V &prev, V &C, ll j) {

  ll temp = 0;
  temp += S[d][j];
  rep(k, 26) {
    if (j == k) {
      continue;
    }
    temp -= C[k] * (d - prev[k]);
  }
  return temp;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  ll d;
  cin >> d;
  V C(26);
  rep(i, 26) cin >> C[i];
  VV S(d, V(26));
  rep(i, d) { rep(j, 26) cin >> S[i][j]; }
  ll width = 330;
  vector<tuple<ll, V, V>> vec;
  V temp;
  rep(i, 26) temp.push_back(-1);
  V temp2;
  vec.push_back({0, temp, temp2});
  rep3(i, 0, d) {
    priority_queue<tuple<ll, V, V>> q;
    ll reg = -INF;
    ll res = 0;
    for (auto j : vec) {
      ll score = get<0>(j);
      V prev = get<1>(j);
      V history = get<2>(j);
      rep(k, 26) {
        ll value = evaluate(S, i, prev, C, k);
        V prev2 = prev;
        prev2[k] = i;
        V history2 = history;
        history2.push_back(k);
        q.push({score + value, prev2, history2});
      }
    }
    vec = {};
    for (ll j = 0; j < width && !q.empty(); j++) {
      vec.push_back(q.top());
      q.pop();
    }
  }
  for (auto i : get<2>(vec[0])) {
    cout << i + 1 << endl;
  }
}


Fixed submission:
/*
    Author:zeke

    pass System Test!
    GET AC!!
*/
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using ll = long long;
using ld = long double;
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define rep3(var, min, max) for (ll(var) = (min); (var) < (max); ++(var))
#define repi3(var, min, max) for (ll(var) = (max)-1; (var) + 1 > (min); --(var))
#define Mp(a, b) make_pair((a), (b))
#define F first
#define S second
#define Icin(s)                                                                \
  ll(s);                                                                       \
  cin >> (s);
#define Scin(s)                                                                \
  ll(s);                                                                       \
  cin >> (s);
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;
}
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef vector<V> VV;
typedef vector<P> VP;
ll mod = 1e9 + 7;
ll MOD = 1e9 + 7;
ll INF = 1e18;
// cout << "Case #" << index << " :IMPOSSIBLE";
ll evaluate(VV &S, ll d, V &prev, V &C, ll j) {

  ll temp = 0;
  temp += S[d][j];
  rep(k, 26) {
    if (j == k) {
      continue;
    }
    temp -= C[k] * (d - prev[k]);
  }
  return temp;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  ll d;
  cin >> d;
  V C(26);
  rep(i, 26) cin >> C[i];
  VV S(d, V(26));
  rep(i, d) { rep(j, 26) cin >> S[i][j]; }
  ll width = 250;
  vector<tuple<ll, V, V>> vec;
  V temp;
  rep(i, 26) temp.push_back(-1);
  V temp2;
  vec.push_back({0, temp, temp2});
  rep3(i, 0, d) {
    priority_queue<tuple<ll, V, V>> q;
    ll reg = -INF;
    ll res = 0;
    for (auto j : vec) {
      ll score = get<0>(j);
      V prev = get<1>(j);
      V history = get<2>(j);
      rep(k, 26) {
        ll value = evaluate(S, i, prev, C, k);
        V prev2 = prev;
        prev2[k] = i;
        V history2 = history;
        history2.push_back(k);
        q.push({score + value, prev2, history2});
      }
    }
    vec = {};
    for (ll j = 0; j < width && !q.empty(); j++) {
      vec.push_back(q.top());
      q.pop();
    }
  }
  for (auto i : get<2>(vec[0])) {
    cout << i + 1 << endl;
  }
}