Problem p02618 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, s, n) for (int i = (s); i < (n); i++)
#define all(v) v.begin(), v.end()
#define outve(v)                                                               \
  for (auto i : v)                                                             \
    cout << i << " ";                                                          \
  cout << endl
#define outmat(v)                                                              \
  for (auto i : v) {                                                           \
    for (auto j : i)                                                           \
      cout << j << " ";                                                        \
    cout << endl;                                                              \
  }
#define in(n, v)                                                               \
  for (int i = 0; i < (n); i++) {                                              \
    cin >> v[i];                                                               \
  }
#define out(n) cout << (n) << endl
#define fi first
#define se second
#define pb push_back
#define si(v) int(v.size())
#define len(v) int(v.length())
#define lob(v, n) lower_bound(all(v), n)
#define lobi(v, n) lower_bound(all(v), n) - v.begin()
#define upb(v, n) upper_bound(all(v), n)
#define upbi(v, n) upper_bound(all(v), n) - v.begin()
#define mod 1000000007
#define infi 1900000000
#define infl 1100000000000000000
#define cyes cout << "Yes" << endl
#define cno cout << "No" << endl
#define csp << " " <<
#define outset(n) cout << fixed << setprecision(n);
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vs = vector<string>;
using vc = vector<char>;
using vd = vector<double>;
using vvd = vector<vector<double>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vvp = vector<vector<pair<int, int>>>;
template <typename T> using ve = vector<T>;
template <typename T> using pq2 = priority_queue<T>;
template <typename T> using pq1 = priority_queue<T, vector<T>, greater<T>>;
template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int D;
int N = 26;
vi c;
vvi s;
vi ans;
vi las;

pii rec(int n, int dd, vi lass) {

  if (dd >= D || n == 6)
    return {0, 1};

  int los = 0;
  rep(i, N) los += c[i] * (dd - lass[i]);
  int res = -infi;
  int an = 1;
  set<int> sss;
  ve<pii> AA(N);
  rep(i, N) AA[i] = {s[dd][i], i};
  sort(all(AA));
  rep(i, 7) sss.insert(AA[N - i - 1].se);

  rep(i, N) {
    if (!sss.count(i))
      continue;
    vi la = lass;
    int ddd = s[dd][i] - los + c[i] * (dd - lass[i]);
    la[i] = dd;
    int da = ddd + rec(n + 1, dd + 1, la).fi;
    if (chmax(res, da))
      an = i + 1;
  }

  return {res, an};
}

int main() {

  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  // int ti = clock();

  cin >> D;
  c.resize(N);
  s.resize(D, vi(N));

  in(N, c);
  rep(i, D) rep(j, N) cin >> s[i][j];

  ans.resize(D);
  las.assign(N, -1);

  rep(d, D) {
    ans[d] = rec(0, d, las).se;
    las[ans[d] - 1] = d;
  }

  rep(i, D) cout << ans[i] << endl;

  // printf("Execution Time: %.4lf sec\n", 1.0 * (clock() - ti) /
  // CLOCKS_PER_SEC);

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, s, n) for (int i = (s); i < (n); i++)
#define all(v) v.begin(), v.end()
#define outve(v)                                                               \
  for (auto i : v)                                                             \
    cout << i << " ";                                                          \
  cout << endl
#define outmat(v)                                                              \
  for (auto i : v) {                                                           \
    for (auto j : i)                                                           \
      cout << j << " ";                                                        \
    cout << endl;                                                              \
  }
#define in(n, v)                                                               \
  for (int i = 0; i < (n); i++) {                                              \
    cin >> v[i];                                                               \
  }
#define out(n) cout << (n) << endl
#define fi first
#define se second
#define pb push_back
#define si(v) int(v.size())
#define len(v) int(v.length())
#define lob(v, n) lower_bound(all(v), n)
#define lobi(v, n) lower_bound(all(v), n) - v.begin()
#define upb(v, n) upper_bound(all(v), n)
#define upbi(v, n) upper_bound(all(v), n) - v.begin()
#define mod 1000000007
#define infi 1900000000
#define infl 1100000000000000000
#define cyes cout << "Yes" << endl
#define cno cout << "No" << endl
#define csp << " " <<
#define outset(n) cout << fixed << setprecision(n);
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vs = vector<string>;
using vc = vector<char>;
using vd = vector<double>;
using vvd = vector<vector<double>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vvp = vector<vector<pair<int, int>>>;
template <typename T> using ve = vector<T>;
template <typename T> using pq2 = priority_queue<T>;
template <typename T> using pq1 = priority_queue<T, vector<T>, greater<T>>;
template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int D;
int N = 26;
vi c;
vvi s;
vi ans;
vi las;

pii rec(int n, int dd, vi lass) {

  if (dd >= D || n == 5)
    return {0, 1};

  int los = 0;
  rep(i, N) los += c[i] * (dd - lass[i]);
  int res = -infi;
  int an = 1;
  set<int> sss;
  ve<pii> AA(N);
  rep(i, N) AA[i] = {s[dd][i], i};
  sort(all(AA));
  rep(i, 7) sss.insert(AA[N - i - 1].se);

  rep(i, N) {
    if (!sss.count(i))
      continue;
    vi la = lass;
    int ddd = s[dd][i] - los + c[i] * (dd - lass[i]);
    la[i] = dd;
    int da = ddd + rec(n + 1, dd + 1, la).fi;
    if (chmax(res, da))
      an = i + 1;
  }

  return {res, an};
}

int main() {

  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  // int ti = clock();

  cin >> D;
  c.resize(N);
  s.resize(D, vi(N));

  in(N, c);
  rep(i, D) rep(j, N) cin >> s[i][j];

  ans.resize(D);
  las.assign(N, -1);

  rep(d, D) {
    ans[d] = rec(0, d, las).se;
    las[ans[d] - 1] = d;
  }

  rep(i, D) cout << ans[i] << endl;

  // printf("Execution Time: %.4lf sec\n", 1.0 * (clock() - ti) /
  // CLOCKS_PER_SEC);

  return 0;
}