Problem p02618 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, n) for (int i = 1; i <= (int)(n); i++)
#define ll long long
#define umap unordered_map
using graph = vector<vector<int>>;
using graph2 = vector<vector<pair<int, int>>>;
#define oorret true // std::out_of_rangeが返された時
#define oor(x)                                                                 \
  [&]() {                                                                      \
    try {                                                                      \
      x;                                                                       \
    } catch (const std::out_of_range &oor) {                                   \
      return oorret;                                                           \
    }                                                                          \
    return x;                                                                  \
  }()
double pi = 3.141592653589793238;
ll mod(ll x, ll y) {
  if (x >= 0 || x % y == 0)
    return x % y;
  return y + x % y;
} // mod including minus
ll dv0(ll x, ll y) {
  if (x >= 0 || x % y == 0)
    return x / y;
  return x / y - 1;
} // rnd down
ll dv1(ll x, ll y) {
  if (x % y == 0)
    return dv0(x, y);
  return dv0(x, y) + 1;
} // rnd up
int D;
void calculate(vector<int> c, vector<vector<int>> s, int contest, int day,
               vector<int> &t, ll &score) {
  ll p = 0;
  int a[26];
  rep(i, 26) { a[i] = 0; }
  rep(i, D) {
    rep(j, 26) { p -= c[j] * (i + 1 - a[j]); }
    if (i != day) {
      p += s[i].at(t[i]) + c[t[i]] * (i + 1 - a[t[i]]);
      a[t[i]] = i + 1;
    } else {
      p += s[day].at(contest) + c[contest] * (day + 1 - a[contest]);
      a[contest] = i + 1;
    }
  }
  if (score < p) {
    score = p;
    t[day] = contest;
  }
}

int main() {
  cin >> D;
  vector<int> c(26);
  rep(i, 26) { cin >> c[i]; }
  vector<vector<int>> s(D);
  int r;
  rep(i, D) {
    rep(j, 26) {
      cin >> r;
      s[i].push_back(r);
    }
  }
  int M[26];
  rep(i, 26) { M[i] = 0; }
  vector<vector<int>> m(D);
  rep(i, D) {
    rep(j, 26) { m[i].push_back(0); }
  }
  vector<int> a(26);

  rep(i, 26) { a[i] = 0; }
  rep(i, D) {}
  vector<int> t(D);
  ll score = 0;
  rep(i, D) {
    rep(j, 26) { M[j] = 0; }
    rep(j, 26) {
      score -= c[j] * (i + 1 - a[j]);
      rep(k, j + 1) {
        if (s[i].at(j) + c[j] * (i + 1 - a[j]) > M[k]) {
          rep(l, j - k) {
            M[j - l] = M[j - l - 1];
            m[i].at(j - l) = m[i].at(j - l - 1);
          }
          M[k] = s[i].at(j) + c[j] * (i + 1 - a[j]);
          m[i].at(k) = j;
        }
      }
    }
    score += M[0];
    a[m[i].at(0)] = i + 1;
    t[i] = m[i].at(0);
  }
  rep(j, 30) {
    rep(i, j + 2) {
      rep(k, D) { calculate(c, s, m[k].at((j + 1 - i) / 2 + 1), k, t, score); }
    }
  }
  rep(i, D) { cout << t[i] + 1 << endl; }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, n) for (int i = 1; i <= (int)(n); i++)
#define ll long long
#define umap unordered_map
using graph = vector<vector<int>>;
using graph2 = vector<vector<pair<int, int>>>;
#define oorret true // std::out_of_rangeが返された時
#define oor(x)                                                                 \
  [&]() {                                                                      \
    try {                                                                      \
      x;                                                                       \
    } catch (const std::out_of_range &oor) {                                   \
      return oorret;                                                           \
    }                                                                          \
    return x;                                                                  \
  }()
double pi = 3.141592653589793238;
ll mod(ll x, ll y) {
  if (x >= 0 || x % y == 0)
    return x % y;
  return y + x % y;
} // mod including minus
ll dv0(ll x, ll y) {
  if (x >= 0 || x % y == 0)
    return x / y;
  return x / y - 1;
} // rnd down
ll dv1(ll x, ll y) {
  if (x % y == 0)
    return dv0(x, y);
  return dv0(x, y) + 1;
} // rnd up
int D;
void calculate(vector<int> c, vector<vector<int>> s, int contest, int day,
               vector<int> &t, ll &score) {
  ll p = 0;
  int a[26];
  rep(i, 26) { a[i] = 0; }
  rep(i, D) {
    rep(j, 26) { p -= c[j] * (i + 1 - a[j]); }
    if (i != day) {
      p += s[i].at(t[i]) + c[t[i]] * (i + 1 - a[t[i]]);
      a[t[i]] = i + 1;
    } else {
      p += s[day].at(contest) + c[contest] * (day + 1 - a[contest]);
      a[contest] = i + 1;
    }
  }
  if (score < p) {
    score = p;
    t[day] = contest;
  }
}

int main() {
  cin >> D;
  vector<int> c(26);
  rep(i, 26) { cin >> c[i]; }
  vector<vector<int>> s(D);
  int r;
  rep(i, D) {
    rep(j, 26) {
      cin >> r;
      s[i].push_back(r);
    }
  }
  int M[26];
  rep(i, 26) { M[i] = 0; }
  vector<vector<int>> m(D);
  rep(i, D) {
    rep(j, 26) { m[i].push_back(0); }
  }
  vector<int> a(26);

  rep(i, 26) { a[i] = 0; }
  rep(i, D) {}
  vector<int> t(D);
  ll score = 0;
  rep(i, D) {
    rep(j, 26) { M[j] = 0; }
    rep(j, 26) {
      score -= c[j] * (i + 1 - a[j]);
      rep(k, j + 1) {
        if (s[i].at(j) + c[j] * (i + 1 - a[j]) > M[k]) {
          rep(l, j - k) {
            M[j - l] = M[j - l - 1];
            m[i].at(j - l) = m[i].at(j - l - 1);
          }
          M[k] = s[i].at(j) + c[j] * (i + 1 - a[j]);
          m[i].at(k) = j;
        }
      }
    }
    score += M[0];
    a[m[i].at(0)] = i + 1;
    t[i] = m[i].at(0);
  }
  rep(j, 20) {
    rep(i, j + 1) {
      rep(k, D) { calculate(c, s, m[k].at(i), k, t, score); }
    }
  }
  rep(i, D) { cout << t[i] + 1 << endl; }
}