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;
std::vector<int> v;
int go(int indx, int S, int depth) {
  if (indx == D || depth == 4)
    return S;

  for (int j = 0; j < 26; j++) {
    S -= c[j] * (indx - last[j]);
  }
  int mx = 0;

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

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  // file();
  srand(time(NULL));
  scanf("%d", &D);
  for (int i = 0; i < 26; i++)
    v.pb(i);
  for (int i = 0; i < 100; i++)
    random_shuffle(all(v));
  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 = -1e9;
    int take = 0;
    for (int j = 0; j < 26; j++) {
      int takeback = s[i][j] + c[j] * (i - last[j]);
      int l = last[j];
      last[j] = i;
      int wa = go(i + 1, S + takeback, 1);
      last[j] = l;
      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;
std::vector<int> v;
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;
  random_shuffle(all(v));
  int lim = 26 / depth;
  for (int i = 0; i < lim; i++) {
    int j = v[i];
    int takeback = s[indx][j] + c[j] * (indx - last[j]);
    int l = last[j];
    last[j] = indx;
    mx = max(mx, go(indx + 1, S + takeback, depth + 1));
    last[j] = l;
  }
  return mx;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  // file();
  srand(time(NULL));
  scanf("%d", &D);
  for (int i = 0; i < 26; i++)
    v.pb(i);
  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 = -1e9;
    int take = 0;
    for (int j = 0; j < 26; j++) {
      int takeback = s[i][j] + c[j] * (i - last[j]);
      int l = last[j];
      last[j] = i;
      int wa = go(i + 1, S + takeback, 1);
      last[j] = l;
      if (mx < wa) {
        mx = wa;
        take = takeback;
        who = j + 1;
      }
    }
    last[who - 1] = i;
    S += take;
    printf("%d\\n", who);
  }
}