prithivMLmods/Coder-Stat huggingface dataset:
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 rep1(i, n) for (int i = 1; i <= (int)(n); i++)
typedef int64_t Int;
typedef pair<Int, Int> P;

Int D;
vector<Int> C(26);
vector<vector<Int>> S(365, vector<Int>(26));

// 最終的な満足度を返す関数
Int score(vector<int> schedule) {
  vector<int> last_day(26, 0); // 各コンテストを最後に行った日

  vector<Int> res(D, 0);

  rep(d, D) {
    int type = schedule[d] - 1; // その日に行われるコンテスト

    res[d] += S[d][type];
    rep(i, 26) {
      if (i == type)
        continue;
      res[d] -= C[i] * (d + 1 - last_day[i]);
    }
    last_day[type] = d + 1;

    if (d != D - 1)
      res[d + 1] += res[d];
  }

  return res[D - 1];
}

int main() {
  clock_t start = clock();

  cin >> D;
  rep(i, 26) cin >> C[i];
  rep(i, D) rep(j, 26) cin >> S[i][j];

  vector<int> t_old(D);
  rep(i, D) t_old[i] = (i % 26) + 1;

  int old_score = score(t_old);

  rep(k, 1000000) {
    vector<int> t_new = t_old;
    int d = rand() % D, q = rand() % 26 + 1;
    // cout << d << " " << q << endl;
    t_new[d] = q;

    if (score(t_new) > old_score)
      t_old = t_new;
  }

  rep(i, D) cout << t_old[i] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
typedef int64_t Int;
typedef pair<Int, Int> P;

Int D;
vector<Int> C(26);
vector<vector<Int>> S(365, vector<Int>(26));

// 最終的な満足度を返す関数
Int score(vector<int> schedule) {
  vector<int> last_day(26, 0); // 各コンテストを最後に行った日

  vector<Int> res(D, 0);

  rep(d, D) {
    int type = schedule[d] - 1; // その日に行われるコンテスト

    res[d] += S[d][type];
    rep(i, 26) {
      if (i == type)
        continue;
      res[d] -= C[i] * (d + 1 - last_day[i]);
    }
    last_day[type] = d + 1;

    if (d != D - 1)
      res[d + 1] += res[d];
  }

  return res[D - 1];
}

int main() {
  clock_t start = clock();

  cin >> D;
  rep(i, 26) cin >> C[i];
  rep(i, D) rep(j, 26) cin >> S[i][j];

  vector<int> t_old(D);
  rep(i, D) t_old[i] = (i % 26) + 1;

  int old_score = score(t_old);

  rep(k, 10000) {
    vector<int> t_new = t_old;
    int d = rand() % D, q = rand() % 26 + 1;
    // cout << d << " " << q << endl;
    t_new[d] = q;

    if (score(t_new) > old_score)
      t_old = t_new;
  }

  rep(i, D) cout << t_old[i] << endl;
}
Problem p02618 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define Int int64_t

using namespace std;

int D;
vector<int> c(30, 0);
vector<vector<int>> s(400, vector<int>(30, 0));

struct schedule {
  int score = 0;
  vector<int> contest;
  vector<int> last;

  schedule() { last.assign(30, 0); };

  bool operator<(const schedule &s) const { return score > s.score; }
};

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

schedule calc_score(schedule sc, int con) {
  schedule ret = sc;
  int day = sc.contest.size() + 1;
  ret.last[con] = day;
  ret.contest.push_back(con);
  ret.score += s[day][con];
  for (int i = 1; i <= 26; ++i) {
    ret.score -= c[i] * (day - ret.last[i]);
  }

  return ret;
}

int main() {
  constexpr int BEAM = 2500;
  input();

  vector<schedule> curr;
  curr.emplace_back(schedule());
  for (int d = 1; d <= D; ++d) {
    vector<schedule> nxt;
    for (auto sc : curr) {
      for (int i = 1; i <= 26; ++i) {
        nxt.push_back(calc_score(sc, i));
      }
    }
    sort(nxt.begin(), nxt.end());
    if (nxt.size() > BEAM) {
      nxt.resize(BEAM);
    }
    curr = nxt;
  }
  // cerr << "score = " << curr[0].score << endl;
  for (auto t : curr[0].contest) {
    cout << t << endl;
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define Int int64_t

using namespace std;

int D;
vector<int> c(30, 0);
vector<vector<int>> s(400, vector<int>(30, 0));

struct schedule {
  int score = 0;
  vector<int> contest;
  vector<int> last;

  schedule() { last.assign(30, 0); };

  bool operator<(const schedule &s) const { return score > s.score; }
};

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

schedule calc_score(schedule sc, int con) {
  schedule ret = sc;
  int day = sc.contest.size() + 1;
  ret.last[con] = day;
  ret.contest.push_back(con);
  ret.score += s[day][con];
  for (int i = 1; i <= 26; ++i) {
    ret.score -= c[i] * (day - ret.last[i]);
  }

  return ret;
}

int main() {
  constexpr int BEAM = 77;
  input();

  vector<schedule> curr;
  curr.emplace_back(schedule());
  for (int d = 1; d <= D; ++d) {
    vector<schedule> nxt;
    for (auto sc : curr) {
      for (int i = 1; i <= 26; ++i) {
        nxt.push_back(calc_score(sc, i));
      }
    }
    sort(nxt.begin(), nxt.end());
    if (nxt.size() > BEAM) {
      nxt.resize(BEAM);
    }
    curr = nxt;
  }
  // cerr << "score = " << curr[0].score << endl;
  for (auto t : curr[0].contest) {
    cout << t << endl;
  }

  return 0;
}
Problem p02618 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <iostream>  // cout, endl, cin
#include <math.h>    // sqrt
#include <string>    // string, to_string, stoi
#include <vector>    // vector
using namespace std;
typedef long long ll;

int main() {
  ll D;
  cin >> D;
  vector<ll> c(26);
  for (int i = 0; i < 26; i++) {
    cin >> c.at(i);
  }
  vector<vector<ll>> s(D, vector<ll>(26));
  for (int i = 0; i < D; i++) {
    for (int j = 0; j < 26; j++) {
      cin >> s.at(i).at(j);
    }
  }
  vector<ll> v(26, 0);
  vector<ll> last;
  for (ll i = 0; i < D; i++) {
    ll pointd = 0;
    int vd = 0;
    for (int j = 0; j < 26; j++) {
      last = v;
      last.at(j) = i + 1;
      ll sumd = 0;
      for (int k = 0; k < 26; k++) {
        if (k == j) {
          sumd += s.at(i).at(k);
        } else {
          sumd -= (i - last.at(k) + 1) * c.at(k);
        }
      }
      if (pointd < sumd) {
        vd = j + 1;
      }
      pointd = max(pointd, sumd);
    }
    v.at(vd - 1) = i + 1;
    cout << vd << endl;
  }
}


Fixed submission:
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <iostream>  // cout, endl, cin
#include <math.h>    // sqrt
#include <string>    // string, to_string, stoi
#include <vector>    // vector
using namespace std;
typedef long long ll;

int main() {
  ll D;
  cin >> D;
  vector<ll> c(26);
  for (int i = 0; i < 26; i++) {
    cin >> c.at(i);
  }
  vector<vector<ll>> s(D, vector<ll>(26));
  for (int i = 0; i < D; i++) {
    for (int j = 0; j < 26; j++) {
      cin >> s.at(i).at(j);
    }
  }
  vector<ll> v(26, 0);
  vector<ll> last;
  for (ll i = 0; i < D; i++) {
    ll pointd = 0;
    int vd = 0;
    for (int j = 0; j < 26; j++) {
      last = v;
      last.at(j) = i + 1;
      ll sumd = 0;
      for (int k = 0; k < 26; k++) {
        if (k == j) {
          sumd += s.at(i).at(k);
        } else {
          sumd -= (i - last.at(k) + 1) * c.at(k);
        }
      }
      if (pointd < sumd) {
        vd = j + 1;
      }
      if (j == 0) {
        pointd = sumd;
        vd = 1;
      } else {
        pointd = max(pointd, sumd);
      }
    }
    v.at(vd - 1) = i + 1;
    cout << vd << endl;
  }
}
Problem p02618 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

/*==============変数宣言==============*/
int N;
const int M = 26;
int n;
int m = M;

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

void input() {
  scanf("%d", &N);
  n = N;

  for (int i = 0; i < M; i++)
    scanf("%d", &c[i]);

  for (int i = 0; i < N; i++)
    for (int j = 0; j < M; j++)
      scanf("%d", &s[i][j]);

  return;
}

/*================乱数================*/
/*====================================*/

int init() { input(); }

void output(int ans[]) {
  for (int i = 0; i < n; i++) {
    printf("%d\n", ans[i] + 1);
  }
}

int main() {
  init();

  int ans[500];

  for (int i = 0; i < n; i++) {
    ans[i] = i % 26;
  }

  output(ans);

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

/*==============変数宣言==============*/
int N;
const int M = 26;
int n;
int m = M;

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

void input() {
  scanf("%d", &N);
  n = N;

  for (int i = 0; i < M; i++)
    scanf("%d", &c[i]);

  for (int i = 0; i < N; i++)
    for (int j = 0; j < M; j++)
      scanf("%d", &s[i][j]);

  return;
}

/*================乱数================*/
/*====================================*/

void init() {
  input();
  return;
}

void output(int ans[]) {
  for (int i = 0; i < n; i++) {
    printf("%d\n", ans[i] + 1);
  }
}

int main() {
  init();

  int ans[500];

  for (int i = 0; i < n; i++) {
    ans[i] = i % 26;
  }

  output(ans);

  return 0;
}
Problem p02618 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <iterator>
#include <string>
#include <vector>
#define rep(i, cc, n) for (int i = cc; i < n; ++i)
using namespace std;
using ll = long long;

int main() {
  int D;
  cin >> D;

  vector<int> C(26);
  vector<int> conts(26, 0);
  rep(i, 0, 26) {
    int c;
    cin >> c;

    C[i] = c;
  }

  vector<vector<int>> S(D, vector<int>(26));
  rep(i, 0, D) {
    rep(j, 0, 26) {
      int s;
      cin >> s;
      S.at(i).at(j) = s;
    }
  }

  rep(d, 1, D + 1) {
    int max = 0;
    int memo = 0;
    rep(j, 0, 26) {
      rep(k, 0, 26) {

        int satit = 0;
        if (k == j) {
          satit += S.at(d - 1).at(j);
        } else {
          satit -= C[j] * (d - conts[j]);
        }

        if (satit > max) {
          max = satit;
          memo = k;
        }
      }
    }
    cout << memo + 1 << endl;
    conts[d - 1] = memo;
  }
}


Fixed submission:
#include <iostream>
#include <iterator>
#include <string>
#include <vector>
#define rep(i, cc, n) for (int i = cc; i < n; ++i)
using namespace std;
using ll = long long;

int main() {
  int D;
  cin >> D;

  vector<int> C(26);
  vector<int> conts(26, 0);
  rep(i, 0, 26) {
    int c;
    cin >> c;

    C[i] = c;
  }

  vector<vector<int>> S(D, vector<int>(26));
  rep(i, 0, D) {
    rep(j, 0, 26) {
      int s;
      cin >> s;
      S.at(i).at(j) = s;
    }
  }

  rep(d, 1, D + 1) {
    int max = 0;
    int memo = 0;
    rep(j, 0, 26) {
      rep(k, 0, 26) {

        int satit = 0;
        if (k == j) {
          satit += S.at(d - 1).at(j);
        } else {
          satit -= C[j] * (d - conts[j]);
        }

        if (satit > max) {
          max = satit;
          memo = k;
        }
      }
    }
    cout << memo + 1 << endl;
    conts[memo] = d;
  }
}
Problem p02618 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int D;
  vector<pair<int, int>> c(26);
  vector<vector<int>> s(D, vector<int>(26));
  vector<int> t(D);

  cin >> D;
  for (int i = 0; i < 26; i++) {
    cin >> c.at(i).first;
    c.at(i).second = 0;
  }

  for (int i = 0; i < D; i++) {
    for (int j = 0; j < 26; j++) {
      cin >> s.at(i).at(j);
    }
  }

  for (int i = 0; i < D; i++) {
    t.at(i) = rand() % 26 + 1;
  }

  int count = 0;
  int S = 0;
  vector<int> ans(D);
  while (count++ < 5000000) {

    t.at((rand() % D + 1) - 1) = rand() % 26 + 1;

    int Pl = 0;
    int Mi = 0;

    for (int i = 0; i < D; i++) {
      Pl += s.at(i).at(t.at(i) - 1);
      c.at(t.at(i) - 1).second = i + 1;
      for (int j = 0; j < 26; j++) {
        Mi += c.at(j).first * (i + 1 - c.at(j).second);
      }
      Pl -= Mi;
      Mi = 0;
    }
    if (S < Pl) {
      S = Pl;
      for (int i = 0; i < D; i++) {
        ans.at(i) = t.at(i);
      }
    } else if (S >= Pl) {
      continue;
    }
  }
  for (int i = 0; i < D; i++) {
    cout << ans.at(i) << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  int D;
  vector<pair<int, int>> c(26);
  vector<vector<int>> s(D, vector<int>(26));
  vector<int> t(D);

  cin >> D;
  for (int i = 0; i < 26; i++) {
    cin >> c.at(i).first;
    c.at(i).second = 0;
  }

  for (int i = 0; i < D; i++) {
    for (int j = 0; j < 26; j++) {
      cin >> s.at(i).at(j);
    }
  }

  for (int i = 0; i < D; i++) {
    t.at(i) = rand() % 26 + 1;
  }

  int count = 0;
  int S = 0;
  vector<int> ans(D);
  while (count++ < 50000) {

    t.at((rand() % D + 1) - 1) = rand() % 26 + 1;

    int Pl = 0;
    int Mi = 0;

    for (int i = 0; i < D; i++) {
      Pl += s.at(i).at(t.at(i) - 1);
      c.at(t.at(i) - 1).second = i + 1;
      for (int j = 0; j < 26; j++) {
        Mi += c.at(j).first * (i + 1 - c.at(j).second);
      }
      Pl -= Mi;
      Mi = 0;
    }
    if (S < Pl) {
      S = Pl;
      for (int i = 0; i < D; i++) {
        ans.at(i) = t.at(i);
      }
    } else if (S >= Pl) {
      continue;
    }
  }
  for (int i = 0; i < D; i++) {
    cout << ans.at(i) << endl;
  }
}
Problem p02618 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int D;
  cin >> D;
  vector<pair<int, int>> c(26);
  vector<vector<int>> s(D, vector<int>(26));
  vector<int> t(D);

  for (int i = 0; i < 26; i++) {
    cin >> c.at(i).first;
    c.at(i).second = 0;
  }

  for (int i = 0; i < D; i++) {
    for (int j = 0; j < 26; j++) {
      cin >> s.at(i).at(j);
    }
  }

  for (int i = 0; i < D; i++) {
    t.at(i) = rand() % 26 + 1;
  }

  int count = 0;
  int S = 0;
  vector<int> ans(D);
  while (count++ < 500000) {

    t.at((rand() % D + 1) - 1) = rand() % 26 + 1;

    int Pl = 0;
    int Mi = 0;

    for (int i = 0; i < D; i++) {
      Pl += s.at(i).at(t.at(i) - 1);
      c.at(t.at(i) - 1).second = i + 1;
      for (int j = 0; j < 26; j++) {
        Mi += c.at(j).first * (i + 1 - c.at(j).second);
      }
      Pl -= Mi;
      Mi = 0;
    }
    if (S < Pl) {
      S = Pl;
      for (int i = 0; i < D; i++) {
        ans.at(i) = t.at(i);
      }
    } else if (S >= Pl) {
      continue;
    }
  }
  for (int i = 0; i < D; i++) {
    cout << ans.at(i) << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  for (int i = 1; i <= 365; i++) {
    cout << rand() % 26 + 1 << endl;
  }
}
Problem p02618 (C++)

Buggy submission (status: Runtime Error):
// Copyright 2020 yaito3014
#include <bits/stdc++.h>

int main() {
  using namespace std;
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  using namespace chrono_literals;
  chrono::high_resolution_clock clock;
  auto be = clock.now();
  int D;
  cin >> D;
  array<int, 26> c;
  for (auto &e : c)
    cin >> e;
  vector<array<int, 26>> d(D);
  for (auto &r : d)
    for (auto &e : r)
      cin >> e;
  auto eval = [&](vector<int> t) {
    int S = 0;
    array<int, 26> last = {};
    for (int i = 0; i < D; ++i) {
      S += d[i][t[i]];
      last[t[i]] = i + 1;
      for (int j = 0; j < 26; ++j)
        S -= c[j] * (i + 1 - last[j]);
    }
    return S;
  };

  mt19937 mt(random_device{}());
  uniform_int_distribution<int> dist_type(0, 25), dist_day(0, D - 1);
  vector<int> t(D);
  for (int i = 0; i < D; ++i)
    t[i] = distance(cbegin(d[i]), max_element(cbegin(d[i]), cend(d[i]))) + 1;
  int S = eval(t);
  int cnt = 0;
  while (clock.now() - be < 1900ms) {
    int day = dist_day(mt);
    int prv = t[day];
    t[day] = dist_type(mt);
    int score = eval(t);
    if (score > S) {
      S = score;
      continue;
    }
    t[day] = prv;
    if (++cnt > 10000) {
      cnt = 0;
      generate(begin(t), end(t), [&]() { return dist_type(mt); });
    }
  }
  for (const auto &e : t)
    cout << (e + 1) << '\n';
  cerr << eval(t);
}


Fixed submission:
// Copyright 2020 yaito3014
#include <bits/stdc++.h>

int main() {
  using namespace std;
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  using namespace chrono_literals;
  chrono::high_resolution_clock clock;
  auto be = clock.now();
  int D;
  cin >> D;
  array<int, 26> c;
  for (auto &e : c)
    cin >> e;
  vector<array<int, 26>> d(D);
  for (auto &r : d)
    for (auto &e : r)
      cin >> e;
  auto eval = [&](vector<int> t) {
    int S = 0;
    array<int, 26> last = {};
    for (int i = 0; i < D; ++i) {
      S += d[i][t[i]];
      last[t[i]] = i + 1;
      for (int j = 0; j < 26; ++j)
        S -= c[j] * (i + 1 - last[j]);
    }
    return S;
  };

  mt19937 mt(random_device{}());
  uniform_int_distribution<int> dist_type(0, 25), dist_day(0, D - 1);
  vector<int> t(D);
  for (int i = 0; i < D; ++i)
    t[i] = distance(cbegin(d[i]), max_element(cbegin(d[i]), cend(d[i]))) + 1;
  int S = eval(t);
  int cnt = 0;
  while (clock.now() - be < 1900ms) {
    int day = dist_day(mt);
    int prv = t[day];
    t[day] = dist_type(mt);
    int score = eval(t);
    if (score > S) {
      S = score;
      continue;
    }
    t[day] = prv;
    if (++cnt > 10000) {
      cnt = 0;
      generate(begin(t), end(t), [&]() { return dist_type(mt); });
    }
  }
  for (const auto &e : t)
    cout << (e + 1) << '\n';
}
Problem p02618 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  } else {
    return false;
  }
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  } else {
    return false;
  }
}
#define ll long long
#define double long double
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, n) for (int i = 1; i <= (n); i++)
#define mod (ll)(1e9 + 7)
#define inf (ll)(3e18 + 7)
#define pi (double)acos(-1.0)
#define P pair<int, int>
#define PiP pair<int, pair<int, int>>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
using namespace std;

int d;
vector<int> c(26);
vector<vector<int>> s(400, vector<int>(26));

int get_score(vector<int> t) {
  int ans = 0;
  vector<int> last(26, 0);
  rep(i, d) {
    ans += s[i][t[i]];
    rep(j, 26) {
      if (t[i] == j)
        last[j] = i + 1;
      ans -= c[j] * ((i + 1) - last[j]);
    }
  }
  return ans;
}

int main() {
  cin >> d;
  rep(i, 26) cin >> c[i];
  rep(i, d) rep(j, 26) cin >> s[i][j];
  //-----code------//
  vector<int> t(d, 0);
  int m;
  cin >> m;
  while ((clock() / CLOCKS_PER_SEC) < 1.9) {
    int D = rand() % d, q = rand() % 26;
    int old = t[D], olds = get_score(t);
    t[D] = q;
    if (get_score(t) < olds)
      t[D] = old;
  }
  rep(i, d) cout << t[i] + 1 << endl;
}


Fixed submission:
#include <bits/stdc++.h>
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  } else {
    return false;
  }
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  } else {
    return false;
  }
}
#define ll long long
#define double long double
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, n) for (int i = 1; i <= (n); i++)
#define mod (ll)(1e9 + 7)
#define inf (ll)(3e18 + 7)
#define pi (double)acos(-1.0)
#define P pair<int, int>
#define PiP pair<int, pair<int, int>>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
using namespace std;

int d;
vector<int> c(26);
vector<vector<int>> s(400, vector<int>(26));

int get_score(vector<int> t) {
  int ans = 0;
  vector<int> last(26, 0);
  rep(i, d) {
    ans += s[i][t[i]];
    rep(j, 26) {
      if (t[i] == j)
        last[j] = i + 1;
      ans -= c[j] * ((i + 1) - last[j]);
    }
  }
  return ans;
}

int main() {
  cin >> d;
  rep(i, 26) cin >> c[i];
  rep(i, d) rep(j, 26) cin >> s[i][j];
  //-----code------//
  vector<int> t(d, 0);
  int m;
  cin >> m;
  rep(i, 100000) {
    int D = rand() % d, q = rand() % 26;
    int old = t[D], olds = get_score(t);
    t[D] = q;
    if (get_score(t) < olds)
      t[D] = old;
  }
  rep(i, d) cout << t[i] + 1 << endl;
}
Problem p02618 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define repn(i, n) for (int i = 1; i <= n; i++)
#define repr(e, x) for (auto &e : x)
using namespace std;
typedef long long ll;
typedef long double ld;
// typedef pair<int,int> P;
int const INF = 1001001001;
ll const LINF = 1001001001001001001;
ll const MOD = 1000000007;

ll D;
ll c[26 + 1];
ll s[365 + 1][26 + 1];

ll last[365 + 1];

void input() {
  cin >> D;
  repn(i, 26) cin >> c[i];
  repn(i, D) repn(j, 26) cin >> s[i][j];
}

// 貪欲法2
void solve2() {
  ll v[27][366];
  ll max_score = -LINF;
  ll max_idx = 0;
  rep(k, 27) {
    ll score = 0;
    fill(last, last + 27, 0);
    repn(d, D + k) {
      ll maxi = -LINF;
      ll idx = 0;
      repn(i, 26) {
        ll res = 0;
        if (d <= D)
          res += s[d][i];
        repn(j, 26) {
          if (i == j)
            continue;
          res -= c[j] * (d - last[j]);
        }
        if (maxi < res) {
          maxi = res;
          idx = i;
        }
      }
      last[idx] = d;
      score += maxi;
      v[k][d] = idx;
    }
    if (max_score < score) {
      max_score = score;
      max_idx = k;
    }
  }

  repn(i, D) { cout << v[max_idx][i] << endl; }
}

int main() {
  input();
  solve2();
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define repn(i, n) for (int i = 1; i <= n; i++)
#define repr(e, x) for (auto &e : x)
using namespace std;
typedef long long ll;
typedef long double ld;
// typedef pair<int,int> P;
int const INF = 1001001001;
ll const LINF = 1001001001001001001;
ll const MOD = 1000000007;

ll D;
ll c[26 + 1];
ll s[365 + 1][26 + 1];

ll last[365 + 1];

void input() {
  cin >> D;
  repn(i, 26) cin >> c[i];
  repn(i, D) repn(j, 26) cin >> s[i][j];
}

// 貪欲法2
void solve2() {
  ll v[27][400];
  ll max_score = -LINF;
  ll max_idx = 0;
  rep(k, 27) {
    ll score = 0;
    fill(last, last + 27, 0);
    repn(d, D + k) {
      ll maxi = -LINF;
      ll idx = 0;
      repn(i, 26) {
        ll res = 0;
        if (d <= D)
          res += s[d][i];
        repn(j, 26) {
          if (i == j)
            continue;
          res -= c[j] * (d - last[j]);
        }
        if (maxi < res) {
          maxi = res;
          idx = i;
        }
      }
      last[idx] = d;
      score += maxi;
      v[k][d] = idx;
    }
    if (max_score < score) {
      max_score = score;
      max_idx = k;
    }
  }

  repn(i, D) { cout << v[max_idx][i] << endl; }
}

int main() {
  input();
  solve2();
}
Problem p02618 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); ++i)
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
using namespace std;
using ll = long long;
using P = pair<int, int>;
using Pl = pair<long long, long long>;
using veci = vector<int>;
using vecl = vector<long long>;
using vecveci = vector<vector<int>>;
using vecvecl = vector<vector<long long>>;

inline void InitRand() { srand((unsigned int)time(NULL)); }

int main() {
  InitRand();
  int D;
  cin >> D;
  veci C(26);
  vecveci S(D, veci(26));
  veci T(D);
  REP(i, 26) cin >> C[i];
  REP(i, D) REP(j, 26) cin >> S[i][j];
  REP(i, D) { T[i] = rand() % 26; }
  int M;
  M = 3 * 1e5;
  veci B(M), Q(M);
  REP(i, M) {
    B[i] = rand() % D;
    Q[i] = rand() % 26;
  }

  veci last_d(26);
  ll old_ans = 0;
  REP(i, D) {
    int d = i + 1;
    last_d[T[i]] = d;
    old_ans += S[i][T[i]];
    REP(j, 26) { old_ans -= C[j] * (d - last_d[j]); }
  }

  REP(i, M) {
    int old_p = T[B[i]];
    T[B[i]] = Q[i];
    ll score = 0;
    veci last(26);
    REP(j, D) {
      int d = j + 1;
      last[T[j]] = d;
      score += S[j][T[j]];
      REP(k, 26) { score -= C[k] * (d - last[k]); }
    }
    if (old_ans < score) {
      old_ans = score;
    } else {
      T[B[i]] = old_p;
    }
  }
  REP(i, D) cout << T[i] + 1 << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); ++i)
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
using namespace std;
using ll = long long;
using P = pair<int, int>;
using Pl = pair<long long, long long>;
using veci = vector<int>;
using vecl = vector<long long>;
using vecveci = vector<vector<int>>;
using vecvecl = vector<vector<long long>>;

inline void InitRand() { srand((unsigned int)time(NULL)); }

int main() {
  InitRand();
  int D;
  cin >> D;
  veci C(26);
  vecveci S(D, veci(26));
  veci T(D);
  REP(i, 26) cin >> C[i];
  REP(i, D) REP(j, 26) cin >> S[i][j];
  REP(i, D) { T[i] = rand() % 26; }
  int M;
  M = 1e5;
  veci B(M), Q(M);
  REP(i, M) {
    B[i] = rand() % D;
    Q[i] = rand() % 26;
  }

  veci last_d(26);
  ll old_ans = 0;
  REP(i, D) {
    int d = i + 1;
    last_d[T[i]] = d;
    old_ans += S[i][T[i]];
    REP(j, 26) { old_ans -= C[j] * (d - last_d[j]); }
  }

  REP(i, M) {
    int old_p = T[B[i]];
    T[B[i]] = Q[i];
    ll score = 0;
    veci last(26);
    REP(j, D) {
      int d = j + 1;
      last[T[j]] = d;
      score += S[j][T[j]];
      REP(k, 26) { score -= C[k] * (d - last[k]); }
    }
    if (old_ans < score) {
      old_ans = score;
    } else {
      T[B[i]] = old_p;
    }
  }
  REP(i, D) cout << T[i] + 1 << endl;
}
Problem p02618 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
const int F = 26;
const int def = 1e6;

int n;
vector<int> c(F);
vector<vector<int>> a;

void init() {
  cin >> n;
  a.resize(n, vector<int>(F));
  for (int i = 0; i < F; i++)
    cin >> c[i];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < F; j++)
      cin >> a[i][j];
}

vector<int> calc(int rng) {
  vector<int> last(F, -1);
  vector<int> ans;
  for (int i = 0; i < n; i++) {
    vector<int> cur = a[i];
    for (int j = 0; j < F; j++)
      cur[j] += (i - last[j]) * (c[j] + rng);
    int ind = max_element(cur.begin(), cur.end()) - cur.begin();
    last[ind] = i;
    ans.emplace_back(ind);
  }
  return ans;
}

int chack(vector<int> p) {
  int sum = 0;
  vector<int> last(F, -1);
  for (int i = 0; i < n; i++) {
    sum += a[i][p[i]];
    last[p[i]] = i;
    for (int j = 0; j < F; j++)
      sum -= (i - last[j]) * c[j];
  }
  return sum;
}

int main() {
  srand((unsigned)time(NULL));
  init();

  vector<int> ans;
  int M = 4 * 1e4, nax = -1e9;
  for (int I = 1; I <= M; I++) {
    int sed = rand() % (rand() % ((I / 50) + 1)) * ((rand() % 2) ? 1 : -1);
    vector<int> cur = calc(sed);
    int k = chack(cur);
    if (k > nax) {
      nax = k;
      ans = cur;
    }
  }

  for (auto i : ans)
    cout << i + 1 << endl;
}

/*
satisfaction: 424801
score: 1424801
*/


Fixed submission:
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
const int F = 26;
const int def = 1e6;

int n;
vector<int> c(F);
vector<vector<int>> a;

void init() {
  cin >> n;
  a.resize(n, vector<int>(F));
  for (int i = 0; i < F; i++)
    cin >> c[i];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < F; j++)
      cin >> a[i][j];
}

vector<int> calc(int rng) {
  vector<int> last(F, -1);
  vector<int> ans;
  for (int i = 0; i < n; i++) {
    vector<int> cur = a[i];
    for (int j = 0; j < F; j++)
      cur[j] += (i - last[j]) * (c[j] + rng);
    int ind = max_element(cur.begin(), cur.end()) - cur.begin();
    last[ind] = i;
    ans.emplace_back(ind);
  }
  return ans;
}

int chack(vector<int> p) {
  int sum = 0;
  vector<int> last(F, -1);
  for (int i = 0; i < n; i++) {
    sum += a[i][p[i]];
    last[p[i]] = i;
    for (int j = 0; j < F; j++)
      sum -= (i - last[j]) * c[j];
  }
  return sum;
}

int main() {
  srand((unsigned)time(NULL));
  init();

  vector<int> ans;
  int M = 4 * 1e4, nax = -1e9;
  for (int I = 1; I <= M; I++) {
    int sed = rand() % (rand() % ((I / 50) + 1) + 1) * ((rand() % 2) ? 1 : -1);
    vector<int> cur = calc(sed);
    int k = chack(cur);
    if (k > nax) {
      nax = k;
      ans = cur;
    }
  }

  for (auto i : ans)
    cout << i + 1 << endl;
}

/*
satisfaction: 424801
score: 1424801
*/
Problem p02618 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
#define MOD 1000000007
#define f(i, n) for (int i = 0; i < int(n); i++)
#define N 2750
int a[N][27];
int a2[N][27];
int e[N][365];
int v[N][365];
int sz, sz2;

int main() {
  int n;
  int x, y;
  vector<pair<int, pair<int, int>>> b;
  int c[26];
  int d[365][26];
  int ans[365];
  scanf("%d", &n);
  f(i, 26) scanf("%d", &c[i]);
  f(i, n) { f(j, 26) scanf("%d", &d[i][j]); }
  sz = 1;
  f(i, 26) a[0][i] = -1;
  a[0][26] = 0;
  f(t, n) {
    b.clear();
    f(i, sz) {
      x = a[i][26];
      f(j, 26) { x -= ((t - a[i][j]) * c[j]); }
      f(j, 26) {
        y = x + d[t][j];
        y += ((t - a[i][j]) * c[j]);
        b.push_back(make_pair(y, make_pair(i, j)));
      }
    }
    sort(b.begin(), b.end(), greater<pair<int, pair<int, int>>>());
    sz2 = b.size();
    if (sz2 > N)
      sz2 = N;
    f(i, sz2) {
      x = b[i].second.first;
      y = b[i].second.second;
      a2[i][26] = b[i].first;
      f(j, 26) a2[i][j] = a[x][j];
      a2[i][y] = t;
      e[i][t] = x;
      v[i][t] = y;
    }
    f(i, sz2) { f(j, 27) a[i][j] = a2[i][j]; }
    sz = sz2;
  }
  x = 0;
  for (int i = n - 1; i >= 0; i--) {
    ans[i] = v[x][i];
    x = e[x][i];
  }
  f(i, n) printf("%d\n", ans[i] + 1);

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
#define MOD 1000000007
#define f(i, n) for (int i = 0; i < int(n); i++)
#define N 2600
int a[N][27];
int a2[N][27];
int e[N][365];
int v[N][365];
int sz, sz2;

int main() {
  int n;
  int x, y;
  vector<pair<int, pair<int, int>>> b;
  int c[26];
  int d[365][26];
  int ans[365];
  scanf("%d", &n);
  f(i, 26) scanf("%d", &c[i]);
  f(i, n) { f(j, 26) scanf("%d", &d[i][j]); }
  sz = 1;
  f(i, 26) a[0][i] = -1;
  a[0][26] = 0;
  f(t, n) {
    b.clear();
    f(i, sz) {
      x = a[i][26];
      f(j, 26) { x -= ((t - a[i][j]) * c[j]); }
      f(j, 26) {
        y = x + d[t][j];
        y += ((t - a[i][j]) * c[j]);
        b.push_back(make_pair(y, make_pair(i, j)));
      }
    }
    sort(b.begin(), b.end(), greater<pair<int, pair<int, int>>>());
    sz2 = b.size();
    if (sz2 > N)
      sz2 = N;
    f(i, sz2) {
      x = b[i].second.first;
      y = b[i].second.second;
      a2[i][26] = b[i].first;
      f(j, 26) a2[i][j] = a[x][j];
      a2[i][y] = t;
      e[i][t] = x;
      v[i][t] = y;
    }
    f(i, sz2) { f(j, 27) a[i][j] = a2[i][j]; }
    sz = sz2;
  }
  x = 0;
  for (int i = n - 1; i >= 0; i--) {
    ans[i] = v[x][i];
    x = e[x][i];
  }
  f(i, n) printf("%d\n", ans[i] + 1);

  return 0;
}
Problem p02618 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <set>
#include <vector>
using namespace std;
typedef long long ll;

int main() {
  ll d, c[26], s[365][26], t[365], ans = 0, m, x, y, v;
  set<ll> se[26];
  srand(time(NULL));
  cin >> d;
  for (int i = 0; i < 26; i++) {
    cin >> c[i];
    se[i].insert(0);
  }
  for (int i = 0; i < d; i++) {
    for (int j = 0; j < 26; j++) {
      cin >> s[i][j];
    }
  }
  for (int i = 0; i < d; i++) {
    t[i] = i % 26;
    se[t[i]].insert(i + 1);
    ans += s[i][t[i]];
    for (int j = 0; j < 26; j++) {
      ans -= (i + 1 - *se[j].rbegin()) * c[j];
    }
  }
  for (int i = 0; i < 26; i++) {
    se[i].insert(d + 1);
  }
  for (int i = 0; i < 20000000; i++) {
    x = rand() % d + 1;
    y = rand() % 26;
    v = 0LL;
    ans += s[x - 1][y] - s[x - 1][t[x - 1]];
    set<ll>::iterator it = se[t[x - 1]].find(x);
    it--;
    ll z = *se[t[x - 1]].upper_bound(x);
    v -= c[t[x - 1]] * (z - x) * (x - *it);
    it = se[y].lower_bound(x);
    it--;
    v += c[y] * (*se[y].lower_bound(x) - x) * (x - *it);
    if (v > 0LL) {
      ans += v;
      se[t[x - 1]].erase(x);
      t[x - 1] = y;
      se[y].insert(x);
    }
  }
  for (int i = 0; i < d; i++) {
    cout << t[i] + 1 << endl;
  }
}

Fixed submission:
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <set>
#include <vector>
using namespace std;
typedef long long ll;

int main() {
  ll d, c[26], s[365][26], t[365], ans = 0, m, x, y, v;
  set<ll> se[26];
  srand(time(NULL));
  cin >> d;
  for (int i = 0; i < 26; i++) {
    cin >> c[i];
    se[i].insert(0);
  }
  for (int i = 0; i < d; i++) {
    for (int j = 0; j < 26; j++) {
      cin >> s[i][j];
    }
  }
  for (int i = 0; i < d; i++) {
    t[i] = i % 26;
    se[t[i]].insert(i + 1);
    ans += s[i][t[i]];
    for (int j = 0; j < 26; j++) {
      ans -= (i + 1 - *se[j].rbegin()) * c[j];
    }
  }
  for (int i = 0; i < 26; i++) {
    se[i].insert(d + 1);
  }
  for (int i = 0; i < 20000000; i++) {
    x = i % d + 1;
    y = i % 26;
    v = 0LL;
    ans += s[x - 1][y] - s[x - 1][t[x - 1]];
    set<ll>::iterator it = se[t[x - 1]].find(x);
    it--;
    ll z = *se[t[x - 1]].upper_bound(x);
    v -= c[t[x - 1]] * (z - x) * (x - *it);
    it = se[y].lower_bound(x);
    it--;
    v += c[y] * (*se[y].lower_bound(x) - x) * (x - *it);
    if (v > 0LL) {
      ans += v;
      se[t[x - 1]].erase(x);
      t[x - 1] = y;
      se[y].insert(x);
    }
  }
  for (int i = 0; i < d; i++) {
    cout << t[i] + 1 << endl;
  }
}
Problem p02618 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int d, ans[365], c[26], s[365][26], last[26], tlast[26];
const int INF = 1e9;

#define REP(i, n) for (int i = 0; i < n; i++)

void FIN(int a) {
  if (a) {
    ofstream fout("output.txt");
    REP(i, d) fout << ans[i] << "\n";
    fout.close();
    return;
  }
  REP(i, d) cout << ans[i] << "\n";
  return;
}

int score() {
  int res = 0;
  memset(tlast, -1, sizeof(tlast));
  REP(day, d) {
    res += s[day][ans[day] - 1];
    tlast[ans[day] - 1] = day;
    REP(i, 26) res -= c[i] * (day - tlast[i]);
  }
  return res;
}
void swap() {
  int day = rand() % 362, diff = rand() % 3, nowscore = score();
  swap(ans[day], ans[day + diff]);
  if (score() < nowscore)
    swap(ans[day], ans[day + diff]);
}
void onechange() {
  int day = rand() % 365, nowscore = score();
  REP(i, 26) {
    int tmp = ans[day];
    ans[day] = i + 1;
    if (score() < nowscore)
      ans[day] = tmp;
    else
      nowscore = score();
  }
}

int main() {
  memset(last, -1, sizeof(last));
  cin >> d;
  REP(i, 26) cin >> c[i];
  REP(i, d) REP(j, 26) cin >> s[i][j];
  REP(day, d) {
    int idx, mx = -INF;
    REP(i, 26) {
      int A = c[i] * (day - last[i]) + s[day][i];
      if (mx < A)
        mx = A, idx = i;
    }
    ans[day] = idx + 1, last[idx] = day;
  }
  REP(_, 90) {
    REP(__, 100) swap();
    REP(__, 100) onechange();
  }
  FIN(0);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int d, ans[365], c[26], s[365][26], last[26], tlast[26];
const int INF = 1e9;

#define REP(i, n) for (int i = 0; i < n; i++)

void FIN(int a) {
  if (a) {
    ofstream fout("output.txt");
    REP(i, d) fout << ans[i] << "\n";
    fout.close();
    return;
  }
  REP(i, d) cout << ans[i] << "\n";
  return;
}

int score() {
  int res = 0;
  memset(tlast, -1, sizeof(tlast));
  REP(day, d) {
    res += s[day][ans[day] - 1];
    tlast[ans[day] - 1] = day;
    REP(i, 26) res -= c[i] * (day - tlast[i]);
  }
  return res;
}
void swap() {
  int day = rand() % 362, diff = rand() % 3, nowscore = score();
  swap(ans[day], ans[day + diff]);
  if (score() < nowscore)
    swap(ans[day], ans[day + diff]);
}
void onechange() {
  int day = rand() % 365, nowscore = score();
  REP(i, 26) {
    int tmp = ans[day];
    ans[day] = i + 1;
    if (score() < nowscore)
      ans[day] = tmp;
    else
      nowscore = score();
  }
}

int main() {
  memset(last, -1, sizeof(last));
  cin >> d;
  REP(i, 26) cin >> c[i];
  REP(i, d) REP(j, 26) cin >> s[i][j];
  REP(day, d) {
    int idx, mx = -INF;
    REP(i, 26) {
      int A = c[i] * (day - last[i]) + s[day][i];
      if (mx < A)
        mx = A, idx = i;
    }
    ans[day] = idx + 1, last[idx] = day;
  }
  REP(_, 80) {
    REP(__, 100) swap();
    REP(__, 100) onechange();
  }
  FIN(0);
}
Problem p02618 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, a, b) for (ll i = (a); i < (b); i++)
#define REP(i, n) rep(i, 0, n)

int cd = 350000;

int d, c[26], s[366][26];
int t[366];
ll r;

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

void greedy() { REP(i, d) REP(j, 26) t[i] = s[i][j] > s[i][t[i]] ? j : t[i]; }

ll score(int *t) {
  ll ans = 0;
  vector<int> cnt(26, 0);
  REP(i, d) {
    cnt[t[i]] = -1;
    ans += s[i][t[i]];
    REP(j, 26) cnt[j]++, ans -= cnt[j] * c[j];
  }
  return ans;
}

void crt_new() {
  int idx = rand() % d;
  int q = rand() % 26;
  while (q == t[idx])
    q = rand() % 26;
  int tmp = t[idx];
  t[idx] = q;
  ll res = score(t);
  if (res > r)
    r = res;
  else
    t[idx] = tmp;
}

void solve() {

  init();
  // ll begin_t=clock();
  greedy();
  r = score(t);
  while (cd--)
    crt_new();
  // REP(i,d)cout<<t[i]<<" ";cout<<endl;
  REP(i, d) cout << t[i] + 1 << endl;
  // cout<<r<<endl;

  // ll end_t=clock();cout<<end_t-begin_t<<"ms"<<endl;
}

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

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, a, b) for (ll i = (a); i < (b); i++)
#define REP(i, n) rep(i, 0, n)

int cd = 20000;

int d, c[26], s[366][26];
int t[366];
ll r;

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

void greedy() { REP(i, d) REP(j, 26) t[i] = s[i][j] > s[i][t[i]] ? j : t[i]; }

ll score(int *t) {
  ll ans = 0;
  vector<int> cnt(26, 0);
  REP(i, d) {
    cnt[t[i]] = -1;
    ans += s[i][t[i]];
    REP(j, 26) cnt[j]++, ans -= cnt[j] * c[j];
  }
  return ans;
}

void crt_new() {
  int idx = rand() % d;
  int q = rand() % 26;
  while (q == t[idx])
    q = rand() % 26;
  int tmp = t[idx];
  t[idx] = q;
  ll res = score(t);
  if (res > r)
    r = res;
  else
    t[idx] = tmp;
}

void solve() {

  init();
  // ll begin_t=clock();
  greedy();
  r = score(t);
  while (cd--)
    crt_new();
  // REP(i,d)cout<<t[i]<<" ";cout<<endl;
  REP(i, d) cout << t[i] + 1 << endl;
  // cout<<r<<endl;

  // ll end_t=clock();cout<<end_t-begin_t<<"ms"<<endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  solve();
  return 0;
}
Problem p02618 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 1; i < (int)(n); i++)
int D, c[27], s[366][27];

int sco(vector<int> &t, int k, int x) {
  int S = 0, L[27] = {};
  REP(i, D) {
    if (i == k) {
      S += s[i][x];
      L[x] = i;
    } else {
      S += s[i][t[i]];
      L[t[i]] = i;
    }
    REP(j, 27) { S -= c[j] * (i - L[j]); }
  }
  return S;
}

int saga(int d, vector<int> &L) {
  int S = 0;
  REP(i, 27) { S += c[i] * (d - L[i]); }
  return S;
}

int main() {
  cin >> D;
  D++;
  REP(i, 27) { cin >> c[i]; }
  REP(i, D) {
    REP(j, 27) { cin >> s[i][j]; }
  }
  vector<int> t(D), last(27, 0);

  REP(i, D) {
    int a = 1, S = -1000000, u = 0;
    REP(j, 27) {
      u = s[i][j];
      u -= saga(i, last);
      u += c[j] * (i - last[j]);
      if (S < u) {
        a = j;
        S = u;
      }
    }
    t[i] = a;
    last[a] = i;
  }
  int S = sco(t, 0, 0), T = 0, n = 0;
  REP(i, D) {
    REP(j, 27) {
      T = sco(t, i, j);
      if (S < T) {
        t[i] = j;
        S = T;
        if (n <= 140) {
          i = 1;
          n++;
          break;
        }
      }
    }
  }

  REP(i, D) { cout << t[i] << "\n"; }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 1; i < (int)(n); i++)
int D, c[27], s[366][27];

int sco(vector<int> &t, int k, int x) {
  int S = 0, L[27] = {};
  REP(i, D) {
    if (i == k) {
      S += s[i][x];
      L[x] = i;
    } else {
      S += s[i][t[i]];
      L[t[i]] = i;
    }
    REP(j, 27) { S -= c[j] * (i - L[j]); }
  }
  return S;
}

int saga(int d, vector<int> &L) {
  int S = 0;
  REP(i, 27) { S += c[i] * (d - L[i]); }
  return S;
}

int main() {
  cin >> D;
  D++;
  REP(i, 27) { cin >> c[i]; }
  REP(i, D) {
    REP(j, 27) { cin >> s[i][j]; }
  }
  vector<int> t(D), last(27, 0);

  REP(i, D) {
    int a = 1, S = -1000000, u = 0;
    REP(j, 27) {
      u = s[i][j];
      u -= saga(i, last);
      u += c[j] * (i - last[j]);
      if (S < u) {
        a = j;
        S = u;
      }
    }
    t[i] = a;
    last[a] = i;
  }
  int S = sco(t, 0, 0), T = 0, n = 0;
  REP(i, D) {
    REP(j, 27) {
      T = sco(t, i, j);
      if (S < T) {
        t[i] = j;
        S = T;
        if (n <= 125) {
          i = 1;
          n++;
          break;
        }
      }
    }
  }

  REP(i, D) { cout << t[i] << "\n"; }
}
Problem p02618 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 1; i < (int)(n); i++)
int D, c[27], s[366][27];

int sco(vector<int> &t, int k, int x) {
  int S = 0, L[27] = {};
  REP(i, D) {
    if (i == k) {
      S += s[i][x];
      L[x] = i;
    } else {
      S += s[i][t[i]];
      L[t[i]] = i;
    }
    REP(j, 27) { S -= c[j] * (i - L[j]); }
  }
  return S;
}

int saga(int d, vector<int> &L) {
  int S = 0;
  REP(i, 27) { S += c[i] * (d - L[i]); }
  return S;
}

int main() {
  cin >> D;
  D++;
  REP(i, 27) { cin >> c[i]; }
  REP(i, D) {
    REP(j, 27) { cin >> s[i][j]; }
  }
  vector<int> t(D), last(27, 0);

  REP(i, D) {
    int a = 1, S = -1000000, u = 0, m = 0;
    REP(j, 27) {
      u = s[i][j];
      u -= saga(i, last);
      u += c[j] * (i - last[j]);
      if (S < u) {
        a = j;
        S = u;
      }
    }
    t[i] = a;
    last[a] = i;
  }
  int S = sco(t, 0, 0), T = 0, n = 0;
  REP(i, D) {
    REP(j, 27) {
      T = sco(t, i, j);
      if (S < T) {
        t[i] = j;
        S = T;
        if (n <= 10000) {
          i = 1;
          n++;
          break;
        }
      }
    }
  }

  REP(i, D) { cout << t[i] << "\n"; }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 1; i < (int)(n); i++)
int D, c[27], s[366][27];

int sco(vector<int> &t, int k, int x) {
  int S = 0, L[27] = {};
  REP(i, D) {
    if (i == k) {
      S += s[i][x];
      L[x] = i;
    } else {
      S += s[i][t[i]];
      L[t[i]] = i;
    }
    REP(j, 27) { S -= c[j] * (i - L[j]); }
  }
  return S;
}

int saga(int d, vector<int> &L) {
  int S = 0;
  REP(i, 27) { S += c[i] * (d - L[i]); }
  return S;
}

int main() {
  cin >> D;
  D++;
  REP(i, 27) { cin >> c[i]; }
  REP(i, D) {
    REP(j, 27) { cin >> s[i][j]; }
  }
  vector<int> t(D), last(27, 0);

  REP(i, D) {
    int a = 1, S = -1000000, u = 0, m = 0;
    REP(j, 27) {
      u = s[i][j];
      u -= saga(i, last);
      u += c[j] * (i - last[j]);
      if (S < u) {
        a = j;
        S = u;
      }
    }
    t[i] = a;
    last[a] = i;
  }
  int S = sco(t, 0, 0), T = 0, n = 0;
  REP(i, D) {
    REP(j, 27) {
      T = sco(t, i, j);
      if (S < T) {
        t[i] = j;
        S = T;
        if (n <= 100) {
          i = 1;
          n++;
          break;
        }
      }
    }
  }

  REP(i, D) { cout << t[i] << "\n"; }
}
Problem p02618 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int d;
  // int n=0;//
  cin >> d;
  int c[26];
  int s[d][26];
  for (int i = 0; i < 26; i++) {
    cin >> c[i];
    // n+=c[i];//
  }
  for (int i = 0; i < d; i++) {
    for (int ii = 0; ii < 26; ii++) {
      cin >> s[i][ii];
    }
  }
  long long int la[26];
  int lad[26];
  for (int i = 0; i < 26; i++) {
    lad[i] = -1;
  }
  int mm, md;
  // long long int m=0;//満足度確認用
  for (int i = 0; i < d; i++) {
    md = 0;
    // la[i]=(n-c[0])*(-i-1)+s[i][0];//
    la[i] = s[i][0];
    for (int ii = 1; ii < 26; ii++) {
      la[i] -= (i - lad[ii]) * c[ii];
    }
    for (int ii = 1; ii < 26; ii++) {
      // mm=(n-c[ii])*(-i-1)+s[i][ii];//
      mm = s[i][ii];
      for (int iii = 0; iii < 26; iii++) {
        if (ii != iii)
          mm -= (i - lad[iii]) * c[iii];
      }
      if ((la[i] < mm) || (la[i] == mm && c[md] < c[ii])) {
        la[i] = mm;
        md = ii;
      }
    }
    lad[md] = i;
    cout << md + 1 << endl;
    // m+=la[i];//満足度確認用
    // cout<<m<<endl;//満足度確認用
  }
  // cout<<m<<endl;//満足度確認用
  // cout<<n<<endl;//
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int d;
  // int n=0;//
  cin >> d;
  int c[26];
  int s[d][26];
  for (int i = 0; i < 26; i++) {
    cin >> c[i];
    // n+=c[i];//
  }
  for (int i = 0; i < d; i++) {
    for (int ii = 0; ii < 26; ii++) {
      cin >> s[i][ii];
    }
  }
  long long int la[d];
  int lad[26];
  for (int i = 0; i < 26; i++) {
    lad[i] = -1;
  }
  int mm, md;
  // long long int m=0;//満足度確認用
  for (int i = 0; i < d; i++) {
    md = 0;
    // la[i]=(n-c[0])*(-i-1)+s[i][0];//
    la[i] = s[i][0];
    for (int ii = 1; ii < 26; ii++) {
      la[i] -= (i - lad[ii]) * c[ii];
    }
    for (int ii = 1; ii < 26; ii++) {
      // mm=(n-c[ii])*(-i-1)+s[i][ii];//
      mm = s[i][ii];
      for (int iii = 0; iii < 26; iii++) {
        if (ii != iii)
          mm -= (i - lad[iii]) * c[iii];
      }
      if ((la[i] < mm) || (la[i] == mm && c[md] < c[ii])) {
        la[i] = mm;
        md = ii;
      }
    }
    lad[md] = i;
    cout << md + 1 << endl;
    // m+=la[i];//満足度確認用
    // cout<<m<<endl;//満足度確認用
  }
  // cout<<m<<endl;//満足度確認用
  // cout<<n<<endl;//
}
Problem p02618 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
using namespace std;
int main() {
  int D;
  scanf("%d", &D);
  int c[26], s[D][26], last[26], ans[D];
  for (int i = 0; i < 26; ++i)
    scanf("%d", &c[i]);
  for (int i = 0; i < D; ++i) {
    for (int j = 0; j < 26; ++j)
      scanf("%d", &s[i][j]);
  }
  fill_n(last, 26, -1);
  for (int i = 0; i < D; ++i) {
    int x = -1000000000, opt = 0;
    for (int j = 0; j < 26; ++j) {
      if (c[j] * (i - last[j]) + s[i][j] > x)
        x = c[j] * (i - last[j]) + s[i][j], opt = i;
    }
    last[opt] = i;
    ans[i] = opt;
  }
  for (int i = 0; i < D; ++i)
    printf("%d\n", ans[i] + 1);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
using namespace std;
int main() {
  int D;
  scanf("%d", &D);
  int c[26], s[D][26], last[26], ans[D];
  for (int i = 0; i < 26; ++i)
    scanf("%d", &c[i]);
  for (int i = 0; i < D; ++i) {
    for (int j = 0; j < 26; ++j)
      scanf("%d", &s[i][j]);
  }
  fill_n(last, 26, -1);
  for (int i = 0; i < D; ++i) {
    int x = -1000000000, opt = 0;
    for (int j = 0; j < 26; ++j) {
      if (c[j] * (i - last[j]) + s[i][j] > x)
        x = c[j] * (i - last[j]) + s[i][j], opt = j;
    }
    last[opt] = i;
    ans[i] = opt;
  }
  for (int i = 0; i < D; ++i)
    printf("%d\n", ans[i] + 1);
  return 0;
}