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;

pl f(vvl s, vl c, vl last, ll depth, ll hukasa) {
  if (hukasa >= 3 || depth >= d)
    return {0, 0};
  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 - 3)
      saki = f(s, c, last, depth + 1, hukasa + 1);
    last[i] = moto;
    if (ret < s[depth][i] - minus + saki.second) {
      ret = s[depth][i] - minus + saki.second;
      retind = i;
    }
  }
  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, 0).first;
    last[tmp] = i;
    cout << tmp + 1 << endl;
    t[i] = tmp;
  }
  // 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;

pl f(vvl s, vl c, vl last, ll depth, ll hukasa) {
  if (hukasa >= 2 || depth >= d)
    return {0, 0};
  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 = f(s, c, last, depth + 1, hukasa + 1);
    last[i] = moto;
    if (ret < s[depth][i] - minus + saki.second) {
      ret = s[depth][i] - minus + saki.second;
      retind = i;
    }
  }
  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, 0).first;
    last[tmp] = i;
    cout << tmp + 1 << endl;
    t[i] = tmp;
  }
  // score(s,c,t);
  return 0;
}