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

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef long double ld;
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define REPR(i, n) for (int i = n; i > -1; --i)
#define ALL(a) (a).begin(), (a).end()
#define FILL(a, n, x)                                                          \\
  ;                                                                            \\
  REP(i, (n)) { (a)[i] = (x); }
#define CINA(a, n)                                                             \\
  ;                                                                            \\
  REP(i, (n)) { cin >> (a)[i]; }
#define FILL2(a, n, m, x)                                                      \\
  ;                                                                            \\
  REP(i, (n)) {                                                                \\
    REP(j, (m)) { (a)[i][j] = (x); }                                           \\
  }
#define CINA2(a, n, m)                                                         \\
  ;                                                                            \\
  REP(i, (n)) {                                                                \\
    REP(j, (m)) { cin >> (a)[i][j]; }                                          \\
  }
#define Liny "Yes\\n"
#define Linn "No\\n"
#define LINY "YES\\n"
#define LINN "NO\\n"

// cout << setfill('0') << right << setw(4) << 12; // "0012"
int keta(ll x) {
  if (x < 10) {
    return 1;
  } else {
    return keta(x / 10) + 1;
  }
}
int keta_wa(ll x) {
  if (x < 10) {
    return x;
  } else {
    return keta_wa(x / 10) + x % 10;
  }
}
int ctoi(char c) { return ((c >= '0' && c <= '9') ? c - '0' : 0); }
int __stoi(string s) { return atoi(s.c_str()); }
ll sum(ll a[], ll N) { return accumulate(a, a + N, 0LL); }
ll gcd(ll a, ll b) {
  if (a < b)
    swap(a, b);
  return b ? gcd(b, a % b) : a;
}
ll lcm(ll a, ll b) {
  if (a < b) {
    swap(a, b);
  }
  return a / gcd(a, b) * b;
}
template <class T> void chmax(T &a, T b) {
  if (a < b) {
    a = b;
  }
}
template <class T> void chmin(T &a, T b) {
  if (a > b) {
    a = b;
  }
}
template <class T> bool isIn(T a, vector<T> v) {
  for (T x : v) {
    if (a == x) {
      return true;
    }
  }
  return false;
}
string strReplace(string s, string target, string replacement) {
  if (!target.empty()) {
    std::string::size_type pos = 0;
    while ((pos = s.find(target, pos)) != std::string::npos) {
      s.replace(pos, target.length(), replacement);
      pos += replacement.length();
    }
  }
  return s;
}
const ll MOD = 1e9 + 7;
#define pii pair<int, int>
#define pll pair<ll, ll>
#define MP make_pair

bool DEBUG = 0;

int D;
int c[30];
int s[400][30];
int t[400];

ll cal_score(int temp[]) {
  int last[30] = {0};
  ll score = 0;
  for (int d = 1; d <= D; ++d) {
    int i = temp[d];
    score += s[d][i];
    last[i] = d;
    for (int j = 1; j <= 26; ++j) {
      score -= c[j] * (d - last[j]);
    }
    // cout << score << "\\n";
  }
  return score;
}

int main() {
  cin >> D;
  for (int i = 1; i <= 26; ++i) {
    cin >> c[i];
  }
  for (int d = 1; d <= D; ++d) {
    for (int i = 1; i <= 26; ++i) {
      cin >> s[d][i];
    }
  }

  int score_max = 0;

  default_random_engine e1(29624389);
  uniform_int_distribution<int> uniform_dist(1, 26);

  REP(count, 1000) {
    int temp[400];
    for (int d = 1; d <= D; ++d) {
      temp[d] = uniform_dist(e1);
      for (int i = 1; i <= 26; ++i) {
        if ((double)s[d][i] > (double)s[d][temp[d]] * 1.4) {
          temp[d] = i;
        }
      }
    }
    if (cal_score(temp) > score_max) {
      for (int d = 1; d <= D; ++d) {
        t[d] = temp[d];
      }
    }
  }
  for (int d = 1; d <= D; ++d) {
    cout << max(t[d] % 27, 1) << "\\n";
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef long double ld;
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define REPR(i, n) for (int i = n; i > -1; --i)
#define ALL(a) (a).begin(), (a).end()
#define FILL(a, n, x)                                                          \\
  ;                                                                            \\
  REP(i, (n)) { (a)[i] = (x); }
#define CINA(a, n)                                                             \\
  ;                                                                            \\
  REP(i, (n)) { cin >> (a)[i]; }
#define FILL2(a, n, m, x)                                                      \\
  ;                                                                            \\
  REP(i, (n)) {                                                                \\
    REP(j, (m)) { (a)[i][j] = (x); }                                           \\
  }
#define CINA2(a, n, m)                                                         \\
  ;                                                                            \\
  REP(i, (n)) {                                                                \\
    REP(j, (m)) { cin >> (a)[i][j]; }                                          \\
  }
#define Liny "Yes\\n"
#define Linn "No\\n"
#define LINY "YES\\n"
#define LINN "NO\\n"

// cout << setfill('0') << right << setw(4) << 12; // "0012"
int keta(ll x) {
  if (x < 10) {
    return 1;
  } else {
    return keta(x / 10) + 1;
  }
}
int keta_wa(ll x) {
  if (x < 10) {
    return x;
  } else {
    return keta_wa(x / 10) + x % 10;
  }
}
int ctoi(char c) { return ((c >= '0' && c <= '9') ? c - '0' : 0); }
int __stoi(string s) { return atoi(s.c_str()); }
ll sum(ll a[], ll N) { return accumulate(a, a + N, 0LL); }
ll gcd(ll a, ll b) {
  if (a < b)
    swap(a, b);
  return b ? gcd(b, a % b) : a;
}
ll lcm(ll a, ll b) {
  if (a < b) {
    swap(a, b);
  }
  return a / gcd(a, b) * b;
}
template <class T> void chmax(T &a, T b) {
  if (a < b) {
    a = b;
  }
}
template <class T> void chmin(T &a, T b) {
  if (a > b) {
    a = b;
  }
}
template <class T> bool isIn(T a, vector<T> v) {
  for (T x : v) {
    if (a == x) {
      return true;
    }
  }
  return false;
}
string strReplace(string s, string target, string replacement) {
  if (!target.empty()) {
    std::string::size_type pos = 0;
    while ((pos = s.find(target, pos)) != std::string::npos) {
      s.replace(pos, target.length(), replacement);
      pos += replacement.length();
    }
  }
  return s;
}
const ll MOD = 1e9 + 7;
#define pii pair<int, int>
#define pll pair<ll, ll>
#define MP make_pair

bool DEBUG = 0;

int D;
int c[30];
int s[400][30];
int t[400];

ll cal_score(int temp[]) {
  int last[30] = {0};
  ll score = 0;
  for (int d = 1; d <= D; ++d) {
    int i = temp[d];
    score += s[d][i];
    last[i] = d;
    for (int j = 1; j <= 26; ++j) {
      score -= c[j] * (d - last[j]);
    }
    // cout << score << "\\n";
  }
  return score;
}

int main() {
  cin >> D;
  for (int i = 1; i <= 26; ++i) {
    cin >> c[i];
  }
  for (int d = 1; d <= D; ++d) {
    for (int i = 1; i <= 26; ++i) {
      cin >> s[d][i];
    }
  }

  int score_max = 0;

  default_random_engine e1(2962439);
  uniform_int_distribution<int> uniform_dist(1, 26);

  REP(count, 2000000) {
    int temp[366];
    for (int d = 1; d <= D; ++d) {
      temp[d] = uniform_dist(e1);
      for (int i = 1; i <= 26; ++i) {
        if ((double)s[d][i] > (double)s[d][temp[d]] * 1.4) {
          temp[d] = i;
        }
      }
    }
    if (cal_score(temp) > score_max) {
      for (int d = 1; d <= D; ++d) {
        t[d] = temp[d];
      }
    }
  }
  for (int d = 1; d <= D; ++d) {
    cout << max(t[d] % 27, 1) << "\\n";
  }
}