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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define ll long long
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define rep1(i, n) for (ll i = 1; i < n; ++i)
#define mrep(i, n) for (ll i = n; i >= 0; --i)
#define all(a) (a).begin(), (a).end()
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vvb vector<vector<bool>>
#define pl pair<ll, ll>
#define inf 1001001001001001000
#define mod 1000000007
// #define mod 998244353
#define pi 3.1415926535
using namespace std;
struct __INIT {
  __INIT() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
  }
} __init;

ll d;

vl f(vvl s, vl c, vl last, ll depth, ll hukasa) {
  if (hukasa >= 2 || depth >= d)
    return {0, 0, 0, 0};
  ll ret = -inf;
  ll retind = 0;
  ll ret2 = -inf;
  ll retind2 = 0;
  rep(i, 26) {
    ll minus = 0;
    rep(j, 26) {
      if (j != i)
        minus += c[j] * (depth - last[j]);
    }
    ll moto = last[i];
    last[i] = depth;
    vl saki = {0, 0, 0, 0};
    if (depth >= d - 3)
      saki = f(s, c, last, depth + 1, hukasa + 1);
    last[i] = moto;
    if (ret < s[depth][i] - minus + saki[1]) {
      ret = s[depth][i] - minus + saki[1];
      retind = i;
      ret2 = saki[1];
      retind2 = saki[0];
    }
  }
  return {retind, ret, retind2, ret2};
}

void score(vvl s, vl c, vl t) {
  vl last(26);
  ll ans = 0;
  rep(i, d) {
    ans += s[i][t[i]];
    ll minus = 0;
    last[t[i]] = i;
    rep(j, 26) minus += c[j] * (i - last[j]);
    ans -= minus;
  }
  cout << ans << endl;
}

int main() {
  cin >> d;
  vl c(26);
  rep(i, 26) cin >> c[i];
  vvl s(d, vl(26));
  rep(i, d) rep(j, 26) cin >> s[i][j];
  vl last(26, 0);
  vl t(d);
  rep(i, d) {
    vl tmp = f(s, c, last, i, 0);
    last[tmp[0]] = i;
    cout << tmp[0] + 1 << endl;
    t[i] = tmp[0];
    i++;
    if (i >= d)
      break;
    last[tmp[2]] = i;
    cout << tmp[2] + 1 << endl;
    t[i] = tmp[2];
  }
  // score(s,c,t);
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define ll long long
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define rep1(i, n) for (ll i = 1; i < n; ++i)
#define mrep(i, n) for (ll i = n; i >= 0; --i)
#define all(a) (a).begin(), (a).end()
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vvb vector<vector<bool>>
#define pl pair<ll, ll>
#define inf 1001001001001001000
#define mod 1000000007
// #define mod 998244353
#define pi 3.1415926535
using namespace std;
struct __INIT {
  __INIT() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
  }
} __init;

ll d;
ll unchi = 4;
vl lt(unchi, -inf);
vl ltv(unchi, -inf);

pl f(vvl s, vl c, vl last, ll depth) {
  if (depth >= d)
    return {0, 0};
  // if(depth >= d-unchi && lt[depth-(d-unchi)] != -inf) return
  // {lt[depth-(d-unchi)],ltv[depth-(d-unchi)]};
  ll ret = -inf;
  ll retind = 0;
  rep(i, 26) {
    ll minus = 0;
    rep(j, 26) {
      if (j != i)
        minus += c[j] * (depth - last[j]);
    }
    ll moto = last[i];
    last[i] = depth;
    pl saki = {0, 0};
    if (depth >= d - unchi)
      saki = f(s, c, last, depth + 1);
    last[i] = moto;
    if (ret < s[depth][i] - minus + saki.second) {
      ret = s[depth][i] - minus + saki.second;
      retind = i;
    }
  }
  if (depth >= d - unchi) {
    lt[depth - (d - unchi)] = retind;
    ltv[depth - (d - unchi)] = ret;
  }
  return {retind, ret};
}

void score(vvl s, vl c, vl t) {
  vl last(26);
  ll ans = 0;
  rep(i, d) {
    ans += s[i][t[i]];
    ll minus = 0;
    last[t[i]] = i;
    rep(j, 26) minus += c[j] * (i - last[j]);
    ans -= minus;
  }
  cout << ans << endl;
}

int main() {
  cin >> d;
  vl c(26);
  rep(i, 26) cin >> c[i];
  vvl s(d, vl(26));
  rep(i, d) rep(j, 26) cin >> s[i][j];
  vl last(26, 0);
  vl t(d);
  rep(i, d) {
    ll tmp = f(s, c, last, i).first;
    last[tmp] = i;
    cout << tmp + 1 << endl;
    t[i] = tmp;
  }
  // score(s,c,t);
  return 0;
}