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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned ll
#define PI acos(-1)
#define vi vector<int>
#define pi pair<int, int>
#define pl pair<ll, ll>
template <typename T> using v2 = vector<vector<T>>;
template <typename T> using v3 = vector<vector<vector<T>>>;
template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &elemnt : v)
    is >> elemnt;
  return is;
}
template <class T, class U> istream &operator>>(istream &is, pair<T, U> &p) {
  is >> p.first >> p.second;
  return is;
}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const int MOD = 1e9 + 7;
const int INF = 1e9 + 200;
const ll INFLL = 2e18 + 200;

struct info {
  vector<ll> contest, minus;
  ll score;

  info(ll value) {
    contest = vector<ll>();
    minus = vector<ll>(26, 0);
    score = value;
  }

  info() {
    contest = vector<ll>();
    minus = vector<ll>(26, 0);
    score = 0;
  }
};

int main() {
  ll D;
  cin >> D;
  vector<ll> c(26);
  cin >> c;
  vector<vector<ll>> satisfaction(26, vector<ll>(D + 1));
  for (int i = 1; i <= D; i++) {
    for (int j = 0; j < 26; j++) {
      ll temp;
      cin >> temp;
      satisfaction[j][i] = temp;
    }
  }
  random_device rnd;
  mt19937 mt(rnd());
  vector<vector<info>> dp(D + 1, vector<info>(26));
  for (int day = 1; day <= D; day++) {
    for (int now = 0; now < 26; now++) {
      for (int before = 0; before < 26; before++) {
        auto before_info = dp[day - 1][before];
        for (int i = 0; i < 26; i++) {
          before_info.minus[i] += c[i];
        }
        before_info.contest.push_back(now);
        before_info.minus[now] = 0;
        before_info.score += satisfaction[now][day];
        for (int i = 0; i < 26; i++) {
          before_info.score -= before_info.minus[i];
        }
        if (dp[day][now].contest.empty()) {
          dp[day][now] = before_info;
          continue;
        }
        if (dp[day][now].score == before_info.score) {
          ll left = 0, right = 0;
          for (int i = 0; i < 26; i++) {
            left += dp[day][now].minus[i];
            right += before_info.minus[i];
          }
          if (left > right)
            dp[day][now] = before_info;
          continue;
        }
        if (dp[day][now].score < before_info.score)
          dp[day][now] = before_info;
      }
      if (day < 2)
        continue;
      for (int before = 0; before < 26; before++) {
        auto calc = [&](int middle) {
          auto before_info = dp[day - 2][before];
          for (int i = 0; i < 26; i++) {
            before_info.minus[i] += c[i];
          }
          before_info.contest.push_back(middle);
          before_info.minus[middle] = 0;
          before_info.score += satisfaction[middle][day - 1];
          for (int i = 0; i < 26; i++) {
            before_info.minus[i] += c[i];
          }
          before_info.contest.push_back(now);
          before_info.minus[now] = 0;
          before_info.score += satisfaction[now][day];
          if (dp[day][now].score == before_info.score) {
            ll left = 0, right = 0;
            for (int i = 0; i < 26; i++) {
              left += dp[day][now].minus[i];
              right += before_info.minus[i];
            }
            if (left > right)
              dp[day][now] = before_info;
            return;
          }
          if (dp[day][now].score < before_info.score)
            dp[day][now] = before_info;
        };
        for (int i = 0; i < 26; i++)
          calc(i);
      }
    }
  }
  auto ans = dp[D][0];
  for (int i = 0; i < 26; i++) {
    if (ans.score < dp[D][i].score)
      ans = dp[D][i];
  }
  for (int i = 0; i < D; i++) {
    cout << ans.contest[i] + 1 << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned ll
#define PI acos(-1)
#define vi vector<int>
#define pi pair<int, int>
#define pl pair<ll, ll>
template <typename T> using v2 = vector<vector<T>>;
template <typename T> using v3 = vector<vector<vector<T>>>;
template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &elemnt : v)
    is >> elemnt;
  return is;
}
template <class T, class U> istream &operator>>(istream &is, pair<T, U> &p) {
  is >> p.first >> p.second;
  return is;
}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const int MOD = 1e9 + 7;
const int INF = 1e9 + 200;
const ll INFLL = 2e18 + 200;

struct info {
  vector<ll> contest, minus;
  ll score;

  info(ll value) {
    contest = vector<ll>();
    minus = vector<ll>(26, 0);
    score = value;
  }

  info() {
    contest = vector<ll>();
    minus = vector<ll>(26, 0);
    score = 0;
  }
};

int main() {
  ll D;
  cin >> D;
  vector<ll> c(26);
  cin >> c;
  vector<vector<ll>> satisfaction(26, vector<ll>(D + 1));
  for (int i = 1; i <= D; i++) {
    for (int j = 0; j < 26; j++) {
      ll temp;
      cin >> temp;
      satisfaction[j][i] = temp;
    }
  }
  vector<vector<info>> dp(D + 1, vector<info>(26));
  for (int day = 1; day <= D; day++) {
    for (int now = 0; now < 26; now++) {
      for (int before = 0; before < 26; before++) {
        auto before_info = dp[day - 1][before];
        for (int i = 0; i < 26; i++) {
          before_info.minus[i] += c[i];
        }
        before_info.contest.push_back(now);
        before_info.minus[now] = 0;
        before_info.score += satisfaction[now][day];
        for (int i = 0; i < 26; i++) {
          before_info.score -= before_info.minus[i];
        }
        if (dp[day][now].contest.empty()) {
          dp[day][now] = before_info;
          continue;
        }
        if (dp[day][now].score == before_info.score) {
          ll left = 0, right = 0;
          for (int i = 0; i < 26; i++) {
            left += dp[day][now].minus[i];
            right += before_info.minus[i];
          }
          if (left > right)
            dp[day][now] = before_info;
          continue;
        }
        if (dp[day][now].score < before_info.score)
          dp[day][now] = before_info;
      }
      if (day < 2)
        continue;
      for (int before = 0; before < 26; before++) {
        auto calc = [&](int middle) {
          auto before_info = dp[day - 2][before];
          for (int i = 0; i < 26; i++) {
            before_info.minus[i] += c[i];
          }
          before_info.contest.push_back(middle);
          before_info.minus[middle] = 0;
          before_info.score += satisfaction[middle][day - 1];
          for (int i = 0; i < 26; i++) {
            before_info.minus[i] += c[i];
          }
          before_info.contest.push_back(now);
          before_info.minus[now] = 0;
          before_info.score += satisfaction[now][day];
          if (dp[day][now].score == before_info.score) {
            ll left = 0, right = 0;
            for (int i = 0; i < 26; i++) {
              left += dp[day][now].minus[i];
              right += before_info.minus[i];
            }
            if (left > right)
              dp[day][now] = before_info;
            return;
          }
          if (dp[day][now].score < before_info.score)
            dp[day][now] = before_info;
        };
        for (int i = 0; i < 10; i++) {
          random_device rnd;
          mt19937 mt(rnd());
          calc(mt() % 26);
        }
      }
    }
  }
  auto ans = dp[D][0];
  for (int i = 0; i < 26; i++) {
    if (ans.score < dp[D][i].score)
      ans = dp[D][i];
  }
  for (int i = 0; i < D; i++) {
    cout << ans.contest[i] + 1 << endl;
  }
}