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

//                             ¯\\_(ツ)_/¯
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mma,ava,tune=native")
// #pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define sz(a) ((int)(a).size())
#define all(a) (a).begin(), (a).end()
#define ll long long

void file() {
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
#endif
}
int tc;

const int N = 2e5 + 5, M = 2e6 + 5, MOD = 1e9 + 7, OO = 1e9;
std::vector<int> c(26);
int D;
std::vector<int> last(26);
std::vector<vector<int>> s;
int go(int indx, int S, int depth) {
  if (indx == D || depth == 2)
    return S;

  for (int j = 0; j < 26; j++) {
    S -= c[j] * (indx - last[j]);
  }
  int mx = 0;
  for (int j = 0; j < 26; j++) {
    int takeback = s[indx][j] + c[j] * (indx - last[j]);
    mx = max(mx, go(indx + 1, S + takeback, depth + 1));
  }
  return mx;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  // file();
  scanf("%d", &D);

  for (int i = 0; i < 26; i++)
    scanf("%d", &c[i]);
  s = vector<vector<int>>(D, vector<int>(26));
  for (int i = 0; i < D; i++) {
    for (int j = 0; j < 26; j++) {
      scanf("%d", &s[i][j]);
    }
  }
  int S = 0;

  for (int i = 0; i < D; i++) {
    for (int j = 0; j < 26; j++) {
      S -= c[j] * (i - last[j]);
    }
    int who = 1;
    int mx = 0;
    int take = 0;
    for (int j = 0; j < 26; j++) {
      int takeback = s[i][j] + c[j] * (i - last[j]);
      int wa = go(i + 1, S + takeback, 0);
      if (mx < wa) {
        mx = wa;
        take = takeback;
        who = j + 1;
      }
    }
    last[who - 1] = i;
    S += take;
    printf("%d\\n", who);
  }
}

Error: TLE

Fixed submission:

//                             ¯\\_(ツ)_/¯
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mma,ava,tune=native")
// #pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define sz(a) ((int)(a).size())
#define all(a) (a).begin(), (a).end()
#define ll long long

void file() {
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
#endif
}
int tc;

const int N = 2e5 + 5, M = 2e6 + 5, MOD = 1e9 + 7, OO = 1e9;
std::vector<int> c(26);
int D;
std::vector<int> last(26);
std::vector<vector<int>> s;
int go(int indx, int S, int depth) {
  if (indx == D || depth == 5)
    return S;

  for (int j = 0; j < 26; j++) {
    S -= c[j] * (indx - last[j]);
  }
  int mx = 0;
  for (int j = 0; j < 26; j++) {
    int takeback = s[indx][j] + c[j] * (indx - last[j]);
    mx = max(mx, go(indx + 1, S + takeback, depth + 1));
  }
  return mx;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  // file();
  scanf("%d", &D);

  for (int i = 0; i < 26; i++)
    scanf("%d", &c[i]);
  s = vector<vector<int>>(D, vector<int>(26));
  for (int i = 0; i < D; i++) {
    for (int j = 0; j < 26; j++) {
      scanf("%d", &s[i][j]);
    }
  }
  int S = 0;

  for (int i = 0; i < D; i++) {
    for (int j = 0; j < 26; j++) {
      S -= c[j] * (i - last[j]);
    }
    int who = 1;
    int mx = 0;
    for (int j = 0; j < 26; j++) {
      int takeback = s[i][j] + c[j] * (i - last[j]);
      int wa = go(i + 1, S + takeback, 0);
      if (mx < wa) {
        mx = wa;
        who = j + 1;
      }
    }
    last[who - 1] = i;
    S += mx;
    printf("%d\\n", who);
  }
}