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

/**
 *    author:  boutarou
 *    created: 28.06.2020 20:54:48
 **/

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < int(n); i++)
#define fcout cout << fixed << setprecision(15)
using lint = long long;
using P = pair<int, int>;

template <class T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

const double PI = 3.1415926535897932;
const int INF = 1e9;

int d;
int c[26];
int s[365][26];
vector<int> previous(26);

int score(int day, int contest, vector<int> before) {
  int cnt = 0;
  cnt += s[day][contest];
  before[contest] = day;
  rep(i, 26) { cnt -= c[i] * (day - before[i]); }
  return cnt;
}

int score2(int day, int contest1, int contest2, vector<int> before) {
  int cnt = 0;
  cnt += s[day][contest1];
  before[contest1] = day;
  rep(i, 26) { cnt -= c[i] * (day - before[i]); }
  day++;
  cnt += s[day][contest2];
  before[contest2] = day;
  rep(i, 26) { cnt -= c[i] * (day - before[i]); }
  return cnt;
}

int score3(int day, int contest1, int contest2, int contest3,
           vector<int> before) {
  int cnt = 0;
  cnt += s[day][contest1];
  before[contest1] = day;
  rep(i, 26) { cnt -= c[i] * (day - before[i]); }
  day++;
  cnt += s[day][contest2];
  before[contest2] = day;
  rep(i, 26) { cnt -= c[i] * (day - before[i]); }
  day++;
  cnt += s[day][contest3];
  before[contest3] = day;
  rep(i, 26) { cnt -= c[i] * (day - before[i]); }
  return cnt;
}

int score4(int day, int contest1, int contest2, int contest3, int contest4,
           vector<int> before) {
  int cnt = 0;
  cnt += s[day][contest1];
  before[contest1] = day;
  rep(i, 26) { cnt -= c[i] * (day - before[i]); }
  day++;
  cnt += s[day][contest2];
  before[contest2] = day;
  rep(i, 26) { cnt -= c[i] * (day - before[i]); }
  day++;
  cnt += s[day][contest3];
  before[contest3] = day;
  rep(i, 26) { cnt -= c[i] * (day - before[i]); }
  day++;
  cnt += s[day][contest4];
  before[contest4] = day;
  rep(i, 26) { cnt -= c[i] * (day - before[i]); }
  return cnt;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> d;
  rep(i, 26) cin >> c[i];
  rep(i, d) { rep(j, 26) cin >> s[i][j]; }

  vector<int> vec;
  rep(i, 26) { rep(j, c[i]) vec.push_back(i + 1); }

  random_device rnd;
  rep(i, d) { cout << vec[rnd() % vec.size()] << endl; }

  return 0;
}

Error: TLE

Fixed submission:

/**
 *    author:  boutarou
 *    created: 28.06.2020 20:54:48
 **/

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < int(n); i++)
#define fcout cout << fixed << setprecision(15)
using lint = long long;
using P = pair<int, int>;

template <class T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

const double PI = 3.1415926535897932;
const int INF = 1e9;

int d;
int c[26];
int s[365][26];
vector<int> previous(26);

int score(int day, int contest, vector<int> before) {
  int cnt = 0;
  cnt += s[day][contest];
  before[contest] = day;
  rep(i, 26) { cnt -= c[i] * (day - before[i]); }
  return cnt;
}

int score2(int day, int contest1, int contest2, vector<int> before) {
  int cnt = 0;
  cnt += s[day][contest1];
  before[contest1] = day;
  rep(i, 26) { cnt -= c[i] * (day - before[i]); }
  day++;
  cnt += s[day][contest2];
  before[contest2] = day;
  rep(i, 26) { cnt -= c[i] * (day - before[i]); }
  return cnt;
}

int score3(int day, int contest1, int contest2, int contest3,
           vector<int> before) {
  int cnt = 0;
  cnt += s[day][contest1];
  before[contest1] = day;
  rep(i, 26) { cnt -= c[i] * (day - before[i]); }
  day++;
  cnt += s[day][contest2];
  before[contest2] = day;
  rep(i, 26) { cnt -= c[i] * (day - before[i]); }
  day++;
  cnt += s[day][contest3];
  before[contest3] = day;
  rep(i, 26) { cnt -= c[i] * (day - before[i]); }
  return cnt;
}

int score4(int day, int contest1, int contest2, int contest3, int contest4,
           vector<int> before) {
  int cnt = 0;
  cnt += s[day][contest1];
  before[contest1] = day;
  rep(i, 26) { cnt -= c[i] * (day - before[i]); }
  day++;
  cnt += s[day][contest2];
  before[contest2] = day;
  rep(i, 26) { cnt -= c[i] * (day - before[i]); }
  day++;
  cnt += s[day][contest3];
  before[contest3] = day;
  rep(i, 26) { cnt -= c[i] * (day - before[i]); }
  day++;
  cnt += s[day][contest4];
  before[contest4] = day;
  rep(i, 26) { cnt -= c[i] * (day - before[i]); }
  return cnt;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> d;
  rep(i, 26) cin >> c[i];
  rep(i, d) { rep(j, 26) cin >> s[i][j]; }

  rep(i, 26) previous[i] = -1;

  int now = 0;
  for (int i = 0; i + 3 < d; i += 4) {
    int ma = -INF;
    rep(j, 26) {
      rep(k, 26) {
        rep(l, 26) {
          rep(m, 26) { ma = max(ma, score4(i, j, k, l, m, previous)); }
        }
      }
    }
    rep(j, 26) {
      rep(k, 26) {
        rep(l, 26) {
          rep(m, 26) {
            if (ma == score4(i, j, k, l, m, previous)) {
              cout << j + 1 << endl;
              cout << k + 1 << endl;
              cout << l + 1 << endl;
              cout << m + 1 << endl;
              now += score4(i, j, k, l, m, previous);
              previous[j] = i;
              previous[k] = i + 1;
              previous[l] = i + 2;
              previous[m] = i + 3;
              goto bou;
            }
          }
        }
      }
    }
  bou:;
  }
  if (d % 4 == 1) {
    int i = d - 1;
    int ma = -INF;
    rep(j, 26) { ma = max(ma, score(i, j, previous)); }
    rep(j, 26) {
      if (ma == score(i, j, previous)) {
        cout << j + 1 << endl;
        now += score(i, j, previous);
        previous[j] = i;
        break;
      }
    }
  }

  return 0;
}